2 * Copyright (c) 2006-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
37 #include <sys/types.h>
38 #include <sys/queue.h>
39 #include <mach/boolean.h>
40 #include <kern/locks.h>
41 #include <libkern/OSAtomic.h>
52 * Unlike VERIFY(), ASSERT() is evaluated only in DEBUG build.
54 #define VERIFY(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
56 #define ASSERT(EX) VERIFY(EX)
58 #define ASSERT(EX) ((void)0)
61 #define atomic_add_16_ov(a, n) \
62 ((u_int16_t) OSAddAtomic16(n, (volatile SInt16 *)a))
64 #define atomic_add_16(a, n) \
65 ((void) atomic_add_16_ov(a, n))
67 #define atomic_add_32_ov(a, n) \
68 ((u_int32_t) OSAddAtomic(n, (volatile SInt32 *)a))
70 #define atomic_add_32(a, n) \
71 ((void) atomic_add_32_ov(a, n))
73 #define atomic_add_64_ov(a, n) \
74 ((u_int64_t) OSAddAtomic64(n, (volatile SInt64 *)a))
76 #define atomic_add_64(a, n) \
77 ((void) atomic_add_64_ov(a, n))
79 #define atomic_set_64(a, n) do { \
80 while (!OSCompareAndSwap64(*a, n, (volatile UInt64 *)a)) \
85 #define atomic_get_64(n, a) do { \
89 #define atomic_get_64(n, a) do { \
90 (n) = atomic_add_64_ov(a, 0); \
94 #define CPU_CACHE_SIZE 64
97 #define IS_P2ALIGNED(v, a) \
98 ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
99 #endif /* IS_P2ALIGNED */
102 #define P2ROUNDUP(x, align) \
103 (-(-((uintptr_t)(x)) & -(align)))
104 #endif /* P2ROUNDUP */
107 #define P2ROUNDDOWN(x, align) \
108 (((uintptr_t)(x)) & ~((uintptr_t)(align) - 1))
109 #endif /* P2ROUNDDOWN */
111 #define MCACHE_FREE_PATTERN 0xdeadbeefdeadbeefULL
112 #define MCACHE_UNINITIALIZED_PATTERN 0xbaddcafebaddcafeULL
115 * mcache allocation request flags.
117 * MCR_NOSLEEP and MCR_FAILOK are mutually exclusive. The latter is used
118 * by the mbuf allocator to handle the implementation of several caches that
119 * involve multiple layers of mcache. It implies a best effort blocking
120 * allocation request; if the request cannot be satisfied, the caller will
121 * be blocked until further notice, similar to MCR_SLEEP, except that upon
122 * a wake up it will return immediately to the caller regardless of whether
123 * the request can been fulfilled.
125 * MCR_TRYHARD implies a non-blocking allocation request, regardless of
126 * whether MCR_NOSLEEP is set. It informs the allocator that the request
127 * should not cause the calling thread to block, and that it must have
128 * exhausted all possible schemes to fulfill the request, including doing
129 * reclaims and/or purges, before returning to the caller.
131 * Regular mcache clients should only use MCR_SLEEP or MCR_NOSLEEP.
133 #define MCR_SLEEP 0x0000 /* same as M_WAITOK */
134 #define MCR_NOSLEEP 0x0001 /* same as M_NOWAIT */
135 #define MCR_FAILOK 0x0100 /* private, for internal use only */
136 #define MCR_TRYHARD 0x0200 /* private, for internal use only */
137 #define MCR_USR1 0x1000 /* private, for internal use only */
139 #define MCR_NONBLOCKING (MCR_NOSLEEP | MCR_FAILOK | MCR_TRYHARD)
142 * Generic one-way linked list element structure. This is used to handle
143 * mcache_alloc_ext() requests in order to chain the allocated objects
144 * together before returning them to the caller.
146 typedef struct mcache_obj
{
147 struct mcache_obj
*obj_next
;
150 typedef struct mcache_bkt
{
151 void *bkt_next
; /* next bucket in list */
152 void *bkt_obj
[1]; /* one or more objects */
155 typedef struct mcache_bktlist
{
156 mcache_bkt_t
*bl_list
; /* bucket list */
157 u_int32_t bl_total
; /* number of buckets */
158 u_int32_t bl_min
; /* min since last update */
159 u_int32_t bl_reaplimit
; /* max reapable buckets */
160 u_int64_t bl_alloc
; /* allocations from this list */
163 typedef struct mcache_bkttype
{
164 int bt_bktsize
; /* bucket size (number of elements) */
165 size_t bt_minbuf
; /* all smaller buffers qualify */
166 size_t bt_maxbuf
; /* no larger bfufers qualify */
167 struct mcache
*bt_cache
; /* bucket cache */
170 typedef struct mcache_cpu
{
171 decl_lck_mtx_data(, cc_lock
);
172 mcache_bkt_t
*cc_filled
; /* the currently filled bucket */
173 mcache_bkt_t
*cc_pfilled
; /* the previously filled bucket */
174 u_int64_t cc_alloc
; /* allocations from this cpu */
175 u_int64_t cc_free
; /* frees to this cpu */
176 int cc_objs
; /* number of objects in filled bkt */
177 int cc_pobjs
; /* number of objects in previous bkt */
178 int cc_bktsize
; /* number of elements in a full bkt */
179 } __attribute__((aligned(CPU_CACHE_SIZE
), packed
)) mcache_cpu_t
;
181 typedef unsigned int (*mcache_allocfn_t
)(void *, mcache_obj_t
***,
183 typedef void (*mcache_freefn_t
)(void *, mcache_obj_t
*, boolean_t
);
184 typedef void (*mcache_auditfn_t
)(void *, mcache_obj_t
*, boolean_t
);
185 typedef void (*mcache_logfn_t
)(u_int32_t
, mcache_obj_t
*, boolean_t
);
186 typedef void (*mcache_notifyfn_t
)(void *, u_int32_t
);
188 typedef struct mcache
{
192 LIST_ENTRY(mcache
) mc_list
; /* cache linkage */
193 char mc_name
[32]; /* cache name */
194 struct zone
*mc_slab_zone
; /* backend zone allocator */
195 mcache_allocfn_t mc_slab_alloc
; /* slab layer allocate callback */
196 mcache_freefn_t mc_slab_free
; /* slab layer free callback */
197 mcache_auditfn_t mc_slab_audit
; /* slab layer audit callback */
198 mcache_logfn_t mc_slab_log
; /* slab layer log callback */
199 mcache_notifyfn_t mc_slab_notify
; /* slab layer notify callback */
200 void *mc_private
; /* opaque arg to callbacks */
201 size_t mc_bufsize
; /* object size */
202 size_t mc_align
; /* object alignment */
203 u_int32_t mc_flags
; /* cache creation flags */
204 u_int32_t mc_purge_cnt
; /* # of purges requested by slab */
205 u_int32_t mc_enable_cnt
; /* # of reenables due to purges */
206 u_int32_t mc_waiter_cnt
; /* # of slab layer waiters */
207 u_int32_t mc_wretry_cnt
; /* # of wait retries */
208 u_int32_t mc_nwretry_cnt
; /* # of no-wait retry attempts */
209 u_int32_t mc_nwfail_cnt
; /* # of no-wait retries that failed */
210 decl_lck_mtx_data(, mc_sync_lock
); /* protects purges and reenables */
211 lck_attr_t
*mc_sync_lock_attr
;
212 lck_grp_t
*mc_sync_lock_grp
;
213 lck_grp_attr_t
*mc_sync_lock_grp_attr
;
215 * Keep CPU and buckets layers lock statistics separate.
217 lck_attr_t
*mc_cpu_lock_attr
;
218 lck_grp_t
*mc_cpu_lock_grp
;
219 lck_grp_attr_t
*mc_cpu_lock_grp_attr
;
222 * Bucket layer common to all CPUs
224 decl_lck_mtx_data(, mc_bkt_lock
);
225 lck_attr_t
*mc_bkt_lock_attr
;
226 lck_grp_t
*mc_bkt_lock_grp
;
227 lck_grp_attr_t
*mc_bkt_lock_grp_attr
;
228 mcache_bkttype_t
*cache_bkttype
; /* bucket type */
229 mcache_bktlist_t mc_full
; /* full buckets */
230 mcache_bktlist_t mc_empty
; /* empty buckets */
231 size_t mc_chunksize
; /* bufsize + alignment */
232 u_int32_t mc_bkt_contention
; /* lock contention count */
233 u_int32_t mc_bkt_contention_prev
; /* previous snapshot */
236 * Per-CPU layer, aligned at cache line boundary
238 mcache_cpu_t mc_cpu
[1];
241 #define MCACHE_ALIGN 8 /* default guaranteed alignment */
243 /* Valid values for mc_flags */
244 #define MCF_VERIFY 0x00000001 /* enable verification */
245 #define MCF_TRACE 0x00000002 /* enable transaction auditing */
246 #define MCF_NOCPUCACHE 0x00000010 /* disable CPU layer caching */
247 #define MCF_NOLEAKLOG 0x00000100 /* disable leak logging */
249 #define MCF_DEBUG (MCF_VERIFY | MCF_TRACE)
250 #define MCF_FLAGS_MASK (MCF_DEBUG | MCF_NOCPUCACHE | MCF_NOLEAKLOG)
252 /* Valid values for notify callback */
253 #define MCN_RETRYALLOC 0x00000001 /* Allocation should be retried */
255 #define MCACHE_STACK_DEPTH 16
257 typedef struct mcache_audit
{
258 struct mcache_audit
*mca_next
; /* next audit struct */
259 void *mca_addr
; /* address of buffer */
260 mcache_t
*mca_cache
; /* parent cache of the buffer */
261 struct thread
*mca_thread
; /* thread doing transaction */
262 struct thread
*mca_pthread
; /* previous transaction thread */
263 size_t mca_contents_size
; /* size of contents */
264 void *mca_contents
; /* contents at last free */
265 uint16_t mca_depth
; /* pc stack depth */
266 uint16_t mca_pdepth
; /* previous transaction pc stack */
267 void *mca_stack
[MCACHE_STACK_DEPTH
];
268 void *mca_pstack
[MCACHE_STACK_DEPTH
];
269 void *mca_uptr
; /* user-specific pointer */
270 uint32_t mca_uflags
; /* user-specific flags */
273 __private_extern__
int assfail(const char *, const char *, int);
274 __private_extern__
void mcache_init(void);
275 __private_extern__
unsigned int mcache_getflags(void);
276 __private_extern__ mcache_t
*mcache_create(const char *, size_t,
277 size_t, u_int32_t
, int);
278 __private_extern__
void *mcache_alloc(mcache_t
*, int);
279 __private_extern__
void mcache_free(mcache_t
*, void *);
280 __private_extern__ mcache_t
*mcache_create_ext(const char *, size_t,
281 mcache_allocfn_t
, mcache_freefn_t
, mcache_auditfn_t
, mcache_logfn_t
,
282 mcache_notifyfn_t
, void *, u_int32_t
, int);
283 __private_extern__
void mcache_destroy(mcache_t
*);
284 __private_extern__
unsigned int mcache_alloc_ext(mcache_t
*, mcache_obj_t
**,
286 __private_extern__
void mcache_free_ext(mcache_t
*, mcache_obj_t
*);
287 __private_extern__
void mcache_reap(void);
288 __private_extern__ boolean_t
mcache_purge_cache(mcache_t
*);
289 __private_extern__
void mcache_waiter_inc(mcache_t
*);
290 __private_extern__
void mcache_waiter_dec(mcache_t
*);
291 __private_extern__ boolean_t
mcache_bkt_isempty(mcache_t
*);
293 __private_extern__
void mcache_buffer_log(mcache_audit_t
*, void *, mcache_t
*);
294 __private_extern__
void mcache_set_pattern(u_int64_t
, void *, size_t);
295 __private_extern__
void *mcache_verify_pattern(u_int64_t
, void *, size_t);
296 __private_extern__
void *mcache_verify_set_pattern(u_int64_t
, u_int64_t
,
298 __private_extern__
void mcache_audit_free_verify(mcache_audit_t
*,
299 void *, size_t, size_t);
300 __private_extern__
void mcache_audit_free_verify_set(mcache_audit_t
*,
301 void *, size_t, size_t);
302 __private_extern__
char *mcache_dump_mca(mcache_audit_t
*);
303 __private_extern__
void mcache_audit_panic(mcache_audit_t
*, void *, size_t,
306 __private_extern__ mcache_t
*mcache_audit_cache
;
312 #endif /* KERNEL_PRIVATE */
314 #endif /* _SYS_MCACHE_H */