1 /* dristat.c --
2  * Created: Mon Jan 15 05:05:07 2001 by faith@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@valinux.com>
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include "xf86drm.h"
38 #include "xf86drmRandom.c"
39 #include "xf86drmHash.c"
40 #include "xf86drm.c"
41 
42 #define DRM_VERSION 0x00000001
43 #define DRM_MEMORY  0x00000002
44 #define DRM_CLIENTS 0x00000004
45 #define DRM_STATS   0x00000008
46 #define DRM_BUSID   0x00000010
47 
getversion(int fd)48 static void getversion(int fd)
49 {
50     drmVersionPtr version;
51 
52     version = drmGetVersion(fd);
53     if (version) {
54 	printf("  Version information:\n");
55 	printf("    Name: %s\n", version->name ? version->name : "?");
56 	printf("    Version: %d.%d.%d\n",
57 	       version->version_major,
58 	       version->version_minor,
59 	       version->version_patchlevel);
60 	printf("    Date: %s\n", version->date ? version->date : "?");
61 	printf("    Desc: %s\n", version->desc ? version->desc : "?");
62 	drmFreeVersion(version);
63     } else {
64 	printf("  No version information available\n");
65     }
66 }
67 
getbusid(int fd)68 static void getbusid(int fd)
69 {
70     const char *busid = drmGetBusid(fd);
71 
72     printf("  Busid: %s\n", *busid ? busid : "(not set)");
73     drmFreeBusid(busid);
74 }
75 
76 
getvm(int fd)77 static void getvm(int fd)
78 {
79     int             i;
80     const char      *typename;
81     char            flagname[33];
82     drm_handle_t    offset;
83     drmSize         size;
84     drmMapType      type;
85     drmMapFlags     flags;
86     drm_handle_t    handle;
87     int             mtrr;
88 
89     printf("  VM map information:\n");
90     printf("  flags: (R)estricted (r)ead/(w)rite (l)ocked (k)ernel (W)rite-combine (L)ock:\n");
91     printf("    slot     offset       size type flags    address mtrr\n");
92 
93     for (i = 0;
94 	 !drmGetMap(fd, i, &offset, &size, &type, &flags, &handle, &mtrr);
95 	 i++) {
96 
97 	switch (type) {
98 	case DRM_FRAME_BUFFER:   typename = "FB";  break;
99 	case DRM_REGISTERS:      typename = "REG"; break;
100 	case DRM_SHM:            typename = "SHM"; break;
101 	case DRM_AGP:            typename = "AGP"; break;
102 	case DRM_SCATTER_GATHER: typename = "SG";  break;
103 	default:                 typename = "???"; break;
104 	}
105 
106 	flagname[0] = (flags & DRM_RESTRICTED)      ? 'R' : ' ';
107 	flagname[1] = (flags & DRM_READ_ONLY)       ? 'r' : 'w';
108 	flagname[2] = (flags & DRM_LOCKED)          ? 'l' : ' ';
109 	flagname[3] = (flags & DRM_KERNEL)          ? 'k' : ' ';
110 	flagname[4] = (flags & DRM_WRITE_COMBINING) ? 'W' : ' ';
111 	flagname[5] = (flags & DRM_CONTAINS_LOCK)   ? 'L' : ' ';
112 	flagname[6] = '\0';
113 
114 	printf("    %4d 0x%08lx 0x%08lx %3.3s %6.6s 0x%08lx ",
115 	       i, (unsigned long)offset, (unsigned long)size,
116 	       typename, flagname, (unsigned long)handle);
117 	if (mtrr < 0) printf("none\n");
118 	else          printf("%4d\n", mtrr);
119     }
120 }
121 
getclients(int fd)122 static void getclients(int fd)
123 {
124     int           i;
125     int           auth;
126     int           pid;
127     int           uid;
128     unsigned long magic;
129     unsigned long iocs;
130     char          buf[64];
131     char          cmd[40];
132     int           procfd;
133 
134     printf("  DRI client information:\n");
135     printf("    a   pid   uid      magic     ioctls   prog\n");
136 
137     for (i = 0; !drmGetClient(fd, i, &auth, &pid, &uid, &magic, &iocs); i++) {
138 	sprintf(buf, "/proc/%d/cmdline", pid);
139 	memset(cmd, 0, sizeof(cmd));
140 	if ((procfd = open(buf, O_RDONLY, 0)) >= 0) {
141 	    read(procfd, cmd, sizeof(cmd)-1);
142 	    close(procfd);
143 	}
144 	if (*cmd) {
145 	    char *pt;
146 
147 	    for (pt = cmd; *pt; pt++) if (!isprint(*pt)) *pt = ' ';
148 	    printf("    %c %5d %5d %10lu %10lu   %s\n",
149 		   auth ? 'y' : 'n', pid, uid, magic, iocs, cmd);
150 	} else {
151 	    printf("    %c %5d %5d %10lu %10lu\n",
152 		   auth ? 'y' : 'n', pid, uid, magic, iocs);
153 	}
154     }
155 }
156 
printhuman(unsigned long value,const char * name,int mult)157 static void printhuman(unsigned long value, const char *name, int mult)
158 {
159     const char *p;
160     double     f;
161 				/* Print width 5 number in width 6 space */
162     if (value < 100000) {
163 	printf(" %5lu", value);
164 	return;
165     }
166 
167     p = name;
168     f = (double)value / (double)mult;
169     if (f < 10.0) {
170 	printf(" %4.2f%c", f, *p);
171 	return;
172     }
173 
174     p++;
175     f = (double)value / (double)mult;
176     if (f < 10.0) {
177 	printf(" %4.2f%c", f, *p);
178 	return;
179     }
180 
181     p++;
182     f = (double)value / (double)mult;
183     if (f < 10.0) {
184 	printf(" %4.2f%c", f, *p);
185 	return;
186     }
187 }
188 
getstats(int fd,int i)189 static void getstats(int fd, int i)
190 {
191     drmStatsT prev, curr;
192     int       j;
193     double    rate;
194 
195     printf("  System statistics:\n");
196 
197     if (drmGetStats(fd, &prev)) return;
198     if (!i) {
199 	for (j = 0; j < prev.count; j++) {
200 	    printf("    ");
201 	    printf(prev.data[j].long_format, prev.data[j].long_name);
202 	    if (prev.data[j].isvalue) printf(" 0x%08lx\n", prev.data[j].value);
203 	    else                      printf(" %10lu\n", prev.data[j].value);
204 	}
205 	return;
206     }
207 
208     printf("    ");
209     for (j = 0; j < prev.count; j++)
210 	if (!prev.data[j].verbose) {
211 	    printf(" ");
212 	    printf(prev.data[j].rate_format, prev.data[j].rate_name);
213 	}
214     printf("\n");
215 
216     for (;;) {
217 	sleep(i);
218 	if (drmGetStats(fd, &curr)) return;
219 	printf("    ");
220 	for (j = 0; j < curr.count; j++) {
221 	    if (curr.data[j].verbose) continue;
222 	    if (curr.data[j].isvalue) {
223 		printf(" %08lx", curr.data[j].value);
224 	    } else {
225 		rate = (curr.data[j].value - prev.data[j].value) / (double)i;
226 		printhuman(rate, curr.data[j].mult_names, curr.data[j].mult);
227 	    }
228 	}
229 	printf("\n");
230 	memcpy(&prev, &curr, sizeof(prev));
231     }
232 
233 }
234 
main(int argc,char ** argv)235 int main(int argc, char **argv)
236 {
237     int  c;
238     int  mask     = 0;
239     int  minor    = 0;
240     int  interval = 0;
241     int  fd;
242     char buf[64];
243     int  i;
244 
245     while ((c = getopt(argc, argv, "avmcsbM:i:")) != EOF)
246 	switch (c) {
247 	case 'a': mask = ~0;                          break;
248 	case 'v': mask |= DRM_VERSION;                break;
249 	case 'm': mask |= DRM_MEMORY;                 break;
250 	case 'c': mask |= DRM_CLIENTS;                break;
251 	case 's': mask |= DRM_STATS;                  break;
252 	case 'b': mask |= DRM_BUSID;                  break;
253 	case 'i': interval = strtol(optarg, NULL, 0); break;
254 	case 'M': minor = strtol(optarg, NULL, 0);    break;
255 	default:
256 	    fprintf( stderr, "Usage: dristat [options]\n\n" );
257 	    fprintf( stderr, "Displays DRM information. Use with no arguments to display available cards.\n\n" );
258 	    fprintf( stderr, "  -a            Show all available information\n" );
259 	    fprintf( stderr, "  -b            Show DRM bus ID's\n" );
260 	    fprintf( stderr, "  -c            Display information about DRM clients\n" );
261 	    fprintf( stderr, "  -i [interval] Continuously display statistics every [interval] seconds\n" );
262 	    fprintf( stderr, "  -v            Display DRM module and card version information\n" );
263 	    fprintf( stderr, "  -m            Display memory use information\n" );
264 	    fprintf( stderr, "  -s            Display DRM statistics\n" );
265 	    fprintf( stderr, "  -M [minor]    Select card by minor number\n" );
266 	    return 1;
267 	}
268 
269     for (i = 0; i < 16; i++) if (!minor || i == minor) {
270 	sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, i);
271 	fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
272 	if (fd >= 0) {
273 	    printf("%s\n", buf);
274 	    if (mask & DRM_BUSID)   getbusid(fd);
275 	    if (mask & DRM_VERSION) getversion(fd);
276 	    if (mask & DRM_MEMORY)  getvm(fd);
277 	    if (mask & DRM_CLIENTS) getclients(fd);
278 	    if (mask & DRM_STATS)   getstats(fd, interval);
279 	    close(fd);
280 	}
281     }
282 
283     return 0;
284 }
285