10cddce49SRobert Watson /*- 20cddce49SRobert Watson * Copyright (c) 2005 Robert N. M. Watson 30cddce49SRobert Watson * All rights reserved. 40cddce49SRobert Watson * 50cddce49SRobert Watson * Redistribution and use in source and binary forms, with or without 60cddce49SRobert Watson * modification, are permitted provided that the following conditions 70cddce49SRobert Watson * are met: 80cddce49SRobert Watson * 1. Redistributions of source code must retain the above copyright 90cddce49SRobert Watson * notice, this list of conditions and the following disclaimer. 100cddce49SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 110cddce49SRobert Watson * notice, this list of conditions and the following disclaimer in the 120cddce49SRobert Watson * documentation and/or other materials provided with the distribution. 130cddce49SRobert Watson * 140cddce49SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 150cddce49SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 160cddce49SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 170cddce49SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 180cddce49SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190cddce49SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 200cddce49SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 210cddce49SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 220cddce49SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 230cddce49SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 240cddce49SRobert Watson * SUCH DAMAGE. 250cddce49SRobert Watson * 260cddce49SRobert Watson * $FreeBSD$ 270cddce49SRobert Watson */ 280cddce49SRobert Watson 290cddce49SRobert Watson #include <sys/param.h> 300cddce49SRobert Watson #include <sys/sysctl.h> 310cddce49SRobert Watson 320cddce49SRobert Watson #include <err.h> 330cddce49SRobert Watson #include <errno.h> 340cddce49SRobert Watson #include <stdio.h> 350cddce49SRobert Watson #include <stdlib.h> 360cddce49SRobert Watson #include <string.h> 370cddce49SRobert Watson 380cddce49SRobert Watson #include "memstat.h" 390cddce49SRobert Watson #include "memstat_internal.h" 400cddce49SRobert Watson 41e754c6bbSRobert Watson const char * 42e754c6bbSRobert Watson memstat_strerror(int error) 43e754c6bbSRobert Watson { 44e754c6bbSRobert Watson 45e754c6bbSRobert Watson switch (error) { 46e754c6bbSRobert Watson case MEMSTAT_ERROR_NOMEMORY: 47e754c6bbSRobert Watson return ("Cannot allocate memory"); 48e754c6bbSRobert Watson case MEMSTAT_ERROR_VERSION: 49e754c6bbSRobert Watson return ("Version mismatch"); 50e754c6bbSRobert Watson case MEMSTAT_ERROR_PERMISSION: 51e754c6bbSRobert Watson return ("Permission denied"); 52e754c6bbSRobert Watson case MEMSTAT_ERROR_TOOMANYCPUS: 53e754c6bbSRobert Watson return ("Too many CPUs"); 54e754c6bbSRobert Watson case MEMSTAT_ERROR_DATAERROR: 55e754c6bbSRobert Watson return ("Data format error"); 5633c20d18SRobert Watson case MEMSTAT_ERROR_KVM: 5733c20d18SRobert Watson return ("KVM error"); 5833c20d18SRobert Watson case MEMSTAT_ERROR_KVM_NOSYMBOL: 5933c20d18SRobert Watson return ("KVM unable to find symbol"); 6033c20d18SRobert Watson case MEMSTAT_ERROR_KVM_SHORTREAD: 6133c20d18SRobert Watson return ("KVM short read"); 62e754c6bbSRobert Watson case MEMSTAT_ERROR_UNDEFINED: 63e754c6bbSRobert Watson default: 64e754c6bbSRobert Watson return ("Unknown error"); 65e754c6bbSRobert Watson } 66e754c6bbSRobert Watson } 67e754c6bbSRobert Watson 680cddce49SRobert Watson struct memory_type_list * 690cddce49SRobert Watson memstat_mtl_alloc(void) 700cddce49SRobert Watson { 710cddce49SRobert Watson struct memory_type_list *mtlp; 720cddce49SRobert Watson 730cddce49SRobert Watson mtlp = malloc(sizeof(*mtlp)); 740cddce49SRobert Watson if (mtlp == NULL) 750cddce49SRobert Watson return (NULL); 760cddce49SRobert Watson 7734562808SRobert Watson LIST_INIT(&mtlp->mtl_list); 7834562808SRobert Watson mtlp->mtl_error = MEMSTAT_ERROR_UNDEFINED; 790cddce49SRobert Watson return (mtlp); 800cddce49SRobert Watson } 810cddce49SRobert Watson 820cddce49SRobert Watson struct memory_type * 830cddce49SRobert Watson memstat_mtl_first(struct memory_type_list *list) 840cddce49SRobert Watson { 850cddce49SRobert Watson 8634562808SRobert Watson return (LIST_FIRST(&list->mtl_list)); 870cddce49SRobert Watson } 880cddce49SRobert Watson 890cddce49SRobert Watson struct memory_type * 900cddce49SRobert Watson memstat_mtl_next(struct memory_type *mtp) 910cddce49SRobert Watson { 920cddce49SRobert Watson 930cddce49SRobert Watson return (LIST_NEXT(mtp, mt_list)); 940cddce49SRobert Watson } 950cddce49SRobert Watson 960cddce49SRobert Watson void 9722247a2aSRobert Watson _memstat_mtl_empty(struct memory_type_list *list) 980cddce49SRobert Watson { 990cddce49SRobert Watson struct memory_type *mtp; 1000cddce49SRobert Watson 10134562808SRobert Watson while ((mtp = LIST_FIRST(&list->mtl_list))) { 1020cddce49SRobert Watson LIST_REMOVE(mtp, mt_list); 1030cddce49SRobert Watson free(mtp); 1040cddce49SRobert Watson } 10522247a2aSRobert Watson } 10622247a2aSRobert Watson 10722247a2aSRobert Watson void 10822247a2aSRobert Watson memstat_mtl_free(struct memory_type_list *list) 10922247a2aSRobert Watson { 11022247a2aSRobert Watson 11122247a2aSRobert Watson _memstat_mtl_empty(list); 1120cddce49SRobert Watson free(list); 1130cddce49SRobert Watson } 1140cddce49SRobert Watson 11534562808SRobert Watson int 11634562808SRobert Watson memstat_mtl_geterror(struct memory_type_list *list) 11734562808SRobert Watson { 11834562808SRobert Watson 11934562808SRobert Watson return (list->mtl_error); 12034562808SRobert Watson } 12134562808SRobert Watson 1220cddce49SRobert Watson /* 1230cddce49SRobert Watson * Look for an existing memory_type entry in a memory_type list, based on the 12434562808SRobert Watson * allocator and name of the type. If not found, return NULL. No errno or 12534562808SRobert Watson * memstat error. 1260cddce49SRobert Watson */ 1270cddce49SRobert Watson struct memory_type * 1280cddce49SRobert Watson memstat_mtl_find(struct memory_type_list *list, int allocator, 1290cddce49SRobert Watson const char *name) 1300cddce49SRobert Watson { 1310cddce49SRobert Watson struct memory_type *mtp; 1320cddce49SRobert Watson 13334562808SRobert Watson LIST_FOREACH(mtp, &list->mtl_list, mt_list) { 1340cddce49SRobert Watson if ((mtp->mt_allocator == allocator || 1350cddce49SRobert Watson allocator == ALLOCATOR_ANY) && 1360cddce49SRobert Watson strcmp(mtp->mt_name, name) == 0) 1370cddce49SRobert Watson return (mtp); 1380cddce49SRobert Watson } 1390cddce49SRobert Watson return (NULL); 1400cddce49SRobert Watson } 1410cddce49SRobert Watson 1420cddce49SRobert Watson /* 1430cddce49SRobert Watson * Allocate a new memory_type with the specificed allocator type and name, 1440cddce49SRobert Watson * then insert into the list. The structure will be zero'd. 145ddefbc89SRobert Watson * 146ddefbc89SRobert Watson * libmemstat(3) internal function. 1470cddce49SRobert Watson */ 1480cddce49SRobert Watson struct memory_type * 149ddefbc89SRobert Watson _memstat_mt_allocate(struct memory_type_list *list, int allocator, 1500cddce49SRobert Watson const char *name) 1510cddce49SRobert Watson { 1520cddce49SRobert Watson struct memory_type *mtp; 1530cddce49SRobert Watson 1540cddce49SRobert Watson mtp = malloc(sizeof(*mtp)); 1550cddce49SRobert Watson if (mtp == NULL) 1560cddce49SRobert Watson return (NULL); 1570cddce49SRobert Watson 1580cddce49SRobert Watson bzero(mtp, sizeof(*mtp)); 1590cddce49SRobert Watson 1600cddce49SRobert Watson mtp->mt_allocator = allocator; 1610cddce49SRobert Watson strlcpy(mtp->mt_name, name, MEMTYPE_MAXNAME); 16234562808SRobert Watson LIST_INSERT_HEAD(&list->mtl_list, mtp, mt_list); 1630cddce49SRobert Watson return (mtp); 1640cddce49SRobert Watson } 1650cddce49SRobert Watson 1660cddce49SRobert Watson /* 1670cddce49SRobert Watson * Reset any libmemstat(3)-owned statistics in a memory_type record so that 1680cddce49SRobert Watson * it can be reused without incremental addition problems. Caller-owned 1690cddce49SRobert Watson * memory is left "as-is", and must be updated by the caller if desired. 170ddefbc89SRobert Watson * 171ddefbc89SRobert Watson * libmemstat(3) internal function. 1720cddce49SRobert Watson */ 1730cddce49SRobert Watson void 174ddefbc89SRobert Watson _memstat_mt_reset_stats(struct memory_type *mtp) 1750cddce49SRobert Watson { 1760cddce49SRobert Watson int i; 1770cddce49SRobert Watson 1780cddce49SRobert Watson mtp->mt_countlimit = 0; 1790cddce49SRobert Watson mtp->mt_byteslimit = 0; 1800cddce49SRobert Watson mtp->mt_sizemask = 0; 1810cddce49SRobert Watson mtp->mt_size = 0; 1820cddce49SRobert Watson 1830cddce49SRobert Watson mtp->mt_memalloced = 0; 1840cddce49SRobert Watson mtp->mt_memfreed = 0; 1850cddce49SRobert Watson mtp->mt_numallocs = 0; 1860cddce49SRobert Watson mtp->mt_numfrees = 0; 1870cddce49SRobert Watson mtp->mt_bytes = 0; 1880cddce49SRobert Watson mtp->mt_count = 0; 1890cddce49SRobert Watson mtp->mt_free = 0; 1900cddce49SRobert Watson mtp->mt_failures = 0; 191*bf965959SSean Bruno mtp->mt_sleeps = 0; 1920cddce49SRobert Watson 1930cddce49SRobert Watson mtp->mt_zonefree = 0; 194ca108fe2SRobert Watson mtp->mt_kegfree = 0; 1950cddce49SRobert Watson 1960cddce49SRobert Watson for (i = 0; i < MEMSTAT_MAXCPU; i++) { 1970cddce49SRobert Watson mtp->mt_percpu_alloc[i].mtp_memalloced = 0; 1980cddce49SRobert Watson mtp->mt_percpu_alloc[i].mtp_memfreed = 0; 1990cddce49SRobert Watson mtp->mt_percpu_alloc[i].mtp_numallocs = 0; 2000cddce49SRobert Watson mtp->mt_percpu_alloc[i].mtp_numfrees = 0; 2010cddce49SRobert Watson mtp->mt_percpu_alloc[i].mtp_sizemask = 0; 2020cddce49SRobert Watson mtp->mt_percpu_cache[i].mtp_free = 0; 2030cddce49SRobert Watson } 2040cddce49SRobert Watson } 2050cddce49SRobert Watson 2060cddce49SRobert Watson /* 2070cddce49SRobert Watson * Accessor methods for struct memory_type. Avoids encoding the structure 2080cddce49SRobert Watson * ABI into the application. 2090cddce49SRobert Watson */ 2100cddce49SRobert Watson const char * 2110cddce49SRobert Watson memstat_get_name(const struct memory_type *mtp) 2120cddce49SRobert Watson { 2130cddce49SRobert Watson 2140cddce49SRobert Watson return (mtp->mt_name); 2150cddce49SRobert Watson } 2160cddce49SRobert Watson 2170cddce49SRobert Watson int 2180cddce49SRobert Watson memstat_get_allocator(const struct memory_type *mtp) 2190cddce49SRobert Watson { 2200cddce49SRobert Watson 2210cddce49SRobert Watson return (mtp->mt_allocator); 2220cddce49SRobert Watson } 2230cddce49SRobert Watson 2240cddce49SRobert Watson uint64_t 2250cddce49SRobert Watson memstat_get_countlimit(const struct memory_type *mtp) 2260cddce49SRobert Watson { 2270cddce49SRobert Watson 2280cddce49SRobert Watson return (mtp->mt_countlimit); 2290cddce49SRobert Watson } 2300cddce49SRobert Watson 2310cddce49SRobert Watson uint64_t 2320cddce49SRobert Watson memstat_get_byteslimit(const struct memory_type *mtp) 2330cddce49SRobert Watson { 2340cddce49SRobert Watson 2350cddce49SRobert Watson return (mtp->mt_byteslimit); 2360cddce49SRobert Watson } 2370cddce49SRobert Watson 2380cddce49SRobert Watson uint64_t 2390cddce49SRobert Watson memstat_get_sizemask(const struct memory_type *mtp) 2400cddce49SRobert Watson { 2410cddce49SRobert Watson 2420cddce49SRobert Watson return (mtp->mt_sizemask); 2430cddce49SRobert Watson } 2440cddce49SRobert Watson 2450cddce49SRobert Watson uint64_t 2460cddce49SRobert Watson memstat_get_size(const struct memory_type *mtp) 2470cddce49SRobert Watson { 2480cddce49SRobert Watson 2490cddce49SRobert Watson return (mtp->mt_size); 2500cddce49SRobert Watson } 2510cddce49SRobert Watson 2520cddce49SRobert Watson uint64_t 2530cddce49SRobert Watson memstat_get_memalloced(const struct memory_type *mtp) 2540cddce49SRobert Watson { 2550cddce49SRobert Watson 2560cddce49SRobert Watson return (mtp->mt_memalloced); 2570cddce49SRobert Watson } 2580cddce49SRobert Watson 2590cddce49SRobert Watson uint64_t 2600cddce49SRobert Watson memstat_get_memfreed(const struct memory_type *mtp) 2610cddce49SRobert Watson { 2620cddce49SRobert Watson 2630cddce49SRobert Watson return (mtp->mt_memfreed); 2640cddce49SRobert Watson } 2650cddce49SRobert Watson 2660cddce49SRobert Watson uint64_t 2670cddce49SRobert Watson memstat_get_numallocs(const struct memory_type *mtp) 2680cddce49SRobert Watson { 2690cddce49SRobert Watson 2700cddce49SRobert Watson return (mtp->mt_numallocs); 2710cddce49SRobert Watson } 2720cddce49SRobert Watson 2730cddce49SRobert Watson uint64_t 2740cddce49SRobert Watson memstat_get_numfrees(const struct memory_type *mtp) 2750cddce49SRobert Watson { 2760cddce49SRobert Watson 2770cddce49SRobert Watson return (mtp->mt_numfrees); 2780cddce49SRobert Watson } 2790cddce49SRobert Watson 2800cddce49SRobert Watson uint64_t 2810cddce49SRobert Watson memstat_get_bytes(const struct memory_type *mtp) 2820cddce49SRobert Watson { 2830cddce49SRobert Watson 2840cddce49SRobert Watson return (mtp->mt_bytes); 2850cddce49SRobert Watson } 2860cddce49SRobert Watson 2870cddce49SRobert Watson uint64_t 2880cddce49SRobert Watson memstat_get_count(const struct memory_type *mtp) 2890cddce49SRobert Watson { 2900cddce49SRobert Watson 2910cddce49SRobert Watson return (mtp->mt_count); 2920cddce49SRobert Watson } 2930cddce49SRobert Watson 2940cddce49SRobert Watson uint64_t 2950cddce49SRobert Watson memstat_get_free(const struct memory_type *mtp) 2960cddce49SRobert Watson { 2970cddce49SRobert Watson 2980cddce49SRobert Watson return (mtp->mt_free); 2990cddce49SRobert Watson } 3000cddce49SRobert Watson 3010cddce49SRobert Watson uint64_t 3020cddce49SRobert Watson memstat_get_failures(const struct memory_type *mtp) 3030cddce49SRobert Watson { 3040cddce49SRobert Watson 3050cddce49SRobert Watson return (mtp->mt_failures); 3060cddce49SRobert Watson } 3070cddce49SRobert Watson 308*bf965959SSean Bruno uint64_t 309*bf965959SSean Bruno memstat_get_sleeps(const struct memory_type *mtp) 310*bf965959SSean Bruno { 311*bf965959SSean Bruno 312*bf965959SSean Bruno return (mtp->mt_sleeps); 313*bf965959SSean Bruno } 314*bf965959SSean Bruno 3150cddce49SRobert Watson void * 3160cddce49SRobert Watson memstat_get_caller_pointer(const struct memory_type *mtp, int index) 3170cddce49SRobert Watson { 3180cddce49SRobert Watson 3190cddce49SRobert Watson return (mtp->mt_caller_pointer[index]); 3200cddce49SRobert Watson } 3210cddce49SRobert Watson 3220cddce49SRobert Watson void 3230cddce49SRobert Watson memstat_set_caller_pointer(struct memory_type *mtp, int index, void *value) 3240cddce49SRobert Watson { 3250cddce49SRobert Watson 3260cddce49SRobert Watson mtp->mt_caller_pointer[index] = value; 3270cddce49SRobert Watson } 3280cddce49SRobert Watson 3290cddce49SRobert Watson uint64_t 3300cddce49SRobert Watson memstat_get_caller_uint64(const struct memory_type *mtp, int index) 3310cddce49SRobert Watson { 3320cddce49SRobert Watson 3330cddce49SRobert Watson return (mtp->mt_caller_uint64[index]); 3340cddce49SRobert Watson } 3350cddce49SRobert Watson 3360cddce49SRobert Watson void 3370cddce49SRobert Watson memstat_set_caller_uint64(struct memory_type *mtp, int index, uint64_t value) 3380cddce49SRobert Watson { 3390cddce49SRobert Watson 3400cddce49SRobert Watson mtp->mt_caller_uint64[index] = value; 3410cddce49SRobert Watson } 3420cddce49SRobert Watson 3430cddce49SRobert Watson uint64_t 3440cddce49SRobert Watson memstat_get_zonefree(const struct memory_type *mtp) 3450cddce49SRobert Watson { 3460cddce49SRobert Watson 3470cddce49SRobert Watson return (mtp->mt_zonefree); 3480cddce49SRobert Watson } 3490cddce49SRobert Watson 3500cddce49SRobert Watson uint64_t 351ca108fe2SRobert Watson memstat_get_kegfree(const struct memory_type *mtp) 352ca108fe2SRobert Watson { 353ca108fe2SRobert Watson 354ca108fe2SRobert Watson return (mtp->mt_kegfree); 355ca108fe2SRobert Watson } 356ca108fe2SRobert Watson 357ca108fe2SRobert Watson uint64_t 3580cddce49SRobert Watson memstat_get_percpu_memalloced(const struct memory_type *mtp, int cpu) 3590cddce49SRobert Watson { 3600cddce49SRobert Watson 3610cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_memalloced); 3620cddce49SRobert Watson } 3630cddce49SRobert Watson 3640cddce49SRobert Watson uint64_t 3650cddce49SRobert Watson memstat_get_percpu_memfreed(const struct memory_type *mtp, int cpu) 3660cddce49SRobert Watson { 3670cddce49SRobert Watson 3680cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_memfreed); 3690cddce49SRobert Watson } 3700cddce49SRobert Watson 3710cddce49SRobert Watson uint64_t 3720cddce49SRobert Watson memstat_get_percpu_numallocs(const struct memory_type *mtp, int cpu) 3730cddce49SRobert Watson { 3740cddce49SRobert Watson 3750cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_numallocs); 3760cddce49SRobert Watson } 3770cddce49SRobert Watson 3780cddce49SRobert Watson uint64_t 3790cddce49SRobert Watson memstat_get_percpu_numfrees(const struct memory_type *mtp, int cpu) 3800cddce49SRobert Watson { 3810cddce49SRobert Watson 3820cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_numfrees); 3830cddce49SRobert Watson } 3840cddce49SRobert Watson 3850cddce49SRobert Watson uint64_t 3860cddce49SRobert Watson memstat_get_percpu_sizemask(const struct memory_type *mtp, int cpu) 3870cddce49SRobert Watson { 3880cddce49SRobert Watson 3890cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_sizemask); 3900cddce49SRobert Watson } 3910cddce49SRobert Watson 3920cddce49SRobert Watson void * 3930cddce49SRobert Watson memstat_get_percpu_caller_pointer(const struct memory_type *mtp, int cpu, 3940cddce49SRobert Watson int index) 3950cddce49SRobert Watson { 3960cddce49SRobert Watson 3970cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index]); 3980cddce49SRobert Watson } 3990cddce49SRobert Watson 4000cddce49SRobert Watson void 4010cddce49SRobert Watson memstat_set_percpu_caller_pointer(struct memory_type *mtp, int cpu, 4020cddce49SRobert Watson int index, void *value) 4030cddce49SRobert Watson { 4040cddce49SRobert Watson 4050cddce49SRobert Watson mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index] = value; 4060cddce49SRobert Watson } 4070cddce49SRobert Watson 4080cddce49SRobert Watson uint64_t 4090cddce49SRobert Watson memstat_get_percpu_caller_uint64(const struct memory_type *mtp, int cpu, 4100cddce49SRobert Watson int index) 4110cddce49SRobert Watson { 4120cddce49SRobert Watson 4130cddce49SRobert Watson return (mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index]); 4140cddce49SRobert Watson } 4150cddce49SRobert Watson 4160cddce49SRobert Watson void 4170cddce49SRobert Watson memstat_set_percpu_caller_uint64(struct memory_type *mtp, int cpu, int index, 4180cddce49SRobert Watson uint64_t value) 4190cddce49SRobert Watson { 4200cddce49SRobert Watson 4210cddce49SRobert Watson mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index] = value; 4220cddce49SRobert Watson } 4230cddce49SRobert Watson 4240cddce49SRobert Watson uint64_t 4250cddce49SRobert Watson memstat_get_percpu_free(const struct memory_type *mtp, int cpu) 4260cddce49SRobert Watson { 4270cddce49SRobert Watson 4280cddce49SRobert Watson return (mtp->mt_percpu_cache[cpu].mtp_free); 4290cddce49SRobert Watson } 430