memstat_malloc.c revision 148354
1147997Srwatson/*-
2147997Srwatson * Copyright (c) 2005 Robert N. M. Watson
3147997Srwatson * All rights reserved.
4147997Srwatson *
5147997Srwatson * Redistribution and use in source and binary forms, with or without
6147997Srwatson * modification, are permitted provided that the following conditions
7147997Srwatson * are met:
8147997Srwatson * 1. Redistributions of source code must retain the above copyright
9147997Srwatson *    notice, this list of conditions and the following disclaimer.
10147997Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11147997Srwatson *    notice, this list of conditions and the following disclaimer in the
12147997Srwatson *    documentation and/or other materials provided with the distribution.
13147997Srwatson *
14147997Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15147997Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16147997Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17147997Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18147997Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19147997Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20147997Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21147997Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22147997Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23147997Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24147997Srwatson * SUCH DAMAGE.
25147997Srwatson *
26147997Srwatson * $FreeBSD: head/lib/libmemstat/memstat_malloc.c 148354 2005-07-23 21:17:15Z rwatson $
27147997Srwatson */
28147997Srwatson
29147997Srwatson#include <sys/param.h>
30147997Srwatson#include <sys/malloc.h>
31147997Srwatson#include <sys/sysctl.h>
32147997Srwatson
33147997Srwatson#include <err.h>
34147997Srwatson#include <errno.h>
35147997Srwatson#include <stdio.h>
36147997Srwatson#include <stdlib.h>
37147997Srwatson#include <string.h>
38147997Srwatson
39147997Srwatson#include "memstat.h"
40147997Srwatson#include "memstat_internal.h"
41147997Srwatson
42147997Srwatson/*
43147997Srwatson * Extract malloc(9) statistics from the running kernel, and store all memory
44147997Srwatson * type information in the passed list.  For each type, check the list for an
45147997Srwatson * existing entry with the right name/allocator -- if present, update that
46147997Srwatson * entry.  Otherwise, add a new entry.  On error, the entire list will be
47147997Srwatson * cleared, as entries will be in an inconsistent state.
48147997Srwatson *
49147997Srwatson * To reduce the level of work for a list that starts empty, we keep around a
50147997Srwatson * hint as to whether it was empty when we began, so we can avoid searching
51147997Srwatson * the list for entries to update.  Updates are O(n^2) due to searching for
52147997Srwatson * each entry before adding it.
53147997Srwatson */
54147997Srwatsonint
55147997Srwatsonmemstat_sysctl_malloc(struct memory_type_list *list, int flags)
56147997Srwatson{
57147997Srwatson	struct malloc_type_stream_header *mtshp;
58147997Srwatson	struct malloc_type_header *mthp;
59147997Srwatson	struct malloc_type_stats *mtsp;
60147997Srwatson	struct memory_type *mtp;
61147997Srwatson	int count, error, hint_dontsearch, i, j, maxcpus;
62147997Srwatson	char *buffer, *p;
63147997Srwatson	size_t size;
64147997Srwatson
65147997Srwatson	hint_dontsearch = LIST_EMPTY(list);
66147997Srwatson
67147997Srwatson	/*
68147997Srwatson	 * Query the number of CPUs, number of malloc types so that we can
69147997Srwatson	 * guess an initial buffer size.  We loop until we succeed or really
70147997Srwatson	 * fail.  Note that the value of maxcpus we query using sysctl is not
71147997Srwatson	 * the version we use when processing the real data -- that is read
72147997Srwatson	 * from the header.
73147997Srwatson	 */
74147997Srwatsonretry:
75147997Srwatson	size = sizeof(maxcpus);
76147997Srwatson	if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
77147997Srwatson		error = errno;
78147997Srwatson		perror("kern.smp.maxcpus");
79147997Srwatson		errno = error;
80147997Srwatson		return (-1);
81147997Srwatson	}
82147997Srwatson	if (size != sizeof(maxcpus)) {
83148038Srwatson		fprintf(stderr, "kern.smp.maxcpus: wrong size");
84147997Srwatson		errno = EINVAL;
85147997Srwatson		return (-1);
86147997Srwatson	}
87147997Srwatson
88147997Srwatson	if (maxcpus > MEMSTAT_MAXCPU) {
89147997Srwatson		fprintf(stderr, "kern.smp.maxcpus: too many CPUs\n");
90147997Srwatson		errno = EINVAL;
91147997Srwatson		return (-1);
92147997Srwatson	}
93147997Srwatson
94147997Srwatson	size = sizeof(count);
95147997Srwatson	if (sysctlbyname("kern.malloc_count", &count, &size, NULL, 0) < 0) {
96147997Srwatson		error = errno;
97147997Srwatson		perror("kern.malloc_count");
98147997Srwatson		errno = error;
99147997Srwatson		return (-1);
100147997Srwatson	}
101147997Srwatson	if (size != sizeof(count)) {
102148038Srwatson		fprintf(stderr, "kern.malloc_count: wrong size");
103147997Srwatson		errno = EINVAL;
104147997Srwatson		return (-1);
105147997Srwatson	}
106147997Srwatson
107147997Srwatson	size = sizeof(*mthp) + count * (sizeof(*mthp) + sizeof(*mtsp) *
108147997Srwatson	    maxcpus);
109147997Srwatson
110147997Srwatson	buffer = malloc(size);
111147997Srwatson	if (buffer == NULL) {
112147997Srwatson		error = errno;
113147997Srwatson		perror("malloc");
114147997Srwatson		errno = error;
115147997Srwatson		return (-1);
116147997Srwatson	}
117147997Srwatson
118147997Srwatson	if (sysctlbyname("kern.malloc_stats", buffer, &size, NULL, 0) < 0) {
119147997Srwatson		/*
120147997Srwatson		 * XXXRW: ENOMEM is an ambiguous return, we should bound the
121147997Srwatson		 * number of loops, perhaps.
122147997Srwatson		 */
123147997Srwatson		if (errno == ENOMEM) {
124147997Srwatson			free(buffer);
125147997Srwatson			goto retry;
126147997Srwatson		}
127147997Srwatson		error = errno;
128147997Srwatson		free(buffer);
129147997Srwatson		perror("kern.malloc_stats");
130147997Srwatson		errno = error;
131147997Srwatson		return (-1);
132147997Srwatson	}
133147997Srwatson
134147997Srwatson	if (size == 0) {
135147997Srwatson		free(buffer);
136147997Srwatson		return (0);
137147997Srwatson	}
138147997Srwatson
139147997Srwatson	if (size < sizeof(*mtshp)) {
140147997Srwatson		fprintf(stderr, "sysctl_malloc: invalid malloc header");
141147997Srwatson		free(buffer);
142147997Srwatson		errno = EINVAL;
143147997Srwatson		return (-1);
144147997Srwatson	}
145147997Srwatson	p = buffer;
146147997Srwatson	mtshp = (struct malloc_type_stream_header *)p;
147147997Srwatson	p += sizeof(*mtshp);
148147997Srwatson
149147997Srwatson	if (mtshp->mtsh_version != MALLOC_TYPE_STREAM_VERSION) {
150147997Srwatson		fprintf(stderr, "sysctl_malloc: unknown malloc version");
151147997Srwatson		free(buffer);
152147997Srwatson		errno = EINVAL;
153147997Srwatson		return (-1);
154147997Srwatson	}
155147997Srwatson
156147997Srwatson	if (mtshp->mtsh_maxcpus > MEMSTAT_MAXCPU) {
157147997Srwatson		fprintf(stderr, "sysctl_malloc: too many CPUs");
158147997Srwatson		free(buffer);
159147997Srwatson		errno = EINVAL;
160147997Srwatson		return (-1);
161147997Srwatson	}
162147997Srwatson
163147997Srwatson	/*
164147997Srwatson	 * For the remainder of this function, we are quite trusting about
165147997Srwatson	 * the layout of structures and sizes, since we've determined we have
166147997Srwatson	 * a matching version and acceptable CPU count.
167147997Srwatson	 */
168147997Srwatson	maxcpus = mtshp->mtsh_maxcpus;
169147997Srwatson	count = mtshp->mtsh_count;
170147997Srwatson	for (i = 0; i < count; i++) {
171147997Srwatson		mthp = (struct malloc_type_header *)p;
172147997Srwatson		p += sizeof(*mthp);
173147997Srwatson
174147997Srwatson		if (hint_dontsearch == 0) {
175147997Srwatson			mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC,
176147997Srwatson			    mthp->mth_name);
177147997Srwatson		} else
178147997Srwatson			mtp = NULL;
179147997Srwatson		if (mtp == NULL)
180148354Srwatson			mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC,
181147997Srwatson			    mthp->mth_name);
182147997Srwatson		if (mtp == NULL) {
183147997Srwatson			memstat_mtl_free(list);
184147997Srwatson			free(buffer);
185147997Srwatson			errno = ENOMEM;
186147997Srwatson			perror("malloc");
187147997Srwatson			errno = ENOMEM;
188147997Srwatson			return (-1);
189147997Srwatson		}
190147997Srwatson
191147997Srwatson		/*
192147997Srwatson		 * Reset the statistics on a current node.
193147997Srwatson		 */
194148354Srwatson		_memstat_mt_reset_stats(mtp);
195147997Srwatson
196147997Srwatson		for (j = 0; j < maxcpus; j++) {
197147997Srwatson			mtsp = (struct malloc_type_stats *)p;
198147997Srwatson			p += sizeof(*mtsp);
199147997Srwatson
200147997Srwatson			/*
201147997Srwatson			 * Sumarize raw statistics across CPUs into coalesced
202147997Srwatson			 * statistics.
203147997Srwatson			 */
204147997Srwatson			mtp->mt_memalloced += mtsp->mts_memalloced;
205147997Srwatson			mtp->mt_memfreed += mtsp->mts_memfreed;
206147997Srwatson			mtp->mt_numallocs += mtsp->mts_numallocs;
207147997Srwatson			mtp->mt_numfrees += mtsp->mts_numfrees;
208147997Srwatson			mtp->mt_sizemask |= mtsp->mts_size;
209147997Srwatson
210147997Srwatson			/*
211147997Srwatson			 * Copies of per-CPU statistics.
212147997Srwatson			 */
213147997Srwatson			mtp->mt_percpu_alloc[j].mtp_memalloced =
214147997Srwatson			    mtsp->mts_memalloced;
215147997Srwatson			mtp->mt_percpu_alloc[j].mtp_memfreed =
216147997Srwatson			    mtsp->mts_memfreed;
217147997Srwatson			mtp->mt_percpu_alloc[j].mtp_numallocs =
218147997Srwatson			    mtsp->mts_numallocs;
219147997Srwatson			mtp->mt_percpu_alloc[j].mtp_numfrees =
220147997Srwatson			    mtsp->mts_numfrees;
221147997Srwatson			mtp->mt_percpu_alloc[j].mtp_sizemask =
222147997Srwatson			    mtsp->mts_size;
223147997Srwatson		}
224147997Srwatson
225147997Srwatson		/*
226147997Srwatson		 * Derived cross-CPU statistics.
227147997Srwatson		 */
228147997Srwatson		mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
229147997Srwatson		mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
230147997Srwatson	}
231147997Srwatson
232147997Srwatson	free(buffer);
233147997Srwatson
234147997Srwatson	return (0);
235147997Srwatson}
236