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