memstat_uma.c revision 148007
1/*-
2 * Copyright (c) 2005 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/lib/libmemstat/memstat_uma.c 148007 2005-07-14 20:01:04Z rwatson $
27 */
28
29#include <sys/param.h>
30#include <sys/sysctl.h>
31
32#include <vm/uma.h>
33
34#include <err.h>
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include "memstat.h"
41#include "memstat_internal.h"
42
43/*
44 * Extract uma(9) statistics from the running kernel, and store all memory
45 * type information in the passed list.  For each type, check the list for an
46 * existing entry with the right name/allocator -- if present, update that
47 * entry.  Otherwise, add a new entry.  On error, the entire list will be
48 * cleared, as entries will be in an inconsistent state.
49 *
50 * To reduce the level of work for a list that starts empty, we keep around a
51 * hint as to whether it was empty when we began, so we can avoid searching
52 * the list for entries to update.  Updates are O(n^2) due to searching for
53 * each entry before adding it.
54 */
55int
56memstat_sysctl_uma(struct memory_type_list *list, int flags)
57{
58	struct uma_stream_header *ushp;
59	struct uma_type_header *uthp;
60	struct uma_percpu_stat *upsp;
61	struct memory_type *mtp;
62	int count, error, hint_dontsearch, i, j, maxcpus;
63	char *buffer, *p;
64	size_t size;
65
66	hint_dontsearch = LIST_EMPTY(list);
67
68	/*
69	 * Query the number of CPUs, number of malloc types so that we can
70	 * guess an initial buffer size.  We loop until we succeed or really
71	 * fail.  Note that the value of maxcpus we query using sysctl is not
72	 * the version we use when processing the real data -- that is read
73	 * from the header.
74	 */
75retry:
76	size = sizeof(maxcpus);
77	if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
78		error = errno;
79		perror("kern.smp.maxcpus");
80		errno = error;
81		return (-1);
82	}
83	if (size != sizeof(maxcpus)) {
84		fprintf(stderr, "kern.smp.maxcpus: wronge size");
85		errno = EINVAL;
86		return (-1);
87	}
88
89	if (maxcpus > MEMSTAT_MAXCPU) {
90		fprintf(stderr, "kern.smp.maxcpus: too many CPUs\n");
91		errno = EINVAL;
92		return (-1);
93	}
94
95	size = sizeof(count);
96	if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) {
97		error = errno;
98		perror("vm.zone_count");
99		errno = error;
100		return (-1);
101	}
102	if (size != sizeof(count)) {
103		fprintf(stderr, "vm.zone_count: wronge size");
104		errno = EINVAL;
105		return (-1);
106	}
107
108	size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) *
109	    maxcpus);
110
111	buffer = malloc(size);
112	if (buffer == NULL) {
113		error = errno;
114		perror("malloc");
115		errno = error;
116		return (-1);
117	}
118
119	if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) {
120		/*
121		 * XXXRW: ENOMEM is an ambiguous return, we should bound the
122		 * number of loops, perhaps.
123		 */
124		if (errno == ENOMEM) {
125			free(buffer);
126			goto retry;
127		}
128		error = errno;
129		free(buffer);
130		perror("vm.zone_stats");
131		errno = error;
132		return (-1);
133	}
134
135	if (size == 0) {
136		free(buffer);
137		return (0);
138	}
139
140	if (size < sizeof(*ushp)) {
141		fprintf(stderr, "sysctl_uma: invalid malloc header");
142		free(buffer);
143		errno = EINVAL;
144		return (-1);
145	}
146	p = buffer;
147	ushp = (struct uma_stream_header *)p;
148	p += sizeof(*ushp);
149
150	if (ushp->ush_version != UMA_STREAM_VERSION) {
151		fprintf(stderr, "sysctl_uma: unknown malloc version");
152		free(buffer);
153		errno = EINVAL;
154		return (-1);
155	}
156
157	if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) {
158		fprintf(stderr, "sysctl_uma: too many CPUs");
159		free(buffer);
160		errno = EINVAL;
161		return (-1);
162	}
163
164	/*
165	 * For the remainder of this function, we are quite trusting about
166	 * the layout of structures and sizes, since we've determined we have
167	 * a matching version and acceptable CPU count.
168	 */
169	maxcpus = ushp->ush_maxcpus;
170	count = ushp->ush_count;
171	for (i = 0; i < count; i++) {
172		uthp = (struct uma_type_header *)p;
173		p += sizeof(*uthp);
174
175		if (hint_dontsearch == 0) {
176			mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
177			    uthp->uth_name);
178			/*
179			 * Reset the statistics on a reused node.
180			 */
181			if (mtp != NULL)
182				memstat_mt_reset_stats(mtp);
183		} else
184			mtp = NULL;
185		if (mtp == NULL)
186			mtp = memstat_mt_allocate(list, ALLOCATOR_UMA,
187			    uthp->uth_name);
188		if (mtp == NULL) {
189			memstat_mtl_free(list);
190			free(buffer);
191			errno = ENOMEM;
192			perror("malloc");
193			errno = ENOMEM;
194			return (-1);
195		}
196
197		/*
198		 * Reset the statistics on a current node.
199		 */
200		memstat_mt_reset_stats(mtp);
201
202		mtp->mt_numallocs = uthp->uth_allocs;
203		mtp->mt_numfrees = uthp->uth_frees;
204
205		for (j = 0; j < maxcpus; j++) {
206			upsp = (struct uma_percpu_stat *)p;
207			p += sizeof(*upsp);
208
209			mtp->mt_percpu_cache[j].mtp_free =
210			    upsp->ups_cache_free;
211			mtp->mt_free += upsp->ups_cache_free;
212			mtp->mt_numallocs += upsp->ups_allocs;
213			mtp->mt_numfrees += upsp->ups_frees;
214		}
215
216		mtp->mt_size = uthp->uth_size;
217		mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size;
218		mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size;
219		mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
220		mtp->mt_countlimit = uthp->uth_limit;
221		mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size;
222
223		mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
224		mtp->mt_zonefree = uthp->uth_zone_free + uthp->uth_keg_free;
225		mtp->mt_free += mtp->mt_zonefree;
226	}
227
228	free(buffer);
229
230	return (0);
231}
232