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