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/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/malloc.h>
32 #include <sys/sysctl.h>
43 #include "memstat_internal.h"
45 static struct nlist namelist[] = {
46 #define X_KMEMSTATISTICS 0
47 { .n_name = "_kmemstatistics" },
48 #define X_MP_MAXCPUS 1
49 { .n_name = "_mp_maxcpus" },
54 * Extract malloc(9) statistics from the running kernel, and store all memory
55 * type information in the passed list. For each type, check the list for an
56 * existing entry with the right name/allocator -- if present, update that
57 * entry. Otherwise, add a new entry. On error, the entire list will be
58 * cleared, as entries will be in an inconsistent state.
60 * To reduce the level of work for a list that starts empty, we keep around a
61 * hint as to whether it was empty when we began, so we can avoid searching
62 * the list for entries to update. Updates are O(n^2) due to searching for
63 * each entry before adding it.
66 memstat_sysctl_malloc(struct memory_type_list *list, int flags)
68 struct malloc_type_stream_header *mtshp;
69 struct malloc_type_header *mthp;
70 struct malloc_type_stats *mtsp;
71 struct memory_type *mtp;
72 int count, hint_dontsearch, i, j, maxcpus;
76 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
79 * Query the number of CPUs, number of malloc types so that we can
80 * guess an initial buffer size. We loop until we succeed or really
81 * fail. Note that the value of maxcpus we query using sysctl is not
82 * the version we use when processing the real data -- that is read
86 size = sizeof(maxcpus);
87 if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
88 if (errno == EACCES || errno == EPERM)
89 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
91 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
94 if (size != sizeof(maxcpus)) {
95 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
99 if (maxcpus > MEMSTAT_MAXCPU) {
100 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
104 size = sizeof(count);
105 if (sysctlbyname("kern.malloc_count", &count, &size, NULL, 0) < 0) {
106 if (errno == EACCES || errno == EPERM)
107 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
109 list->mtl_error = MEMSTAT_ERROR_VERSION;
112 if (size != sizeof(count)) {
113 list->mtl_error = MEMSTAT_ERROR_DATAERROR;
117 size = sizeof(*mthp) + count * (sizeof(*mthp) + sizeof(*mtsp) *
120 buffer = malloc(size);
121 if (buffer == NULL) {
122 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
126 if (sysctlbyname("kern.malloc_stats", buffer, &size, NULL, 0) < 0) {
128 * XXXRW: ENOMEM is an ambiguous return, we should bound the
129 * number of loops, perhaps.
131 if (errno == ENOMEM) {
135 if (errno == EACCES || errno == EPERM)
136 list->mtl_error = MEMSTAT_ERROR_PERMISSION;
138 list->mtl_error = MEMSTAT_ERROR_VERSION;
148 if (size < sizeof(*mtshp)) {
149 list->mtl_error = MEMSTAT_ERROR_VERSION;
154 mtshp = (struct malloc_type_stream_header *)p;
157 if (mtshp->mtsh_version != MALLOC_TYPE_STREAM_VERSION) {
158 list->mtl_error = MEMSTAT_ERROR_VERSION;
163 if (mtshp->mtsh_maxcpus > MEMSTAT_MAXCPU) {
164 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
170 * For the remainder of this function, we are quite trusting about
171 * the layout of structures and sizes, since we've determined we have
172 * a matching version and acceptable CPU count.
174 maxcpus = mtshp->mtsh_maxcpus;
175 count = mtshp->mtsh_count;
176 for (i = 0; i < count; i++) {
177 mthp = (struct malloc_type_header *)p;
180 if (hint_dontsearch == 0) {
181 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC,
186 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC,
189 _memstat_mtl_empty(list);
191 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
196 * Reset the statistics on a current node.
198 _memstat_mt_reset_stats(mtp);
200 for (j = 0; j < maxcpus; j++) {
201 mtsp = (struct malloc_type_stats *)p;
205 * Sumarize raw statistics across CPUs into coalesced
208 mtp->mt_memalloced += mtsp->mts_memalloced;
209 mtp->mt_memfreed += mtsp->mts_memfreed;
210 mtp->mt_numallocs += mtsp->mts_numallocs;
211 mtp->mt_numfrees += mtsp->mts_numfrees;
212 mtp->mt_sizemask |= mtsp->mts_size;
215 * Copies of per-CPU statistics.
217 mtp->mt_percpu_alloc[j].mtp_memalloced =
218 mtsp->mts_memalloced;
219 mtp->mt_percpu_alloc[j].mtp_memfreed =
221 mtp->mt_percpu_alloc[j].mtp_numallocs =
223 mtp->mt_percpu_alloc[j].mtp_numfrees =
225 mtp->mt_percpu_alloc[j].mtp_sizemask =
230 * Derived cross-CPU statistics.
232 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
233 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
242 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
247 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
250 return (MEMSTAT_ERROR_KVM);
251 if ((size_t)ret != size)
252 return (MEMSTAT_ERROR_KVM_SHORTREAD);
257 kread_string(kvm_t *kvm, const void *kvm_pointer, char *buffer, int buflen)
262 for (i = 0; i < buflen; i++) {
263 ret = kvm_read(kvm, __DECONST(unsigned long, kvm_pointer) +
264 i, &(buffer[i]), sizeof(char));
266 return (MEMSTAT_ERROR_KVM);
267 if ((size_t)ret != sizeof(char))
268 return (MEMSTAT_ERROR_KVM_SHORTREAD);
269 if (buffer[i] == '\0')
278 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
283 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
285 return (MEMSTAT_ERROR_KVM);
286 if ((size_t)ret != size)
287 return (MEMSTAT_ERROR_KVM_SHORTREAD);
292 memstat_kvm_malloc(struct memory_type_list *list, void *kvm_handle)
294 struct memory_type *mtp;
295 void *kmemstatistics;
296 int hint_dontsearch, j, mp_maxcpus, ret;
297 char name[MEMTYPE_MAXNAME];
298 struct malloc_type_stats mts[MEMSTAT_MAXCPU], *mtsp;
299 struct malloc_type type, *typep;
302 kvm = (kvm_t *)kvm_handle;
304 hint_dontsearch = LIST_EMPTY(&list->mtl_list);
306 if (kvm_nlist(kvm, namelist) != 0) {
307 list->mtl_error = MEMSTAT_ERROR_KVM;
311 if (namelist[X_KMEMSTATISTICS].n_type == 0 ||
312 namelist[X_KMEMSTATISTICS].n_value == 0) {
313 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL;
317 ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus,
318 sizeof(mp_maxcpus), 0);
320 list->mtl_error = ret;
324 if (mp_maxcpus > MEMSTAT_MAXCPU) {
325 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
329 ret = kread_symbol(kvm, X_KMEMSTATISTICS, &kmemstatistics,
330 sizeof(kmemstatistics), 0);
332 list->mtl_error = ret;
336 for (typep = kmemstatistics; typep != NULL; typep = type.ks_next) {
337 ret = kread(kvm, typep, &type, sizeof(type), 0);
339 _memstat_mtl_empty(list);
340 list->mtl_error = ret;
343 ret = kread_string(kvm, (void *)type.ks_shortdesc, name,
346 _memstat_mtl_empty(list);
347 list->mtl_error = ret;
352 * Take advantage of explicit knowledge that
353 * malloc_type_internal is simply an array of statistics
354 * structures of number MAXCPU. Since our compile-time
355 * value for MAXCPU may differ from the kernel's, we
356 * populate our own array.
358 ret = kread(kvm, type.ks_handle, mts, mp_maxcpus *
359 sizeof(struct malloc_type_stats), 0);
361 _memstat_mtl_empty(list);
362 list->mtl_error = ret;
366 if (hint_dontsearch == 0) {
367 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC, name);
371 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC,
374 _memstat_mtl_empty(list);
375 list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
380 * This logic is replicated from kern_malloc.c, and should
383 _memstat_mt_reset_stats(mtp);
384 for (j = 0; j < mp_maxcpus; j++) {
386 mtp->mt_memalloced += mtsp->mts_memalloced;
387 mtp->mt_memfreed += mtsp->mts_memfreed;
388 mtp->mt_numallocs += mtsp->mts_numallocs;
389 mtp->mt_numfrees += mtsp->mts_numfrees;
390 mtp->mt_sizemask |= mtsp->mts_size;
392 mtp->mt_percpu_alloc[j].mtp_memalloced =
393 mtsp->mts_memalloced;
394 mtp->mt_percpu_alloc[j].mtp_memfreed =
396 mtp->mt_percpu_alloc[j].mtp_numallocs =
398 mtp->mt_percpu_alloc[j].mtp_numfrees =
400 mtp->mt_percpu_alloc[j].mtp_sizemask =
404 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
405 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;