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>
49 /* Support gcc's __FUNCTION__ for people using other compilers */
50 #if !defined(__GNUC__) && !defined(__FUNCTION__)
51 # define __FUNCTION__ __func__ /* C99 */
56 static double usec(struct timeval *end, struct timeval *start)
58 double e = end->tv_sec * 1000000 + end->tv_usec;
59 double s = start->tv_sec * 1000000 + start->tv_usec;
64 static void getversion(int fd)
66 drmVersionPtr version;
68 version = drmGetVersion(fd);
70 printf( "Name: %s\n", version->name ? version->name : "?" );
71 printf( " Version: %d.%d.%d\n",
72 version->version_major,
73 version->version_minor,
74 version->version_patchlevel );
75 printf( " Date: %s\n", version->date ? version->date : "?" );
76 printf( " Desc: %s\n", version->desc ? version->desc : "?" );
77 drmFreeVersion(version);
79 printf( "No driver available\n" );
83 void handler(int fd, void *oldctx, void *newctx)
85 printf("Got fd %d\n", fd);
88 void process_sigio(char *device)
92 if ((fd = open(device, 0)) < 0) {
93 drmError(-errno, __FUNCTION__);
98 /* drmInstallSIGIOHandler(fd, handler); */
102 int main(int argc, char **argv)
111 unsigned long offset;
113 drm_context_t context;
122 while ((c = getopt(argc, argv,
123 "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
126 count = strtoul(optarg, NULL, 0);
133 case 'v': getversion(fd); break;
135 if ((r = drmCreateContext(fd, &context))) {
136 drmError(r, argv[0]);
139 printf( "Got %d\n", context);
142 process_sigio(optarg);
145 if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
146 drmError(r, argv[0]);
151 if ((r = drmSetBusid(fd,optarg))) {
152 drmError(r, argv[0]);
157 if ((fd = drmOpen(optarg, NULL)) < 0) {
158 drmError(fd, argv[0]);
163 if ((fd = drmOpen(NULL, optarg)) < 0) {
164 drmError(fd, argv[0]);
168 case 'B': /* Test buffer allocation */
169 count = strtoul(optarg, &pt, 0);
170 size = strtoul(pt+1, &pt, 0);
171 secs = strtoul(pt+1, NULL, 0);
174 int *indices, *sizes;
176 indices = alloca(sizeof(*indices) * count);
177 sizes = alloca(sizeof(*sizes) * count);
178 dma.context = context;
180 dma.request_count = count;
181 dma.request_size = size;
182 dma.request_list = indices;
183 dma.request_sizes = sizes;
184 dma.flags = DRM_DMA_WAIT;
185 if ((r = drmDMA(fd, &dma))) {
186 drmError(r, argv[0]);
189 for (i = 0; i < dma.granted_count; i++) {
190 printf("%5d: index = %d, size = %d\n",
191 i, dma.request_list[i], dma.request_sizes[i]);
194 drmFreeBufs(fd, dma.granted_count, indices);
198 count = strtoul(optarg, &pt, 0);
199 size = strtoul(pt+1, NULL, 0);
200 if ((r = drmAddBufs(fd, count, size, 0, 65536)) < 0) {
201 drmError(r, argv[0]);
204 if (!(info = drmGetBufInfo(fd))) {
205 drmError(0, argv[0]);
208 for (i = 0; i < info->count; i++) {
209 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
212 info->list[i].low_mark,
213 info->list[i].high_mark);
215 if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
216 drmError(r, argv[0]);
219 if (!(info = drmGetBufInfo(fd))) {
220 drmError(0, argv[0]);
223 for (i = 0; i < info->count; i++) {
224 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
227 info->list[i].low_mark,
228 info->list[i].high_mark);
230 printf("===== /proc/dri/0/mem =====\n");
231 sprintf(buf, "cat /proc/dri/0/mem");
234 if (!(bufs = drmMapBufs(fd))) {
235 drmError(0, argv[0]);
238 printf("===============================\n");
239 printf( "%d bufs\n", bufs->count);
240 for (i = 0; i < bufs->count; i++) {
241 printf( " %4d: %8d bytes at %p\n",
244 bufs->list[i].address);
246 printf("===== /proc/dri/0/vma =====\n");
247 sprintf(buf, "cat /proc/dri/0/vma");
252 offset = strtoul(optarg, &pt, 0);
253 size = strtoul(pt+1, NULL, 0);
255 if ((r = drmAddMap(fd, offset, size,
256 DRM_FRAME_BUFFER, 0, &handle))) {
257 drmError(r, argv[0]);
260 printf("0x%08lx:0x%04lx added\n", offset, size);
261 printf("===== /proc/dri/0/mem =====\n");
262 sprintf(buf, "cat /proc/dri/0/mem");
267 offset = strtoul(optarg, &pt, 0);
268 size = strtoul(pt+1, NULL, 0);
270 if ((r = drmAddMap(fd, offset, size,
272 c == 'R' ? DRM_READ_ONLY : 0,
274 drmError(r, argv[0]);
277 printf("0x%08lx:0x%04lx added\n", offset, size);
278 printf("===== /proc/dri/0/mem =====\n");
279 sprintf(buf, "cat /proc/dri/0/mem");
283 size = strtoul(optarg, &pt, 0);
285 if ((r = drmAddMap(fd, 0, size,
286 DRM_SHM, DRM_CONTAINS_LOCK,
288 drmError(r, argv[0]);
291 printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
292 sprintf(buf, "cat /proc/dri/0/vm");
296 offset = strtoul(optarg, &pt, 0);
297 size = strtoul(pt+1, NULL, 0);
299 if ((r = drmMap(fd, offset, size, &address))) {
300 drmError(r, argv[0]);
303 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
304 offset, size, address, getpid());
305 printf("===== /proc/dri/0/vma =====\n");
306 sprintf(buf, "cat /proc/dri/0/vma");
308 mprotect((void *)offset, size, PROT_READ);
309 printf("===== /proc/dri/0/vma =====\n");
310 sprintf(buf, "cat /proc/dri/0/vma");
315 offset = strtoul(optarg, &pt, 0);
316 size = strtoul(pt+1, NULL, 0);
318 if ((r = drmMap(fd, offset, size, &address))) {
319 drmError(r, argv[0]);
322 printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
323 offset, size, address, getpid());
324 printf("===== /proc/%d/maps =====\n", getpid());
325 sprintf(buf, "cat /proc/%d/maps", getpid());
327 printf("===== /proc/dri/0/mem =====\n");
328 sprintf(buf, "cat /proc/dri/0/mem");
330 printf("===== /proc/dri/0/vma =====\n");
331 sprintf(buf, "cat /proc/dri/0/vma");
333 printf("===== READING =====\n");
334 for (i = 0; i < 0x10; i++)
335 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
338 printf("===== WRITING =====\n");
339 for (i = 0; i < size; i+=2) {
340 ((char *)address)[i] = i & 0xff;
341 ((char *)address)[i+1] = i & 0xff;
344 printf("===== READING =====\n");
345 for (i = 0; i < 0x10; i++)
346 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
348 printf("===== /proc/dri/0/vma =====\n");
349 sprintf(buf, "cat /proc/dri/0/vma");
353 context = strtoul(optarg, &pt, 0);
354 offset = strtoul(pt+1, &pt, 0);
355 size = strtoul(pt+1, &pt, 0);
356 loops = strtoul(pt+1, NULL, 0);
358 if ((r = drmMap(fd, offset, size, &address))) {
359 drmError(r, argv[0]);
366 struct timeval loop_start, loop_end;
367 struct timeval lock_start, lock_end;
370 int histo[HISTOSIZE];
379 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
381 gettimeofday(&loop_start, NULL);
382 for (i = 0; i < loops; i++) {
383 gettimeofday(&lock_start, NULL);
384 DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
385 gettimeofday(&lock_end, NULL);
386 DRM_UNLOCK(fd,lock,context);
388 wt = usec(&lock_end, &lock_start);
389 if (wt <= 2.5) ++histo[8];
390 if (wt < 5.0) ++histo[0];
391 else if (wt < 50.0) ++histo[1];
392 else if (wt < 500.0) ++histo[2];
393 else if (wt < 5000.0) ++histo[3];
394 else if (wt < 50000.0) ++histo[4];
395 else if (wt < 500000.0) ++histo[5];
396 else if (wt < 5000000.0) ++histo[6];
398 if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
400 gettimeofday(&loop_end, NULL);
401 printf( "Average wait time = %.2f usec, %d fast\n",
402 usec(&loop_end, &loop_start) / counter, fast);
403 printf( "%9d <= 2.5 uS\n", histo[8]);
404 printf( "%9d < 5 uS\n", histo[0]);
405 printf( "%9d < 50 uS\n", histo[1]);
406 printf( "%9d < 500 uS\n", histo[2]);
407 printf( "%9d < 5000 uS\n", histo[3]);
408 printf( "%9d < 50000 uS\n", histo[4]);
409 printf( "%9d < 500000 uS\n", histo[5]);
410 printf( "%9d < 5000000 uS\n", histo[6]);
411 printf( "%9d >= 5000000 uS\n", histo[7]);
414 printf( "before lock: 0x%08x\n", lock->lock);
415 printf( "lock: 0x%08x\n", lock->lock);
417 printf( "unlock: 0x%08x\n", lock->lock);
421 fprintf( stderr, "Usage: drmstat [options]\n" );
428 void DRM_PRINTFLIKE(4, 0)
429 xf86VDrvMsgVerb(int scrnIndex, int type, int verb, const char *format,
432 vfprintf(stderr, format, args);
435 int xf86ConfigDRI[10];