xref: /illumos-gate/usr/src/lib/libumem/common/umem_impl.h (revision 831abf2c3ce98eddc86402eb1c97c92fa48c7349)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
274f364e7cSRobert Mustacchi /*
284f364e7cSRobert Mustacchi  * Copyright (c) 2012 Joyent, Inc.  All rights reserved.
29*831abf2cSDan Kimmel  * Copyright (c) 2015 by Delphix. All rights reserved.
304f364e7cSRobert Mustacchi  */
314f364e7cSRobert Mustacchi 
327c478bd9Sstevel@tonic-gate #ifndef _UMEM_IMPL_H
337c478bd9Sstevel@tonic-gate #define	_UMEM_IMPL_H
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <umem.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/time.h>
397c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
407c478bd9Sstevel@tonic-gate #include <thread.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
437c478bd9Sstevel@tonic-gate extern "C" {
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * umem memory allocator: implementation-private data structures
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Internal flags for umem_cache_create
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate #define	UMC_QCACHE	0x00100000
547c478bd9Sstevel@tonic-gate #define	UMC_INTERNAL	0x80000000
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Cache flags
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate #define	UMF_AUDIT	0x00000001	/* transaction auditing */
607c478bd9Sstevel@tonic-gate #define	UMF_DEADBEEF	0x00000002	/* deadbeef checking */
617c478bd9Sstevel@tonic-gate #define	UMF_REDZONE	0x00000004	/* redzone checking */
627c478bd9Sstevel@tonic-gate #define	UMF_CONTENTS	0x00000008	/* freed-buffer content logging */
637c478bd9Sstevel@tonic-gate #define	UMF_CHECKSIGNAL	0x00000010	/* abort when in signal context */
647c478bd9Sstevel@tonic-gate #define	UMF_NOMAGAZINE	0x00000020	/* disable per-cpu magazines */
657c478bd9Sstevel@tonic-gate #define	UMF_FIREWALL	0x00000040	/* put all bufs before unmapped pages */
667c478bd9Sstevel@tonic-gate #define	UMF_LITE	0x00000100	/* lightweight debugging */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	UMF_HASH	0x00000200	/* cache has hash table */
697c478bd9Sstevel@tonic-gate #define	UMF_RANDOMIZE	0x00000400	/* randomize other umem_flags */
704f364e7cSRobert Mustacchi #define	UMF_PTC		0x00000800	/* cache has per-thread caching */
717c478bd9Sstevel@tonic-gate 
72*831abf2cSDan Kimmel #define	UMF_CHECKNULL	0x00001000	/* heap exhaustion checking */
73*831abf2cSDan Kimmel 
747c478bd9Sstevel@tonic-gate #define	UMF_BUFTAG	(UMF_DEADBEEF | UMF_REDZONE)
757c478bd9Sstevel@tonic-gate #define	UMF_TOUCH	(UMF_BUFTAG | UMF_LITE | UMF_CONTENTS)
767c478bd9Sstevel@tonic-gate #define	UMF_RANDOM	(UMF_TOUCH | UMF_AUDIT | UMF_NOMAGAZINE)
777c478bd9Sstevel@tonic-gate #define	UMF_DEBUG	(UMF_RANDOM | UMF_FIREWALL)
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	UMEM_STACK_DEPTH	umem_stack_depth
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	UMEM_FREE_PATTERN		0xdeadbeefdeadbeefULL
827c478bd9Sstevel@tonic-gate #define	UMEM_UNINITIALIZED_PATTERN	0xbaddcafebaddcafeULL
837c478bd9Sstevel@tonic-gate #define	UMEM_REDZONE_PATTERN		0xfeedfacefeedfaceULL
847c478bd9Sstevel@tonic-gate #define	UMEM_REDZONE_BYTE		0xbb
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	UMEM_FATAL_FLAGS	(UMEM_NOFAIL)
877c478bd9Sstevel@tonic-gate #define	UMEM_SLEEP_FLAGS	(0)
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Redzone size encodings for umem_alloc() / umem_free().  We encode the
917c478bd9Sstevel@tonic-gate  * allocation size, rather than storing it directly, so that umem_free()
927c478bd9Sstevel@tonic-gate  * can distinguish frees of the wrong size from redzone violations.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate #define	UMEM_SIZE_ENCODE(x)	(251 * (x) + 1)
957c478bd9Sstevel@tonic-gate #define	UMEM_SIZE_DECODE(x)	((x) / 251)
967c478bd9Sstevel@tonic-gate #define	UMEM_SIZE_VALID(x)	((x) % 251 == 1)
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * The bufctl (buffer control) structure keeps some minimal information
1007c478bd9Sstevel@tonic-gate  * about each buffer: its address, its slab, and its current linkage,
1017c478bd9Sstevel@tonic-gate  * which is either on the slab's freelist (if the buffer is free), or
1027c478bd9Sstevel@tonic-gate  * on the cache's buf-to-bufctl hash table (if the buffer is allocated).
1037c478bd9Sstevel@tonic-gate  * In the case of non-hashed, or "raw", caches (the common case), only
1047c478bd9Sstevel@tonic-gate  * the freelist linkage is necessary: the buffer address is at a fixed
1057c478bd9Sstevel@tonic-gate  * offset from the bufctl address, and the slab is at the end of the page.
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  * NOTE: bc_next must be the first field; raw buffers have linkage only.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate typedef struct umem_bufctl {
1107c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*bc_next;	/* next bufctl struct */
1117c478bd9Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1127c478bd9Sstevel@tonic-gate 	struct umem_slab	*bc_slab;	/* controlling slab */
1137c478bd9Sstevel@tonic-gate } umem_bufctl_t;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * The UMF_AUDIT version of the bufctl structure.  The beginning of this
1177c478bd9Sstevel@tonic-gate  * structure must be identical to the normal bufctl structure so that
1187c478bd9Sstevel@tonic-gate  * pointers are interchangeable.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_SIZE_DEPTH(frames) \
1227c478bd9Sstevel@tonic-gate 	((size_t)(&((umem_bufctl_audit_t *)0)->bc_stack[frames]))
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * umem_bufctl_audits must be allocated from a UMC_NOHASH cache, so we
1267c478bd9Sstevel@tonic-gate  * require that 2 of them, plus 2 buftags, plus a umem_slab_t, all fit on
1277c478bd9Sstevel@tonic-gate  * a single page.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * For ILP32, this is about 1000 frames.
1307c478bd9Sstevel@tonic-gate  * For LP64, this is about 490 frames.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_ALIGN	32
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_MAX_SIZE					\
1367c478bd9Sstevel@tonic-gate 	(P2ALIGN((PAGESIZE - sizeof (umem_slab_t))/2 -			\
1377c478bd9Sstevel@tonic-gate 	    sizeof (umem_buftag_t), UMEM_BUFCTL_AUDIT_ALIGN))
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	UMEM_MAX_STACK_DEPTH						\
1407c478bd9Sstevel@tonic-gate 	((UMEM_BUFCTL_AUDIT_MAX_SIZE -					\
1417c478bd9Sstevel@tonic-gate 	    UMEM_BUFCTL_AUDIT_SIZE_DEPTH(0)) / sizeof (uintptr_t))
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate typedef struct umem_bufctl_audit {
1447c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*bc_next;	/* next bufctl struct */
1457c478bd9Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1467c478bd9Sstevel@tonic-gate 	struct umem_slab	*bc_slab;	/* controlling slab */
1477c478bd9Sstevel@tonic-gate 	umem_cache_t		*bc_cache;	/* controlling cache */
1487c478bd9Sstevel@tonic-gate 	hrtime_t		bc_timestamp;	/* transaction time */
1497c478bd9Sstevel@tonic-gate 	thread_t		bc_thread;	/* thread doing transaction */
1507c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*bc_lastlog;	/* last log entry */
1517c478bd9Sstevel@tonic-gate 	void			*bc_contents;	/* contents at last free */
1527c478bd9Sstevel@tonic-gate 	int			bc_depth;	/* stack depth */
1537c478bd9Sstevel@tonic-gate 	uintptr_t		bc_stack[1];	/* pc stack */
1547c478bd9Sstevel@tonic-gate } umem_bufctl_audit_t;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #define	UMEM_LOCAL_BUFCTL_AUDIT(bcpp)					\
1577c478bd9Sstevel@tonic-gate 		*(bcpp) = (umem_bufctl_audit_t *)			\
1587c478bd9Sstevel@tonic-gate 		    alloca(UMEM_BUFCTL_AUDIT_SIZE)
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL_AUDIT_SIZE						\
1617c478bd9Sstevel@tonic-gate 	UMEM_BUFCTL_AUDIT_SIZE_DEPTH(UMEM_STACK_DEPTH)
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * A umem_buftag structure is appended to each buffer whenever any of the
1657c478bd9Sstevel@tonic-gate  * UMF_BUFTAG flags (UMF_DEADBEEF, UMF_REDZONE, UMF_VERIFY) are set.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate typedef struct umem_buftag {
1687c478bd9Sstevel@tonic-gate 	uint64_t		bt_redzone;	/* 64-bit redzone pattern */
1697c478bd9Sstevel@tonic-gate 	umem_bufctl_t		*bt_bufctl;	/* bufctl */
1707c478bd9Sstevel@tonic-gate 	intptr_t		bt_bxstat;	/* bufctl ^ (alloc/free) */
1717c478bd9Sstevel@tonic-gate } umem_buftag_t;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	UMEM_BUFTAG(cp, buf)		\
1747c478bd9Sstevel@tonic-gate 	((umem_buftag_t *)((char *)(buf) + (cp)->cache_buftag))
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #define	UMEM_BUFCTL(cp, buf)		\
1777c478bd9Sstevel@tonic-gate 	((umem_bufctl_t *)((char *)(buf) + (cp)->cache_bufctl))
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #define	UMEM_BUF(cp, bcp)		\
1807c478bd9Sstevel@tonic-gate 	((void *)((char *)(bcp) - (cp)->cache_bufctl))
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	UMEM_SLAB(cp, buf)		\
1837c478bd9Sstevel@tonic-gate 	((umem_slab_t *)P2END((uintptr_t)(buf), (cp)->cache_slabsize) - 1)
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #define	UMEM_CPU_CACHE(cp, cpu)		\
1867c478bd9Sstevel@tonic-gate 	(umem_cpu_cache_t *)((char *)cp + cpu->cpu_cache_offset)
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #define	UMEM_MAGAZINE_VALID(cp, mp)	\
1897c478bd9Sstevel@tonic-gate 	(((umem_slab_t *)P2END((uintptr_t)(mp), PAGESIZE) - 1)->slab_cache == \
1907c478bd9Sstevel@tonic-gate 	    (cp)->cache_magtype->mt_cache)
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #define	UMEM_SLAB_MEMBER(sp, buf)	\
1937c478bd9Sstevel@tonic-gate 	((size_t)(buf) - (size_t)(sp)->slab_base < \
1947c478bd9Sstevel@tonic-gate 	    (sp)->slab_cache->cache_slabsize)
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #define	UMEM_BUFTAG_ALLOC	0xa110c8edUL
1977c478bd9Sstevel@tonic-gate #define	UMEM_BUFTAG_FREE	0xf4eef4eeUL
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate typedef struct umem_slab {
2007c478bd9Sstevel@tonic-gate 	struct umem_cache	*slab_cache;	/* controlling cache */
2017c478bd9Sstevel@tonic-gate 	void			*slab_base;	/* base of allocated memory */
2027c478bd9Sstevel@tonic-gate 	struct umem_slab	*slab_next;	/* next slab on freelist */
2037c478bd9Sstevel@tonic-gate 	struct umem_slab	*slab_prev;	/* prev slab on freelist */
2047c478bd9Sstevel@tonic-gate 	struct umem_bufctl	*slab_head;	/* first free buffer */
2057c478bd9Sstevel@tonic-gate 	long			slab_refcnt;	/* outstanding allocations */
2067c478bd9Sstevel@tonic-gate 	long			slab_chunks;	/* chunks (bufs) in this slab */
2077c478bd9Sstevel@tonic-gate } umem_slab_t;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #define	UMEM_HASH_INITIAL	64
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #define	UMEM_HASH(cp, buf)	\
2127c478bd9Sstevel@tonic-gate 	((cp)->cache_hash_table +	\
2137c478bd9Sstevel@tonic-gate 	(((uintptr_t)(buf) >> (cp)->cache_hash_shift) & (cp)->cache_hash_mask))
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate typedef struct umem_magazine {
2167c478bd9Sstevel@tonic-gate 	void	*mag_next;
2177c478bd9Sstevel@tonic-gate 	void	*mag_round[1];		/* one or more rounds */
2187c478bd9Sstevel@tonic-gate } umem_magazine_t;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * The magazine types for fast per-cpu allocation
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate typedef struct umem_magtype {
2247c478bd9Sstevel@tonic-gate 	int		mt_magsize;	/* magazine size (number of rounds) */
2257c478bd9Sstevel@tonic-gate 	int		mt_align;	/* magazine alignment */
2267c478bd9Sstevel@tonic-gate 	size_t		mt_minbuf;	/* all smaller buffers qualify */
2277c478bd9Sstevel@tonic-gate 	size_t		mt_maxbuf;	/* no larger buffers qualify */
2287c478bd9Sstevel@tonic-gate 	umem_cache_t	*mt_cache;	/* magazine cache */
2297c478bd9Sstevel@tonic-gate } umem_magtype_t;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate #define	UMEM_CPU_CACHE_SIZE	64	/* must be power of 2 */
2327c478bd9Sstevel@tonic-gate #define	UMEM_CPU_PAD		(UMEM_CPU_CACHE_SIZE - sizeof (mutex_t) - \
2337c478bd9Sstevel@tonic-gate 	2 * sizeof (uint_t) - 2 * sizeof (void *) - 4 * sizeof (int))
2347c478bd9Sstevel@tonic-gate #define	UMEM_CACHE_SIZE(ncpus)	\
2357c478bd9Sstevel@tonic-gate 	((size_t)(&((umem_cache_t *)0)->cache_cpu[ncpus]))
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate typedef struct umem_cpu_cache {
2387c478bd9Sstevel@tonic-gate 	mutex_t		cc_lock;	/* protects this cpu's local cache */
2397c478bd9Sstevel@tonic-gate 	uint_t		cc_alloc;	/* allocations from this cpu */
2407c478bd9Sstevel@tonic-gate 	uint_t		cc_free;	/* frees to this cpu */
2417c478bd9Sstevel@tonic-gate 	umem_magazine_t	*cc_loaded;	/* the currently loaded magazine */
2427c478bd9Sstevel@tonic-gate 	umem_magazine_t	*cc_ploaded;	/* the previously loaded magazine */
2437c478bd9Sstevel@tonic-gate 	int		cc_rounds;	/* number of objects in loaded mag */
2447c478bd9Sstevel@tonic-gate 	int		cc_prounds;	/* number of objects in previous mag */
2457c478bd9Sstevel@tonic-gate 	int		cc_magsize;	/* number of rounds in a full mag */
2467c478bd9Sstevel@tonic-gate 	int		cc_flags;	/* CPU-local copy of cache_flags */
2477c478bd9Sstevel@tonic-gate #ifndef _LP64
2487c478bd9Sstevel@tonic-gate 	char		cc_pad[UMEM_CPU_PAD]; /* for nice alignment (32-bit) */
2497c478bd9Sstevel@tonic-gate #endif
2507c478bd9Sstevel@tonic-gate } umem_cpu_cache_t;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * The magazine lists used in the depot.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate typedef struct umem_maglist {
2567c478bd9Sstevel@tonic-gate 	umem_magazine_t	*ml_list;	/* magazine list */
2577c478bd9Sstevel@tonic-gate 	long		ml_total;	/* number of magazines */
2587c478bd9Sstevel@tonic-gate 	long		ml_min;		/* min since last update */
2597c478bd9Sstevel@tonic-gate 	long		ml_reaplimit;	/* max reapable magazines */
2607c478bd9Sstevel@tonic-gate 	uint64_t	ml_alloc;	/* allocations from this list */
2617c478bd9Sstevel@tonic-gate } umem_maglist_t;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate #define	UMEM_CACHE_NAMELEN	31
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate struct umem_cache {
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Statistics
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_create;	/* slab creates */
2707c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_destroy;	/* slab destroys */
2717c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_alloc;	/* slab layer allocations */
2727c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_free;	/* slab layer frees */
2737c478bd9Sstevel@tonic-gate 	uint64_t	cache_alloc_fail;	/* total failed allocations */
2747c478bd9Sstevel@tonic-gate 	uint64_t	cache_buftotal;		/* total buffers */
2757c478bd9Sstevel@tonic-gate 	uint64_t	cache_bufmax;		/* max buffers ever */
2767c478bd9Sstevel@tonic-gate 	uint64_t	cache_rescale;		/* # of hash table rescales */
2777c478bd9Sstevel@tonic-gate 	uint64_t	cache_lookup_depth;	/* hash lookup depth */
2787c478bd9Sstevel@tonic-gate 	uint64_t	cache_depot_contention;	/* mutex contention count */
2797c478bd9Sstevel@tonic-gate 	uint64_t	cache_depot_contention_prev; /* previous snapshot */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Cache properties
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	char		cache_name[UMEM_CACHE_NAMELEN + 1];
2857c478bd9Sstevel@tonic-gate 	size_t		cache_bufsize;		/* object size */
2867c478bd9Sstevel@tonic-gate 	size_t		cache_align;		/* object alignment */
2877c478bd9Sstevel@tonic-gate 	umem_constructor_t *cache_constructor;
2887c478bd9Sstevel@tonic-gate 	umem_destructor_t *cache_destructor;
2897c478bd9Sstevel@tonic-gate 	umem_reclaim_t	*cache_reclaim;
2907c478bd9Sstevel@tonic-gate 	void		*cache_private;		/* opaque arg to callbacks */
2917c478bd9Sstevel@tonic-gate 	vmem_t		*cache_arena;		/* vmem source for slabs */
2927c478bd9Sstevel@tonic-gate 	int		cache_cflags;		/* cache creation flags */
2937c478bd9Sstevel@tonic-gate 	int		cache_flags;		/* various cache state info */
2947c478bd9Sstevel@tonic-gate 	int		cache_uflags;		/* UMU_* flags */
2957c478bd9Sstevel@tonic-gate 	uint32_t	cache_mtbf;		/* induced alloc failure rate */
2967c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_next;		/* forward cache linkage */
2977c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_prev;		/* backward cache linkage */
2987c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_unext;		/* next in update list */
2997c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_uprev;		/* prev in update list */
3007c478bd9Sstevel@tonic-gate 	uint32_t	cache_cpu_mask;		/* mask for cpu offset */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * Slab layer
3047c478bd9Sstevel@tonic-gate 	 */
3057c478bd9Sstevel@tonic-gate 	mutex_t		cache_lock;		/* protects slab layer */
3067c478bd9Sstevel@tonic-gate 	size_t		cache_chunksize;	/* buf + alignment [+ debug] */
3077c478bd9Sstevel@tonic-gate 	size_t		cache_slabsize;		/* size of a slab */
3087c478bd9Sstevel@tonic-gate 	size_t		cache_bufctl;		/* buf-to-bufctl distance */
3097c478bd9Sstevel@tonic-gate 	size_t		cache_buftag;		/* buf-to-buftag distance */
3107c478bd9Sstevel@tonic-gate 	size_t		cache_verify;		/* bytes to verify */
3117c478bd9Sstevel@tonic-gate 	size_t		cache_contents;		/* bytes of saved content */
3127c478bd9Sstevel@tonic-gate 	size_t		cache_color;		/* next slab color */
3137c478bd9Sstevel@tonic-gate 	size_t		cache_mincolor;		/* maximum slab color */
3147c478bd9Sstevel@tonic-gate 	size_t		cache_maxcolor;		/* maximum slab color */
3157c478bd9Sstevel@tonic-gate 	size_t		cache_hash_shift;	/* get to interesting bits */
3167c478bd9Sstevel@tonic-gate 	size_t		cache_hash_mask;	/* hash table mask */
3177c478bd9Sstevel@tonic-gate 	umem_slab_t	*cache_freelist;	/* slab free list */
3187c478bd9Sstevel@tonic-gate 	umem_slab_t	cache_nullslab;		/* end of freelist marker */
3197c478bd9Sstevel@tonic-gate 	umem_cache_t	*cache_bufctl_cache;	/* source of bufctls */
3207c478bd9Sstevel@tonic-gate 	umem_bufctl_t	**cache_hash_table;	/* hash table base */
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * Depot layer
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	mutex_t		cache_depot_lock;	/* protects depot */
3257c478bd9Sstevel@tonic-gate 	umem_magtype_t	*cache_magtype;		/* magazine type */
3267c478bd9Sstevel@tonic-gate 	umem_maglist_t	cache_full;		/* full magazines */
3277c478bd9Sstevel@tonic-gate 	umem_maglist_t	cache_empty;		/* empty magazines */
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/*
3307c478bd9Sstevel@tonic-gate 	 * Per-CPU layer
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	umem_cpu_cache_t cache_cpu[1];		/* cache_cpu_mask + 1 entries */
3337c478bd9Sstevel@tonic-gate };
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate typedef struct umem_cpu_log_header {
3367c478bd9Sstevel@tonic-gate 	mutex_t		clh_lock;
3377c478bd9Sstevel@tonic-gate 	char		*clh_current;
3387c478bd9Sstevel@tonic-gate 	size_t		clh_avail;
3397c478bd9Sstevel@tonic-gate 	int		clh_chunk;
3407c478bd9Sstevel@tonic-gate 	int		clh_hits;
3417c478bd9Sstevel@tonic-gate 	char		clh_pad[64 - sizeof (mutex_t) - sizeof (char *) -
3427c478bd9Sstevel@tonic-gate 				sizeof (size_t) - 2 * sizeof (int)];
3437c478bd9Sstevel@tonic-gate } umem_cpu_log_header_t;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate typedef struct umem_log_header {
3467c478bd9Sstevel@tonic-gate 	mutex_t		lh_lock;
3477c478bd9Sstevel@tonic-gate 	char		*lh_base;
3487c478bd9Sstevel@tonic-gate 	int		*lh_free;
3497c478bd9Sstevel@tonic-gate 	size_t		lh_chunksize;
3507c478bd9Sstevel@tonic-gate 	int		lh_nchunks;
3517c478bd9Sstevel@tonic-gate 	int		lh_head;
3527c478bd9Sstevel@tonic-gate 	int		lh_tail;
3537c478bd9Sstevel@tonic-gate 	int		lh_hits;
3547c478bd9Sstevel@tonic-gate 	umem_cpu_log_header_t lh_cpu[1];	/* actually umem_max_ncpus */
3557c478bd9Sstevel@tonic-gate } umem_log_header_t;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate typedef struct umem_cpu {
3587c478bd9Sstevel@tonic-gate 	uint32_t cpu_cache_offset;
3597c478bd9Sstevel@tonic-gate 	uint32_t cpu_number;
3607c478bd9Sstevel@tonic-gate } umem_cpu_t;
3617c478bd9Sstevel@tonic-gate 
36238849194SRobert Mustacchi #define	UMEM_MAXBUF	131072
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate #define	UMEM_ALIGN		8	/* min guaranteed alignment */
3657c478bd9Sstevel@tonic-gate #define	UMEM_ALIGN_SHIFT	3	/* log2(UMEM_ALIGN) */
3667c478bd9Sstevel@tonic-gate #define	UMEM_VOID_FRACTION	8	/* never waste more than 1/8 of slab */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * For 64 bits, buffers >= 16 bytes must be 16-byte aligned
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate #ifdef _LP64
3727c478bd9Sstevel@tonic-gate #define	UMEM_SECOND_ALIGN 16
3737c478bd9Sstevel@tonic-gate #else
3747c478bd9Sstevel@tonic-gate #define	UMEM_SECOND_ALIGN UMEM_ALIGN
3757c478bd9Sstevel@tonic-gate #endif
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #define	MALLOC_MAGIC			0x3a10c000 /* 8-byte tag */
3787c478bd9Sstevel@tonic-gate #define	MEMALIGN_MAGIC			0x3e3a1000
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate #ifdef _LP64
3817c478bd9Sstevel@tonic-gate #define	MALLOC_SECOND_MAGIC		0x16ba7000 /* 8-byte tag, 16-aligned */
3827c478bd9Sstevel@tonic-gate #define	MALLOC_OVERSIZE_MAGIC		0x06e47000 /* 16-byte tag, _LP64 */
3837c478bd9Sstevel@tonic-gate #endif
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate #define	UMEM_MALLOC_ENCODE(type, sz)	(uint32_t)((type) - (sz))
3867c478bd9Sstevel@tonic-gate #define	UMEM_MALLOC_DECODE(stat, sz)	(uint32_t)((stat) + (sz))
3877c478bd9Sstevel@tonic-gate #define	UMEM_FREE_PATTERN_32		(uint32_t)(UMEM_FREE_PATTERN)
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate #define	UMU_MAGAZINE_RESIZE	0x00000001
3907c478bd9Sstevel@tonic-gate #define	UMU_HASH_RESCALE	0x00000002
3917c478bd9Sstevel@tonic-gate #define	UMU_REAP		0x00000004
3927c478bd9Sstevel@tonic-gate #define	UMU_NOTIFY		0x08000000
3937c478bd9Sstevel@tonic-gate #define	UMU_ACTIVE		0x80000000
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate #define	UMEM_READY_INIT_FAILED		-1
3967c478bd9Sstevel@tonic-gate #define	UMEM_READY_STARTUP		1
3977c478bd9Sstevel@tonic-gate #define	UMEM_READY_INITING		2
3987c478bd9Sstevel@tonic-gate #define	UMEM_READY			3
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate #ifdef UMEM_STANDALONE
4017c478bd9Sstevel@tonic-gate extern void umem_startup(caddr_t, size_t, size_t, caddr_t, caddr_t);
4027c478bd9Sstevel@tonic-gate extern int umem_add(caddr_t, size_t);
4037c478bd9Sstevel@tonic-gate #endif
4047c478bd9Sstevel@tonic-gate 
4054f364e7cSRobert Mustacchi /*
4064f364e7cSRobert Mustacchi  * Private interface with libc for tcumem.
4074f364e7cSRobert Mustacchi  */
4084f364e7cSRobert Mustacchi extern uintptr_t _tmem_get_base(void);
4094f364e7cSRobert Mustacchi extern int _tmem_get_nentries(void);
4104f364e7cSRobert Mustacchi extern void _tmem_set_cleanup(void(*)(void *, int));
4114f364e7cSRobert Mustacchi 
4127c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate #endif
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate #endif	/* _UMEM_IMPL_H */
417