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/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/sysctl.h>
45 #include "memstat_internal.h"
47 static struct nlist namelist[] = {
48 #define X_KMEMSTATISTICS 0
49 { .n_name = "_kmemstatistics" },
50 #define X_MP_MAXCPUS 1
51 { .n_name = "_mp_maxcpus" },
56 * Extract malloc(9) statistics from the running kernel, and store all memory
57 * type information in the passed list. For each type, check the list for an
58 * existing entry with the right name/allocator -- if present, update that
59 * entry. Otherwise, add a new entry. On error, the entire list will be
60 * cleared, as entries will be in an inconsistent state.
62 * To reduce the level of work for a list that starts empty, we keep around a
63 * hint as to whether it was empty when we began, so we can avoid searching
64 * the list for entries to update. Updates are O(n^2) due to searching for
65 * each entry before adding it.
68 memstat_sysctl_malloc(struct memory_type_list *list, int flags)
70 struct malloc_type_stream_header *mtshp;
71 struct malloc_type_header *mthp;
72 struct malloc_type_stats *mtsp;
73 struct memory_type *mtp;
74 int count, hint_dontsearch, i, j, maxcpus;
78 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
81 * Query the number of CPUs, number of malloc types so that we can
82 * guess an initial buffer size. We loop until we succeed or really
83 * fail. Note that the value of maxcpus we query using sysctl is not
84 * the version we use when processing the real data -- that is read
88 size = sizeof(maxcpus);
89 if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
90 if (errno == EACCES || errno == EPERM)
91 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
93 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
96 if (size != sizeof(maxcpus)) {
97 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
101 size = sizeof(count);
102 if (sysctlbyname("kern.malloc_count", &count, &size, NULL, 0) < 0) {
103 if (errno == EACCES || errno == EPERM)
104 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
106 list->mtl_error = MEMSTAT_ERROR_VERSION;
109 if (size != sizeof(count)) {
110 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
114 size = sizeof(*mthp) + count * (sizeof(*mthp) + sizeof(*mtsp) *
117 buffer = malloc(size);
118 if (buffer == NULL) {
119 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
123 if (sysctlbyname("kern.malloc_stats", buffer, &size, NULL, 0) < 0) {
125 * XXXRW: ENOMEM is an ambiguous return, we should bound the
126 * number of loops, perhaps.
128 if (errno == ENOMEM) {
132 if (errno == EACCES || errno == EPERM)
133 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
135 list->mtl_error = MEMSTAT_ERROR_VERSION;
145 if (size < sizeof(*mtshp)) {
146 list->mtl_error = MEMSTAT_ERROR_VERSION;
151 mtshp = (struct malloc_type_stream_header *)p;
154 if (mtshp->mtsh_version != MALLOC_TYPE_STREAM_VERSION) {
155 list->mtl_error = MEMSTAT_ERROR_VERSION;
161 * For the remainder of this function, we are quite trusting about
162 * the layout of structures and sizes, since we've determined we have
163 * a matching version and acceptable CPU count.
165 maxcpus = mtshp->mtsh_maxcpus;
166 count = mtshp->mtsh_count;
167 for (i = 0; i < count; i++) {
168 mthp = (struct malloc_type_header *)p;
171 if (hint_dontsearch == 0) {
172 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC,
177 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC,
178 mthp->mth_name, maxcpus);
180 _memstat_mtl_empty(list);
182 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
187 * Reset the statistics on a current node.
189 _memstat_mt_reset_stats(mtp, maxcpus);
191 for (j = 0; j < maxcpus; j++) {
192 mtsp = (struct malloc_type_stats *)p;
196 * Sumarize raw statistics across CPUs into coalesced
199 mtp->mt_memalloced += mtsp->mts_memalloced;
200 mtp->mt_memfreed += mtsp->mts_memfreed;
201 mtp->mt_numallocs += mtsp->mts_numallocs;
202 mtp->mt_numfrees += mtsp->mts_numfrees;
203 mtp->mt_sizemask |= mtsp->mts_size;
206 * Copies of per-CPU statistics.
208 mtp->mt_percpu_alloc[j].mtp_memalloced =
209 mtsp->mts_memalloced;
210 mtp->mt_percpu_alloc[j].mtp_memfreed =
212 mtp->mt_percpu_alloc[j].mtp_numallocs =
214 mtp->mt_percpu_alloc[j].mtp_numfrees =
216 mtp->mt_percpu_alloc[j].mtp_sizemask =
221 * Derived cross-CPU statistics.
223 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
224 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
233 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
238 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
241 return (MEMSTAT_ERROR_KVM);
242 if ((size_t)ret != size)
243 return (MEMSTAT_ERROR_KVM_SHORTREAD);
248 kread_string(kvm_t *kvm, const void *kvm_pointer, char *buffer, int buflen)
253 for (i = 0; i < buflen; i++) {
254 ret = kvm_read(kvm, __DECONST(unsigned long, kvm_pointer) +
255 i, &(buffer[i]), sizeof(char));
257 return (MEMSTAT_ERROR_KVM);
258 if ((size_t)ret != sizeof(char))
259 return (MEMSTAT_ERROR_KVM_SHORTREAD);
260 if (buffer[i] == '\0')
269 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
274 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
276 return (MEMSTAT_ERROR_KVM);
277 if ((size_t)ret != size)
278 return (MEMSTAT_ERROR_KVM_SHORTREAD);
283 memstat_kvm_malloc(struct memory_type_list *list, void *kvm_handle)
285 struct memory_type *mtp;
286 void *kmemstatistics;
287 int hint_dontsearch, j, mp_maxcpus, ret;
288 char name[MEMTYPE_MAXNAME];
289 struct malloc_type_stats *mts, *mtsp;
290 struct malloc_type_internal *mtip;
291 struct malloc_type type, *typep;
294 kvm = (kvm_t *)kvm_handle;
296 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
298 if (kvm_nlist(kvm, namelist) != 0) {
299 list->mtl_error = MEMSTAT_ERROR_KVM;
303 if (namelist[X_KMEMSTATISTICS].n_type == 0 ||
304 namelist[X_KMEMSTATISTICS].n_value == 0) {
305 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL;
309 ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus,
310 sizeof(mp_maxcpus), 0);
312 list->mtl_error = ret;
316 ret = kread_symbol(kvm, X_KMEMSTATISTICS, &kmemstatistics,
317 sizeof(kmemstatistics), 0);
319 list->mtl_error = ret;
323 mts = malloc(sizeof(struct malloc_type_stats) * mp_maxcpus);
325 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
329 for (typep = kmemstatistics; typep != NULL; typep = type.ks_next) {
330 ret = kread(kvm, typep, &type, sizeof(type), 0);
332 _memstat_mtl_empty(list);
334 list->mtl_error = ret;
337 ret = kread_string(kvm, (void *)type.ks_shortdesc, name,
340 _memstat_mtl_empty(list);
342 list->mtl_error = ret;
347 * Since our compile-time value for MAXCPU may differ from the
348 * kernel's, we populate our own array.
350 mtip = type.ks_handle;
351 ret = kread(kvm, mtip->mti_stats, mts, mp_maxcpus *
352 sizeof(struct malloc_type_stats), 0);
354 _memstat_mtl_empty(list);
356 list->mtl_error = ret;
360 if (hint_dontsearch == 0) {
361 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC, name);
365 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC,
368 _memstat_mtl_empty(list);
370 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
375 * This logic is replicated from kern_malloc.c, and should
378 _memstat_mt_reset_stats(mtp, mp_maxcpus);
379 for (j = 0; j < mp_maxcpus; j++) {
381 mtp->mt_memalloced += mtsp->mts_memalloced;
382 mtp->mt_memfreed += mtsp->mts_memfreed;
383 mtp->mt_numallocs += mtsp->mts_numallocs;
384 mtp->mt_numfrees += mtsp->mts_numfrees;
385 mtp->mt_sizemask |= mtsp->mts_size;
387 mtp->mt_percpu_alloc[j].mtp_memalloced =
388 mtsp->mts_memalloced;
389 mtp->mt_percpu_alloc[j].mtp_memfreed =
391 mtp->mt_percpu_alloc[j].mtp_numallocs =
393 mtp->mt_percpu_alloc[j].mtp_numfrees =
395 mtp->mt_percpu_alloc[j].mtp_sizemask =
399 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
400 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;