builtin-sched.c 44.2 KB
Newer Older
I
Ingo Molnar 已提交
1
#include "builtin.h"
2
#include "perf.h"
I
Ingo Molnar 已提交
3 4

#include "util/util.h"
5
#include "util/evlist.h"
I
Ingo Molnar 已提交
6
#include "util/cache.h"
7
#include "util/evsel.h"
I
Ingo Molnar 已提交
8 9 10
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
11
#include "util/session.h"
12
#include "util/tool.h"
13
#include "util/cloexec.h"
I
Ingo Molnar 已提交
14 15

#include "util/parse-options.h"
16
#include "util/trace-event.h"
I
Ingo Molnar 已提交
17 18 19

#include "util/debug.h"

20
#include <sys/prctl.h>
21
#include <sys/resource.h>
I
Ingo Molnar 已提交
22

23 24 25
#include <semaphore.h>
#include <pthread.h>
#include <math.h>
26
#include <api/fs/fs.h>
27

28 29 30 31
#define PR_SET_NAME		15               /* Set process name */
#define MAX_CPUS		4096
#define COMM_LEN		20
#define SYM_LEN			129
32
#define MAX_PID			1024000
I
Ingo Molnar 已提交
33

34
struct sched_atom;
I
Ingo Molnar 已提交
35

36 37 38 39
struct task_desc {
	unsigned long		nr;
	unsigned long		pid;
	char			comm[COMM_LEN];
I
Ingo Molnar 已提交
40

41 42
	unsigned long		nr_events;
	unsigned long		curr_event;
43
	struct sched_atom	**atoms;
44 45 46

	pthread_t		thread;
	sem_t			sleep_sem;
I
Ingo Molnar 已提交
47

48 49 50 51 52 53 54 55 56 57
	sem_t			ready_for_work;
	sem_t			work_done_sem;

	u64			cpu_usage;
};

enum sched_event_type {
	SCHED_EVENT_RUN,
	SCHED_EVENT_SLEEP,
	SCHED_EVENT_WAKEUP,
58
	SCHED_EVENT_MIGRATION,
59 60
};

61
struct sched_atom {
62
	enum sched_event_type	type;
63
	int			specific_wait;
64 65 66 67 68 69 70
	u64			timestamp;
	u64			duration;
	unsigned long		nr;
	sem_t			*wait_sem;
	struct task_desc	*wakee;
};

71
#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
72 73 74 75 76 77 78 79 80 81 82

enum thread_state {
	THREAD_SLEEPING = 0,
	THREAD_WAIT_CPU,
	THREAD_SCHED_IN,
	THREAD_IGNORE
};

struct work_atom {
	struct list_head	list;
	enum thread_state	state;
83
	u64			sched_out_time;
84 85 86 87 88
	u64			wake_up_time;
	u64			sched_in_time;
	u64			runtime;
};

89 90
struct work_atoms {
	struct list_head	work_list;
91 92 93
	struct thread		*thread;
	struct rb_node		node;
	u64			max_lat;
94
	u64			max_lat_at;
95 96 97 98 99
	u64			total_lat;
	u64			nb_atoms;
	u64			total_runtime;
};

100
typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
101

102
struct perf_sched;
103

104 105 106
struct trace_sched_handler {
	int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
			    struct perf_sample *sample, struct machine *machine);
107

108 109
	int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
			     struct perf_sample *sample, struct machine *machine);
110

111 112
	int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
			    struct perf_sample *sample, struct machine *machine);
113

114 115 116
	/* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
	int (*fork_event)(struct perf_sched *sched, union perf_event *event,
			  struct machine *machine);
117 118

	int (*migrate_task_event)(struct perf_sched *sched,
119 120 121
				  struct perf_evsel *evsel,
				  struct perf_sample *sample,
				  struct machine *machine);
122 123 124 125 126 127
};

struct perf_sched {
	struct perf_tool tool;
	const char	 *sort_order;
	unsigned long	 nr_tasks;
128
	struct task_desc **pid_to_task;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	struct task_desc **tasks;
	const struct trace_sched_handler *tp_handler;
	pthread_mutex_t	 start_work_mutex;
	pthread_mutex_t	 work_done_wait_mutex;
	int		 profile_cpu;
/*
 * Track the current task - that way we can know whether there's any
 * weird events, such as a task being switched away that is not current.
 */
	int		 max_cpu;
	u32		 curr_pid[MAX_CPUS];
	struct thread	 *curr_thread[MAX_CPUS];
	char		 next_shortname1;
	char		 next_shortname2;
	unsigned int	 replay_repeat;
	unsigned long	 nr_run_events;
	unsigned long	 nr_sleep_events;
	unsigned long	 nr_wakeup_events;
	unsigned long	 nr_sleep_corrections;
	unsigned long	 nr_run_events_optimized;
	unsigned long	 targetless_wakeups;
	unsigned long	 multitarget_wakeups;
	unsigned long	 nr_runs;
	unsigned long	 nr_timestamps;
	unsigned long	 nr_unordered_timestamps;
	unsigned long	 nr_context_switch_bugs;
	unsigned long	 nr_events;
	unsigned long	 nr_lost_chunks;
	unsigned long	 nr_lost_events;
	u64		 run_measurement_overhead;
	u64		 sleep_measurement_overhead;
	u64		 start_time;
	u64		 cpu_usage;
	u64		 runavg_cpu_usage;
	u64		 parent_cpu_usage;
	u64		 runavg_parent_cpu_usage;
	u64		 sum_runtime;
	u64		 sum_fluct;
	u64		 run_avg;
	u64		 all_runtime;
	u64		 all_count;
	u64		 cpu_last_switched[MAX_CPUS];
	struct rb_root	 atom_root, sorted_atom_root;
	struct list_head sort_list, cmp_pid;
};
174 175

static u64 get_nsecs(void)
I
Ingo Molnar 已提交
176 177 178 179 180 181 182 183
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);

	return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
}

184
static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
I
Ingo Molnar 已提交
185
{
186
	u64 T0 = get_nsecs(), T1;
I
Ingo Molnar 已提交
187 188 189

	do {
		T1 = get_nsecs();
190
	} while (T1 + sched->run_measurement_overhead < T0 + nsecs);
I
Ingo Molnar 已提交
191 192
}

193
static void sleep_nsecs(u64 nsecs)
I
Ingo Molnar 已提交
194 195 196 197 198 199 200 201 202
{
	struct timespec ts;

	ts.tv_nsec = nsecs % 999999999;
	ts.tv_sec = nsecs / 999999999;

	nanosleep(&ts, NULL);
}

203
static void calibrate_run_measurement_overhead(struct perf_sched *sched)
I
Ingo Molnar 已提交
204
{
205
	u64 T0, T1, delta, min_delta = 1000000000ULL;
I
Ingo Molnar 已提交
206 207 208 209
	int i;

	for (i = 0; i < 10; i++) {
		T0 = get_nsecs();
210
		burn_nsecs(sched, 0);
I
Ingo Molnar 已提交
211 212 213 214
		T1 = get_nsecs();
		delta = T1-T0;
		min_delta = min(min_delta, delta);
	}
215
	sched->run_measurement_overhead = min_delta;
I
Ingo Molnar 已提交
216

217
	printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
I
Ingo Molnar 已提交
218 219
}

220
static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
I
Ingo Molnar 已提交
221
{
222
	u64 T0, T1, delta, min_delta = 1000000000ULL;
I
Ingo Molnar 已提交
223 224 225 226 227 228 229 230 231 232
	int i;

	for (i = 0; i < 10; i++) {
		T0 = get_nsecs();
		sleep_nsecs(10000);
		T1 = get_nsecs();
		delta = T1-T0;
		min_delta = min(min_delta, delta);
	}
	min_delta -= 10000;
233
	sched->sleep_measurement_overhead = min_delta;
I
Ingo Molnar 已提交
234

235
	printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
I
Ingo Molnar 已提交
236 237
}

