]>
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" |
0a02ad93 | 16 | |
4b6ab94e | 17 | #include <subcmd/parse-options.h> |
b1ffe8f3 | 18 | #include "util/trace-event.h" |
0a02ad93 | 19 | |
0a02ad93 IM |
20 | #include "util/debug.h" |
21 | ||
b1ffe8f3 | 22 | #include <sys/prctl.h> |
7b78f136 | 23 | #include <sys/resource.h> |
0a02ad93 | 24 | |
b1ffe8f3 IM |
25 | #include <semaphore.h> |
26 | #include <pthread.h> | |
27 | #include <math.h> | |
cb06ac25 | 28 | #include <api/fs/fs.h> |
419ab0d6 | 29 | |
b1ffe8f3 IM |
30 | #define PR_SET_NAME 15 /* Set process name */ |
31 | #define MAX_CPUS 4096 | |
b1ffe8f3 IM |
32 | #define COMM_LEN 20 |
33 | #define SYM_LEN 129 | |
a35e27d0 | 34 | #define MAX_PID 1024000 |
ec156764 | 35 | |
39aeb52f | 36 | struct sched_atom; |
ec156764 | 37 | |
b1ffe8f3 IM |
38 | struct task_desc { |
39 | unsigned long nr; | |
40 | unsigned long pid; | |
41 | char comm[COMM_LEN]; | |
ec156764 | 42 | |
b1ffe8f3 IM |
43 | unsigned long nr_events; |
44 | unsigned long curr_event; | |
39aeb52f | 45 | struct sched_atom **atoms; |
b1ffe8f3 IM |
46 | |
47 | pthread_t thread; | |
48 | sem_t sleep_sem; | |
ec156764 | 49 | |
b1ffe8f3 IM |
50 | sem_t ready_for_work; |
51 | sem_t work_done_sem; | |
52 | ||
53 | u64 cpu_usage; | |
54 | }; | |
55 | ||
56 | enum sched_event_type { | |
57 | SCHED_EVENT_RUN, | |
58 | SCHED_EVENT_SLEEP, | |
59 | SCHED_EVENT_WAKEUP, | |
55ffb7a6 | 60 | SCHED_EVENT_MIGRATION, |
b1ffe8f3 IM |
61 | }; |
62 | ||
39aeb52f | 63 | struct sched_atom { |
b1ffe8f3 | 64 | enum sched_event_type type; |
eed05fe7 | 65 | int specific_wait; |
b1ffe8f3 IM |
66 | u64 timestamp; |
67 | u64 duration; | |
68 | unsigned long nr; | |
b1ffe8f3 IM |
69 | sem_t *wait_sem; |
70 | struct task_desc *wakee; | |
71 | }; | |
72 | ||
e936e8e4 | 73 | #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP" |
b1ffe8f3 IM |
74 | |
75 | enum thread_state { | |
76 | THREAD_SLEEPING = 0, | |
77 | THREAD_WAIT_CPU, | |
78 | THREAD_SCHED_IN, | |
79 | THREAD_IGNORE | |
80 | }; | |
81 | ||
82 | struct work_atom { | |
83 | struct list_head list; | |
84 | enum thread_state state; | |
aa1ab9d2 | 85 | u64 sched_out_time; |
b1ffe8f3 IM |
86 | u64 wake_up_time; |
87 | u64 sched_in_time; | |
88 | u64 runtime; | |
89 | }; | |
90 | ||
39aeb52f | 91 | struct work_atoms { |
92 | struct list_head work_list; | |
b1ffe8f3 IM |
93 | struct thread *thread; |
94 | struct rb_node node; | |
95 | u64 max_lat; | |
3786310a | 96 | u64 max_lat_at; |
b1ffe8f3 IM |
97 | u64 total_lat; |
98 | u64 nb_atoms; | |
99 | u64 total_runtime; | |
2f80dd44 | 100 | int num_merged; |
b1ffe8f3 IM |
101 | }; |
102 | ||
39aeb52f | 103 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); |
b1ffe8f3 | 104 | |
9ec3f4e4 | 105 | struct perf_sched; |
0e9b07e5 | 106 | |
9ec3f4e4 ACM |
107 | struct trace_sched_handler { |
108 | int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel, | |
109 | struct perf_sample *sample, struct machine *machine); | |
0e9b07e5 | 110 | |
9ec3f4e4 ACM |
111 | int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
112 | struct perf_sample *sample, struct machine *machine); | |
0e9b07e5 | 113 | |
9ec3f4e4 ACM |
114 | int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
115 | struct perf_sample *sample, struct machine *machine); | |
0e9b07e5 | 116 | |
cb627505 DA |
117 | /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */ |
118 | int (*fork_event)(struct perf_sched *sched, union perf_event *event, | |
119 | struct machine *machine); | |
0e9b07e5 ACM |
120 | |
121 | int (*migrate_task_event)(struct perf_sched *sched, | |
9ec3f4e4 ACM |
122 | struct perf_evsel *evsel, |
123 | struct perf_sample *sample, | |
124 | struct machine *machine); | |
0e9b07e5 ACM |
125 | }; |
126 | ||
a151a37a | 127 | #define COLOR_PIDS PERF_COLOR_BLUE |
cf294f24 | 128 | #define COLOR_CPUS PERF_COLOR_BG_RED |
a151a37a | 129 | |
99623c62 JO |
130 | struct perf_sched_map { |
131 | DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS); | |
132 | int *comp_cpus; | |
133 | bool comp; | |
a151a37a JO |
134 | struct thread_map *color_pids; |
135 | const char *color_pids_str; | |
cf294f24 JO |
136 | struct cpu_map *color_cpus; |
137 | const char *color_cpus_str; | |
73643bb6 JO |
138 | struct cpu_map *cpus; |
139 | const char *cpus_str; | |
99623c62 JO |
140 | }; |
141 | ||
0e9b07e5 ACM |
142 | struct perf_sched { |
143 | struct perf_tool tool; | |
0e9b07e5 ACM |
144 | const char *sort_order; |
145 | unsigned long nr_tasks; | |
cb06ac25 | 146 | struct task_desc **pid_to_task; |
0e9b07e5 ACM |
147 | struct task_desc **tasks; |
148 | const struct trace_sched_handler *tp_handler; | |
149 | pthread_mutex_t start_work_mutex; | |
150 | pthread_mutex_t work_done_wait_mutex; | |
151 | int profile_cpu; | |
152 | /* | |
153 | * Track the current task - that way we can know whether there's any | |
154 | * weird events, such as a task being switched away that is not current. | |
155 | */ | |
156 | int max_cpu; | |
157 | u32 curr_pid[MAX_CPUS]; | |
158 | struct thread *curr_thread[MAX_CPUS]; | |
159 | char next_shortname1; | |
160 | char next_shortname2; | |
161 | unsigned int replay_repeat; | |
162 | unsigned long nr_run_events; | |
163 | unsigned long nr_sleep_events; | |
164 | unsigned long nr_wakeup_events; | |
165 | unsigned long nr_sleep_corrections; | |
166 | unsigned long nr_run_events_optimized; | |
167 | unsigned long targetless_wakeups; | |
168 | unsigned long multitarget_wakeups; | |
169 | unsigned long nr_runs; | |
170 | unsigned long nr_timestamps; | |
171 | unsigned long nr_unordered_timestamps; | |
0e9b07e5 ACM |
172 | unsigned long nr_context_switch_bugs; |
173 | unsigned long nr_events; | |
174 | unsigned long nr_lost_chunks; | |
175 | unsigned long nr_lost_events; | |
176 | u64 run_measurement_overhead; | |
177 | u64 sleep_measurement_overhead; | |
178 | u64 start_time; | |
179 | u64 cpu_usage; | |
180 | u64 runavg_cpu_usage; | |
181 | u64 parent_cpu_usage; | |
182 | u64 runavg_parent_cpu_usage; | |
183 | u64 sum_runtime; | |
184 | u64 sum_fluct; | |
185 | u64 run_avg; | |
186 | u64 all_runtime; | |
187 | u64 all_count; | |
188 | u64 cpu_last_switched[MAX_CPUS]; | |
2f80dd44 | 189 | struct rb_root atom_root, sorted_atom_root, merged_atom_root; |
0e9b07e5 | 190 | struct list_head sort_list, cmp_pid; |
939cda52 | 191 | bool force; |
2f80dd44 | 192 | bool skip_merge; |
99623c62 | 193 | struct perf_sched_map map; |
0e9b07e5 | 194 | }; |
b1ffe8f3 IM |
195 | |
196 | static u64 get_nsecs(void) | |
ec156764 IM |
197 | { |
198 | struct timespec ts; | |
199 | ||
200 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
201 | ||
202 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; | |
203 | } | |
204 | ||
0e9b07e5 | 205 | static void burn_nsecs(struct perf_sched *sched, u64 nsecs) |
ec156764 | 206 | { |
b1ffe8f3 | 207 | u64 T0 = get_nsecs(), T1; |
ec156764 IM |
208 | |
209 | do { | |
210 | T1 = get_nsecs(); | |
0e9b07e5 | 211 | } while (T1 + sched->run_measurement_overhead < T0 + nsecs); |
ec156764 IM |
212 | } |
213 | ||
b1ffe8f3 | 214 | static void sleep_nsecs(u64 nsecs) |
ec156764 IM |
215 | { |
216 | struct timespec ts; | |
217 | ||
218 | ts.tv_nsec = nsecs % 999999999; | |
219 | ts.tv_sec = nsecs / 999999999; | |
220 | ||
221 | nanosleep(&ts, NULL); | |
222 | } | |
223 | ||
0e9b07e5 | 224 | static void calibrate_run_measurement_overhead(struct perf_sched *sched) |
ec156764 | 225 | { |
b1ffe8f3 | 226 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
ec156764 IM |
227 | int i; |
228 | ||
229 | for (i = 0; i < 10; i++) { | |
230 | T0 = get_nsecs(); | |
0e9b07e5 | 231 | burn_nsecs(sched, 0); |
ec156764 IM |
232 | T1 = get_nsecs(); |
233 | delta = T1-T0; | |
234 | min_delta = min(min_delta, delta); | |
235 | } | |
0e9b07e5 | 236 | sched->run_measurement_overhead = min_delta; |
ec156764 | 237 | |
9486aa38 | 238 | printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
ec156764 IM |
239 | } |
240 | ||
0e9b07e5 | 241 | static void calibrate_sleep_measurement_overhead(struct perf_sched *sched) |
ec156764 | 242 | { |
b1ffe8f3 | 243 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
ec156764 IM |
244 | int i; |
245 | ||
246 | for (i = 0; i < 10; i++) { | |
247 | T0 = get_nsecs(); | |
248 | sleep_nsecs(10000); | |
249 | T1 = get_nsecs(); | |
250 | delta = T1-T0; | |
251 | min_delta = min(min_delta, delta); | |
252 | } | |
253 | min_delta -= 10000; | |
0e9b07e5 | 254 | sched->sleep_measurement_overhead = min_delta; |
ec156764 | 255 | |
9486aa38 | 256 | printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
ec156764 IM |
257 | } |
258 | ||
39aeb52f | 259 | static struct sched_atom * |
b1ffe8f3 | 260 | get_new_event(struct task_desc *task, u64 timestamp) |
ec156764 | 261 | { |
36479484 | 262 | struct sched_atom *event = zalloc(sizeof(*event)); |
ec156764 IM |
263 | unsigned long idx = task->nr_events; |
264 | size_t size; | |
265 | ||
266 | event->timestamp = timestamp; | |
267 | event->nr = idx; | |
268 | ||
269 | task->nr_events++; | |
39aeb52f | 270 | size = sizeof(struct sched_atom *) * task->nr_events; |
271 | task->atoms = realloc(task->atoms, size); | |
272 | BUG_ON(!task->atoms); | |
ec156764 | 273 | |
39aeb52f | 274 | task->atoms[idx] = event; |
ec156764 IM |
275 | |
276 | return event; | |
277 | } | |
278 | ||
39aeb52f | 279 | static struct sched_atom *last_event(struct task_desc *task) |
ec156764 IM |
280 | { |
281 | if (!task->nr_events) | |
282 | return NULL; | |
283 | ||
39aeb52f | 284 | return task->atoms[task->nr_events - 1]; |
ec156764 IM |
285 | } |
286 | ||
0e9b07e5 ACM |
287 | static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task, |
288 | u64 timestamp, u64 duration) | |
ec156764 | 289 | { |
39aeb52f | 290 | struct sched_atom *event, *curr_event = last_event(task); |
ec156764 IM |
291 | |
292 | /* | |
fbf94829 IM |
293 | * optimize an existing RUN event by merging this one |
294 | * to it: | |
295 | */ | |
ec156764 | 296 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
0e9b07e5 | 297 | sched->nr_run_events_optimized++; |
ec156764 IM |
298 | curr_event->duration += duration; |
299 | return; | |
300 | } | |
301 | ||
302 | event = get_new_event(task, timestamp); | |
303 | ||
304 | event->type = SCHED_EVENT_RUN; | |
305 | event->duration = duration; | |
306 | ||
0e9b07e5 | 307 | sched->nr_run_events++; |
ec156764 IM |
308 | } |
309 | ||
0e9b07e5 ACM |
310 | static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task, |
311 | u64 timestamp, struct task_desc *wakee) | |
ec156764 | 312 | { |
39aeb52f | 313 | struct sched_atom *event, *wakee_event; |
ec156764 IM |
314 | |
315 | event = get_new_event(task, timestamp); | |
316 | event->type = SCHED_EVENT_WAKEUP; | |
317 | event->wakee = wakee; | |
318 | ||
319 | wakee_event = last_event(wakee); | |
320 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { | |
0e9b07e5 | 321 | sched->targetless_wakeups++; |
ec156764 IM |
322 | return; |
323 | } | |
324 | if (wakee_event->wait_sem) { | |
0e9b07e5 | 325 | sched->multitarget_wakeups++; |
ec156764 IM |
326 | return; |
327 | } | |
328 | ||
36479484 | 329 | wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); |
ec156764 IM |
330 | sem_init(wakee_event->wait_sem, 0, 0); |
331 | wakee_event->specific_wait = 1; | |
332 | event->wait_sem = wakee_event->wait_sem; | |
333 | ||
0e9b07e5 | 334 | sched->nr_wakeup_events++; |
ec156764 IM |
335 | } |
336 | ||
0e9b07e5 ACM |
337 | static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task, |
338 | u64 timestamp, u64 task_state __maybe_unused) | |
ec156764 | 339 | { |
39aeb52f | 340 | struct sched_atom *event = get_new_event(task, timestamp); |
ec156764 IM |
341 | |
342 | event->type = SCHED_EVENT_SLEEP; | |
343 | ||
0e9b07e5 | 344 | sched->nr_sleep_events++; |
ec156764 IM |
345 | } |
346 | ||
0e9b07e5 ACM |
347 | static struct task_desc *register_pid(struct perf_sched *sched, |
348 | unsigned long pid, const char *comm) | |
ec156764 IM |
349 | { |
350 | struct task_desc *task; | |
cb06ac25 | 351 | static int pid_max; |
ec156764 | 352 | |
cb06ac25 YS |
353 | if (sched->pid_to_task == NULL) { |
354 | if (sysctl__read_int("kernel/pid_max", &pid_max) < 0) | |
355 | pid_max = MAX_PID; | |
356 | BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL); | |
357 | } | |
3a423a5c YS |
358 | if (pid >= (unsigned long)pid_max) { |
359 | BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) * | |
360 | sizeof(struct task_desc *))) == NULL); | |
361 | while (pid >= (unsigned long)pid_max) | |
362 | sched->pid_to_task[pid_max++] = NULL; | |
363 | } | |
ec156764 | 364 | |
0e9b07e5 | 365 | task = sched->pid_to_task[pid]; |
ec156764 IM |
366 | |
367 | if (task) | |
368 | return task; | |
369 | ||
36479484 | 370 | task = zalloc(sizeof(*task)); |
ec156764 | 371 | task->pid = pid; |
0e9b07e5 | 372 | task->nr = sched->nr_tasks; |
ec156764 IM |
373 | strcpy(task->comm, comm); |
374 | /* | |
375 | * every task starts in sleeping state - this gets ignored | |
376 | * if there's no wakeup pointing to this sleep state: | |
377 | */ | |
0e9b07e5 | 378 | add_sched_event_sleep(sched, task, 0, 0); |
ec156764 | 379 | |
0e9b07e5 ACM |
380 | sched->pid_to_task[pid] = task; |
381 | sched->nr_tasks++; | |
0755bc4d | 382 | sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *)); |
0e9b07e5 ACM |
383 | BUG_ON(!sched->tasks); |
384 | sched->tasks[task->nr] = task; | |
ec156764 | 385 | |
ad236fd2 | 386 | if (verbose) |
0e9b07e5 | 387 | printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm); |
ec156764 IM |
388 | |
389 | return task; | |
390 | } | |
391 | ||
392 | ||
0e9b07e5 | 393 | static void print_task_traces(struct perf_sched *sched) |
ec156764 IM |
394 | { |
395 | struct task_desc *task; | |
396 | unsigned long i; | |
397 | ||
0e9b07e5 ACM |
398 | for (i = 0; i < sched->nr_tasks; i++) { |
399 | task = sched->tasks[i]; | |
ad236fd2 | 400 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
ec156764 IM |
401 | task->nr, task->comm, task->pid, task->nr_events); |
402 | } | |
403 | } | |
404 | ||
0e9b07e5 | 405 | static void add_cross_task_wakeups(struct perf_sched *sched) |
ec156764 IM |
406 | { |
407 | struct task_desc *task1, *task2; | |
408 | unsigned long i, j; | |
409 | ||
0e9b07e5 ACM |
410 | for (i = 0; i < sched->nr_tasks; i++) { |
411 | task1 = sched->tasks[i]; | |
ec156764 | 412 | j = i + 1; |
0e9b07e5 | 413 | if (j == sched->nr_tasks) |
ec156764 | 414 | j = 0; |
0e9b07e5 ACM |
415 | task2 = sched->tasks[j]; |
416 | add_sched_event_wakeup(sched, task1, 0, task2); | |
ec156764 IM |
417 | } |
418 | } | |
419 | ||
0e9b07e5 ACM |
420 | static void perf_sched__process_event(struct perf_sched *sched, |
421 | struct sched_atom *atom) | |
ec156764 IM |
422 | { |
423 | int ret = 0; | |
ec156764 | 424 | |
39aeb52f | 425 | switch (atom->type) { |
ec156764 | 426 | case SCHED_EVENT_RUN: |
0e9b07e5 | 427 | burn_nsecs(sched, atom->duration); |
ec156764 IM |
428 | break; |
429 | case SCHED_EVENT_SLEEP: | |
39aeb52f | 430 | if (atom->wait_sem) |
431 | ret = sem_wait(atom->wait_sem); | |
ec156764 IM |
432 | BUG_ON(ret); |
433 | break; | |
434 | case SCHED_EVENT_WAKEUP: | |
39aeb52f | 435 | if (atom->wait_sem) |
436 | ret = sem_post(atom->wait_sem); | |
ec156764 IM |
437 | BUG_ON(ret); |
438 | break; | |
55ffb7a6 MG |
439 | case SCHED_EVENT_MIGRATION: |
440 | break; | |
ec156764 IM |
441 | default: |
442 | BUG_ON(1); | |
443 | } | |
444 | } | |
445 | ||
b1ffe8f3 | 446 | static u64 get_cpu_usage_nsec_parent(void) |
ec156764 IM |
447 | { |
448 | struct rusage ru; | |
b1ffe8f3 | 449 | u64 sum; |
ec156764 IM |
450 | int err; |
451 | ||
452 | err = getrusage(RUSAGE_SELF, &ru); | |
453 | BUG_ON(err); | |
454 | ||
455 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; | |
456 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; | |
457 | ||
458 | return sum; | |
459 | } | |
460 | ||
939cda52 | 461 | static int self_open_counters(struct perf_sched *sched, unsigned long cur_task) |
ec156764 | 462 | { |
c0c9e721 | 463 | struct perf_event_attr attr; |
939cda52 | 464 | char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE]; |
c0c9e721 | 465 | int fd; |
939cda52 YS |
466 | struct rlimit limit; |
467 | bool need_privilege = false; | |
ec156764 | 468 | |
c0c9e721 | 469 | memset(&attr, 0, sizeof(attr)); |
ec156764 | 470 | |
c0c9e721 XG |
471 | attr.type = PERF_TYPE_SOFTWARE; |
472 | attr.config = PERF_COUNT_SW_TASK_CLOCK; | |
ec156764 | 473 | |
939cda52 | 474 | force_again: |
57480d2c YD |
475 | fd = sys_perf_event_open(&attr, 0, -1, -1, |
476 | perf_event_open_cloexec_flag()); | |
c0c9e721 | 477 | |
1aff59be | 478 | if (fd < 0) { |
939cda52 YS |
479 | if (errno == EMFILE) { |
480 | if (sched->force) { | |
481 | BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1); | |
482 | limit.rlim_cur += sched->nr_tasks - cur_task; | |
483 | if (limit.rlim_cur > limit.rlim_max) { | |
484 | limit.rlim_max = limit.rlim_cur; | |
485 | need_privilege = true; | |
486 | } | |
487 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { | |
488 | if (need_privilege && errno == EPERM) | |
489 | strcpy(info, "Need privilege\n"); | |
490 | } else | |
491 | goto force_again; | |
492 | } else | |
493 | strcpy(info, "Have a try with -f option\n"); | |
494 | } | |
60b7d14a | 495 | pr_err("Error: sys_perf_event_open() syscall returned " |
939cda52 YS |
496 | "with %d (%s)\n%s", fd, |
497 | strerror_r(errno, sbuf, sizeof(sbuf)), info); | |
1aff59be YS |
498 | exit(EXIT_FAILURE); |
499 | } | |
c0c9e721 XG |
500 | return fd; |
501 | } | |
502 | ||
503 | static u64 get_cpu_usage_nsec_self(int fd) | |
504 | { | |
505 | u64 runtime; | |
506 | int ret; | |
507 | ||
508 | ret = read(fd, &runtime, sizeof(runtime)); | |
509 | BUG_ON(ret != sizeof(runtime)); | |
510 | ||
511 | return runtime; | |
ec156764 IM |
512 | } |
513 | ||
0e9b07e5 ACM |
514 | struct sched_thread_parms { |
515 | struct task_desc *task; | |
516 | struct perf_sched *sched; | |
08097abc | 517 | int fd; |
0e9b07e5 ACM |
518 | }; |
519 | ||
ec156764 IM |
520 | static void *thread_func(void *ctx) |
521 | { | |
0e9b07e5 ACM |
522 | struct sched_thread_parms *parms = ctx; |
523 | struct task_desc *this_task = parms->task; | |
524 | struct perf_sched *sched = parms->sched; | |
b1ffe8f3 | 525 | u64 cpu_usage_0, cpu_usage_1; |
ec156764 IM |
526 | unsigned long i, ret; |
527 | char comm2[22]; | |
08097abc | 528 | int fd = parms->fd; |
ec156764 | 529 | |
74cf249d | 530 | zfree(&parms); |
0e9b07e5 | 531 | |
ec156764 IM |
532 | sprintf(comm2, ":%s", this_task->comm); |
533 | prctl(PR_SET_NAME, comm2); | |
a116e05d ACM |
534 | if (fd < 0) |
535 | return NULL; | |
ec156764 IM |
536 | again: |
537 | ret = sem_post(&this_task->ready_for_work); | |
538 | BUG_ON(ret); | |
0e9b07e5 | 539 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
ec156764 | 540 | BUG_ON(ret); |
0e9b07e5 | 541 | ret = pthread_mutex_unlock(&sched->start_work_mutex); |
ec156764 | 542 | BUG_ON(ret); |
ec156764 | 543 | |
c0c9e721 | 544 | cpu_usage_0 = get_cpu_usage_nsec_self(fd); |
ec156764 IM |
545 | |
546 | for (i = 0; i < this_task->nr_events; i++) { | |
547 | this_task->curr_event = i; | |
0e9b07e5 | 548 | perf_sched__process_event(sched, this_task->atoms[i]); |
ec156764 IM |
549 | } |
550 | ||
c0c9e721 | 551 | cpu_usage_1 = get_cpu_usage_nsec_self(fd); |
ec156764 | 552 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; |
ec156764 IM |
553 | ret = sem_post(&this_task->work_done_sem); |
554 | BUG_ON(ret); | |
ec156764 | 555 | |
0e9b07e5 | 556 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
ec156764 | 557 | BUG_ON(ret); |
0e9b07e5 | 558 | ret = pthread_mutex_unlock(&sched->work_done_wait_mutex); |
ec156764 | 559 | BUG_ON(ret); |
ec156764 IM |
560 | |
561 | goto again; | |
562 | } | |
563 | ||
0e9b07e5 | 564 | static void create_tasks(struct perf_sched *sched) |
ec156764 IM |
565 | { |
566 | struct task_desc *task; | |
567 | pthread_attr_t attr; | |
568 | unsigned long i; | |
569 | int err; | |
570 | ||
571 | err = pthread_attr_init(&attr); | |
572 | BUG_ON(err); | |
12f7e036 JP |
573 | err = pthread_attr_setstacksize(&attr, |
574 | (size_t) max(16 * 1024, PTHREAD_STACK_MIN)); | |
ec156764 | 575 | BUG_ON(err); |
0e9b07e5 | 576 | err = pthread_mutex_lock(&sched->start_work_mutex); |
ec156764 | 577 | BUG_ON(err); |
0e9b07e5 | 578 | err = pthread_mutex_lock(&sched->work_done_wait_mutex); |
ec156764 | 579 | BUG_ON(err); |
0e9b07e5 ACM |
580 | for (i = 0; i < sched->nr_tasks; i++) { |
581 | struct sched_thread_parms *parms = malloc(sizeof(*parms)); | |
582 | BUG_ON(parms == NULL); | |
583 | parms->task = task = sched->tasks[i]; | |
584 | parms->sched = sched; | |
939cda52 | 585 | parms->fd = self_open_counters(sched, i); |
ec156764 IM |
586 | sem_init(&task->sleep_sem, 0, 0); |
587 | sem_init(&task->ready_for_work, 0, 0); | |
588 | sem_init(&task->work_done_sem, 0, 0); | |
589 | task->curr_event = 0; | |
0e9b07e5 | 590 | err = pthread_create(&task->thread, &attr, thread_func, parms); |
ec156764 IM |
591 | BUG_ON(err); |
592 | } | |
593 | } | |
594 | ||
0e9b07e5 | 595 | static void wait_for_tasks(struct perf_sched *sched) |
ec156764 | 596 | { |
b1ffe8f3 | 597 | u64 cpu_usage_0, cpu_usage_1; |
ec156764 IM |
598 | struct task_desc *task; |
599 | unsigned long i, ret; | |
600 | ||
0e9b07e5 ACM |
601 | sched->start_time = get_nsecs(); |
602 | sched->cpu_usage = 0; | |
603 | pthread_mutex_unlock(&sched->work_done_wait_mutex); | |
ec156764 | 604 | |
0e9b07e5 ACM |
605 | for (i = 0; i < sched->nr_tasks; i++) { |
606 | task = sched->tasks[i]; | |
ec156764 IM |
607 | ret = sem_wait(&task->ready_for_work); |
608 | BUG_ON(ret); | |
609 | sem_init(&task->ready_for_work, 0, 0); | |
610 | } | |
0e9b07e5 | 611 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
ec156764 IM |
612 | BUG_ON(ret); |
613 | ||
614 | cpu_usage_0 = get_cpu_usage_nsec_parent(); | |
615 | ||
0e9b07e5 | 616 | pthread_mutex_unlock(&sched->start_work_mutex); |
ec156764 | 617 | |
0e9b07e5 ACM |
618 | for (i = 0; i < sched->nr_tasks; i++) { |
619 | task = sched->tasks[i]; | |
ec156764 IM |
620 | ret = sem_wait(&task->work_done_sem); |
621 | BUG_ON(ret); | |
622 | sem_init(&task->work_done_sem, 0, 0); | |
0e9b07e5 | 623 | sched->cpu_usage += task->cpu_usage; |
ec156764 IM |
624 | task->cpu_usage = 0; |
625 | } | |
626 | ||
627 | cpu_usage_1 = get_cpu_usage_nsec_parent(); | |
0e9b07e5 ACM |
628 | if (!sched->runavg_cpu_usage) |
629 | sched->runavg_cpu_usage = sched->cpu_usage; | |
ff5f3bbd | 630 | sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat; |
ec156764 | 631 | |
0e9b07e5 ACM |
632 | sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; |
633 | if (!sched->runavg_parent_cpu_usage) | |
634 | sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; | |
ff5f3bbd YS |
635 | sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + |
636 | sched->parent_cpu_usage)/sched->replay_repeat; | |
ec156764 | 637 | |
0e9b07e5 | 638 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
ec156764 IM |
639 | BUG_ON(ret); |
640 | ||
0e9b07e5 ACM |
641 | for (i = 0; i < sched->nr_tasks; i++) { |
642 | task = sched->tasks[i]; | |
ec156764 IM |
643 | sem_init(&task->sleep_sem, 0, 0); |
644 | task->curr_event = 0; | |
645 | } | |
646 | } | |
647 | ||
0e9b07e5 | 648 | static void run_one_test(struct perf_sched *sched) |
ec156764 | 649 | { |
fb7d0b3c | 650 | u64 T0, T1, delta, avg_delta, fluct; |
ec156764 IM |
651 | |
652 | T0 = get_nsecs(); | |
0e9b07e5 | 653 | wait_for_tasks(sched); |
ec156764 IM |
654 | T1 = get_nsecs(); |
655 | ||
656 | delta = T1 - T0; | |
0e9b07e5 ACM |
657 | sched->sum_runtime += delta; |
658 | sched->nr_runs++; | |
ec156764 | 659 | |
0e9b07e5 | 660 | avg_delta = sched->sum_runtime / sched->nr_runs; |
ec156764 IM |
661 | if (delta < avg_delta) |
662 | fluct = avg_delta - delta; | |
663 | else | |
664 | fluct = delta - avg_delta; | |
0e9b07e5 ACM |
665 | sched->sum_fluct += fluct; |
666 | if (!sched->run_avg) | |
667 | sched->run_avg = delta; | |
ff5f3bbd | 668 | sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat; |
ec156764 | 669 | |
0e9b07e5 | 670 | printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0); |
ec156764 | 671 | |
0e9b07e5 | 672 | printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6); |
ec156764 | 673 | |
ad236fd2 | 674 | printf("cpu: %0.2f / %0.2f", |
0e9b07e5 | 675 | (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6); |
ec156764 IM |
676 | |
677 | #if 0 | |
678 | /* | |
fbf94829 | 679 | * rusage statistics done by the parent, these are less |
0e9b07e5 | 680 | * accurate than the sched->sum_exec_runtime based statistics: |
fbf94829 | 681 | */ |
ad236fd2 | 682 | printf(" [%0.2f / %0.2f]", |
0e9b07e5 ACM |
683 | (double)sched->parent_cpu_usage/1e6, |
684 | (double)sched->runavg_parent_cpu_usage/1e6); | |
ec156764 IM |
685 | #endif |
686 | ||
ad236fd2 | 687 | printf("\n"); |
ec156764 | 688 | |
0e9b07e5 ACM |
689 | if (sched->nr_sleep_corrections) |
690 | printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections); | |
691 | sched->nr_sleep_corrections = 0; | |
ec156764 IM |
692 | } |
693 | ||
0e9b07e5 | 694 | static void test_calibrations(struct perf_sched *sched) |
ec156764 | 695 | { |
b1ffe8f3 | 696 | u64 T0, T1; |
ec156764 IM |
697 | |
698 | T0 = get_nsecs(); | |
0e9b07e5 | 699 | burn_nsecs(sched, 1e6); |
ec156764 IM |
700 | T1 = get_nsecs(); |
701 | ||
9486aa38 | 702 | printf("the run test took %" PRIu64 " nsecs\n", T1 - T0); |
ec156764 IM |
703 | |
704 | T0 = get_nsecs(); | |
705 | sleep_nsecs(1e6); | |
706 | T1 = get_nsecs(); | |
707 | ||
9486aa38 | 708 | printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0); |
ec156764 IM |
709 | } |
710 | ||
a116e05d | 711 | static int |
0e9b07e5 | 712 | replay_wakeup_event(struct perf_sched *sched, |
9ec3f4e4 ACM |
713 | struct perf_evsel *evsel, struct perf_sample *sample, |
714 | struct machine *machine __maybe_unused) | |
419ab0d6 | 715 | { |
9ec3f4e4 ACM |
716 | const char *comm = perf_evsel__strval(evsel, sample, "comm"); |
717 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); | |
419ab0d6 | 718 | struct task_desc *waker, *wakee; |
fbf94829 | 719 | |
ad236fd2 | 720 | if (verbose) { |
2b7fcbc5 | 721 | printf("sched_wakeup event %p\n", evsel); |
fbf94829 | 722 | |
9ec3f4e4 | 723 | printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid); |
ad236fd2 | 724 | } |
fbf94829 | 725 | |
2b7fcbc5 | 726 | waker = register_pid(sched, sample->tid, "<unknown>"); |
9ec3f4e4 | 727 | wakee = register_pid(sched, pid, comm); |
fbf94829 | 728 | |
0e9b07e5 | 729 | add_sched_event_wakeup(sched, waker, sample->time, wakee); |
a116e05d | 730 | return 0; |
ec156764 IM |
731 | } |
732 | ||
9ec3f4e4 ACM |
733 | static int replay_switch_event(struct perf_sched *sched, |
734 | struct perf_evsel *evsel, | |
735 | struct perf_sample *sample, | |
736 | struct machine *machine __maybe_unused) | |
ec156764 | 737 | { |
9ec3f4e4 ACM |
738 | const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"), |
739 | *next_comm = perf_evsel__strval(evsel, sample, "next_comm"); | |
740 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), | |
741 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); | |
742 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); | |
1d037ca1 | 743 | struct task_desc *prev, __maybe_unused *next; |
7f7f8d0b ACM |
744 | u64 timestamp0, timestamp = sample->time; |
745 | int cpu = sample->cpu; | |
fbf94829 IM |
746 | s64 delta; |
747 | ||
ad236fd2 | 748 | if (verbose) |
2b7fcbc5 | 749 | printf("sched_switch event %p\n", evsel); |
ad236fd2 | 750 | |
fbf94829 | 751 | if (cpu >= MAX_CPUS || cpu < 0) |
a116e05d | 752 | return 0; |
fbf94829 | 753 | |
0e9b07e5 | 754 | timestamp0 = sched->cpu_last_switched[cpu]; |
fbf94829 IM |
755 | if (timestamp0) |
756 | delta = timestamp - timestamp0; | |
757 | else | |
758 | delta = 0; | |
759 | ||
a116e05d | 760 | if (delta < 0) { |
60b7d14a | 761 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
a116e05d ACM |
762 | return -1; |
763 | } | |
fbf94829 | 764 | |
9ec3f4e4 ACM |
765 | pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n", |
766 | prev_comm, prev_pid, next_comm, next_pid, delta); | |
fbf94829 | 767 | |
9ec3f4e4 ACM |
768 | prev = register_pid(sched, prev_pid, prev_comm); |
769 | next = register_pid(sched, next_pid, next_comm); | |
fbf94829 | 770 | |
0e9b07e5 | 771 | sched->cpu_last_switched[cpu] = timestamp; |
fbf94829 | 772 | |
0e9b07e5 | 773 | add_sched_event_run(sched, prev, timestamp, delta); |
9ec3f4e4 | 774 | add_sched_event_sleep(sched, prev, timestamp, prev_state); |
a116e05d ACM |
775 | |
776 | return 0; | |
fbf94829 IM |
777 | } |
778 | ||
cb627505 DA |
779 | static int replay_fork_event(struct perf_sched *sched, |
780 | union perf_event *event, | |
781 | struct machine *machine) | |
419ab0d6 | 782 | { |
cb627505 DA |
783 | struct thread *child, *parent; |
784 | ||
314add6b AH |
785 | child = machine__findnew_thread(machine, event->fork.pid, |
786 | event->fork.tid); | |
787 | parent = machine__findnew_thread(machine, event->fork.ppid, | |
788 | event->fork.ptid); | |
cb627505 DA |
789 | |
790 | if (child == NULL || parent == NULL) { | |
791 | pr_debug("thread does not exist on fork event: child %p, parent %p\n", | |
792 | child, parent); | |
b91fc39f | 793 | goto out_put; |
cb627505 | 794 | } |
9ec3f4e4 | 795 | |
419ab0d6 | 796 | if (verbose) { |
cb627505 | 797 | printf("fork event\n"); |
b9c5143a FW |
798 | printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid); |
799 | printf("... child: %s/%d\n", thread__comm_str(child), child->tid); | |
419ab0d6 | 800 | } |
9ec3f4e4 | 801 | |
b9c5143a FW |
802 | register_pid(sched, parent->tid, thread__comm_str(parent)); |
803 | register_pid(sched, child->tid, thread__comm_str(child)); | |
b91fc39f ACM |
804 | out_put: |
805 | thread__put(child); | |
806 | thread__put(parent); | |
a116e05d | 807 | return 0; |
419ab0d6 | 808 | } |
fbf94829 | 809 | |
b1ffe8f3 IM |
810 | struct sort_dimension { |
811 | const char *name; | |
b5fae128 | 812 | sort_fn_t cmp; |
b1ffe8f3 IM |
813 | struct list_head list; |
814 | }; | |
815 | ||
daa1d7a5 | 816 | static int |
39aeb52f | 817 | thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
818 | { |
819 | struct sort_dimension *sort; | |
820 | int ret = 0; | |
821 | ||
b5fae128 IM |
822 | BUG_ON(list_empty(list)); |
823 | ||
daa1d7a5 FW |
824 | list_for_each_entry(sort, list, list) { |
825 | ret = sort->cmp(l, r); | |
826 | if (ret) | |
827 | return ret; | |
828 | } | |
829 | ||
830 | return ret; | |
831 | } | |
832 | ||
39aeb52f | 833 | static struct work_atoms * |
b5fae128 IM |
834 | thread_atoms_search(struct rb_root *root, struct thread *thread, |
835 | struct list_head *sort_list) | |
836 | { | |
837 | struct rb_node *node = root->rb_node; | |
39aeb52f | 838 | struct work_atoms key = { .thread = thread }; |
b5fae128 IM |
839 | |
840 | while (node) { | |
39aeb52f | 841 | struct work_atoms *atoms; |
b5fae128 IM |
842 | int cmp; |
843 | ||
39aeb52f | 844 | atoms = container_of(node, struct work_atoms, node); |
b5fae128 IM |
845 | |
846 | cmp = thread_lat_cmp(sort_list, &key, atoms); | |
847 | if (cmp > 0) | |
848 | node = node->rb_left; | |
849 | else if (cmp < 0) | |
850 | node = node->rb_right; | |
851 | else { | |
852 | BUG_ON(thread != atoms->thread); | |
853 | return atoms; | |
854 | } | |
855 | } | |
856 | return NULL; | |
857 | } | |
858 | ||
cdce9d73 | 859 | static void |
39aeb52f | 860 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, |
daa1d7a5 | 861 | struct list_head *sort_list) |
cdce9d73 FW |
862 | { |
863 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
864 | ||
865 | while (*new) { | |
39aeb52f | 866 | struct work_atoms *this; |
daa1d7a5 | 867 | int cmp; |
cdce9d73 | 868 | |
39aeb52f | 869 | this = container_of(*new, struct work_atoms, node); |
cdce9d73 | 870 | parent = *new; |
daa1d7a5 FW |
871 | |
872 | cmp = thread_lat_cmp(sort_list, data, this); | |
873 | ||
874 | if (cmp > 0) | |
cdce9d73 | 875 | new = &((*new)->rb_left); |
cdce9d73 | 876 | else |
daa1d7a5 | 877 | new = &((*new)->rb_right); |
cdce9d73 FW |
878 | } |
879 | ||
880 | rb_link_node(&data->node, parent, new); | |
881 | rb_insert_color(&data->node, root); | |
882 | } | |
883 | ||
0e9b07e5 | 884 | static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread) |
cdce9d73 | 885 | { |
36479484 | 886 | struct work_atoms *atoms = zalloc(sizeof(*atoms)); |
a116e05d ACM |
887 | if (!atoms) { |
888 | pr_err("No memory at %s\n", __func__); | |
889 | return -1; | |
890 | } | |
cdce9d73 | 891 | |
f3b623b8 | 892 | atoms->thread = thread__get(thread); |
39aeb52f | 893 | INIT_LIST_HEAD(&atoms->work_list); |
0e9b07e5 | 894 | __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); |
a116e05d | 895 | return 0; |
cdce9d73 FW |
896 | } |
897 | ||
9ec3f4e4 | 898 | static char sched_out_state(u64 prev_state) |
cdce9d73 FW |
899 | { |
900 | const char *str = TASK_STATE_TO_CHAR_STR; | |
901 | ||
9ec3f4e4 | 902 | return str[prev_state]; |
cdce9d73 FW |
903 | } |
904 | ||
a116e05d | 905 | static int |
39aeb52f | 906 | add_sched_out_event(struct work_atoms *atoms, |
907 | char run_state, | |
908 | u64 timestamp) | |
cdce9d73 | 909 | { |
36479484 | 910 | struct work_atom *atom = zalloc(sizeof(*atom)); |
a116e05d ACM |
911 | if (!atom) { |
912 | pr_err("Non memory at %s", __func__); | |
913 | return -1; | |
914 | } | |
cdce9d73 | 915 | |
aa1ab9d2 FW |
916 | atom->sched_out_time = timestamp; |
917 | ||
39aeb52f | 918 | if (run_state == 'R') { |
b1ffe8f3 | 919 | atom->state = THREAD_WAIT_CPU; |
aa1ab9d2 | 920 | atom->wake_up_time = atom->sched_out_time; |
c6ced611 FW |
921 | } |
922 | ||
39aeb52f | 923 | list_add_tail(&atom->list, &atoms->work_list); |
a116e05d | 924 | return 0; |
cdce9d73 FW |
925 | } |
926 | ||
927 | static void | |
1d037ca1 IT |
928 | add_runtime_event(struct work_atoms *atoms, u64 delta, |
929 | u64 timestamp __maybe_unused) | |
39aeb52f | 930 | { |
931 | struct work_atom *atom; | |
932 | ||
933 | BUG_ON(list_empty(&atoms->work_list)); | |
934 | ||
935 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | |
936 | ||
937 | atom->runtime += delta; | |
938 | atoms->total_runtime += delta; | |
939 | } | |
940 | ||
941 | static void | |
942 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) | |
cdce9d73 | 943 | { |
b1ffe8f3 | 944 | struct work_atom *atom; |
66685678 | 945 | u64 delta; |
cdce9d73 | 946 | |
39aeb52f | 947 | if (list_empty(&atoms->work_list)) |
cdce9d73 FW |
948 | return; |
949 | ||
39aeb52f | 950 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
cdce9d73 | 951 | |
b1ffe8f3 | 952 | if (atom->state != THREAD_WAIT_CPU) |
cdce9d73 FW |
953 | return; |
954 | ||
b1ffe8f3 IM |
955 | if (timestamp < atom->wake_up_time) { |
956 | atom->state = THREAD_IGNORE; | |
cdce9d73 FW |
957 | return; |
958 | } | |
959 | ||
b1ffe8f3 IM |
960 | atom->state = THREAD_SCHED_IN; |
961 | atom->sched_in_time = timestamp; | |
66685678 | 962 | |
b1ffe8f3 | 963 | delta = atom->sched_in_time - atom->wake_up_time; |
66685678 | 964 | atoms->total_lat += delta; |
3786310a | 965 | if (delta > atoms->max_lat) { |
66685678 | 966 | atoms->max_lat = delta; |
3786310a FW |
967 | atoms->max_lat_at = timestamp; |
968 | } | |
66685678 | 969 | atoms->nb_atoms++; |
cdce9d73 FW |
970 | } |
971 | ||
9ec3f4e4 ACM |
972 | static int latency_switch_event(struct perf_sched *sched, |
973 | struct perf_evsel *evsel, | |
974 | struct perf_sample *sample, | |
975 | struct machine *machine) | |
cdce9d73 | 976 | { |
9ec3f4e4 ACM |
977 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
978 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); | |
979 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); | |
39aeb52f | 980 | struct work_atoms *out_events, *in_events; |
cdce9d73 | 981 | struct thread *sched_out, *sched_in; |
7f7f8d0b | 982 | u64 timestamp0, timestamp = sample->time; |
b91fc39f | 983 | int cpu = sample->cpu, err = -1; |
ea92ed5a IM |
984 | s64 delta; |
985 | ||
39aeb52f | 986 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
ea92ed5a | 987 | |
0e9b07e5 ACM |
988 | timestamp0 = sched->cpu_last_switched[cpu]; |
989 | sched->cpu_last_switched[cpu] = timestamp; | |
ea92ed5a IM |
990 | if (timestamp0) |
991 | delta = timestamp - timestamp0; | |
992 | else | |
993 | delta = 0; | |
994 | ||
a116e05d ACM |
995 | if (delta < 0) { |
996 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); | |
997 | return -1; | |
998 | } | |
cdce9d73 | 999 | |
1fcb8768 AH |
1000 | sched_out = machine__findnew_thread(machine, -1, prev_pid); |
1001 | sched_in = machine__findnew_thread(machine, -1, next_pid); | |
b91fc39f ACM |
1002 | if (sched_out == NULL || sched_in == NULL) |
1003 | goto out_put; | |
cdce9d73 | 1004 | |
0e9b07e5 | 1005 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
39aeb52f | 1006 | if (!out_events) { |
0e9b07e5 | 1007 | if (thread_atoms_insert(sched, sched_out)) |
b91fc39f | 1008 | goto out_put; |
0e9b07e5 | 1009 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
a116e05d ACM |
1010 | if (!out_events) { |
1011 | pr_err("out-event: Internal tree error"); | |
b91fc39f | 1012 | goto out_put; |
a116e05d | 1013 | } |
39aeb52f | 1014 | } |
9ec3f4e4 | 1015 | if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp)) |
a116e05d | 1016 | return -1; |
39aeb52f | 1017 | |
0e9b07e5 | 1018 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
39aeb52f | 1019 | if (!in_events) { |
0e9b07e5 | 1020 | if (thread_atoms_insert(sched, sched_in)) |
b91fc39f | 1021 | goto out_put; |
0e9b07e5 | 1022 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
a116e05d ACM |
1023 | if (!in_events) { |
1024 | pr_err("in-event: Internal tree error"); | |
b91fc39f | 1025 | goto out_put; |
a116e05d | 1026 | } |
39aeb52f | 1027 | /* |
1028 | * Take came in we have not heard about yet, | |
1029 | * add in an initial atom in runnable state: | |
1030 | */ | |
a116e05d | 1031 | if (add_sched_out_event(in_events, 'R', timestamp)) |
b91fc39f | 1032 | goto out_put; |
cdce9d73 | 1033 | } |
39aeb52f | 1034 | add_sched_in_event(in_events, timestamp); |
b91fc39f ACM |
1035 | err = 0; |
1036 | out_put: | |
1037 | thread__put(sched_out); | |
1038 | thread__put(sched_in); | |
1039 | return err; | |
39aeb52f | 1040 | } |
cdce9d73 | 1041 | |
9ec3f4e4 ACM |
1042 | static int latency_runtime_event(struct perf_sched *sched, |
1043 | struct perf_evsel *evsel, | |
1044 | struct perf_sample *sample, | |
1045 | struct machine *machine) | |
39aeb52f | 1046 | { |
9ec3f4e4 ACM |
1047 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
1048 | const u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); | |
1fcb8768 | 1049 | struct thread *thread = machine__findnew_thread(machine, -1, pid); |
0e9b07e5 | 1050 | struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); |
7f7f8d0b | 1051 | u64 timestamp = sample->time; |
b91fc39f ACM |
1052 | int cpu = sample->cpu, err = -1; |
1053 | ||
1054 | if (thread == NULL) | |
1055 | return -1; | |
39aeb52f | 1056 | |
1057 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); | |
39aeb52f | 1058 | if (!atoms) { |
0e9b07e5 | 1059 | if (thread_atoms_insert(sched, thread)) |
b91fc39f | 1060 | goto out_put; |
0e9b07e5 | 1061 | atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); |
a116e05d | 1062 | if (!atoms) { |
60b7d14a | 1063 | pr_err("in-event: Internal tree error"); |
b91fc39f | 1064 | goto out_put; |
a116e05d ACM |
1065 | } |
1066 | if (add_sched_out_event(atoms, 'R', timestamp)) | |
b91fc39f | 1067 | goto out_put; |
cdce9d73 FW |
1068 | } |
1069 | ||
9ec3f4e4 | 1070 | add_runtime_event(atoms, runtime, timestamp); |
b91fc39f ACM |
1071 | err = 0; |
1072 | out_put: | |
1073 | thread__put(thread); | |
1074 | return err; | |
cdce9d73 FW |
1075 | } |
1076 | ||
9ec3f4e4 ACM |
1077 | static int latency_wakeup_event(struct perf_sched *sched, |
1078 | struct perf_evsel *evsel, | |
1079 | struct perf_sample *sample, | |
1080 | struct machine *machine) | |
cdce9d73 | 1081 | { |
0680ee7d | 1082 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
39aeb52f | 1083 | struct work_atoms *atoms; |
b1ffe8f3 | 1084 | struct work_atom *atom; |
cdce9d73 | 1085 | struct thread *wakee; |
7f7f8d0b | 1086 | u64 timestamp = sample->time; |
b91fc39f | 1087 | int err = -1; |
cdce9d73 | 1088 | |
1fcb8768 | 1089 | wakee = machine__findnew_thread(machine, -1, pid); |
b91fc39f ACM |
1090 | if (wakee == NULL) |
1091 | return -1; | |
0e9b07e5 | 1092 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
17562205 | 1093 | if (!atoms) { |
0e9b07e5 | 1094 | if (thread_atoms_insert(sched, wakee)) |
b91fc39f | 1095 | goto out_put; |
0e9b07e5 | 1096 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
a116e05d | 1097 | if (!atoms) { |
60b7d14a | 1098 | pr_err("wakeup-event: Internal tree error"); |
b91fc39f | 1099 | goto out_put; |
a116e05d ACM |
1100 | } |
1101 | if (add_sched_out_event(atoms, 'S', timestamp)) | |
b91fc39f | 1102 | goto out_put; |
cdce9d73 FW |
1103 | } |
1104 | ||
39aeb52f | 1105 | BUG_ON(list_empty(&atoms->work_list)); |
cdce9d73 | 1106 | |
39aeb52f | 1107 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
cdce9d73 | 1108 | |
55ffb7a6 | 1109 | /* |
67d6259d DY |
1110 | * As we do not guarantee the wakeup event happens when |
1111 | * task is out of run queue, also may happen when task is | |
1112 | * on run queue and wakeup only change ->state to TASK_RUNNING, | |
1113 | * then we should not set the ->wake_up_time when wake up a | |
1114 | * task which is on run queue. | |
1115 | * | |
55ffb7a6 MG |
1116 | * You WILL be missing events if you've recorded only |
1117 | * one CPU, or are only looking at only one, so don't | |
67d6259d | 1118 | * skip in this case. |
55ffb7a6 | 1119 | */ |
0e9b07e5 | 1120 | if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING) |
b91fc39f | 1121 | goto out_ok; |
cdce9d73 | 1122 | |
0e9b07e5 | 1123 | sched->nr_timestamps++; |
ea57c4f5 | 1124 | if (atom->sched_out_time > timestamp) { |
0e9b07e5 | 1125 | sched->nr_unordered_timestamps++; |
b91fc39f | 1126 | goto out_ok; |
ea57c4f5 | 1127 | } |
aa1ab9d2 | 1128 | |
b1ffe8f3 IM |
1129 | atom->state = THREAD_WAIT_CPU; |
1130 | atom->wake_up_time = timestamp; | |
b91fc39f ACM |
1131 | out_ok: |
1132 | err = 0; | |
1133 | out_put: | |
1134 | thread__put(wakee); | |
1135 | return err; | |
cdce9d73 FW |
1136 | } |
1137 | ||
9ec3f4e4 ACM |
1138 | static int latency_migrate_task_event(struct perf_sched *sched, |
1139 | struct perf_evsel *evsel, | |
1140 | struct perf_sample *sample, | |
1141 | struct machine *machine) | |
55ffb7a6 | 1142 | { |
9ec3f4e4 | 1143 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
7f7f8d0b | 1144 | u64 timestamp = sample->time; |
55ffb7a6 MG |
1145 | struct work_atoms *atoms; |
1146 | struct work_atom *atom; | |
1147 | struct thread *migrant; | |
b91fc39f | 1148 | int err = -1; |
55ffb7a6 MG |
1149 | |
1150 | /* | |
1151 | * Only need to worry about migration when profiling one CPU. | |
1152 | */ | |
0e9b07e5 | 1153 | if (sched->profile_cpu == -1) |
a116e05d | 1154 | return 0; |
55ffb7a6 | 1155 | |
1fcb8768 | 1156 | migrant = machine__findnew_thread(machine, -1, pid); |
b91fc39f ACM |
1157 | if (migrant == NULL) |
1158 | return -1; | |
0e9b07e5 | 1159 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
55ffb7a6 | 1160 | if (!atoms) { |
0e9b07e5 | 1161 | if (thread_atoms_insert(sched, migrant)) |
b91fc39f | 1162 | goto out_put; |
b9c5143a | 1163 | register_pid(sched, migrant->tid, thread__comm_str(migrant)); |
0e9b07e5 | 1164 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
a116e05d | 1165 | if (!atoms) { |
60b7d14a | 1166 | pr_err("migration-event: Internal tree error"); |
b91fc39f | 1167 | goto out_put; |
a116e05d ACM |
1168 | } |
1169 | if (add_sched_out_event(atoms, 'R', timestamp)) | |
b91fc39f | 1170 | goto out_put; |
55ffb7a6 MG |
1171 | } |
1172 | ||
1173 | BUG_ON(list_empty(&atoms->work_list)); | |
1174 | ||
1175 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | |
1176 | atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; | |
1177 | ||
0e9b07e5 | 1178 | sched->nr_timestamps++; |
55ffb7a6 MG |
1179 | |
1180 | if (atom->sched_out_time > timestamp) | |
0e9b07e5 | 1181 | sched->nr_unordered_timestamps++; |
b91fc39f ACM |
1182 | err = 0; |
1183 | out_put: | |
1184 | thread__put(migrant); | |
1185 | return err; | |
55ffb7a6 MG |
1186 | } |
1187 | ||
0e9b07e5 | 1188 | static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list) |
cdce9d73 | 1189 | { |
cdce9d73 FW |
1190 | int i; |
1191 | int ret; | |
66685678 | 1192 | u64 avg; |
cdce9d73 | 1193 | |
39aeb52f | 1194 | if (!work_list->nb_atoms) |
cdce9d73 | 1195 | return; |
ea57c4f5 IM |
1196 | /* |
1197 | * Ignore idle threads: | |
1198 | */ | |
b9c5143a | 1199 | if (!strcmp(thread__comm_str(work_list->thread), "swapper")) |
ea57c4f5 | 1200 | return; |
cdce9d73 | 1201 | |
0e9b07e5 ACM |
1202 | sched->all_runtime += work_list->total_runtime; |
1203 | sched->all_count += work_list->nb_atoms; | |
66685678 | 1204 | |
2f80dd44 JB |
1205 | if (work_list->num_merged > 1) |
1206 | ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged); | |
1207 | else | |
1208 | ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid); | |
cdce9d73 | 1209 | |
08f69e6c | 1210 | for (i = 0; i < 24 - ret; i++) |
cdce9d73 FW |
1211 | printf(" "); |
1212 | ||
39aeb52f | 1213 | avg = work_list->total_lat / work_list->nb_atoms; |
cdce9d73 | 1214 | |
80790e0b | 1215 | printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n", |
39aeb52f | 1216 | (double)work_list->total_runtime / 1e6, |
1217 | work_list->nb_atoms, (double)avg / 1e6, | |
3786310a FW |
1218 | (double)work_list->max_lat / 1e6, |
1219 | (double)work_list->max_lat_at / 1e9); | |
cdce9d73 FW |
1220 | } |
1221 | ||
39aeb52f | 1222 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 | 1223 | { |
0014de17 JO |
1224 | if (l->thread == r->thread) |
1225 | return 0; | |
38051234 | 1226 | if (l->thread->tid < r->thread->tid) |
daa1d7a5 | 1227 | return -1; |
38051234 | 1228 | if (l->thread->tid > r->thread->tid) |
daa1d7a5 | 1229 | return 1; |
0014de17 | 1230 | return (int)(l->thread - r->thread); |
daa1d7a5 FW |
1231 | } |
1232 | ||
39aeb52f | 1233 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1234 | { |
1235 | u64 avgl, avgr; | |
1236 | ||
1237 | if (!l->nb_atoms) | |
1238 | return -1; | |
1239 | ||
1240 | if (!r->nb_atoms) | |
1241 | return 1; | |
1242 | ||
1243 | avgl = l->total_lat / l->nb_atoms; | |
1244 | avgr = r->total_lat / r->nb_atoms; | |
1245 | ||
1246 | if (avgl < avgr) | |
1247 | return -1; | |
1248 | if (avgl > avgr) | |
1249 | return 1; | |
1250 | ||
1251 | return 0; | |
1252 | } | |
1253 | ||
39aeb52f | 1254 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1255 | { |
1256 | if (l->max_lat < r->max_lat) | |
1257 | return -1; | |
1258 | if (l->max_lat > r->max_lat) | |
1259 | return 1; | |
1260 | ||
1261 | return 0; | |
1262 | } | |
1263 | ||
39aeb52f | 1264 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1265 | { |
1266 | if (l->nb_atoms < r->nb_atoms) | |
1267 | return -1; | |
1268 | if (l->nb_atoms > r->nb_atoms) | |
1269 | return 1; | |
1270 | ||
1271 | return 0; | |
1272 | } | |
1273 | ||
39aeb52f | 1274 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1275 | { |
1276 | if (l->total_runtime < r->total_runtime) | |
1277 | return -1; | |
1278 | if (l->total_runtime > r->total_runtime) | |
1279 | return 1; | |
1280 | ||
1281 | return 0; | |
1282 | } | |
1283 | ||
cbef79a8 | 1284 | static int sort_dimension__add(const char *tok, struct list_head *list) |
daa1d7a5 | 1285 | { |
0e9b07e5 ACM |
1286 | size_t i; |
1287 | static struct sort_dimension avg_sort_dimension = { | |
1288 | .name = "avg", | |
1289 | .cmp = avg_cmp, | |
1290 | }; | |
1291 | static struct sort_dimension max_sort_dimension = { | |
1292 | .name = "max", | |
1293 | .cmp = max_cmp, | |
1294 | }; | |
1295 | static struct sort_dimension pid_sort_dimension = { | |
1296 | .name = "pid", | |
1297 | .cmp = pid_cmp, | |
1298 | }; | |
1299 | static struct sort_dimension runtime_sort_dimension = { | |
1300 | .name = "runtime", | |
1301 | .cmp = runtime_cmp, | |
1302 | }; | |
1303 | static struct sort_dimension switch_sort_dimension = { | |
1304 | .name = "switch", | |
1305 | .cmp = switch_cmp, | |
1306 | }; | |
1307 | struct sort_dimension *available_sorts[] = { | |
1308 | &pid_sort_dimension, | |
1309 | &avg_sort_dimension, | |
1310 | &max_sort_dimension, | |
1311 | &switch_sort_dimension, | |
1312 | &runtime_sort_dimension, | |
1313 | }; | |
daa1d7a5 | 1314 | |
0e9b07e5 | 1315 | for (i = 0; i < ARRAY_SIZE(available_sorts); i++) { |
daa1d7a5 FW |
1316 | if (!strcmp(available_sorts[i]->name, tok)) { |
1317 | list_add_tail(&available_sorts[i]->list, list); | |
1318 | ||
1319 | return 0; | |
1320 | } | |
1321 | } | |
1322 | ||
1323 | return -1; | |
1324 | } | |
1325 | ||
0e9b07e5 | 1326 | static void perf_sched__sort_lat(struct perf_sched *sched) |
daa1d7a5 FW |
1327 | { |
1328 | struct rb_node *node; | |
2f80dd44 JB |
1329 | struct rb_root *root = &sched->atom_root; |
1330 | again: | |
daa1d7a5 | 1331 | for (;;) { |
39aeb52f | 1332 | struct work_atoms *data; |
2f80dd44 | 1333 | node = rb_first(root); |
daa1d7a5 FW |
1334 | if (!node) |
1335 | break; | |
1336 | ||
2f80dd44 | 1337 | rb_erase(node, root); |
39aeb52f | 1338 | data = rb_entry(node, struct work_atoms, node); |
0e9b07e5 | 1339 | __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list); |
daa1d7a5 | 1340 | } |
2f80dd44 JB |
1341 | if (root == &sched->atom_root) { |
1342 | root = &sched->merged_atom_root; | |
1343 | goto again; | |
1344 | } | |
daa1d7a5 FW |
1345 | } |
1346 | ||
0e9b07e5 | 1347 | static int process_sched_wakeup_event(struct perf_tool *tool, |
2b7fcbc5 | 1348 | struct perf_evsel *evsel, |
1d037ca1 | 1349 | struct perf_sample *sample, |
4218e673 | 1350 | struct machine *machine) |
419ab0d6 | 1351 | { |
0e9b07e5 | 1352 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
419ab0d6 | 1353 | |
9ec3f4e4 ACM |
1354 | if (sched->tp_handler->wakeup_event) |
1355 | return sched->tp_handler->wakeup_event(sched, evsel, sample, machine); | |
a116e05d | 1356 | |
2b7fcbc5 | 1357 | return 0; |
419ab0d6 FW |
1358 | } |
1359 | ||
a151a37a JO |
1360 | union map_priv { |
1361 | void *ptr; | |
1362 | bool color; | |
1363 | }; | |
1364 | ||
1365 | static bool thread__has_color(struct thread *thread) | |
1366 | { | |
1367 | union map_priv priv = { | |
1368 | .ptr = thread__priv(thread), | |
1369 | }; | |
1370 | ||
1371 | return priv.color; | |
1372 | } | |
1373 | ||
1374 | static struct thread* | |
1375 | map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid) | |
1376 | { | |
1377 | struct thread *thread = machine__findnew_thread(machine, pid, tid); | |
1378 | union map_priv priv = { | |
1379 | .color = false, | |
1380 | }; | |
1381 | ||
1382 | if (!sched->map.color_pids || !thread || thread__priv(thread)) | |
1383 | return thread; | |
1384 | ||
1385 | if (thread_map__has(sched->map.color_pids, tid)) | |
1386 | priv.color = true; | |
1387 | ||
1388 | thread__set_priv(thread, priv.ptr); | |
1389 | return thread; | |
1390 | } | |
1391 | ||
9ec3f4e4 ACM |
1392 | static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel, |
1393 | struct perf_sample *sample, struct machine *machine) | |
0ec04e16 | 1394 | { |
9d372ca5 DY |
1395 | const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
1396 | struct thread *sched_in; | |
0ec04e16 | 1397 | int new_shortname; |
7f7f8d0b | 1398 | u64 timestamp0, timestamp = sample->time; |
0ec04e16 | 1399 | s64 delta; |
99623c62 JO |
1400 | int i, this_cpu = sample->cpu; |
1401 | int cpus_nr; | |
1402 | bool new_cpu = false; | |
8cd91195 | 1403 | const char *color = PERF_COLOR_NORMAL; |
0ec04e16 IM |
1404 | |
1405 | BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); | |
1406 | ||
0e9b07e5 ACM |
1407 | if (this_cpu > sched->max_cpu) |
1408 | sched->max_cpu = this_cpu; | |
0ec04e16 | 1409 | |
99623c62 JO |
1410 | if (sched->map.comp) { |
1411 | cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS); | |
1412 | if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) { | |
1413 | sched->map.comp_cpus[cpus_nr++] = this_cpu; | |
1414 | new_cpu = true; | |
1415 | } | |
1416 | } else | |
1417 | cpus_nr = sched->max_cpu; | |
1418 | ||
0e9b07e5 ACM |
1419 | timestamp0 = sched->cpu_last_switched[this_cpu]; |
1420 | sched->cpu_last_switched[this_cpu] = timestamp; | |
0ec04e16 IM |
1421 | if (timestamp0) |
1422 | delta = timestamp - timestamp0; | |
1423 | else | |
1424 | delta = 0; | |
1425 | ||
a116e05d | 1426 | if (delta < 0) { |
60b7d14a | 1427 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
a116e05d ACM |
1428 | return -1; |
1429 | } | |
0ec04e16 | 1430 | |
a151a37a | 1431 | sched_in = map__findnew_thread(sched, machine, -1, next_pid); |
b91fc39f ACM |
1432 | if (sched_in == NULL) |
1433 | return -1; | |
0ec04e16 | 1434 | |
b91fc39f | 1435 | sched->curr_thread[this_cpu] = thread__get(sched_in); |
0ec04e16 IM |
1436 | |
1437 | printf(" "); | |
1438 | ||
1439 | new_shortname = 0; | |
1440 | if (!sched_in->shortname[0]) { | |
6bcab4e1 D |
1441 | if (!strcmp(thread__comm_str(sched_in), "swapper")) { |
1442 | /* | |
1443 | * Don't allocate a letter-number for swapper:0 | |
1444 | * as a shortname. Instead, we use '.' for it. | |
1445 | */ | |
1446 | sched_in->shortname[0] = '.'; | |
1447 | sched_in->shortname[1] = ' '; | |
0ec04e16 | 1448 | } else { |
6bcab4e1 D |
1449 | sched_in->shortname[0] = sched->next_shortname1; |
1450 | sched_in->shortname[1] = sched->next_shortname2; | |
1451 | ||
1452 | if (sched->next_shortname1 < 'Z') { | |
1453 | sched->next_shortname1++; | |
0ec04e16 | 1454 | } else { |
6bcab4e1 D |
1455 | sched->next_shortname1 = 'A'; |
1456 | if (sched->next_shortname2 < '9') | |
1457 | sched->next_shortname2++; | |
1458 | else | |
1459 | sched->next_shortname2 = '0'; | |
0ec04e16 IM |
1460 | } |
1461 | } | |
1462 | new_shortname = 1; | |
1463 | } | |
1464 | ||
99623c62 JO |
1465 | for (i = 0; i < cpus_nr; i++) { |
1466 | int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i; | |
a151a37a JO |
1467 | struct thread *curr_thread = sched->curr_thread[cpu]; |
1468 | const char *pid_color = color; | |
cf294f24 | 1469 | const char *cpu_color = color; |
a151a37a JO |
1470 | |
1471 | if (curr_thread && thread__has_color(curr_thread)) | |
1472 | pid_color = COLOR_PIDS; | |
99623c62 | 1473 | |
73643bb6 JO |
1474 | if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu)) |
1475 | continue; | |
1476 | ||
cf294f24 JO |
1477 | if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu)) |
1478 | cpu_color = COLOR_CPUS; | |
1479 | ||
0ec04e16 | 1480 | if (cpu != this_cpu) |
cf294f24 | 1481 | color_fprintf(stdout, cpu_color, " "); |
0ec04e16 | 1482 | else |
cf294f24 | 1483 | color_fprintf(stdout, cpu_color, "*"); |
0ec04e16 | 1484 | |
6bcab4e1 | 1485 | if (sched->curr_thread[cpu]) |
a151a37a | 1486 | color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname); |
6bcab4e1 | 1487 | else |
8cd91195 | 1488 | color_fprintf(stdout, color, " "); |
0ec04e16 IM |
1489 | } |
1490 | ||
73643bb6 JO |
1491 | if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu)) |
1492 | goto out; | |
1493 | ||
8cd91195 | 1494 | color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp/1e9); |
0ec04e16 | 1495 | if (new_shortname) { |
a151a37a JO |
1496 | const char *pid_color = color; |
1497 | ||
1498 | if (thread__has_color(sched_in)) | |
1499 | pid_color = COLOR_PIDS; | |
1500 | ||
1501 | color_fprintf(stdout, pid_color, "%s => %s:%d", | |
b9c5143a | 1502 | sched_in->shortname, thread__comm_str(sched_in), sched_in->tid); |
0ec04e16 | 1503 | } |
a116e05d | 1504 | |
99623c62 | 1505 | if (sched->map.comp && new_cpu) |
8cd91195 | 1506 | color_fprintf(stdout, color, " (CPU %d)", this_cpu); |
99623c62 | 1507 | |
73643bb6 | 1508 | out: |
8cd91195 | 1509 | color_fprintf(stdout, color, "\n"); |
99623c62 | 1510 | |
b91fc39f ACM |
1511 | thread__put(sched_in); |
1512 | ||
a116e05d | 1513 | return 0; |
0ec04e16 IM |
1514 | } |
1515 | ||
0e9b07e5 | 1516 | static int process_sched_switch_event(struct perf_tool *tool, |
2b7fcbc5 | 1517 | struct perf_evsel *evsel, |
1d037ca1 | 1518 | struct perf_sample *sample, |
4218e673 | 1519 | struct machine *machine) |
419ab0d6 | 1520 | { |
0e9b07e5 | 1521 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
a116e05d | 1522 | int this_cpu = sample->cpu, err = 0; |
2b7fcbc5 ACM |
1523 | u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
1524 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); | |
419ab0d6 | 1525 | |
0e9b07e5 | 1526 | if (sched->curr_pid[this_cpu] != (u32)-1) { |
c8a37751 IM |
1527 | /* |
1528 | * Are we trying to switch away a PID that is | |
1529 | * not current? | |
1530 | */ | |
2b7fcbc5 | 1531 | if (sched->curr_pid[this_cpu] != prev_pid) |
0e9b07e5 | 1532 | sched->nr_context_switch_bugs++; |
c8a37751 | 1533 | } |
c8a37751 | 1534 | |
9ec3f4e4 ACM |
1535 | if (sched->tp_handler->switch_event) |
1536 | err = sched->tp_handler->switch_event(sched, evsel, sample, machine); | |
2b7fcbc5 ACM |
1537 | |
1538 | sched->curr_pid[this_cpu] = next_pid; | |
a116e05d | 1539 | return err; |
419ab0d6 FW |
1540 | } |
1541 | ||
0e9b07e5 | 1542 | static int process_sched_runtime_event(struct perf_tool *tool, |
2b7fcbc5 | 1543 | struct perf_evsel *evsel, |
1d037ca1 | 1544 | struct perf_sample *sample, |
4218e673 | 1545 | struct machine *machine) |
39aeb52f | 1546 | { |
0e9b07e5 | 1547 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
39aeb52f | 1548 | |
9ec3f4e4 ACM |
1549 | if (sched->tp_handler->runtime_event) |
1550 | return sched->tp_handler->runtime_event(sched, evsel, sample, machine); | |
a116e05d | 1551 | |
2b7fcbc5 | 1552 | return 0; |
39aeb52f | 1553 | } |
1554 | ||
cb627505 DA |
1555 | static int perf_sched__process_fork_event(struct perf_tool *tool, |
1556 | union perf_event *event, | |
1557 | struct perf_sample *sample, | |
1558 | struct machine *machine) | |
fbf94829 | 1559 | { |
0e9b07e5 | 1560 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
46538818 | 1561 | |
cb627505 DA |
1562 | /* run the fork event through the perf machineruy */ |
1563 | perf_event__process_fork(tool, event, sample, machine); | |
1564 | ||
1565 | /* and then run additional processing needed for this command */ | |
9ec3f4e4 | 1566 | if (sched->tp_handler->fork_event) |
cb627505 | 1567 | return sched->tp_handler->fork_event(sched, event, machine); |
a116e05d | 1568 | |
2b7fcbc5 | 1569 | return 0; |
fbf94829 IM |
1570 | } |
1571 | ||
0e9b07e5 | 1572 | static int process_sched_migrate_task_event(struct perf_tool *tool, |
2b7fcbc5 | 1573 | struct perf_evsel *evsel, |
1d037ca1 | 1574 | struct perf_sample *sample, |
4218e673 | 1575 | struct machine *machine) |
55ffb7a6 | 1576 | { |
0e9b07e5 | 1577 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
55ffb7a6 | 1578 | |
9ec3f4e4 ACM |
1579 | if (sched->tp_handler->migrate_task_event) |
1580 | return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine); | |
a116e05d | 1581 | |
2b7fcbc5 | 1582 | return 0; |
55ffb7a6 MG |
1583 | } |
1584 | ||
a116e05d | 1585 | typedef int (*tracepoint_handler)(struct perf_tool *tool, |
2b7fcbc5 | 1586 | struct perf_evsel *evsel, |
a116e05d | 1587 | struct perf_sample *sample, |
4218e673 | 1588 | struct machine *machine); |
ec156764 | 1589 | |
1d037ca1 IT |
1590 | static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused, |
1591 | union perf_event *event __maybe_unused, | |
ee29be62 ACM |
1592 | struct perf_sample *sample, |
1593 | struct perf_evsel *evsel, | |
1594 | struct machine *machine) | |
0a02ad93 | 1595 | { |
a116e05d | 1596 | int err = 0; |
0a02ad93 | 1597 | |
744a9719 ACM |
1598 | if (evsel->handler != NULL) { |
1599 | tracepoint_handler f = evsel->handler; | |
2b7fcbc5 | 1600 | err = f(tool, evsel, sample, machine); |
ee29be62 | 1601 | } |
0a02ad93 | 1602 | |
a116e05d | 1603 | return err; |
0a02ad93 IM |
1604 | } |
1605 | ||
ae536acf | 1606 | static int perf_sched__read_events(struct perf_sched *sched) |
0a02ad93 | 1607 | { |
ee29be62 ACM |
1608 | const struct perf_evsel_str_handler handlers[] = { |
1609 | { "sched:sched_switch", process_sched_switch_event, }, | |
1610 | { "sched:sched_stat_runtime", process_sched_runtime_event, }, | |
1611 | { "sched:sched_wakeup", process_sched_wakeup_event, }, | |
1612 | { "sched:sched_wakeup_new", process_sched_wakeup_event, }, | |
ee29be62 ACM |
1613 | { "sched:sched_migrate_task", process_sched_migrate_task_event, }, |
1614 | }; | |
da378962 | 1615 | struct perf_session *session; |
f5fc1412 JO |
1616 | struct perf_data_file file = { |
1617 | .path = input_name, | |
1618 | .mode = PERF_DATA_MODE_READ, | |
f0dd330f | 1619 | .force = sched->force, |
f5fc1412 | 1620 | }; |
ae536acf | 1621 | int rc = -1; |
da378962 | 1622 | |
f5fc1412 | 1623 | session = perf_session__new(&file, false, &sched->tool); |
a116e05d ACM |
1624 | if (session == NULL) { |
1625 | pr_debug("No Memory for session\n"); | |
1626 | return -1; | |
1627 | } | |
94c744b6 | 1628 | |
0a7e6d1b | 1629 | symbol__init(&session->header.env); |
04934106 | 1630 | |
a116e05d ACM |
1631 | if (perf_session__set_tracepoints_handlers(session, handlers)) |
1632 | goto out_delete; | |
ee29be62 | 1633 | |
cee75ac7 | 1634 | if (perf_session__has_traces(session, "record -R")) { |
b7b61cbe | 1635 | int err = perf_session__process_events(session); |
a116e05d ACM |
1636 | if (err) { |
1637 | pr_err("Failed to process events, error %d", err); | |
1638 | goto out_delete; | |
1639 | } | |
4c09bafa | 1640 | |
75be989a ACM |
1641 | sched->nr_events = session->evlist->stats.nr_events[0]; |
1642 | sched->nr_lost_events = session->evlist->stats.total_lost; | |
1643 | sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; | |
cee75ac7 | 1644 | } |
d549c769 | 1645 | |
ae536acf | 1646 | rc = 0; |
a116e05d ACM |
1647 | out_delete: |
1648 | perf_session__delete(session); | |
ae536acf | 1649 | return rc; |
0a02ad93 IM |
1650 | } |
1651 | ||
0e9b07e5 | 1652 | static void print_bad_events(struct perf_sched *sched) |
0ec04e16 | 1653 | { |
0e9b07e5 | 1654 | if (sched->nr_unordered_timestamps && sched->nr_timestamps) { |
0ec04e16 | 1655 | printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n", |
0e9b07e5 ACM |
1656 | (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0, |
1657 | sched->nr_unordered_timestamps, sched->nr_timestamps); | |
0ec04e16 | 1658 | } |
0e9b07e5 | 1659 | if (sched->nr_lost_events && sched->nr_events) { |
0ec04e16 | 1660 | printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n", |
0e9b07e5 ACM |
1661 | (double)sched->nr_lost_events/(double)sched->nr_events * 100.0, |
1662 | sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks); | |
0ec04e16 | 1663 | } |
0e9b07e5 | 1664 | if (sched->nr_context_switch_bugs && sched->nr_timestamps) { |
0ec04e16 | 1665 | printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)", |
0e9b07e5 ACM |
1666 | (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0, |
1667 | sched->nr_context_switch_bugs, sched->nr_timestamps); | |
1668 | if (sched->nr_lost_events) | |
0ec04e16 IM |
1669 | printf(" (due to lost events?)"); |
1670 | printf("\n"); | |
1671 | } | |
1672 | } | |
1673 | ||
2f80dd44 JB |
1674 | static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data) |
1675 | { | |
1676 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
1677 | struct work_atoms *this; | |
1678 | const char *comm = thread__comm_str(data->thread), *this_comm; | |
1679 | ||
1680 | while (*new) { | |
1681 | int cmp; | |
1682 | ||
1683 | this = container_of(*new, struct work_atoms, node); | |
1684 | parent = *new; | |
1685 | ||
1686 | this_comm = thread__comm_str(this->thread); | |
1687 | cmp = strcmp(comm, this_comm); | |
1688 | if (cmp > 0) { | |
1689 | new = &((*new)->rb_left); | |
1690 | } else if (cmp < 0) { | |
1691 | new = &((*new)->rb_right); | |
1692 | } else { | |
1693 | this->num_merged++; | |
1694 | this->total_runtime += data->total_runtime; | |
1695 | this->nb_atoms += data->nb_atoms; | |
1696 | this->total_lat += data->total_lat; | |
1697 | list_splice(&data->work_list, &this->work_list); | |
1698 | if (this->max_lat < data->max_lat) { | |
1699 | this->max_lat = data->max_lat; | |
1700 | this->max_lat_at = data->max_lat_at; | |
1701 | } | |
1702 | zfree(&data); | |
1703 | return; | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | data->num_merged++; | |
1708 | rb_link_node(&data->node, parent, new); | |
1709 | rb_insert_color(&data->node, root); | |
1710 | } | |
1711 | ||
1712 | static void perf_sched__merge_lat(struct perf_sched *sched) | |
1713 | { | |
1714 | struct work_atoms *data; | |
1715 | struct rb_node *node; | |
1716 | ||
1717 | if (sched->skip_merge) | |
1718 | return; | |
1719 | ||
1720 | while ((node = rb_first(&sched->atom_root))) { | |
1721 | rb_erase(node, &sched->atom_root); | |
1722 | data = rb_entry(node, struct work_atoms, node); | |
1723 | __merge_work_atoms(&sched->merged_atom_root, data); | |
1724 | } | |
1725 | } | |
1726 | ||
0e9b07e5 | 1727 | static int perf_sched__lat(struct perf_sched *sched) |
0ec04e16 IM |
1728 | { |
1729 | struct rb_node *next; | |
1730 | ||
1731 | setup_pager(); | |
ad9def7c | 1732 | |
ae536acf | 1733 | if (perf_sched__read_events(sched)) |
a116e05d | 1734 | return -1; |
ad9def7c | 1735 | |
2f80dd44 | 1736 | perf_sched__merge_lat(sched); |
0e9b07e5 | 1737 | perf_sched__sort_lat(sched); |
0ec04e16 | 1738 | |
80790e0b RR |
1739 | printf("\n -----------------------------------------------------------------------------------------------------------------\n"); |
1740 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n"); | |
1741 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); | |
0ec04e16 | 1742 | |
0e9b07e5 | 1743 | next = rb_first(&sched->sorted_atom_root); |
0ec04e16 IM |
1744 | |
1745 | while (next) { | |
1746 | struct work_atoms *work_list; | |
1747 | ||
1748 | work_list = rb_entry(next, struct work_atoms, node); | |
0e9b07e5 | 1749 | output_lat_thread(sched, work_list); |
0ec04e16 | 1750 | next = rb_next(next); |
ae536acf | 1751 | thread__zput(work_list->thread); |
0ec04e16 IM |
1752 | } |
1753 | ||
80790e0b | 1754 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); |
9486aa38 | 1755 | printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n", |
0e9b07e5 | 1756 | (double)sched->all_runtime / 1e6, sched->all_count); |
0ec04e16 IM |
1757 | |
1758 | printf(" ---------------------------------------------------\n"); | |
1759 | ||
0e9b07e5 | 1760 | print_bad_events(sched); |
0ec04e16 IM |
1761 | printf("\n"); |
1762 | ||
a116e05d | 1763 | return 0; |
0ec04e16 IM |
1764 | } |
1765 | ||
99623c62 JO |
1766 | static int setup_map_cpus(struct perf_sched *sched) |
1767 | { | |
73643bb6 JO |
1768 | struct cpu_map *map; |
1769 | ||
99623c62 JO |
1770 | sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF); |
1771 | ||
1772 | if (sched->map.comp) { | |
1773 | sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int)); | |
cf294f24 JO |
1774 | if (!sched->map.comp_cpus) |
1775 | return -1; | |
99623c62 JO |
1776 | } |
1777 | ||
73643bb6 JO |
1778 | if (!sched->map.cpus_str) |
1779 | return 0; | |
1780 | ||
1781 | map = cpu_map__new(sched->map.cpus_str); | |
1782 | if (!map) { | |
1783 | pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); | |
1784 | return -1; | |
1785 | } | |
1786 | ||
1787 | sched->map.cpus = map; | |
99623c62 JO |
1788 | return 0; |
1789 | } | |
1790 | ||
a151a37a JO |
1791 | static int setup_color_pids(struct perf_sched *sched) |
1792 | { | |
1793 | struct thread_map *map; | |
1794 | ||
1795 | if (!sched->map.color_pids_str) | |
1796 | return 0; | |
1797 | ||
1798 | map = thread_map__new_by_tid_str(sched->map.color_pids_str); | |
1799 | if (!map) { | |
1800 | pr_err("failed to get thread map from %s\n", sched->map.color_pids_str); | |
1801 | return -1; | |
1802 | } | |
1803 | ||
1804 | sched->map.color_pids = map; | |
1805 | return 0; | |
1806 | } | |
1807 | ||
cf294f24 JO |
1808 | static int setup_color_cpus(struct perf_sched *sched) |
1809 | { | |
1810 | struct cpu_map *map; | |
1811 | ||
1812 | if (!sched->map.color_cpus_str) | |
1813 | return 0; | |
1814 | ||
1815 | map = cpu_map__new(sched->map.color_cpus_str); | |
1816 | if (!map) { | |
1817 | pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str); | |
1818 | return -1; | |
1819 | } | |
1820 | ||
1821 | sched->map.color_cpus = map; | |
1822 | return 0; | |
1823 | } | |
1824 | ||
0e9b07e5 | 1825 | static int perf_sched__map(struct perf_sched *sched) |
0ec04e16 | 1826 | { |
99623c62 JO |
1827 | if (setup_map_cpus(sched)) |
1828 | return -1; | |
40749d0f | 1829 | |
a151a37a JO |
1830 | if (setup_color_pids(sched)) |
1831 | return -1; | |
1832 | ||
cf294f24 JO |
1833 | if (setup_color_cpus(sched)) |
1834 | return -1; | |
1835 | ||
0ec04e16 | 1836 | setup_pager(); |
ae536acf | 1837 | if (perf_sched__read_events(sched)) |
a116e05d | 1838 | return -1; |
0e9b07e5 | 1839 | print_bad_events(sched); |
a116e05d | 1840 | return 0; |
0ec04e16 IM |
1841 | } |
1842 | ||
0e9b07e5 | 1843 | static int perf_sched__replay(struct perf_sched *sched) |
0ec04e16 IM |
1844 | { |
1845 | unsigned long i; | |
1846 | ||
0e9b07e5 ACM |
1847 | calibrate_run_measurement_overhead(sched); |
1848 | calibrate_sleep_measurement_overhead(sched); | |
0ec04e16 | 1849 | |
0e9b07e5 | 1850 | test_calibrations(sched); |
0ec04e16 | 1851 | |
ae536acf | 1852 | if (perf_sched__read_events(sched)) |
a116e05d | 1853 | return -1; |
0ec04e16 | 1854 | |
0e9b07e5 ACM |
1855 | printf("nr_run_events: %ld\n", sched->nr_run_events); |
1856 | printf("nr_sleep_events: %ld\n", sched->nr_sleep_events); | |
1857 | printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events); | |
0ec04e16 | 1858 | |
0e9b07e5 ACM |
1859 | if (sched->targetless_wakeups) |
1860 | printf("target-less wakeups: %ld\n", sched->targetless_wakeups); | |
1861 | if (sched->multitarget_wakeups) | |
1862 | printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups); | |
1863 | if (sched->nr_run_events_optimized) | |
0ec04e16 | 1864 | printf("run atoms optimized: %ld\n", |
0e9b07e5 | 1865 | sched->nr_run_events_optimized); |
0ec04e16 | 1866 | |
0e9b07e5 ACM |
1867 | print_task_traces(sched); |
1868 | add_cross_task_wakeups(sched); | |
0ec04e16 | 1869 | |
0e9b07e5 | 1870 | create_tasks(sched); |
0ec04e16 | 1871 | printf("------------------------------------------------------------\n"); |
0e9b07e5 ACM |
1872 | for (i = 0; i < sched->replay_repeat; i++) |
1873 | run_one_test(sched); | |
a116e05d ACM |
1874 | |
1875 | return 0; | |
0ec04e16 IM |
1876 | } |
1877 | ||
0e9b07e5 ACM |
1878 | static void setup_sorting(struct perf_sched *sched, const struct option *options, |
1879 | const char * const usage_msg[]) | |
daa1d7a5 | 1880 | { |
0e9b07e5 | 1881 | char *tmp, *tok, *str = strdup(sched->sort_order); |
daa1d7a5 FW |
1882 | |
1883 | for (tok = strtok_r(str, ", ", &tmp); | |
1884 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
0e9b07e5 | 1885 | if (sort_dimension__add(tok, &sched->sort_list) < 0) { |
c7118369 NK |
1886 | usage_with_options_msg(usage_msg, options, |
1887 | "Unknown --sort key: `%s'", tok); | |
daa1d7a5 FW |
1888 | } |
1889 | } | |
1890 | ||
1891 | free(str); | |
1892 | ||
0e9b07e5 | 1893 | sort_dimension__add("pid", &sched->cmp_pid); |
daa1d7a5 FW |
1894 | } |
1895 | ||
1fc35b29 IM |
1896 | static int __cmd_record(int argc, const char **argv) |
1897 | { | |
1898 | unsigned int rec_argc, i, j; | |
1899 | const char **rec_argv; | |
0e9b07e5 ACM |
1900 | const char * const record_args[] = { |
1901 | "record", | |
1902 | "-a", | |
1903 | "-R", | |
0e9b07e5 ACM |
1904 | "-m", "1024", |
1905 | "-c", "1", | |
1906 | "-e", "sched:sched_switch", | |
1907 | "-e", "sched:sched_stat_wait", | |
1908 | "-e", "sched:sched_stat_sleep", | |
1909 | "-e", "sched:sched_stat_iowait", | |
1910 | "-e", "sched:sched_stat_runtime", | |
0e9b07e5 ACM |
1911 | "-e", "sched:sched_process_fork", |
1912 | "-e", "sched:sched_wakeup", | |
7fff9597 | 1913 | "-e", "sched:sched_wakeup_new", |
0e9b07e5 ACM |
1914 | "-e", "sched:sched_migrate_task", |
1915 | }; | |
1fc35b29 IM |
1916 | |
1917 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
1918 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | |
1919 | ||
e462dc55 | 1920 | if (rec_argv == NULL) |
ce47dc56 CS |
1921 | return -ENOMEM; |
1922 | ||
1fc35b29 IM |
1923 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
1924 | rec_argv[i] = strdup(record_args[i]); | |
1925 | ||
1926 | for (j = 1; j < (unsigned int)argc; j++, i++) | |
1927 | rec_argv[i] = argv[j]; | |
1928 | ||
1929 | BUG_ON(i != rec_argc); | |
1930 | ||
1931 | return cmd_record(i, rec_argv, NULL); | |
1932 | } | |
1933 | ||
1d037ca1 | 1934 | int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused) |
0a02ad93 | 1935 | { |
8a39df8f AH |
1936 | const char default_sort_order[] = "avg, max, switch, runtime"; |
1937 | struct perf_sched sched = { | |
1938 | .tool = { | |
1939 | .sample = perf_sched__process_tracepoint_sample, | |
1940 | .comm = perf_event__process_comm, | |
1941 | .lost = perf_event__process_lost, | |
1942 | .fork = perf_sched__process_fork_event, | |
0a8cb85c | 1943 | .ordered_events = true, |
8a39df8f AH |
1944 | }, |
1945 | .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid), | |
1946 | .sort_list = LIST_HEAD_INIT(sched.sort_list), | |
1947 | .start_work_mutex = PTHREAD_MUTEX_INITIALIZER, | |
1948 | .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER, | |
8a39df8f AH |
1949 | .sort_order = default_sort_order, |
1950 | .replay_repeat = 10, | |
1951 | .profile_cpu = -1, | |
1952 | .next_shortname1 = 'A', | |
1953 | .next_shortname2 = '0', | |
2f80dd44 | 1954 | .skip_merge = 0, |
8a39df8f | 1955 | }; |
0e9b07e5 ACM |
1956 | const struct option latency_options[] = { |
1957 | OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]", | |
1958 | "sort by key(s): runtime, switch, avg, max"), | |
1959 | OPT_INCR('v', "verbose", &verbose, | |
1960 | "be more verbose (show symbol address, etc)"), | |
1961 | OPT_INTEGER('C', "CPU", &sched.profile_cpu, | |
1962 | "CPU to profile on"), | |
1963 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
1964 | "dump raw trace in ASCII"), | |
2f80dd44 JB |
1965 | OPT_BOOLEAN('p', "pids", &sched.skip_merge, |
1966 | "latency stats per pid instead of per comm"), | |
0e9b07e5 ACM |
1967 | OPT_END() |
1968 | }; | |
1969 | const struct option replay_options[] = { | |
1970 | OPT_UINTEGER('r', "repeat", &sched.replay_repeat, | |
1971 | "repeat the workload replay N times (-1: infinite)"), | |
1972 | OPT_INCR('v', "verbose", &verbose, | |
1973 | "be more verbose (show symbol address, etc)"), | |
1974 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
1975 | "dump raw trace in ASCII"), | |
939cda52 | 1976 | OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"), |
0e9b07e5 ACM |
1977 | OPT_END() |
1978 | }; | |
1979 | const struct option sched_options[] = { | |
70cb4e96 | 1980 | OPT_STRING('i', "input", &input_name, "file", |
0e9b07e5 ACM |
1981 | "input file name"), |
1982 | OPT_INCR('v', "verbose", &verbose, | |
1983 | "be more verbose (show symbol address, etc)"), | |
1984 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
1985 | "dump raw trace in ASCII"), | |
1986 | OPT_END() | |
1987 | }; | |
99623c62 JO |
1988 | const struct option map_options[] = { |
1989 | OPT_BOOLEAN(0, "compact", &sched.map.comp, | |
1990 | "map output in compact mode"), | |
a151a37a JO |
1991 | OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids", |
1992 | "highlight given pids in map"), | |
cf294f24 JO |
1993 | OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus", |
1994 | "highlight given CPUs in map"), | |
73643bb6 JO |
1995 | OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus", |
1996 | "display given CPUs in map"), | |
99623c62 JO |
1997 | OPT_END() |
1998 | }; | |
0e9b07e5 ACM |
1999 | const char * const latency_usage[] = { |
2000 | "perf sched latency [<options>]", | |
2001 | NULL | |
2002 | }; | |
2003 | const char * const replay_usage[] = { | |
2004 | "perf sched replay [<options>]", | |
2005 | NULL | |
2006 | }; | |
99623c62 JO |
2007 | const char * const map_usage[] = { |
2008 | "perf sched map [<options>]", | |
2009 | NULL | |
2010 | }; | |
a83edb2d RR |
2011 | const char *const sched_subcommands[] = { "record", "latency", "map", |
2012 | "replay", "script", NULL }; | |
2013 | const char *sched_usage[] = { | |
2014 | NULL, | |
0e9b07e5 ACM |
2015 | NULL |
2016 | }; | |
2017 | struct trace_sched_handler lat_ops = { | |
2018 | .wakeup_event = latency_wakeup_event, | |
2019 | .switch_event = latency_switch_event, | |
2020 | .runtime_event = latency_runtime_event, | |
0e9b07e5 ACM |
2021 | .migrate_task_event = latency_migrate_task_event, |
2022 | }; | |
2023 | struct trace_sched_handler map_ops = { | |
2024 | .switch_event = map_switch_event, | |
2025 | }; | |
2026 | struct trace_sched_handler replay_ops = { | |
2027 | .wakeup_event = replay_wakeup_event, | |
2028 | .switch_event = replay_switch_event, | |
2029 | .fork_event = replay_fork_event, | |
2030 | }; | |
156a2b02 AH |
2031 | unsigned int i; |
2032 | ||
2033 | for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++) | |
2034 | sched.curr_pid[i] = -1; | |
0e9b07e5 | 2035 | |
a83edb2d RR |
2036 | argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands, |
2037 | sched_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
f2858d8a IM |
2038 | if (!argc) |
2039 | usage_with_options(sched_usage, sched_options); | |
0a02ad93 | 2040 | |
c0777c5a | 2041 | /* |
133dc4c3 | 2042 | * Aliased to 'perf script' for now: |
c0777c5a | 2043 | */ |
133dc4c3 IM |
2044 | if (!strcmp(argv[0], "script")) |
2045 | return cmd_script(argc, argv, prefix); | |
c0777c5a | 2046 | |
1fc35b29 IM |
2047 | if (!strncmp(argv[0], "rec", 3)) { |
2048 | return __cmd_record(argc, argv); | |
2049 | } else if (!strncmp(argv[0], "lat", 3)) { | |
0e9b07e5 | 2050 | sched.tp_handler = &lat_ops; |
f2858d8a IM |
2051 | if (argc > 1) { |
2052 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); | |
2053 | if (argc) | |
2054 | usage_with_options(latency_usage, latency_options); | |
f2858d8a | 2055 | } |
0e9b07e5 ACM |
2056 | setup_sorting(&sched, latency_options, latency_usage); |
2057 | return perf_sched__lat(&sched); | |
0ec04e16 | 2058 | } else if (!strcmp(argv[0], "map")) { |
99623c62 | 2059 | if (argc) { |
a151a37a | 2060 | argc = parse_options(argc, argv, map_options, map_usage, 0); |
99623c62 JO |
2061 | if (argc) |
2062 | usage_with_options(map_usage, map_options); | |
2063 | } | |
0e9b07e5 ACM |
2064 | sched.tp_handler = &map_ops; |
2065 | setup_sorting(&sched, latency_options, latency_usage); | |
2066 | return perf_sched__map(&sched); | |
f2858d8a | 2067 | } else if (!strncmp(argv[0], "rep", 3)) { |
0e9b07e5 | 2068 | sched.tp_handler = &replay_ops; |
f2858d8a IM |
2069 | if (argc) { |
2070 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); | |
2071 | if (argc) | |
2072 | usage_with_options(replay_usage, replay_options); | |
2073 | } | |
0e9b07e5 | 2074 | return perf_sched__replay(&sched); |
f2858d8a IM |
2075 | } else { |
2076 | usage_with_options(sched_usage, sched_options); | |
2077 | } | |
2078 | ||
ec156764 | 2079 | return 0; |
0a02ad93 | 2080 | } |