uma.h revision 244024
1139825Simp/*-
2148078Srwatson * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
3148078Srwatson * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4148078Srwatson * All rights reserved.
592654Sjeff *
692654Sjeff * Redistribution and use in source and binary forms, with or without
792654Sjeff * modification, are permitted provided that the following conditions
892654Sjeff * are met:
992654Sjeff * 1. Redistributions of source code must retain the above copyright
1092654Sjeff *    notice unmodified, this list of conditions, and the following
1192654Sjeff *    disclaimer.
1292654Sjeff * 2. Redistributions in binary form must reproduce the above copyright
1392654Sjeff *    notice, this list of conditions and the following disclaimer in the
1492654Sjeff *    documentation and/or other materials provided with the distribution.
1592654Sjeff *
1692654Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1792654Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1892654Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1992654Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2092654Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2192654Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2292654Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2392654Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2492654Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2592654Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2692654Sjeff *
2792654Sjeff * $FreeBSD: head/sys/vm/uma.h 244024 2012-12-08 09:23:05Z pjd $
2892654Sjeff *
2992654Sjeff */
3092654Sjeff
3192654Sjeff/*
3292654Sjeff * uma.h - External definitions for the Universal Memory Allocator
3392654Sjeff *
3492654Sjeff*/
3592654Sjeff
3692654Sjeff#ifndef VM_UMA_H
3792654Sjeff#define VM_UMA_H
3892654Sjeff
3992654Sjeff#include <sys/param.h>		/* For NULL */
4092654Sjeff#include <sys/malloc.h>		/* For M_* */
4192654Sjeff
42184546Skeramida/* User visible parameters */
4392654Sjeff#define UMA_SMALLEST_UNIT       (PAGE_SIZE / 256) /* Smallest item allocated */
4492654Sjeff
4592654Sjeff/* Types and type defs */
4692654Sjeff
47129906Sbmilekicstruct uma_zone;
4892654Sjeff/* Opaque type used as a handle to the zone */
4992654Sjefftypedef struct uma_zone * uma_zone_t;
5092654Sjeff
51166213Smohansvoid zone_drain(uma_zone_t);
52166213Smohans
53244024Spjd/*
5492654Sjeff * Item constructor
5592654Sjeff *
5692654Sjeff * Arguments:
5792654Sjeff *	item  A pointer to the memory which has been allocated.
5892654Sjeff *	arg   The arg field passed to uma_zalloc_arg
5992654Sjeff *	size  The size of the allocated item
60132987Sgreen *	flags See zalloc flags
61244024Spjd *
6292654Sjeff * Returns:
63132987Sgreen *	0      on success
64132987Sgreen *      errno  on failure
6592654Sjeff *
6692654Sjeff * Discussion:
6792654Sjeff *	The constructor is called just before the memory is returned
68105689Ssheldonh *	to the user. It may block if necessary.
6992654Sjeff */
70132987Sgreentypedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
7192654Sjeff
7292654Sjeff/*
7392654Sjeff * Item destructor
7492654Sjeff *
7592654Sjeff * Arguments:
7692654Sjeff *	item  A pointer to the memory which has been allocated.
7792654Sjeff *	size  The size of the item being destructed.
7892654Sjeff *	arg   Argument passed through uma_zfree_arg
79244024Spjd *
8092654Sjeff * Returns:
8192654Sjeff *	Nothing
8292654Sjeff *
8392654Sjeff * Discussion:
8492654Sjeff *	The destructor may perform operations that differ from those performed
8592654Sjeff *	by the initializer, but it must leave the object in the same state.
8692654Sjeff *	This IS type stable storage.  This is called after EVERY zfree call.
8792654Sjeff */
8892654Sjefftypedef void (*uma_dtor)(void *mem, int size, void *arg);
8992654Sjeff
90244024Spjd/*
9192654Sjeff * Item initializer
9292654Sjeff *
9392654Sjeff * Arguments:
9492654Sjeff *	item  A pointer to the memory which has been allocated.
9592654Sjeff *	size  The size of the item being initialized.
96132987Sgreen *	flags See zalloc flags
97244024Spjd *
9892654Sjeff * Returns:
99132987Sgreen *	0      on success
100132987Sgreen *      errno  on failure
10192654Sjeff *
10292654Sjeff * Discussion:
103244024Spjd *	The initializer is called when the memory is cached in the uma zone.
104184546Skeramida *	The initializer and the destructor should leave the object in the same
105184546Skeramida *	state.
10692654Sjeff */
107132987Sgreentypedef int (*uma_init)(void *mem, int size, int flags);
10892654Sjeff
10992654Sjeff/*
11092654Sjeff * Item discard function
11192654Sjeff *
11292654Sjeff * Arguments:
113244024Spjd *	item  A pointer to memory which has been 'freed' but has not left the
11492654Sjeff *	      zone's cache.
11592654Sjeff *	size  The size of the item being discarded.
11692654Sjeff *
11792654Sjeff * Returns:
11892654Sjeff *	Nothing
11992654Sjeff *
12092654Sjeff * Discussion:
12192654Sjeff *	This routine is called when memory leaves a zone and is returned to the
122184546Skeramida *	system for other uses.  It is the counter-part to the init function.
12392654Sjeff */
12492654Sjefftypedef void (*uma_fini)(void *mem, int size);
12592654Sjeff
12692654Sjeff/*
12792654Sjeff * What's the difference between initializing and constructing?
12892654Sjeff *
129244024Spjd * The item is initialized when it is cached, and this is the state that the
13092654Sjeff * object should be in when returned to the allocator. The purpose of this is
13192654Sjeff * to remove some code which would otherwise be called on each allocation by
13292654Sjeff * utilizing a known, stable state.  This differs from the constructor which
13392654Sjeff * will be called on EVERY allocation.
13492654Sjeff *
135184546Skeramida * For example, in the initializer you may want to initialize embedded locks,
13692654Sjeff * NULL list pointers, set up initial states, magic numbers, etc.  This way if
137105689Ssheldonh * the object is held in the allocator and re-used it won't be necessary to
13892654Sjeff * re-initialize it.
13992654Sjeff *
14092654Sjeff * The constructor may be used to lock a data structure, link it on to lists,
14192654Sjeff * bump reference counts or total counts of outstanding structures, etc.
14292654Sjeff *
14392654Sjeff */
14492654Sjeff
14592654Sjeff
14692654Sjeff/* Function proto types */
14792654Sjeff
14892654Sjeff/*
14992654Sjeff * Create a new uma zone
15092654Sjeff *
15192654Sjeff * Arguments:
152184546Skeramida *	name  The text name of the zone for debugging and stats. This memory
15392654Sjeff *		should not be freed until the zone has been deallocated.
15492654Sjeff *	size  The size of the object that is being created.
155184546Skeramida *	ctor  The constructor that is called when the object is allocated.
15692654Sjeff *	dtor  The destructor that is called when the object is freed.
15792654Sjeff *	init  An initializer that sets up the initial state of the memory.
15892654Sjeff *	fini  A discard function that undoes initialization done by init.
15992654Sjeff *		ctor/dtor/init/fini may all be null, see notes above.
160184546Skeramida *	align A bitmask that corresponds to the requested alignment
16192654Sjeff *		eg 4 would be 0x3
162184546Skeramida *	flags A set of parameters that control the behavior of the zone.
16392654Sjeff *
16492654Sjeff * Returns:
16592654Sjeff *	A pointer to a structure which is intended to be opaque to users of
16692654Sjeff *	the interface.  The value may be null if the wait flag is not set.
16792654Sjeff */
168242152Smdfuma_zone_t uma_zcreate(const char *name, size_t size, uma_ctor ctor,
169242152Smdf		    uma_dtor dtor, uma_init uminit, uma_fini fini,
170242152Smdf		    int align, u_int32_t flags);
17192654Sjeff
172120223Sjeff/*
173129906Sbmilekic * Create a secondary uma zone
174129906Sbmilekic *
175129906Sbmilekic * Arguments:
176184546Skeramida *	name  The text name of the zone for debugging and stats. This memory
177129906Sbmilekic *		should not be freed until the zone has been deallocated.
178184546Skeramida *	ctor  The constructor that is called when the object is allocated.
179129906Sbmilekic *	dtor  The destructor that is called when the object is freed.
180129906Sbmilekic *	zinit  An initializer that sets up the initial state of the memory
181129906Sbmilekic *		as the object passes from the Keg's slab to the Zone's cache.
182129906Sbmilekic *	zfini  A discard function that undoes initialization done by init
183129906Sbmilekic *		as the object passes from the Zone's cache to the Keg's slab.
184129906Sbmilekic *
185129906Sbmilekic *		ctor/dtor/zinit/zfini may all be null, see notes above.
186129906Sbmilekic *		Note that the zinit and zfini specified here are NOT
187129906Sbmilekic *		exactly the same as the init/fini specified to uma_zcreate()
188129906Sbmilekic *		when creating a master zone.  These zinit/zfini are called
189129906Sbmilekic *		on the TRANSITION from keg to zone (and vice-versa). Once
190129906Sbmilekic *		these are set, the primary zone may alter its init/fini
191129906Sbmilekic *		(which are called when the object passes from VM to keg)
192129906Sbmilekic *		using uma_zone_set_init/fini()) as well as its own
193129906Sbmilekic *		zinit/zfini (unset by default for master zone) with
194129906Sbmilekic *		uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
195129906Sbmilekic *
196129913Sbmilekic *	master  A reference to this zone's Master Zone (Primary Zone),
197129913Sbmilekic *		which contains the backing Keg for the Secondary Zone
198129913Sbmilekic *		being added.
199129906Sbmilekic *
200129906Sbmilekic * Returns:
201129906Sbmilekic *	A pointer to a structure which is intended to be opaque to users of
202129906Sbmilekic *	the interface.  The value may be null if the wait flag is not set.
203129906Sbmilekic */
204129906Sbmilekicuma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
205129906Sbmilekic		    uma_init zinit, uma_fini zfini, uma_zone_t master);
206129906Sbmilekic
207129906Sbmilekic/*
208187681Sjeff * Add a second master to a secondary zone.  This provides multiple data
209187681Sjeff * backends for objects with the same size.  Both masters must have
210187681Sjeff * compatible allocation flags.  Presently, UMA_ZONE_MALLOC type zones are
211187681Sjeff * the only supported.
212187681Sjeff *
213187681Sjeff * Returns:
214244024Spjd *	Error on failure, 0 on success.
215187681Sjeff */
216187681Sjeffint uma_zsecond_add(uma_zone_t zone, uma_zone_t master);
217187681Sjeff
218187681Sjeff/*
219120223Sjeff * Definitions for uma_zcreate flags
220120223Sjeff *
221120223Sjeff * These flags share space with UMA_ZFLAGs in uma_int.h.  Be careful not to
222148072Ssilby * overlap when adding new features.  0xf0000000 is in use by uma_int.h.
223120223Sjeff */
22492654Sjeff#define UMA_ZONE_PAGEABLE	0x0001	/* Return items not fully backed by
22592654Sjeff					   physical memory XXX Not yet */
22692654Sjeff#define UMA_ZONE_ZINIT		0x0002	/* Initialize with zeros */
227184546Skeramida#define UMA_ZONE_STATIC		0x0004	/* Statically sized zone */
22892654Sjeff#define UMA_ZONE_OFFPAGE	0x0008	/* Force the slab structure allocation
22992654Sjeff					   off of the real memory */
23092654Sjeff#define UMA_ZONE_MALLOC		0x0010	/* For use by malloc(9) only! */
23192654Sjeff#define UMA_ZONE_NOFREE		0x0020	/* Do not free slabs of this type! */
23295758Sjeff#define UMA_ZONE_MTXCLASS	0x0040	/* Create a new lock class */
233103531Sjeff#define	UMA_ZONE_VM		0x0080	/*
234103531Sjeff					 * Used for internal vm datastructures
235103531Sjeff					 * only.
236103531Sjeff					 */
237103531Sjeff#define	UMA_ZONE_HASH		0x0100	/*
238103531Sjeff					 * Use a hash table instead of caching
239103531Sjeff					 * information in the vm_page.
240103531Sjeff					 */
241129906Sbmilekic#define	UMA_ZONE_SECONDARY	0x0200	/* Zone is a Secondary Zone */
242129906Sbmilekic#define	UMA_ZONE_REFCNT		0x0400	/* Allocate refcnts in slabs */
243129906Sbmilekic#define	UMA_ZONE_MAXBUCKET	0x0800	/* Use largest buckets */
244187681Sjeff#define	UMA_ZONE_CACHESPREAD	0x1000	/*
245187681Sjeff					 * Spread memory start locations across
246187681Sjeff					 * all possible cache lines.  May
247187681Sjeff					 * require many virtually contiguous
248187681Sjeff					 * backend pages and can fail early.
249187681Sjeff					 */
250187681Sjeff#define	UMA_ZONE_VTOSLAB	0x2000	/* Zone uses vtoslab for lookup. */
251230623Skmacy#define	UMA_ZONE_NODUMP		0x4000	/*
252230623Skmacy					 * Zone's pages will not be included in
253230623Skmacy					 * mini-dumps.
254230623Skmacy					 */
25592654Sjeff
256187681Sjeff/*
257187681Sjeff * These flags are shared between the keg and zone.  In zones wishing to add
258187681Sjeff * new kegs these flags must be compatible.  Some are determined based on
259187681Sjeff * physical parameters of the request and may not be provided by the consumer.
260187681Sjeff */
261187681Sjeff#define	UMA_ZONE_INHERIT						\
262226313Sglebius    (UMA_ZONE_OFFPAGE | UMA_ZONE_MALLOC | UMA_ZONE_NOFREE |		\
263226313Sglebius    UMA_ZONE_HASH | UMA_ZONE_REFCNT | UMA_ZONE_VTOSLAB)
264187681Sjeff
26592654Sjeff/* Definitions for align */
26692654Sjeff#define UMA_ALIGN_PTR	(sizeof(void *) - 1)	/* Alignment fit for ptr */
26792654Sjeff#define UMA_ALIGN_LONG	(sizeof(long) - 1)	/* "" long */
26892654Sjeff#define UMA_ALIGN_INT	(sizeof(int) - 1)	/* "" int */
26992654Sjeff#define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
27092654Sjeff#define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
271166654Srwatson#define UMA_ALIGN_CACHE	(0 - 1)			/* Cache line size align */
27292654Sjeff
27392654Sjeff/*
27494161Sjeff * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
27592654Sjeff *
27692654Sjeff * Arguments:
27792654Sjeff *	zone  The zone we want to destroy.
27892654Sjeff *
27992654Sjeff */
28094161Sjeffvoid uma_zdestroy(uma_zone_t zone);
28192654Sjeff
28292654Sjeff/*
28392654Sjeff * Allocates an item out of a zone
28492654Sjeff *
28592654Sjeff * Arguments:
28692654Sjeff *	zone  The zone we are allocating from
28792654Sjeff *	arg   This data is passed to the ctor function
28895766Sjeff *	flags See sys/malloc.h for available flags.
28992654Sjeff *
29092654Sjeff * Returns:
291184546Skeramida *	A non-null pointer to an initialized element from the zone is
292184546Skeramida *	guaranteed if the wait flag is M_WAITOK.  Otherwise a null pointer
293184546Skeramida *	may be returned if the zone is empty or the ctor failed.
29492654Sjeff */
29592654Sjeff
29695766Sjeffvoid *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
29792654Sjeff
29892654Sjeff/*
29992654Sjeff * Allocates an item out of a zone without supplying an argument
30092654Sjeff *
30192654Sjeff * This is just a wrapper for uma_zalloc_arg for convenience.
30292654Sjeff *
30392654Sjeff */
30495766Sjeffstatic __inline void *uma_zalloc(uma_zone_t zone, int flags);
30592654Sjeff
30692654Sjeffstatic __inline void *
30795766Sjeffuma_zalloc(uma_zone_t zone, int flags)
30892654Sjeff{
30995766Sjeff	return uma_zalloc_arg(zone, NULL, flags);
31092654Sjeff}
31192654Sjeff
31292654Sjeff/*
31392654Sjeff * Frees an item back into the specified zone.
31492654Sjeff *
31592654Sjeff * Arguments:
31692654Sjeff *	zone  The zone the item was originally allocated out of.
31792654Sjeff *	item  The memory to be freed.
31892654Sjeff *	arg   Argument passed to the destructor
31992654Sjeff *
32092654Sjeff * Returns:
32192654Sjeff *	Nothing.
32292654Sjeff */
32392654Sjeff
32492654Sjeffvoid uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
32592654Sjeff
32692654Sjeff/*
32792654Sjeff * Frees an item back to a zone without supplying an argument
32892654Sjeff *
32992654Sjeff * This is just a wrapper for uma_zfree_arg for convenience.
33092654Sjeff *
33192654Sjeff */
33292654Sjeffstatic __inline void uma_zfree(uma_zone_t zone, void *item);
33392654Sjeff
33492654Sjeffstatic __inline void
33592654Sjeffuma_zfree(uma_zone_t zone, void *item)
33692654Sjeff{
337100326Smarkm	uma_zfree_arg(zone, item, NULL);
33892654Sjeff}
33992654Sjeff
34092654Sjeff/*
34192654Sjeff * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
34292654Sjeff * If you think you need to use it for a normal zone you're probably incorrect.
34392654Sjeff */
34492654Sjeff
34592654Sjeff/*
34692654Sjeff * Backend page supplier routines
34792654Sjeff *
34892654Sjeff * Arguments:
349184546Skeramida *	zone  The zone that is requesting pages.
350184546Skeramida *	size  The number of bytes being requested.
35192654Sjeff *	pflag Flags for these memory pages, see below.
35292654Sjeff *	wait  Indicates our willingness to block.
35392654Sjeff *
35492654Sjeff * Returns:
355184546Skeramida *	A pointer to the allocated memory or NULL on failure.
35692654Sjeff */
35792654Sjeff
35892654Sjefftypedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait);
35992654Sjeff
36092654Sjeff/*
36192654Sjeff * Backend page free routines
36292654Sjeff *
36392654Sjeff * Arguments:
364184546Skeramida *	item  A pointer to the previously allocated pages.
365184546Skeramida *	size  The original size of the allocation.
366184546Skeramida *	pflag The flags for the slab.  See UMA_SLAB_* below.
36792654Sjeff *
36892654Sjeff * Returns:
36992654Sjeff *	None
37092654Sjeff */
37192654Sjefftypedef void (*uma_free)(void *item, int size, u_int8_t pflag);
37292654Sjeff
37392654Sjeff
37492654Sjeff
37592654Sjeff/*
37692654Sjeff * Sets up the uma allocator. (Called by vm_mem_init)
37792654Sjeff *
37892654Sjeff * Arguments:
37992654Sjeff *	bootmem  A pointer to memory used to bootstrap the system.
38092654Sjeff *
38192654Sjeff * Returns:
38292654Sjeff *	Nothing
38392654Sjeff *
38492654Sjeff * Discussion:
38592654Sjeff *	This memory is used for zones which allocate things before the
38692654Sjeff *	backend page supplier can give us pages.  It should be
387151104Sdes *	UMA_SLAB_SIZE * boot_pages bytes. (see uma_int.h)
38892654Sjeff *
38992654Sjeff */
39092654Sjeff
391151104Sdesvoid uma_startup(void *bootmem, int boot_pages);
39292654Sjeff
39392654Sjeff/*
39492654Sjeff * Finishes starting up the allocator.  This should
39592654Sjeff * be called when kva is ready for normal allocs.
39692654Sjeff *
39792654Sjeff * Arguments:
398103531Sjeff *	None
39992654Sjeff *
40092654Sjeff * Returns:
40192654Sjeff *	Nothing
40292654Sjeff *
40392654Sjeff * Discussion:
404103531Sjeff *	uma_startup2 is called by kmeminit() to enable us of uma for malloc.
40592654Sjeff */
406244024Spjd
407103531Sjeffvoid uma_startup2(void);
40892654Sjeff
40992654Sjeff/*
41092654Sjeff * Reclaims unused memory for all zones
41192654Sjeff *
41292654Sjeff * Arguments:
41392654Sjeff *	None
41492654Sjeff * Returns:
41592654Sjeff *	None
41692654Sjeff *
41792654Sjeff * This should only be called by the page out daemon.
41892654Sjeff */
41992654Sjeff
42092654Sjeffvoid uma_reclaim(void);
42192654Sjeff
42292654Sjeff/*
423166654Srwatson * Sets the alignment mask to be used for all zones requesting cache
424166654Srwatson * alignment.  Should be called by MD boot code prior to starting VM/UMA.
425166654Srwatson *
426166654Srwatson * Arguments:
427166654Srwatson *	align The alignment mask
428166654Srwatson *
429166654Srwatson * Returns:
430166654Srwatson *	Nothing
431166654Srwatson */
432166654Srwatsonvoid uma_set_align(int align);
433166654Srwatson
434166654Srwatson/*
43592654Sjeff * Switches the backing object of a zone
43692654Sjeff *
43792654Sjeff * Arguments:
438184546Skeramida *	zone  The zone to update.
439184546Skeramida *	obj   The VM object to use for future allocations.
440184546Skeramida *	size  The size of the object to allocate.
44192654Sjeff *
44292654Sjeff * Returns:
44392654Sjeff *	0  if kva space can not be allocated
44492654Sjeff *	1  if successful
44592654Sjeff *
44692654Sjeff * Discussion:
44792654Sjeff *	A NULL object can be used and uma will allocate one for you.  Setting
44892654Sjeff *	the size will limit the amount of memory allocated to this zone.
44992654Sjeff *
45092654Sjeff */
45192654Sjeffstruct vm_object;
45292654Sjeffint uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size);
45392654Sjeff
45492758Sjeff/*
45592758Sjeff * Sets a high limit on the number of items allowed in a zone
45692758Sjeff *
45792758Sjeff * Arguments:
45892758Sjeff *	zone  The zone to limit
459213911Slstewart *	nitems  The requested upper limit on the number of items allowed
46092758Sjeff *
46192758Sjeff * Returns:
462213911Slstewart *	int  The effective value of nitems after rounding up based on page size
46392758Sjeff */
464213911Slstewartint uma_zone_set_max(uma_zone_t zone, int nitems);
46592654Sjeff
46692654Sjeff/*
467211396Sandre * Obtains the effective limit on the number of items in a zone
468211396Sandre *
469211396Sandre * Arguments:
470211396Sandre *	zone  The zone to obtain the effective limit from
471211396Sandre *
472211396Sandre * Return:
473211396Sandre *	0  No limit
474211396Sandre *	int  The effective limit of the zone
475211396Sandre */
476211396Sandreint uma_zone_get_max(uma_zone_t zone);
477211396Sandre
478211396Sandre/*
479243998Spjd * Sets a warning to be printed when limit is reached
480243998Spjd *
481243998Spjd * Arguments:
482243998Spjd *	zone  The zone we will warn about
483243998Spjd *	warning  Warning content
484243998Spjd *
485243998Spjd * Returns:
486243998Spjd *	Nothing
487243998Spjd */
488243998Spjdvoid uma_zone_set_warning(uma_zone_t zone, const char *warning);
489243998Spjd
490243998Spjd/*
491213910Slstewart * Obtains the approximate current number of items allocated from a zone
492213910Slstewart *
493213910Slstewart * Arguments:
494213910Slstewart *	zone  The zone to obtain the current allocation count from
495213910Slstewart *
496213910Slstewart * Return:
497213910Slstewart *	int  The approximate current number of items allocated from the zone
498213910Slstewart */
499213910Slstewartint uma_zone_get_cur(uma_zone_t zone);
500213910Slstewart
501213910Slstewart/*
502129906Sbmilekic * The following two routines (uma_zone_set_init/fini)
503129906Sbmilekic * are used to set the backend init/fini pair which acts on an
504129906Sbmilekic * object as it becomes allocated and is placed in a slab within
505129906Sbmilekic * the specified zone's backing keg.  These should probably not
506184546Skeramida * be changed once allocations have already begun, but only be set
507129906Sbmilekic * immediately upon zone creation.
508129906Sbmilekic */
509129906Sbmilekicvoid uma_zone_set_init(uma_zone_t zone, uma_init uminit);
510129906Sbmilekicvoid uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
511129906Sbmilekic
512129906Sbmilekic/*
513129906Sbmilekic * The following two routines (uma_zone_set_zinit/zfini) are
514129906Sbmilekic * used to set the zinit/zfini pair which acts on an object as
515129906Sbmilekic * it passes from the backing Keg's slab cache to the
516129906Sbmilekic * specified Zone's bucket cache.  These should probably not
517184546Skeramida * be changed once allocations have already begun, but only be set
518184546Skeramida * immediately upon zone creation.
519129906Sbmilekic */
520129906Sbmilekicvoid uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
521129906Sbmilekicvoid uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
522129906Sbmilekic
523129906Sbmilekic/*
52492654Sjeff * Replaces the standard page_alloc or obj_alloc functions for this zone
52592654Sjeff *
52692654Sjeff * Arguments:
527184546Skeramida *	zone   The zone whose backend allocator is being changed.
52892654Sjeff *	allocf A pointer to the allocation function
52992654Sjeff *
53092654Sjeff * Returns:
53192654Sjeff *	Nothing
53292654Sjeff *
53392654Sjeff * Discussion:
53492654Sjeff *	This could be used to implement pageable allocation, or perhaps
53592654Sjeff *	even DMA allocators if used in conjunction with the OFFPAGE
53692654Sjeff *	zone flag.
53792654Sjeff */
53892654Sjeff
53992654Sjeffvoid uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
54092654Sjeff
54192654Sjeff/*
54292654Sjeff * Used for freeing memory provided by the allocf above
54392654Sjeff *
54492654Sjeff * Arguments:
54592654Sjeff *	zone  The zone that intends to use this free routine.
54692654Sjeff *	freef The page freeing routine.
54792654Sjeff *
54892654Sjeff * Returns:
54992654Sjeff *	Nothing
55092654Sjeff */
55192654Sjeff
55292654Sjeffvoid uma_zone_set_freef(uma_zone_t zone, uma_free freef);
55392654Sjeff
55492654Sjeff/*
555184546Skeramida * These flags are setable in the allocf and visible in the freef.
55692654Sjeff */
55792654Sjeff#define UMA_SLAB_BOOT	0x01		/* Slab alloced from boot pages */
55892654Sjeff#define UMA_SLAB_KMEM	0x02		/* Slab alloced from kmem_map */
559177921Salc#define UMA_SLAB_KERNEL	0x04		/* Slab alloced from kernel_map */
56092654Sjeff#define UMA_SLAB_PRIV	0x08		/* Slab alloced from priv allocator */
56194157Sjeff#define UMA_SLAB_OFFP	0x10		/* Slab is managed separately  */
56292654Sjeff#define UMA_SLAB_MALLOC	0x20		/* Slab is a large malloc slab */
56392654Sjeff/* 0x40 and 0x80 are available */
56492654Sjeff
56592654Sjeff/*
56692654Sjeff * Used to pre-fill a zone with some number of items
56792654Sjeff *
56892654Sjeff * Arguments:
56992654Sjeff *	zone    The zone to fill
57092654Sjeff *	itemcnt The number of items to reserve
57192654Sjeff *
57292654Sjeff * Returns:
57392654Sjeff *	Nothing
57492654Sjeff *
57592654Sjeff * NOTE: This is blocking and should only be done at startup
57692654Sjeff */
57792654Sjeffvoid uma_prealloc(uma_zone_t zone, int itemcnt);
57892654Sjeff
579129906Sbmilekic/*
580129906Sbmilekic * Used to lookup the reference counter allocated for an item
581129906Sbmilekic * from a UMA_ZONE_REFCNT zone.  For UMA_ZONE_REFCNT zones,
582129906Sbmilekic * reference counters are allocated for items and stored in
583129906Sbmilekic * the underlying slab header.
584129906Sbmilekic *
585129906Sbmilekic * Arguments:
586244024Spjd *	zone  The UMA_ZONE_REFCNT zone to which the item belongs.
587129906Sbmilekic *	item  The address of the item for which we want a refcnt.
588129906Sbmilekic *
589129906Sbmilekic * Returns:
590244024Spjd *	A pointer to a u_int32_t reference counter.
591129906Sbmilekic */
592129906Sbmilekicu_int32_t *uma_find_refcnt(uma_zone_t zone, void *item);
59392654Sjeff
594147996Srwatson/*
595165809Sjhb * Used to determine if a fixed-size zone is exhausted.
596165809Sjhb *
597165809Sjhb * Arguments:
598165809Sjhb *	zone    The zone to check
599165809Sjhb *
600165809Sjhb * Returns:
601244024Spjd *	Non-zero if zone is exhausted.
602165809Sjhb */
603165809Sjhbint uma_zone_exhausted(uma_zone_t zone);
604166213Smohansint uma_zone_exhausted_nolock(uma_zone_t zone);
605165809Sjhb
606165809Sjhb/*
607147996Srwatson * Exported statistics structures to be used by user space monitoring tools.
608184546Skeramida * Statistics stream consists of a uma_stream_header, followed by a series of
609184546Skeramida * alternative uma_type_header and uma_type_stat structures.
610147996Srwatson */
611147996Srwatson#define	UMA_STREAM_VERSION	0x00000001
612147996Srwatsonstruct uma_stream_header {
613147996Srwatson	u_int32_t	ush_version;	/* Stream format version. */
614147996Srwatson	u_int32_t	ush_maxcpus;	/* Value of MAXCPU for stream. */
615147996Srwatson	u_int32_t	ush_count;	/* Number of records. */
616147996Srwatson	u_int32_t	_ush_pad;	/* Pad/reserved field. */
617147996Srwatson};
618147996Srwatson
619148371Srwatson#define	UTH_MAX_NAME	32
620148371Srwatson#define	UTH_ZONE_SECONDARY	0x00000001
621147996Srwatsonstruct uma_type_header {
622147996Srwatson	/*
623147996Srwatson	 * Static per-zone data, some extracted from the supporting keg.
624147996Srwatson	 */
625148371Srwatson	char		uth_name[UTH_MAX_NAME];
626147996Srwatson	u_int32_t	uth_align;	/* Keg: alignment. */
627147996Srwatson	u_int32_t	uth_size;	/* Keg: requested size of item. */
628147996Srwatson	u_int32_t	uth_rsize;	/* Keg: real size of item. */
629147996Srwatson	u_int32_t	uth_maxpages;	/* Keg: maximum number of pages. */
630147996Srwatson	u_int32_t	uth_limit;	/* Keg: max items to allocate. */
631147996Srwatson
632147996Srwatson	/*
633147996Srwatson	 * Current dynamic zone/keg-derived statistics.
634147996Srwatson	 */
635147996Srwatson	u_int32_t	uth_pages;	/* Keg: pages allocated. */
636147996Srwatson	u_int32_t	uth_keg_free;	/* Keg: items free. */
637147996Srwatson	u_int32_t	uth_zone_free;	/* Zone: items free. */
638147996Srwatson	u_int32_t	uth_bucketsize;	/* Zone: desired bucket size. */
639148371Srwatson	u_int32_t	uth_zone_flags;	/* Zone: flags. */
640147996Srwatson	u_int64_t	uth_allocs;	/* Zone: number of allocations. */
641147996Srwatson	u_int64_t	uth_frees;	/* Zone: number of frees. */
642148070Srwatson	u_int64_t	uth_fails;	/* Zone: number of alloc failures. */
643209215Ssbruno	u_int64_t	uth_sleeps;	/* Zone: number of alloc sleeps. */
644209215Ssbruno	u_int64_t	_uth_reserved1[2];	/* Reserved. */
645147996Srwatson};
646147996Srwatson
647147996Srwatsonstruct uma_percpu_stat {
648184546Skeramida	u_int64_t	ups_allocs;	/* Cache: number of allocations. */
649147996Srwatson	u_int64_t	ups_frees;	/* Cache: number of frees. */
650147996Srwatson	u_int64_t	ups_cache_free;	/* Cache: free items in cache. */
651147996Srwatson	u_int64_t	_ups_reserved[5];	/* Reserved. */
652147996Srwatson};
653147996Srwatson
65492654Sjeff#endif
655