memstat.h revision 148094
172878Skris/*-
272878Skris * Copyright (c) 2005 Robert N. M. Watson
3100772Sjhb * All rights reserved.
4100772Sjhb *
5100772Sjhb * Redistribution and use in source and binary forms, with or without
672878Skris * modification, are permitted provided that the following conditions
7101232Sru * are met:
8136606Sobrien * 1. Redistributions of source code must retain the above copyright
9100773Sjhb *    notice, this list of conditions and the following disclaimer.
10113374Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11126657Sbde *    notice, this list of conditions and the following disclaimer in the
12115175Speter *    documentation and/or other materials provided with the distribution.
13100773Sjhb *
14103560Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15176776Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16176776Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17100773Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18129217Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19129217Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20177385Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21177385Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22100773Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23100772Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2472878Skris * SUCH DAMAGE.
2572878Skris *
2672878Skris * $FreeBSD: head/lib/libmemstat/memstat.h 148094 2005-07-17 13:54:46Z rwatson $
2772878Skris */
28100773Sjhb
29166071Sdes#ifndef _MEMSTAT_H_
30166071Sdes#define	_MEMSTAT_H_
31166071Sdes
32166071Sdes/*
33136607Sobrien * Number of CPU slots in library-internal data structures.  This should be
34136606Sobrien * at least the value of MAXCPU from param.h.
35136606Sobrien */
36136606Sobrien#define	MEMSTAT_MAXCPU	16
37136606Sobrien
38136606Sobrien/*
39136606Sobrien * Amount of caller data to maintain for each caller data slot.  Applications
40136606Sobrien * must not request more than this number of caller save data, or risk
41136606Sobrien * corrupting internal libmemstat(3) data structures.  A compile time check
42136606Sobrien * in the application is probably appropriate.
43136606Sobrien */
44136606Sobrien#define	MEMSTAT_MAXCALLER	8
45136606Sobrien
46136606Sobrien/*
47136606Sobrien * libmemstat(3) is able to extract memory data from different allocators;
48136606Sobrien * when it does so, it tags which allocator it got the data from so that
49136606Sobrien * consumers can determine which fields are usable, as data returned varies
50136606Sobrien * some.
51136607Sobrien */
52136607Sobrien#define	ALLOCATOR_UNKNOWN	0
53133525Sobrien#define	ALLOCATOR_MALLOC	1
54103045Smux#define	ALLOCATOR_UMA		2
55103045Smux#define	ALLOCATOR_ANY		255
56100773Sjhb
57136607Sobrien/*
58166072Sdes * Library maximum type name.  Should be max(set of name maximums over
59136607Sobrien * various allocators).
60136607Sobrien */
6172878Skris#define	MEMTYPE_MAXNAME		32
6272878Skris
63136606Sobrien/*
6496421Sobrien * Forward declare struct memory_type, which holds per-type properties and
6572878Skris * statistics.  This is an opaque type, to be frobbed only from within the
6696421Sobrien * library, in order to avoid building ABI assumptions into the application.
67160536Simp * Accessor methods should be used to get and sometimes set the fields from
68127258Smarcel * consumers of the library.
69127258Smarcel */
70177385Simpstruct memory_type;
7196421Sobrien
72127258Smarcel/*
7372878Skris * struct memory_type_list is the head of a list of memory types and
7472878Skris * statistics.
75127888Sdfr */
76133000Sobrienstruct memory_type_list;
77136606Sobrien
78136606Sobrien__BEGIN_DECLS
79136606Sobrien/*
80136606Sobrien * Functions for managing memory type and statistics data.
81136606Sobrien */
82136606Sobrienstruct memory_type_list	*memstat_mtl_alloc(void);
83127888Sdfrstruct memory_type	*memstat_mtl_first(struct memory_type_list *list);
84127888Sdfrstruct memory_type	*memstat_mtl_next(struct memory_type *mtp);
85126938Strhodesstruct memory_type	*memstat_mtl_find(struct memory_type_list *list,
86126890Strhodes			    int allocator, const char *name);
87126890Strhodesvoid	memstat_mtl_free(struct memory_type_list *list);
88126890Strhodes
89112768Sobrien/*
90126890Strhodes * Functions to retrieve data from a live kernel using sysctl.
9172878Skris */
92126890Strhodesint	memstat_sysctl_all(struct memory_type_list *list, int flags);
93136606Sobrienint	memstat_sysctl_malloc(struct memory_type_list *list, int flags);
94126890Strhodesint	memstat_sysctl_uma(struct memory_type_list *list, int flags);
95136606Sobrien
96136606Sobrien/*
97126890Strhodes * Accessor methods for struct memory_type_list.
98136606Sobrien */
99126890Strhodesconst char	*memstat_get_name(const struct memory_type *mtp);
100136606Sobrienint		 memstat_get_allocator(const struct memory_type *mtp);
101126890Strhodesuint64_t	 memstat_get_countlimit(const struct memory_type *mtp);
102136606Sobrienuint64_t	 memstat_get_byteslimit(const struct memory_type *mtp);
103126890Strhodesuint64_t	 memstat_get_sizemask(const struct memory_type *mtp);
104136606Sobrienuint64_t	 memstat_get_size(const struct memory_type *mtp);
105126890Strhodesuint64_t	 memstat_get_memalloced(const struct memory_type *mtp);
106136606Sobrienuint64_t	 memstat_get_memfreed(const struct memory_type *mtp);
107136607Sobrienuint64_t	 memstat_get_numallocs(const struct memory_type *mtp);
108136607Sobrienuint64_t	 memstat_get_numfrees(const struct memory_type *mtp);
109135678Scognetuint64_t	 memstat_get_bytes(const struct memory_type *mtp);
110136606Sobrienuint64_t	 memstat_get_count(const struct memory_type *mtp);
111172706Scognetuint64_t	 memstat_get_free(const struct memory_type *mtp);
112172706Scognetuint64_t	 memstat_get_failures(const struct memory_type *mtp);
113172706Scognetvoid		*memstat_get_caller_pointer(const struct memory_type *mtp,
114136606Sobrien		    int index);
115136606Sobrienvoid		 memstat_set_caller_pointer(struct memory_type *mtp,
116135678Scognet		    int index, void *value);
117176776Srajuint64_t	 memstat_get_caller_uint64(const struct memory_type *mtp,
118176776Sraj		    int index);
119176776Srajvoid		 memstat_set_caller_uint64(struct memory_type *mtp, int index,
120176776Sraj		    uint64_t value);
121176776Srajuint64_t	 memstat_get_zonefree(const struct memory_type *mtp);
122177385Simpuint64_t	 memstat_get_percpu_memalloced(const struct memory_type *mtp,
123177385Simp		    int cpu);
124177385Simpuint64_t	 memstat_get_percpu_memfreed(const struct memory_type *mtp,
125177385Simp		    int cpu);
126177385Simpuint64_t	 memstat_get_percpu_numallocs(const struct memory_type *mtp,
127177385Simp		    int cpu);
128177385Simpuint64_t	 memstat_get_percpu_numfrees(const struct memory_type *mtp,
129177385Simp		    int cpu);
130177385Simpuint64_t	 memstat_get_percpu_sizemask(const struct memory_type *mtp,
131177385Simp		    int cpu);
132177385Simpvoid		*memstat_get_percpu_caller_pointer(
133177385Simp		    const struct memory_type *mtp, int cpu, int index);
134177385Simpvoid		 memstat_set_percpu_caller_pointer(struct memory_type *mtp,
135177385Simp		    int cpu, int index, void *value);
13672878Skrisuint64_t	 memstat_get_percpu_caller_uint64(
13772878Skris		    const struct memory_type *mtp, int cpu, int index);
13872878Skrisvoid		 memstat_set_percpu_caller_uint64(struct memory_type *mtp,
13972878Skris		    int cpu, int index, uint64_t value);
14072878Skrisuint64_t	 memstat_get_percpu_free(const struct memory_type *mtp,
14172878Skris		    int cpu);
142126657Sbde__END_DECLS
143136607Sobrien
144136607Sobrien#endif /* !_MEMSTAT_H_ */
145136607Sobrien