xref: /freebsd/tools/tools/umastat/umastat.c (revision aa0a1e58)
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 
31 #define LIBMEMSTAT	/* Cause vm_page.h not to include opt_vmpage.h */
32 #include <vm/vm.h>
33 #include <vm/vm_page.h>
34 
35 #include <vm/uma.h>
36 #include <vm/uma_int.h>
37 
38 #include <err.h>
39 #include <kvm.h>
40 #include <limits.h>
41 #include <memstat.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 
46 static struct nlist namelist[] = {
47 #define X_UMA_KEGS	0
48 	{ .n_name = "_uma_kegs" },
49 #define X_MP_MAXCPUS	1
50 	{ .n_name = "_mp_maxcpus" },
51 #define X_MP_MAXID	2
52 	{ .n_name = "_mp_maxid" },
53 #define	X_ALLCPU	3
54 	{ .n_name = "_all_cpus" },
55 	{ .n_name = "" },
56 };
57 
58 static void
59 usage(void)
60 {
61 
62 	fprintf(stderr, "umastat [-M core [-N system]]\n");
63 	exit(-1);
64 }
65 
66 static int
67 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
68     size_t offset)
69 {
70 	ssize_t ret;
71 
72 	ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
73 	    size);
74 	if (ret < 0)
75 		return (MEMSTAT_ERROR_KVM);
76 	if ((size_t)ret != size)
77 		return (MEMSTAT_ERROR_KVM_SHORTREAD);
78 	return (0);
79 }
80 
81 static int
82 kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen)
83 {
84 	ssize_t ret;
85 	int i;
86 
87 	for (i = 0; i < buflen; i++) {
88 		ret = kvm_read(kvm, (unsigned long)kvm_pointer + i,
89 		    &(buffer[i]), sizeof(char));
90 		if (ret < 0)
91 			return (MEMSTAT_ERROR_KVM);
92 		if ((size_t)ret != sizeof(char))
93 			return (MEMSTAT_ERROR_KVM_SHORTREAD);
94 		if (buffer[i] == '\0')
95 			return (0);
96 	}
97 	/* Truncate. */
98 	buffer[i-1] = '\0';
99 	return (0);
100 }
101 
102 static int
103 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
104     size_t offset)
105 {
106 	ssize_t ret;
107 
108 	ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
109 	if (ret < 0)
110 		return (MEMSTAT_ERROR_KVM);
111 	if ((size_t)ret != size)
112 		return (MEMSTAT_ERROR_KVM_SHORTREAD);
113 	return (0);
114 }
115 
116 static const struct flaginfo {
117 	u_int32_t	 fi_flag;
118 	const char	*fi_name;
119 } flaginfo[] = {
120 	{ UMA_ZFLAG_PRIVALLOC, "privalloc" },
121 	{ UMA_ZFLAG_INTERNAL, "internal" },
122 	{ UMA_ZFLAG_FULL, "full" },
123 	{ UMA_ZFLAG_CACHEONLY, "cacheonly" },
124 	{ UMA_ZONE_PAGEABLE, "pageable" },
125 	{ UMA_ZONE_ZINIT, "zinit" },
126 	{ UMA_ZONE_STATIC, "static" },
127 	{ UMA_ZONE_OFFPAGE, "offpage" },
128 	{ UMA_ZONE_MALLOC, "malloc" },
129 	{ UMA_ZONE_NOFREE, "nofree" },
130 	{ UMA_ZONE_MTXCLASS, "mtxclass" },
131 	{ UMA_ZONE_VM, "vm" },
132 	{ UMA_ZONE_HASH, "hash" },
133 	{ UMA_ZONE_SECONDARY, "secondary" },
134 	{ UMA_ZONE_REFCNT, "refcnt" },
135 	{ UMA_ZONE_MAXBUCKET, "maxbucket" },
136 };
137 static const int flaginfo_count = sizeof(flaginfo) / sizeof(struct flaginfo);
138 
139 static void
140 uma_print_keg_flags(struct uma_keg *ukp, const char *spaces)
141 {
142 	int count, i;
143 
144 	if (!ukp->uk_flags) {
145 		printf("%suk_flags = 0;\n", spaces);
146 		return;
147 	}
148 
149 	printf("%suk_flags = ", spaces);
150 	for (i = 0, count = 0; i < flaginfo_count; i++) {
151 		if (ukp->uk_flags & flaginfo[i].fi_flag) {
152 			if (count++ > 0)
153 				printf(" | ");
154 			printf(flaginfo[i].fi_name);
155 		}
156 
157 	}
158 	printf(";\n");
159 }
160 
161 static void
162 uma_print_keg_align(struct uma_keg *ukp, const char *spaces)
163 {
164 
165 	switch(ukp->uk_align) {
166 	case UMA_ALIGN_PTR:
167 		printf("%suk_align = UMA_ALIGN_PTR;\n", spaces);
168 		break;
169 
170 #if 0
171 	case UMA_ALIGN_LONG:
172 		printf("%suk_align = UMA_ALIGN_LONG;\n", spaces);
173 		break;
174 
175 	case UMA_ALIGN_INT:
176 		printf("%suk_align = UMA_ALIGN_INT;\n", spaces);
177 		break;
178 #endif
179 
180 	case UMA_ALIGN_SHORT:
181 		printf("%suk_align = UMA_ALIGN_SHORT;\n", spaces);
182 		break;
183 
184 	case UMA_ALIGN_CHAR:
185 		printf("%suk_align = UMA_ALIGN_CHAR;\n", spaces);
186 		break;
187 
188 	case UMA_ALIGN_CACHE:
189 		printf("%suk_align = UMA_ALIGN_CACHE;\n", spaces);
190 		break;
191 
192 	default:
193 		printf("%suk_align = %d\n", spaces, ukp->uk_align);
194 	}
195 }
196 
197 LIST_HEAD(bucketlist, uma_bucket);
198 
199 static void
200 uma_print_bucket(struct uma_bucket *ubp, const char *spaces __unused)
201 {
202 
203 	printf("{ ub_cnt = %d, ub_entries = %d }", ubp->ub_cnt,
204 	    ubp->ub_entries);
205 }
206 
207 static void
208 uma_print_bucketlist(kvm_t *kvm, struct bucketlist *bucketlist,
209     const char *name, const char *spaces)
210 {
211 	struct uma_bucket *ubp, ub;
212 	uint64_t total_entries, total_cnt;
213 	int count, ret;
214 
215 	printf("%s%s {", spaces, name);
216 
217 	total_entries = total_cnt = 0;
218 	count = 0;
219 	for (ubp = LIST_FIRST(bucketlist); ubp != NULL; ubp =
220 	    LIST_NEXT(&ub, ub_link)) {
221 		ret = kread(kvm, ubp, &ub, sizeof(ub), 0);
222 		if (ret != 0)
223 			errx(-1, "uma_print_bucketlist: %s", kvm_geterr(kvm));
224 		if (count % 2 == 0)
225 			printf("\n%s  ", spaces);
226 		uma_print_bucket(&ub, "");
227 		printf(" ");
228 		total_entries += ub.ub_entries;
229 		total_cnt += ub.ub_cnt;
230 		count++;
231 	}
232 
233 	printf("\n");
234 	printf("%s};  // total cnt %ju, total entries %ju\n", spaces,
235 	    total_cnt, total_entries);
236 }
237 
238 static void
239 uma_print_cache(kvm_t *kvm, struct uma_cache *cache, const char *name,
240     int cpu, const char *spaces, int *ub_cnt_add, int *ub_entries_add)
241 {
242 	struct uma_bucket ub;
243 	int ret;
244 
245 	printf("%s%s[%d] = {\n", spaces, name, cpu);
246 	printf("%s  uc_frees = %ju;\n", spaces, cache->uc_frees);
247 	printf("%s  uc_allocs = %ju;\n", spaces, cache->uc_allocs);
248 
249 	if (cache->uc_freebucket != NULL) {
250 		ret = kread(kvm, cache->uc_freebucket, &ub, sizeof(ub), 0);
251 		if (ret != 0)
252 			errx(-1, "uma_print_cache: %s", kvm_geterr(kvm));
253 		printf("%s  uc_freebucket ", spaces);
254 		uma_print_bucket(&ub, spaces);
255 		printf(";\n");
256 		if (ub_cnt_add != NULL)
257 			*ub_cnt_add += ub.ub_cnt;
258 		if (ub_entries_add != NULL)
259 			*ub_entries_add += ub.ub_entries;
260 	} else
261 		printf("%s  uc_freebucket = NULL;\n", spaces);
262 	if (cache->uc_allocbucket != NULL) {
263 		ret = kread(kvm, cache->uc_allocbucket, &ub, sizeof(ub), 0);
264 		if (ret != 0)
265 			errx(-1, "uma_print_cache: %s", kvm_geterr(kvm));
266 		printf("%s  uc_allocbucket ", spaces);
267 		uma_print_bucket(&ub, spaces);
268 		printf(";\n");
269 		if (ub_cnt_add != NULL)
270 			*ub_cnt_add += ub.ub_cnt;
271 		if (ub_entries_add != NULL)
272 			*ub_entries_add += ub.ub_entries;
273 	} else
274 		printf("%s  uc_allocbucket = NULL;\n", spaces);
275 	printf("%s};\n", spaces);
276 }
277 
278 int
279 main(int argc, char *argv[])
280 {
281 	LIST_HEAD(, uma_keg) uma_kegs;
282 	char name[MEMTYPE_MAXNAME];
283 	struct uma_keg *kzp, kz;
284 	struct uma_zone *uzp, *uzp_userspace;
285 	kvm_t *kvm;
286 	int all_cpus, cpu, mp_maxcpus, mp_maxid, ret, ub_cnt, ub_entries;
287 	size_t uzp_userspace_len;
288 	char *memf, *nlistf;
289 	int ch;
290 	char errbuf[_POSIX2_LINE_MAX];
291 
292 	memf = nlistf = NULL;
293 	while ((ch = getopt(argc, argv, "M:N:")) != -1) {
294 		switch (ch) {
295 		case 'M':
296 			memf = optarg;
297 			break;
298 		case 'N':
299 			nlistf = optarg;
300 			break;
301 		default:
302 			usage();
303 		}
304 	}
305 	argc -= optind;
306 	argv += optind;
307 
308 	if (argc != 0)
309 		usage();
310 	if (nlistf != NULL && memf == NULL)
311 		usage();
312 
313 	kvm = kvm_openfiles(nlistf, memf, NULL, 0, errbuf);
314 	if (kvm == NULL)
315 		errx(-1, "kvm_openfiles: %s", errbuf);
316 
317 	if (kvm_nlist(kvm, namelist) != 0)
318 		err(-1, "kvm_nlist");
319 
320 	if (namelist[X_UMA_KEGS].n_type == 0 ||
321 	    namelist[X_UMA_KEGS].n_value == 0)
322 		errx(-1, "kvm_nlist return");
323 
324 	ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus, sizeof(mp_maxcpus),
325 	    0);
326 	if (ret != 0)
327 		errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
328 
329 	printf("mp_maxcpus = %d\n", mp_maxcpus);
330 
331 	ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0);
332 	if (ret != 0)
333 		errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
334 
335 	printf("mp_maxid = %d\n", mp_maxid);
336 
337 	ret = kread_symbol(kvm, X_ALLCPU, &all_cpus, sizeof(all_cpus), 0);
338 	if (ret != 0)
339 		errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
340 
341 	printf("all_cpus = %x\n", all_cpus);
342 
343 	ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0);
344 	if (ret != 0)
345 		errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
346 
347 	/*
348 	 * uma_zone_t ends in an array of mp_maxid cache entries.  However,
349 	 * it is statically declared as an array of size 1, so we need to
350 	 * provide additional space.
351 	 */
352 	uzp_userspace_len = sizeof(struct uma_zone) + mp_maxid *
353 	    sizeof(struct uma_cache);
354 	uzp_userspace = malloc(uzp_userspace_len);
355 	if (uzp_userspace == NULL)
356 		err(-1, "malloc");
357 
358 	for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp =
359 	    LIST_NEXT(&kz, uk_link)) {
360 		ret = kread(kvm, kzp, &kz, sizeof(kz), 0);
361 		if (ret != 0) {
362 			free(uzp_userspace);
363 			errx(-1, "kread: %s", kvm_geterr(kvm));
364 		}
365 		printf("Keg {\n");
366 
367 		printf("  uk_recurse = %d\n", kz.uk_recurse);
368 		uma_print_keg_align(&kz, "  ");
369 		printf("  uk_pages = %d\n", kz.uk_pages);
370 		printf("  uk_free = %d\n", kz.uk_free);
371 		printf("  uk_size = %d\n", kz.uk_size);
372 		printf("  uk_rsize = %d\n", kz.uk_rsize);
373 		printf("  uk_maxpages = %d\n", kz.uk_maxpages);
374 
375 		printf("  uk_pgoff = %d\n", kz.uk_pgoff);
376 		printf("  uk_ppera = %d\n", kz.uk_ppera);
377 		printf("  uk_ipers = %d\n", kz.uk_ipers);
378 		uma_print_keg_flags(&kz, "  ");
379 
380 		if (LIST_FIRST(&kz.uk_zones) == NULL) {
381 			printf("; No zones.\n");
382 			printf("};\n");
383 			continue;
384 		}
385 		for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp =
386 		    LIST_NEXT(uzp_userspace, uz_link)) {
387 			/*
388 			 * We actually copy in twice: once with the base
389 			 * structure, so that we can then decide if we also
390 			 * need to copy in the caches.  This prevents us
391 			 * from reading past the end of the base UMA zones,
392 			 * which is unlikely to cause problems but could.
393 			 */
394 			ret = kread(kvm, uzp, uzp_userspace,
395 			    sizeof(struct uma_zone), 0);
396 			if (ret != 0) {
397 				free(uzp_userspace);
398 				errx(-1, "kread: %s", kvm_geterr(kvm));
399 			}
400 			if (!(kz.uk_flags & UMA_ZFLAG_INTERNAL)) {
401 				ret = kread(kvm, uzp, uzp_userspace,
402 				    uzp_userspace_len, 0);
403 				if (ret != 0) {
404 					free(uzp_userspace);
405 					errx(-1, "kread: %s",
406 					    kvm_geterr(kvm));
407 				}
408 			}
409 			ret = kread_string(kvm, uzp_userspace->uz_name, name,
410 			    MEMTYPE_MAXNAME);
411 			if (ret != 0) {
412 				free(uzp_userspace);
413 				errx(-1, "kread_string: %s", kvm_geterr(kvm));
414 			}
415 			printf("  Zone {\n");
416 			printf("    uz_name = \"%s\";\n", name);
417 			printf("    uz_allocs = %ju;\n",
418 			    uzp_userspace->uz_allocs);
419 			printf("    uz_frees = %ju;\n",
420 			    uzp_userspace->uz_frees);
421 			printf("    uz_fails = %ju;\n",
422 			    uzp_userspace->uz_fails);
423 			printf("    uz_fills = %u;\n",
424 			    uzp_userspace->uz_fills);
425 			printf("    uz_count = %u;\n",
426 			    uzp_userspace->uz_count);
427 			uma_print_bucketlist(kvm, (void *)
428 			    &uzp_userspace->uz_full_bucket, "uz_full_bucket",
429 			    "    ");
430 			uma_print_bucketlist(kvm, (void *)
431 			    &uzp_userspace->uz_free_bucket, "uz_free_bucket",
432 			    "    ");
433 
434 			if (!(kz.uk_flags & UMA_ZFLAG_INTERNAL)) {
435 				ub_cnt = ub_entries = 0;
436 				for (cpu = 0; cpu <= mp_maxid; cpu++) {
437 					/* if (CPU_ABSENT(cpu)) */
438 					if ((all_cpus & (1 << cpu)) == 0)
439 						continue;
440 					uma_print_cache(kvm,
441 					    &uzp_userspace->uz_cpu[cpu],
442 					    "uc_cache", cpu, "    ", &ub_cnt,
443 					    &ub_entries);
444 				}
445 				printf("    // %d cache total cnt, %d total "
446 				    "entries\n", ub_cnt, ub_entries);
447 			}
448 
449 			printf("  };\n");
450 		}
451 		printf("};\n");
452 	}
453 
454 	free(uzp_userspace);
455 	return (0);
456 }
457