memstat_uma.c revision 147997
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_uma.c 147997 2005-07-14 17:40:02Z rwatson $
27147997Srwatson */
28147997Srwatson
29147997Srwatson#include <sys/param.h>
30147997Srwatson#include <sys/sysctl.h>
31147997Srwatson
32147997Srwatson#include <vm/uma.h>
33147997Srwatson
34147997Srwatson#include <err.h>
35147997Srwatson#include <errno.h>
36147997Srwatson#include <stdio.h>
37147997Srwatson#include <stdlib.h>
38147997Srwatson#include <string.h>
39147997Srwatson
40147997Srwatson#include "memstat.h"
41147997Srwatson#include "memstat_internal.h"
42147997Srwatson
43147997Srwatson/*
44147997Srwatson * Extract uma(9) statistics from the running kernel, and store all memory
45147997Srwatson * type information in the passed list.  For each type, check the list for an
46147997Srwatson * existing entry with the right name/allocator -- if present, update that
47147997Srwatson * entry.  Otherwise, add a new entry.  On error, the entire list will be
48147997Srwatson * cleared, as entries will be in an inconsistent state.
49147997Srwatson *
50147997Srwatson * To reduce the level of work for a list that starts empty, we keep around a
51147997Srwatson * hint as to whether it was empty when we began, so we can avoid searching
52147997Srwatson * the list for entries to update.  Updates are O(n^2) due to searching for
53147997Srwatson * each entry before adding it.
54147997Srwatson */
55147997Srwatsonint
56147997Srwatsonmemstat_sysctl_uma(struct memory_type_list *list, int flags)
57147997Srwatson{
58147997Srwatson	struct uma_stream_header *ushp;
59147997Srwatson	struct uma_type_header *uthp;
60147997Srwatson	struct uma_percpu_stat *upsp;
61147997Srwatson	struct memory_type *mtp;
62147997Srwatson	int count, error, hint_dontsearch, i, j, maxcpus;
63147997Srwatson	char *buffer, *p;
64147997Srwatson	size_t size;
65147997Srwatson
66147997Srwatson	hint_dontsearch = LIST_EMPTY(list);
67147997Srwatson
68147997Srwatson	/*
69147997Srwatson	 * Query the number of CPUs, number of malloc types so that we can
70147997Srwatson	 * guess an initial buffer size.  We loop until we succeed or really
71147997Srwatson	 * fail.  Note that the value of maxcpus we query using sysctl is not
72147997Srwatson	 * the version we use when processing the real data -- that is read
73147997Srwatson	 * from the header.
74147997Srwatson	 */
75147997Srwatsonretry:
76147997Srwatson	size = sizeof(maxcpus);
77147997Srwatson	if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
78147997Srwatson		error = errno;
79147997Srwatson		perror("kern.smp.maxcpus");
80147997Srwatson		errno = error;
81147997Srwatson		return (-1);
82147997Srwatson	}
83147997Srwatson	if (size != sizeof(maxcpus)) {
84147997Srwatson		fprintf(stderr, "kern.smp.maxcpus: wronge size");
85147997Srwatson		errno = EINVAL;
86147997Srwatson		return (-1);
87147997Srwatson	}
88147997Srwatson
89147997Srwatson	if (maxcpus > MEMSTAT_MAXCPU) {
90147997Srwatson		fprintf(stderr, "kern.smp.maxcpus: too many CPUs\n");
91147997Srwatson		errno = EINVAL;
92147997Srwatson		return (-1);
93147997Srwatson	}
94147997Srwatson
95147997Srwatson	size = sizeof(count);
96147997Srwatson	if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) {
97147997Srwatson		error = errno;
98147997Srwatson		perror("vm.zone_count");
99147997Srwatson		errno = error;
100147997Srwatson		return (-1);
101147997Srwatson	}
102147997Srwatson	if (size != sizeof(count)) {
103147997Srwatson		fprintf(stderr, "vm.zone_count: wronge size");
104147997Srwatson		errno = EINVAL;
105147997Srwatson		return (-1);
106147997Srwatson	}
107147997Srwatson
108147997Srwatson	size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) *
109147997Srwatson	    maxcpus);
110147997Srwatson
111147997Srwatson	buffer = malloc(size);
112147997Srwatson	if (buffer == NULL) {
113147997Srwatson		error = errno;
114147997Srwatson		perror("malloc");
115147997Srwatson		errno = error;
116147997Srwatson		return (-1);
117147997Srwatson	}
118147997Srwatson
119147997Srwatson	if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) {
120147997Srwatson		/*
121147997Srwatson		 * XXXRW: ENOMEM is an ambiguous return, we should bound the
122147997Srwatson		 * number of loops, perhaps.
123147997Srwatson		 */
124147997Srwatson		if (errno == ENOMEM) {
125147997Srwatson			free(buffer);
126147997Srwatson			goto retry;
127147997Srwatson		}
128147997Srwatson		error = errno;
129147997Srwatson		free(buffer);
130147997Srwatson		perror("vm.zone_stats");
131147997Srwatson		errno = error;
132147997Srwatson		return (-1);
133147997Srwatson	}
134147997Srwatson
135147997Srwatson	if (size == 0) {
136147997Srwatson		free(buffer);
137147997Srwatson		return (0);
138147997Srwatson	}
139147997Srwatson
140147997Srwatson	if (size < sizeof(*ushp)) {
141147997Srwatson		fprintf(stderr, "sysctl_uma: invalid malloc header");
142147997Srwatson		free(buffer);
143147997Srwatson		errno = EINVAL;
144147997Srwatson		return (-1);
145147997Srwatson	}
146147997Srwatson	p = buffer;
147147997Srwatson	ushp = (struct uma_stream_header *)p;
148147997Srwatson	p += sizeof(*ushp);
149147997Srwatson
150147997Srwatson	if (ushp->ush_version != UMA_STREAM_VERSION) {
151147997Srwatson		fprintf(stderr, "sysctl_uma: unknown malloc version");
152147997Srwatson		free(buffer);
153147997Srwatson		errno = EINVAL;
154147997Srwatson		return (-1);
155147997Srwatson	}
156147997Srwatson
157147997Srwatson	if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) {
158147997Srwatson		fprintf(stderr, "sysctl_uma: too many CPUs");
159147997Srwatson		free(buffer);
160147997Srwatson		errno = EINVAL;
161147997Srwatson		return (-1);
162147997Srwatson	}
163147997Srwatson
164147997Srwatson	/*
165147997Srwatson	 * For the remainder of this function, we are quite trusting about
166147997Srwatson	 * the layout of structures and sizes, since we've determined we have
167147997Srwatson	 * a matching version and acceptable CPU count.
168147997Srwatson	 */
169147997Srwatson	maxcpus = ushp->ush_maxcpus;
170147997Srwatson	count = ushp->ush_count;
171147997Srwatson	for (i = 0; i < count; i++) {
172147997Srwatson		uthp = (struct uma_type_header *)p;
173147997Srwatson		p += sizeof(*uthp);
174147997Srwatson
175147997Srwatson		if (hint_dontsearch == 0) {
176147997Srwatson			mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
177147997Srwatson			    uthp->uth_name);
178147997Srwatson			/*
179147997Srwatson			 * Reset the statistics on a reused node.
180147997Srwatson			 */
181147997Srwatson			if (mtp != NULL)
182147997Srwatson				memstat_mt_reset_stats(mtp);
183147997Srwatson		} else
184147997Srwatson			mtp = NULL;
185147997Srwatson		if (mtp == NULL)
186147997Srwatson			mtp = memstat_mt_allocate(list, ALLOCATOR_UMA,
187147997Srwatson			    uthp->uth_name);
188147997Srwatson		if (mtp == NULL) {
189147997Srwatson			memstat_mtl_free(list);
190147997Srwatson			free(buffer);
191147997Srwatson			errno = ENOMEM;
192147997Srwatson			perror("malloc");
193147997Srwatson			errno = ENOMEM;
194147997Srwatson			return (-1);
195147997Srwatson		}
196147997Srwatson
197147997Srwatson		/*
198147997Srwatson		 * Reset the statistics on a current node.
199147997Srwatson		 */
200147997Srwatson		memstat_mt_reset_stats(mtp);
201147997Srwatson
202147997Srwatson		for (j = 0; j < maxcpus; j++) {
203147997Srwatson			upsp = (struct uma_percpu_stat *)p;
204147997Srwatson			p += sizeof(*upsp);
205147997Srwatson
206147997Srwatson			mtp->mt_percpu_cache[j].mtp_free =
207147997Srwatson			    upsp->ups_cache_free;
208147997Srwatson			mtp->mt_free += upsp->ups_cache_free;
209147997Srwatson			mtp->mt_numallocs += upsp->ups_allocs;
210147997Srwatson			mtp->mt_numfrees += upsp->ups_frees;
211147997Srwatson		}
212147997Srwatson
213147997Srwatson		mtp->mt_size = uthp->uth_size;
214147997Srwatson		mtp->mt_memalloced = uthp->uth_allocs * uthp->uth_size;
215147997Srwatson		mtp->mt_memfreed = uthp->uth_frees * uthp->uth_size;
216147997Srwatson		mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
217147997Srwatson		mtp->mt_countlimit = uthp->uth_limit;
218147997Srwatson		mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size;
219147997Srwatson
220147997Srwatson		mtp->mt_numallocs = uthp->uth_allocs;
221147997Srwatson		mtp->mt_numfrees = uthp->uth_frees;
222147997Srwatson		mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
223147997Srwatson		mtp->mt_zonefree = uthp->uth_zone_free + uthp->uth_keg_free;
224147997Srwatson		mtp->mt_free += mtp->mt_zonefree;
225147997Srwatson	}
226147997Srwatson
227147997Srwatson	free(buffer);
228147997Srwatson
229147997Srwatson	return (0);
230147997Srwatson}
231