238
static struct sched_atom *
239
get_new_event(struct task_desc *task, u64 timestamp)
I
Ingo Molnar 已提交
240
{
241
	struct sched_atom *event = zalloc(sizeof(*event));
I
Ingo Molnar 已提交
242 243 244 245 246 247 248
	unsigned long idx = task->nr_events;
	size_t size;

	event->timestamp = timestamp;
	event->nr = idx;

	task->nr_events++;
249 250 251
	size = sizeof(struct sched_atom *) * task->nr_events;
	task->atoms = realloc(task->atoms, size);
	BUG_ON(!task->atoms);
I
Ingo Molnar 已提交
252

253
	task->atoms[idx] = event;
I
Ingo Molnar 已提交
254 255 256 257

	return event;
}

258
static struct sched_atom *last_event(struct task_desc *task)
I
Ingo Molnar 已提交
259 260 261 262
{
	if (!task->nr_events)
		return NULL;

263
	return task->atoms[task->nr_events - 1];
I
Ingo Molnar 已提交
264 265
}

266 267
static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
				u64 timestamp, u64 duration)
I
Ingo Molnar 已提交
268
{
269
	struct sched_atom *event, *curr_event = last_event(task);
I
Ingo Molnar 已提交
270 271

	/*
272 273 274
	 * optimize an existing RUN event by merging this one
	 * to it:
	 */
I
Ingo Molnar 已提交
275
	if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
276
		sched->nr_run_events_optimized++;
I
Ingo Molnar 已提交
277 278 279 280 281 282 283 284 285
		curr_event->duration += duration;
		return;
	}

	event = get_new_event(task, timestamp);

	event->type = SCHED_EVENT_RUN;
	event->duration = duration;

286
	sched->nr_run_events++;
I
Ingo Molnar 已提交
287 288
}

289 290
static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
				   u64 timestamp, struct task_desc *wakee)
I
Ingo Molnar 已提交
291
{
292
	struct sched_atom *event, *wakee_event;
I
Ingo Molnar 已提交
293 294 295 296 297 298 299

	event = get_new_event(task, timestamp);
	event->type = SCHED_EVENT_WAKEUP;
	event->wakee = wakee;

	wakee_event = last_event(wakee);
	if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
300
		sched->targetless_wakeups++;
I
Ingo Molnar 已提交
301 302 303
		return;
	}
	if (wakee_event->wait_sem) {
304
		sched->multitarget_wakeups++;
I
Ingo Molnar 已提交
305 306 307
		return;
	}

308
	wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
I
Ingo Molnar 已提交
309 310 311 312
	sem_init(wakee_event->wait_sem, 0, 0);
	wakee_event->specific_wait = 1;
	event->wait_sem = wakee_event->wait_sem;

313
	sched->nr_wakeup_events++;
I
Ingo Molnar 已提交
314 315
}

316 317
static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
				  u64 timestamp, u64 task_state __maybe_unused)
I
Ingo Molnar 已提交
318
{
319
	struct sched_atom *event = get_new_event(task, timestamp);
I
Ingo Molnar 已提交
320 321 322

	event->type = SCHED_EVENT_SLEEP;

323
	sched->nr_sleep_events++;
I
Ingo Molnar 已提交
324 325
}

326 327
static struct task_desc *register_pid(struct perf_sched *sched,
				      unsigned long pid, const char *comm)
I
Ingo Molnar 已提交
328 329
{
	struct task_desc *task;
330
	static int pid_max;
I
Ingo Molnar 已提交
331

332 333 334 335 336
	if (sched->pid_to_task == NULL) {
		if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
			pid_max = MAX_PID;
		BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
	}
337 338 339 340 341 342
	if (pid >= (unsigned long)pid_max) {
		BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
			sizeof(struct task_desc *))) == NULL);
		while (pid >= (unsigned long)pid_max)
			sched->pid_to_task[pid_max++] = NULL;
	}
I
Ingo Molnar 已提交
343

344
	task = sched->pid_to_task[pid];
I
Ingo Molnar 已提交
345 346 347 348

	if (task)
		return task;

349
	task = zalloc(sizeof(*task));
I
Ingo Molnar 已提交
350
	task->pid = pid;
351
	task->nr = sched->nr_tasks;
I
Ingo Molnar 已提交
352 353 354 355 356
	strcpy(task->comm, comm);
	/*
	 * every task starts in sleeping state - this gets ignored
	 * if there's no wakeup pointing to this sleep state:
	 */
357
	add_sched_event_sleep(sched, task, 0, 0);
I
Ingo Molnar 已提交
358

359 360
	sched->pid_to_task[pid] = task;
	sched->nr_tasks++;
361
	sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
362 363
	BUG_ON(!sched->tasks);
	sched->tasks[task->nr] = task;
I
Ingo Molnar 已提交
364

I
Ingo Molnar 已提交
365
	if (verbose)
366
		printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
I
Ingo Molnar 已提交
367 368 369 370 371

	return task;
}


372
static void print_task_traces(struct perf_sched *sched)
I
Ingo Molnar 已提交
373 374 375 376
{
	struct task_desc *task;
	unsigned long i;

377 378
	for (i = 0; i < sched->nr_tasks; i++) {
		task = sched->tasks[i];
I
Ingo Molnar 已提交
379
		printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
I
Ingo Molnar 已提交
380 381 382 383
			task->nr, task->comm, task->pid, task->nr_events);
	}
}

384
static void add_cross_task_wakeups(struct perf_sched *sched)
I
Ingo Molnar 已提交
385 386 387 388
{
	struct task_desc *task1, *task2;
	unsigned long i, j;

389 390
	for (i = 0; i < sched->nr_tasks; i++) {
		task1 = sched->tasks[i];
I
Ingo Molnar 已提交
391
		j = i + 1;
392
		if (j == sched->nr_tasks)
I
Ingo Molnar 已提交
393
			j = 0;
394 395
		task2 = sched->tasks[j];
		add_sched_event_wakeup(sched, task1, 0, task2);
I
Ingo Molnar 已提交
396 397 398
	}
}

399 400
static void perf_sched__process_event(struct perf_sched *sched,
				      struct sched_atom *atom)
I
Ingo Molnar 已提交
401 402 403
{
	int ret = 0;

404
	switch (atom->type) {
I
Ingo Molnar 已提交
405
		case SCHED_EVENT_RUN:
406
			burn_nsecs(sched, atom->duration);
I
Ingo Molnar 已提交
407 408
			break;
		case SCHED_EVENT_SLEEP:
409 410
			if (atom->wait_sem)
				ret = sem_wait(atom->wait_sem);
I
Ingo Molnar 已提交
411 412 413
			BUG_ON(ret);
			break;
		case SCHED_EVENT_WAKEUP:
414 415
			if (atom->wait_sem)
				ret = sem_post(atom->wait_sem);
I
Ingo Molnar 已提交
416 417
			BUG_ON(ret);
			break;
418 419
		case SCHED_EVENT_MIGRATION:
			break;
I
Ingo Molnar 已提交
420 421 422 423 424
		default:
			BUG_ON(1);
	}
}

425
static u64 get_cpu_usage_nsec_parent(void)
I
Ingo Molnar 已提交
426 427
{
	struct rusage ru;
428
	u64 sum;
I
Ingo Molnar 已提交
429 430 431 432 433 434 435 436 437 438 439
	int err;

	err = getrusage(RUSAGE_SELF, &ru);
	BUG_ON(err);

	sum =  ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
	sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;

	return sum;
}

