memstat.c revision 148357
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.c 148357 2005-07-24 01:28:54Z rwatson $
27 */
28
29#include <sys/param.h>
30#include <sys/sysctl.h>
31
32#include <err.h>
33#include <errno.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38#include "memstat.h"
39#include "memstat_internal.h"
40
41struct memory_type_list *
42memstat_mtl_alloc(void)
43{
44	struct memory_type_list *mtlp;
45
46	mtlp = malloc(sizeof(*mtlp));
47	if (mtlp == NULL)
48		return (NULL);
49
50	LIST_INIT(&mtlp->mtl_list);
51	mtlp->mtl_error = MEMSTAT_ERROR_UNDEFINED;
52	return (mtlp);
53}
54
55struct memory_type *
56memstat_mtl_first(struct memory_type_list *list)
57{
58
59	return (LIST_FIRST(&list->mtl_list));
60}
61
62struct memory_type *
63memstat_mtl_next(struct memory_type *mtp)
64{
65
66	return (LIST_NEXT(mtp, mt_list));
67}
68
69void
70memstat_mtl_free(struct memory_type_list *list)
71{
72	struct memory_type *mtp;
73
74	while ((mtp = LIST_FIRST(&list->mtl_list))) {
75		LIST_REMOVE(mtp, mt_list);
76		free(mtp);
77	}
78	free(list);
79}
80
81int
82memstat_mtl_geterror(struct memory_type_list *list)
83{
84
85	return (list->mtl_error);
86}
87
88/*
89 * Look for an existing memory_type entry in a memory_type list, based on the
90 * allocator and name of the type.  If not found, return NULL.  No errno or
91 * memstat error.
92 */
93struct memory_type *
94memstat_mtl_find(struct memory_type_list *list, int allocator,
95    const char *name)
96{
97	struct memory_type *mtp;
98
99	LIST_FOREACH(mtp, &list->mtl_list, mt_list) {
100		if ((mtp->mt_allocator == allocator ||
101		    allocator == ALLOCATOR_ANY) &&
102		    strcmp(mtp->mt_name, name) == 0)
103			return (mtp);
104	}
105	return (NULL);
106}
107
108/*
109 * Allocate a new memory_type with the specificed allocator type and name,
110 * then insert into the list.  The structure will be zero'd.
111 *
112 * libmemstat(3) internal function.
113 */
114struct memory_type *
115_memstat_mt_allocate(struct memory_type_list *list, int allocator,
116    const char *name)
117{
118	struct memory_type *mtp;
119
120	mtp = malloc(sizeof(*mtp));
121	if (mtp == NULL)
122		return (NULL);
123
124	bzero(mtp, sizeof(*mtp));
125
126	mtp->mt_allocator = allocator;
127	strlcpy(mtp->mt_name, name, MEMTYPE_MAXNAME);
128	LIST_INSERT_HEAD(&list->mtl_list, mtp, mt_list);
129	return (mtp);
130}
131
132/*
133 * Reset any libmemstat(3)-owned statistics in a memory_type record so that
134 * it can be reused without incremental addition problems.  Caller-owned
135 * memory is left "as-is", and must be updated by the caller if desired.
136 *
137 * libmemstat(3) internal function.
138 */
139void
140_memstat_mt_reset_stats(struct memory_type *mtp)
141{
142	int i;
143
144	mtp->mt_countlimit = 0;
145	mtp->mt_byteslimit = 0;
146	mtp->mt_sizemask = 0;
147	mtp->mt_size = 0;
148
149	mtp->mt_memalloced = 0;
150	mtp->mt_memfreed = 0;
151	mtp->mt_numallocs = 0;
152	mtp->mt_numfrees = 0;
153	mtp->mt_bytes = 0;
154	mtp->mt_count = 0;
155	mtp->mt_free = 0;
156	mtp->mt_failures = 0;
157
158	mtp->mt_zonefree = 0;
159	mtp->mt_kegfree = 0;
160
161	for (i = 0; i < MEMSTAT_MAXCPU; i++) {
162		mtp->mt_percpu_alloc[i].mtp_memalloced = 0;
163		mtp->mt_percpu_alloc[i].mtp_memfreed = 0;
164		mtp->mt_percpu_alloc[i].mtp_numallocs = 0;
165		mtp->mt_percpu_alloc[i].mtp_numfrees = 0;
166		mtp->mt_percpu_alloc[i].mtp_sizemask = 0;
167		mtp->mt_percpu_cache[i].mtp_free = 0;
168	}
169}
170
171/*
172 * Accessor methods for struct memory_type.  Avoids encoding the structure
173 * ABI into the application.
174 */
175const char *
176memstat_get_name(const struct memory_type *mtp)
177{
178
179	return (mtp->mt_name);
180}
181
182int
183memstat_get_allocator(const struct memory_type *mtp)
184{
185
186	return (mtp->mt_allocator);
187}
188
189uint64_t
190memstat_get_countlimit(const struct memory_type *mtp)
191{
192
193	return (mtp->mt_countlimit);
194}
195
196uint64_t
197memstat_get_byteslimit(const struct memory_type *mtp)
198{
199
200	return (mtp->mt_byteslimit);
201}
202
203uint64_t
204memstat_get_sizemask(const struct memory_type *mtp)
205{
206
207	return (mtp->mt_sizemask);
208}
209
210uint64_t
211memstat_get_size(const struct memory_type *mtp)
212{
213
214	return (mtp->mt_size);
215}
216
217uint64_t
218memstat_get_memalloced(const struct memory_type *mtp)
219{
220
221	return (mtp->mt_memalloced);
222}
223
224uint64_t
225memstat_get_memfreed(const struct memory_type *mtp)
226{
227
228	return (mtp->mt_memfreed);
229}
230
231uint64_t
232memstat_get_numallocs(const struct memory_type *mtp)
233{
234
235	return (mtp->mt_numallocs);
236}
237
238uint64_t
239memstat_get_numfrees(const struct memory_type *mtp)
240{
241
242	return (mtp->mt_numfrees);
243}
244
245uint64_t
246memstat_get_bytes(const struct memory_type *mtp)
247{
248
249	return (mtp->mt_bytes);
250}
251
252uint64_t
253memstat_get_count(const struct memory_type *mtp)
254{
255
256	return (mtp->mt_count);
257}
258
259uint64_t
260memstat_get_free(const struct memory_type *mtp)
261{
262
263	return (mtp->mt_free);
264}
265
266uint64_t
267memstat_get_failures(const struct memory_type *mtp)
268{
269
270	return (mtp->mt_failures);
271}
272
273void *
274memstat_get_caller_pointer(const struct memory_type *mtp, int index)
275{
276
277	return (mtp->mt_caller_pointer[index]);
278}
279
280void
281memstat_set_caller_pointer(struct memory_type *mtp, int index, void *value)
282{
283
284	mtp->mt_caller_pointer[index] = value;
285}
286
287uint64_t
288memstat_get_caller_uint64(const struct memory_type *mtp, int index)
289{
290
291	return (mtp->mt_caller_uint64[index]);
292}
293
294void
295memstat_set_caller_uint64(struct memory_type *mtp, int index, uint64_t value)
296{
297
298	mtp->mt_caller_uint64[index] = value;
299}
300
301uint64_t
302memstat_get_zonefree(const struct memory_type *mtp)
303{
304
305	return (mtp->mt_zonefree);
306}
307
308uint64_t
309memstat_get_kegfree(const struct memory_type *mtp)
310{
311
312	return (mtp->mt_kegfree);
313}
314
315uint64_t
316memstat_get_percpu_memalloced(const struct memory_type *mtp, int cpu)
317{
318
319	return (mtp->mt_percpu_alloc[cpu].mtp_memalloced);
320}
321
322uint64_t
323memstat_get_percpu_memfreed(const struct memory_type *mtp, int cpu)
324{
325
326	return (mtp->mt_percpu_alloc[cpu].mtp_memfreed);
327}
328
329uint64_t
330memstat_get_percpu_numallocs(const struct memory_type *mtp, int cpu)
331{
332
333	return (mtp->mt_percpu_alloc[cpu].mtp_numallocs);
334}
335
336uint64_t
337memstat_get_percpu_numfrees(const struct memory_type *mtp, int cpu)
338{
339
340	return (mtp->mt_percpu_alloc[cpu].mtp_numfrees);
341}
342
343uint64_t
344memstat_get_percpu_sizemask(const struct memory_type *mtp, int cpu)
345{
346
347	return (mtp->mt_percpu_alloc[cpu].mtp_sizemask);
348}
349
350void *
351memstat_get_percpu_caller_pointer(const struct memory_type *mtp, int cpu,
352    int index)
353{
354
355	return (mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index]);
356}
357
358void
359memstat_set_percpu_caller_pointer(struct memory_type *mtp, int cpu,
360    int index, void *value)
361{
362
363	mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index] = value;
364}
365
366uint64_t
367memstat_get_percpu_caller_uint64(const struct memory_type *mtp, int cpu,
368    int index)
369{
370
371	return (mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index]);
372}
373
374void
375memstat_set_percpu_caller_uint64(struct memory_type *mtp, int cpu, int index,
376    uint64_t value)
377{
378
379	mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index] = value;
380}
381
382uint64_t
383memstat_get_percpu_free(const struct memory_type *mtp, int cpu)
384{
385
386	return (mtp->mt_percpu_cache[cpu].mtp_free);
387}
388