1 /* drmstat.c -- DRM device status and testing program
2 * Created: Tue Jan 5 08:19:24 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
36 #include <sys/types.h>
51 static double usec(struct timeval *end, struct timeval *start)
53 double e = end->tv_sec * 1000000 + end->tv_usec;
54 double s = start->tv_sec * 1000000 + start->tv_usec;
59 static void getversion(int fd)
61 drmVersionPtr version;
63 version = drmGetVersion(fd);
65 printf( "Name: %s\n", version->name ? version->name : "?" );
66 printf( " Version: %d.%d.%d\n",
67 version->version_major,
68 version->version_minor,
69 version->version_patchlevel );
70 printf( " Date: %s\n", version->date ? version->date : "?" );
71 printf( " Desc: %s\n", version->desc ? version->desc : "?" );
72 drmFreeVersion(version);
74 printf( "No driver available\n" );
78 void handler(int fd, void *oldctx, void *newctx)
80 printf("Got fd %d\n", fd);
83 void process_sigio(char *device)
87 if ((fd = open(device, 0)) < 0) {
88 drmError(-errno, __FUNCTION__);
93 /* drmInstallSIGIOHandler(fd, handler); */
97 int main(int argc, char **argv)
106 unsigned long offset;
108 drm_context_t context;
117 while ((c = getopt(argc, argv,
118 "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
121 count = strtoul(optarg, NULL, 0);
128 case 'v': getversion(fd); break;
130 if ((r = drmCreateContext(fd, &context))) {
131 drmError(r, argv[0]);
134 printf( "Got %d\n", context);
137 process_sigio(optarg);
140 if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
141 drmError(r, argv[0]);
146 if ((r = drmSetBusid(fd,optarg))) {
147 drmError(r, argv[0]);
152 if ((fd = drmOpen(optarg, NULL)) < 0) {
153 drmError(fd, argv[0]);
158 if ((fd = drmOpen(NULL, optarg)) < 0) {
159 drmError(fd, argv[0]);
163 case 'B': /* Test buffer allocation */
164 count = strtoul(optarg, &pt, 0);
165 size = strtoul(pt+1, &pt, 0);
166 secs = strtoul(pt+1, NULL, 0);
169 int *indices, *sizes;
171 indices = alloca(sizeof(*indices) * count);
172 sizes = alloca(sizeof(*sizes) * count);
173 dma.context = context;
175 dma.request_count = count;
176 dma.request_size = size;
177 dma.request_list = indices;
178 dma.request_sizes = sizes;
179 dma.flags = DRM_DMA_WAIT;
180 if ((r = drmDMA(fd, &dma))) {
181 drmError(r, argv[0]);
184 for (i = 0; i < dma.granted_count; i++) {
185 printf("%5d: index = %d, size = %d\n",
186 i, dma.request_list[i], dma.request_sizes[i]);
189 drmFreeBufs(fd, dma.granted_count, indices);
193 count = strtoul(optarg, &pt, 0);
194 size = strtoul(pt+1, NULL, 0);
195 if ((r = drmAddBufs(fd, count, size, 0, 65536)) < 0) {
196 drmError(r, argv[0]);
199 if (!(info = drmGetBufInfo(fd))) {
200 drmError(0, argv[0]);
203 for (i = 0; i < info->count; i++) {
204 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
207 info->list[i].low_mark,
208 info->list[i].high_mark);
210 if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
211 drmError(r, argv[0]);
214 if (!(info = drmGetBufInfo(fd))) {
215 drmError(0, argv[0]);
218 for (i = 0; i < info->count; i++) {
219 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
222 info->list[i].low_mark,
223 info->list[i].high_mark);
225 printf("===== /proc/dri/0/mem =====\n");
226 sprintf(buf, "cat /proc/dri/0/mem");
229 if (!(bufs = drmMapBufs(fd))) {
230 drmError(0, argv[0]);
233 printf("===============================\n");
234 printf( "%d bufs\n", bufs->count);
235 for (i = 0; i < bufs->count; i++) {
236 printf( " %4d: %8d bytes at %p\n",
239 bufs->list[i].address);
241 printf("===== /proc/dri/0/vma =====\n");
242 sprintf(buf, "cat /proc/dri/0/vma");
247 offset = strtoul(optarg, &pt, 0);
248 size = strtoul(pt+1, NULL, 0);
250 if ((r = drmAddMap(fd, offset, size,
251 DRM_FRAME_BUFFER, 0, &handle))) {
252 drmError(r, argv[0]);
255 printf("0x%08lx:0x%04lx added\n", offset, size);
256 printf("===== /proc/dri/0/mem =====\n");
257 sprintf(buf, "cat /proc/dri/0/mem");
262 offset = strtoul(optarg, &pt, 0);
263 size = strtoul(pt+1, NULL, 0);
265 if ((r = drmAddMap(fd, offset, size,
267 c == 'R' ? DRM_READ_ONLY : 0,
269 drmError(r, argv[0]);
272 printf("0x%08lx:0x%04lx added\n", offset, size);
273 printf("===== /proc/dri/0/mem =====\n");
274 sprintf(buf, "cat /proc/dri/0/mem");
278 size = strtoul(optarg, &pt, 0);
280 if ((r = drmAddMap(fd, 0, size,
281 DRM_SHM, DRM_CONTAINS_LOCK,
283 drmError(r, argv[0]);
286 printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
287 sprintf(buf, "cat /proc/dri/0/vm");
291 offset = strtoul(optarg, &pt, 0);
292 size = strtoul(pt+1, NULL, 0);
294 if ((r = drmMap(fd, offset, size, &address))) {
295 drmError(r, argv[0]);
298 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
299 offset, size, address, getpid());
300 printf("===== /proc/dri/0/vma =====\n");
301 sprintf(buf, "cat /proc/dri/0/vma");
303 mprotect((void *)offset, size, PROT_READ);
304 printf("===== /proc/dri/0/vma =====\n");
305 sprintf(buf, "cat /proc/dri/0/vma");
310 offset = strtoul(optarg, &pt, 0);
311 size = strtoul(pt+1, NULL, 0);
313 if ((r = drmMap(fd, offset, size, &address))) {
314 drmError(r, argv[0]);
317 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
318 offset, size, address, getpid());
319 printf("===== /proc/%d/maps =====\n", getpid());
320 sprintf(buf, "cat /proc/%d/maps", getpid());
322 printf("===== /proc/dri/0/mem =====\n");
323 sprintf(buf, "cat /proc/dri/0/mem");
325 printf("===== /proc/dri/0/vma =====\n");
326 sprintf(buf, "cat /proc/dri/0/vma");
328 printf("===== READING =====\n");
329 for (i = 0; i < 0x10; i++)
330 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
333 printf("===== WRITING =====\n");
334 for (i = 0; i < size; i+=2) {
335 ((char *)address)[i] = i & 0xff;
336 ((char *)address)[i+1] = i & 0xff;
339 printf("===== READING =====\n");
340 for (i = 0; i < 0x10; i++)
341 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
343 printf("===== /proc/dri/0/vma =====\n");
344 sprintf(buf, "cat /proc/dri/0/vma");
348 context = strtoul(optarg, &pt, 0);
349 offset = strtoul(pt+1, &pt, 0);
350 size = strtoul(pt+1, &pt, 0);
351 loops = strtoul(pt+1, NULL, 0);
353 if ((r = drmMap(fd, offset, size, &address))) {
354 drmError(r, argv[0]);
361 struct timeval loop_start, loop_end;
362 struct timeval lock_start, lock_end;
365 int histo[HISTOSIZE];
374 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
376 gettimeofday(&loop_start, NULL);
377 for (i = 0; i < loops; i++) {
378 gettimeofday(&lock_start, NULL);
379 DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
380 gettimeofday(&lock_end, NULL);
381 DRM_UNLOCK(fd,lock,context);
383 wt = usec(&lock_end, &lock_start);
384 if (wt <= 2.5) ++histo[8];
385 if (wt < 5.0) ++histo[0];
386 else if (wt < 50.0) ++histo[1];
387 else if (wt < 500.0) ++histo[2];
388 else if (wt < 5000.0) ++histo[3];
389 else if (wt < 50000.0) ++histo[4];
390 else if (wt < 500000.0) ++histo[5];
391 else if (wt < 5000000.0) ++histo[6];
393 if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
395 gettimeofday(&loop_end, NULL);
396 printf( "Average wait time = %.2f usec, %d fast\n",
397 usec(&loop_end, &loop_start) / counter, fast);
398 printf( "%9d <= 2.5 uS\n", histo[8]);
399 printf( "%9d < 5 uS\n", histo[0]);
400 printf( "%9d < 50 uS\n", histo[1]);
401 printf( "%9d < 500 uS\n", histo[2]);
402 printf( "%9d < 5000 uS\n", histo[3]);
403 printf( "%9d < 50000 uS\n", histo[4]);
404 printf( "%9d < 500000 uS\n", histo[5]);
405 printf( "%9d < 5000000 uS\n", histo[6]);
406 printf( "%9d >= 5000000 uS\n", histo[7]);
409 printf( "before lock: 0x%08x\n", lock->lock);
410 printf( "lock: 0x%08x\n", lock->lock);
412 printf( "unlock: 0x%08x\n", lock->lock);
416 fprintf( stderr, "Usage: drmstat [options]\n" );
424 xf86VDrvMsgVerb(int scrnIndex, int type, int verb, const char *format,
427 vfprintf(stderr, format, args);
430 int xf86ConfigDRI[10];