440
static int self_open_counters(void)
I
Ingo Molnar 已提交
441
{
442
	struct perf_event_attr attr;
443
	char sbuf[STRERR_BUFSIZE];
444
	int fd;
I
Ingo Molnar 已提交
445

446
	memset(&attr, 0, sizeof(attr));
I
Ingo Molnar 已提交
447

448 449
	attr.type = PERF_TYPE_SOFTWARE;
	attr.config = PERF_COUNT_SW_TASK_CLOCK;
I
Ingo Molnar 已提交
450

451 452
	fd = sys_perf_event_open(&attr, 0, -1, -1,
				 perf_event_open_cloexec_flag());
453 454

	if (fd < 0)
455
		pr_err("Error: sys_perf_event_open() syscall returned "
456 457
		       "with %d (%s)\n", fd,
		       strerror_r(errno, sbuf, sizeof(sbuf)));
458 459 460 461 462 463 464 465 466 467 468 469
	return fd;
}

static u64 get_cpu_usage_nsec_self(int fd)
{
	u64 runtime;
	int ret;

	ret = read(fd, &runtime, sizeof(runtime));
	BUG_ON(ret != sizeof(runtime));

	return runtime;
I
Ingo Molnar 已提交
470 471
}

472 473 474 475 476
struct sched_thread_parms {
	struct task_desc  *task;
	struct perf_sched *sched;
};

I
Ingo Molnar 已提交
477 478
static void *thread_func(void *ctx)
{
479 480 481
	struct sched_thread_parms *parms = ctx;
	struct task_desc *this_task = parms->task;
	struct perf_sched *sched = parms->sched;
482
	u64 cpu_usage_0, cpu_usage_1;
I
Ingo Molnar 已提交
483 484
	unsigned long i, ret;
	char comm2[22];
485
	int fd;
I
Ingo Molnar 已提交
486

487
	zfree(&parms);
488

I
Ingo Molnar 已提交
489 490
	sprintf(comm2, ":%s", this_task->comm);
	prctl(PR_SET_NAME, comm2);
491
	fd = self_open_counters();
492 493
	if (fd < 0)
		return NULL;
I
Ingo Molnar 已提交
494 495 496
again:
	ret = sem_post(&this_task->ready_for_work);
	BUG_ON(ret);
497
	ret = pthread_mutex_lock(&sched->start_work_mutex);
I
Ingo Molnar 已提交
498
	BUG_ON(ret);
499
	ret = pthread_mutex_unlock(&sched->start_work_mutex);
I
Ingo Molnar 已提交
500 501
	BUG_ON(ret);

502
	cpu_usage_0 = get_cpu_usage_nsec_self(fd);
I
Ingo Molnar 已提交
503 504 505

	for (i = 0; i < this_task->nr_events; i++) {
		this_task->curr_event = i;
506
		perf_sched__process_event(sched, this_task->atoms[i]);
I
Ingo Molnar 已提交
507 508
	}

509
	cpu_usage_1 = get_cpu_usage_nsec_self(fd);
I
Ingo Molnar 已提交
510 511 512 513
	this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
	ret = sem_post(&this_task->work_done_sem);
	BUG_ON(ret);

514
	ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
I
Ingo Molnar 已提交
515
	BUG_ON(ret);
516
	ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
I
Ingo Molnar 已提交
517 518 519 520 521
	BUG_ON(ret);

	goto again;
}

522
static void create_tasks(struct perf_sched *sched)
I
Ingo Molnar 已提交
523 524 525 526 527 528 529 530
{
	struct task_desc *task;
	pthread_attr_t attr;
	unsigned long i;
	int err;

	err = pthread_attr_init(&attr);
	BUG_ON(err);
531 532
	err = pthread_attr_setstacksize(&attr,
			(size_t) max(16 * 1024, PTHREAD_STACK_MIN));
I
Ingo Molnar 已提交
533
	BUG_ON(err);
534
	err = pthread_mutex_lock(&sched->start_work_mutex);
I
Ingo Molnar 已提交
535
	BUG_ON(err);
536
	err = pthread_mutex_lock(&sched->work_done_wait_mutex);
I
Ingo Molnar 已提交
537
	BUG_ON(err);
538 539 540 541 542
	for (i = 0; i < sched->nr_tasks; i++) {
		struct sched_thread_parms *parms = malloc(sizeof(*parms));
		BUG_ON(parms == NULL);
		parms->task = task = sched->tasks[i];
		parms->sched = sched;
I
Ingo Molnar 已提交
543 544 545 546
		sem_init(&task->sleep_sem, 0, 0);
		sem_init(&task->ready_for_work, 0, 0);
		sem_init(&task->work_done_sem, 0, 0);
		task->curr_event = 0;
547
		err = pthread_create(&task->thread, &attr, thread_func, parms);
I
Ingo Molnar 已提交
548 549 550 551
		BUG_ON(err);
	}
}

552
static void wait_for_tasks(struct perf_sched *sched)
I
Ingo Molnar 已提交
553
{
554
	u64 cpu_usage_0, cpu_usage_1;
I
Ingo Molnar 已提交
555 556 557
	struct task_desc *task;
	unsigned long i, ret;

558 559 560
	sched->start_time = get_nsecs();
	sched->cpu_usage = 0;
	pthread_mutex_unlock(&sched->work_done_wait_mutex);
I
Ingo Molnar 已提交
561

562 563
	for (i = 0; i < sched->nr_tasks; i++) {
		task = sched->tasks[i];
I
Ingo Molnar 已提交
564 565 566 567
		ret = sem_wait(&task->ready_for_work);
		BUG_ON(ret);
		sem_init(&task->ready_for_work, 0, 0);
	}
568
	ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
I
Ingo Molnar 已提交
569 570 571 572
	BUG_ON(ret);

	cpu_usage_0 = get_cpu_usage_nsec_parent();

573
	pthread_mutex_unlock(&sched->start_work_mutex);
I
Ingo Molnar 已提交
574

575 576
	for (i = 0; i < sched->nr_tasks; i++) {
		task = sched->tasks[i];
I
Ingo Molnar 已提交
577 578 579
		ret = sem_wait(&task->work_done_sem);
		BUG_ON(ret);
		sem_init(&task->work_done_sem, 0, 0);
580
		sched->cpu_usage += task->cpu_usage;
I
Ingo Molnar 已提交
581 582 583 584
		task->cpu_usage = 0;
	}

	cpu_usage_1 = get_cpu_usage_nsec_parent();
585 586 587
	if (!sched->runavg_cpu_usage)
		sched->runavg_cpu_usage = sched->cpu_usage;
	sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10;
I
Ingo Molnar 已提交
588

589 590 591 592 593
	sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
	if (!sched->runavg_parent_cpu_usage)
		sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
	sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 +
					 sched->parent_cpu_usage)/10;
I
Ingo Molnar 已提交
594

595
	ret = pthread_mutex_lock(&sched->start_work_mutex);
I
Ingo Molnar 已提交
596 597
	BUG_ON(ret);

598 599
	for (i = 0; i < sched->nr_tasks; i++) {
		task = sched->tasks[i];
I
Ingo Molnar 已提交
600 601 602 603 604
		sem_init(&task->sleep_sem, 0, 0);
		task->curr_event = 0;
	}
}

