00001 /* 00002 * CDDL HEADER START 00003 * 00004 * The contents of this file are subject to the terms of the 00005 * Common Development and Distribution License, Version 1.0 only 00006 * (the "License"). You may not use this file except in compliance 00007 * with the License. 00008 * 00009 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 00010 * or http://www.opensolaris.org/os/licensing. 00011 * See the License for the specific language governing permissions 00012 * and limitations under the License. 00013 * 00014 * When distributing Covered Code, include this CDDL HEADER in each 00015 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 00016 * If applicable, add the following below this CDDL HEADER, with the 00017 * fields enclosed by brackets "[]" replaced with your own identifying 00018 * information: Portions Copyright [yyyy] [name of copyright owner] 00019 * 00020 * CDDL HEADER END 00021 */ 00022 /* 00023 * Copyright 1999-2002 Sun Microsystems, Inc. All rights reserved. 00024 * Use is subject to license terms. 00025 */ 00026 /* 00027 * Portions Copyright 2006 OmniTI, Inc. 00028 */ 00029 00030 #ifndef _SYS_VMEM_IMPL_USER_H 00031 #define _SYS_VMEM_IMPL_USER_H 00032 00033 /* #pragma ident "@(#)vmem_impl_user.h 1.2 05/06/08 SMI" */ 00034 00035 #if HAVE_SYS_KSTAT 00036 #include <sys/kstat.h> 00037 #endif 00038 #ifndef _WIN32 00039 #include <sys/time.h> 00040 #endif 00041 #include <sys/vmem.h> 00042 #if HAVE_THREAD_H 00043 #include <thread.h> 00044 #else 00045 # include "sol_compat.h" 00046 #endif 00047 #if HAVE_SYNC_H 00048 #include <synch.h> 00049 #endif 00050 00051 #ifdef __cplusplus 00052 extern "C" { 00053 #endif 00054 00055 typedef struct vmem_seg vmem_seg_t; 00056 00057 #define VMEM_STACK_DEPTH 20 00058 00059 struct vmem_seg { 00060 /* 00061 * The first four fields must match vmem_freelist_t exactly. 00062 */ 00063 uintptr_t vs_start; /* start of segment (inclusive) */ 00064 uintptr_t vs_end; /* end of segment (exclusive) */ 00065 vmem_seg_t *vs_knext; /* next of kin (alloc, free, span) */ 00066 vmem_seg_t *vs_kprev; /* prev of kin */ 00067 00068 vmem_seg_t *vs_anext; /* next in arena */ 00069 vmem_seg_t *vs_aprev; /* prev in arena */ 00070 uint8_t vs_type; /* alloc, free, span */ 00071 uint8_t vs_import; /* non-zero if segment was imported */ 00072 uint8_t vs_depth; /* stack depth if UMF_AUDIT active */ 00073 /* 00074 * The following fields are present only when UMF_AUDIT is set. 00075 */ 00076 thread_t vs_thread; 00077 hrtime_t vs_timestamp; 00078 uintptr_t vs_stack[VMEM_STACK_DEPTH]; 00079 }; 00080 00081 typedef struct vmem_freelist { 00082 uintptr_t vs_start; /* always zero */ 00083 uintptr_t vs_end; /* segment size */ 00084 vmem_seg_t *vs_knext; /* next of kin */ 00085 vmem_seg_t *vs_kprev; /* prev of kin */ 00086 } vmem_freelist_t; 00087 00088 #define VS_SIZE(vsp) ((vsp)->vs_end - (vsp)->vs_start) 00089 00090 /* 00091 * Segment hashing 00092 */ 00093 #define VMEM_HASH_INDEX(a, s, q, m) \ 00094 ((((a) + ((a) >> (s)) + ((a) >> ((s) << 1))) >> (q)) & (m)) 00095 00096 #define VMEM_HASH(vmp, addr) \ 00097 (&(vmp)->vm_hash_table[VMEM_HASH_INDEX(addr, \ 00098 (vmp)->vm_hash_shift, (vmp)->vm_qshift, (vmp)->vm_hash_mask)]) 00099 00100 #define VMEM_NAMELEN 30 00101 #define VMEM_HASH_INITIAL 16 00102 #define VMEM_NQCACHE_MAX 16 00103 #define VMEM_FREELISTS (sizeof (void *) * 8) 00104 00105 typedef struct vmem_kstat { 00106 uint64_t vk_mem_inuse; /* memory in use */ 00107 uint64_t vk_mem_import; /* memory imported */ 00108 uint64_t vk_mem_total; /* total memory in arena */ 00109 uint32_t vk_source_id; /* vmem id of vmem source */ 00110 uint64_t vk_alloc; /* number of allocations */ 00111 uint64_t vk_free; /* number of frees */ 00112 uint64_t vk_wait; /* number of allocations that waited */ 00113 uint64_t vk_fail; /* number of allocations that failed */ 00114 uint64_t vk_lookup; /* hash lookup count */ 00115 uint64_t vk_search; /* freelist search count */ 00116 uint64_t vk_populate_wait; /* populates that waited */ 00117 uint64_t vk_populate_fail; /* populates that failed */ 00118 uint64_t vk_contains; /* vmem_contains() calls */ 00119 uint64_t vk_contains_search; /* vmem_contains() search cnt */ 00120 } vmem_kstat_t; 00121 00122 struct vmem { 00123 char vm_name[VMEM_NAMELEN]; /* arena name */ 00124 cond_t vm_cv; /* cv for blocking allocations */ 00125 mutex_t vm_lock; /* arena lock */ 00126 uint32_t vm_id; /* vmem id */ 00127 uint32_t vm_mtbf; /* induced alloc failure rate */ 00128 int vm_cflags; /* arena creation flags */ 00129 int vm_qshift; /* log2(vm_quantum) */ 00130 size_t vm_quantum; /* vmem quantum */ 00131 size_t vm_qcache_max; /* maximum size to front by umem */ 00132 vmem_alloc_t *vm_source_alloc; 00133 vmem_free_t *vm_source_free; 00134 vmem_t *vm_source; /* vmem source for imported memory */ 00135 vmem_t *vm_next; /* next in vmem_list */ 00136 ssize_t vm_nsegfree; /* number of free vmem_seg_t's */ 00137 vmem_seg_t *vm_segfree; /* free vmem_seg_t list */ 00138 vmem_seg_t **vm_hash_table; /* allocated-segment hash table */ 00139 size_t vm_hash_mask; /* hash_size - 1 */ 00140 size_t vm_hash_shift; /* log2(vm_hash_mask + 1) */ 00141 ulong_t vm_freemap; /* bitmap of non-empty freelists */ 00142 vmem_seg_t vm_seg0; /* anchor segment */ 00143 vmem_seg_t vm_rotor; /* rotor for VM_NEXTFIT allocations */ 00144 vmem_seg_t *vm_hash0[VMEM_HASH_INITIAL]; /* initial hash table */ 00145 void *vm_qcache[VMEM_NQCACHE_MAX]; /* quantum caches */ 00146 vmem_freelist_t vm_freelist[VMEM_FREELISTS + 1]; /* power-of-2 flists */ 00147 vmem_kstat_t vm_kstat; /* kstat data */ 00148 }; 00149 00150 /* 00151 * We cannot use a mutex_t and MUTEX_HELD, since that will not work 00152 * when libthread is not linked. 00153 */ 00154 typedef struct vmem_populate_lock { 00155 mutex_t vmpl_mutex; 00156 thread_t vmpl_thr; 00157 } vmem_populate_lock_t; 00158 00159 #define VM_UMFLAGS VM_KMFLAGS 00160 00161 #ifdef __cplusplus 00162 } 00163 #endif 00164 00165 #endif /* _SYS_VMEM_IMPL_USER_H */