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