blob: c9e847b7b54c1d6721ef436055826e94a752ba88 [file] [log] [blame]
Chris Wilson8f3f8622009-09-01 10:09:55 +01001/*
2 * Copyright © 2008-9 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
Damien Lespiau7ee278f2013-02-20 14:40:07 +000029#ifdef HAVE_CONFIG_H
Alan Coopersmith504c4fa2012-01-06 15:45:29 -080030#include "config.h"
Damien Lespiau7ee278f2013-02-20 14:40:07 +000031#endif
Chris Wilson8f3f8622009-09-01 10:09:55 +010032
33#include <unistd.h>
34#include <stdlib.h>
35#include <stdio.h>
36#include <string.h>
37#include <assert.h>
38#include <fcntl.h>
39#include <inttypes.h>
40#include <errno.h>
41#include <sys/stat.h>
42#include <sys/ioctl.h>
43#include <sys/mman.h>
44#include <pthread.h>
45#include "drm.h"
46#include "i915_drm.h"
47#include "drmtest.h"
48
Daniel Vetter2e9e27c2011-10-15 13:46:19 +020049#define OBJECT_SIZE (128*1024) /* restricted to 1MiB alignment on i915 fences */
Chris Wilson8f3f8622009-09-01 10:09:55 +010050
51/* Before introduction of the LRU list for fences, allocation of a fence for a page
52 * fault would use the first inactive fence (i.e. in preference one with no outstanding
53 * GPU activity, or it would wait on the first to finish). Given the choice, it would simply
54 * reuse the fence that had just been allocated for the previous page-fault - the worst choice
55 * when copying between two buffers and thus constantly swapping fences.
56 */
57
58static void *
59bo_create (int fd)
60{
Chris Wilson8f3f8622009-09-01 10:09:55 +010061 void *ptr;
62 int handle;
Chris Wilson8f3f8622009-09-01 10:09:55 +010063
Daniel Vetterff93f352012-03-24 19:22:21 +010064 handle = gem_create(fd, OBJECT_SIZE);
Chris Wilson8f3f8622009-09-01 10:09:55 +010065
Daniel Vetterff93f352012-03-24 19:22:21 +010066 gem_set_tiling(fd, handle, I915_TILING_X, 1024);
Chris Wilson8f3f8622009-09-01 10:09:55 +010067
Daniel Vetterff93f352012-03-24 19:22:21 +010068 ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
Chris Wilson8f3f8622009-09-01 10:09:55 +010069
70 /* XXX: mmap_gtt pulls the bo into the GTT read domain. */
Daniel Vetter673e6b22012-01-10 16:05:34 +010071 gem_sync(fd, handle);
Chris Wilson8f3f8622009-09-01 10:09:55 +010072
73 return ptr;
74}
75
76static void *
77bo_copy (void *_arg)
78{
79 int fd = *(int *)_arg;
Daniel Vetter2e9e27c2011-10-15 13:46:19 +020080 int n;
Chris Wilson8f3f8622009-09-01 10:09:55 +010081 char *a, *b;
82
83 a = bo_create (fd);
84 b = bo_create (fd);
85
Daniel Vetter2e9e27c2011-10-15 13:46:19 +020086 for (n = 0; n < 1000; n++) {
87 memcpy (a, b, OBJECT_SIZE);
Alan Coopersmith00751592012-01-06 15:45:28 -080088 sched_yield ();
Chris Wilson8f3f8622009-09-01 10:09:55 +010089 }
90
91 return NULL;
92}
93
94int
95main(int argc, char **argv)
96{
97 drm_i915_getparam_t gp;
98 pthread_t threads[32];
99 int n, num_fences;
100 int fd, ret;
101
102 fd = drm_open_any();
103
104 gp.param = I915_PARAM_NUM_FENCES_AVAIL;
105 gp.value = &num_fences;
106 ret = ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
107 assert (ret == 0);
108
109 printf ("creating %d threads\n", num_fences);
110 assert (num_fences < sizeof (threads) / sizeof (threads[0]));
111
112 for (n = 0; n < num_fences; n++)
113 pthread_create (&threads[n], NULL, bo_copy, &fd);
114
115 for (n = 0; n < num_fences; n++)
116 pthread_join (threads[n], NULL);
117
118 close(fd);
119
120 return 0;
121}