xref: /illumos-gate/usr/src/uts/common/sys/vmem_impl.h (revision 2d6eb4a5)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999-2001, 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #ifndef _SYS_VMEM_IMPL_H
28*7c478bd9Sstevel@tonic-gate #define	_SYS_VMEM_IMPL_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/condvar.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
38*7c478bd9Sstevel@tonic-gate extern "C" {
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate typedef struct vmem_seg vmem_seg_t;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	VMEM_STACK_DEPTH	20
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate struct vmem_seg {
46*7c478bd9Sstevel@tonic-gate 	/*
47*7c478bd9Sstevel@tonic-gate 	 * The first four fields must match vmem_freelist_t exactly.
48*7c478bd9Sstevel@tonic-gate 	 */
49*7c478bd9Sstevel@tonic-gate 	uintptr_t	vs_start;	/* start of segment (inclusive) */
50*7c478bd9Sstevel@tonic-gate 	uintptr_t	vs_end;		/* end of segment (exclusive) */
51*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vs_knext;	/* next of kin (alloc, free, span) */
52*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vs_kprev;	/* prev of kin */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vs_anext;	/* next in arena */
55*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vs_aprev;	/* prev in arena */
56*7c478bd9Sstevel@tonic-gate 	uint8_t		vs_type;	/* alloc, free, span */
57*7c478bd9Sstevel@tonic-gate 	uint8_t		vs_import;	/* non-zero if segment was imported */
58*7c478bd9Sstevel@tonic-gate 	uint8_t		vs_depth;	/* stack depth if KMF_AUDIT active */
59*7c478bd9Sstevel@tonic-gate 	/*
60*7c478bd9Sstevel@tonic-gate 	 * The following fields are present only when KMF_AUDIT is set.
61*7c478bd9Sstevel@tonic-gate 	 */
62*7c478bd9Sstevel@tonic-gate 	kthread_t	*vs_thread;
63*7c478bd9Sstevel@tonic-gate 	hrtime_t	vs_timestamp;
64*7c478bd9Sstevel@tonic-gate 	pc_t		vs_stack[VMEM_STACK_DEPTH];
65*7c478bd9Sstevel@tonic-gate };
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate typedef struct vmem_freelist {
68*7c478bd9Sstevel@tonic-gate 	uintptr_t	vs_start;	/* always zero */
69*7c478bd9Sstevel@tonic-gate 	uintptr_t	vs_end;		/* segment size */
70*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vs_knext;	/* next of kin */
71*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vs_kprev;	/* prev of kin */
72*7c478bd9Sstevel@tonic-gate } vmem_freelist_t;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	VS_SIZE(vsp)	((vsp)->vs_end - (vsp)->vs_start)
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * Segment hashing
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate #define	VMEM_HASH_INDEX(a, s, q, m)					\
80*7c478bd9Sstevel@tonic-gate 	((((a) + ((a) >> (s)) + ((a) >> ((s) << 1))) >> (q)) & (m))
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate #define	VMEM_HASH(vmp, addr)						\
83*7c478bd9Sstevel@tonic-gate 	(&(vmp)->vm_hash_table[VMEM_HASH_INDEX(addr,			\
84*7c478bd9Sstevel@tonic-gate 	(vmp)->vm_hash_shift, (vmp)->vm_qshift, (vmp)->vm_hash_mask)])
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #define	VMEM_QCACHE_SLABSIZE(max) \
87*7c478bd9Sstevel@tonic-gate 	MAX(1 << highbit(3 * (max)), 64)
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #define	VMEM_NAMELEN		30
90*7c478bd9Sstevel@tonic-gate #define	VMEM_HASH_INITIAL	16
91*7c478bd9Sstevel@tonic-gate #define	VMEM_NQCACHE_MAX	16
92*7c478bd9Sstevel@tonic-gate #define	VMEM_FREELISTS		(sizeof (void *) * 8)
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate typedef struct vmem_kstat {
95*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_mem_inuse;	/* memory in use */
96*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_mem_import;	/* memory imported */
97*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_mem_total;	/* total memory in arena */
98*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_source_id;	/* vmem id of vmem source */
99*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_alloc;	/* number of allocations */
100*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_free;	/* number of frees */
101*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_wait;	/* number of allocations that waited */
102*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_fail;	/* number of allocations that failed */
103*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_lookup;	/* hash lookup count */
104*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_search;	/* freelist search count */
105*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_populate_wait;	/* populates that waited */
106*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_populate_fail;	/* populates that failed */
107*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_contains;		/* vmem_contains() calls */
108*7c478bd9Sstevel@tonic-gate 	kstat_named_t	vk_contains_search;	/* vmem_contains() search cnt */
109*7c478bd9Sstevel@tonic-gate } vmem_kstat_t;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate struct vmem {
112*7c478bd9Sstevel@tonic-gate 	char		vm_name[VMEM_NAMELEN];	/* arena name */
113*7c478bd9Sstevel@tonic-gate 	kcondvar_t	vm_cv;		/* cv for blocking allocations */
114*7c478bd9Sstevel@tonic-gate 	kmutex_t	vm_lock;	/* arena lock */
115*7c478bd9Sstevel@tonic-gate 	uint32_t	vm_id;		/* vmem id */
116*7c478bd9Sstevel@tonic-gate 	uint32_t	vm_mtbf;	/* induced alloc failure rate */
117*7c478bd9Sstevel@tonic-gate 	int		vm_cflags;	/* arena creation flags */
118*7c478bd9Sstevel@tonic-gate 	int		vm_qshift;	/* log2(vm_quantum) */
119*7c478bd9Sstevel@tonic-gate 	size_t		vm_quantum;	/* vmem quantum */
120*7c478bd9Sstevel@tonic-gate 	size_t		vm_qcache_max;	/* maximum size to front by kmem */
121*7c478bd9Sstevel@tonic-gate 	size_t		vm_min_import;	/* smallest amount to import */
122*7c478bd9Sstevel@tonic-gate 	void		*(*vm_source_alloc)(vmem_t *, size_t, int);
123*7c478bd9Sstevel@tonic-gate 	void		(*vm_source_free)(vmem_t *, void *, size_t);
124*7c478bd9Sstevel@tonic-gate 	vmem_t		*vm_source;	/* vmem source for imported memory */
125*7c478bd9Sstevel@tonic-gate 	vmem_t		*vm_next;	/* next in vmem_list */
126*7c478bd9Sstevel@tonic-gate 	kstat_t		*vm_ksp;	/* kstat */
127*7c478bd9Sstevel@tonic-gate 	ssize_t		vm_nsegfree;	/* number of free vmem_seg_t's */
128*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vm_segfree;	/* free vmem_seg_t list */
129*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	**vm_hash_table; /* allocated-segment hash table */
130*7c478bd9Sstevel@tonic-gate 	size_t		vm_hash_mask;	/* hash_size - 1 */
131*7c478bd9Sstevel@tonic-gate 	size_t		vm_hash_shift;	/* log2(vm_hash_mask + 1) */
132*7c478bd9Sstevel@tonic-gate 	ulong_t		vm_freemap;	/* bitmap of non-empty freelists */
133*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	vm_seg0;	/* anchor segment */
134*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	vm_rotor;	/* rotor for VM_NEXTFIT allocations */
135*7c478bd9Sstevel@tonic-gate 	vmem_seg_t	*vm_hash0[VMEM_HASH_INITIAL];	/* initial hash table */
136*7c478bd9Sstevel@tonic-gate 	void		*vm_qcache[VMEM_NQCACHE_MAX];	/* quantum caches */
137*7c478bd9Sstevel@tonic-gate 	vmem_freelist_t	vm_freelist[VMEM_FREELISTS + 1]; /* power-of-2 flists */
138*7c478bd9Sstevel@tonic-gate 	vmem_kstat_t	vm_kstat;	/* kstat data */
139*7c478bd9Sstevel@tonic-gate };
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate #endif
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate #endif	/* _SYS_VMEM_IMPL_H */
146