source: rtems-libbsd/freebsd/lib/libmemstat/memstat.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was 66659ff, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 15:20:21

Update to FreeBSD 9.2

  • Property mode set to 100644
File size: 8.8 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*-
4 * Copyright (c) 2005 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31#include <rtems/bsd/sys/param.h>
32#include <sys/sysctl.h>
33
34#include <err.h>
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include "memstat.h"
41#include "memstat_internal.h"
42
43const char *
44memstat_strerror(int error)
45{
46
47        switch (error) {
48        case MEMSTAT_ERROR_NOMEMORY:
49                return ("Cannot allocate memory");
50        case MEMSTAT_ERROR_VERSION:
51                return ("Version mismatch");
52        case MEMSTAT_ERROR_PERMISSION:
53                return ("Permission denied");
54        case MEMSTAT_ERROR_DATAERROR:
55                return ("Data format error");
56        case MEMSTAT_ERROR_KVM:
57                return ("KVM error");
58        case MEMSTAT_ERROR_KVM_NOSYMBOL:
59                return ("KVM unable to find symbol");
60        case MEMSTAT_ERROR_KVM_SHORTREAD:
61                return ("KVM short read");
62        case MEMSTAT_ERROR_UNDEFINED:
63        default:
64                return ("Unknown error");
65        }
66}
67
68struct memory_type_list *
69memstat_mtl_alloc(void)
70{
71        struct memory_type_list *mtlp;
72
73        mtlp = malloc(sizeof(*mtlp));
74        if (mtlp == NULL)
75                return (NULL);
76
77        LIST_INIT(&mtlp->mtl_list);
78        mtlp->mtl_error = MEMSTAT_ERROR_UNDEFINED;
79        return (mtlp);
80}
81
82struct memory_type *
83memstat_mtl_first(struct memory_type_list *list)
84{
85
86        return (LIST_FIRST(&list->mtl_list));
87}
88
89struct memory_type *
90memstat_mtl_next(struct memory_type *mtp)
91{
92
93        return (LIST_NEXT(mtp, mt_list));
94}
95
96void
97_memstat_mtl_empty(struct memory_type_list *list)
98{
99        struct memory_type *mtp;
100
101        while ((mtp = LIST_FIRST(&list->mtl_list))) {
102                free(mtp->mt_percpu_alloc);
103                free(mtp->mt_percpu_cache);
104                LIST_REMOVE(mtp, mt_list);
105                free(mtp);
106        }
107}
108
109void
110memstat_mtl_free(struct memory_type_list *list)
111{
112
113        _memstat_mtl_empty(list);
114        free(list);
115}
116
117int
118memstat_mtl_geterror(struct memory_type_list *list)
119{
120
121        return (list->mtl_error);
122}
123
124/*
125 * Look for an existing memory_type entry in a memory_type list, based on the
126 * allocator and name of the type.  If not found, return NULL.  No errno or
127 * memstat error.
128 */
129struct memory_type *
130memstat_mtl_find(struct memory_type_list *list, int allocator,
131    const char *name)
132{
133        struct memory_type *mtp;
134
135        LIST_FOREACH(mtp, &list->mtl_list, mt_list) {
136                if ((mtp->mt_allocator == allocator ||
137                    allocator == ALLOCATOR_ANY) &&
138                    strcmp(mtp->mt_name, name) == 0)
139                        return (mtp);
140        }
141        return (NULL);
142}
143
144/*
145 * Allocate a new memory_type with the specificed allocator type and name,
146 * then insert into the list.  The structure will be zero'd.
147 *
148 * libmemstat(3) internal function.
149 */
150struct memory_type *
151_memstat_mt_allocate(struct memory_type_list *list, int allocator,
152    const char *name, int maxcpus)
153{
154        struct memory_type *mtp;
155
156        mtp = malloc(sizeof(*mtp));
157        if (mtp == NULL)
158                return (NULL);
159
160        bzero(mtp, sizeof(*mtp));
161
162        mtp->mt_allocator = allocator;
163        mtp->mt_percpu_alloc = malloc(sizeof(struct mt_percpu_alloc_s) *
164            maxcpus);
165        mtp->mt_percpu_cache = malloc(sizeof(struct mt_percpu_cache_s) *
166            maxcpus);
167        strlcpy(mtp->mt_name, name, MEMTYPE_MAXNAME);
168        LIST_INSERT_HEAD(&list->mtl_list, mtp, mt_list);
169        return (mtp);
170}
171
172/*
173 * Reset any libmemstat(3)-owned statistics in a memory_type record so that
174 * it can be reused without incremental addition problems.  Caller-owned
175 * memory is left "as-is", and must be updated by the caller if desired.
176 *
177 * libmemstat(3) internal function.
178 */
179void
180_memstat_mt_reset_stats(struct memory_type *mtp, int maxcpus)
181{
182        int i;
183
184        mtp->mt_countlimit = 0;
185        mtp->mt_byteslimit = 0;
186        mtp->mt_sizemask = 0;
187        mtp->mt_size = 0;
188
189        mtp->mt_memalloced = 0;
190        mtp->mt_memfreed = 0;
191        mtp->mt_numallocs = 0;
192        mtp->mt_numfrees = 0;
193        mtp->mt_bytes = 0;
194        mtp->mt_count = 0;
195        mtp->mt_free = 0;
196        mtp->mt_failures = 0;
197        mtp->mt_sleeps = 0;
198
199        mtp->mt_zonefree = 0;
200        mtp->mt_kegfree = 0;
201
202        for (i = 0; i < maxcpus; i++) {
203                mtp->mt_percpu_alloc[i].mtp_memalloced = 0;
204                mtp->mt_percpu_alloc[i].mtp_memfreed = 0;
205                mtp->mt_percpu_alloc[i].mtp_numallocs = 0;
206                mtp->mt_percpu_alloc[i].mtp_numfrees = 0;
207                mtp->mt_percpu_alloc[i].mtp_sizemask = 0;
208                mtp->mt_percpu_cache[i].mtp_free = 0;
209        }
210}
211
212/*
213 * Accessor methods for struct memory_type.  Avoids encoding the structure
214 * ABI into the application.
215 */
216const char *
217memstat_get_name(const struct memory_type *mtp)
218{
219
220        return (mtp->mt_name);
221}
222
223int
224memstat_get_allocator(const struct memory_type *mtp)
225{
226
227        return (mtp->mt_allocator);
228}
229
230uint64_t
231memstat_get_countlimit(const struct memory_type *mtp)
232{
233
234        return (mtp->mt_countlimit);
235}
236
237uint64_t
238memstat_get_byteslimit(const struct memory_type *mtp)
239{
240
241        return (mtp->mt_byteslimit);
242}
243
244uint64_t
245memstat_get_sizemask(const struct memory_type *mtp)
246{
247
248        return (mtp->mt_sizemask);
249}
250
251uint64_t
252memstat_get_size(const struct memory_type *mtp)
253{
254
255        return (mtp->mt_size);
256}
257
258uint64_t
259memstat_get_memalloced(const struct memory_type *mtp)
260{
261
262        return (mtp->mt_memalloced);
263}
264
265uint64_t
266memstat_get_memfreed(const struct memory_type *mtp)
267{
268
269        return (mtp->mt_memfreed);
270}
271
272uint64_t
273memstat_get_numallocs(const struct memory_type *mtp)
274{
275
276        return (mtp->mt_numallocs);
277}
278
279uint64_t
280memstat_get_numfrees(const struct memory_type *mtp)
281{
282
283        return (mtp->mt_numfrees);
284}
285
286uint64_t
287memstat_get_bytes(const struct memory_type *mtp)
288{
289
290        return (mtp->mt_bytes);
291}
292
293uint64_t
294memstat_get_count(const struct memory_type *mtp)
295{
296
297        return (mtp->mt_count);
298}
299
300uint64_t
301memstat_get_free(const struct memory_type *mtp)
302{
303
304        return (mtp->mt_free);
305}
306
307uint64_t
308memstat_get_failures(const struct memory_type *mtp)
309{
310
311        return (mtp->mt_failures);
312}
313
314uint64_t
315memstat_get_sleeps(const struct memory_type *mtp)
316{
317
318        return (mtp->mt_sleeps);
319}
320
321void *
322memstat_get_caller_pointer(const struct memory_type *mtp, int index)
323{
324
325        return (mtp->mt_caller_pointer[index]);
326}
327
328void
329memstat_set_caller_pointer(struct memory_type *mtp, int index, void *value)
330{
331
332        mtp->mt_caller_pointer[index] = value;
333}
334
335uint64_t
336memstat_get_caller_uint64(const struct memory_type *mtp, int index)
337{
338
339        return (mtp->mt_caller_uint64[index]);
340}
341
342void
343memstat_set_caller_uint64(struct memory_type *mtp, int index, uint64_t value)
344{
345
346        mtp->mt_caller_uint64[index] = value;
347}
348
349uint64_t
350memstat_get_zonefree(const struct memory_type *mtp)
351{
352
353        return (mtp->mt_zonefree);
354}
355
356uint64_t
357memstat_get_kegfree(const struct memory_type *mtp)
358{
359
360        return (mtp->mt_kegfree);
361}
362
363uint64_t
364memstat_get_percpu_memalloced(const struct memory_type *mtp, int cpu)
365{
366
367        return (mtp->mt_percpu_alloc[cpu].mtp_memalloced);
368}
369
370uint64_t
371memstat_get_percpu_memfreed(const struct memory_type *mtp, int cpu)
372{
373
374        return (mtp->mt_percpu_alloc[cpu].mtp_memfreed);
375}
376
377uint64_t
378memstat_get_percpu_numallocs(const struct memory_type *mtp, int cpu)
379{
380
381        return (mtp->mt_percpu_alloc[cpu].mtp_numallocs);
382}
383
384uint64_t
385memstat_get_percpu_numfrees(const struct memory_type *mtp, int cpu)
386{
387
388        return (mtp->mt_percpu_alloc[cpu].mtp_numfrees);
389}
390
391uint64_t
392memstat_get_percpu_sizemask(const struct memory_type *mtp, int cpu)
393{
394
395        return (mtp->mt_percpu_alloc[cpu].mtp_sizemask);
396}
397
398void *
399memstat_get_percpu_caller_pointer(const struct memory_type *mtp, int cpu,
400    int index)
401{
402
403        return (mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index]);
404}
405
406void
407memstat_set_percpu_caller_pointer(struct memory_type *mtp, int cpu,
408    int index, void *value)
409{
410
411        mtp->mt_percpu_alloc[cpu].mtp_caller_pointer[index] = value;
412}
413
414uint64_t
415memstat_get_percpu_caller_uint64(const struct memory_type *mtp, int cpu,
416    int index)
417{
418
419        return (mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index]);
420}
421
422void
423memstat_set_percpu_caller_uint64(struct memory_type *mtp, int cpu, int index,
424    uint64_t value)
425{
426
427        mtp->mt_percpu_alloc[cpu].mtp_caller_uint64[index] = value;
428}
429
430uint64_t
431memstat_get_percpu_free(const struct memory_type *mtp, int cpu)
432{
433
434        return (mtp->mt_percpu_cache[cpu].mtp_free);
435}
Note: See TracBrowser for help on using the repository browser.