Home | History | Annotate | Download | only in tests
      1 /* dristat.c --
      2  * Created: Mon Jan 15 05:05:07 2001 by faith (at) acm.org
      3  *
      4  * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  * and/or sell copies of the Software, and to permit persons to whom the
     12  * Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the next
     15  * paragraph) shall be included in all copies or substantial portions of the
     16  * Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     24  * DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors: Rickard E. (Rik) Faith <faith (at) valinux.com>
     27  *
     28  */
     29 
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <unistd.h>
     33 #include "xf86drm.h"
     34 #include "xf86drmRandom.c"
     35 #include "xf86drmHash.c"
     36 #include "xf86drm.c"
     37 
     38 #define DRM_VERSION 0x00000001
     39 #define DRM_MEMORY  0x00000002
     40 #define DRM_CLIENTS 0x00000004
     41 #define DRM_STATS   0x00000008
     42 #define DRM_BUSID   0x00000010
     43 
     44 static void getversion(int fd)
     45 {
     46     drmVersionPtr version;
     47 
     48     version = drmGetVersion(fd);
     49     if (version) {
     50 	printf("  Version information:\n");
     51 	printf("    Name: %s\n", version->name ? version->name : "?");
     52 	printf("    Version: %d.%d.%d\n",
     53 	       version->version_major,
     54 	       version->version_minor,
     55 	       version->version_patchlevel);
     56 	printf("    Date: %s\n", version->date ? version->date : "?");
     57 	printf("    Desc: %s\n", version->desc ? version->desc : "?");
     58 	drmFreeVersion(version);
     59     } else {
     60 	printf("  No version information available\n");
     61     }
     62 }
     63 
     64 static void getbusid(int fd)
     65 {
     66     const char *busid = drmGetBusid(fd);
     67 
     68     printf("  Busid: %s\n", *busid ? busid : "(not set)");
     69     drmFreeBusid(busid);
     70 }
     71 
     72 
     73 static void getvm(int fd)
     74 {
     75     int             i;
     76     const char      *typename;
     77     char            flagname[33];
     78     drm_handle_t    offset;
     79     drmSize         size;
     80     drmMapType      type;
     81     drmMapFlags     flags;
     82     drm_handle_t    handle;
     83     int             mtrr;
     84 
     85     printf("  VM map information:\n");
     86     printf("  flags: (R)estricted (r)ead/(w)rite (l)ocked (k)ernel (W)rite-combine (L)ock:\n");
     87     printf("    slot     offset       size type flags    address mtrr\n");
     88 
     89     for (i = 0;
     90 	 !drmGetMap(fd, i, &offset, &size, &type, &flags, &handle, &mtrr);
     91 	 i++) {
     92 
     93 	switch (type) {
     94 	case DRM_FRAME_BUFFER:   typename = "FB";  break;
     95 	case DRM_REGISTERS:      typename = "REG"; break;
     96 	case DRM_SHM:            typename = "SHM"; break;
     97 	case DRM_AGP:            typename = "AGP"; break;
     98 	case DRM_SCATTER_GATHER: typename = "SG";  break;
     99 	default:                 typename = "???"; break;
    100 	}
    101 
    102 	flagname[0] = (flags & DRM_RESTRICTED)      ? 'R' : ' ';
    103 	flagname[1] = (flags & DRM_READ_ONLY)       ? 'r' : 'w';
    104 	flagname[2] = (flags & DRM_LOCKED)          ? 'l' : ' ';
    105 	flagname[3] = (flags & DRM_KERNEL)          ? 'k' : ' ';
    106 	flagname[4] = (flags & DRM_WRITE_COMBINING) ? 'W' : ' ';
    107 	flagname[5] = (flags & DRM_CONTAINS_LOCK)   ? 'L' : ' ';
    108 	flagname[6] = '\0';
    109 
    110 	printf("    %4d 0x%08lx 0x%08lx %3.3s %6.6s 0x%08lx ",
    111 	       i, offset, (unsigned long)size, typename, flagname, handle);
    112 	if (mtrr < 0) printf("none\n");
    113 	else          printf("%4d\n", mtrr);
    114     }
    115 }
    116 
    117 static void getclients(int fd)
    118 {
    119     int           i;
    120     int           auth;
    121     int           pid;
    122     int           uid;
    123     unsigned long magic;
    124     unsigned long iocs;
    125     char          buf[64];
    126     char          cmd[40];
    127     int           procfd;
    128 
    129     printf("  DRI client information:\n");
    130     printf("    a   pid   uid      magic     ioctls   prog\n");
    131 
    132     for (i = 0; !drmGetClient(fd, i, &auth, &pid, &uid, &magic, &iocs); i++) {
    133 	sprintf(buf, "/proc/%d/cmdline", pid);
    134 	memset(cmd, 0, sizeof(cmd));
    135 	if ((procfd = open(buf, O_RDONLY, 0)) >= 0) {
    136 	    read(procfd, cmd, sizeof(cmd)-1);
    137 	    close(procfd);
    138 	}
    139 	if (*cmd) {
    140 	    char *pt;
    141 
    142 	    for (pt = cmd; *pt; pt++) if (!isprint(*pt)) *pt = ' ';
    143 	    printf("    %c %5d %5d %10lu %10lu   %s\n",
    144 		   auth ? 'y' : 'n', pid, uid, magic, iocs, cmd);
    145 	} else {
    146 	    printf("    %c %5d %5d %10lu %10lu\n",
    147 		   auth ? 'y' : 'n', pid, uid, magic, iocs);
    148 	}
    149     }
    150 }
    151 
    152 static void printhuman(unsigned long value, const char *name, int mult)
    153 {
    154     const char *p;
    155     double     f;
    156 				/* Print width 5 number in width 6 space */
    157     if (value < 100000) {
    158 	printf(" %5lu", value);
    159 	return;
    160     }
    161 
    162     p = name;
    163     f = (double)value / (double)mult;
    164     if (f < 10.0) {
    165 	printf(" %4.2f%c", f, *p);
    166 	return;
    167     }
    168 
    169     p++;
    170     f = (double)value / (double)mult;
    171     if (f < 10.0) {
    172 	printf(" %4.2f%c", f, *p);
    173 	return;
    174     }
    175 
    176     p++;
    177     f = (double)value / (double)mult;
    178     if (f < 10.0) {
    179 	printf(" %4.2f%c", f, *p);
    180 	return;
    181     }
    182 }
    183 
    184 static void getstats(int fd, int i)
    185 {
    186     drmStatsT prev, curr;
    187     int       j;
    188     double    rate;
    189 
    190     printf("  System statistics:\n");
    191 
    192     if (drmGetStats(fd, &prev)) return;
    193     if (!i) {
    194 	for (j = 0; j < prev.count; j++) {
    195 	    printf("    ");
    196 	    printf(prev.data[j].long_format, prev.data[j].long_name);
    197 	    if (prev.data[j].isvalue) printf(" 0x%08lx\n", prev.data[j].value);
    198 	    else                      printf(" %10lu\n", prev.data[j].value);
    199 	}
    200 	return;
    201     }
    202 
    203     printf("    ");
    204     for (j = 0; j < prev.count; j++)
    205 	if (!prev.data[j].verbose) {
    206 	    printf(" ");
    207 	    printf(prev.data[j].rate_format, prev.data[j].rate_name);
    208 	}
    209     printf("\n");
    210 
    211     for (;;) {
    212 	sleep(i);
    213 	if (drmGetStats(fd, &curr)) return;
    214 	printf("    ");
    215 	for (j = 0; j < curr.count; j++) {
    216 	    if (curr.data[j].verbose) continue;
    217 	    if (curr.data[j].isvalue) {
    218 		printf(" %08lx", curr.data[j].value);
    219 	    } else {
    220 		rate = (curr.data[j].value - prev.data[j].value) / (double)i;
    221 		printhuman(rate, curr.data[j].mult_names, curr.data[j].mult);
    222 	    }
    223 	}
    224 	printf("\n");
    225 	memcpy(&prev, &curr, sizeof(prev));
    226     }
    227 
    228 }
    229 
    230 int main(int argc, char **argv)
    231 {
    232     int  c;
    233     int  mask     = 0;
    234     int  minor    = 0;
    235     int  interval = 0;
    236     int  fd;
    237     char buf[64];
    238     int  i;
    239 
    240     while ((c = getopt(argc, argv, "avmcsbM:i:")) != EOF)
    241 	switch (c) {
    242 	case 'a': mask = ~0;                          break;
    243 	case 'v': mask |= DRM_VERSION;                break;
    244 	case 'm': mask |= DRM_MEMORY;                 break;
    245 	case 'c': mask |= DRM_CLIENTS;                break;
    246 	case 's': mask |= DRM_STATS;                  break;
    247 	case 'b': mask |= DRM_BUSID;                  break;
    248 	case 'i': interval = strtol(optarg, NULL, 0); break;
    249 	case 'M': minor = strtol(optarg, NULL, 0);    break;
    250 	default:
    251 	    fprintf( stderr, "Usage: dristat [options]\n\n" );
    252 	    fprintf( stderr, "Displays DRM information. Use with no arguments to display available cards.\n\n" );
    253 	    fprintf( stderr, "  -a            Show all available information\n" );
    254 	    fprintf( stderr, "  -b            Show DRM bus ID's\n" );
    255 	    fprintf( stderr, "  -c            Display information about DRM clients\n" );
    256 	    fprintf( stderr, "  -i [interval] Continuously display statistics every [interval] seconds\n" );
    257 	    fprintf( stderr, "  -v            Display DRM module and card version information\n" );
    258 	    fprintf( stderr, "  -m            Display memory use information\n" );
    259 	    fprintf( stderr, "  -s            Display DRM statistics\n" );
    260 	    fprintf( stderr, "  -M [minor]    Select card by minor number\n" );
    261 	    return 1;
    262 	}
    263 
    264     for (i = 0; i < 16; i++) if (!minor || i == minor) {
    265 	sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, i);
    266 	fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
    267 	if (fd >= 0) {
    268 	    printf("%s\n", buf);
    269 	    if (mask & DRM_BUSID)   getbusid(fd);
    270 	    if (mask & DRM_VERSION) getversion(fd);
    271 	    if (mask & DRM_MEMORY)  getvm(fd);
    272 	    if (mask & DRM_CLIENTS) getclients(fd);
    273 	    if (mask & DRM_STATS)   getstats(fd, interval);
    274 	    close(fd);
    275 	}
    276     }
    277 
    278     return 0;
    279 }
    280