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 void handler(int fd, void *oldctx, void *newctx)
87 printf("Got fd %d\n", fd);
90 void process_sigio(char *device)
94 if ((fd = open(device, 0)) < 0) {
95 drmError(-errno, __FUNCTION__);
100 /* drmInstallSIGIOHandler(fd, handler); */
104 int main(int argc, char **argv)
113 unsigned long offset;
115 drm_context_t context;
124 while ((c = getopt(argc, argv,
125 "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
128 count = strtoul(optarg, NULL, 0);
135 case 'v': getversion(fd); break;
137 if ((r = drmCreateContext(fd, &context))) {
138 drmError(r, argv[0]);
141 printf( "Got %d\n", context);
144 process_sigio(optarg);
147 if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
148 drmError(r, argv[0]);
153 if ((r = drmSetBusid(fd,optarg))) {
154 drmError(r, argv[0]);
159 if ((fd = drmOpen(optarg, NULL)) < 0) {
160 drmError(fd, argv[0]);
165 if ((fd = drmOpen(NULL, optarg)) < 0) {
166 drmError(fd, argv[0]);
170 case 'B': /* Test buffer allocation */
171 count = strtoul(optarg, &pt, 0);
172 size = strtoul(pt+1, &pt, 0);
173 secs = strtoul(pt+1, NULL, 0);
176 int *indices, *sizes;
178 indices = alloca(sizeof(*indices) * count);
179 sizes = alloca(sizeof(*sizes) * count);
180 dma.context = context;
182 dma.request_count = count;
183 dma.request_size = size;
184 dma.request_list = indices;
185 dma.request_sizes = sizes;
186 dma.flags = DRM_DMA_WAIT;
187 if ((r = drmDMA(fd, &dma))) {
188 drmError(r, argv[0]);
191 for (i = 0; i < dma.granted_count; i++) {
192 printf("%5d: index = %d, size = %d\n",
193 i, dma.request_list[i], dma.request_sizes[i]);
196 drmFreeBufs(fd, dma.granted_count, indices);
200 count = strtoul(optarg, &pt, 0);
201 size = strtoul(pt+1, NULL, 0);
202 if ((r = drmAddBufs(fd, count, size, 0, 65536)) < 0) {
203 drmError(r, argv[0]);
206 if (!(info = drmGetBufInfo(fd))) {
207 drmError(0, argv[0]);
210 for (i = 0; i < info->count; i++) {
211 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
214 info->list[i].low_mark,
215 info->list[i].high_mark);
217 if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
218 drmError(r, argv[0]);
221 if (!(info = drmGetBufInfo(fd))) {
222 drmError(0, argv[0]);
225 for (i = 0; i < info->count; i++) {
226 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
229 info->list[i].low_mark,
230 info->list[i].high_mark);
232 printf("===== /proc/dri/0/mem =====\n");
233 sprintf(buf, "cat /proc/dri/0/mem");
236 if (!(bufs = drmMapBufs(fd))) {
237 drmError(0, argv[0]);
240 printf("===============================\n");
241 printf( "%d bufs\n", bufs->count);
242 for (i = 0; i < bufs->count; i++) {
243 printf( " %4d: %8d bytes at %p\n",
246 bufs->list[i].address);
248 printf("===== /proc/dri/0/vma =====\n");
249 sprintf(buf, "cat /proc/dri/0/vma");
254 offset = strtoul(optarg, &pt, 0);
255 size = strtoul(pt+1, NULL, 0);
257 if ((r = drmAddMap(fd, offset, size,
258 DRM_FRAME_BUFFER, 0, &handle))) {
259 drmError(r, argv[0]);
262 printf("0x%08lx:0x%04lx added\n", offset, size);
263 printf("===== /proc/dri/0/mem =====\n");
264 sprintf(buf, "cat /proc/dri/0/mem");
269 offset = strtoul(optarg, &pt, 0);
270 size = strtoul(pt+1, NULL, 0);
272 if ((r = drmAddMap(fd, offset, size,
274 c == 'R' ? DRM_READ_ONLY : 0,
276 drmError(r, argv[0]);
279 printf("0x%08lx:0x%04lx added\n", offset, size);
280 printf("===== /proc/dri/0/mem =====\n");
281 sprintf(buf, "cat /proc/dri/0/mem");
285 size = strtoul(optarg, &pt, 0);
287 if ((r = drmAddMap(fd, 0, size,
288 DRM_SHM, DRM_CONTAINS_LOCK,
290 drmError(r, argv[0]);
293 printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
294 sprintf(buf, "cat /proc/dri/0/vm");
298 offset = strtoul(optarg, &pt, 0);
299 size = strtoul(pt+1, NULL, 0);
301 if ((r = drmMap(fd, offset, size, &address))) {
302 drmError(r, argv[0]);
305 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
306 offset, size, address, getpid());
307 printf("===== /proc/dri/0/vma =====\n");
308 sprintf(buf, "cat /proc/dri/0/vma");
310 mprotect((void *)offset, size, PROT_READ);
311 printf("===== /proc/dri/0/vma =====\n");
312 sprintf(buf, "cat /proc/dri/0/vma");
317 offset = strtoul(optarg, &pt, 0);
318 size = strtoul(pt+1, NULL, 0);
320 if ((r = drmMap(fd, offset, size, &address))) {
321 drmError(r, argv[0]);
324 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
325 offset, size, address, getpid());
326 printf("===== /proc/%d/maps =====\n", getpid());
327 sprintf(buf, "cat /proc/%d/maps", getpid());
329 printf("===== /proc/dri/0/mem =====\n");
330 sprintf(buf, "cat /proc/dri/0/mem");
332 printf("===== /proc/dri/0/vma =====\n");
333 sprintf(buf, "cat /proc/dri/0/vma");
335 printf("===== READING =====\n");
336 for (i = 0; i < 0x10; i++)
337 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
340 printf("===== WRITING =====\n");
341 for (i = 0; i < size; i+=2) {
342 ((char *)address)[i] = i & 0xff;
343 ((char *)address)[i+1] = i & 0xff;
346 printf("===== READING =====\n");
347 for (i = 0; i < 0x10; i++)
348 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
350 printf("===== /proc/dri/0/vma =====\n");
351 sprintf(buf, "cat /proc/dri/0/vma");
355 context = strtoul(optarg, &pt, 0);
356 offset = strtoul(pt+1, &pt, 0);
357 size = strtoul(pt+1, &pt, 0);
358 loops = strtoul(pt+1, NULL, 0);
360 if ((r = drmMap(fd, offset, size, &address))) {
361 drmError(r, argv[0]);
368 struct timeval loop_start, loop_end;
369 struct timeval lock_start, lock_end;
372 int histo[HISTOSIZE];
381 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
383 gettimeofday(&loop_start, NULL);
384 for (i = 0; i < loops; i++) {
385 gettimeofday(&lock_start, NULL);
386 DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
387 gettimeofday(&lock_end, NULL);
388 DRM_UNLOCK(fd,lock,context);
390 wt = usec(&lock_end, &lock_start);
391 if (wt <= 2.5) ++histo[8];
392 if (wt < 5.0) ++histo[0];
393 else if (wt < 50.0) ++histo[1];
394 else if (wt < 500.0) ++histo[2];
395 else if (wt < 5000.0) ++histo[3];
396 else if (wt < 50000.0) ++histo[4];
397 else if (wt < 500000.0) ++histo[5];
398 else if (wt < 5000000.0) ++histo[6];
400 if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
402 gettimeofday(&loop_end, NULL);
403 printf( "Average wait time = %.2f usec, %d fast\n",
404 usec(&loop_end, &loop_start) / counter, fast);
405 printf( "%9d <= 2.5 uS\n", histo[8]);
406 printf( "%9d < 5 uS\n", histo[0]);
407 printf( "%9d < 50 uS\n", histo[1]);
408 printf( "%9d < 500 uS\n", histo[2]);
409 printf( "%9d < 5000 uS\n", histo[3]);
410 printf( "%9d < 50000 uS\n", histo[4]);
411 printf( "%9d < 500000 uS\n", histo[5]);
412 printf( "%9d < 5000000 uS\n", histo[6]);
413 printf( "%9d >= 5000000 uS\n", histo[7]);
416 printf( "before lock: 0x%08x\n", lock->lock);
417 printf( "lock: 0x%08x\n", lock->lock);
419 printf( "unlock: 0x%08x\n", lock->lock);
423 fprintf( stderr, "Usage: drmstat [options]\n" );
430 void DRM_PRINTFLIKE(4, 0)
431 xf86VDrvMsgVerb(int scrnIndex, int type, int verb, const char *format,
434 vfprintf(stderr, format, args);
437 int xf86ConfigDRI[10];