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: stable/10/tools/tools/umastat/umastat.c 316835 2017-04-14 14:11:59Z avg $ 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 46static 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 58static void 59usage(void) 60{ 61 62 fprintf(stderr, "umastat [-M core [-N system]]\n"); 63 exit(-1); 64} 65 66static int 67kread(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 81static int 82kread_string(kvm_t *kvm, const 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 102static int 103kread_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 116static const struct flaginfo { 117 u_int32_t fi_flag; 118 const char *fi_name; 119} flaginfo[] = { 120 { UMA_ZFLAG_MULTI, "multi" }, 121 { UMA_ZFLAG_DRAINING, "draining" }, 122 { UMA_ZFLAG_BUCKET, "bucket" }, 123 { UMA_ZFLAG_INTERNAL, "internal" }, 124 { UMA_ZFLAG_FULL, "full" }, 125 { UMA_ZFLAG_CACHEONLY, "cacheonly" }, 126 { UMA_ZONE_PAGEABLE, "pageable" }, 127 { UMA_ZONE_ZINIT, "zinit" }, 128 { UMA_ZONE_STATIC, "static" }, 129 { UMA_ZONE_OFFPAGE, "offpage" }, 130 { UMA_ZONE_MALLOC, "malloc" }, 131 { UMA_ZONE_NOFREE, "nofree" }, 132 { UMA_ZONE_MTXCLASS, "mtxclass" }, 133 { UMA_ZONE_VM, "vm" }, 134 { UMA_ZONE_HASH, "hash" }, 135 { UMA_ZONE_SECONDARY, "secondary" }, 136 { UMA_ZONE_REFCNT, "refcnt" }, 137 { UMA_ZONE_MAXBUCKET, "maxbucket" }, 138 { UMA_ZONE_CACHESPREAD, "cachespread" }, 139 { UMA_ZONE_VTOSLAB, "vtoslab" }, 140 { UMA_ZONE_NODUMP, "nodump" }, 141 { UMA_ZONE_PCPU, "pcpu" }, 142}; 143static const int flaginfo_count = sizeof(flaginfo) / sizeof(struct flaginfo); 144 145static void 146uma_print_keg_flags(struct uma_keg *ukp, const char *spaces) 147{ 148 int count, i; 149 150 if (!ukp->uk_flags) { 151 printf("%suk_flags = 0;\n", spaces); 152 return; 153 } 154 155 printf("%suk_flags = ", spaces); 156 for (i = 0, count = 0; i < flaginfo_count; i++) { 157 if (ukp->uk_flags & flaginfo[i].fi_flag) { 158 if (count++ > 0) 159 printf(" | "); 160 printf("%s", flaginfo[i].fi_name); 161 } 162 163 } 164 printf(";\n"); 165} 166 167static void 168uma_print_keg_align(struct uma_keg *ukp, const char *spaces) 169{ 170 171 switch(ukp->uk_align) { 172 case UMA_ALIGN_PTR: 173 printf("%suk_align = UMA_ALIGN_PTR;\n", spaces); 174 break; 175 176#if 0 177 case UMA_ALIGN_LONG: 178 printf("%suk_align = UMA_ALIGN_LONG;\n", spaces); 179 break; 180 181 case UMA_ALIGN_INT: 182 printf("%suk_align = UMA_ALIGN_INT;\n", spaces); 183 break; 184#endif 185 186 case UMA_ALIGN_SHORT: 187 printf("%suk_align = UMA_ALIGN_SHORT;\n", spaces); 188 break; 189 190 case UMA_ALIGN_CHAR: 191 printf("%suk_align = UMA_ALIGN_CHAR;\n", spaces); 192 break; 193 194 case UMA_ALIGN_CACHE: 195 printf("%suk_align = UMA_ALIGN_CACHE;\n", spaces); 196 break; 197 198 default: 199 printf("%suk_align = %d\n", spaces, ukp->uk_align); 200 } 201} 202 203LIST_HEAD(bucketlist, uma_bucket); 204 205static void 206uma_print_bucket(struct uma_bucket *ubp, const char *spaces __unused) 207{ 208 209 printf("{ ub_cnt = %d, ub_entries = %d }", ubp->ub_cnt, 210 ubp->ub_entries); 211} 212 213static void 214uma_print_bucketlist(kvm_t *kvm, struct bucketlist *bucketlist, 215 const char *name, const char *spaces) 216{ 217 struct uma_bucket *ubp, ub; 218 uint64_t total_entries, total_cnt; 219 int count, ret; 220 221 printf("%s%s {", spaces, name); 222 223 total_entries = total_cnt = 0; 224 count = 0; 225 for (ubp = LIST_FIRST(bucketlist); ubp != NULL; ubp = 226 LIST_NEXT(&ub, ub_link)) { 227 ret = kread(kvm, ubp, &ub, sizeof(ub), 0); 228 if (ret != 0) 229 errx(-1, "uma_print_bucketlist: %s", kvm_geterr(kvm)); 230 if (count % 2 == 0) 231 printf("\n%s ", spaces); 232 uma_print_bucket(&ub, ""); 233 printf(" "); 234 total_entries += ub.ub_entries; 235 total_cnt += ub.ub_cnt; 236 count++; 237 } 238 239 printf("\n"); 240 printf("%s}; // total cnt %ju, total entries %ju\n", spaces, 241 total_cnt, total_entries); 242} 243 244static void 245uma_print_cache(kvm_t *kvm, struct uma_cache *cache, const char *name, 246 int cpu, const char *spaces, int *ub_cnt_add, int *ub_entries_add) 247{ 248 struct uma_bucket ub; 249 int ret; 250 251 printf("%s%s[%d] = {\n", spaces, name, cpu); 252 printf("%s uc_frees = %ju;\n", spaces, cache->uc_frees); 253 printf("%s uc_allocs = %ju;\n", spaces, cache->uc_allocs); 254 255 if (cache->uc_freebucket != NULL) { 256 ret = kread(kvm, cache->uc_freebucket, &ub, sizeof(ub), 0); 257 if (ret != 0) 258 errx(-1, "uma_print_cache: %s", kvm_geterr(kvm)); 259 printf("%s uc_freebucket ", spaces); 260 uma_print_bucket(&ub, spaces); 261 printf(";\n"); 262 if (ub_cnt_add != NULL) 263 *ub_cnt_add += ub.ub_cnt; 264 if (ub_entries_add != NULL) 265 *ub_entries_add += ub.ub_entries; 266 } else 267 printf("%s uc_freebucket = NULL;\n", spaces); 268 if (cache->uc_allocbucket != NULL) { 269 ret = kread(kvm, cache->uc_allocbucket, &ub, sizeof(ub), 0); 270 if (ret != 0) 271 errx(-1, "uma_print_cache: %s", kvm_geterr(kvm)); 272 printf("%s uc_allocbucket ", spaces); 273 uma_print_bucket(&ub, spaces); 274 printf(";\n"); 275 if (ub_cnt_add != NULL) 276 *ub_cnt_add += ub.ub_cnt; 277 if (ub_entries_add != NULL) 278 *ub_entries_add += ub.ub_entries; 279 } else 280 printf("%s uc_allocbucket = NULL;\n", spaces); 281 printf("%s};\n", spaces); 282} 283 284int 285main(int argc, char *argv[]) 286{ 287 LIST_HEAD(, uma_keg) uma_kegs; 288 char name[MEMTYPE_MAXNAME]; 289 struct uma_keg *kzp, kz; 290 struct uma_zone *uzp, *uzp_userspace; 291 kvm_t *kvm; 292 int all_cpus, cpu, mp_maxcpus, mp_maxid, ret, ub_cnt, ub_entries; 293 size_t uzp_userspace_len; 294 char *memf, *nlistf; 295 int ch; 296 char errbuf[_POSIX2_LINE_MAX]; 297 298 memf = nlistf = NULL; 299 while ((ch = getopt(argc, argv, "M:N:")) != -1) { 300 switch (ch) { 301 case 'M': 302 memf = optarg; 303 break; 304 case 'N': 305 nlistf = optarg; 306 break; 307 default: 308 usage(); 309 } 310 } 311 argc -= optind; 312 argv += optind; 313 314 if (argc != 0) 315 usage(); 316 if (nlistf != NULL && memf == NULL) 317 usage(); 318 319 kvm = kvm_openfiles(nlistf, memf, NULL, 0, errbuf); 320 if (kvm == NULL) 321 errx(-1, "kvm_openfiles: %s", errbuf); 322 323 if (kvm_nlist(kvm, namelist) != 0) 324 err(-1, "kvm_nlist"); 325 326 if (namelist[X_UMA_KEGS].n_type == 0 || 327 namelist[X_UMA_KEGS].n_value == 0) 328 errx(-1, "kvm_nlist return"); 329 330 ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus, sizeof(mp_maxcpus), 331 0); 332 if (ret != 0) 333 errx(-1, "kread_symbol: %s", kvm_geterr(kvm)); 334 335 printf("mp_maxcpus = %d\n", mp_maxcpus); 336 337 ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0); 338 if (ret != 0) 339 errx(-1, "kread_symbol: %s", kvm_geterr(kvm)); 340 341 printf("mp_maxid = %d\n", mp_maxid); 342 343 ret = kread_symbol(kvm, X_ALLCPU, &all_cpus, sizeof(all_cpus), 0); 344 if (ret != 0) 345 errx(-1, "kread_symbol: %s", kvm_geterr(kvm)); 346 347 printf("all_cpus = %x\n", all_cpus); 348 349 ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0); 350 if (ret != 0) 351 errx(-1, "kread_symbol: %s", kvm_geterr(kvm)); 352 353 /* 354 * uma_zone_t ends in an array of mp_maxid cache entries. However, 355 * it is statically declared as an array of size 1, so we need to 356 * provide additional space. 357 */ 358 uzp_userspace_len = sizeof(struct uma_zone) + mp_maxid * 359 sizeof(struct uma_cache); 360 uzp_userspace = malloc(uzp_userspace_len); 361 if (uzp_userspace == NULL) 362 err(-1, "malloc"); 363 364 for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp = 365 LIST_NEXT(&kz, uk_link)) { 366 ret = kread(kvm, kzp, &kz, sizeof(kz), 0); 367 if (ret != 0) { 368 free(uzp_userspace); 369 errx(-1, "kread: %s", kvm_geterr(kvm)); 370 } 371 printf("Keg {\n"); 372 373 uma_print_keg_align(&kz, " "); 374 printf(" uk_pages = %d\n", kz.uk_pages); 375 printf(" uk_free = %d\n", kz.uk_free); 376 printf(" uk_reserve = %d\n", kz.uk_reserve); 377 printf(" uk_size = %d\n", kz.uk_size); 378 printf(" uk_rsize = %d\n", kz.uk_rsize); 379 printf(" uk_maxpages = %d\n", kz.uk_maxpages); 380 381 printf(" uk_pgoff = %d\n", kz.uk_pgoff); 382 printf(" uk_ppera = %d\n", kz.uk_ppera); 383 printf(" uk_ipers = %d\n", kz.uk_ipers); 384 uma_print_keg_flags(&kz, " "); 385 386 if (LIST_FIRST(&kz.uk_zones) == NULL) { 387 printf("; No zones.\n"); 388 printf("};\n"); 389 continue; 390 } 391 for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp = 392 LIST_NEXT(uzp_userspace, uz_link)) { 393 /* 394 * We actually copy in twice: once with the base 395 * structure, so that we can then decide if we also 396 * need to copy in the caches. This prevents us 397 * from reading past the end of the base UMA zones, 398 * which is unlikely to cause problems but could. 399 */ 400 ret = kread(kvm, uzp, uzp_userspace, 401 sizeof(struct uma_zone), 0); 402 if (ret != 0) { 403 free(uzp_userspace); 404 errx(-1, "kread: %s", kvm_geterr(kvm)); 405 } 406 if (!(kz.uk_flags & UMA_ZFLAG_INTERNAL)) { 407 ret = kread(kvm, uzp, uzp_userspace, 408 uzp_userspace_len, 0); 409 if (ret != 0) { 410 free(uzp_userspace); 411 errx(-1, "kread: %s", 412 kvm_geterr(kvm)); 413 } 414 } 415 ret = kread_string(kvm, uzp_userspace->uz_name, name, 416 MEMTYPE_MAXNAME); 417 if (ret != 0) { 418 free(uzp_userspace); 419 errx(-1, "kread_string: %s", kvm_geterr(kvm)); 420 } 421 printf(" Zone {\n"); 422 printf(" uz_name = \"%s\";\n", name); 423 printf(" uz_allocs = %lu;\n", 424 uzp_userspace->uz_allocs); 425 printf(" uz_frees = %lu;\n", 426 uzp_userspace->uz_frees); 427 printf(" uz_fails = %lu;\n", 428 uzp_userspace->uz_fails); 429 printf(" uz_sleeps = %ju;\n", 430 uzp_userspace->uz_sleeps); 431 printf(" uz_count = %u;\n", 432 uzp_userspace->uz_count); 433 uma_print_bucketlist(kvm, (void *) 434 &uzp_userspace->uz_buckets, "uz_buckets", 435 " "); 436 437 if (!(kz.uk_flags & UMA_ZFLAG_INTERNAL)) { 438 ub_cnt = ub_entries = 0; 439 for (cpu = 0; cpu <= mp_maxid; cpu++) { 440 /* if (CPU_ABSENT(cpu)) */ 441 if ((all_cpus & (1 << cpu)) == 0) 442 continue; 443 uma_print_cache(kvm, 444 &uzp_userspace->uz_cpu[cpu], 445 "uc_cache", cpu, " ", &ub_cnt, 446 &ub_entries); 447 } 448 printf(" // %d cache total cnt, %d total " 449 "entries\n", ub_cnt, ub_entries); 450 } 451 452 printf(" };\n"); 453 } 454 printf("};\n"); 455 } 456 457 free(uzp_userspace); 458 return (0); 459} 460