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