2 * SPDX-License-Identifier: BSD-2-Clause
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
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/sysctl.h>
40 #include "memstat_internal.h"
43 memstat_strerror(int error)
47 case MEMSTAT_ERROR_NOMEMORY:
48 return ("Cannot allocate memory");
49 case MEMSTAT_ERROR_VERSION:
50 return ("Version mismatch");
51 case MEMSTAT_ERROR_PERMISSION:
52 return ("Permission denied");
53 case MEMSTAT_ERROR_DATAERROR:
54 return ("Data format error");
55 case MEMSTAT_ERROR_KVM:
57 case MEMSTAT_ERROR_KVM_NOSYMBOL:
58 return ("KVM unable to find symbol");
59 case MEMSTAT_ERROR_KVM_SHORTREAD:
60 return ("KVM short read");
61 case MEMSTAT_ERROR_UNDEFINED:
63 return ("Unknown error");
67 struct memory_type_list *
68 memstat_mtl_alloc(void)
70 struct memory_type_list *mtlp;
72 mtlp = malloc(sizeof(*mtlp));
76 LIST_INIT(&mtlp->mtl_list);
77 mtlp->mtl_error = MEMSTAT_ERROR_UNDEFINED;
82 memstat_mtl_first(struct memory_type_list *list)
85 return (LIST_FIRST(&list->mtl_list));
89 memstat_mtl_next(struct memory_type *mtp)
92 return (LIST_NEXT(mtp, mt_list));
96 _memstat_mtl_empty(struct memory_type_list *list)
98 struct memory_type *mtp;
100 while ((mtp = LIST_FIRST(&list->mtl_list))) {
101 free(mtp->mt_percpu_alloc);
102 free(mtp->mt_percpu_cache);
103 LIST_REMOVE(mtp, mt_list);
109 memstat_mtl_free(struct memory_type_list *list)
112 _memstat_mtl_empty(list);
117 memstat_mtl_geterror(struct memory_type_list *list)
120 return (list->mtl_error);
124 * Look for an existing memory_type entry in a memory_type list, based on the
125 * allocator and name of the type. If not found, return NULL. No errno or
129 memstat_mtl_find(struct memory_type_list *list, int allocator,
132 struct memory_type *mtp;
134 LIST_FOREACH(mtp, &list->mtl_list, mt_list) {
135 if ((mtp->mt_allocator == allocator ||
136 allocator == ALLOCATOR_ANY) &&
137 strcmp(mtp->mt_name, name) == 0)
144 * Allocate a new memory_type with the specificed allocator type and name,
145 * then insert into the list. The structure will be zero'd.
147 * libmemstat(3) internal function.
150 _memstat_mt_allocate(struct memory_type_list *list, int allocator,
151 const char *name, int maxcpus)
153 struct memory_type *mtp;
155 mtp = malloc(sizeof(*mtp));
159 bzero(mtp, sizeof(*mtp));
161 mtp->mt_allocator = allocator;
162 mtp->mt_percpu_alloc = malloc(sizeof(struct mt_percpu_alloc_s) *
164 mtp->mt_percpu_cache = malloc(sizeof(struct mt_percpu_cache_s) *
166 strlcpy(mtp->mt_name, name, MEMTYPE_MAXNAME);
167 LIST_INSERT_HEAD(&list->mtl_list, mtp, mt_list);
172 * Reset any libmemstat(3)-owned statistics in a memory_type record so that
173 * it can be reused without incremental addition problems. Caller-owned
174 * memory is left "as-is", and must be updated by the caller if desired.
176 * libmemstat(3) internal function.
179 _memstat_mt_reset_stats(struct memory_type *mtp, int maxcpus)
183 mtp->mt_countlimit = 0;
184 mtp->mt_byteslimit = 0;
185 mtp->mt_sizemask = 0;
188 mtp->mt_memalloced = 0;
189 mtp->mt_memfreed = 0;
190 mtp->mt_numallocs = 0;
191 mtp->mt_numfrees = 0;
195 mtp->mt_failures = 0;
198 mtp->mt_zonefree = 0;
201 for (i = 0; i < maxcpus; i++) {
202 mtp->mt_percpu_alloc[i].mtp_memalloced = 0;
203 mtp->mt_percpu_alloc[i].mtp_memfreed = 0;
204 mtp->mt_percpu_alloc[i].mtp_numallocs = 0;
205 mtp->mt_percpu_alloc[i].mtp_numfrees = 0;
206 mtp->mt_percpu_alloc[i].mtp_sizemask = 0;
207 mtp->mt_percpu_cache[i].mtp_free = 0;
212 * Accessor methods for struct memory_type. Avoids encoding the structure
213 * ABI into the application.
216 memstat_get_name(const struct memory_type *mtp)
219 return (mtp->mt_name);
223 memstat_get_allocator(const struct memory_type *mtp)
226 return (mtp->mt_allocator);
230 memstat_get_countlimit(const struct memory_type *mtp)
233 return (mtp->mt_countlimit);
237 memstat_get_byteslimit(const struct memory_type *mtp)
240 return (mtp->mt_byteslimit);
244 memstat_get_sizemask(const struct memory_type *mtp)
247 return (mtp->mt_sizemask);
251 memstat_get_size(const struct memory_type *mtp)
254 return (mtp->mt_size);
258 memstat_get_rsize(const struct memory_type *mtp)
261 return (mtp->mt_rsize);
265 memstat_get_memalloced(const struct memory_type *mtp)
268 return (mtp->mt_memalloced);
272 memstat_get_memfreed(const struct memory_type *mtp)
275 return (mtp->mt_memfreed);
279 memstat_get_numallocs(const struct memory_type *mtp)
282 return (mtp->mt_numallocs);
286 memstat_get_numfrees(const struct memory_type *mtp)
289 return (mtp->mt_numfrees);
293 memstat_get_bytes(const struct memory_type *mtp)
296 return (mtp->mt_bytes);
300 memstat_get_count(const struct memory_type *mtp)
303 return (mtp->mt_count);
307 memstat_get_free(const struct memory_type *mtp)
310 return (mtp->mt_free);
314 memstat_get_failures(const struct memory_type *mtp)
317 return (mtp->mt_failures);
321 memstat_get_sleeps(const struct memory_type *mtp)
324 return (mtp->mt_sleeps);
328 memstat_get_xdomain(const struct memory_type *mtp)
331 return (mtp->mt_xdomain);
335 memstat_get_caller_pointer(const struct memory_type *mtp, int index)
338 return (mtp->mt_caller_pointer[index]);
342 memstat_set_caller_pointer(struct memory_type *mtp, int index, void *value)
345 mtp->mt_caller_pointer[index] = value;
349 memstat_get_caller_uint64(const struct memory_type *mtp, int index)
352 return (mtp->mt_caller_uint64[index]);
356 memstat_set_caller_uint64(struct memory_type *mtp, int index, uint64_t value)
359 mtp->mt_caller_uint64[index] = value;
363 memstat_get_zonefree(const struct memory_type *mtp)
366 return (mtp->mt_zonefree);
370 memstat_get_kegfree(const struct memory_type *mtp)
373 return (mtp->mt_kegfree);
377 memstat_get_percpu_memalloced(const struct memory_type *mtp, int cpu)
380 return (mtp->mt_percpu_alloc[cpu].mtp_memalloced);
384 memstat_get_percpu_memfreed(const struct memory_type *mtp, int cpu)
387 return (mtp->mt_percpu_alloc[cpu].mtp_memfreed);
391 memstat_get_percpu_numallocs(const struct memory_type *mtp, int cpu)
394 return (mtp->mt_percpu_alloc[cpu].mtp_numallocs);
398 memstat_get_percpu_numfrees(const struct memory_type *mtp, int cpu)
401 return (mtp->mt_percpu_alloc[cpu].mtp_numfrees);
405 memstat_get_percpu_sizemask(const struct memory_type *mtp, int cpu)
408 return (mtp->mt_percpu_alloc[cpu].mtp_sizemask);
412 memstat_get_percpu_caller_pointer(const struct memory_type *mtp, int cpu,
416 return (mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index]);
420 memstat_set_percpu_caller_pointer(struct memory_type *mtp, int cpu,
421 int index, void *value)
424 mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index] = value;
428 memstat_get_percpu_caller_uint64(const struct memory_type *mtp, int cpu,
432 return (mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index]);
436 memstat_set_percpu_caller_uint64(struct memory_type *mtp, int cpu, int index,
440 mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index] = value;
444 memstat_get_percpu_free(const struct memory_type *mtp, int cpu)
447 return (mtp->mt_percpu_cache[cpu].mtp_free);