memstat_uma.c revision 148354
1147997Srwatson/*- 2147997Srwatson * Copyright (c) 2005 Robert N. M. Watson 3147997Srwatson * All rights reserved. 4147997Srwatson * 5147997Srwatson * Redistribution and use in source and binary forms, with or without 6147997Srwatson * modification, are permitted provided that the following conditions 7147997Srwatson * are met: 8147997Srwatson * 1. Redistributions of source code must retain the above copyright 9147997Srwatson * notice, this list of conditions and the following disclaimer. 10147997Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11147997Srwatson * notice, this list of conditions and the following disclaimer in the 12147997Srwatson * documentation and/or other materials provided with the distribution. 13147997Srwatson * 14147997Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15147997Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16147997Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17147997Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18147997Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19147997Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20147997Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21147997Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22147997Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23147997Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24147997Srwatson * SUCH DAMAGE. 25147997Srwatson * 26147997Srwatson * $FreeBSD: head/lib/libmemstat/memstat_uma.c 148354 2005-07-23 21:17:15Z rwatson $ 27147997Srwatson */ 28147997Srwatson 29147997Srwatson#include <sys/param.h> 30147997Srwatson#include <sys/sysctl.h> 31147997Srwatson 32147997Srwatson#include <vm/uma.h> 33147997Srwatson 34147997Srwatson#include <err.h> 35147997Srwatson#include <errno.h> 36147997Srwatson#include <stdio.h> 37147997Srwatson#include <stdlib.h> 38147997Srwatson#include <string.h> 39147997Srwatson 40147997Srwatson#include "memstat.h" 41147997Srwatson#include "memstat_internal.h" 42147997Srwatson 43147997Srwatson/* 44147997Srwatson * Extract uma(9) statistics from the running kernel, and store all memory 45147997Srwatson * type information in the passed list. For each type, check the list for an 46147997Srwatson * existing entry with the right name/allocator -- if present, update that 47147997Srwatson * entry. Otherwise, add a new entry. On error, the entire list will be 48147997Srwatson * cleared, as entries will be in an inconsistent state. 49147997Srwatson * 50147997Srwatson * To reduce the level of work for a list that starts empty, we keep around a 51147997Srwatson * hint as to whether it was empty when we began, so we can avoid searching 52147997Srwatson * the list for entries to update. Updates are O(n^2) due to searching for 53147997Srwatson * each entry before adding it. 54147997Srwatson */ 55147997Srwatsonint 56147997Srwatsonmemstat_sysctl_uma(struct memory_type_list *list, int flags) 57147997Srwatson{ 58147997Srwatson struct uma_stream_header *ushp; 59147997Srwatson struct uma_type_header *uthp; 60147997Srwatson struct uma_percpu_stat *upsp; 61147997Srwatson struct memory_type *mtp; 62147997Srwatson int count, error, hint_dontsearch, i, j, maxcpus; 63147997Srwatson char *buffer, *p; 64147997Srwatson size_t size; 65147997Srwatson 66147997Srwatson hint_dontsearch = LIST_EMPTY(list); 67147997Srwatson 68147997Srwatson /* 69147997Srwatson * Query the number of CPUs, number of malloc types so that we can 70147997Srwatson * guess an initial buffer size. We loop until we succeed or really 71147997Srwatson * fail. Note that the value of maxcpus we query using sysctl is not 72147997Srwatson * the version we use when processing the real data -- that is read 73147997Srwatson * from the header. 74147997Srwatson */ 75147997Srwatsonretry: 76147997Srwatson size = sizeof(maxcpus); 77147997Srwatson if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) { 78147997Srwatson error = errno; 79147997Srwatson perror("kern.smp.maxcpus"); 80147997Srwatson errno = error; 81147997Srwatson return (-1); 82147997Srwatson } 83147997Srwatson if (size != sizeof(maxcpus)) { 84148038Srwatson fprintf(stderr, "kern.smp.maxcpus: wrong size"); 85147997Srwatson errno = EINVAL; 86147997Srwatson return (-1); 87147997Srwatson } 88147997Srwatson 89147997Srwatson if (maxcpus > MEMSTAT_MAXCPU) { 90147997Srwatson fprintf(stderr, "kern.smp.maxcpus: too many CPUs\n"); 91147997Srwatson errno = EINVAL; 92147997Srwatson return (-1); 93147997Srwatson } 94147997Srwatson 95147997Srwatson size = sizeof(count); 96147997Srwatson if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) { 97147997Srwatson error = errno; 98147997Srwatson perror("vm.zone_count"); 99147997Srwatson errno = error; 100147997Srwatson return (-1); 101147997Srwatson } 102147997Srwatson if (size != sizeof(count)) { 103148038Srwatson fprintf(stderr, "vm.zone_count: wrong size"); 104147997Srwatson errno = EINVAL; 105147997Srwatson return (-1); 106147997Srwatson } 107147997Srwatson 108147997Srwatson size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) * 109147997Srwatson maxcpus); 110147997Srwatson 111147997Srwatson buffer = malloc(size); 112147997Srwatson if (buffer == NULL) { 113147997Srwatson error = errno; 114147997Srwatson perror("malloc"); 115147997Srwatson errno = error; 116147997Srwatson return (-1); 117147997Srwatson } 118147997Srwatson 119147997Srwatson if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) { 120147997Srwatson /* 121147997Srwatson * XXXRW: ENOMEM is an ambiguous return, we should bound the 122147997Srwatson * number of loops, perhaps. 123147997Srwatson */ 124147997Srwatson if (errno == ENOMEM) { 125147997Srwatson free(buffer); 126147997Srwatson goto retry; 127147997Srwatson } 128147997Srwatson error = errno; 129147997Srwatson free(buffer); 130147997Srwatson perror("vm.zone_stats"); 131147997Srwatson errno = error; 132147997Srwatson return (-1); 133147997Srwatson } 134147997Srwatson 135147997Srwatson if (size == 0) { 136147997Srwatson free(buffer); 137147997Srwatson return (0); 138147997Srwatson } 139147997Srwatson 140147997Srwatson if (size < sizeof(*ushp)) { 141147997Srwatson fprintf(stderr, "sysctl_uma: invalid malloc header"); 142147997Srwatson free(buffer); 143147997Srwatson errno = EINVAL; 144147997Srwatson return (-1); 145147997Srwatson } 146147997Srwatson p = buffer; 147147997Srwatson ushp = (struct uma_stream_header *)p; 148147997Srwatson p += sizeof(*ushp); 149147997Srwatson 150147997Srwatson if (ushp->ush_version != UMA_STREAM_VERSION) { 151147997Srwatson fprintf(stderr, "sysctl_uma: unknown malloc version"); 152147997Srwatson free(buffer); 153147997Srwatson errno = EINVAL; 154147997Srwatson return (-1); 155147997Srwatson } 156147997Srwatson 157147997Srwatson if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) { 158147997Srwatson fprintf(stderr, "sysctl_uma: too many CPUs"); 159147997Srwatson free(buffer); 160147997Srwatson errno = EINVAL; 161147997Srwatson return (-1); 162147997Srwatson } 163147997Srwatson 164147997Srwatson /* 165147997Srwatson * For the remainder of this function, we are quite trusting about 166147997Srwatson * the layout of structures and sizes, since we've determined we have 167147997Srwatson * a matching version and acceptable CPU count. 168147997Srwatson */ 169147997Srwatson maxcpus = ushp->ush_maxcpus; 170147997Srwatson count = ushp->ush_count; 171147997Srwatson for (i = 0; i < count; i++) { 172147997Srwatson uthp = (struct uma_type_header *)p; 173147997Srwatson p += sizeof(*uthp); 174147997Srwatson 175147997Srwatson if (hint_dontsearch == 0) { 176147997Srwatson mtp = memstat_mtl_find(list, ALLOCATOR_UMA, 177147997Srwatson uthp->uth_name); 178147997Srwatson } else 179147997Srwatson mtp = NULL; 180147997Srwatson if (mtp == NULL) 181148354Srwatson mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA, 182147997Srwatson uthp->uth_name); 183147997Srwatson if (mtp == NULL) { 184147997Srwatson memstat_mtl_free(list); 185147997Srwatson free(buffer); 186147997Srwatson errno = ENOMEM; 187147997Srwatson perror("malloc"); 188147997Srwatson errno = ENOMEM; 189147997Srwatson return (-1); 190147997Srwatson } 191147997Srwatson 192147997Srwatson /* 193147997Srwatson * Reset the statistics on a current node. 194147997Srwatson */ 195148354Srwatson _memstat_mt_reset_stats(mtp); 196147997Srwatson 197148007Srwatson mtp->mt_numallocs = uthp->uth_allocs; 198148007Srwatson mtp->mt_numfrees = uthp->uth_frees; 199148071Srwatson mtp->mt_failures = uthp->uth_fails; 200148007Srwatson 201147997Srwatson for (j = 0; j < maxcpus; j++) { 202147997Srwatson upsp = (struct uma_percpu_stat *)p; 203147997Srwatson p += sizeof(*upsp); 204147997Srwatson 205147997Srwatson mtp->mt_percpu_cache[j].mtp_free = 206147997Srwatson upsp->ups_cache_free; 207147997Srwatson mtp->mt_free += upsp->ups_cache_free; 208147997Srwatson mtp->mt_numallocs += upsp->ups_allocs; 209147997Srwatson mtp->mt_numfrees += upsp->ups_frees; 210147997Srwatson } 211147997Srwatson 212147997Srwatson mtp->mt_size = uthp->uth_size; 213148007Srwatson mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size; 214148007Srwatson mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size; 215147997Srwatson mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed; 216147997Srwatson mtp->mt_countlimit = uthp->uth_limit; 217147997Srwatson mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size; 218147997Srwatson 219147997Srwatson mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees; 220148170Srwatson mtp->mt_zonefree = uthp->uth_zone_free; 221148170Srwatson mtp->mt_kegfree = uthp->uth_keg_free; 222147997Srwatson mtp->mt_free += mtp->mt_zonefree; 223147997Srwatson } 224147997Srwatson 225147997Srwatson free(buffer); 226147997Srwatson 227147997Srwatson return (0); 228147997Srwatson} 229