Include alloca.h in tests/drmstat.c when configure detects it
[platform/upstream/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 #include "config.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/mman.h>
39 #include <getopt.h>
40 #include <strings.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <fcntl.h>
44 #ifdef HAVE_ALLOCA_H
45 # include <alloca.h>
46 #endif
47 #include "xf86drm.h"
48
49 int sigio_fd;
50
51 static double usec(struct timeval *end, struct timeval *start)
52 {
53     double e = end->tv_sec   * 1000000 + end->tv_usec;
54     double s = start->tv_sec * 1000000 + start->tv_usec;
55
56     return e - s;
57 }
58
59 static void getversion(int fd)
60 {
61     drmVersionPtr version;
62     
63     version = drmGetVersion(fd);
64     if (version) {
65         printf( "Name: %s\n", version->name ? version->name : "?" );
66         printf( "    Version: %d.%d.%d\n",
67                 version->version_major,
68                 version->version_minor,
69                 version->version_patchlevel );
70         printf( "    Date: %s\n", version->date ? version->date : "?" );
71         printf( "    Desc: %s\n", version->desc ? version->desc : "?" );
72         drmFreeVersion(version);
73     } else {
74         printf( "No driver available\n" );
75     }
76 }
77         
78 void handler(int fd, void *oldctx, void *newctx)
79 {
80     printf("Got fd %d\n", fd);
81 }
82
83 void process_sigio(char *device)
84 {
85     int              fd;
86
87     if ((fd = open(device, 0)) < 0) {
88         drmError(-errno, __FUNCTION__);
89         exit(1);
90     }
91
92     sigio_fd = fd;
93     /*  drmInstallSIGIOHandler(fd, handler); */
94     for (;;) sleep(60);
95 }
96
97 int main(int argc, char **argv)
98 {
99     int            c;
100     int            r  = 0;
101     int            fd = -1;
102     drm_handle_t      handle;
103     void           *address;
104     char           *pt;
105     unsigned long  count;
106     unsigned long  offset;
107     unsigned long  size;
108     drm_context_t  context;
109     int            loops;
110     char           buf[1024];
111     int            i;
112     drmBufInfoPtr  info;
113     drmBufMapPtr   bufs;
114     drmLockPtr     lock;
115     int            secs;
116
117     while ((c = getopt(argc, argv,
118                        "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
119         switch (c) {
120         case 'F':
121             count  = strtoul(optarg, NULL, 0);
122             if (!fork()) {
123                 dup(fd);
124                 sleep(count);
125             }
126             close(fd);
127             break;
128         case 'v': getversion(fd);                                        break;
129         case 'X':
130             if ((r = drmCreateContext(fd, &context))) {
131                 drmError(r, argv[0]);
132                 return 1;
133             }
134             printf( "Got %d\n", context);
135             break;
136         case 'S':
137             process_sigio(optarg);
138             break;
139         case 'C':
140             if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
141                 drmError(r, argv[0]);
142                 return 1;
143             }
144             break;
145         case 'c':
146             if ((r = drmSetBusid(fd,optarg))) {
147                 drmError(r, argv[0]);
148                 return 1;
149             }
150             break;
151         case 'o':
152             if ((fd = drmOpen(optarg, NULL)) < 0) {
153                 drmError(fd, argv[0]);
154                 return 1;
155             }
156             break;
157         case 'O':
158             if ((fd = drmOpen(NULL, optarg)) < 0) {
159                 drmError(fd, argv[0]);
160                 return 1;
161             }
162             break;
163         case 'B':               /* Test buffer allocation */
164             count  = strtoul(optarg, &pt, 0);
165             size   = strtoul(pt+1, &pt, 0);
166             secs   = strtoul(pt+1, NULL, 0);
167             {
168                 drmDMAReq      dma;
169                 int            *indices, *sizes;
170
171                 indices = alloca(sizeof(*indices) * count);
172                 sizes   = alloca(sizeof(*sizes)   * count);
173                 dma.context         = context;
174                 dma.send_count      = 0;
175                 dma.request_count   = count;
176                 dma.request_size    = size;
177                 dma.request_list    = indices;
178                 dma.request_sizes   = sizes;
179                 dma.flags           = DRM_DMA_WAIT;
180                 if ((r = drmDMA(fd, &dma))) {
181                     drmError(r, argv[0]);
182                     return 1;
183                 }
184                 for (i = 0; i < dma.granted_count; i++) {
185                     printf("%5d: index = %d, size = %d\n",
186                            i, dma.request_list[i], dma.request_sizes[i]);
187                 }
188                 sleep(secs);
189                 drmFreeBufs(fd, dma.granted_count, indices);
190             }
191             break;
192         case 'b':
193             count   = strtoul(optarg, &pt, 0);
194             size    = strtoul(pt+1, NULL, 0);
195             if ((r = drmAddBufs(fd, count, size, 0, 65536)) < 0) {
196                 drmError(r, argv[0]);
197                 return 1;
198             }
199             if (!(info = drmGetBufInfo(fd))) {
200                 drmError(0, argv[0]);
201                 return 1;
202             }
203             for (i = 0; i < info->count; i++) {
204                 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
205                        info->list[i].count,
206                        info->list[i].size,
207                        info->list[i].low_mark,
208                        info->list[i].high_mark);
209             }
210             if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
211                 drmError(r, argv[0]);
212                 return 1;
213             }
214             if (!(info = drmGetBufInfo(fd))) {
215                 drmError(0, argv[0]);
216                 return 1;
217             }
218             for (i = 0; i < info->count; i++) {
219                 printf("%5d buffers of size %6d (low = %d, high = %d)\n",
220                        info->list[i].count,
221                        info->list[i].size,
222                        info->list[i].low_mark,
223                        info->list[i].high_mark);
224             }
225             printf("===== /proc/dri/0/mem =====\n");
226             sprintf(buf, "cat /proc/dri/0/mem");
227             system(buf);
228 #if 1
229             if (!(bufs = drmMapBufs(fd))) {
230                 drmError(0, argv[0]);
231                 return 1;
232             }
233             printf("===============================\n");
234             printf( "%d bufs\n", bufs->count);
235             for (i = 0; i < bufs->count; i++) {
236                 printf( "  %4d: %8d bytes at %p\n",
237                         i,
238                         bufs->list[i].total,
239                         bufs->list[i].address);
240             }
241             printf("===== /proc/dri/0/vma =====\n");
242             sprintf(buf, "cat /proc/dri/0/vma");
243             system(buf);
244 #endif
245             break;
246         case 'f':
247             offset  = strtoul(optarg, &pt, 0);
248             size    = strtoul(pt+1, NULL, 0);
249             handle  = 0;
250             if ((r = drmAddMap(fd, offset, size,
251                                DRM_FRAME_BUFFER, 0, &handle))) {
252                 drmError(r, argv[0]);
253                 return 1;
254             }
255             printf("0x%08lx:0x%04lx added\n", offset, size);
256             printf("===== /proc/dri/0/mem =====\n");
257             sprintf(buf, "cat /proc/dri/0/mem");
258             system(buf);
259             break;
260         case 'r':
261         case 'R':
262             offset  = strtoul(optarg, &pt, 0);
263             size    = strtoul(pt+1, NULL, 0);
264             handle  = 0;
265             if ((r = drmAddMap(fd, offset, size,
266                                DRM_REGISTERS,
267                                c == 'R' ? DRM_READ_ONLY : 0,
268                                &handle))) {
269                 drmError(r, argv[0]);
270                 return 1;
271             }
272             printf("0x%08lx:0x%04lx added\n", offset, size);
273             printf("===== /proc/dri/0/mem =====\n");
274             sprintf(buf, "cat /proc/dri/0/mem");
275             system(buf);
276             break;
277         case 's':
278             size = strtoul(optarg, &pt, 0);
279             handle = 0;
280             if ((r = drmAddMap(fd, 0, size,
281                                DRM_SHM, DRM_CONTAINS_LOCK,
282                                &handle))) {
283                 drmError(r, argv[0]);
284                 return 1;
285             }
286             printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
287             sprintf(buf, "cat /proc/dri/0/vm");
288             system(buf);
289             break;
290         case 'P':
291             offset  = strtoul(optarg, &pt, 0);
292             size    = strtoul(pt+1, NULL, 0);
293             address = NULL;
294             if ((r = drmMap(fd, offset, size, &address))) {
295                 drmError(r, argv[0]);
296                 return 1;
297             }
298             printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
299                    offset, size, address, getpid());
300             printf("===== /proc/dri/0/vma =====\n");
301             sprintf(buf, "cat /proc/dri/0/vma");
302             system(buf);
303             mprotect((void *)offset, size, PROT_READ);
304             printf("===== /proc/dri/0/vma =====\n");
305             sprintf(buf, "cat /proc/dri/0/vma");
306             system(buf);
307             break;
308         case 'w':
309         case 'W':
310             offset  = strtoul(optarg, &pt, 0);
311             size    = strtoul(pt+1, NULL, 0);
312             address = NULL;
313             if ((r = drmMap(fd, offset, size, &address))) {
314                 drmError(r, argv[0]);
315                 return 1;
316             }
317             printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
318                    offset, size, address, getpid());
319             printf("===== /proc/%d/maps =====\n", getpid());
320             sprintf(buf, "cat /proc/%d/maps", getpid());
321             system(buf);
322             printf("===== /proc/dri/0/mem =====\n");
323             sprintf(buf, "cat /proc/dri/0/mem");
324             system(buf);
325             printf("===== /proc/dri/0/vma =====\n");
326             sprintf(buf, "cat /proc/dri/0/vma");
327             system(buf);
328             printf("===== READING =====\n");
329             for (i = 0; i < 0x10; i++)
330                 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
331             printf("\n");
332             if (c == 'w') {
333                 printf("===== WRITING =====\n");
334                 for (i = 0; i < size; i+=2) {
335                     ((char *)address)[i]   = i & 0xff;
336                     ((char *)address)[i+1] = i & 0xff;
337                 }
338             }
339             printf("===== READING =====\n");
340             for (i = 0; i < 0x10; i++)
341                 printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
342             printf("\n");
343             printf("===== /proc/dri/0/vma =====\n");
344             sprintf(buf, "cat /proc/dri/0/vma");
345             system(buf);
346             break;
347         case 'L':
348             context = strtoul(optarg, &pt, 0);
349             offset  = strtoul(pt+1, &pt, 0);
350             size    = strtoul(pt+1, &pt, 0);
351             loops   = strtoul(pt+1, NULL, 0);
352             address = NULL;
353             if ((r = drmMap(fd, offset, size, &address))) {
354                 drmError(r, argv[0]);
355                 return 1;
356             }
357             lock       = address;
358 #if 1
359             {
360                 int            counter = 0;
361                 struct timeval loop_start, loop_end;
362                 struct timeval lock_start, lock_end;
363                 double         wt;
364 #define HISTOSIZE 9
365                 int            histo[HISTOSIZE];
366                 int            output = 0;
367                 int            fast   = 0;
368
369                 if (loops < 0) {
370                     loops = -loops;
371                     ++output;
372                 }
373
374                 for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
375
376                 gettimeofday(&loop_start, NULL);
377                 for (i = 0; i < loops; i++) {
378                     gettimeofday(&lock_start, NULL);
379                     DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
380                     gettimeofday(&lock_end, NULL);
381                     DRM_UNLOCK(fd,lock,context);
382                     ++counter;
383                     wt = usec(&lock_end, &lock_start);
384                     if      (wt <=      2.5) ++histo[8];
385                     if      (wt <       5.0) ++histo[0];
386                     else if (wt <      50.0) ++histo[1];
387                     else if (wt <     500.0) ++histo[2];
388                     else if (wt <    5000.0) ++histo[3];
389                     else if (wt <   50000.0) ++histo[4];
390                     else if (wt <  500000.0) ++histo[5];
391                     else if (wt < 5000000.0) ++histo[6];
392                     else                     ++histo[7];
393                     if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
394                 }
395                 gettimeofday(&loop_end, NULL);
396                 printf( "Average wait time = %.2f usec, %d fast\n",
397                         usec(&loop_end, &loop_start) /  counter, fast);
398                 printf( "%9d <=     2.5 uS\n", histo[8]);
399                 printf( "%9d <        5 uS\n", histo[0]);
400                 printf( "%9d <       50 uS\n", histo[1]);
401                 printf( "%9d <      500 uS\n", histo[2]);
402                 printf( "%9d <     5000 uS\n", histo[3]);
403                 printf( "%9d <    50000 uS\n", histo[4]);
404                 printf( "%9d <   500000 uS\n", histo[5]);
405                 printf( "%9d <  5000000 uS\n", histo[6]);
406                 printf( "%9d >= 5000000 uS\n", histo[7]);
407             }
408 #else
409             printf( "before lock: 0x%08x\n", lock->lock);
410             printf( "lock: 0x%08x\n", lock->lock);
411             sleep(5);
412             printf( "unlock: 0x%08x\n", lock->lock);
413 #endif
414             break;
415         default:
416             fprintf( stderr, "Usage: drmstat [options]\n" );
417             return 1;
418         }
419
420     return r; 
421 }
422
423 void
424 xf86VDrvMsgVerb(int scrnIndex, int type, int verb, const char *format,
425                 va_list args)
426 {
427         vfprintf(stderr, format, args);
428 }
429
430 int xf86ConfigDRI[10];