1 /* drmstat.c -- DRM device status and testing program
2 * Created: Tue Jan 5 08:19:24 1999 by faith@precisioninsight.com
3 * Revised: Sun Aug 1 11:02:00 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/drmstat.c,v 1.28 1999/08/04 18:12:11 faith Exp $
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/drmstat.c,v 1.1 1999/09/25 14:37:59 dawes Exp $
35 #include <sys/types.h>
47 static double usec(struct timeval *end, struct timeval *start)
49 double e = end->tv_sec * 1000000 + end->tv_usec;
50 double s = start->tv_sec * 1000000 + start->tv_usec;
55 static void getversion(int fd)
57 drmVersionPtr version;
59 version = drmGetVersion(fd);
61 printf( "Name: %s\n", version->name ? version->name : "?" );
62 printf( " Version: %d.%d.%d\n",
63 version->version_major,
64 version->version_minor,
65 version->version_patchlevel );
66 printf( " Date: %s\n", version->date ? version->date : "?" );
67 printf( " Desc: %s\n", version->desc ? version->desc : "?" );
68 drmFreeVersion(version);
70 printf( "No driver available\n" );
74 void handler(int fd, void *oldctx, void *newctx)
76 printf("Got fd %d\n", fd);
79 void process_sigio(char *device)
83 if ((fd = open(device, 0)) < 0) {
84 drmError(-errno, __FUNCTION__);
89 drmInstallSIGIOHandler(fd, handler);
93 int main(int argc, char **argv)
102 unsigned long offset;
113 while ((c = getopt(argc, argv,
114 "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
117 count = strtoul(optarg, NULL, 0);
124 case 'v': getversion(fd); break;
126 if ((r = drmCreateContext(fd, &context))) {
127 drmError(r, argv[0]);
130 printf( "Got %d\n", context);
133 process_sigio(optarg);
136 if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
137 drmError(r, argv[0]);
142 if ((r = drmSetBusid(fd,optarg))) {
143 drmError(r, argv[0]);
148 if ((fd = drmOpen(optarg, NULL)) < 0) {
149 drmError(fd, argv[0]);
154 if ((fd = drmOpen(NULL, optarg)) < 0) {
155 drmError(fd, argv[0]);
159 case 'B': /* Test buffer allocation */
160 count = strtoul(optarg, &pt, 0);
161 size = strtoul(pt+1, &pt, 0);
162 secs = strtoul(pt+1, NULL, 0);
165 int *indices, *sizes;
167 indices = alloca(sizeof(*indices) * count);
168 sizes = alloca(sizeof(*sizes) * count);
169 dma.context = context;
171 dma.request_count = count;
172 dma.request_size = size;
173 dma.request_list = indices;
174 dma.request_sizes = sizes;
175 dma.flags = DRM_DMA_WAIT;
176 if ((r = drmDMA(fd, &dma))) {
177 drmError(r, argv[0]);
180 for (i = 0; i < dma.granted_count; i++) {
181 printf("%5d: index = %d, size = %d\n",
182 i, dma.request_list[i], dma.request_sizes[i]);
185 drmFreeBufs(fd, dma.granted_count, indices);
189 count = strtoul(optarg, &pt, 0);
190 size = strtoul(pt+1, NULL, 0);
191 if ((r = drmAddBufs(fd, count, size, 0)) < 0) {
192 drmError(r, argv[0]);
195 if (!(info = drmGetBufInfo(fd))) {
196 drmError(0, argv[0]);
199 for (i = 0; i < info->count; i++) {
200 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
203 info->list[i].low_mark,
204 info->list[i].high_mark);
206 if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
207 drmError(r, argv[0]);
210 if (!(info = drmGetBufInfo(fd))) {
211 drmError(0, argv[0]);
214 for (i = 0; i < info->count; i++) {
215 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
218 info->list[i].low_mark,
219 info->list[i].high_mark);
221 printf("===== /proc/drm/1/meminfo =====\n");
222 sprintf(buf, "cat /proc/drm/1/meminfo");
225 if (!(bufs = drmMapBufs(fd))) {
226 drmError(0, argv[0]);
229 printf("===============================\n");
230 printf( "%d bufs\n", bufs->count);
231 for (i = 0; i < bufs->count; i++) {
232 printf( " %4d: %8d bytes at %p\n",
235 bufs->list[i].address);
237 printf("===== /proc/drm/1/vmainfo =====\n");
238 sprintf(buf, "cat /proc/drm/1/vmainfo");
243 offset = strtoul(optarg, &pt, 0);
244 size = strtoul(pt+1, NULL, 0);
246 if ((r = drmAddMap(fd, offset, size,
247 DRM_FRAME_BUFFER, 0, &handle))) {
248 drmError(r, argv[0]);
251 printf("0x%08lx:0x%04lx added\n", offset, size);
252 printf("===== /proc/drm/1/meminfo =====\n");
253 sprintf(buf, "cat /proc/drm/1/meminfo");
258 offset = strtoul(optarg, &pt, 0);
259 size = strtoul(pt+1, NULL, 0);
261 if ((r = drmAddMap(fd, offset, size,
263 c == 'R' ? DRM_READ_ONLY : 0,
265 drmError(r, argv[0]);
268 printf("0x%08lx:0x%04lx added\n", offset, size);
269 printf("===== /proc/drm/1/meminfo =====\n");
270 sprintf(buf, "cat /proc/drm/1/meminfo");
274 size = strtoul(optarg, &pt, 0);
276 if ((r = drmAddMap(fd, 0, size,
277 DRM_SHM, DRM_CONTAINS_LOCK,
279 drmError(r, argv[0]);
282 printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
283 sprintf(buf, "cat /proc/graphics/0/vm");
287 offset = strtoul(optarg, &pt, 0);
288 size = strtoul(pt+1, NULL, 0);
290 if ((r = drmMap(fd, offset, size, &address))) {
291 drmError(r, argv[0]);
294 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
295 offset, size, address, getpid());
296 printf("===== /proc/graphics/0/vma =====\n");
297 sprintf(buf, "cat /proc/graphics/0/vma");
299 mprotect((void *)offset, size, PROT_READ);
300 printf("===== /proc/graphics/0/vma =====\n");
301 sprintf(buf, "cat /proc/graphics/0/vma");
306 offset = strtoul(optarg, &pt, 0);
307 size = strtoul(pt+1, NULL, 0);
309 if ((r = drmMap(fd, offset, size, &address))) {
310 drmError(r, argv[0]);
313 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
314 offset, size, address, getpid());
315 printf("===== /proc/%d/maps =====\n", getpid());
316 sprintf(buf, "cat /proc/%d/maps", getpid());
318 printf("===== /proc/drm/1/meminfo =====\n");
319 sprintf(buf, "cat /proc/drm/1/meminfo");
321 printf("===== /proc/drm/1/vmainfo =====\n");
322 sprintf(buf, "cat /proc/drm/1/vmainfo");
324 printf("===== READING =====\n");
325 for (i = 0; i < 0x10; i++)
326 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
329 printf("===== WRITING =====\n");
330 for (i = 0; i < size; i+=2) {
331 ((char *)address)[i] = i & 0xff;
332 ((char *)address)[i+1] = i & 0xff;
335 printf("===== READING =====\n");
336 for (i = 0; i < 0x10; i++)
337 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
339 printf("===== /proc/drm/1/vmainfo =====\n");
340 sprintf(buf, "cat /proc/drm/1/vmainfo");
344 context = strtoul(optarg, &pt, 0);
345 offset = strtoul(pt+1, &pt, 0);
346 size = strtoul(pt+1, &pt, 0);
347 loops = strtoul(pt+1, NULL, 0);
349 if ((r = drmMap(fd, offset, size, &address))) {
350 drmError(r, argv[0]);
357 struct timeval loop_start, loop_end;
358 struct timeval lock_start, lock_end;
361 int histo[HISTOSIZE];
370 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
372 gettimeofday(&loop_start, NULL);
373 for (i = 0; i < loops; i++) {
374 gettimeofday(&lock_start, NULL);
375 DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
376 gettimeofday(&lock_end, NULL);
377 DRM_UNLOCK(fd,lock,context);
379 wt = usec(&lock_end, &lock_start);
380 if (wt <= 2.5) ++histo[8];
381 if (wt < 5.0) ++histo[0];
382 else if (wt < 50.0) ++histo[1];
383 else if (wt < 500.0) ++histo[2];
384 else if (wt < 5000.0) ++histo[3];
385 else if (wt < 50000.0) ++histo[4];
386 else if (wt < 500000.0) ++histo[5];
387 else if (wt < 5000000.0) ++histo[6];
389 if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
391 gettimeofday(&loop_end, NULL);
392 printf( "Average wait time = %.2f usec, %d fast\n",
393 usec(&loop_end, &loop_start) / counter, fast);
394 printf( "%9d <= 2.5 uS\n", histo[8]);
395 printf( "%9d < 5 uS\n", histo[0]);
396 printf( "%9d < 50 uS\n", histo[1]);
397 printf( "%9d < 500 uS\n", histo[2]);
398 printf( "%9d < 5000 uS\n", histo[3]);
399 printf( "%9d < 50000 uS\n", histo[4]);
400 printf( "%9d < 500000 uS\n", histo[5]);
401 printf( "%9d < 5000000 uS\n", histo[6]);
402 printf( "%9d >= 5000000 uS\n", histo[7]);
405 printf( "before lock: 0x%08x\n", lock->lock);
406 printf( "lock: 0x%08x\n", lock->lock);
408 printf( "unlock: 0x%08x\n", lock->lock);
412 fprintf( stderr, "Usage: drmstat [options]\n" );