xref: /illumos-gate/usr/src/lib/libumem/common/sys/vmem_impl_user.h (revision 86d949f9497332fe19be6b5d711d265eb957439f)
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-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_VMEM_IMPL_USER_H
28 #define	_SYS_VMEM_IMPL_USER_H
29 
30 #include <sys/kstat.h>
31 #include <sys/time.h>
32 #include <sys/vmem.h>
33 #include <thread.h>
34 #include <synch.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 typedef struct vmem_seg vmem_seg_t;
41 
42 #define	VMEM_STACK_DEPTH	20
43 
44 struct vmem_seg {
45 	/*
46 	 * The first four fields must match vmem_freelist_t exactly.
47 	 */
48 	uintptr_t	vs_start;	/* start of segment (inclusive) */
49 	uintptr_t	vs_end;		/* end of segment (exclusive) */
50 	vmem_seg_t	*vs_knext;	/* next of kin (alloc, free, span) */
51 	vmem_seg_t	*vs_kprev;	/* prev of kin */
52 
53 	vmem_seg_t	*vs_anext;	/* next in arena */
54 	vmem_seg_t	*vs_aprev;	/* prev in arena */
55 	uint8_t		vs_type;	/* alloc, free, span */
56 	uint8_t		vs_import;	/* non-zero if segment was imported */
57 	uint8_t		vs_depth;	/* stack depth if UMF_AUDIT active */
58 	/*
59 	 * The following fields are present only when UMF_AUDIT is set.
60 	 */
61 	thread_t	vs_thread;
62 	hrtime_t	vs_timestamp;
63 	uintptr_t	vs_stack[VMEM_STACK_DEPTH];
64 };
65 
66 typedef struct vmem_freelist {
67 	uintptr_t	vs_start;	/* always zero */
68 	uintptr_t	vs_end;		/* segment size */
69 	vmem_seg_t	*vs_knext;	/* next of kin */
70 	vmem_seg_t	*vs_kprev;	/* prev of kin */
71 } vmem_freelist_t;
72 
73 #define	VS_SIZE(vsp)	((vsp)->vs_end - (vsp)->vs_start)
74 
75 /*
76  * Segment hashing
77  */
78 #define	VMEM_HASH_INDEX(a, s, q, m)					\
79 	((((a) + ((a) >> (s)) + ((a) >> ((s) << 1))) >> (q)) & (m))
80 
81 #define	VMEM_HASH(vmp, addr)						\
82 	(&(vmp)->vm_hash_table[VMEM_HASH_INDEX(addr,			\
83 	(vmp)->vm_hash_shift, (vmp)->vm_qshift, (vmp)->vm_hash_mask)])
84 
85 #define	VMEM_NAMELEN		30
86 #define	VMEM_HASH_INITIAL	16
87 #define	VMEM_NQCACHE_MAX	16
88 #define	VMEM_FREELISTS		(sizeof (void *) * 8)
89 
90 typedef struct vmem_kstat {
91 	uint64_t	vk_mem_inuse;	/* memory in use */
92 	uint64_t	vk_mem_import;	/* memory imported */
93 	uint64_t	vk_mem_total;	/* total memory in arena */
94 	uint32_t	vk_source_id;	/* vmem id of vmem source */
95 	uint64_t	vk_alloc;	/* number of allocations */
96 	uint64_t	vk_free;	/* number of frees */
97 	uint64_t	vk_wait;	/* number of allocations that waited */
98 	uint64_t	vk_fail;	/* number of allocations that failed */
99 	uint64_t	vk_lookup;	/* hash lookup count */
100 	uint64_t	vk_search;	/* freelist search count */
101 	uint64_t	vk_populate_wait;	/* populates that waited */
102 	uint64_t	vk_populate_fail;	/* populates that failed */
103 	uint64_t	vk_contains;		/* vmem_contains() calls */
104 	uint64_t	vk_contains_search;	/* vmem_contains() search cnt */
105 } vmem_kstat_t;
106 
107 struct vmem {
108 	char		vm_name[VMEM_NAMELEN];	/* arena name */
109 	cond_t		vm_cv;		/* cv for blocking allocations */
110 	mutex_t		vm_lock;	/* arena lock */
111 	uint32_t	vm_id;		/* vmem id */
112 	uint32_t	vm_mtbf;	/* induced alloc failure rate */
113 	int		vm_cflags;	/* arena creation flags */
114 	int		vm_qshift;	/* log2(vm_quantum) */
115 	size_t		vm_quantum;	/* vmem quantum */
116 	size_t		vm_qcache_max;	/* maximum size to front by umem */
117 	vmem_alloc_t	*vm_source_alloc;
118 	vmem_free_t	*vm_source_free;
119 	vmem_t		*vm_source;	/* vmem source for imported memory */
120 	vmem_t		*vm_next;	/* next in vmem_list */
121 	ssize_t		vm_nsegfree;	/* number of free vmem_seg_t's */
122 	vmem_seg_t	*vm_segfree;	/* free vmem_seg_t list */
123 	vmem_seg_t	**vm_hash_table; /* allocated-segment hash table */
124 	size_t		vm_hash_mask;	/* hash_size - 1 */
125 	size_t		vm_hash_shift;	/* log2(vm_hash_mask + 1) */
126 	ulong_t		vm_freemap;	/* bitmap of non-empty freelists */
127 	vmem_seg_t	vm_seg0;	/* anchor segment */
128 	vmem_seg_t	vm_rotor;	/* rotor for VM_NEXTFIT allocations */
129 	vmem_seg_t	*vm_hash0[VMEM_HASH_INITIAL];	/* initial hash table */
130 	void		*vm_qcache[VMEM_NQCACHE_MAX];	/* quantum caches */
131 	vmem_freelist_t	vm_freelist[VMEM_FREELISTS + 1]; /* power-of-2 flists */
132 	vmem_kstat_t	vm_kstat;	/* kstat data */
133 };
134 
135 /*
136  * We cannot use a mutex_t and MUTEX_HELD, since that will not work
137  * when libthread is not linked.
138  */
139 typedef struct vmem_populate_lock {
140 	mutex_t		vmpl_mutex;
141 	thread_t	vmpl_thr;
142 } vmem_populate_lock_t;
143 
144 #define	VM_UMFLAGS	VM_KMFLAGS
145 
146 #ifdef	__cplusplus
147 }
148 #endif
149 
150 #endif	/* _SYS_VMEM_IMPL_USER_H */
151