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