tests: Benchmark new API for using a LUT with the execbuffer
[platform/upstream/intel-gpu-tools.git] / tests / gem_exec_lut_handle.c
1 /*
2  * Copyright © 2012 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  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 /* Exercises the basic execbuffer using theh andle LUT interface */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include "drm.h"
40 #include "i915_drm.h"
41 #include "drmtest.h"
42
43 #define MI_BATCH_BUFFER_END     (0xA<<23)
44 #define BATCH_SIZE              (1024*1024)
45
46 #define LOCAL_I915_EXEC_NO_RELOC (1<<11)
47 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
48
49 #define MAX_NUM_EXEC 2048
50 #define MAX_NUM_RELOC 4096
51
52 #define USE_LUT 0x1
53 #define SKIP_RELOC 0x2
54 #define NO_RELOC 0x4
55
56 struct drm_i915_gem_exec_object2 gem_exec[MAX_NUM_EXEC+1];
57 struct drm_i915_gem_relocation_entry gem_reloc[MAX_NUM_RELOC];
58
59 static uint32_t state = 0x12345678;
60
61 static uint32_t
62 hars_petruska_f54_1_random (void)
63 {
64 #define rol(x,k) ((x << k) | (x >> (32-k)))
65     return state = (state ^ rol (state, 5) ^ rol (state, 24)) + 0x37798849;
66 #undef rol
67 }
68
69
70 static int exec(int fd, int num_exec, int num_relocs, unsigned flags)
71 {
72         struct drm_i915_gem_execbuffer2 execbuf;
73         struct drm_i915_gem_exec_object2 *objects;
74         int i;
75
76         gem_exec[MAX_NUM_EXEC].relocation_count = num_relocs;
77         gem_exec[MAX_NUM_EXEC].relocs_ptr = (uintptr_t) gem_reloc;
78
79         objects = gem_exec + MAX_NUM_EXEC - num_exec;
80
81         for (i = 0; i < num_relocs; i++) {
82                 int target = hars_petruska_f54_1_random() % num_exec;
83                 gem_reloc[i].offset = 1024;
84                 gem_reloc[i].delta = 0;
85                 gem_reloc[i].target_handle =
86                         flags & USE_LUT ? target : objects[target].handle;
87                 gem_reloc[i].read_domains = I915_GEM_DOMAIN_RENDER;
88                 gem_reloc[i].write_domain = 0;
89                 gem_reloc[i].presumed_offset = 0;
90                 if (flags & SKIP_RELOC)
91                         gem_reloc[i].presumed_offset = objects[target].offset;
92         }
93
94         execbuf.buffers_ptr = (uintptr_t)objects;
95         execbuf.buffer_count = num_exec + 1;
96         execbuf.batch_start_offset = 0;
97         execbuf.batch_len = 8;
98         execbuf.cliprects_ptr = 0;
99         execbuf.num_cliprects = 0;
100         execbuf.DR1 = 0;
101         execbuf.DR4 = 0;
102         execbuf.flags = 0;
103         if (flags & USE_LUT)
104                 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
105         if (flags & NO_RELOC)
106                 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
107         i915_execbuffer2_set_context_id(execbuf, 0);
108         execbuf.rsvd2 = 0;
109
110         return drmIoctl(fd,
111                         DRM_IOCTL_I915_GEM_EXECBUFFER2,
112                         &execbuf);
113 }
114
115 #define ELAPSED(a,b) (1e6*((b)->tv_sec - (a)->tv_sec) + ((b)->tv_usec - (a)->tv_usec))
116 int main(int argc, char **argv)
117 {
118         uint32_t batch[2] = {MI_BATCH_BUFFER_END};
119         int fd, n, m, count;
120         const struct {
121                 const char *name;
122                 unsigned int flags;
123         } pass[] = {
124                 { .name = "relocation", .flags = 0 },
125                 { .name = "skip-relocs", .flags = SKIP_RELOC },
126                 { .name = "no-relocs", .flags = NO_RELOC },
127                 { .name = NULL },
128         }, *p;
129
130         fd = drm_open_any();
131
132         for (n = 0; n < MAX_NUM_EXEC; n++) {
133                 gem_exec[n].handle = gem_create(fd, 4096);
134                 gem_exec[n].relocation_count = 0;
135                 gem_exec[n].relocs_ptr = 0;
136                 gem_exec[n].alignment = 0;
137                 gem_exec[n].offset = 0;
138                 gem_exec[n].flags = 0;
139                 gem_exec[n].rsvd1 = 0;
140                 gem_exec[n].rsvd2 = 0;
141         }
142
143         gem_exec[n].handle =  gem_create(fd, 4096);
144         gem_write(fd, gem_exec[n].handle, 0, batch, sizeof(batch));
145
146         do_or_die(exec(fd, MAX_NUM_EXEC, 0, USE_LUT));
147
148         for (p = pass; p->name != NULL; p++) {
149                 for (n = 1; n <= MAX_NUM_EXEC; n *= 2) {
150                         for (m = 1; m <= MAX_NUM_RELOC; m *= 2) {
151                                 struct timeval start, end;
152                                 double elapsed[2];
153
154                                 gettimeofday(&start, NULL);
155                                 for (count = 0; count < 1000; count++)
156                                         do_or_die(exec(fd, n, m, 0 | p->flags));
157                                 gettimeofday(&end, NULL);
158                                 gem_sync(fd, gem_exec[MAX_NUM_EXEC].handle);
159                                 elapsed[0] = ELAPSED(&start, &end) / 1000.;
160
161                                 gettimeofday(&start, NULL);
162                                 for (count = 0; count < 1000; count++)
163                                         do_or_die(exec(fd, n, m, USE_LUT | p->flags));
164                                 gettimeofday(&end, NULL);
165                                 gem_sync(fd, gem_exec[MAX_NUM_EXEC].handle);
166                                 elapsed[1] = ELAPSED(&start, &end) / 1000.;
167
168                                 printf("%s: buffer_count=%d, reloc_count=%d: old=%f us, lut=%f us\n",
169                                        p->name, n, m, elapsed[0], elapsed[1]);
170                         }
171                 }
172         }
173
174         return 0;
175 }