memstat.h revision 209215
1147997Srwatson/*-
2147997Srwatson * Copyright (c) 2005 Robert N. M. Watson
3147997Srwatson * All rights reserved.
4147997Srwatson *
5147997Srwatson * Redistribution and use in source and binary forms, with or without
6147997Srwatson * modification, are permitted provided that the following conditions
7147997Srwatson * are met:
8147997Srwatson * 1. Redistributions of source code must retain the above copyright
9147997Srwatson *    notice, this list of conditions and the following disclaimer.
10147997Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11147997Srwatson *    notice, this list of conditions and the following disclaimer in the
12147997Srwatson *    documentation and/or other materials provided with the distribution.
13147997Srwatson *
14147997Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15147997Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16147997Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17147997Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18147997Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19147997Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20147997Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21147997Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22147997Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23147997Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24147997Srwatson * SUCH DAMAGE.
25147997Srwatson *
26147997Srwatson * $FreeBSD: head/lib/libmemstat/memstat.h 209215 2010-06-15 19:28:37Z sbruno $
27147997Srwatson */
28147997Srwatson
29147997Srwatson#ifndef _MEMSTAT_H_
30147997Srwatson#define	_MEMSTAT_H_
31147997Srwatson
32147997Srwatson/*
33147997Srwatson * Number of CPU slots in library-internal data structures.  This should be
34147997Srwatson * at least the value of MAXCPU from param.h.
35147997Srwatson */
36160695Sjb#define	MEMSTAT_MAXCPU	32
37147997Srwatson
38147997Srwatson/*
39148041Srwatson * Amount of caller data to maintain for each caller data slot.  Applications
40148041Srwatson * must not request more than this number of caller save data, or risk
41148041Srwatson * corrupting internal libmemstat(3) data structures.  A compile time check
42148041Srwatson * in the application is probably appropriate.
43148041Srwatson */
44148121Srwatson#define	MEMSTAT_MAXCALLER	16
45148041Srwatson
46148041Srwatson/*
47147997Srwatson * libmemstat(3) is able to extract memory data from different allocators;
48147997Srwatson * when it does so, it tags which allocator it got the data from so that
49147997Srwatson * consumers can determine which fields are usable, as data returned varies
50147997Srwatson * some.
51147997Srwatson */
52147997Srwatson#define	ALLOCATOR_UNKNOWN	0
53147997Srwatson#define	ALLOCATOR_MALLOC	1
54147997Srwatson#define	ALLOCATOR_UMA		2
55147997Srwatson#define	ALLOCATOR_ANY		255
56147997Srwatson
57147997Srwatson/*
58147997Srwatson * Library maximum type name.  Should be max(set of name maximums over
59147997Srwatson * various allocators).
60147997Srwatson */
61147997Srwatson#define	MEMTYPE_MAXNAME		32
62147997Srwatson
63147997Srwatson/*
64148357Srwatson * Library error conditions, mostly from the underlying data sources.  On
65148357Srwatson * failure, functions typically return (-1) or (NULL); on success, (0) or a
66148357Srwatson * valid data pointer.  The error from the last operation is stored in
67167597Srwatson * struct memory_type_list, and accessed via memstat_get_error(list).
68148357Srwatson */
69148357Srwatson#define	MEMSTAT_ERROR_UNDEFINED		0	/* Initialization value. */
70148357Srwatson#define	MEMSTAT_ERROR_NOMEMORY		1	/* Out of memory. */
71148357Srwatson#define	MEMSTAT_ERROR_VERSION		2	/* Unsupported version. */
72148357Srwatson#define	MEMSTAT_ERROR_PERMISSION	3	/* Permission denied. */
73148357Srwatson#define	MEMSTAT_ERROR_TOOMANYCPUS	4	/* Too many CPUs. */
74148357Srwatson#define	MEMSTAT_ERROR_DATAERROR		5	/* Error in stat data. */
75148627Srwatson#define	MEMSTAT_ERROR_KVM		6	/* See kvm_geterr() for err. */
76148627Srwatson#define	MEMSTAT_ERROR_KVM_NOSYMBOL	7	/* Symbol not available. */
77148627Srwatson#define	MEMSTAT_ERROR_KVM_SHORTREAD	8	/* Short kvm_read return. */
78148357Srwatson
79148357Srwatson/*
80147997Srwatson * Forward declare struct memory_type, which holds per-type properties and
81147997Srwatson * statistics.  This is an opaque type, to be frobbed only from within the
82147997Srwatson * library, in order to avoid building ABI assumptions into the application.
83147997Srwatson * Accessor methods should be used to get and sometimes set the fields from
84147997Srwatson * consumers of the library.
85147997Srwatson */
86147997Srwatsonstruct memory_type;
87147997Srwatson
88147997Srwatson/*
89147997Srwatson * struct memory_type_list is the head of a list of memory types and
90147997Srwatson * statistics.
91147997Srwatson */
92147997Srwatsonstruct memory_type_list;
93147997Srwatson
94148094Srwatson__BEGIN_DECLS
95147997Srwatson/*
96148359Srwatson * Functions that operate without memory type or memory type list context.
97148359Srwatson */
98148359Srwatsonconst char	*memstat_strerror(int error);
99148359Srwatson
100148359Srwatson/*
101147997Srwatson * Functions for managing memory type and statistics data.
102147997Srwatson */
103147997Srwatsonstruct memory_type_list	*memstat_mtl_alloc(void);
104147997Srwatsonstruct memory_type	*memstat_mtl_first(struct memory_type_list *list);
105147997Srwatsonstruct memory_type	*memstat_mtl_next(struct memory_type *mtp);
106147997Srwatsonstruct memory_type	*memstat_mtl_find(struct memory_type_list *list,
107147997Srwatson			    int allocator, const char *name);
108147997Srwatsonvoid	memstat_mtl_free(struct memory_type_list *list);
109148357Srwatsonint	memstat_mtl_geterror(struct memory_type_list *list);
110147997Srwatson
111147997Srwatson/*
112147997Srwatson * Functions to retrieve data from a live kernel using sysctl.
113147997Srwatson */
114147997Srwatsonint	memstat_sysctl_all(struct memory_type_list *list, int flags);
115147997Srwatsonint	memstat_sysctl_malloc(struct memory_type_list *list, int flags);
116147997Srwatsonint	memstat_sysctl_uma(struct memory_type_list *list, int flags);
117147997Srwatson
118147997Srwatson/*
119148627Srwatson * Functions to retrieve data from a kernel core (or /dev/kmem).
120148627Srwatson */
121148627Srwatsonint	memstat_kvm_all(struct memory_type_list *list, void *kvm_handle);
122148789Srwatsonint	memstat_kvm_malloc(struct memory_type_list *list, void *kvm_handle);
123148627Srwatsonint	memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle);
124148627Srwatson
125148627Srwatson/*
126148357Srwatson * Accessor methods for struct memory_type.
127147997Srwatson */
128147997Srwatsonconst char	*memstat_get_name(const struct memory_type *mtp);
129147997Srwatsonint		 memstat_get_allocator(const struct memory_type *mtp);
130147997Srwatsonuint64_t	 memstat_get_countlimit(const struct memory_type *mtp);
131147997Srwatsonuint64_t	 memstat_get_byteslimit(const struct memory_type *mtp);
132147997Srwatsonuint64_t	 memstat_get_sizemask(const struct memory_type *mtp);
133147997Srwatsonuint64_t	 memstat_get_size(const struct memory_type *mtp);
134147997Srwatsonuint64_t	 memstat_get_memalloced(const struct memory_type *mtp);
135147997Srwatsonuint64_t	 memstat_get_memfreed(const struct memory_type *mtp);
136147997Srwatsonuint64_t	 memstat_get_numallocs(const struct memory_type *mtp);
137147997Srwatsonuint64_t	 memstat_get_numfrees(const struct memory_type *mtp);
138147997Srwatsonuint64_t	 memstat_get_bytes(const struct memory_type *mtp);
139147997Srwatsonuint64_t	 memstat_get_count(const struct memory_type *mtp);
140147997Srwatsonuint64_t	 memstat_get_free(const struct memory_type *mtp);
141147997Srwatsonuint64_t	 memstat_get_failures(const struct memory_type *mtp);
142209215Ssbrunouint64_t	 memstat_get_sleeps(const struct memory_type *mtp);
143147997Srwatsonvoid		*memstat_get_caller_pointer(const struct memory_type *mtp,
144147997Srwatson		    int index);
145147997Srwatsonvoid		 memstat_set_caller_pointer(struct memory_type *mtp,
146147997Srwatson		    int index, void *value);
147147997Srwatsonuint64_t	 memstat_get_caller_uint64(const struct memory_type *mtp,
148147997Srwatson		    int index);
149147997Srwatsonvoid		 memstat_set_caller_uint64(struct memory_type *mtp, int index,
150147997Srwatson		    uint64_t value);
151147997Srwatsonuint64_t	 memstat_get_zonefree(const struct memory_type *mtp);
152148170Srwatsonuint64_t	 memstat_get_kegfree(const struct memory_type *mtp);
153147997Srwatsonuint64_t	 memstat_get_percpu_memalloced(const struct memory_type *mtp,
154147997Srwatson		    int cpu);
155147997Srwatsonuint64_t	 memstat_get_percpu_memfreed(const struct memory_type *mtp,
156147997Srwatson		    int cpu);
157147997Srwatsonuint64_t	 memstat_get_percpu_numallocs(const struct memory_type *mtp,
158147997Srwatson		    int cpu);
159147997Srwatsonuint64_t	 memstat_get_percpu_numfrees(const struct memory_type *mtp,
160147997Srwatson		    int cpu);
161147997Srwatsonuint64_t	 memstat_get_percpu_sizemask(const struct memory_type *mtp,
162147997Srwatson		    int cpu);
163147997Srwatsonvoid		*memstat_get_percpu_caller_pointer(
164147997Srwatson		    const struct memory_type *mtp, int cpu, int index);
165147997Srwatsonvoid		 memstat_set_percpu_caller_pointer(struct memory_type *mtp,
166147997Srwatson		    int cpu, int index, void *value);
167147997Srwatsonuint64_t	 memstat_get_percpu_caller_uint64(
168147997Srwatson		    const struct memory_type *mtp, int cpu, int index);
169147997Srwatsonvoid		 memstat_set_percpu_caller_uint64(struct memory_type *mtp,
170147997Srwatson		    int cpu, int index, uint64_t value);
171147997Srwatsonuint64_t	 memstat_get_percpu_free(const struct memory_type *mtp,
172147997Srwatson		    int cpu);
173148094Srwatson__END_DECLS
174147997Srwatson
175147997Srwatson#endif /* !_MEMSTAT_H_ */
176