xref: /f-stack/tools/libmemstat/memstat.h (revision 22ce4aff)
11eaf0ac3Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
41eaf0ac3Slogwang  * Copyright (c) 2005 Robert N. M. Watson
51eaf0ac3Slogwang  * All rights reserved.
61eaf0ac3Slogwang  *
71eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
81eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
91eaf0ac3Slogwang  * are met:
101eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
111eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer.
121eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
131eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
141eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
151eaf0ac3Slogwang  *
161eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171eaf0ac3Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181eaf0ac3Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191eaf0ac3Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201eaf0ac3Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211eaf0ac3Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221eaf0ac3Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231eaf0ac3Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241eaf0ac3Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251eaf0ac3Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261eaf0ac3Slogwang  * SUCH DAMAGE.
271eaf0ac3Slogwang  *
281eaf0ac3Slogwang  * $FreeBSD$
291eaf0ac3Slogwang  */
301eaf0ac3Slogwang 
311eaf0ac3Slogwang #ifndef _MEMSTAT_H_
321eaf0ac3Slogwang #define	_MEMSTAT_H_
331eaf0ac3Slogwang 
341eaf0ac3Slogwang /*
351eaf0ac3Slogwang  * Amount of caller data to maintain for each caller data slot.  Applications
361eaf0ac3Slogwang  * must not request more than this number of caller save data, or risk
371eaf0ac3Slogwang  * corrupting internal libmemstat(3) data structures.  A compile time check
381eaf0ac3Slogwang  * in the application is probably appropriate.
391eaf0ac3Slogwang  */
401eaf0ac3Slogwang #define	MEMSTAT_MAXCALLER	16
411eaf0ac3Slogwang 
421eaf0ac3Slogwang /*
431eaf0ac3Slogwang  * libmemstat(3) is able to extract memory data from different allocators;
441eaf0ac3Slogwang  * when it does so, it tags which allocator it got the data from so that
451eaf0ac3Slogwang  * consumers can determine which fields are usable, as data returned varies
461eaf0ac3Slogwang  * some.
471eaf0ac3Slogwang  */
481eaf0ac3Slogwang #define	ALLOCATOR_UNKNOWN	0
491eaf0ac3Slogwang #define	ALLOCATOR_MALLOC	1
501eaf0ac3Slogwang #define	ALLOCATOR_UMA		2
511eaf0ac3Slogwang #define	ALLOCATOR_ANY		255
521eaf0ac3Slogwang 
531eaf0ac3Slogwang /*
541eaf0ac3Slogwang  * Library maximum type name.  Should be max(set of name maximums over
551eaf0ac3Slogwang  * various allocators).
561eaf0ac3Slogwang  */
571eaf0ac3Slogwang #define	MEMTYPE_MAXNAME		32
581eaf0ac3Slogwang 
591eaf0ac3Slogwang /*
601eaf0ac3Slogwang  * Library error conditions, mostly from the underlying data sources.  On
611eaf0ac3Slogwang  * failure, functions typically return (-1) or (NULL); on success, (0) or a
621eaf0ac3Slogwang  * valid data pointer.  The error from the last operation is stored in
631eaf0ac3Slogwang  * struct memory_type_list, and accessed via memstat_get_error(list).
641eaf0ac3Slogwang  */
651eaf0ac3Slogwang #define	MEMSTAT_ERROR_UNDEFINED		0	/* Initialization value. */
661eaf0ac3Slogwang #define	MEMSTAT_ERROR_NOMEMORY		1	/* Out of memory. */
671eaf0ac3Slogwang #define	MEMSTAT_ERROR_VERSION		2	/* Unsupported version. */
681eaf0ac3Slogwang #define	MEMSTAT_ERROR_PERMISSION	3	/* Permission denied. */
691eaf0ac3Slogwang #define	MEMSTAT_ERROR_DATAERROR		5	/* Error in stat data. */
701eaf0ac3Slogwang #define	MEMSTAT_ERROR_KVM		6	/* See kvm_geterr() for err. */
711eaf0ac3Slogwang #define	MEMSTAT_ERROR_KVM_NOSYMBOL	7	/* Symbol not available. */
721eaf0ac3Slogwang #define	MEMSTAT_ERROR_KVM_SHORTREAD	8	/* Short kvm_read return. */
731eaf0ac3Slogwang 
741eaf0ac3Slogwang /*
751eaf0ac3Slogwang  * Forward declare struct memory_type, which holds per-type properties and
761eaf0ac3Slogwang  * statistics.  This is an opaque type, to be frobbed only from within the
771eaf0ac3Slogwang  * library, in order to avoid building ABI assumptions into the application.
781eaf0ac3Slogwang  * Accessor methods should be used to get and sometimes set the fields from
791eaf0ac3Slogwang  * consumers of the library.
801eaf0ac3Slogwang  */
811eaf0ac3Slogwang struct memory_type;
821eaf0ac3Slogwang 
831eaf0ac3Slogwang /*
841eaf0ac3Slogwang  * struct memory_type_list is the head of a list of memory types and
851eaf0ac3Slogwang  * statistics.
861eaf0ac3Slogwang  */
871eaf0ac3Slogwang struct memory_type_list;
881eaf0ac3Slogwang 
891eaf0ac3Slogwang __BEGIN_DECLS
901eaf0ac3Slogwang /*
911eaf0ac3Slogwang  * Functions that operate without memory type or memory type list context.
921eaf0ac3Slogwang  */
931eaf0ac3Slogwang const char	*memstat_strerror(int error);
941eaf0ac3Slogwang 
951eaf0ac3Slogwang /*
961eaf0ac3Slogwang  * Functions for managing memory type and statistics data.
971eaf0ac3Slogwang  */
981eaf0ac3Slogwang struct memory_type_list	*memstat_mtl_alloc(void);
991eaf0ac3Slogwang struct memory_type	*memstat_mtl_first(struct memory_type_list *list);
1001eaf0ac3Slogwang struct memory_type	*memstat_mtl_next(struct memory_type *mtp);
1011eaf0ac3Slogwang struct memory_type	*memstat_mtl_find(struct memory_type_list *list,
1021eaf0ac3Slogwang 			    int allocator, const char *name);
1031eaf0ac3Slogwang void	memstat_mtl_free(struct memory_type_list *list);
1041eaf0ac3Slogwang int	memstat_mtl_geterror(struct memory_type_list *list);
1051eaf0ac3Slogwang 
1061eaf0ac3Slogwang /*
1071eaf0ac3Slogwang  * Functions to retrieve data from a live kernel using sysctl.
1081eaf0ac3Slogwang  */
1091eaf0ac3Slogwang int	memstat_sysctl_all(struct memory_type_list *list, int flags);
1101eaf0ac3Slogwang int	memstat_sysctl_malloc(struct memory_type_list *list, int flags);
1111eaf0ac3Slogwang int	memstat_sysctl_uma(struct memory_type_list *list, int flags);
1121eaf0ac3Slogwang 
1131eaf0ac3Slogwang /*
1141eaf0ac3Slogwang  * Functions to retrieve data from a kernel core (or /dev/kmem).
1151eaf0ac3Slogwang  */
1161eaf0ac3Slogwang int	memstat_kvm_all(struct memory_type_list *list, void *kvm_handle);
1171eaf0ac3Slogwang int	memstat_kvm_malloc(struct memory_type_list *list, void *kvm_handle);
1181eaf0ac3Slogwang int	memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle);
1191eaf0ac3Slogwang 
1201eaf0ac3Slogwang /*
121*22ce4affSfengbojiang  * General malloc routines.
122*22ce4affSfengbojiang  */
123*22ce4affSfengbojiang size_t	memstat_malloc_zone_get_count(void);
124*22ce4affSfengbojiang size_t	memstat_malloc_zone_get_size(size_t n);
125*22ce4affSfengbojiang int	memstat_malloc_zone_used(const struct memory_type *mtp, size_t n);
126*22ce4affSfengbojiang 
127*22ce4affSfengbojiang /*
1281eaf0ac3Slogwang  * Accessor methods for struct memory_type.
1291eaf0ac3Slogwang  */
1301eaf0ac3Slogwang const char	*memstat_get_name(const struct memory_type *mtp);
1311eaf0ac3Slogwang int		 memstat_get_allocator(const struct memory_type *mtp);
1321eaf0ac3Slogwang uint64_t	 memstat_get_countlimit(const struct memory_type *mtp);
1331eaf0ac3Slogwang uint64_t	 memstat_get_byteslimit(const struct memory_type *mtp);
1341eaf0ac3Slogwang uint64_t	 memstat_get_sizemask(const struct memory_type *mtp);
1351eaf0ac3Slogwang uint64_t	 memstat_get_size(const struct memory_type *mtp);
1361eaf0ac3Slogwang uint64_t	 memstat_get_rsize(const struct memory_type *mtp);
1371eaf0ac3Slogwang uint64_t	 memstat_get_memalloced(const struct memory_type *mtp);
1381eaf0ac3Slogwang uint64_t	 memstat_get_memfreed(const struct memory_type *mtp);
1391eaf0ac3Slogwang uint64_t	 memstat_get_numallocs(const struct memory_type *mtp);
1401eaf0ac3Slogwang uint64_t	 memstat_get_numfrees(const struct memory_type *mtp);
1411eaf0ac3Slogwang uint64_t	 memstat_get_bytes(const struct memory_type *mtp);
1421eaf0ac3Slogwang uint64_t	 memstat_get_count(const struct memory_type *mtp);
1431eaf0ac3Slogwang uint64_t	 memstat_get_free(const struct memory_type *mtp);
1441eaf0ac3Slogwang uint64_t	 memstat_get_failures(const struct memory_type *mtp);
1451eaf0ac3Slogwang uint64_t	 memstat_get_sleeps(const struct memory_type *mtp);
146*22ce4affSfengbojiang uint64_t	 memstat_get_xdomain(const struct memory_type *mtp);
1471eaf0ac3Slogwang void		*memstat_get_caller_pointer(const struct memory_type *mtp,
1481eaf0ac3Slogwang 		    int index);
1491eaf0ac3Slogwang void		 memstat_set_caller_pointer(struct memory_type *mtp,
1501eaf0ac3Slogwang 		    int index, void *value);
1511eaf0ac3Slogwang uint64_t	 memstat_get_caller_uint64(const struct memory_type *mtp,
1521eaf0ac3Slogwang 		    int index);
1531eaf0ac3Slogwang void		 memstat_set_caller_uint64(struct memory_type *mtp, int index,
1541eaf0ac3Slogwang 		    uint64_t value);
1551eaf0ac3Slogwang uint64_t	 memstat_get_zonefree(const struct memory_type *mtp);
1561eaf0ac3Slogwang uint64_t	 memstat_get_kegfree(const struct memory_type *mtp);
1571eaf0ac3Slogwang uint64_t	 memstat_get_percpu_memalloced(const struct memory_type *mtp,
1581eaf0ac3Slogwang 		    int cpu);
1591eaf0ac3Slogwang uint64_t	 memstat_get_percpu_memfreed(const struct memory_type *mtp,
1601eaf0ac3Slogwang 		    int cpu);
1611eaf0ac3Slogwang uint64_t	 memstat_get_percpu_numallocs(const struct memory_type *mtp,
1621eaf0ac3Slogwang 		    int cpu);
1631eaf0ac3Slogwang uint64_t	 memstat_get_percpu_numfrees(const struct memory_type *mtp,
1641eaf0ac3Slogwang 		    int cpu);
1651eaf0ac3Slogwang uint64_t	 memstat_get_percpu_sizemask(const struct memory_type *mtp,
1661eaf0ac3Slogwang 		    int cpu);
1671eaf0ac3Slogwang void		*memstat_get_percpu_caller_pointer(
1681eaf0ac3Slogwang 		    const struct memory_type *mtp, int cpu, int index);
1691eaf0ac3Slogwang void		 memstat_set_percpu_caller_pointer(struct memory_type *mtp,
1701eaf0ac3Slogwang 		    int cpu, int index, void *value);
1711eaf0ac3Slogwang uint64_t	 memstat_get_percpu_caller_uint64(
1721eaf0ac3Slogwang 		    const struct memory_type *mtp, int cpu, int index);
1731eaf0ac3Slogwang void		 memstat_set_percpu_caller_uint64(struct memory_type *mtp,
1741eaf0ac3Slogwang 		    int cpu, int index, uint64_t value);
1751eaf0ac3Slogwang uint64_t	 memstat_get_percpu_free(const struct memory_type *mtp,
1761eaf0ac3Slogwang 		    int cpu);
1771eaf0ac3Slogwang __END_DECLS
1781eaf0ac3Slogwang 
1791eaf0ac3Slogwang #endif /* !_MEMSTAT_H_ */
180