blob: 0eac04b03d8c1fac64a83f5440c90b2e4391d73d [file] [log] [blame]
Chris Wilsone984d492015-07-22 15:01:47 +01001/*
2 * Copyright © 2011 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#include <unistd.h>
29#include <stdlib.h>
30#include <stdint.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/ioctl.h>
38#include <sys/time.h>
39#include <time.h>
40
41#include "drm.h"
42#include "ioctl_wrappers.h"
43#include "drmtest.h"
44#include "intel_io.h"
45#include "igt_stats.h"
46
Chris Wilsond44100e2015-11-27 09:56:09 +000047enum mode { NOP, CREATE, SWITCH, DEFAULT };
Chris Wilson69538992016-01-02 12:10:14 +000048#define SYNC 0x1
Chris Wilsone984d492015-07-22 15:01:47 +010049
50#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
51#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
52
Chris Wilson69538992016-01-02 12:10:14 +000053static double elapsed(const struct timespec *start,
54 const struct timespec *end)
Chris Wilsone984d492015-07-22 15:01:47 +010055{
Chris Wilson69538992016-01-02 12:10:14 +000056 return (end->tv_sec - start->tv_sec) + 1e-9*(end->tv_nsec - start->tv_nsec);
Chris Wilsone984d492015-07-22 15:01:47 +010057}
58
Chris Wilsone984d492015-07-22 15:01:47 +010059static uint32_t batch(int fd)
60{
61 const uint32_t buf[] = {MI_BATCH_BUFFER_END};
62 uint32_t handle = gem_create(fd, 4096);
63 gem_write(fd, handle, 0, buf, sizeof(buf));
64 return handle;
65}
66
67static uint32_t __gem_context_create(int fd)
68{
69 struct drm_i915_gem_context_create create;
70
71 memset(&create, 0, sizeof(create));
72 drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
73
74 return create.ctx_id;
75}
76
Chris Wilson6ec897c2016-05-07 21:26:56 +010077static int loop(unsigned ring,
78 int reps,
79 enum mode mode,
80 int ncpus,
81 unsigned flags)
Chris Wilsone984d492015-07-22 15:01:47 +010082{
83 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson6ec897c2016-05-07 21:26:56 +010084 struct drm_i915_gem_exec_object2 obj;
85 double *shared;
Chris Wilson69538992016-01-02 12:10:14 +000086 int fds[2], fd;
Chris Wilson6ec897c2016-05-07 21:26:56 +010087
88 shared = mmap(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
Chris Wilsone984d492015-07-22 15:01:47 +010089
Chris Wilsond44100e2015-11-27 09:56:09 +000090 fd = fds[0] = drm_open_driver(DRIVER_INTEL);
91 fds[1] = drm_open_driver(DRIVER_INTEL);
Chris Wilsone984d492015-07-22 15:01:47 +010092
Chris Wilson6ec897c2016-05-07 21:26:56 +010093 memset(&obj, 0, sizeof(obj));
94 obj.handle = batch(fd);
95 igt_assert(gem_open(fds[1], gem_flink(fds[0], obj.handle)) == obj.handle);
Chris Wilsone984d492015-07-22 15:01:47 +010096
97 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson6ec897c2016-05-07 21:26:56 +010098 execbuf.buffers_ptr = (uintptr_t)&obj;
Chris Wilsone984d492015-07-22 15:01:47 +010099 execbuf.buffer_count = 1;
100 execbuf.flags = ring;
101 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
102 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilsond44100e2015-11-27 09:56:09 +0000103 if (mode != DEFAULT) {
104 execbuf.rsvd1 = __gem_context_create(fd);
105 if (execbuf.rsvd1 == 0)
106 return 77;
107 }
Chris Wilsone984d492015-07-22 15:01:47 +0100108
109 if (__gem_execbuf(fd, &execbuf)) {
110 execbuf.flags = ring;
111 if (__gem_execbuf(fd, &execbuf))
112 return 77;
113 }
Chris Wilson6ec897c2016-05-07 21:26:56 +0100114 if (mode != DEFAULT && mode != NOP)
115 gem_context_destroy(fd, execbuf.rsvd1);
Chris Wilsone984d492015-07-22 15:01:47 +0100116
Chris Wilson69538992016-01-02 12:10:14 +0000117 while (reps--) {
Chris Wilson69538992016-01-02 12:10:14 +0000118 sleep(1); /* wait for the hw to go back to sleep */
Chris Wilsone984d492015-07-22 15:01:47 +0100119
Chris Wilson6ec897c2016-05-07 21:26:56 +0100120 memset(shared, 0, 4096);
Chris Wilsone984d492015-07-22 15:01:47 +0100121
Chris Wilson6ec897c2016-05-07 21:26:56 +0100122 igt_fork(child, ncpus) {
123 struct timespec start, end;
124 unsigned count = 0;
125 uint32_t ctx = 0;
Chris Wilsond44100e2015-11-27 09:56:09 +0000126
Chris Wilson6ec897c2016-05-07 21:26:56 +0100127 if (mode != DEFAULT && mode != NOP) {
128 execbuf.rsvd1 = __gem_context_create(fd);
129 ctx = gem_context_create(fd);
Chris Wilsone984d492015-07-22 15:01:47 +0100130 }
Chris Wilsone984d492015-07-22 15:01:47 +0100131
Chris Wilson6ec897c2016-05-07 21:26:56 +0100132 clock_gettime(CLOCK_MONOTONIC, &start);
133 do {
134 uint32_t tmp;
135 switch (mode) {
136 case CREATE:
137 ctx = execbuf.rsvd1;
138 execbuf.rsvd1 = gem_context_create(fd);
139 break;
140
141 case SWITCH:
142 tmp = execbuf.rsvd1;
143 execbuf.rsvd1 = ctx;
144 ctx = tmp;
145 break;
146
147 case DEFAULT:
148 fd = fds[count & 1];
149 break;
150
151 case NOP:
152 break;
153 }
154 gem_execbuf(fd, &execbuf);
155 count++;
156 if (mode == CREATE)
157 gem_context_destroy(fd, ctx);
158
159 if (flags & SYNC)
160 gem_sync(fd, obj.handle);
161
162 clock_gettime(CLOCK_MONOTONIC, &end);
163 } while (elapsed(&start, &end) < 2.);
164
165 gem_sync(fd, obj.handle);
Chris Wilson69538992016-01-02 12:10:14 +0000166
167 clock_gettime(CLOCK_MONOTONIC, &end);
Chris Wilson6ec897c2016-05-07 21:26:56 +0100168 shared[child] = 1e6*elapsed(&start, &end) / count;
Chris Wilson69538992016-01-02 12:10:14 +0000169
Chris Wilson6ec897c2016-05-07 21:26:56 +0100170 if (mode != DEFAULT && mode != NOP) {
171 if (mode != CREATE)
172 gem_context_destroy(fd, ctx);
173 gem_context_destroy(fd, execbuf.rsvd1);
174 }
175 }
176 igt_waitchildren();
Chris Wilson69538992016-01-02 12:10:14 +0000177
Chris Wilson6ec897c2016-05-07 21:26:56 +0100178 for (int child = 0; child < ncpus; child++)
179 shared[ncpus] += shared[child];
180 printf("%7.3f\n", shared[ncpus] / ncpus);
Chris Wilsone984d492015-07-22 15:01:47 +0100181 }
182 return 0;
183}
184
185int main(int argc, char **argv)
186{
187 unsigned ring = I915_EXEC_RENDER;
Chris Wilson69538992016-01-02 12:10:14 +0000188 unsigned flags = 0;
Chris Wilsone984d492015-07-22 15:01:47 +0100189 enum mode mode = NOP;
Chris Wilson69538992016-01-02 12:10:14 +0000190 int reps = 1;
Chris Wilson6ec897c2016-05-07 21:26:56 +0100191 int ncpus = 1;
Chris Wilsone984d492015-07-22 15:01:47 +0100192 int c;
193
Chris Wilson6ec897c2016-05-07 21:26:56 +0100194 while ((c = getopt (argc, argv, "e:r:b:sf")) != -1) {
Chris Wilsone984d492015-07-22 15:01:47 +0100195 switch (c) {
196 case 'e':
197 if (strcmp(optarg, "rcs") == 0)
198 ring = I915_EXEC_RENDER;
199 else if (strcmp(optarg, "vcs") == 0)
200 ring = I915_EXEC_BSD;
201 else if (strcmp(optarg, "bcs") == 0)
202 ring = I915_EXEC_BLT;
203 else if (strcmp(optarg, "vecs") == 0)
204 ring = I915_EXEC_VEBOX;
205 else
206 ring = atoi(optarg);
207 break;
208
209 case 'b':
210 if (strcmp(optarg, "create") == 0)
211 mode = CREATE;
212 else if (strcmp(optarg, "switch") == 0)
213 mode = SWITCH;
Chris Wilsond44100e2015-11-27 09:56:09 +0000214 else if (strcmp(optarg, "default") == 0)
215 mode = DEFAULT;
Chris Wilsone984d492015-07-22 15:01:47 +0100216 else if (strcmp(optarg, "nop") == 0)
217 mode = NOP;
218 else
219 abort();
220 break;
221
Chris Wilson6ec897c2016-05-07 21:26:56 +0100222 case 'f':
223 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
224 break;
225
Chris Wilsone984d492015-07-22 15:01:47 +0100226 case 'r':
227 reps = atoi(optarg);
228 if (reps < 1)
229 reps = 1;
230 break;
231
Chris Wilson69538992016-01-02 12:10:14 +0000232 case 's':
233 flags |= SYNC;
234 break;
235
Chris Wilsone984d492015-07-22 15:01:47 +0100236 default:
237 break;
238 }
239 }
240
Chris Wilson6ec897c2016-05-07 21:26:56 +0100241 return loop(ring, reps, mode, ncpus, flags);
Chris Wilsone984d492015-07-22 15:01:47 +0100242}