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/cpuset.h>
31 #include <sys/sysctl.h>
34 #include <vm/vm_page.h>
37 #include <vm/uma_int.h>
50 #include "memstat_internal.h"
52 static struct nlist namelist[] = {
54 { .n_name = "_uma_kegs" },
56 { .n_name = "_mp_maxid" },
58 { .n_name = "_all_cpus" },
63 * Extract uma(9) statistics from the running kernel, and store all memory
64 * type information in the passed list. For each type, check the list for an
65 * existing entry with the right name/allocator -- if present, update that
66 * entry. Otherwise, add a new entry. On error, the entire list will be
67 * cleared, as entries will be in an inconsistent state.
69 * To reduce the level of work for a list that starts empty, we keep around a
70 * hint as to whether it was empty when we began, so we can avoid searching
71 * the list for entries to update. Updates are O(n^2) due to searching for
72 * each entry before adding it.
75 memstat_sysctl_uma(struct memory_type_list *list, int flags)
77 struct uma_stream_header *ushp;
78 struct uma_type_header *uthp;
79 struct uma_percpu_stat *upsp;
80 struct memory_type *mtp;
81 int count, hint_dontsearch, i, j, maxcpus, maxid;
85 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
88 * Query the number of CPUs, number of malloc types so that we can
89 * guess an initial buffer size. We loop until we succeed or really
90 * fail. Note that the value of maxcpus we query using sysctl is not
91 * the version we use when processing the real data -- that is read
96 if (sysctlbyname("kern.smp.maxid", &maxid, &size, NULL, 0) < 0) {
97 if (errno == EACCES || errno == EPERM)
98 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
100 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
103 if (size != sizeof(maxid)) {
104 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
108 size = sizeof(count);
109 if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) {
110 if (errno == EACCES || errno == EPERM)
111 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
113 list->mtl_error = MEMSTAT_ERROR_VERSION;
116 if (size != sizeof(count)) {
117 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
121 size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) *
124 buffer = malloc(size);
125 if (buffer == NULL) {
126 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
130 if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) {
132 * XXXRW: ENOMEM is an ambiguous return, we should bound the
133 * number of loops, perhaps.
135 if (errno == ENOMEM) {
139 if (errno == EACCES || errno == EPERM)
140 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
142 list->mtl_error = MEMSTAT_ERROR_VERSION;
152 if (size < sizeof(*ushp)) {
153 list->mtl_error = MEMSTAT_ERROR_VERSION;
158 ushp = (struct uma_stream_header *)p;
161 if (ushp->ush_version != UMA_STREAM_VERSION) {
162 list->mtl_error = MEMSTAT_ERROR_VERSION;
168 * For the remainder of this function, we are quite trusting about
169 * the layout of structures and sizes, since we've determined we have
170 * a matching version and acceptable CPU count.
172 maxcpus = ushp->ush_maxcpus;
173 count = ushp->ush_count;
174 for (i = 0; i < count; i++) {
175 uthp = (struct uma_type_header *)p;
178 if (hint_dontsearch == 0) {
179 mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
184 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
185 uthp->uth_name, maxid + 1);
187 _memstat_mtl_empty(list);
189 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
194 * Reset the statistics on a current node.
196 _memstat_mt_reset_stats(mtp, maxid + 1);
198 mtp->mt_numallocs = uthp->uth_allocs;
199 mtp->mt_numfrees = uthp->uth_frees;
200 mtp->mt_failures = uthp->uth_fails;
201 mtp->mt_sleeps = uthp->uth_sleeps;
203 for (j = 0; j < maxcpus; j++) {
204 upsp = (struct uma_percpu_stat *)p;
207 mtp->mt_percpu_cache[j].mtp_free =
208 upsp->ups_cache_free;
209 mtp->mt_free += upsp->ups_cache_free;
210 mtp->mt_numallocs += upsp->ups_allocs;
211 mtp->mt_numfrees += upsp->ups_frees;
214 mtp->mt_size = uthp->uth_size;
215 mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size;
216 mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size;
217 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
218 mtp->mt_countlimit = uthp->uth_limit;
219 mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size;
221 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
222 mtp->mt_zonefree = uthp->uth_zone_free;
225 * UMA secondary zones share a keg with the primary zone. To
226 * avoid double-reporting of free items, report keg free
227 * items only in the primary zone.
229 if (!(uthp->uth_zone_flags & UTH_ZONE_SECONDARY)) {
230 mtp->mt_kegfree = uthp->uth_keg_free;
231 mtp->mt_free += mtp->mt_kegfree;
233 mtp->mt_free += mtp->mt_zonefree;
242 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
247 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
250 return (MEMSTAT_ERROR_KVM);
251 if ((size_t)ret != size)
252 return (MEMSTAT_ERROR_KVM_SHORTREAD);
257 kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen)
262 for (i = 0; i < buflen; i++) {
263 ret = kvm_read(kvm, (unsigned long)kvm_pointer + i,
264 &(buffer[i]), sizeof(char));
266 return (MEMSTAT_ERROR_KVM);
267 if ((size_t)ret != sizeof(char))
268 return (MEMSTAT_ERROR_KVM_SHORTREAD);
269 if (buffer[i] == '\0')
278 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
283 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
285 return (MEMSTAT_ERROR_KVM);
286 if ((size_t)ret != size)
287 return (MEMSTAT_ERROR_KVM_SHORTREAD);
292 * memstat_kvm_uma() is similar to memstat_sysctl_uma(), only it extracts
293 * UMA(9) statistics from a kernel core/memory file.
296 memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle)
298 LIST_HEAD(, uma_keg) uma_kegs;
299 struct memory_type *mtp;
300 struct uma_bucket *ubp, ub;
301 struct uma_cache *ucp, *ucp_array;
302 struct uma_zone *uzp, uz;
303 struct uma_keg *kzp, kz;
304 int hint_dontsearch, i, mp_maxid, ret;
305 char name[MEMTYPE_MAXNAME];
310 kvm = (kvm_t *)kvm_handle;
311 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
312 if (kvm_nlist(kvm, namelist) != 0) {
313 list->mtl_error = MEMSTAT_ERROR_KVM;
316 if (namelist[X_UMA_KEGS].n_type == 0 ||
317 namelist[X_UMA_KEGS].n_value == 0) {
318 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL;
321 ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0);
323 list->mtl_error = ret;
326 ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0);
328 list->mtl_error = ret;
331 cpusetsize = sysconf(_SC_CPUSET_SIZE);
332 if (cpusetsize == -1 || (u_long)cpusetsize > sizeof(cpuset_t)) {
333 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL;
337 ret = kread_symbol(kvm, X_ALL_CPUS, &all_cpus, cpusetsize, 0);
339 list->mtl_error = ret;
342 ucp_array = malloc(sizeof(struct uma_cache) * (mp_maxid + 1));
343 if (ucp_array == NULL) {
344 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
347 for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp =
348 LIST_NEXT(&kz, uk_link)) {
349 ret = kread(kvm, kzp, &kz, sizeof(kz), 0);
352 _memstat_mtl_empty(list);
353 list->mtl_error = ret;
356 for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp =
357 LIST_NEXT(&uz, uz_link)) {
358 ret = kread(kvm, uzp, &uz, sizeof(uz), 0);
361 _memstat_mtl_empty(list);
362 list->mtl_error = ret;
365 ret = kread(kvm, uzp, ucp_array,
366 sizeof(struct uma_cache) * (mp_maxid + 1),
367 offsetof(struct uma_zone, uz_cpu[0]));
370 _memstat_mtl_empty(list);
371 list->mtl_error = ret;
374 ret = kread_string(kvm, uz.uz_name, name,
378 _memstat_mtl_empty(list);
379 list->mtl_error = ret;
382 if (hint_dontsearch == 0) {
383 mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
388 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
392 _memstat_mtl_empty(list);
393 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
397 * Reset the statistics on a current node.
399 _memstat_mt_reset_stats(mtp, mp_maxid + 1);
400 mtp->mt_numallocs = uz.uz_allocs;
401 mtp->mt_numfrees = uz.uz_frees;
402 mtp->mt_failures = uz.uz_fails;
403 mtp->mt_sleeps = uz.uz_sleeps;
404 if (kz.uk_flags & UMA_ZFLAG_INTERNAL)
406 for (i = 0; i < mp_maxid + 1; i++) {
407 if (!CPU_ISSET(i, &all_cpus))
410 mtp->mt_numallocs += ucp->uc_allocs;
411 mtp->mt_numfrees += ucp->uc_frees;
413 if (ucp->uc_allocbucket != NULL) {
414 ret = kread(kvm, ucp->uc_allocbucket,
418 _memstat_mtl_empty(list);
419 list->mtl_error = ret;
422 mtp->mt_free += ub.ub_cnt;
424 if (ucp->uc_freebucket != NULL) {
425 ret = kread(kvm, ucp->uc_freebucket,
429 _memstat_mtl_empty(list);
430 list->mtl_error = ret;
433 mtp->mt_free += ub.ub_cnt;
437 mtp->mt_size = kz.uk_size;
438 mtp->mt_memalloced = mtp->mt_numallocs * mtp->mt_size;
439 mtp->mt_memfreed = mtp->mt_numfrees * mtp->mt_size;
440 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
442 mtp->mt_countlimit = kz.uk_maxpages /
445 mtp->mt_countlimit = kz.uk_maxpages *
447 mtp->mt_byteslimit = mtp->mt_countlimit * mtp->mt_size;
448 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
449 for (ubp = LIST_FIRST(&uz.uz_full_bucket); ubp !=
450 NULL; ubp = LIST_NEXT(&ub, ub_link)) {
451 ret = kread(kvm, ubp, &ub, sizeof(ub), 0);
452 mtp->mt_zonefree += ub.ub_cnt;
454 if (!((kz.uk_flags & UMA_ZONE_SECONDARY) &&
455 LIST_FIRST(&kz.uk_zones) != uzp)) {
456 mtp->mt_kegfree = kz.uk_free;
457 mtp->mt_free += mtp->mt_kegfree;
459 mtp->mt_free += mtp->mt_zonefree;