2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
31 #define LIBMEMSTAT /* Cause vm_page.h not to include opt_vmpage.h */
33 #include <vm/vm_page.h>
36 #include <vm/uma_int.h>
46 static struct nlist namelist[] = {
48 { .n_name = "_uma_kegs" },
49 #define X_MP_MAXCPUS 1
50 { .n_name = "_mp_maxcpus" },
52 { .n_name = "_mp_maxid" },
54 { .n_name = "_all_cpus" },
62 fprintf(stderr, "umastat [-M core [-N system]]\n");
67 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
72 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
75 return (MEMSTAT_ERROR_KVM);
76 if ((size_t)ret != size)
77 return (MEMSTAT_ERROR_KVM_SHORTREAD);
82 kread_string(kvm_t *kvm, const void *kvm_pointer, char *buffer, int buflen)
87 for (i = 0; i < buflen; i++) {
88 ret = kvm_read(kvm, (unsigned long)kvm_pointer + i,
89 &(buffer[i]), sizeof(char));
91 return (MEMSTAT_ERROR_KVM);
92 if ((size_t)ret != sizeof(char))
93 return (MEMSTAT_ERROR_KVM_SHORTREAD);
94 if (buffer[i] == '\0')
103 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
108 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
110 return (MEMSTAT_ERROR_KVM);
111 if ((size_t)ret != size)
112 return (MEMSTAT_ERROR_KVM_SHORTREAD);
116 static const struct 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" },
143 static const int flaginfo_count = sizeof(flaginfo) / sizeof(struct flaginfo);
146 uma_print_keg_flags(struct uma_keg *ukp, const char *spaces)
150 if (!ukp->uk_flags) {
151 printf("%suk_flags = 0;\n", spaces);
155 printf("%suk_flags = ", spaces);
156 for (i = 0, count = 0; i < flaginfo_count; i++) {
157 if (ukp->uk_flags & flaginfo[i].fi_flag) {
160 printf("%s", flaginfo[i].fi_name);
168 uma_print_keg_align(struct uma_keg *ukp, const char *spaces)
171 switch(ukp->uk_align) {
173 printf("%suk_align = UMA_ALIGN_PTR;\n", spaces);
178 printf("%suk_align = UMA_ALIGN_LONG;\n", spaces);
182 printf("%suk_align = UMA_ALIGN_INT;\n", spaces);
186 case UMA_ALIGN_SHORT:
187 printf("%suk_align = UMA_ALIGN_SHORT;\n", spaces);
191 printf("%suk_align = UMA_ALIGN_CHAR;\n", spaces);
194 case UMA_ALIGN_CACHE:
195 printf("%suk_align = UMA_ALIGN_CACHE;\n", spaces);
199 printf("%suk_align = %d\n", spaces, ukp->uk_align);
203 LIST_HEAD(bucketlist, uma_bucket);
206 uma_print_bucket(struct uma_bucket *ubp, const char *spaces __unused)
209 printf("{ ub_cnt = %d, ub_entries = %d }", ubp->ub_cnt,
214 uma_print_bucketlist(kvm_t *kvm, struct bucketlist *bucketlist,
215 const char *name, const char *spaces)
217 struct uma_bucket *ubp, ub;
218 uint64_t total_entries, total_cnt;
221 printf("%s%s {", spaces, name);
223 total_entries = total_cnt = 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);
229 errx(-1, "uma_print_bucketlist: %s", kvm_geterr(kvm));
231 printf("\n%s ", spaces);
232 uma_print_bucket(&ub, "");
234 total_entries += ub.ub_entries;
235 total_cnt += ub.ub_cnt;
240 printf("%s}; // total cnt %ju, total entries %ju\n", spaces,
241 total_cnt, total_entries);
245 uma_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)
248 struct uma_bucket ub;
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);
255 if (cache->uc_freebucket != NULL) {
256 ret = kread(kvm, cache->uc_freebucket, &ub, sizeof(ub), 0);
258 errx(-1, "uma_print_cache: %s", kvm_geterr(kvm));
259 printf("%s uc_freebucket ", spaces);
260 uma_print_bucket(&ub, spaces);
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;
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);
271 errx(-1, "uma_print_cache: %s", kvm_geterr(kvm));
272 printf("%s uc_allocbucket ", spaces);
273 uma_print_bucket(&ub, spaces);
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;
280 printf("%s uc_allocbucket = NULL;\n", spaces);
281 printf("%s};\n", spaces);
285 main(int argc, char *argv[])
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;
292 int all_cpus, cpu, mp_maxcpus, mp_maxid, ret, ub_cnt, ub_entries;
293 size_t uzp_userspace_len;
296 char errbuf[_POSIX2_LINE_MAX];
298 memf = nlistf = NULL;
299 while ((ch = getopt(argc, argv, "M:N:")) != -1) {
316 if (nlistf != NULL && memf == NULL)
319 kvm = kvm_openfiles(nlistf, memf, NULL, 0, errbuf);
321 errx(-1, "kvm_openfiles: %s", errbuf);
323 if (kvm_nlist(kvm, namelist) != 0)
324 err(-1, "kvm_nlist");
326 if (namelist[X_UMA_KEGS].n_type == 0 ||
327 namelist[X_UMA_KEGS].n_value == 0)
328 errx(-1, "kvm_nlist return");
330 ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus, sizeof(mp_maxcpus),
333 errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
335 printf("mp_maxcpus = %d\n", mp_maxcpus);
337 ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0);
339 errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
341 printf("mp_maxid = %d\n", mp_maxid);
343 ret = kread_symbol(kvm, X_ALLCPU, &all_cpus, sizeof(all_cpus), 0);
345 errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
347 printf("all_cpus = %x\n", all_cpus);
349 ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0);
351 errx(-1, "kread_symbol: %s", kvm_geterr(kvm));
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.
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)
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);
369 errx(-1, "kread: %s", kvm_geterr(kvm));
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);
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, " ");
386 if (LIST_FIRST(&kz.uk_zones) == NULL) {
387 printf("; No zones.\n");
391 for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp =
392 LIST_NEXT(uzp_userspace, uz_link)) {
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.
400 ret = kread(kvm, uzp, uzp_userspace,
401 sizeof(struct uma_zone), 0);
404 errx(-1, "kread: %s", kvm_geterr(kvm));
406 if (!(kz.uk_flags & UMA_ZFLAG_INTERNAL)) {
407 ret = kread(kvm, uzp, uzp_userspace,
408 uzp_userspace_len, 0);
411 errx(-1, "kread: %s",
415 ret = kread_string(kvm, uzp_userspace->uz_name, name,
419 errx(-1, "kread_string: %s", kvm_geterr(kvm));
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",
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)
444 &uzp_userspace->uz_cpu[cpu],
445 "uc_cache", cpu, " ", &ub_cnt,
448 printf(" // %d cache total cnt, %d total "
449 "entries\n", ub_cnt, ub_entries);