gem_syslatency.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright © 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

#include "igt.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
32
#include <ftw.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <inttypes.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <time.h>
#include <limits.h>
#include "drm.h"

#include <linux/unistd.h>

#define gettid() syscall(__NR_gettid)
#define sigev_notify_thread_id _sigev_un._tid

static volatile int done;

struct gem_busyspin {
	pthread_t thread;
54
	unsigned long sz;
55
	unsigned long count;
56
	bool leak;
57
	bool interrupts;
58 59 60 61 62 63 64
};

struct sys_wait {
	pthread_t thread;
	struct igt_mean mean;
};

65 66 67 68 69 70 71 72 73 74
static void force_low_latency(void)
{
	int32_t target = 0;
	int fd = open("/dev/cpu_dma_latency", O_RDWR);
	if (fd < 0 || write(fd, &target, sizeof(target)) < 0)
		fprintf(stderr,
			"Unable to prevent CPU sleeps and force low latency using /dev/cpu_dma_latency: %s\n",
			strerror(errno));
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)

#define LOCAL_I915_EXEC_BSD_SHIFT      (13)
#define LOCAL_I915_EXEC_BSD_MASK       (3 << LOCAL_I915_EXEC_BSD_SHIFT)

#define ENGINE_FLAGS  (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)

static bool ignore_engine(int fd, unsigned engine)
{
	if (engine == 0)
		return true;

	if (gem_has_bsd2(fd) && engine == I915_EXEC_BSD)
		return true;

	return false;
}

static void *gem_busyspin(void *arg)
{
	const uint32_t bbe = MI_BATCH_BUFFER_END;
	struct gem_busyspin *bs = arg;
	struct drm_i915_gem_execbuffer2 execbuf;
99
	struct drm_i915_gem_exec_object2 obj[2];
100 101
	const unsigned sz =
		bs->sz ? bs->sz + sizeof(bbe) : bs->leak ? 16 << 20 : 4 << 10;
102 103 104 105 106 107 108 109 110 111 112
	unsigned engines[16];
	unsigned nengine;
	unsigned engine;
	int fd;

	fd = drm_open_driver(DRIVER_INTEL);

	nengine = 0;
	for_each_engine(fd, engine)
		if (!ignore_engine(fd, engine)) engines[nengine++] = engine;

113 114 115 116
	memset(obj, 0, sizeof(obj));
	obj[0].handle = gem_create(fd, 4096);
	obj[0].flags = EXEC_OBJECT_WRITE;
	obj[1].handle = gem_create(fd, sz);
117
	gem_write(fd, obj[1].handle, bs->sz, &bbe, sizeof(bbe));
118 119

	memset(&execbuf, 0, sizeof(execbuf));
120 121 122 123 124 125 126
	if (bs->interrupts) {
		execbuf.buffers_ptr = (uintptr_t)&obj[0];
		execbuf.buffer_count = 2;
	} else {
		execbuf.buffers_ptr = (uintptr_t)&obj[1];
		execbuf.buffer_count = 1;
	}
127 128
	execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
	execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
129
	if (__gem_execbuf(fd, &execbuf)) {
130 131 132 133 134
		execbuf.flags = 0;
		gem_execbuf(fd, &execbuf);
	}

	while (!done) {
135 136 137 138 139 140
		for (int n = 0; n < nengine; n++) {
			const int m = rand() % nengine;
			unsigned int tmp = engines[n];
			engines[n] = engines[m];
			engines[m] = tmp;
		}
141 142 143 144 145 146
		for (int n = 0; n < nengine; n++) {
			execbuf.flags &= ~ENGINE_FLAGS;
			execbuf.flags |= engines[n];
			gem_execbuf(fd, &execbuf);
		}
		bs->count += nengine;
147
		if (bs->leak) {
148 149
			gem_madvise(fd, obj[1].handle, I915_MADV_DONTNEED);
			obj[1].handle = gem_create(fd, sz);
150
			gem_write(fd, obj[1].handle, bs->sz, &bbe, sizeof(bbe));
151
		}
152 153 154 155 156 157 158 159
	}

	close(fd);
	return NULL;
}

