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