1 /* drmstat.c -- DRM device status and testing program
2 * Created: Tue Jan 5 08:19:24 1999 by faith@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
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:
14 *
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
17 * Software.
18 *
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.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 *
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/mman.h>
41 #include <getopt.h>
42 #include <strings.h>
43 #include <errno.h>
44 #include <signal.h>
45 #include <fcntl.h>
46 #ifdef HAVE_ALLOCA_H
47 # include <alloca.h>
48 #endif
49 #include "xf86drm.h"
50
51 /* Support gcc's __FUNCTION__ for people using other compilers */
52 #if !defined(__GNUC__) && !defined(__FUNCTION__)
53 # define __FUNCTION__ __func__ /* C99 */
54 #endif
55
56 int sigio_fd;
57
usec(struct timeval * end,struct timeval * start)58 static double usec(struct timeval *end, struct timeval *start)
59 {
60 double e = end->tv_sec * 1000000 + end->tv_usec;
61 double s = start->tv_sec * 1000000 + start->tv_usec;
62
63 return e - s;
64 }
65
getversion(int fd)66 static void getversion(int fd)
67 {
68 drmVersionPtr version;
69
70 version = drmGetVersion(fd);
71 if (version) {
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);
80 } else {
81 printf( "No driver available\n" );
82 }
83 }
84
handler(int fd,void * oldctx,void * newctx)85 void handler(int fd, void *oldctx, void *newctx)
86 {
87 printf("Got fd %d\n", fd);
88 }
89
process_sigio(char * device)90 void process_sigio(char *device)
91 {
92 int fd;
93
94 if ((fd = open(device, 0)) < 0) {
95 drmError(-errno, __FUNCTION__);
96 exit(1);
97 }
98
99 sigio_fd = fd;
100 /* drmInstallSIGIOHandler(fd, handler); */
101 for (;;) sleep(60);
102 }
103
main(int argc,char ** argv)104 int main(int argc, char **argv)
105 {
106 int c;
107 int r = 0;
108 int fd = -1;
109 drm_handle_t handle;
110 void *address;
111 char *pt;
112 unsigned long count;
113 unsigned long offset;
114 unsigned long size;
115 drm_context_t context;
116 int loops;
117 char buf[1024];
118 int i;
119 drmBufInfoPtr info;
120 drmBufMapPtr bufs;
121 drmLockPtr lock;
122 int secs;
123
124 while ((c = getopt(argc, argv,
125 "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
126 switch (c) {
127 case 'F':
128 count = strtoul(optarg, NULL, 0);
129 if (!fork()) {
130 dup(fd);
131 sleep(count);
132 }
133 close(fd);
134 break;
135 case 'v': getversion(fd); break;
136 case 'X':
137 if ((r = drmCreateContext(fd, &context))) {
138 drmError(r, argv[0]);
139 return 1;
140 }
141 printf( "Got %d\n", context);
142 break;
143 case 'S':
144 process_sigio(optarg);
145 break;
146 case 'C':
147 if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
148 drmError(r, argv[0]);
149 return 1;
150 }
151 break;
152 case 'c':
153 if ((r = drmSetBusid(fd,optarg))) {
154 drmError(r, argv[0]);
155 return 1;
156 }
157 break;
158 case 'o':
159 if ((fd = drmOpen(optarg, NULL)) < 0) {
160 drmError(fd, argv[0]);
161 return 1;
162 }
163 break;
164 case 'O':
165 if ((fd = drmOpen(NULL, optarg)) < 0) {
166 drmError(fd, argv[0]);
167 return 1;
168 }
169 break;
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);
174 {
175 drmDMAReq dma;
176 int *indices, *sizes;
177
178 indices = alloca(sizeof(*indices) * count);
179 sizes = alloca(sizeof(*sizes) * count);
180 dma.context = context;
181 dma.send_count = 0;
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]);
189 return 1;
190 }
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]);
194 }
195 sleep(secs);
196 drmFreeBufs(fd, dma.granted_count, indices);
197 }
198 break;
199 case 'b':
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]);
204 return 1;
205 }
206 if (!(info = drmGetBufInfo(fd))) {
207 drmError(0, argv[0]);
208 return 1;
209 }
210 for (i = 0; i < info->count; i++) {
211 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
212 info->list[i].count,
213 info->list[i].size,
214 info->list[i].low_mark,
215 info->list[i].high_mark);
216 }
217 if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
218 drmError(r, argv[0]);
219 return 1;
220 }
221 if (!(info = drmGetBufInfo(fd))) {
222 drmError(0, argv[0]);
223 return 1;
224 }
225 for (i = 0; i < info->count; i++) {
226 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
227 info->list[i].count,
228 info->list[i].size,
229 info->list[i].low_mark,
230 info->list[i].high_mark);
231 }
232 printf("===== /proc/dri/0/mem =====\n");
233 sprintf(buf, "cat /proc/dri/0/mem");
234 system(buf);
235 #if 1
236 if (!(bufs = drmMapBufs(fd))) {
237 drmError(0, argv[0]);
238 return 1;
239 }
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",
244 i,
245 bufs->list[i].total,
246 bufs->list[i].address);
247 }
248 printf("===== /proc/dri/0/vma =====\n");
249 sprintf(buf, "cat /proc/dri/0/vma");
250 system(buf);
251 #endif
252 break;
253 case 'f':
254 offset = strtoul(optarg, &pt, 0);
255 size = strtoul(pt+1, NULL, 0);
256 handle = 0;
257 if ((r = drmAddMap(fd, offset, size,
258 DRM_FRAME_BUFFER, 0, &handle))) {
259 drmError(r, argv[0]);
260 return 1;
261 }
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");
265 system(buf);
266 break;
267 case 'r':
268 case 'R':
269 offset = strtoul(optarg, &pt, 0);
270 size = strtoul(pt+1, NULL, 0);
271 handle = 0;
272 if ((r = drmAddMap(fd, offset, size,
273 DRM_REGISTERS,
274 c == 'R' ? DRM_READ_ONLY : 0,
275 &handle))) {
276 drmError(r, argv[0]);
277 return 1;
278 }
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");
282 system(buf);
283 break;
284 case 's':
285 size = strtoul(optarg, &pt, 0);
286 handle = 0;
287 if ((r = drmAddMap(fd, 0, size,
288 DRM_SHM, DRM_CONTAINS_LOCK,
289 &handle))) {
290 drmError(r, argv[0]);
291 return 1;
292 }
293 printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
294 sprintf(buf, "cat /proc/dri/0/vm");
295 system(buf);
296 break;
297 case 'P':
298 offset = strtoul(optarg, &pt, 0);
299 size = strtoul(pt+1, NULL, 0);
300 address = NULL;
301 if ((r = drmMap(fd, offset, size, &address))) {
302 drmError(r, argv[0]);
303 return 1;
304 }
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");
309 system(buf);
310 mprotect((void *)offset, size, PROT_READ);
311 printf("===== /proc/dri/0/vma =====\n");
312 sprintf(buf, "cat /proc/dri/0/vma");
313 system(buf);
314 break;
315 case 'w':
316 case 'W':
317 offset = strtoul(optarg, &pt, 0);
318 size = strtoul(pt+1, NULL, 0);
319 address = NULL;
320 if ((r = drmMap(fd, offset, size, &address))) {
321 drmError(r, argv[0]);
322 return 1;
323 }
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());
328 system(buf);
329 printf("===== /proc/dri/0/mem =====\n");
330 sprintf(buf, "cat /proc/dri/0/mem");
331 system(buf);
332 printf("===== /proc/dri/0/vma =====\n");
333 sprintf(buf, "cat /proc/dri/0/vma");
334 system(buf);
335 printf("===== READING =====\n");
336 for (i = 0; i < 0x10; i++)
337 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
338 printf("\n");
339 if (c == 'w') {
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;
344 }
345 }
346 printf("===== READING =====\n");
347 for (i = 0; i < 0x10; i++)
348 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
349 printf("\n");
350 printf("===== /proc/dri/0/vma =====\n");
351 sprintf(buf, "cat /proc/dri/0/vma");
352 system(buf);
353 break;
354 case 'L':
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);
359 address = NULL;
360 if ((r = drmMap(fd, offset, size, &address))) {
361 drmError(r, argv[0]);
362 return 1;
363 }
364 lock = address;
365 #if 1
366 {
367 int counter = 0;
368 struct timeval loop_start, loop_end;
369 struct timeval lock_start, lock_end;
370 double wt;
371 #define HISTOSIZE 9
372 int histo[HISTOSIZE];
373 int output = 0;
374 int fast = 0;
375
376 if (loops < 0) {
377 loops = -loops;
378 ++output;
379 }
380
381 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
382
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);
389 ++counter;
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];
399 else ++histo[7];
400 if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
401 }
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]);
414 }
415 #else
416 printf( "before lock: 0x%08x\n", lock->lock);
417 printf( "lock: 0x%08x\n", lock->lock);
418 sleep(5);
419 printf( "unlock: 0x%08x\n", lock->lock);
420 #endif
421 break;
422 default:
423 fprintf( stderr, "Usage: drmstat [options]\n" );
424 return 1;
425 }
426
427 return r;
428 }
429
430 void DRM_PRINTFLIKE(4, 0)
xf86VDrvMsgVerb(int scrnIndex,int type,int verb,const char * format,va_list args)431 xf86VDrvMsgVerb(int scrnIndex, int type, int verb, const char *format,
432 va_list args)
433 {
434 vfprintf(stderr, format, args);
435 }
436
437 int xf86ConfigDRI[10];
438