2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/sysctl.h>
42 #include "memstat_internal.h"
45 memstat_strerror(int error)
49 case MEMSTAT_ERROR_NOMEMORY:
50 return ("Cannot allocate memory");
51 case MEMSTAT_ERROR_VERSION:
52 return ("Version mismatch");
53 case MEMSTAT_ERROR_PERMISSION:
54 return ("Permission denied");
55 case MEMSTAT_ERROR_DATAERROR:
56 return ("Data format error");
57 case MEMSTAT_ERROR_KVM:
59 case MEMSTAT_ERROR_KVM_NOSYMBOL:
60 return ("KVM unable to find symbol");
61 case MEMSTAT_ERROR_KVM_SHORTREAD:
62 return ("KVM short read");
63 case MEMSTAT_ERROR_UNDEFINED:
65 return ("Unknown error");
69 struct memory_type_list *
70 memstat_mtl_alloc(void)
72 struct memory_type_list *mtlp;
74 mtlp = malloc(sizeof(*mtlp));
78 LIST_INIT(&mtlp->mtl_list);
79 mtlp->mtl_error = MEMSTAT_ERROR_UNDEFINED;
84 memstat_mtl_first(struct memory_type_list *list)
87 return (LIST_FIRST(&list->mtl_list));
91 memstat_mtl_next(struct memory_type *mtp)
94 return (LIST_NEXT(mtp, mt_list));
98 _memstat_mtl_empty(struct memory_type_list *list)
100 struct memory_type *mtp;
102 while ((mtp = LIST_FIRST(&list->mtl_list))) {
103 free(mtp->mt_percpu_alloc);
104 free(mtp->mt_percpu_cache);
105 LIST_REMOVE(mtp, mt_list);
111 memstat_mtl_free(struct memory_type_list *list)
114 _memstat_mtl_empty(list);
119 memstat_mtl_geterror(struct memory_type_list *list)
122 return (list->mtl_error);
126 * Look for an existing memory_type entry in a memory_type list, based on the
127 * allocator and name of the type. If not found, return NULL. No errno or
131 memstat_mtl_find(struct memory_type_list *list, int allocator,
134 struct memory_type *mtp;
136 LIST_FOREACH(mtp, &list->mtl_list, mt_list) {
137 if ((mtp->mt_allocator == allocator ||
138 allocator == ALLOCATOR_ANY) &&
139 strcmp(mtp->mt_name, name) == 0)
146 * Allocate a new memory_type with the specificed allocator type and name,
147 * then insert into the list. The structure will be zero'd.
149 * libmemstat(3) internal function.
152 _memstat_mt_allocate(struct memory_type_list *list, int allocator,
153 const char *name, int maxcpus)
155 struct memory_type *mtp;
157 mtp = malloc(sizeof(*mtp));
161 bzero(mtp, sizeof(*mtp));
163 mtp->mt_allocator = allocator;
164 mtp->mt_percpu_alloc = malloc(sizeof(struct mt_percpu_alloc_s) *
166 mtp->mt_percpu_cache = malloc(sizeof(struct mt_percpu_cache_s) *
168 strlcpy(mtp->mt_name, name, MEMTYPE_MAXNAME);
169 LIST_INSERT_HEAD(&list->mtl_list, mtp, mt_list);
174 * Reset any libmemstat(3)-owned statistics in a memory_type record so that
175 * it can be reused without incremental addition problems. Caller-owned
176 * memory is left "as-is", and must be updated by the caller if desired.
178 * libmemstat(3) internal function.
181 _memstat_mt_reset_stats(struct memory_type *mtp, int maxcpus)
185 mtp->mt_countlimit = 0;
186 mtp->mt_byteslimit = 0;
187 mtp->mt_sizemask = 0;
190 mtp->mt_memalloced = 0;
191 mtp->mt_memfreed = 0;
192 mtp->mt_numallocs = 0;
193 mtp->mt_numfrees = 0;
197 mtp->mt_failures = 0;
200 mtp->mt_zonefree = 0;
203 for (i = 0; i < maxcpus; i++) {
204 mtp->mt_percpu_alloc[i].mtp_memalloced = 0;
205 mtp->mt_percpu_alloc[i].mtp_memfreed = 0;
206 mtp->mt_percpu_alloc[i].mtp_numallocs = 0;
207 mtp->mt_percpu_alloc[i].mtp_numfrees = 0;
208 mtp->mt_percpu_alloc[i].mtp_sizemask = 0;
209 mtp->mt_percpu_cache[i].mtp_free = 0;
214 * Accessor methods for struct memory_type. Avoids encoding the structure
215 * ABI into the application.
218 memstat_get_name(const struct memory_type *mtp)
221 return (mtp->mt_name);
225 memstat_get_allocator(const struct memory_type *mtp)
228 return (mtp->mt_allocator);
232 memstat_get_countlimit(const struct memory_type *mtp)
235 return (mtp->mt_countlimit);
239 memstat_get_byteslimit(const struct memory_type *mtp)
242 return (mtp->mt_byteslimit);
246 memstat_get_sizemask(const struct memory_type *mtp)
249 return (mtp->mt_sizemask);
253 memstat_get_size(const struct memory_type *mtp)
256 return (mtp->mt_size);
260 memstat_get_rsize(const struct memory_type *mtp)
263 return (mtp->mt_rsize);
267 memstat_get_memalloced(const struct memory_type *mtp)
270 return (mtp->mt_memalloced);
274 memstat_get_memfreed(const struct memory_type *mtp)
277 return (mtp->mt_memfreed);
281 memstat_get_numallocs(const struct memory_type *mtp)
284 return (mtp->mt_numallocs);
288 memstat_get_numfrees(const struct memory_type *mtp)
291 return (mtp->mt_numfrees);
295 memstat_get_bytes(const struct memory_type *mtp)
298 return (mtp->mt_bytes);
302 memstat_get_count(const struct memory_type *mtp)
305 return (mtp->mt_count);
309 memstat_get_free(const struct memory_type *mtp)
312 return (mtp->mt_free);
316 memstat_get_failures(const struct memory_type *mtp)
319 return (mtp->mt_failures);
323 memstat_get_sleeps(const struct memory_type *mtp)
326 return (mtp->mt_sleeps);
330 memstat_get_caller_pointer(const struct memory_type *mtp, int index)
333 return (mtp->mt_caller_pointer[index]);
337 memstat_set_caller_pointer(struct memory_type *mtp, int index, void *value)
340 mtp->mt_caller_pointer[index] = value;
344 memstat_get_caller_uint64(const struct memory_type *mtp, int index)
347 return (mtp->mt_caller_uint64[index]);
351 memstat_set_caller_uint64(struct memory_type *mtp, int index, uint64_t value)
354 mtp->mt_caller_uint64[index] = value;
358 memstat_get_zonefree(const struct memory_type *mtp)
361 return (mtp->mt_zonefree);
365 memstat_get_kegfree(const struct memory_type *mtp)
368 return (mtp->mt_kegfree);
372 memstat_get_percpu_memalloced(const struct memory_type *mtp, int cpu)
375 return (mtp->mt_percpu_alloc[cpu].mtp_memalloced);
379 memstat_get_percpu_memfreed(const struct memory_type *mtp, int cpu)
382 return (mtp->mt_percpu_alloc[cpu].mtp_memfreed);
386 memstat_get_percpu_numallocs(const struct memory_type *mtp, int cpu)
389 return (mtp->mt_percpu_alloc[cpu].mtp_numallocs);
393 memstat_get_percpu_numfrees(const struct memory_type *mtp, int cpu)
396 return (mtp->mt_percpu_alloc[cpu].mtp_numfrees);
400 memstat_get_percpu_sizemask(const struct memory_type *mtp, int cpu)
403 return (mtp->mt_percpu_alloc[cpu].mtp_sizemask);
407 memstat_get_percpu_caller_pointer(const struct memory_type *mtp, int cpu,
411 return (mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index]);
415 memstat_set_percpu_caller_pointer(struct memory_type *mtp, int cpu,
416 int index, void *value)
419 mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index] = value;
423 memstat_get_percpu_caller_uint64(const struct memory_type *mtp, int cpu,
427 return (mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index]);
431 memstat_set_percpu_caller_uint64(struct memory_type *mtp, int cpu, int index,
435 mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index] = value;
439 memstat_get_percpu_free(const struct memory_type *mtp, int cpu)
442 return (mtp->mt_percpu_cache[cpu].mtp_free);