605
static void run_one_test(struct perf_sched *sched)
I
Ingo Molnar 已提交
606
{
K
Kyle McMartin 已提交
607
	u64 T0, T1, delta, avg_delta, fluct;
I
Ingo Molnar 已提交
608 609

	T0 = get_nsecs();
610
	wait_for_tasks(sched);
I
Ingo Molnar 已提交
611 612 613
	T1 = get_nsecs();

	delta = T1 - T0;
614 615
	sched->sum_runtime += delta;
	sched->nr_runs++;
I
Ingo Molnar 已提交
616

617
	avg_delta = sched->sum_runtime / sched->nr_runs;
I
Ingo Molnar 已提交
618 619 620 621
	if (delta < avg_delta)
		fluct = avg_delta - delta;
	else
		fluct = delta - avg_delta;
622 623 624 625
	sched->sum_fluct += fluct;
	if (!sched->run_avg)
		sched->run_avg = delta;
	sched->run_avg = (sched->run_avg * 9 + delta) / 10;
I
Ingo Molnar 已提交
626

627
	printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
I
Ingo Molnar 已提交
628

629
	printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
I
Ingo Molnar 已提交
630

I
Ingo Molnar 已提交
631
	printf("cpu: %0.2f / %0.2f",
632
		(double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
I
Ingo Molnar 已提交
633 634 635

#if 0
	/*
636
	 * rusage statistics done by the parent, these are less
637
	 * accurate than the sched->sum_exec_runtime based statistics:
638
	 */
I
Ingo Molnar 已提交
639
	printf(" [%0.2f / %0.2f]",
640 641
		(double)sched->parent_cpu_usage/1e6,
		(double)sched->runavg_parent_cpu_usage/1e6);
I
Ingo Molnar 已提交
642 643
#endif

I
Ingo Molnar 已提交
644
	printf("\n");
I
Ingo Molnar 已提交
645

646 647 648
	if (sched->nr_sleep_corrections)
		printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
	sched->nr_sleep_corrections = 0;
I
Ingo Molnar 已提交
649 650
}

651
static void test_calibrations(struct perf_sched *sched)
I
Ingo Molnar 已提交
652
{
653
	u64 T0, T1;
I
Ingo Molnar 已提交
654 655

	T0 = get_nsecs();
656
	burn_nsecs(sched, 1e6);
I
Ingo Molnar 已提交
657 658
	T1 = get_nsecs();

659
	printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
I
Ingo Molnar 已提交
660 661 662 663 664

	T0 = get_nsecs();
	sleep_nsecs(1e6);
	T1 = get_nsecs();

665
	printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
I
Ingo Molnar 已提交
666 667
}

668
static int
669
replay_wakeup_event(struct perf_sched *sched,
670 671
		    struct perf_evsel *evsel, struct perf_sample *sample,
		    struct machine *machine __maybe_unused)
672
{
673 674
	const char *comm = perf_evsel__strval(evsel, sample, "comm");
	const u32 pid	 = perf_evsel__intval(evsel, sample, "pid");
675
	struct task_desc *waker, *wakee;
676

I
Ingo Molnar 已提交
677
	if (verbose) {
678
		printf("sched_wakeup event %p\n", evsel);
679

680
		printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
I
Ingo Molnar 已提交
681
	}
682

683
	waker = register_pid(sched, sample->tid, "<unknown>");
684
	wakee = register_pid(sched, pid, comm);
685

686
	add_sched_event_wakeup(sched, waker, sample->time, wakee);
687
	return 0;
I
Ingo Molnar 已提交
688 689
}

690 691 692 693
static int replay_switch_event(struct perf_sched *sched,
			       struct perf_evsel *evsel,
			       struct perf_sample *sample,
			       struct machine *machine __maybe_unused)
I
Ingo Molnar 已提交
694
{
695 696 697 698 699
	const char *prev_comm  = perf_evsel__strval(evsel, sample, "prev_comm"),
		   *next_comm  = perf_evsel__strval(evsel, sample, "next_comm");
	const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
		  next_pid = perf_evsel__intval(evsel, sample, "next_pid");
	const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
700
	struct task_desc *prev, __maybe_unused *next;
701 702
	u64 timestamp0, timestamp = sample->time;
	int cpu = sample->cpu;
703 704
	s64 delta;

I
Ingo Molnar 已提交
705
	if (verbose)
706
		printf("sched_switch event %p\n", evsel);
I
Ingo Molnar 已提交
707

708
	if (cpu >= MAX_CPUS || cpu < 0)
709
		return 0;
710

711
	timestamp0 = sched->cpu_last_switched[cpu];
712 713 714 715 716
	if (timestamp0)
		delta = timestamp - timestamp0;
	else
		delta = 0;

717
	if (delta < 0) {
718
		pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
719 720
		return -1;
	}
721

722 723
	pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
		 prev_comm, prev_pid, next_comm, next_pid, delta);
724

725 726
	prev = register_pid(sched, prev_pid, prev_comm);
	next = register_pid(sched, next_pid, next_comm);
727

728
	sched->cpu_last_switched[cpu] = timestamp;
729

730
	add_sched_event_run(sched, prev, timestamp, delta);
731
	add_sched_event_sleep(sched, prev, timestamp, prev_state);
732 733

	return 0;
734 735
}

736 737 738
static int replay_fork_event(struct perf_sched *sched,
			     union perf_event *event,
			     struct machine *machine)
739
{
740 741
	struct thread *child, *parent;

742 743 744 745
	child = machine__findnew_thread(machine, event->fork.pid,
					event->fork.tid);
	parent = machine__findnew_thread(machine, event->fork.ppid,
					 event->fork.ptid);
746 747 748 749 750 751

	if (child == NULL || parent == NULL) {
		pr_debug("thread does not exist on fork event: child %p, parent %p\n",
				 child, parent);
		return 0;
	}
752

753
	if (verbose) {
754
		printf("fork event\n");
755 756
		printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
		printf("...  child: %s/%d\n", thread__comm_str(child), child->tid);
757
	}
758

759 760
	register_pid(sched, parent->tid, thread__comm_str(parent));
	register_pid(sched, child->tid, thread__comm_str(child));
761
	return 0;
762
}
763

764 765
struct sort_dimension {
	const char		*name;
766
	sort_fn_t		cmp;
767 768 769
	struct list_head	list;
};

770
static int
771
thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
772 773 774 775
{
	struct sort_dimension *sort;
	int ret = 0;

776 777
	BUG_ON(list_empty(list));

778 779 780 781 782 783 784 785 786
	list_for_each_entry(sort, list, list) {
		ret = sort->cmp(l, r);
		if (ret)
			return ret;
	}

	return ret;
}

787
static struct work_atoms *
788 789 790 791
thread_atoms_search(struct rb_root *root, struct thread *thread,
			 struct list_head *sort_list)
{
	struct rb_node *node = root->rb_node;
792
	struct work_atoms key = { .thread = thread };
793 794

	while (node) {
795
		struct work_atoms *atoms;
796 797
		int cmp;

798
		atoms = container_of(node, struct work_atoms, node);
799 800 801 802 803 804 805 806 807 808 809 810 811 812

		cmp = thread_lat_cmp(sort_list, &key, atoms);
		if (cmp > 0)
			node = node->rb_left;
		else if (cmp < 0)
			node = node->rb_right;
		else {
			BUG_ON(thread != atoms->thread);
			return atoms;
		}
	}
	return NULL;
}

813
static void
814
__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
815
			 struct list_head *sort_list)
816 817 818 819
{
	struct rb_node **new = &(root->rb_node), *parent = NULL;

	while (*new) {
820
		struct work_atoms *this;
821
		int cmp;
822

823
		this = container_of(*new, struct work_atoms, node);
824
		parent = *new;
825 826 827 828

		cmp = thread_lat_cmp(sort_list, data, this);

		if (cmp > 0)
829 830
			new = &((*new)->rb_left);
		else
831
			new = &((*new)->rb_right);
832 833 834 835 836 837
	}

	rb_link_node(&data->node, parent, new);
	rb_insert_color(&data->node, root);
}

