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