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