838
static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
839
{
840
	struct work_atoms *atoms = zalloc(sizeof(*atoms));
841 842 843 844
	if (!atoms) {
		pr_err("No memory at %s\n", __func__);
		return -1;
	}
845

846
	atoms->thread = thread__get(thread);
847
	INIT_LIST_HEAD(&atoms->work_list);
848
	__thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
849
	return 0;
850 851
}

852
static char sched_out_state(u64 prev_state)
853 854 855
{
	const char *str = TASK_STATE_TO_CHAR_STR;

856
	return str[prev_state];
857 858
}

859
static int
860 861 862
add_sched_out_event(struct work_atoms *atoms,
		    char run_state,
		    u64 timestamp)
863
{
864
	struct work_atom *atom = zalloc(sizeof(*atom));
865 866 867 868
	if (!atom) {
		pr_err("Non memory at %s", __func__);
		return -1;
	}
869

870 871
	atom->sched_out_time = timestamp;

872
	if (run_state == 'R') {
873
		atom->state = THREAD_WAIT_CPU;
874
		atom->wake_up_time = atom->sched_out_time;
875 876
	}

877
	list_add_tail(&atom->list, &atoms->work_list);
878
	return 0;
879 880 881
}

static void
882 883
add_runtime_event(struct work_atoms *atoms, u64 delta,
		  u64 timestamp __maybe_unused)
884 885 886 887 888 889 890 891 892 893 894 895 896
{
	struct work_atom *atom;

	BUG_ON(list_empty(&atoms->work_list));

	atom = list_entry(atoms->work_list.prev, struct work_atom, list);

	atom->runtime += delta;
	atoms->total_runtime += delta;
}

static void
add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
897
{
898
	struct work_atom *atom;
899
	u64 delta;
900

901
	if (list_empty(&atoms->work_list))
902 903
		return;

904
	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
905

906
	if (atom->state != THREAD_WAIT_CPU)
907 908
		return;

909 910
	if (timestamp < atom->wake_up_time) {
		atom->state = THREAD_IGNORE;
911 912 913
		return;
	}

914 915
	atom->state = THREAD_SCHED_IN;
	atom->sched_in_time = timestamp;
916

917
	delta = atom->sched_in_time - atom->wake_up_time;
918
	atoms->total_lat += delta;
919
	if (delta > atoms->max_lat) {
920
		atoms->max_lat = delta;
921 922
		atoms->max_lat_at = timestamp;
	}
923
	atoms->nb_atoms++;
924 925
}

926 927 928 929
static int latency_switch_event(struct perf_sched *sched,
				struct perf_evsel *evsel,
				struct perf_sample *sample,
				struct machine *machine)
930
{
931 932 933
	const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
		  next_pid = perf_evsel__intval(evsel, sample, "next_pid");
	const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
934
	struct work_atoms *out_events, *in_events;
935
	struct thread *sched_out, *sched_in;
936 937
	u64 timestamp0, timestamp = sample->time;
	int cpu = sample->cpu;
I
Ingo Molnar 已提交
938 939
	s64 delta;

940
	BUG_ON(cpu >= MAX_CPUS || cpu < 0);
I
Ingo Molnar 已提交
941

942 943
	timestamp0 = sched->cpu_last_switched[cpu];
	sched->cpu_last_switched[cpu] = timestamp;
I
Ingo Molnar 已提交
944 945 946 947 948
	if (timestamp0)
		delta = timestamp - timestamp0;
	else
		delta = 0;

949 950 951 952
	if (delta < 0) {
		pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
		return -1;
	}
953

954 955
	sched_out = machine__findnew_thread(machine, -1, prev_pid);
	sched_in = machine__findnew_thread(machine, -1, next_pid);
956

957
	out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
958
	if (!out_events) {
959
		if (thread_atoms_insert(sched, sched_out))
960
			return -1;
961
		out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
962 963 964 965
		if (!out_events) {
			pr_err("out-event: Internal tree error");
			return -1;
		}
966
	}
967
	if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
968
		return -1;
969

970
	in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
971
	if (!in_events) {
972
		if (thread_atoms_insert(sched, sched_in))
973
			return -1;
974
		in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
975 976 977 978
		if (!in_events) {
			pr_err("in-event: Internal tree error");
			return -1;
		}
979 980 981 982
		/*
		 * Take came in we have not heard about yet,
		 * add in an initial atom in runnable state:
		 */
983 984
		if (add_sched_out_event(in_events, 'R', timestamp))
			return -1;
985
	}
986
	add_sched_in_event(in_events, timestamp);
987 988

	return 0;
989
}
990

991 992 993 994
static int latency_runtime_event(struct perf_sched *sched,
				 struct perf_evsel *evsel,
				 struct perf_sample *sample,
				 struct machine *machine)
995
{
996 997
	const u32 pid	   = perf_evsel__intval(evsel, sample, "pid");
	const u64 runtime  = perf_evsel__intval(evsel, sample, "runtime");
998
	struct thread *thread = machine__findnew_thread(machine, -1, pid);
999
	struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
1000 1001
	u64 timestamp = sample->time;
	int cpu = sample->cpu;
1002 1003 1004

	BUG_ON(cpu >= MAX_CPUS || cpu < 0);
	if (!atoms) {
1005
		if (thread_atoms_insert(sched, thread))
1006
			return -1;
1007
		atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
1008
		if (!atoms) {
1009
			pr_err("in-event: Internal tree error");
1010 1011 1012 1013
			return -1;
		}
		if (add_sched_out_event(atoms, 'R', timestamp))
			return -1;
1014 1015
	}

1016
	add_runtime_event(atoms, runtime, timestamp);
1017
	return 0;
1018 1019
}

1020 1021 1022 1023
static int latency_wakeup_event(struct perf_sched *sched,
				struct perf_evsel *evsel,
				struct perf_sample *sample,
				struct machine *machine)
1024
{
1025
	const u32 pid	  = perf_evsel__intval(evsel, sample, "pid");
1026
	struct work_atoms *atoms;
1027
	struct work_atom *atom;
1028
	struct thread *wakee;
1029
	u64 timestamp = sample->time;
1030

1031
	wakee = machine__findnew_thread(machine, -1, pid);
1032
	atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
1033
	if (!atoms) {
1034
		if (thread_atoms_insert(sched, wakee))
1035
			return -1;
1036
		atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
1037
		if (!atoms) {
1038
			pr_err("wakeup-event: Internal tree error");
1039 1040 1041 1042
			return -1;
		}
		if (add_sched_out_event(atoms, 'S', timestamp))
			return -1;
1043 1044
	}

1045
	BUG_ON(list_empty(&atoms->work_list));
1046

1047
	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1048

1049
	/*
1050 1051 1052 1053 1054 1055
	 * As we do not guarantee the wakeup event happens when
	 * task is out of run queue, also may happen when task is
	 * on run queue and wakeup only change ->state to TASK_RUNNING,
	 * then we should not set the ->wake_up_time when wake up a
	 * task which is on run queue.
	 *
1056 1057
	 * You WILL be missing events if you've recorded only
	 * one CPU, or are only looking at only one, so don't
1058
	 * skip in this case.
1059
	 */
1060
	if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1061
		return 0;
1062

1063
	sched->nr_timestamps++;
1064
	if (atom->sched_out_time > timestamp) {
1065
		sched->nr_unordered_timestamps++;
1066
		return 0;
1067
	}
1068

1069 1070
	atom->state = THREAD_WAIT_CPU;
	atom->wake_up_time = timestamp;
1071
	return 0;
1072 1073
}

