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$
27147997Srwatson */
28147997Srwatson
29147997Srwatson#ifndef _MEMSTAT_INTERNAL_H_
30147997Srwatson#define	_MEMSTAT_INTERNAL_H_
31147997Srwatson
32147997Srwatson/*
33147997Srwatson * memstat maintains its own internal notion of statistics on each memory
34147997Srwatson * type, common across UMA and kernel malloc.  Some fields are straight from
35147997Srwatson * the allocator statistics, others are derived when extracted from the
36147997Srwatson * kernel.  A struct memory_type will describe each type supported by an
37147997Srwatson * allocator.  memory_type structures can be chained into lists.
38147997Srwatson */
39147997Srwatsonstruct memory_type {
40147997Srwatson	/*
41147997Srwatson	 * Static properties of type.
42147997Srwatson	 */
43147997Srwatson	int	 mt_allocator;		/* malloc(9), uma(9), etc. */
44147997Srwatson	char	 mt_name[MEMTYPE_MAXNAME];	/* name of memory type. */
45147997Srwatson
46147997Srwatson	/*
47147997Srwatson	 * (Relatively) static zone settings, that don't uniquely identify
48147997Srwatson	 * the zone, but also don't change much.
49147997Srwatson	 */
50147997Srwatson	uint64_t	 mt_countlimit;	/* 0, or maximum allocations. */
51147997Srwatson	uint64_t	 mt_byteslimit;	/* 0, or maximum bytes. */
52147997Srwatson	uint64_t	 mt_sizemask;	/* malloc: allocated size bitmask. */
53147997Srwatson	uint64_t	 mt_size;	/* uma: size of objects. */
54147997Srwatson
55147997Srwatson	/*
56147997Srwatson	 * Zone or type information that includes all caches and any central
57147997Srwatson	 * zone state.  Depending on the allocator, this may be synthesized
58147997Srwatson	 * from several sources, or directly measured.
59147997Srwatson	 */
60147997Srwatson	uint64_t	 mt_memalloced;	/* Bytes allocated over life time. */
61147997Srwatson	uint64_t	 mt_memfreed;	/* Bytes freed over life time. */
62147997Srwatson	uint64_t	 mt_numallocs;	/* Allocations over life time. */
63147997Srwatson	uint64_t	 mt_numfrees;	/* Frees over life time. */
64147997Srwatson	uint64_t	 mt_bytes;	/* Bytes currently allocated. */
65147997Srwatson	uint64_t	 mt_count;	/* Number of current allocations. */
66147997Srwatson	uint64_t	 mt_free;	/* Number of cached free items. */
67147997Srwatson	uint64_t	 mt_failures;	/* Number of allocation failures. */
68209215Ssbruno	uint64_t	 mt_sleeps;	/* Number of allocation sleeps. */
69147997Srwatson
70147997Srwatson	/*
71147997Srwatson	 * Caller-owned memory.
72147997Srwatson	 */
73148041Srwatson	void		*mt_caller_pointer[MEMSTAT_MAXCALLER];	/* Pointers. */
74148041Srwatson	uint64_t	 mt_caller_uint64[MEMSTAT_MAXCALLER];	/* Integers. */
75147997Srwatson
76147997Srwatson	/*
77147997Srwatson	 * For allocators making use of per-CPU caches, we also provide raw
78147997Srwatson	 * statistics from the central allocator and each per-CPU cache,
79147997Srwatson	 * which (combined) sometimes make up the above general statistics.
80147997Srwatson	 *
81147997Srwatson	 * First, central zone/type state, all numbers excluding any items
82147997Srwatson	 * cached in per-CPU caches.
83147997Srwatson	 *
84147997Srwatson	 * XXXRW: Might be desirable to separately expose allocation stats
85147997Srwatson	 * from zone, which should (combined with per-cpu) add up to the
86147997Srwatson	 * global stats above.
87147997Srwatson	 */
88147997Srwatson	uint64_t	 mt_zonefree;	/* Free items in zone. */
89148170Srwatson	uint64_t	 mt_kegfree;	/* Free items in keg. */
90147997Srwatson
91147997Srwatson	/*
92147997Srwatson	 * Per-CPU measurements fall into two categories: per-CPU allocation,
93147997Srwatson	 * and per-CPU cache state.
94147997Srwatson	 */
95224569Spluknet	struct mt_percpu_alloc_s {
96147997Srwatson		uint64_t	 mtp_memalloced;/* Per-CPU mt_memalloced. */
97147997Srwatson		uint64_t	 mtp_memfreed;	/* Per-CPU mt_memfreed. */
98147997Srwatson		uint64_t	 mtp_numallocs;	/* Per-CPU mt_numallocs. */
99147997Srwatson		uint64_t	 mtp_numfrees;	/* Per-CPU mt_numfrees. */
100147997Srwatson		uint64_t	 mtp_sizemask;	/* Per-CPU mt_sizemask. */
101148041Srwatson		void		*mtp_caller_pointer[MEMSTAT_MAXCALLER];
102148041Srwatson		uint64_t	 mtp_caller_uint64[MEMSTAT_MAXCALLER];
103224569Spluknet	}	*mt_percpu_alloc;
104147997Srwatson
105224569Spluknet	struct mt_percpu_cache_s {
106147997Srwatson		uint64_t	 mtp_free;	/* Per-CPU cache free items. */
107224569Spluknet	}	*mt_percpu_cache;
108147997Srwatson
109147997Srwatson	LIST_ENTRY(memory_type)	mt_list;	/* List of types. */
110147997Srwatson};
111147997Srwatson
112147997Srwatson/*
113147997Srwatson * Description of struct memory_type_list is in memstat.h.
114147997Srwatson */
115148357Srwatsonstruct memory_type_list {
116148357Srwatson	LIST_HEAD(, memory_type)	mtl_list;
117148357Srwatson	int				mtl_error;
118148357Srwatson};
119147997Srwatson
120148619Srwatsonvoid			 _memstat_mtl_empty(struct memory_type_list *list);
121148354Srwatsonstruct memory_type	*_memstat_mt_allocate(struct memory_type_list *list,
122224569Spluknet			    int allocator, const char *name, int maxcpus);
123224569Spluknetvoid			 _memstat_mt_reset_stats(struct memory_type *mtp,
124224569Spluknet			    int maxcpus);
125147997Srwatson
126147997Srwatson#endif /* !_MEMSTAT_INTERNAL_H_ */
127