2 * Copyright (c) 2005 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>
39 #include "memstat_internal.h"
42 memstat_strerror(int error)
46 case MEMSTAT_ERROR_NOMEMORY:
47 return ("Cannot allocate memory");
48 case MEMSTAT_ERROR_VERSION:
49 return ("Version mismatch");
50 case MEMSTAT_ERROR_PERMISSION:
51 return ("Permission denied");
52 case MEMSTAT_ERROR_DATAERROR:
53 return ("Data format error");
54 case MEMSTAT_ERROR_KVM:
56 case MEMSTAT_ERROR_KVM_NOSYMBOL:
57 return ("KVM unable to find symbol");
58 case MEMSTAT_ERROR_KVM_SHORTREAD:
59 return ("KVM short read");
60 case MEMSTAT_ERROR_UNDEFINED:
62 return ("Unknown error");
66 struct memory_type_list *
67 memstat_mtl_alloc(void)
69 struct memory_type_list *mtlp;
71 mtlp = malloc(sizeof(*mtlp));
75 LIST_INIT(&mtlp->mtl_list);
76 mtlp->mtl_error = MEMSTAT_ERROR_UNDEFINED;
81 memstat_mtl_first(struct memory_type_list *list)
84 return (LIST_FIRST(&list->mtl_list));
88 memstat_mtl_next(struct memory_type *mtp)
91 return (LIST_NEXT(mtp, mt_list));
95 _memstat_mtl_empty(struct memory_type_list *list)
97 struct memory_type *mtp;
99 while ((mtp = LIST_FIRST(&list->mtl_list))) {
100 free(mtp->mt_percpu_alloc);
101 free(mtp->mt_percpu_cache);
102 LIST_REMOVE(mtp, mt_list);
108 memstat_mtl_free(struct memory_type_list *list)
111 _memstat_mtl_empty(list);
116 memstat_mtl_geterror(struct memory_type_list *list)
119 return (list->mtl_error);
123 * Look for an existing memory_type entry in a memory_type list, based on the
124 * allocator and name of the type. If not found, return NULL. No errno or
128 memstat_mtl_find(struct memory_type_list *list, int allocator,
131 struct memory_type *mtp;
133 LIST_FOREACH(mtp, &list->mtl_list, mt_list) {
134 if ((mtp->mt_allocator == allocator ||
135 allocator == ALLOCATOR_ANY) &&
136 strcmp(mtp->mt_name, name) == 0)
143 * Allocate a new memory_type with the specificed allocator type and name,
144 * then insert into the list. The structure will be zero'd.
146 * libmemstat(3) internal function.
149 _memstat_mt_allocate(struct memory_type_list *list, int allocator,
150 const char *name, int maxcpus)
152 struct memory_type *mtp;
154 mtp = malloc(sizeof(*mtp));
158 bzero(mtp, sizeof(*mtp));
160 mtp->mt_allocator = allocator;
161 mtp->mt_percpu_alloc = malloc(sizeof(struct mt_percpu_alloc_s) *
163 mtp->mt_percpu_cache = malloc(sizeof(struct mt_percpu_cache_s) *
165 strlcpy(mtp->mt_name, name, MEMTYPE_MAXNAME);
166 LIST_INSERT_HEAD(&list->mtl_list, mtp, mt_list);
171 * Reset any libmemstat(3)-owned statistics in a memory_type record so that
172 * it can be reused without incremental addition problems. Caller-owned
173 * memory is left "as-is", and must be updated by the caller if desired.
175 * libmemstat(3) internal function.
178 _memstat_mt_reset_stats(struct memory_type *mtp, int maxcpus)
182 mtp->mt_countlimit = 0;
183 mtp->mt_byteslimit = 0;
184 mtp->mt_sizemask = 0;
187 mtp->mt_memalloced = 0;
188 mtp->mt_memfreed = 0;
189 mtp->mt_numallocs = 0;
190 mtp->mt_numfrees = 0;
194 mtp->mt_failures = 0;
197 mtp->mt_zonefree = 0;
200 for (i = 0; i < maxcpus; i++) {
201 mtp->mt_percpu_alloc[i].mtp_memalloced = 0;
202 mtp->mt_percpu_alloc[i].mtp_memfreed = 0;
203 mtp->mt_percpu_alloc[i].mtp_numallocs = 0;
204 mtp->mt_percpu_alloc[i].mtp_numfrees = 0;
205 mtp->mt_percpu_alloc[i].mtp_sizemask = 0;
206 mtp->mt_percpu_cache[i].mtp_free = 0;
211 * Accessor methods for struct memory_type. Avoids encoding the structure
212 * ABI into the application.
215 memstat_get_name(const struct memory_type *mtp)
218 return (mtp->mt_name);
222 memstat_get_allocator(const struct memory_type *mtp)
225 return (mtp->mt_allocator);
229 memstat_get_countlimit(const struct memory_type *mtp)
232 return (mtp->mt_countlimit);
236 memstat_get_byteslimit(const struct memory_type *mtp)
239 return (mtp->mt_byteslimit);
243 memstat_get_sizemask(const struct memory_type *mtp)
246 return (mtp->mt_sizemask);
250 memstat_get_size(const struct memory_type *mtp)
253 return (mtp->mt_size);
257 memstat_get_rsize(const struct memory_type *mtp)
260 return (mtp->mt_rsize);
264 memstat_get_memalloced(const struct memory_type *mtp)
267 return (mtp->mt_memalloced);
271 memstat_get_memfreed(const struct memory_type *mtp)
274 return (mtp->mt_memfreed);
278 memstat_get_numallocs(const struct memory_type *mtp)
281 return (mtp->mt_numallocs);
285 memstat_get_numfrees(const struct memory_type *mtp)
288 return (mtp->mt_numfrees);
292 memstat_get_bytes(const struct memory_type *mtp)
295 return (mtp->mt_bytes);
299 memstat_get_count(const struct memory_type *mtp)
302 return (mtp->mt_count);
306 memstat_get_free(const struct memory_type *mtp)
309 return (mtp->mt_free);
313 memstat_get_failures(const struct memory_type *mtp)
316 return (mtp->mt_failures);
320 memstat_get_sleeps(const struct memory_type *mtp)
323 return (mtp->mt_sleeps);
327 memstat_get_caller_pointer(const struct memory_type *mtp, int index)
330 return (mtp->mt_caller_pointer[index]);
334 memstat_set_caller_pointer(struct memory_type *mtp, int index, void *value)
337 mtp->mt_caller_pointer[index] = value;
341 memstat_get_caller_uint64(const struct memory_type *mtp, int index)
344 return (mtp->mt_caller_uint64[index]);
348 memstat_set_caller_uint64(struct memory_type *mtp, int index, uint64_t value)
351 mtp->mt_caller_uint64[index] = value;
355 memstat_get_zonefree(const struct memory_type *mtp)
358 return (mtp->mt_zonefree);
362 memstat_get_kegfree(const struct memory_type *mtp)
365 return (mtp->mt_kegfree);
369 memstat_get_percpu_memalloced(const struct memory_type *mtp, int cpu)
372 return (mtp->mt_percpu_alloc[cpu].mtp_memalloced);
376 memstat_get_percpu_memfreed(const struct memory_type *mtp, int cpu)
379 return (mtp->mt_percpu_alloc[cpu].mtp_memfreed);
383 memstat_get_percpu_numallocs(const struct memory_type *mtp, int cpu)
386 return (mtp->mt_percpu_alloc[cpu].mtp_numallocs);
390 memstat_get_percpu_numfrees(const struct memory_type *mtp, int cpu)
393 return (mtp->mt_percpu_alloc[cpu].mtp_numfrees);
397 memstat_get_percpu_sizemask(const struct memory_type *mtp, int cpu)
400 return (mtp->mt_percpu_alloc[cpu].mtp_sizemask);
404 memstat_get_percpu_caller_pointer(const struct memory_type *mtp, int cpu,
408 return (mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index]);
412 memstat_set_percpu_caller_pointer(struct memory_type *mtp, int cpu,
413 int index, void *value)
416 mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index] = value;
420 memstat_get_percpu_caller_uint64(const struct memory_type *mtp, int cpu,
424 return (mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index]);
428 memstat_set_percpu_caller_uint64(struct memory_type *mtp, int cpu, int index,
432 mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index] = value;
436 memstat_get_percpu_free(const struct memory_type *mtp, int cpu)
439 return (mtp->mt_percpu_cache[cpu].mtp_free);