xref: /freebsd/lib/libmemstat/memstat_uma.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
1 /*-
2  * Copyright (c) 2005-2006 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$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 
32 #define	LIBMEMSTAT	/* Cause vm_page.h not to include opt_vmpage.h */
33 #include <vm/vm.h>
34 #include <vm/vm_page.h>
35 
36 #include <vm/uma.h>
37 #include <vm/uma_int.h>
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <kvm.h>
42 #include <nlist.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include "memstat.h"
49 #include "memstat_internal.h"
50 
51 static struct nlist namelist[] = {
52 #define	X_UMA_KEGS	0
53 	{ .n_name = "_uma_kegs" },
54 #define	X_MP_MAXID	1
55 	{ .n_name = "_mp_maxid" },
56 #define	X_ALL_CPUS	2
57 	{ .n_name = "_all_cpus" },
58 	{ .n_name = "" },
59 };
60 
61 /*
62  * Extract uma(9) statistics from the running kernel, and store all memory
63  * type information in the passed list.  For each type, check the list for an
64  * existing entry with the right name/allocator -- if present, update that
65  * entry.  Otherwise, add a new entry.  On error, the entire list will be
66  * cleared, as entries will be in an inconsistent state.
67  *
68  * To reduce the level of work for a list that starts empty, we keep around a
69  * hint as to whether it was empty when we began, so we can avoid searching
70  * the list for entries to update.  Updates are O(n^2) due to searching for
71  * each entry before adding it.
72  */
73 int
74 memstat_sysctl_uma(struct memory_type_list *list, int flags)
75 {
76 	struct uma_stream_header *ushp;
77 	struct uma_type_header *uthp;
78 	struct uma_percpu_stat *upsp;
79 	struct memory_type *mtp;
80 	int count, hint_dontsearch, i, j, maxcpus;
81 	char *buffer, *p;
82 	size_t size;
83 
84 	hint_dontsearch = LIST_EMPTY(&list->mtl_list);
85 
86 	/*
87 	 * Query the number of CPUs, number of malloc types so that we can
88 	 * guess an initial buffer size.  We loop until we succeed or really
89 	 * fail.  Note that the value of maxcpus we query using sysctl is not
90 	 * the version we use when processing the real data -- that is read
91 	 * from the header.
92 	 */
93 retry:
94 	size = sizeof(maxcpus);
95 	if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &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_DATAERROR;
100 		return (-1);
101 	}
102 	if (size != sizeof(maxcpus)) {
103 		list->mtl_error = MEMSTAT_ERROR_DATAERROR;
104 		return (-1);
105 	}
106 
107 	if (maxcpus > MEMSTAT_MAXCPU) {
108 		list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
109 		return (-1);
110 	}
111 
112 	size = sizeof(count);
113 	if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) {
114 		if (errno == EACCES || errno == EPERM)
115 			list->mtl_error = MEMSTAT_ERROR_PERMISSION;
116 		else
117 			list->mtl_error = MEMSTAT_ERROR_VERSION;
118 		return (-1);
119 	}
120 	if (size != sizeof(count)) {
121 		list->mtl_error = MEMSTAT_ERROR_DATAERROR;
122 		return (-1);
123 	}
124 
125 	size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) *
126 	    maxcpus);
127 
128 	buffer = malloc(size);
129 	if (buffer == NULL) {
130 		list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
131 		return (-1);
132 	}
133 
134 	if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) {
135 		/*
136 		 * XXXRW: ENOMEM is an ambiguous return, we should bound the
137 		 * number of loops, perhaps.
138 		 */
139 		if (errno == ENOMEM) {
140 			free(buffer);
141 			goto retry;
142 		}
143 		if (errno == EACCES || errno == EPERM)
144 			list->mtl_error = MEMSTAT_ERROR_PERMISSION;
145 		else
146 			list->mtl_error = MEMSTAT_ERROR_VERSION;
147 		free(buffer);
148 		return (-1);
149 	}
150 
151 	if (size == 0) {
152 		free(buffer);
153 		return (0);
154 	}
155 
156 	if (size < sizeof(*ushp)) {
157 		list->mtl_error = MEMSTAT_ERROR_VERSION;
158 		free(buffer);
159 		return (-1);
160 	}
161 	p = buffer;
162 	ushp = (struct uma_stream_header *)p;
163 	p += sizeof(*ushp);
164 
165 	if (ushp->ush_version != UMA_STREAM_VERSION) {
166 		list->mtl_error = MEMSTAT_ERROR_VERSION;
167 		free(buffer);
168 		return (-1);
169 	}
170 
171 	if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) {
172 		list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS;
173 		free(buffer);
174 		return (-1);
175 	}
176 
177 	/*
178 	 * For the remainder of this function, we are quite trusting about
179 	 * the layout of structures and sizes, since we've determined we have
180 	 * a matching version and acceptable CPU count.
181 	 */
182 	maxcpus = ushp->ush_maxcpus;
183 	count = ushp->ush_count;
184 	for (i = 0; i < count; i++) {
185 		uthp = (struct uma_type_header *)p;
186 		p += sizeof(*uthp);
187 
188 		if (hint_dontsearch == 0) {
189 			mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
190 			    uthp->uth_name);
191 		} else
192 			mtp = NULL;
193 		if (mtp == NULL)
194 			mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
195 			    uthp->uth_name);
196 		if (mtp == NULL) {
197 			_memstat_mtl_empty(list);
198 			free(buffer);
199 			list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
200 			return (-1);
201 		}
202 
203 		/*
204 		 * Reset the statistics on a current node.
205 		 */
206 		_memstat_mt_reset_stats(mtp);
207 
208 		mtp->mt_numallocs = uthp->uth_allocs;
209 		mtp->mt_numfrees = uthp->uth_frees;
210 		mtp->mt_failures = uthp->uth_fails;
211 		mtp->mt_sleeps = uthp->uth_sleeps;
212 
213 		for (j = 0; j < maxcpus; j++) {
214 			upsp = (struct uma_percpu_stat *)p;
215 			p += sizeof(*upsp);
216 
217 			mtp->mt_percpu_cache[j].mtp_free =
218 			    upsp->ups_cache_free;
219 			mtp->mt_free += upsp->ups_cache_free;
220 			mtp->mt_numallocs += upsp->ups_allocs;
221 			mtp->mt_numfrees += upsp->ups_frees;
222 		}
223 
224 		mtp->mt_size = uthp->uth_size;
225 		mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size;
226 		mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size;
227 		mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
228 		mtp->mt_countlimit = uthp->uth_limit;
229 		mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size;
230 
231 		mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
232 		mtp->mt_zonefree = uthp->uth_zone_free;
233 
234 		/*
235 		 * UMA secondary zones share a keg with the primary zone.  To
236 		 * avoid double-reporting of free items, report keg free
237 		 * items only in the primary zone.
238 		 */
239 		if (!(uthp->uth_zone_flags & UTH_ZONE_SECONDARY)) {
240 			mtp->mt_kegfree = uthp->uth_keg_free;
241 			mtp->mt_free += mtp->mt_kegfree;
242 		}
243 		mtp->mt_free += mtp->mt_zonefree;
244 	}
245 
246 	free(buffer);
247 
248 	return (0);
249 }
250 
251 static int
252 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
253     size_t offset)
254 {
255 	ssize_t ret;
256 
257 	ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
258 	    size);
259 	if (ret < 0)
260 		return (MEMSTAT_ERROR_KVM);
261 	if ((size_t)ret != size)
262 		return (MEMSTAT_ERROR_KVM_SHORTREAD);
263 	return (0);
264 }
265 
266 static int
267 kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen)
268 {
269 	ssize_t ret;
270 	int i;
271 
272 	for (i = 0; i < buflen; i++) {
273 		ret = kvm_read(kvm, (unsigned long)kvm_pointer + i,
274 		    &(buffer[i]), sizeof(char));
275 		if (ret < 0)
276 			return (MEMSTAT_ERROR_KVM);
277 		if ((size_t)ret != sizeof(char))
278 			return (MEMSTAT_ERROR_KVM_SHORTREAD);
279 		if (buffer[i] == '\0')
280 			return (0);
281 	}
282 	/* Truncate. */
283 	buffer[i-1] = '\0';
284 	return (0);
285 }
286 
287 static int
288 kread_symbol(kvm_t *kvm, int index, void *address, size_t size,
289     size_t offset)
290 {
291 	ssize_t ret;
292 
293 	ret = kvm_read(kvm, namelist[index].n_value + offset, address, size);
294 	if (ret < 0)
295 		return (MEMSTAT_ERROR_KVM);
296 	if ((size_t)ret != size)
297 		return (MEMSTAT_ERROR_KVM_SHORTREAD);
298 	return (0);
299 }
300 
301 /*
302  * memstat_kvm_uma() is similar to memstat_sysctl_uma(), only it extracts
303  * UMA(9) statistics from a kernel core/memory file.
304  */
305 int
306 memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle)
307 {
308 	LIST_HEAD(, uma_keg) uma_kegs;
309 	struct memory_type *mtp;
310 	struct uma_bucket *ubp, ub;
311 	struct uma_cache *ucp, *ucp_array;
312 	struct uma_zone *uzp, uz;
313 	struct uma_keg *kzp, kz;
314 	int hint_dontsearch, i, mp_maxid, ret;
315 	char name[MEMTYPE_MAXNAME];
316 	__cpumask_t all_cpus;
317 	kvm_t *kvm;
318 
319 	kvm = (kvm_t *)kvm_handle;
320 	hint_dontsearch = LIST_EMPTY(&list->mtl_list);
321 	if (kvm_nlist(kvm, namelist) != 0) {
322 		list->mtl_error = MEMSTAT_ERROR_KVM;
323 		return (-1);
324 	}
325 	if (namelist[X_UMA_KEGS].n_type == 0 ||
326 	    namelist[X_UMA_KEGS].n_value == 0) {
327 		list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL;
328 		return (-1);
329 	}
330 	ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0);
331 	if (ret != 0) {
332 		list->mtl_error = ret;
333 		return (-1);
334 	}
335 	ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0);
336 	if (ret != 0) {
337 		list->mtl_error = ret;
338 		return (-1);
339 	}
340 	ret = kread_symbol(kvm, X_ALL_CPUS, &all_cpus, sizeof(all_cpus), 0);
341 	if (ret != 0) {
342 		list->mtl_error = ret;
343 		return (-1);
344 	}
345 	ucp_array = malloc(sizeof(struct uma_cache) * (mp_maxid + 1));
346 	if (ucp_array == NULL) {
347 		list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
348 		return (-1);
349 	}
350 	for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp =
351 	    LIST_NEXT(&kz, uk_link)) {
352 		ret = kread(kvm, kzp, &kz, sizeof(kz), 0);
353 		if (ret != 0) {
354 			free(ucp_array);
355 			_memstat_mtl_empty(list);
356 			list->mtl_error = ret;
357 			return (-1);
358 		}
359 		for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp =
360 		    LIST_NEXT(&uz, uz_link)) {
361 			ret = kread(kvm, uzp, &uz, sizeof(uz), 0);
362 			if (ret != 0) {
363 				free(ucp_array);
364 				_memstat_mtl_empty(list);
365 				list->mtl_error = ret;
366 				return (-1);
367 			}
368 			ret = kread(kvm, uzp, ucp_array,
369 			    sizeof(struct uma_cache) * (mp_maxid + 1),
370 			    offsetof(struct uma_zone, uz_cpu[0]));
371 			if (ret != 0) {
372 				free(ucp_array);
373 				_memstat_mtl_empty(list);
374 				list->mtl_error = ret;
375 				return (-1);
376 			}
377 			ret = kread_string(kvm, uz.uz_name, name,
378 			    MEMTYPE_MAXNAME);
379 			if (ret != 0) {
380 				free(ucp_array);
381 				_memstat_mtl_empty(list);
382 				list->mtl_error = ret;
383 				return (-1);
384 			}
385 			if (hint_dontsearch == 0) {
386 				mtp = memstat_mtl_find(list, ALLOCATOR_UMA,
387 				    name);
388 			} else
389 				mtp = NULL;
390 			if (mtp == NULL)
391 				mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA,
392 				    name);
393 			if (mtp == NULL) {
394 				free(ucp_array);
395 				_memstat_mtl_empty(list);
396 				list->mtl_error = MEMSTAT_ERROR_NOMEMORY;
397 				return (-1);
398 			}
399 			/*
400 			 * Reset the statistics on a current node.
401 			 */
402 			_memstat_mt_reset_stats(mtp);
403 			mtp->mt_numallocs = uz.uz_allocs;
404 			mtp->mt_numfrees = uz.uz_frees;
405 			mtp->mt_failures = uz.uz_fails;
406 			mtp->mt_sleeps = uz.uz_sleeps;
407 			if (kz.uk_flags & UMA_ZFLAG_INTERNAL)
408 				goto skip_percpu;
409 			for (i = 0; i < mp_maxid + 1; i++) {
410 				if ((all_cpus & (1 << i)) == 0)
411 					continue;
412 				ucp = &ucp_array[i];
413 				mtp->mt_numallocs += ucp->uc_allocs;
414 				mtp->mt_numfrees += ucp->uc_frees;
415 
416 				if (ucp->uc_allocbucket != NULL) {
417 					ret = kread(kvm, ucp->uc_allocbucket,
418 					    &ub, sizeof(ub), 0);
419 					if (ret != 0) {
420 						free(ucp_array);
421 						_memstat_mtl_empty(list);
422 						list->mtl_error = ret;
423 						return (-1);
424 					}
425 					mtp->mt_free += ub.ub_cnt;
426 				}
427 				if (ucp->uc_freebucket != NULL) {
428 					ret = kread(kvm, ucp->uc_freebucket,
429 					    &ub, sizeof(ub), 0);
430 					if (ret != 0) {
431 						free(ucp_array);
432 						_memstat_mtl_empty(list);
433 						list->mtl_error = ret;
434 						return (-1);
435 					}
436 					mtp->mt_free += ub.ub_cnt;
437 				}
438 			}
439 skip_percpu:
440 			mtp->mt_size = kz.uk_size;
441 			mtp->mt_memalloced = mtp->mt_numallocs * mtp->mt_size;
442 			mtp->mt_memfreed = mtp->mt_numfrees * mtp->mt_size;
443 			mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed;
444 			if (kz.uk_ppera > 1)
445 				mtp->mt_countlimit = kz.uk_maxpages /
446 				    kz.uk_ipers;
447 			else
448 				mtp->mt_countlimit = kz.uk_maxpages *
449 				    kz.uk_ipers;
450 			mtp->mt_byteslimit = mtp->mt_countlimit * mtp->mt_size;
451 			mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees;
452 			for (ubp = LIST_FIRST(&uz.uz_full_bucket); ubp !=
453 			    NULL; ubp = LIST_NEXT(&ub, ub_link)) {
454 				ret = kread(kvm, ubp, &ub, sizeof(ub), 0);
455 				mtp->mt_zonefree += ub.ub_cnt;
456 			}
457 			if (!((kz.uk_flags & UMA_ZONE_SECONDARY) &&
458 			    LIST_FIRST(&kz.uk_zones) != uzp)) {
459 				mtp->mt_kegfree = kz.uk_free;
460 				mtp->mt_free += mtp->mt_kegfree;
461 			}
462 			mtp->mt_free += mtp->mt_zonefree;
463 		}
464 	}
465 	free(ucp_array);
466 	return (0);
467 }
468