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>
38 #include <sys/types.h>
51 /* Support gcc's __FUNCTION__ for people using other compilers */
52 #if !defined(__GNUC__) && !defined(__FUNCTION__)
53 # define __FUNCTION__ __func__ /* C99 */
58 static double usec(struct timeval *end, struct timeval *start)
60 double e = end->tv_sec * 1000000 + end->tv_usec;
61 double s = start->tv_sec * 1000000 + start->tv_usec;
66 static void getversion(int fd)
68 drmVersionPtr version;
70 version = drmGetVersion(fd);
72 printf( "Name: %s\n", version->name ? version->name : "?" );
73 printf( " Version: %d.%d.%d\n",
74 version->version_major,
75 version->version_minor,
76 version->version_patchlevel );
77 printf( " Date: %s\n", version->date ? version->date : "?" );
78 printf( " Desc: %s\n", version->desc ? version->desc : "?" );
79 drmFreeVersion(version);
81 printf( "No driver available\n" );
85 static void process_sigio(char *device)
89 if ((fd = open(device, 0)) < 0) {
90 drmError(-errno, __FUNCTION__);
98 int main(int argc, char **argv)
107 unsigned long offset;
109 drm_context_t context;
118 while ((c = getopt(argc, argv,
119 "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
122 count = strtoul(optarg, NULL, 0);
129 case 'v': getversion(fd); break;
131 if ((r = drmCreateContext(fd, &context))) {
132 drmError(r, argv[0]);
135 printf( "Got %d\n", context);
138 process_sigio(optarg);
141 if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
142 drmError(r, argv[0]);
147 if ((r = drmSetBusid(fd,optarg))) {
148 drmError(r, argv[0]);
153 if ((fd = drmOpen(optarg, NULL)) < 0) {
154 drmError(fd, argv[0]);
159 if ((fd = drmOpen(NULL, optarg)) < 0) {
160 drmError(fd, argv[0]);
164 case 'B': /* Test buffer allocation */
165 count = strtoul(optarg, &pt, 0);
166 size = strtoul(pt+1, &pt, 0);
167 secs = strtoul(pt+1, NULL, 0);
170 int *indices, *sizes;
172 indices = alloca(sizeof(*indices) * count);
173 sizes = alloca(sizeof(*sizes) * count);
174 dma.context = context;
176 dma.request_count = count;
177 dma.request_size = size;
178 dma.request_list = indices;
179 dma.request_sizes = sizes;
180 dma.flags = DRM_DMA_WAIT;
181 if ((r = drmDMA(fd, &dma))) {
182 drmError(r, argv[0]);
185 for (i = 0; i < dma.granted_count; i++) {
186 printf("%5d: index = %d, size = %d\n",
187 i, dma.request_list[i], dma.request_sizes[i]);
190 drmFreeBufs(fd, dma.granted_count, indices);
194 count = strtoul(optarg, &pt, 0);
195 size = strtoul(pt+1, NULL, 0);
196 if ((r = drmAddBufs(fd, count, size, 0, 65536)) < 0) {
197 drmError(r, argv[0]);
200 if (!(info = drmGetBufInfo(fd))) {
201 drmError(0, argv[0]);
204 for (i = 0; i < info->count; i++) {
205 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
208 info->list[i].low_mark,
209 info->list[i].high_mark);
211 if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
212 drmError(r, argv[0]);
215 if (!(info = drmGetBufInfo(fd))) {
216 drmError(0, argv[0]);
219 for (i = 0; i < info->count; i++) {
220 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
223 info->list[i].low_mark,
224 info->list[i].high_mark);
226 printf("===== /proc/dri/0/mem =====\n");
227 sprintf(buf, "cat /proc/dri/0/mem");
230 if (!(bufs = drmMapBufs(fd))) {
231 drmError(0, argv[0]);
234 printf("===============================\n");
235 printf( "%d bufs\n", bufs->count);
236 for (i = 0; i < bufs->count; i++) {
237 printf( " %4d: %8d bytes at %p\n",
240 bufs->list[i].address);
242 printf("===== /proc/dri/0/vma =====\n");
243 sprintf(buf, "cat /proc/dri/0/vma");
248 offset = strtoul(optarg, &pt, 0);
249 size = strtoul(pt+1, NULL, 0);
251 if ((r = drmAddMap(fd, offset, size,
252 DRM_FRAME_BUFFER, 0, &handle))) {
253 drmError(r, argv[0]);
256 printf("0x%08lx:0x%04lx added\n", offset, size);
257 printf("===== /proc/dri/0/mem =====\n");
258 sprintf(buf, "cat /proc/dri/0/mem");
263 offset = strtoul(optarg, &pt, 0);
264 size = strtoul(pt+1, NULL, 0);
266 if ((r = drmAddMap(fd, offset, size,
268 c == 'R' ? DRM_READ_ONLY : 0,
270 drmError(r, argv[0]);
273 printf("0x%08lx:0x%04lx added\n", offset, size);
274 printf("===== /proc/dri/0/mem =====\n");
275 sprintf(buf, "cat /proc/dri/0/mem");
279 size = strtoul(optarg, &pt, 0);
281 if ((r = drmAddMap(fd, 0, size,
282 DRM_SHM, DRM_CONTAINS_LOCK,
284 drmError(r, argv[0]);
287 printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
288 sprintf(buf, "cat /proc/dri/0/vm");
292 offset = strtoul(optarg, &pt, 0);
293 size = strtoul(pt+1, NULL, 0);
295 if ((r = drmMap(fd, offset, size, &address))) {
296 drmError(r, argv[0]);
299 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
300 offset, size, address, getpid());
301 printf("===== /proc/dri/0/vma =====\n");
302 sprintf(buf, "cat /proc/dri/0/vma");
304 mprotect((void *)offset, size, PROT_READ);
305 printf("===== /proc/dri/0/vma =====\n");
306 sprintf(buf, "cat /proc/dri/0/vma");
311 offset = strtoul(optarg, &pt, 0);
312 size = strtoul(pt+1, NULL, 0);
314 if ((r = drmMap(fd, offset, size, &address))) {
315 drmError(r, argv[0]);
318 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
319 offset, size, address, getpid());
320 printf("===== /proc/%d/maps =====\n", getpid());
321 sprintf(buf, "cat /proc/%d/maps", getpid());
323 printf("===== /proc/dri/0/mem =====\n");
324 sprintf(buf, "cat /proc/dri/0/mem");
326 printf("===== /proc/dri/0/vma =====\n");
327 sprintf(buf, "cat /proc/dri/0/vma");
329 printf("===== READING =====\n");
330 for (i = 0; i < 0x10; i++)
331 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
334 printf("===== WRITING =====\n");
335 for (i = 0; i < size; i+=2) {
336 ((char *)address)[i] = i & 0xff;
337 ((char *)address)[i+1] = i & 0xff;
340 printf("===== READING =====\n");
341 for (i = 0; i < 0x10; i++)
342 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
344 printf("===== /proc/dri/0/vma =====\n");
345 sprintf(buf, "cat /proc/dri/0/vma");
349 context = strtoul(optarg, &pt, 0);
350 offset = strtoul(pt+1, &pt, 0);
351 size = strtoul(pt+1, &pt, 0);
352 loops = strtoul(pt+1, NULL, 0);
354 if ((r = drmMap(fd, offset, size, &address))) {
355 drmError(r, argv[0]);
362 struct timeval loop_start, loop_end;
363 struct timeval lock_start, lock_end;
366 int histo[HISTOSIZE];
375 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
377 gettimeofday(&loop_start, NULL);
378 for (i = 0; i < loops; i++) {
379 gettimeofday(&lock_start, NULL);
380 DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
381 gettimeofday(&lock_end, NULL);
382 DRM_UNLOCK(fd,lock,context);
384 wt = usec(&lock_end, &lock_start);
385 if (wt <= 2.5) ++histo[8];
386 if (wt < 5.0) ++histo[0];
387 else if (wt < 50.0) ++histo[1];
388 else if (wt < 500.0) ++histo[2];
389 else if (wt < 5000.0) ++histo[3];
390 else if (wt < 50000.0) ++histo[4];
391 else if (wt < 500000.0) ++histo[5];
392 else if (wt < 5000000.0) ++histo[6];
394 if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
396 gettimeofday(&loop_end, NULL);
397 printf( "Average wait time = %.2f usec, %d fast\n",
398 usec(&loop_end, &loop_start) / counter, fast);
399 printf( "%9d <= 2.5 uS\n", histo[8]);
400 printf( "%9d < 5 uS\n", histo[0]);
401 printf( "%9d < 50 uS\n", histo[1]);
402 printf( "%9d < 500 uS\n", histo[2]);
403 printf( "%9d < 5000 uS\n", histo[3]);
404 printf( "%9d < 50000 uS\n", histo[4]);
405 printf( "%9d < 500000 uS\n", histo[5]);
406 printf( "%9d < 5000000 uS\n", histo[6]);
407 printf( "%9d >= 5000000 uS\n", histo[7]);
410 printf( "before lock: 0x%08x\n", lock->lock);
411 printf( "lock: 0x%08x\n", lock->lock);
413 printf( "unlock: 0x%08x\n", lock->lock);
417 fprintf( stderr, "Usage: drmstat [options]\n" );
424 int xf86ConfigDRI[10];