2 * Copyright (c) 2005-2006 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>
30 #include <sys/sysctl.h>
32 #define LIBMEMSTAT /* Cause vm_page.h not to include opt_vmpage.h */
34 #include <vm/vm_page.h>
37 #include <vm/uma_int.h>
49 #include "memstat_internal.h"
51 static struct nlist namelist[] = {
53 { .n_name = "_uma_kegs" },
55 { .n_name = "_mp_maxid" },
57 { .n_name = "_all_cpus" },
62 * Extract uma(9) statistics from the running kernel, and store all memory
63 * type information in the passed list. For each type, check the list for an
64 * existing entry with the right name/allocator -- if present, update that
65 * entry. Otherwise, add a new entry. On error, the entire list will be
66 * cleared, as entries will be in an inconsistent state.
68 * To reduce the level of work for a list that starts empty, we keep around a
69 * hint as to whether it was empty when we began, so we can avoid searching
70 * the list for entries to update. Updates are O(n^2) due to searching for
71 * each entry before adding it.
74 memstat_sysctl_uma(struct memory_type_list *list, int flags)
76 struct uma_stream_header *ushp;
77 struct uma_type_header *uthp;
78 struct uma_percpu_stat *upsp;
79 struct memory_type *mtp;
80 int count, hint_dontsearch, i, j, maxcpus;
84 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
87 * Query the number of CPUs, number of malloc types so that we can
88 * guess an initial buffer size. We loop until we succeed or really
89 * fail. Note that the value of maxcpus we query using sysctl is not
90 * the version we use when processing the real data -- that is read
94 size = sizeof(maxcpus);
95 if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
96 if (errno == EACCES || errno == EPERM)
97 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
99 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
102 if (size != sizeof(maxcpus)) {
103 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
107 if (maxcpus > MEMSTAT_MAXCPU) {
108 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
112 size = sizeof(count);
113 if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) {
114 if (errno == EACCES || errno == EPERM)
115 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
117 list->mtl_error = MEMSTAT_ERROR_VERSION;
120 if (size != sizeof(count)) {
121 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
125 size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) *
128 buffer = malloc(size);
129 if (buffer == NULL) {
130 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
134 if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) {
136 * XXXRW: ENOMEM is an ambiguous return, we should bound the
137 * number of loops, perhaps.
139 if (errno == ENOMEM) {
143 if (errno == EACCES || errno == EPERM)
144 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
146 list->mtl_error = MEMSTAT_ERROR_VERSION;
156 if (size < sizeof(*ushp)) {
157 list->mtl_error = MEMSTAT_ERROR_VERSION;
162 ushp = (struct uma_stream_header *)p;
165 if (ushp->ush_version != UMA_STREAM_VERSION) {
166 list->mtl_error = MEMSTAT_ERROR_VERSION;
171 if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) {
172 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
178 * For the remainder of this function, we are quite trusting about
179 * the layout of structures and sizes, since we've determined we have
180 * a matching version and acceptable CPU count.
182 maxcpus = ushp->ush_maxcpus;
183 count = ushp->ush_count;
184 for (i = 0; i < count; i++) {
185 uthp = (struct uma_type_header *)p;
188 if (hint_dontsearch == 0) {
189 mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
194 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
197 _memstat_mtl_empty(list);
199 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
204 * Reset the statistics on a current node.
206 _memstat_mt_reset_stats(mtp);
208 mtp->mt_numallocs = uthp->uth_allocs;
209 mtp->mt_numfrees = uthp->uth_frees;
210 mtp->mt_failures = uthp->uth_fails;
212 for (j = 0; j < maxcpus; j++) {
213 upsp = (struct uma_percpu_stat *)p;
216 mtp->mt_percpu_cache[j].mtp_free =
217 upsp->ups_cache_free;
218 mtp->mt_free += upsp->ups_cache_free;
219 mtp->mt_numallocs += upsp->ups_allocs;
220 mtp->mt_numfrees += upsp->ups_frees;
223 mtp->mt_size = uthp->uth_size;
224 mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size;
225 mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size;
226 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
227 mtp->mt_countlimit = uthp->uth_limit;
228 mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size;
230 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
231 mtp->mt_zonefree = uthp->uth_zone_free;
234 * UMA secondary zones share a keg with the primary zone. To
235 * avoid double-reporting of free items, report keg free
236 * items only in the primary zone.
238 if (!(uthp->uth_zone_flags & UTH_ZONE_SECONDARY)) {
239 mtp->mt_kegfree = uthp->uth_keg_free;
240 mtp->mt_free += mtp->mt_kegfree;
242 mtp->mt_free += mtp->mt_zonefree;
251 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
256 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
259 return (MEMSTAT_ERROR_KVM);
260 if ((size_t)ret != size)
261 return (MEMSTAT_ERROR_KVM_SHORTREAD);
266 kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen)
271 for (i = 0; i < buflen; i++) {
272 ret = kvm_read(kvm, (unsigned long)kvm_pointer + i,
273 &(buffer[i]), sizeof(char));
275 return (MEMSTAT_ERROR_KVM);
276 if ((size_t)ret != sizeof(char))
277 return (MEMSTAT_ERROR_KVM_SHORTREAD);
278 if (buffer[i] == '\0')
287 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
292 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
294 return (MEMSTAT_ERROR_KVM);
295 if ((size_t)ret != size)
296 return (MEMSTAT_ERROR_KVM_SHORTREAD);
301 * memstat_kvm_uma() is similar to memstat_sysctl_uma(), only it extracts
302 * UMA(9) statistics from a kernel core/memory file.
305 memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle)
307 LIST_HEAD(, uma_keg) uma_kegs;
308 struct memory_type *mtp;
309 struct uma_bucket *ubp, ub;
310 struct uma_cache *ucp, *ucp_array;
311 struct uma_zone *uzp, uz;
312 struct uma_keg *kzp, kz;
313 int hint_dontsearch, i, mp_maxid, ret;
314 char name[MEMTYPE_MAXNAME];
315 __cpumask_t all_cpus;
318 kvm = (kvm_t *)kvm_handle;
319 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
320 if (kvm_nlist(kvm, namelist) != 0) {
321 list->mtl_error = MEMSTAT_ERROR_KVM;
324 if (namelist[X_UMA_KEGS].n_type == 0 ||
325 namelist[X_UMA_KEGS].n_value == 0) {
326 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL;
329 ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0);
331 list->mtl_error = ret;
334 ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0);
336 list->mtl_error = ret;
339 ret = kread_symbol(kvm, X_ALL_CPUS, &all_cpus, sizeof(all_cpus), 0);
341 list->mtl_error = ret;
344 ucp_array = malloc(sizeof(struct uma_cache) * (mp_maxid + 1));
345 if (ucp_array == NULL) {
346 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
349 for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp =
350 LIST_NEXT(&kz, uk_link)) {
351 ret = kread(kvm, kzp, &kz, sizeof(kz), 0);
354 _memstat_mtl_empty(list);
355 list->mtl_error = ret;
358 for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp =
359 LIST_NEXT(&uz, uz_link)) {
360 ret = kread(kvm, uzp, &uz, sizeof(uz), 0);
363 _memstat_mtl_empty(list);
364 list->mtl_error = ret;
367 ret = kread(kvm, uzp, ucp_array,
368 sizeof(struct uma_cache) * (mp_maxid + 1),
369 offsetof(struct uma_zone, uz_cpu[0]));
372 _memstat_mtl_empty(list);
373 list->mtl_error = ret;
376 ret = kread_string(kvm, uz.uz_name, name,
380 _memstat_mtl_empty(list);
381 list->mtl_error = ret;
384 if (hint_dontsearch == 0) {
385 mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
390 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
394 _memstat_mtl_empty(list);
395 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
399 * Reset the statistics on a current node.
401 _memstat_mt_reset_stats(mtp);
402 mtp->mt_numallocs = uz.uz_allocs;
403 mtp->mt_numfrees = uz.uz_frees;
404 mtp->mt_failures = uz.uz_fails;
405 if (kz.uk_flags & UMA_ZFLAG_INTERNAL)
407 for (i = 0; i < mp_maxid + 1; i++) {
408 if ((all_cpus & (1 << i)) == 0)
411 mtp->mt_numallocs += ucp->uc_allocs;
412 mtp->mt_numfrees += ucp->uc_frees;
414 if (ucp->uc_allocbucket != NULL) {
415 ret = kread(kvm, ucp->uc_allocbucket,
419 _memstat_mtl_empty(list);
420 list->mtl_error = ret;
423 mtp->mt_free += ub.ub_cnt;
425 if (ucp->uc_freebucket != NULL) {
426 ret = kread(kvm, ucp->uc_freebucket,
430 _memstat_mtl_empty(list);
431 list->mtl_error = ret;
434 mtp->mt_free += ub.ub_cnt;
438 mtp->mt_size = kz.uk_size;
439 mtp->mt_memalloced = mtp->mt_numallocs * mtp->mt_size;
440 mtp->mt_memfreed = mtp->mt_numfrees * mtp->mt_size;
441 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
443 mtp->mt_countlimit = kz.uk_maxpages /
446 mtp->mt_countlimit = kz.uk_maxpages *
448 mtp->mt_byteslimit = mtp->mt_countlimit * mtp->mt_size;
449 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
450 for (ubp = LIST_FIRST(&uz.uz_full_bucket); ubp !=
451 NULL; ubp = LIST_NEXT(&ub, ub_link)) {
452 ret = kread(kvm, ubp, &ub, sizeof(ub), 0);
453 mtp->mt_zonefree += ub.ub_cnt;
455 if (!((kz.uk_flags & UMA_ZONE_SECONDARY) &&
456 LIST_FIRST(&kz.uk_zones) != uzp)) {
457 mtp->mt_kegfree = kz.uk_free;
458 mtp->mt_free += mtp->mt_kegfree;
460 mtp->mt_free += mtp->mt_zonefree;