1074 1075 1076 1077
static int latency_migrate_task_event(struct perf_sched *sched,
				      struct perf_evsel *evsel,
				      struct perf_sample *sample,
				      struct machine *machine)
1078
{
1079
	const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1080
	u64 timestamp = sample->time;
1081 1082 1083 1084 1085 1086 1087
	struct work_atoms *atoms;
	struct work_atom *atom;
	struct thread *migrant;

	/*
	 * Only need to worry about migration when profiling one CPU.
	 */
1088
	if (sched->profile_cpu == -1)
1089
		return 0;
1090

1091
	migrant = machine__findnew_thread(machine, -1, pid);
1092
	atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
1093
	if (!atoms) {
1094
		if (thread_atoms_insert(sched, migrant))
1095
			return -1;
1096
		register_pid(sched, migrant->tid, thread__comm_str(migrant));
1097
		atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
1098
		if (!atoms) {
1099
			pr_err("migration-event: Internal tree error");
1100 1101 1102 1103
			return -1;
		}
		if (add_sched_out_event(atoms, 'R', timestamp))
			return -1;
1104 1105 1106 1107 1108 1109 1110
	}

	BUG_ON(list_empty(&atoms->work_list));

	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
	atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;

1111
	sched->nr_timestamps++;
1112 1113

	if (atom->sched_out_time > timestamp)
1114
		sched->nr_unordered_timestamps++;
1115 1116

	return 0;
1117 1118
}

1119
static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
1120 1121 1122
{
	int i;
	int ret;
1123
	u64 avg;
1124

1125
	if (!work_list->nb_atoms)
1126
		return;
1127 1128 1129
	/*
	 * Ignore idle threads:
	 */
1130
	if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
1131
		return;
1132

1133 1134
	sched->all_runtime += work_list->total_runtime;
	sched->all_count   += work_list->nb_atoms;
1135

1136
	ret = printf("  %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
1137

M
mingo 已提交
1138
	for (i = 0; i < 24 - ret; i++)
1139 1140
		printf(" ");

1141
	avg = work_list->total_lat / work_list->nb_atoms;
1142

1143
	printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
1144 1145
	      (double)work_list->total_runtime / 1e6,
		 work_list->nb_atoms, (double)avg / 1e6,
1146 1147
		 (double)work_list->max_lat / 1e6,
		 (double)work_list->max_lat_at / 1e9);
1148 1149
}

1150
static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1151
{
1152
	if (l->thread->tid < r->thread->tid)
1153
		return -1;
1154
	if (l->thread->tid > r->thread->tid)
1155 1156 1157 1158 1159
		return 1;

	return 0;
}

1160
static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
{
	u64 avgl, avgr;

	if (!l->nb_atoms)
		return -1;

	if (!r->nb_atoms)
		return 1;

	avgl = l->total_lat / l->nb_atoms;
	avgr = r->total_lat / r->nb_atoms;

	if (avgl < avgr)
		return -1;
	if (avgl > avgr)
		return 1;

	return 0;
}

1181
static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1182 1183 1184 1185 1186 1187 1188 1189 1190
{
	if (l->max_lat < r->max_lat)
		return -1;
	if (l->max_lat > r->max_lat)
		return 1;

	return 0;
}

1191
static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1192 1193 1194 1195 1196 1197 1198 1199 1200
{
	if (l->nb_atoms < r->nb_atoms)
		return -1;
	if (l->nb_atoms > r->nb_atoms)
		return 1;

	return 0;
}

1201
static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1202 1203 1204 1205 1206 1207 1208 1209 1210
{
	if (l->total_runtime < r->total_runtime)
		return -1;
	if (l->total_runtime > r->total_runtime)
		return 1;

	return 0;
}

1211
static int sort_dimension__add(const char *tok, struct list_head *list)
1212
{
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	size_t i;
	static struct sort_dimension avg_sort_dimension = {
		.name = "avg",
		.cmp  = avg_cmp,
	};
	static struct sort_dimension max_sort_dimension = {
		.name = "max",
		.cmp  = max_cmp,
	};
	static struct sort_dimension pid_sort_dimension = {
		.name = "pid",
		.cmp  = pid_cmp,
	};
	static struct sort_dimension runtime_sort_dimension = {
		.name = "runtime",
		.cmp  = runtime_cmp,
	};
	static struct sort_dimension switch_sort_dimension = {
		.name = "switch",
		.cmp  = switch_cmp,
	};
	struct sort_dimension *available_sorts[] = {
		&pid_sort_dimension,
		&avg_sort_dimension,
		&max_sort_dimension,
		&switch_sort_dimension,
		&runtime_sort_dimension,
	};
1241

1242
	for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		if (!strcmp(available_sorts[i]->name, tok)) {
			list_add_tail(&available_sorts[i]->list, list);

			return 0;
		}
	}

	return -1;
}

1253
static void perf_sched__sort_lat(struct perf_sched *sched)
1254 1255 1256 1257
{
	struct rb_node *node;

	for (;;) {
1258
		struct work_atoms *data;
1259
		node = rb_first(&sched->atom_root);
1260 1261 1262
		if (!node)
			break;

1263
		rb_erase(node, &sched->atom_root);
1264
		data = rb_entry(node, struct work_atoms, node);
1265
		__thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
1266 1267 1268
	}
}

1269
static int process_sched_wakeup_event(struct perf_tool *tool,
1270
				      struct perf_evsel *evsel,
1271
				      struct perf_sample *sample,
1272
				      struct machine *machine)
1273
{
1274
	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1275

1276 1277
	if (sched->tp_handler->wakeup_event)
		return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
1278

1279
	return 0;
1280 1281
}

1282 1283
static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
			    struct perf_sample *sample, struct machine *machine)
1284
{
1285 1286
	const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
	struct thread *sched_in;
1287
	int new_shortname;
1288
	u64 timestamp0, timestamp = sample->time;
1289
	s64 delta;
1290
	int cpu, this_cpu = sample->cpu;
1291 1292 1293

	BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);

1294 1295
	if (this_cpu > sched->max_cpu)
		sched->max_cpu = this_cpu;
1296

1297 1298
	timestamp0 = sched->cpu_last_switched[this_cpu];
	sched->cpu_last_switched[this_cpu] = timestamp;
1299 1300 1301 1302 1303
	if (timestamp0)
		delta = timestamp - timestamp0;
	else
		delta = 0;

1304
	if (delta < 0) {
1305
		pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1306 1307
		return -1;
	}
1308

1309
	sched_in = machine__findnew_thread(machine, -1, next_pid);
1310

1311
	sched->curr_thread[this_cpu] = sched_in;
1312 1313 1314 1315 1316

	printf("  ");

	new_shortname = 0;
	if (!sched_in->shortname[0]) {
1317 1318 1319 1320 1321 1322 1323
		if (!strcmp(thread__comm_str(sched_in), "swapper")) {
			/*
			 * Don't allocate a letter-number for swapper:0
			 * as a shortname. Instead, we use '.' for it.
			 */
			sched_in->shortname[0] = '.';
			sched_in->shortname[1] = ' ';
1324
		} else {
1325 1326 1327 1328 1329
			sched_in->shortname[0] = sched->next_shortname1;
			sched_in->shortname[1] = sched->next_shortname2;

			if (sched->next_shortname1 < 'Z') {
				sched->next_shortname1++;
1330
			} else {
1331 1332 1333 1334 1335
				sched->next_shortname1 = 'A';
				if (sched->next_shortname2 < '9')
					sched->next_shortname2++;
				else
					sched->next_shortname2 = '0';
1336 1337 1338 1339 1340
			}
		}
		new_shortname = 1;
	}

1341
	for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
1342 1343 1344 1345 1346
		if (cpu != this_cpu)
			printf(" ");
		else
			printf("*");

