Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 1 | /* |
| 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 Wilson | d44100e | 2015-11-27 09:56:09 +0000 | [diff] [blame] | 47 | enum mode { NOP, CREATE, SWITCH, DEFAULT }; |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 48 | #define SYNC 0x1 |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 49 | |
| 50 | #define LOCAL_I915_EXEC_NO_RELOC (1<<11) |
| 51 | #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12) |
| 52 | |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 53 | static double elapsed(const struct timespec *start, |
| 54 | const struct timespec *end) |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 55 | { |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 56 | return (end->tv_sec - start->tv_sec) + 1e-9*(end->tv_nsec - start->tv_nsec); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 59 | static 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 | |
| 67 | static 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 Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 77 | static int loop(unsigned ring, |
| 78 | int reps, |
| 79 | enum mode mode, |
| 80 | int ncpus, |
| 81 | unsigned flags) |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 82 | { |
| 83 | struct drm_i915_gem_execbuffer2 execbuf; |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 84 | struct drm_i915_gem_exec_object2 obj; |
| 85 | double *shared; |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 86 | int fds[2], fd; |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 87 | |
| 88 | shared = mmap(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 89 | |
Chris Wilson | d44100e | 2015-11-27 09:56:09 +0000 | [diff] [blame] | 90 | fd = fds[0] = drm_open_driver(DRIVER_INTEL); |
| 91 | fds[1] = drm_open_driver(DRIVER_INTEL); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 92 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 93 | 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 Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 96 | |
| 97 | memset(&execbuf, 0, sizeof(execbuf)); |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 98 | execbuf.buffers_ptr = (uintptr_t)&obj; |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 99 | 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 Wilson | d44100e | 2015-11-27 09:56:09 +0000 | [diff] [blame] | 103 | if (mode != DEFAULT) { |
| 104 | execbuf.rsvd1 = __gem_context_create(fd); |
| 105 | if (execbuf.rsvd1 == 0) |
| 106 | return 77; |
| 107 | } |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 108 | |
| 109 | if (__gem_execbuf(fd, &execbuf)) { |
| 110 | execbuf.flags = ring; |
| 111 | if (__gem_execbuf(fd, &execbuf)) |
| 112 | return 77; |
| 113 | } |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 114 | if (mode != DEFAULT && mode != NOP) |
| 115 | gem_context_destroy(fd, execbuf.rsvd1); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 116 | |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 117 | while (reps--) { |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 118 | sleep(1); /* wait for the hw to go back to sleep */ |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 119 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 120 | memset(shared, 0, 4096); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 121 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 122 | igt_fork(child, ncpus) { |
| 123 | struct timespec start, end; |
| 124 | unsigned count = 0; |
| 125 | uint32_t ctx = 0; |
Chris Wilson | d44100e | 2015-11-27 09:56:09 +0000 | [diff] [blame] | 126 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 127 | if (mode != DEFAULT && mode != NOP) { |
| 128 | execbuf.rsvd1 = __gem_context_create(fd); |
| 129 | ctx = gem_context_create(fd); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 130 | } |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 131 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 132 | 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 Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 166 | |
| 167 | clock_gettime(CLOCK_MONOTONIC, &end); |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 168 | shared[child] = 1e6*elapsed(&start, &end) / count; |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 169 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 170 | 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 Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 177 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 178 | for (int child = 0; child < ncpus; child++) |
| 179 | shared[ncpus] += shared[child]; |
| 180 | printf("%7.3f\n", shared[ncpus] / ncpus); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int main(int argc, char **argv) |
| 186 | { |
| 187 | unsigned ring = I915_EXEC_RENDER; |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 188 | unsigned flags = 0; |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 189 | enum mode mode = NOP; |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 190 | int reps = 1; |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 191 | int ncpus = 1; |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 192 | int c; |
| 193 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 194 | while ((c = getopt (argc, argv, "e:r:b:sf")) != -1) { |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 195 | 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 Wilson | d44100e | 2015-11-27 09:56:09 +0000 | [diff] [blame] | 214 | else if (strcmp(optarg, "default") == 0) |
| 215 | mode = DEFAULT; |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 216 | else if (strcmp(optarg, "nop") == 0) |
| 217 | mode = NOP; |
| 218 | else |
| 219 | abort(); |
| 220 | break; |
| 221 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 222 | case 'f': |
| 223 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 224 | break; |
| 225 | |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 226 | case 'r': |
| 227 | reps = atoi(optarg); |
| 228 | if (reps < 1) |
| 229 | reps = 1; |
| 230 | break; |
| 231 | |
Chris Wilson | 6953899 | 2016-01-02 12:10:14 +0000 | [diff] [blame] | 232 | case 's': |
| 233 | flags |= SYNC; |
| 234 | break; |
| 235 | |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 236 | default: |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
Chris Wilson | 6ec897c | 2016-05-07 21:26:56 +0100 | [diff] [blame] | 241 | return loop(ring, reps, mode, ncpus, flags); |
Chris Wilson | e984d49 | 2015-07-22 15:01:47 +0100 | [diff] [blame] | 242 | } |