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