1347 1348 1349
		if (sched->curr_thread[cpu])
			printf("%2s ", sched->curr_thread[cpu]->shortname);
		else
1350 1351 1352 1353 1354 1355
			printf("   ");
	}

	printf("  %12.6f secs ", (double)timestamp/1e9);
	if (new_shortname) {
		printf("%s => %s:%d\n",
1356
		       sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
1357 1358 1359
	} else {
		printf("\n");
	}
1360 1361

	return 0;
1362 1363
}

1364
static int process_sched_switch_event(struct perf_tool *tool,
1365
				      struct perf_evsel *evsel,
1366
				      struct perf_sample *sample,
1367
				      struct machine *machine)
1368
{
1369
	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1370
	int this_cpu = sample->cpu, err = 0;
1371 1372
	u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
	    next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1373

1374
	if (sched->curr_pid[this_cpu] != (u32)-1) {
1375 1376 1377 1378
		/*
		 * Are we trying to switch away a PID that is
		 * not current?
		 */
1379
		if (sched->curr_pid[this_cpu] != prev_pid)
1380
			sched->nr_context_switch_bugs++;
1381 1382
	}

1383 1384
	if (sched->tp_handler->switch_event)
		err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
1385 1386

	sched->curr_pid[this_cpu] = next_pid;
1387
	return err;
1388 1389
}

1390
static int process_sched_runtime_event(struct perf_tool *tool,
1391
				       struct perf_evsel *evsel,
1392
				       struct perf_sample *sample,
1393
				       struct machine *machine)
1394
{
1395
	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1396

1397 1398
	if (sched->tp_handler->runtime_event)
		return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
1399

1400
	return 0;
1401 1402
}

1403 1404 1405 1406
static int perf_sched__process_fork_event(struct perf_tool *tool,
					  union perf_event *event,
					  struct perf_sample *sample,
					  struct machine *machine)
1407
{
1408
	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1409

1410 1411 1412 1413
	/* run the fork event through the perf machineruy */
	perf_event__process_fork(tool, event, sample, machine);

	/* and then run additional processing needed for this command */
1414
	if (sched->tp_handler->fork_event)
1415
		return sched->tp_handler->fork_event(sched, event, machine);
1416

1417
	return 0;
1418 1419
}

1420
static int process_sched_migrate_task_event(struct perf_tool *tool,
1421
					    struct perf_evsel *evsel,
1422
					    struct perf_sample *sample,
1423
					    struct machine *machine)
1424
{
1425
	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1426

1427 1428
	if (sched->tp_handler->migrate_task_event)
		return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
1429

1430
	return 0;
1431 1432
}

1433
typedef int (*tracepoint_handler)(struct perf_tool *tool,
1434
				  struct perf_evsel *evsel,
1435
				  struct perf_sample *sample,
1436
				  struct machine *machine);
I
Ingo Molnar 已提交
1437

1438 1439
static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
						 union perf_event *event __maybe_unused,
1440 1441 1442
						 struct perf_sample *sample,
						 struct perf_evsel *evsel,
						 struct machine *machine)
I
Ingo Molnar 已提交
1443
{
1444
	int err = 0;
I
Ingo Molnar 已提交
1445

1446 1447
	if (evsel->handler != NULL) {
		tracepoint_handler f = evsel->handler;
1448
		err = f(tool, evsel, sample, machine);
1449
	}
I
Ingo Molnar 已提交
1450

1451
	return err;
I
Ingo Molnar 已提交
1452 1453
}

1454
static int perf_sched__read_events(struct perf_sched *sched)
I
Ingo Molnar 已提交
1455
{
1456 1457 1458 1459 1460 1461 1462
	const struct perf_evsel_str_handler handlers[] = {
		{ "sched:sched_switch",	      process_sched_switch_event, },
		{ "sched:sched_stat_runtime", process_sched_runtime_event, },
		{ "sched:sched_wakeup",	      process_sched_wakeup_event, },
		{ "sched:sched_wakeup_new",   process_sched_wakeup_event, },
		{ "sched:sched_migrate_task", process_sched_migrate_task_event, },
	};
1463
	struct perf_session *session;
1464 1465 1466 1467
	struct perf_data_file file = {
		.path = input_name,
		.mode = PERF_DATA_MODE_READ,
	};
1468
	int rc = -1;
1469

1470
	session = perf_session__new(&file, false, &sched->tool);
1471 1472 1473 1474
	if (session == NULL) {
		pr_debug("No Memory for session\n");
		return -1;
	}
1475

1476
	symbol__init(&session->header.env);
1477

1478 1479
	if (perf_session__set_tracepoints_handlers(session, handlers))
		goto out_delete;
1480

1481
	if (perf_session__has_traces(session, "record -R")) {
1482
		int err = perf_session__process_events(session);
1483 1484 1485 1486
		if (err) {
			pr_err("Failed to process events, error %d", err);
			goto out_delete;
		}
1487

1488 1489 1490
		sched->nr_events      = session->evlist->stats.nr_events[0];
		sched->nr_lost_events = session->evlist->stats.total_lost;
		sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
1491
	}
1492

1493
	rc = 0;
1494 1495
out_delete:
	perf_session__delete(session);
1496
	return rc;
I
Ingo Molnar 已提交
1497 1498
}

