OSDN Git Service

tests/random: extract test out of xf86drmRandom.c
[android-x86/external-libdrm.git] / tests / drmstat.c
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
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
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
85 static void process_sigio(char *device)
86 {
87     int              fd;
88
89     if ((fd = open(device, 0)) < 0) {
90         drmError(-errno, __FUNCTION__);
91         exit(1);
92     }
93
94     sigio_fd = fd;
95     for (;;) sleep(60);
96 }
97
98 int main(int argc, char **argv)
99 {
100     int            c;
101     int            r  = 0;
102     int            fd = -1;
103     drm_handle_t      handle;
104     void           *address;
105     char           *pt;
106     unsigned long  count;
107     unsigned long  offset;
108     unsigned long  size;
109     drm_context_t  context;
110     int            loops;
111     char           buf[1024];
112     int            i;
113     drmBufInfoPtr  info;
114     drmBufMapPtr   bufs;
115     drmLockPtr     lock;
116     int            secs;
117
118     while ((c = getopt(argc, argv,
119                        "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
120         switch (c) {
121         case 'F':
122             count  = strtoul(optarg, NULL, 0);
123             if (!fork()) {
124                 dup(fd);
125                 sleep(count);
126             }
127             close(fd);
128             break;
129         case 'v': getversion(fd);                                        break;
130         case 'X':
131             if ((r = drmCreateContext(fd, &context))) {
132                 drmError(r, argv[0]);
133                 return 1;
134             }
135             printf( "Got %d\n", context);
136             break;
137         case 'S':
138             process_sigio(optarg);
139             break;
140         case 'C':
141             if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
142                 drmError(r, argv[0]);
143                 return 1;
144             }
145             break;
146         case 'c':
147             if ((r = drmSetBusid(fd,optarg))) {
148                 drmError(r, argv[0]);
149                 return 1;
150             }
151             break;
152         case 'o':
153             if ((fd = drmOpen(optarg, NULL)) < 0) {
154                 drmError(fd, argv[0]);
155                 return 1;
156             }
157             break;
158         case 'O':
159             if ((fd = drmOpen(NULL, optarg)) < 0) {
160                 drmError(fd, argv[0]);
161                 return 1;
162             }
163             break;
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);
168             {
169                 drmDMAReq      dma;
170                 int            *indices, *sizes;
171
172                 indices = alloca(sizeof(*indices) * count);
173                 sizes   = alloca(sizeof(*sizes)   * count);
174                 dma.context         = context;
175                 dma.send_count      = 0;
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]);
183                     return 1;
184                 }
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]);
188                 }
189                 sleep(secs);
190                 drmFreeBufs(fd, dma.granted_count, indices);
191             }
192             break;
193         case 'b':
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]);
198                 return 1;
199             }
200             if (!(info = drmGetBufInfo(fd))) {
201                 drmError(0, argv[0]);
202                 return 1;
203             }
204             for (i = 0; i < info->count; i++) {
205                 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
206                        info->list[i].count,
207                        info->list[i].size,
208                        info->list[i].low_mark,
209                        info->list[i].high_mark);
210             }
211             if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
212                 drmError(r, argv[0]);
213                 return 1;
214             }
215             if (!(info = drmGetBufInfo(fd))) {
216                 drmError(0, argv[0]);
217                 return 1;
218             }
219             for (i = 0; i < info->count; i++) {
220                 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
221                        info->list[i].count,
222                        info->list[i].size,
223                        info->list[i].low_mark,
224                        info->list[i].high_mark);
225             }
226             printf("===== /proc/dri/0/mem =====\n");
227             sprintf(buf, "cat /proc/dri/0/mem");
228             system(buf);
229 #if 1
230             if (!(bufs = drmMapBufs(fd))) {
231                 drmError(0, argv[0]);
232                 return 1;
233             }
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",
238                         i,
239                         bufs->list[i].total,
240                         bufs->list[i].address);
241             }
242             printf("===== /proc/dri/0/vma =====\n");
243             sprintf(buf, "cat /proc/dri/0/vma");
244             system(buf);
245 #endif
246             break;
247         case 'f':
248             offset  = strtoul(optarg, &pt, 0);
249             size    = strtoul(pt+1, NULL, 0);
250             handle  = 0;
251             if ((r = drmAddMap(fd, offset, size,
252                                DRM_FRAME_BUFFER, 0, &handle))) {
253                 drmError(r, argv[0]);
254                 return 1;
255             }
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");
259             system(buf);
260             break;
261         case 'r':
262         case 'R':
263             offset  = strtoul(optarg, &pt, 0);
264             size    = strtoul(pt+1, NULL, 0);
265             handle  = 0;
266             if ((r = drmAddMap(fd, offset, size,
267                                DRM_REGISTERS,
268                                c == 'R' ? DRM_READ_ONLY : 0,
269                                &handle))) {
270                 drmError(r, argv[0]);
271                 return 1;
272             }
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");
276             system(buf);
277             break;
278         case 's':
279             size = strtoul(optarg, &pt, 0);
280             handle = 0;
281             if ((r = drmAddMap(fd, 0, size,
282                                DRM_SHM, DRM_CONTAINS_LOCK,
283                                &handle))) {
284                 drmError(r, argv[0]);
285                 return 1;
286             }
287             printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
288             sprintf(buf, "cat /proc/dri/0/vm");
289             system(buf);
290             break;
291         case 'P':
292             offset  = strtoul(optarg, &pt, 0);
293             size    = strtoul(pt+1, NULL, 0);
294             address = NULL;
295             if ((r = drmMap(fd, offset, size, &address))) {
296                 drmError(r, argv[0]);
297                 return 1;
298             }
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");
303             system(buf);
304             mprotect((void *)offset, size, PROT_READ);
305             printf("===== /proc/dri/0/vma =====\n");
306             sprintf(buf, "cat /proc/dri/0/vma");
307             system(buf);
308             break;
309         case 'w':
310         case 'W':
311             offset  = strtoul(optarg, &pt, 0);
312             size    = strtoul(pt+1, NULL, 0);
313             address = NULL;
314             if ((r = drmMap(fd, offset, size, &address))) {
315                 drmError(r, argv[0]);
316                 return 1;
317             }
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());
322             system(buf);
323             printf("===== /proc/dri/0/mem =====\n");
324             sprintf(buf, "cat /proc/dri/0/mem");
325             system(buf);
326             printf("===== /proc/dri/0/vma =====\n");
327             sprintf(buf, "cat /proc/dri/0/vma");
328             system(buf);
329             printf("===== READING =====\n");
330             for (i = 0; i < 0x10; i++)
331                 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
332             printf("\n");
333             if (c == 'w') {
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;
338                 }
339             }
340             printf("===== READING =====\n");
341             for (i = 0; i < 0x10; i++)
342                 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
343             printf("\n");
344             printf("===== /proc/dri/0/vma =====\n");
345             sprintf(buf, "cat /proc/dri/0/vma");
346             system(buf);
347             break;
348         case 'L':
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);
353             address = NULL;
354             if ((r = drmMap(fd, offset, size, &address))) {
355                 drmError(r, argv[0]);
356                 return 1;
357             }
358             lock       = address;
359 #if 1
360             {
361                 int            counter = 0;
362                 struct timeval loop_start, loop_end;
363                 struct timeval lock_start, lock_end;
364                 double         wt;
365 #define HISTOSIZE 9
366                 int            histo[HISTOSIZE];
367                 int            output = 0;
368                 int            fast   = 0;
369
370                 if (loops < 0) {
371                     loops = -loops;
372                     ++output;
373                 }
374
375                 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
376
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);
383                     ++counter;
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];
393                     else                     ++histo[7];
394                     if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
395                 }
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]);
408             }
409 #else
410             printf( "before lock: 0x%08x\n", lock->lock);
411             printf( "lock: 0x%08x\n", lock->lock);
412             sleep(5);
413             printf( "unlock: 0x%08x\n", lock->lock);
414 #endif
415             break;
416         default:
417             fprintf( stderr, "Usage: drmstat [options]\n" );
418             return 1;
419         }
420
421     return r; 
422 }
423
424 int xf86ConfigDRI[10];