xref: /freebsd/lib/libmemstat/memstat_internal.h (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #ifndef _MEMSTAT_INTERNAL_H_
32 #define	_MEMSTAT_INTERNAL_H_
33 
34 /*
35  * memstat maintains its own internal notion of statistics on each memory
36  * type, common across UMA and kernel malloc.  Some fields are straight from
37  * the allocator statistics, others are derived when extracted from the
38  * kernel.  A struct memory_type will describe each type supported by an
39  * allocator.  memory_type structures can be chained into lists.
40  */
41 struct memory_type {
42 	/*
43 	 * Static properties of type.
44 	 */
45 	int	 mt_allocator;		/* malloc(9), uma(9), etc. */
46 	char	 mt_name[MEMTYPE_MAXNAME];	/* name of memory type. */
47 
48 	/*
49 	 * (Relatively) static zone settings, that don't uniquely identify
50 	 * the zone, but also don't change much.
51 	 */
52 	uint64_t	 mt_countlimit;	/* 0, or maximum allocations. */
53 	uint64_t	 mt_byteslimit;	/* 0, or maximum bytes. */
54 	uint64_t	 mt_sizemask;	/* malloc: allocated size bitmask. */
55 	uint64_t	 mt_size;	/* uma: size of objects. */
56 	uint64_t	 mt_rsize;	/* uma: real size of objects. */
57 
58 	/*
59 	 * Zone or type information that includes all caches and any central
60 	 * zone state.  Depending on the allocator, this may be synthesized
61 	 * from several sources, or directly measured.
62 	 */
63 	uint64_t	 mt_memalloced;	/* Bytes allocated over life time. */
64 	uint64_t	 mt_memfreed;	/* Bytes freed over life time. */
65 	uint64_t	 mt_numallocs;	/* Allocations over life time. */
66 	uint64_t	 mt_numfrees;	/* Frees over life time. */
67 	uint64_t	 mt_bytes;	/* Bytes currently allocated. */
68 	uint64_t	 mt_count;	/* Number of current allocations. */
69 	uint64_t	 mt_free;	/* Number of cached free items. */
70 	uint64_t	 mt_failures;	/* Number of allocation failures. */
71 	uint64_t	 mt_sleeps;	/* Number of allocation sleeps. */
72 	uint64_t	 mt_xdomain;	/* Number of cross domain sleeps. */
73 
74 	/*
75 	 * Caller-owned memory.
76 	 */
77 	void		*mt_caller_pointer[MEMSTAT_MAXCALLER];	/* Pointers. */
78 	uint64_t	 mt_caller_uint64[MEMSTAT_MAXCALLER];	/* Integers. */
79 
80 	/*
81 	 * For allocators making use of per-CPU caches, we also provide raw
82 	 * statistics from the central allocator and each per-CPU cache,
83 	 * which (combined) sometimes make up the above general statistics.
84 	 *
85 	 * First, central zone/type state, all numbers excluding any items
86 	 * cached in per-CPU caches.
87 	 *
88 	 * XXXRW: Might be desirable to separately expose allocation stats
89 	 * from zone, which should (combined with per-cpu) add up to the
90 	 * global stats above.
91 	 */
92 	uint64_t	 mt_zonefree;	/* Free items in zone. */
93 	uint64_t	 mt_kegfree;	/* Free items in keg. */
94 
95 	/*
96 	 * Per-CPU measurements fall into two categories: per-CPU allocation,
97 	 * and per-CPU cache state.
98 	 */
99 	struct mt_percpu_alloc_s {
100 		uint64_t	 mtp_memalloced;/* Per-CPU mt_memalloced. */
101 		uint64_t	 mtp_memfreed;	/* Per-CPU mt_memfreed. */
102 		uint64_t	 mtp_numallocs;	/* Per-CPU mt_numallocs. */
103 		uint64_t	 mtp_numfrees;	/* Per-CPU mt_numfrees. */
104 		uint64_t	 mtp_sizemask;	/* Per-CPU mt_sizemask. */
105 		void		*mtp_caller_pointer[MEMSTAT_MAXCALLER];
106 		uint64_t	 mtp_caller_uint64[MEMSTAT_MAXCALLER];
107 	}	*mt_percpu_alloc;
108 
109 	struct mt_percpu_cache_s {
110 		uint64_t	 mtp_free;	/* Per-CPU cache free items. */
111 	}	*mt_percpu_cache;
112 
113 	LIST_ENTRY(memory_type)	mt_list;	/* List of types. */
114 };
115 
116 /*
117  * Description of struct memory_type_list is in memstat.h.
118  */
119 struct memory_type_list {
120 	LIST_HEAD(, memory_type)	mtl_list;
121 	int				mtl_error;
122 };
123 
124 void			 _memstat_mtl_empty(struct memory_type_list *list);
125 struct memory_type	*_memstat_mt_allocate(struct memory_type_list *list,
126 			    int allocator, const char *name, int maxcpus);
127 void			 _memstat_mt_reset_stats(struct memory_type *mtp,
128 			    int maxcpus);
129 
130 #endif /* !_MEMSTAT_INTERNAL_H_ */
131