1499
static void print_bad_events(struct perf_sched *sched)
1500
{
1501
	if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
1502
		printf("  INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1503 1504
			(double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
			sched->nr_unordered_timestamps, sched->nr_timestamps);
1505
	}
1506
	if (sched->nr_lost_events && sched->nr_events) {
1507
		printf("  INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1508 1509
			(double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
			sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
1510
	}
1511
	if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
1512
		printf("  INFO: %.3f%% context switch bugs (%ld out of %ld)",
1513 1514 1515
			(double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
			sched->nr_context_switch_bugs, sched->nr_timestamps);
		if (sched->nr_lost_events)
1516 1517 1518 1519 1520
			printf(" (due to lost events?)");
		printf("\n");
	}
}

1521
static int perf_sched__lat(struct perf_sched *sched)
1522 1523 1524 1525
{
	struct rb_node *next;

	setup_pager();
1526

1527
	if (perf_sched__read_events(sched))
1528
		return -1;
1529

1530
	perf_sched__sort_lat(sched);
1531

1532 1533 1534
	printf("\n -----------------------------------------------------------------------------------------------------------------\n");
	printf("  Task                  |   Runtime ms  | Switches | Average delay ms | Maximum delay ms | Maximum delay at       |\n");
	printf(" -----------------------------------------------------------------------------------------------------------------\n");
1535

1536
	next = rb_first(&sched->sorted_atom_root);
1537 1538 1539 1540 1541

	while (next) {
		struct work_atoms *work_list;

		work_list = rb_entry(next, struct work_atoms, node);
1542
		output_lat_thread(sched, work_list);
1543
		next = rb_next(next);
1544
		thread__zput(work_list->thread);
1545 1546
	}

1547
	printf(" -----------------------------------------------------------------------------------------------------------------\n");
1548
	printf("  TOTAL:                |%11.3f ms |%9" PRIu64 " |\n",
1549
		(double)sched->all_runtime / 1e6, sched->all_count);
1550 1551 1552

	printf(" ---------------------------------------------------\n");

1553
	print_bad_events(sched);
1554 1555
	printf("\n");

1556
	return 0;
1557 1558
}

1559
static int perf_sched__map(struct perf_sched *sched)
1560
{
1561
	sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1562

1563
	setup_pager();
1564
	if (perf_sched__read_events(sched))
1565
		return -1;
1566
	print_bad_events(sched);
1567
	return 0;
1568 1569
}

1570
static int perf_sched__replay(struct perf_sched *sched)
1571 1572 1573
{
	unsigned long i;

1574 1575
	calibrate_run_measurement_overhead(sched);
	calibrate_sleep_measurement_overhead(sched);
1576

1577
	test_calibrations(sched);
1578

1579
	if (perf_sched__read_events(sched))
1580
		return -1;
1581

1582 1583 1584
	printf("nr_run_events:        %ld\n", sched->nr_run_events);
	printf("nr_sleep_events:      %ld\n", sched->nr_sleep_events);
	printf("nr_wakeup_events:     %ld\n", sched->nr_wakeup_events);
1585

1586 1587 1588 1589 1590
	if (sched->targetless_wakeups)
		printf("target-less wakeups:  %ld\n", sched->targetless_wakeups);
	if (sched->multitarget_wakeups)
		printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
	if (sched->nr_run_events_optimized)
1591
		printf("run atoms optimized: %ld\n",
1592
			sched->nr_run_events_optimized);
1593

1594 1595
	print_task_traces(sched);
	add_cross_task_wakeups(sched);
1596

1597
	create_tasks(sched);
1598
	printf("------------------------------------------------------------\n");
1599 1600
	for (i = 0; i < sched->replay_repeat; i++)
		run_one_test(sched);
1601 1602

	return 0;
1603 1604
}

1605 1606
static void setup_sorting(struct perf_sched *sched, const struct option *options,
			  const char * const usage_msg[])
1607
{
1608
	char *tmp, *tok, *str = strdup(sched->sort_order);
1609 1610 1611

	for (tok = strtok_r(str, ", ", &tmp);
			tok; tok = strtok_r(NULL, ", ", &tmp)) {
1612
		if (sort_dimension__add(tok, &sched->sort_list) < 0) {
1613
			error("Unknown --sort key: `%s'", tok);
1614
			usage_with_options(usage_msg, options);
1615 1616 1617 1618 1619
		}
	}

	free(str);

1620
	sort_dimension__add("pid", &sched->cmp_pid);
1621 1622
}

1623 1624 1625 1626
static int __cmd_record(int argc, const char **argv)
{
	unsigned int rec_argc, i, j;
	const char **rec_argv;
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
	const char * const record_args[] = {
		"record",
		"-a",
		"-R",
		"-m", "1024",
		"-c", "1",
		"-e", "sched:sched_switch",
		"-e", "sched:sched_stat_wait",
		"-e", "sched:sched_stat_sleep",
		"-e", "sched:sched_stat_iowait",
		"-e", "sched:sched_stat_runtime",
		"-e", "sched:sched_process_fork",
		"-e", "sched:sched_wakeup",
1640
		"-e", "sched:sched_wakeup_new",
1641 1642
		"-e", "sched:sched_migrate_task",
	};
1643 1644 1645 1646

	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
	rec_argv = calloc(rec_argc + 1, sizeof(char *));

1647
	if (rec_argv == NULL)
1648 1649
		return -ENOMEM;

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
	for (i = 0; i < ARRAY_SIZE(record_args); i++)
		rec_argv[i] = strdup(record_args[i]);

	for (j = 1; j < (unsigned int)argc; j++, i++)
		rec_argv[i] = argv[j];

	BUG_ON(i != rec_argc);

	return cmd_record(i, rec_argv, NULL);
}

1661
int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
I
Ingo Molnar 已提交
1662
{
1663 1664 1665 1666 1667 1668 1669
	const char default_sort_order[] = "avg, max, switch, runtime";
	struct perf_sched sched = {
		.tool = {
			.sample		 = perf_sched__process_tracepoint_sample,
			.comm		 = perf_event__process_comm,
			.lost		 = perf_event__process_lost,
			.fork		 = perf_sched__process_fork_event,
1670
			.ordered_events = true,
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
		},
		.cmp_pid	      = LIST_HEAD_INIT(sched.cmp_pid),
		.sort_list	      = LIST_HEAD_INIT(sched.sort_list),
		.start_work_mutex     = PTHREAD_MUTEX_INITIALIZER,
		.work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
		.sort_order	      = default_sort_order,
		.replay_repeat	      = 10,
		.profile_cpu	      = -1,
		.next_shortname1      = 'A',
		.next_shortname2      = '0',
	};
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
	const struct option latency_options[] = {
	OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
		   "sort by key(s): runtime, switch, avg, max"),
	OPT_INCR('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
	OPT_INTEGER('C', "CPU", &sched.profile_cpu,
		    "CPU to profile on"),
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
	OPT_END()
	};
	const struct option replay_options[] = {
	OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
		     "repeat the workload replay N times (-1: infinite)"),
	OPT_INCR('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
	OPT_END()
	};
	const struct option sched_options[] = {
1703
	OPT_STRING('i', "input", &input_name, "file",
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
		    "input file name"),
	OPT_INCR('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
	OPT_END()
	};
	const char * const latency_usage[] = {
		"perf sched latency [<options>]",
		NULL
	};
	const char * const replay_usage[] = {
		"perf sched replay [<options>]",
		NULL
	};
1719 1720 1721 1722
	const char *const sched_subcommands[] = { "record", "latency", "map",
						  "replay", "script", NULL };
	const char *sched_usage[] = {
		NULL,
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
		NULL
	};
	struct trace_sched_handler lat_ops  = {
		.wakeup_event	    = latency_wakeup_event,
		.switch_event	    = latency_switch_event,
		.runtime_event	    = latency_runtime_event,
		.migrate_task_event = latency_migrate_task_event,
	};
	struct trace_sched_handler map_ops  = {
		.switch_event	    = map_switch_event,
	};
	struct trace_sched_handler replay_ops  = {
		.wakeup_event	    = replay_wakeup_event,
		.switch_event	    = replay_switch_event,
		.fork_event	    = replay_fork_event,
	};
A
Adrian Hunter 已提交
1739 1740 1741 1742
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
		sched.curr_pid[i] = -1;
1743

1744 1745
	argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
					sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
1746 1747
	if (!argc)
		usage_with_options(sched_usage, sched_options);
I
Ingo Molnar 已提交
1748

1749
	/*
1750
	 * Aliased to 'perf script' for now:
1751
	 */
1752 1753
	if (!strcmp(argv[0], "script"))
		return cmd_script(argc, argv, prefix);
1754

1755 1756 1757
	if (!strncmp(argv[0], "rec", 3)) {
		return __cmd_record(argc, argv);
	} else if (!strncmp(argv[0], "lat", 3)) {
1758
		sched.tp_handler = &lat_ops;
1759 1760 1761 1762 1763
		if (argc > 1) {
			argc = parse_options(argc, argv, latency_options, latency_usage, 0);
			if (argc)
				usage_with_options(latency_usage, latency_options);
		}
1764 1765
		setup_sorting(&sched, latency_options, latency_usage);
		return perf_sched__lat(&sched);
1766
	} else if (!strcmp(argv[0], "map")) {
1767 1768 1769
		sched.tp_handler = &map_ops;
		setup_sorting(&sched, latency_options, latency_usage);
		return perf_sched__map(&sched);
1770
	} else if (!strncmp(argv[0], "rep", 3)) {
1771
		sched.tp_handler = &replay_ops;
1772 1773 1774 1775 1776
		if (argc) {
			argc = parse_options(argc, argv, replay_options, replay_usage, 0);
			if (argc)
				usage_with_options(replay_usage, replay_options);
		}
1777
		return perf_sched__replay(&sched);
1778 1779 1780 1781
	} else {
		usage_with_options(sched_usage, sched_options);
	}

I
Ingo Molnar 已提交
1782
	return 0;
I
Ingo Molnar 已提交
1783
}