memstat_uma.c revision 148619
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 148619 2005-08-01 13:18:21Z 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, hint_dontsearch, i, j, maxcpus;
63	char *buffer, *p;
64	size_t size;
65
66	hint_dontsearch = LIST_EMPTY(&list->mtl_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		if (errno == EACCES || errno == EPERM)
79			list->mtl_error = MEMSTAT_ERROR_PERMISSION;
80		else
81			list->mtl_error = MEMSTAT_ERROR_DATAERROR;
82		return (-1);
83	}
84	if (size != sizeof(maxcpus)) {
85		list->mtl_error = MEMSTAT_ERROR_DATAERROR;
86		return (-1);
87	}
88
89	if (maxcpus > MEMSTAT_MAXCPU) {
90		list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
91		return (-1);
92	}
93
94	size = sizeof(count);
95	if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) {
96		if (errno == EACCES || errno == EPERM)
97			list->mtl_error = MEMSTAT_ERROR_PERMISSION;
98		else
99			list->mtl_error = MEMSTAT_ERROR_VERSION;
100		return (-1);
101	}
102	if (size != sizeof(count)) {
103		list->mtl_error = MEMSTAT_ERROR_DATAERROR;
104		return (-1);
105	}
106
107	size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) *
108	    maxcpus);
109
110	buffer = malloc(size);
111	if (buffer == NULL) {
112		list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
113		return (-1);
114	}
115
116	if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) {
117		/*
118		 * XXXRW: ENOMEM is an ambiguous return, we should bound the
119		 * number of loops, perhaps.
120		 */
121		if (errno == ENOMEM) {
122			free(buffer);
123			goto retry;
124		}
125		if (errno == EACCES || errno == EPERM)
126			list->mtl_error = MEMSTAT_ERROR_PERMISSION;
127		else
128			list->mtl_error = MEMSTAT_ERROR_VERSION;
129		free(buffer);
130		return (-1);
131	}
132
133	if (size == 0) {
134		free(buffer);
135		return (0);
136	}
137
138	if (size < sizeof(*ushp)) {
139		list->mtl_error = MEMSTAT_ERROR_VERSION;
140		free(buffer);
141		return (-1);
142	}
143	p = buffer;
144	ushp = (struct uma_stream_header *)p;
145	p += sizeof(*ushp);
146
147	if (ushp->ush_version != UMA_STREAM_VERSION) {
148		list->mtl_error = MEMSTAT_ERROR_VERSION;
149		free(buffer);
150		return (-1);
151	}
152
153	if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) {
154		list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
155		free(buffer);
156		return (-1);
157	}
158
159	/*
160	 * For the remainder of this function, we are quite trusting about
161	 * the layout of structures and sizes, since we've determined we have
162	 * a matching version and acceptable CPU count.
163	 */
164	maxcpus = ushp->ush_maxcpus;
165	count = ushp->ush_count;
166	for (i = 0; i < count; i++) {
167		uthp = (struct uma_type_header *)p;
168		p += sizeof(*uthp);
169
170		if (hint_dontsearch == 0) {
171			mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
172			    uthp->uth_name);
173		} else
174			mtp = NULL;
175		if (mtp == NULL)
176			mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
177			    uthp->uth_name);
178		if (mtp == NULL) {
179			_memstat_mtl_empty(list);
180			free(buffer);
181			list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
182			return (-1);
183		}
184
185		/*
186		 * Reset the statistics on a current node.
187		 */
188		_memstat_mt_reset_stats(mtp);
189
190		mtp->mt_numallocs = uthp->uth_allocs;
191		mtp->mt_numfrees = uthp->uth_frees;
192		mtp->mt_failures = uthp->uth_fails;
193
194		for (j = 0; j < maxcpus; j++) {
195			upsp = (struct uma_percpu_stat *)p;
196			p += sizeof(*upsp);
197
198			mtp->mt_percpu_cache[j].mtp_free =
199			    upsp->ups_cache_free;
200			mtp->mt_free += upsp->ups_cache_free;
201			mtp->mt_numallocs += upsp->ups_allocs;
202			mtp->mt_numfrees += upsp->ups_frees;
203		}
204
205		mtp->mt_size = uthp->uth_size;
206		mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size;
207		mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size;
208		mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
209		mtp->mt_countlimit = uthp->uth_limit;
210		mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size;
211
212		mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
213		mtp->mt_zonefree = uthp->uth_zone_free;
214
215		/*
216		 * UMA secondary zones share a keg with the primary zone.  To
217		 * avoid double-reporting of free items, report keg free
218		 * items only in the primary zone.
219		 */
220		if (!(uthp->uth_zone_flags & UTH_ZONE_SECONDARY)) {
221			mtp->mt_kegfree = uthp->uth_keg_free;
222			mtp->mt_free += mtp->mt_kegfree;
223		}
224		mtp->mt_free += mtp->mt_zonefree;
225	}
226
227	free(buffer);
228
229	return (0);
230}
231