static double elapsed(const struct timespec *a, const struct timespec *b)
{
160
	return 1e9*(b->tv_sec - a->tv_sec) + (b->tv_nsec - a ->tv_nsec);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

static void *sys_wait(void *arg)
{
	struct sys_wait *w = arg;
	struct sigevent sev;
	timer_t timer;
	sigset_t mask;
	struct timespec now;
#define SIG SIGRTMIN

	sigemptyset(&mask);
	sigaddset(&mask, SIG);
	sigprocmask(SIG_SETMASK, &mask, NULL);

	sev.sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID;
	sev.sigev_notify_thread_id = gettid();
	sev.sigev_signo = SIG;
	timer_create(CLOCK_MONOTONIC, &sev, &timer);

	clock_gettime(CLOCK_MONOTONIC, &now);
	while (!done) {
		struct itimerspec its;
		int sigs;

		its.it_value = now;
		its.it_value.tv_nsec += 100 * 1000;
		its.it_value.tv_nsec += rand() % (NSEC_PER_SEC / 1000);
		if (its.it_value.tv_nsec >= NSEC_PER_SEC) {
			its.it_value.tv_nsec -= NSEC_PER_SEC;
			its.it_value.tv_sec += 1;
		}
		its.it_interval.tv_sec = its.it_interval.tv_nsec = 0;
		timer_settime(timer, TIMER_ABSTIME, &its, NULL);

		sigwait(&mask, &sigs);
		clock_gettime(CLOCK_MONOTONIC, &now);
		igt_mean_add(&w->mean, elapsed(&its.it_value, &now));
	}

	sigprocmask(SIG_UNBLOCK, &mask, NULL);
	timer_delete(timer);

	return NULL;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#define PAGE_SIZE 4096
static void *sys_thp_alloc(void *arg)
{
	struct sys_wait *w = arg;
	struct timespec now;

	clock_gettime(CLOCK_MONOTONIC, &now);
	while (!done) {
		const size_t sz = 2 << 20;
		const struct timespec start = now;
		void *ptr;

		ptr = mmap(NULL, sz,
			   PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
			   -1, 0);
		assert(ptr != MAP_FAILED);
		madvise(ptr, sz, MADV_HUGEPAGE);
		for (size_t page = 0; page < sz; page += PAGE_SIZE)
225
			*(volatile uint32_t *)((unsigned char *)ptr + page) = 0;
226 227 228 229 230 231 232 233 234
		munmap(ptr, sz);

		clock_gettime(CLOCK_MONOTONIC, &now);
		igt_mean_add(&w->mean, elapsed(&start, &now));
	}

	return NULL;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static void bind_cpu(pthread_attr_t *attr, int cpu)
{
#ifdef __USE_GNU
	cpu_set_t mask;

	if (cpu == -1)
		return;

	CPU_ZERO(&mask);
	CPU_SET(cpu, &mask);

	pthread_attr_setaffinity_np(attr, sizeof(mask), &mask);
#endif
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
static void rtprio(pthread_attr_t *attr, int prio)
{
#ifdef PTHREAD_EXPLICIT_SCHED
	struct sched_param param = { .sched_priority = 99 };
	pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(attr, SCHED_FIFO);
	pthread_attr_setschedparam(attr, &param);
#endif
}

static double l_estimate(igt_stats_t *stats)
{
	if (stats->n_values > 9)
		return igt_stats_get_trimean(stats);
	else if (stats->n_values > 5)
		return igt_stats_get_median(stats);
	else
		return igt_stats_get_mean(stats);
}

270 271 272 273 274 275 276 277 278 279 280 281
static double min_measurement_error(void)
{
	struct timespec start, end;
	int n;

	clock_gettime(CLOCK_MONOTONIC, &start);
	for (n = 0; n < 1024; n++)
		clock_gettime(CLOCK_MONOTONIC, &end);

	return elapsed(&start, &end) / n;
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
static int print_entry(const char *filepath, const struct stat *info,
		       const int typeflag, struct FTW *pathinfo)
{
	int fd;

	fd = open(filepath, O_RDONLY);
	if (fd != -1)  {
		void *ptr;

		ptr = mmap(NULL, info->st_size,
			   PROT_READ, MAP_SHARED | MAP_POPULATE,
			   fd, 0);
		if (ptr != MAP_FAILED)
			munmap(ptr, info->st_size);

		close(fd);
	}

	return 0;
}

static void *background_fs(void *path)
{
	while (1)
		nftw(path, print_entry, 20, FTW_PHYS | FTW_MOUNT);
	return NULL;
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static unsigned long calibrate_nop(unsigned int target_us,
				   unsigned int tolerance_pct)
{
	const uint32_t bbe = MI_BATCH_BUFFER_END;
	const unsigned int loops = 100;
	struct drm_i915_gem_exec_object2 obj = {};
	struct drm_i915_gem_execbuffer2 eb =
		{ .buffer_count = 1, .buffers_ptr = (uintptr_t)&obj};
	struct timespec t_0, t_end;
	long sz, prev;
	int fd;

	fd = drm_open_driver(DRIVER_INTEL);

	clock_gettime(CLOCK_MONOTONIC, &t_0);

	sz = 256 * 1024;
	do {
		struct timespec t_start;

		obj.handle = gem_create(fd, sz + sizeof(bbe));
		gem_write(fd, obj.handle, sz, &bbe, sizeof(bbe));
		gem_execbuf(fd, &eb);
		gem_sync(fd, obj.handle);

		clock_gettime(CLOCK_MONOTONIC, &t_start);
		for (int loop = 0; loop < loops; loop++)
			gem_execbuf(fd, &eb);
		gem_sync(fd, obj.handle);
		clock_gettime(CLOCK_MONOTONIC, &t_end);

		gem_close(fd, obj.handle);

		prev = sz;
		sz = loops * sz / elapsed(&t_start, &t_end) * 1e3 * target_us;
		sz = ALIGN(sz, sizeof(uint32_t));
	} while (elapsed(&t_0, &t_end) < 5 ||
		 abs(sz - prev) > (sz * tolerance_pct / 100));

	close(fd);

	return sz;
}

354 355 356 357
int main(int argc, char **argv)
{
	struct gem_busyspin *busy;
	struct sys_wait *wait;
358
	void *sys_fn = sys_wait;
359
	pthread_attr_t attr;
360
	pthread_t bg_fs = 0;
361 362
	int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
	igt_stats_t cycles, mean, max;
363
	double min;
364 365
	int time = 10;
	int field = -1;
366
	int enable_gem_sysbusy = 1;
367
	bool leak = false;
368
	bool interrupts = false;
369
	long batch = 0;
370 371
	int n, c;

372
	while ((c = getopt(argc, argv, "r:t:f:bmni1")) != -1) {
373
		switch (c) {
374 375 376
		case '1':
			ncpus = 1;
			break;
377 378 379
		case 'n': /* dry run, measure baseline system latency */
			enable_gem_sysbusy = 0;
			break;
380 381 382
		case 'i': /* interrupts ahoy! */
			interrupts = true;
			break;
383 384 385 386 387 388
		case 't':
			/* How long to run the benchmark for (seconds) */
			time = atoi(optarg);
			if (time < 0)
				time = INT_MAX;
			break;
389 390 391 392
		case 'r':
			/* Duration of each batch (microseconds) */
			batch = atoi(optarg);
			break;
393 394 395 396
		case 'f':
			/* Select an output field */
			field = atoi(optarg);
			break;
397 398 399 400 401 402 403 404 405
		case 'b':
			pthread_create(&bg_fs, NULL,
				       background_fs, (void *)"/");
			sleep(5);
			break;
		case 'm':
			sys_fn = sys_thp_alloc;
			leak = true;
			break;
406 407 408 409 410
		default:
			break;
		}
	}

411 412
	/* Prevent CPU sleeps so that busy and idle loads are consistent. */
	force_low_latency();
413
	min = min_measurement_error();
414

415 416 417 418 419
	if (batch > 0)
		batch = calibrate_nop(batch, 2);
	else
		batch = -batch;

420
	busy = calloc(ncpus, sizeof(*busy));
421
	pthread_attr_init(&attr);
422 423
	if (enable_gem_sysbusy) {
		for (n = 0; n < ncpus; n++) {
424
			bind_cpu(&attr, n);
425
			busy[n].sz = batch;
426
			busy[n].leak = leak;
427
			busy[n].interrupts = interrupts;
428
			pthread_create(&busy[n].thread, &attr,
429 430
				       gem_busyspin, &busy[n]);
		}
431 432
	}

433
	wait = calloc(ncpus, sizeof(*wait));
434 435 436 437
	pthread_attr_init(&attr);
	rtprio(&attr, 99);
	for (n = 0; n < ncpus; n++) {
		igt_mean_init(&wait[n].mean);
438
		bind_cpu(&attr, n);
439
		pthread_create(&wait[n].thread, &attr, sys_fn, &wait[n]);
440 441 442 443 444 445
	}

	sleep(time);
	done = 1;

	igt_stats_init_with_size(&cycles, ncpus);
446 447 448 449 450
	if (enable_gem_sysbusy) {
		for (n = 0; n < ncpus; n++) {
			pthread_join(busy[n].thread, NULL);
			igt_stats_push(&cycles, busy[n].count);
		}
451 452 453 454 455 456 457 458 459
	}

	igt_stats_init_with_size(&mean, ncpus);
	igt_stats_init_with_size(&max, ncpus);
	for (n = 0; n < ncpus; n++) {
		pthread_join(wait[n].thread, NULL);
		igt_stats_push_float(&mean, wait[n].mean.mean);
		igt_stats_push_float(&max, wait[n].mean.max);
	}
460 461 462 463
	if (bg_fs) {
		pthread_cancel(bg_fs);
		pthread_join(bg_fs, NULL);
	}
464 465 466

	switch (field) {
	default:
467
		printf("gem_syslatency: cycles=%.0f, latency mean=%.3fus max=%.0fus\n",
468
		       igt_stats_get_mean(&cycles),
469 470
		       (igt_stats_get_mean(&mean) - min)/ 1000,
		       (l_estimate(&max) - min) / 1000);
471 472 473 474 475
		break;
	case 0:
		printf("%.0f\n", igt_stats_get_mean(&cycles));
		break;
	case 1:
476
		printf("%.3f\n", (igt_stats_get_mean(&mean) - min) / 1000);
477 478
		break;
	case 2:
479
		printf("%.0f\n", (l_estimate(&max) - min) / 1000);
480 481 482 483 484 485
		break;
	}

	return 0;

}