tests: sprinkle igt logging
[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 <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include "drm.h"
39 #include "ioctl_wrappers.h"
40 #include "drmtest.h"
41
42 #define BATCH_SIZE              (1024*1024)
43
44 #define LOCAL_I915_EXEC_NO_RELOC (1<<11)
45 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
46
47 #define MAX_NUM_EXEC 2048
48 #define MAX_NUM_RELOC 4096
49
50 #define USE_LUT 0x1
51 #define SKIP_RELOC 0x2
52 #define NO_RELOC 0x4
53
54 struct drm_i915_gem_exec_object2 gem_exec[MAX_NUM_EXEC+1];
55 struct drm_i915_gem_relocation_entry gem_reloc[MAX_NUM_RELOC];
56
57 static uint32_t state = 0x12345678;
58
59 static uint32_t
60 hars_petruska_f54_1_random (void)
61 {
62 #define rol(x,k) ((x << k) | (x >> (32-k)))
63     return state = (state ^ rol (state, 5) ^ rol (state, 24)) + 0x37798849;
64 #undef rol
65 }
66
67
68 static int exec(int fd, int num_exec, int num_relocs, unsigned flags)
69 {
70         struct drm_i915_gem_execbuffer2 execbuf;
71         struct drm_i915_gem_exec_object2 *objects;
72         int i;
73
74         gem_exec[MAX_NUM_EXEC].relocation_count = num_relocs;
75         gem_exec[MAX_NUM_EXEC].relocs_ptr = (uintptr_t) gem_reloc;
76
77         objects = gem_exec + MAX_NUM_EXEC - num_exec;
78
79         for (i = 0; i < num_relocs; i++) {
80                 int target = hars_petruska_f54_1_random() % num_exec;
81                 gem_reloc[i].offset = 1024;
82                 gem_reloc[i].delta = 0;
83                 gem_reloc[i].target_handle =
84                         flags & USE_LUT ? target : objects[target].handle;
85                 gem_reloc[i].read_domains = I915_GEM_DOMAIN_RENDER;
86                 gem_reloc[i].write_domain = 0;
87                 gem_reloc[i].presumed_offset = 0;
88                 if (flags & SKIP_RELOC)
89                         gem_reloc[i].presumed_offset = objects[target].offset;
90         }
91
92         execbuf.buffers_ptr = (uintptr_t)objects;
93         execbuf.buffer_count = num_exec + 1;
94         execbuf.batch_start_offset = 0;
95         execbuf.batch_len = 8;
96         execbuf.cliprects_ptr = 0;
97         execbuf.num_cliprects = 0;
98         execbuf.DR1 = 0;
99         execbuf.DR4 = 0;
100         execbuf.flags = 0;
101         if (flags & USE_LUT)
102                 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
103         if (flags & NO_RELOC)
104                 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
105         i915_execbuffer2_set_context_id(execbuf, 0);
106         execbuf.rsvd2 = 0;
107
108         return drmIoctl(fd,
109                         DRM_IOCTL_I915_GEM_EXECBUFFER2,
110                         &execbuf);
111 }
112
113 #define ELAPSED(a,b) (1e6*((b)->tv_sec - (a)->tv_sec) + ((b)->tv_usec - (a)->tv_usec))
114 igt_simple_main
115 {
116         uint32_t batch[2] = {MI_BATCH_BUFFER_END};
117         int fd, n, m, count;
118         const struct {
119                 const char *name;
120                 unsigned int flags;
121         } pass[] = {
122                 { .name = "relocation", .flags = 0 },
123                 { .name = "skip-relocs", .flags = SKIP_RELOC },
124                 { .name = "no-relocs", .flags = NO_RELOC },
125                 { .name = NULL },
126         }, *p;
127
128         igt_skip_on_simulation();
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         igt_skip_on(exec(fd, 1, 0, USE_LUT));
147
148         for (p = pass; p->name != NULL; p++) {
149                 for (n = 1; n <= MAX_NUM_EXEC; n *= 2) {
150                         double elapsed[16][2];
151                         double s_x, s_y, s_xx, s_xy;
152                         double A, B;
153                         int i, j;
154
155                         for (i = 0, m = 1; m <= MAX_NUM_RELOC; m *= 2, i++) {
156                                 struct timeval start, end;
157
158                                 gettimeofday(&start, NULL);
159                                 for (count = 0; count < 1000; count++)
160                                         do_or_die(exec(fd, n, m, 0 | p->flags));
161                                 gettimeofday(&end, NULL);
162                                 gem_sync(fd, gem_exec[MAX_NUM_EXEC].handle);
163                                 elapsed[i][0] = ELAPSED(&start, &end);
164
165                                 gettimeofday(&start, NULL);
166                                 for (count = 0; count < 1000; count++)
167                                         do_or_die(exec(fd, n, m, USE_LUT | p->flags));
168                                 gettimeofday(&end, NULL);
169                                 gem_sync(fd, gem_exec[MAX_NUM_EXEC].handle);
170                                 elapsed[i][1] = ELAPSED(&start, &end);
171                         }
172
173                         igt_info("%s: buffers=%4d:", p->name, n);
174
175                         s_x = s_y = s_xx = s_xy = 0;
176                         for (j = 0; j < i; j++) {
177                                 int k = 1 << j;
178                                 s_x += k;
179                                 s_y += elapsed[j][0];
180                                 s_xx += k * k;
181                                 s_xy += k * elapsed[j][0];
182                         }
183                         B = (s_xy - s_x * s_y / j) / (s_xx - s_x * s_x / j);
184                         A = s_y / j - B * s_x / j;
185                         igt_info(" old=%7.0f + %.1f*reloc,", A, B);
186
187                         s_x = s_y = s_xx = s_xy = 0;
188                         for (j = 0; j < i; j++) {
189                                 int k = 1 << j;
190                                 s_x += k;
191                                 s_y += elapsed[j][1];
192                                 s_xx += k * k;
193                                 s_xy += k * elapsed[j][1];
194                         }
195                         B = (s_xy - s_x * s_y / j) / (s_xx - s_x * s_x / j);
196                         A = s_y / j - B * s_x / j;
197                         igt_info(" lut=%7.0f + %.1f*reloc (ns)", A, B);
198
199                         igt_info("\n");
200                 }
201         }
202 }