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