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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/*
* Copyright © 2014 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.
*
* Authors:
* Tvrtko Ursulin <tvrtko.ursulin@intel.com>
*
*/
/** @file gem_userptr_benchmark.c
*
* Benchmark the userptr code and impact of having userptr surfaces
* in process address space on some normal operations.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <signal.h>
#include "drm.h"
#include "i915_drm.h"
#include "drmtest.h"
#include "intel_batchbuffer.h"
#include "intel_chipset.h"
#include "ioctl_wrappers.h"
#include "igt_aux.h"
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
static uint32_t userptr_flags = 0;
#define BO_SIZE (65536)
static void **handle_ptr_map;
static unsigned int num_handle_ptr_map;
static void add_handle_ptr(uint32_t handle, void *ptr)
{
if (handle >= num_handle_ptr_map) {
handle_ptr_map = realloc(handle_ptr_map,
(handle + 1000) * sizeof(void*));
num_handle_ptr_map = handle + 1000;
}
handle_ptr_map[handle] = ptr;
}
static void *get_handle_ptr(uint32_t handle)
{
return handle_ptr_map[handle];
}
static void free_handle_ptr(uint32_t handle)
{
igt_assert(handle < num_handle_ptr_map);
igt_assert(handle_ptr_map[handle]);
free(handle_ptr_map[handle]);
handle_ptr_map[handle] = NULL;
}
static uint32_t create_userptr_bo(int fd, int size)
{
void *ptr;
uint32_t handle;
int ret;
ret = posix_memalign(&ptr, PAGE_SIZE, size);
igt_assert(ret == 0);
gem_userptr(fd, (uint32_t *)ptr, size, 0, userptr_flags, &handle);
add_handle_ptr(handle, ptr);
return handle;
}
static void free_userptr_bo(int fd, uint32_t handle)
{
gem_close(fd, handle);
free_handle_ptr(handle);
}
static int has_userptr(int fd)
{
uint32_t handle = 0;
void *ptr;
int ret;
assert(posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE) == 0);
ret = __gem_userptr(fd, ptr, PAGE_SIZE, 0, userptr_flags, &handle);
if (ret != 0) {
free(ptr);
return 0;
}
gem_close(fd, handle);
free(ptr);
return handle != 0;
}
static const unsigned int nr_bos[] = {0, 1, 10, 100, 1000, 10000};
static const unsigned int test_duration_sec = 3;
static volatile unsigned int run_test;
static void alarm_handler(int sig)
{
assert(run_test == 1);
run_test = 0;
}
static void start_test(unsigned int duration)
{
run_test = 1;
if (duration == 0)
duration = test_duration_sec;
signal(SIGALRM, alarm_handler);
alarm(duration);
}
static void exchange_ptr(void *array, unsigned i, unsigned j)
{
void **arr, *tmp;
arr = (void **)array;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
static void test_malloc_free(int random)
{
unsigned long iter = 0;
unsigned int i, tot = 1000;
void *ptr[tot];
start_test(test_duration_sec);
while (run_test) {
for (i = 0; i < tot; i++) {
ptr[i] = malloc(1000);
assert(ptr[i]);
}
if (random)
igt_permute_array(ptr, tot, exchange_ptr);
for (i = 0; i < tot; i++)
free(ptr[i]);
iter++;
}
printf("%8lu iter/s\n", iter / test_duration_sec);
}
static void test_malloc_realloc_free(int random)
{
unsigned long iter = 0;
unsigned int i, tot = 1000;
void *ptr[tot];
start_test(test_duration_sec);
while (run_test) {
for (i = 0; i < tot; i++) {
ptr[i] = malloc(1000);
assert(ptr[i]);
}
if (random)
igt_permute_array(ptr, tot, exchange_ptr);
for (i = 0; i < tot; i++) {
ptr[i] = realloc(ptr[i], 2000);
assert(ptr[i]);
}
if (random)
igt_permute_array(ptr, tot, exchange_ptr);
for (i = 0; i < tot; i++)
free(ptr[i]);
iter++;
}
printf("%8lu iter/s\n", iter / test_duration_sec);
}
static void test_mmap_unmap(int random)
{
unsigned long iter = 0;
unsigned int i, tot = 1000;
void *ptr[tot];
start_test(test_duration_sec);
while (run_test) {
for (i = 0; i < tot; i++) {
ptr[i] = mmap(NULL, 1000, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assert(ptr[i] != MAP_FAILED);
}
if (random)
igt_permute_array(ptr, tot, exchange_ptr);
for (i = 0; i < tot; i++)
munmap(ptr[i], 1000);
iter++;
}
printf("%8lu iter/s\n", iter / test_duration_sec);
}
static void test_ptr_read(void *ptr)
{
unsigned long iter = 0;
volatile unsigned long *p;
unsigned long i, loops;
loops = BO_SIZE / sizeof(unsigned long) / 4;
start_test(test_duration_sec);
while (run_test) {
p = (unsigned long *)ptr;
for (i = 0; i < loops; i++) {
(void)*p++;
(void)*p++;
(void)*p++;
(void)*p++;
}
iter++;
}
printf("%8lu MB/s\n", iter / test_duration_sec * BO_SIZE / 1000000);
}
static void test_ptr_write(void *ptr)
{
unsigned long iter = 0;
volatile unsigned long *p;
register unsigned long i, loops;
loops = BO_SIZE / sizeof(unsigned long) / 4;
start_test(test_duration_sec);
while (run_test) {
p = (unsigned long *)ptr;
for (i = 0; i < loops; i++) {
*p++ = i;
*p++ = i;
*p++ = i;
*p++ = i;
}
iter++;
}
printf("%8lu MB/s\n", iter / test_duration_sec * BO_SIZE / 1000000);
}
static void do_impact_tests(unsigned int n, const char *pfix, const char *pfix2,
void *ptr)
{
printf("%s%sptr-read, %5u bos = ", pfix, pfix2, n);
test_ptr_read(ptr);
printf("%s%sptr-write %5u bos = ", pfix, pfix2, n);
test_ptr_write(ptr);
printf("%s%smalloc-free, %5u bos = ", pfix, pfix2, n);
test_malloc_free(0);
printf("%s%smalloc-free-random %5u bos = ", pfix, pfix2, n);
test_malloc_free(1);
printf("%s%smalloc-realloc-free, %5u bos = ", pfix, pfix2, n);
test_malloc_realloc_free(0);
printf("%s%smalloc-realloc-free-random, %5u bos = ", pfix, pfix2, n);
test_malloc_realloc_free(1);
printf("%s%smmap-unmap, %5u bos = ", pfix, pfix2, n);
test_mmap_unmap(0);
printf("%s%smmap-unmap-random, %5u bos = ", pfix, pfix2, n);
test_mmap_unmap(1);
}
static void test_impact_overlap(int fd, const char *prefix)
{
unsigned int total = sizeof(nr_bos) / sizeof(nr_bos[0]);
unsigned int subtest, i;
uint32_t handles[nr_bos[total-1]];
void *block = NULL;
void *ptr;
unsigned char *p;
char buffer[BO_SIZE];
int ret;
for (subtest = 0; subtest < total; subtest++) {
if (nr_bos[subtest] > 0) {
igt_assert(PAGE_SIZE < BO_SIZE);
ret = posix_memalign(&block, PAGE_SIZE,
PAGE_SIZE * nr_bos[subtest] + BO_SIZE);
igt_assert(ret == 0);
for (i = 0, p = block; i < nr_bos[subtest];
i++, p += PAGE_SIZE)
gem_userptr(fd, (uint32_t *)p, BO_SIZE, 0, userptr_flags, &handles[i]);
}
if (nr_bos[subtest] > 0)
ptr = block;
else
ptr = buffer;
do_impact_tests(nr_bos[subtest], prefix, "overlap-", ptr);
for (i = 0; i < nr_bos[subtest]; i++)
gem_close(fd, handles[i]);
free(block);
block = NULL;
}
}
static void test_impact(int fd, const char *prefix)
{
unsigned int total = sizeof(nr_bos) / sizeof(nr_bos[0]);
unsigned int subtest, i;
uint32_t handles[nr_bos[total-1]];
void *ptr;
char buffer[BO_SIZE];
for (subtest = 0; subtest < total; subtest++) {
for (i = 0; i < nr_bos[subtest]; i++)
handles[i] = create_userptr_bo(fd, BO_SIZE);
if (nr_bos[subtest] > 0)
ptr = get_handle_ptr(handles[0]);
else
ptr = buffer;
do_impact_tests(nr_bos[subtest], prefix, "no-overlap-", ptr);
for (i = 0; i < nr_bos[subtest]; i++)
free_userptr_bo(fd, handles[i]);
}
}
static void test_single(int fd)
{
char *ptr, *bo_ptr;
uint32_t handle = 0;
unsigned long iter = 0;
unsigned long map_size = BO_SIZE + PAGE_SIZE - 1;
ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assert(ptr != MAP_FAILED);
bo_ptr = (char *)ALIGN((unsigned long)ptr, PAGE_SIZE);
start_test(test_duration_sec);
while (run_test) {
gem_userptr(fd, bo_ptr, BO_SIZE, 0, userptr_flags, &handle);
gem_close(fd, handle);
iter++;
}
munmap(ptr, map_size);
printf("%8lu iter/s\n", iter / test_duration_sec);
}
static void test_multiple(int fd, unsigned int batch, int random)
{
char *ptr, *bo_ptr;
uint32_t handles[10000];
int map[10000];
unsigned long iter = 0;
int i;
unsigned long map_size = batch * BO_SIZE + PAGE_SIZE - 1;
assert(batch < (sizeof(handles) / sizeof(handles[0])));
assert(batch < (sizeof(map) / sizeof(map[0])));
ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assert(ptr != MAP_FAILED);
bo_ptr = (char *)ALIGN((unsigned long)ptr, PAGE_SIZE);
for (i = 0; i < batch; i++)
map[i] = i;
start_test(test_duration_sec);
while (run_test) {
if (random)
igt_permute_array(map, batch, igt_exchange_int);
for (i = 0; i < batch; i++) {
gem_userptr(fd, bo_ptr + map[i] * BO_SIZE, BO_SIZE,
0, userptr_flags, &handles[i]);
}
if (random)
igt_permute_array(map, batch, igt_exchange_int);
for (i = 0; i < batch; i++)
gem_close(fd, handles[map[i]]);
iter++;
}
munmap(ptr, map_size);
printf("%8lu iter/s\n", iter * batch / test_duration_sec);
}
static void test_userptr(int fd)
{
printf("create-destroy = ");
test_single(fd);
printf("multi-create-destroy = ");
test_multiple(fd, 100, 0);
printf("multi-create-destroy-random = ");
test_multiple(fd, 100, 1);
}
igt_main
{
int fd = -1, ret;
igt_fixture {
fd = drm_open_driver(DRIVER_INTEL);
igt_assert(fd >= 0);
ret = has_userptr(fd);
igt_skip_on_f(ret == 0, "No userptr support - %s (%d)\n",
strerror(errno), ret);
}
igt_subtest("userptr-sync")
test_userptr(fd);
igt_subtest("userptr-impact-sync")
test_impact(fd, "sync-");
igt_subtest("userptr-impact-sync-overlap")
test_impact_overlap(fd, "sync-");
igt_fixture {
close(fd);
}
}
|