]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
39037602 | 2 | * Copyright (c) 2006-2015 Apple Inc. All rights reserved. |
2d21ac55 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
39236c6e | 5 | * |
2d21ac55 A |
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. | |
39236c6e | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39236c6e | 17 | * |
2d21ac55 A |
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. | |
39236c6e | 25 | * |
2d21ac55 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | #ifndef _SYS_MCACHE_H | |
29 | #define _SYS_MCACHE_H | |
30 | ||
31 | #ifdef KERNEL_PRIVATE | |
32 | ||
33 | #ifdef __cplusplus | |
34 | extern "C" { | |
35 | #endif | |
36 | ||
37 | #include <sys/types.h> | |
38 | #include <sys/queue.h> | |
39 | #include <mach/boolean.h> | |
39037602 | 40 | #include <mach/branch_predicates.h> |
2d21ac55 | 41 | #include <kern/locks.h> |
6d2010ae | 42 | #include <libkern/OSAtomic.h> |
2d21ac55 A |
43 | |
44 | #ifdef ASSERT | |
45 | #undef ASSERT | |
46 | #endif | |
47 | ||
48 | #ifdef VERIFY | |
49 | #undef VERIFY | |
50 | #endif | |
51 | ||
52 | /* | |
53 | * Unlike VERIFY(), ASSERT() is evaluated only in DEBUG build. | |
54 | */ | |
39037602 A |
55 | #define VERIFY(EX) \ |
56 | ((void)(__probable((EX)) || assfail(#EX, __FILE__, __LINE__))) | |
2d21ac55 A |
57 | #if DEBUG |
58 | #define ASSERT(EX) VERIFY(EX) | |
59 | #else | |
60 | #define ASSERT(EX) ((void)0) | |
61 | #endif | |
62 | ||
316670eb A |
63 | /* |
64 | * Compile time assert; this should be on its own someday. | |
65 | */ | |
39037602 | 66 | #define _CASSERT(x) _Static_assert(x, "compile-time assertion failed") |
316670eb A |
67 | |
68 | /* | |
69 | * Atomic macros; these should be on their own someday. | |
70 | */ | |
6d2010ae A |
71 | #define atomic_add_16_ov(a, n) \ |
72 | ((u_int16_t) OSAddAtomic16(n, (volatile SInt16 *)a)) | |
73 | ||
74 | #define atomic_add_16(a, n) \ | |
75 | ((void) atomic_add_16_ov(a, n)) | |
76 | ||
77 | #define atomic_add_32_ov(a, n) \ | |
78 | ((u_int32_t) OSAddAtomic(n, (volatile SInt32 *)a)) | |
79 | ||
80 | #define atomic_add_32(a, n) \ | |
81 | ((void) atomic_add_32_ov(a, n)) | |
82 | ||
83 | #define atomic_add_64_ov(a, n) \ | |
84 | ((u_int64_t) OSAddAtomic64(n, (volatile SInt64 *)a)) | |
85 | ||
86 | #define atomic_add_64(a, n) \ | |
87 | ((void) atomic_add_64_ov(a, n)) | |
88 | ||
39037602 A |
89 | #define atomic_test_set_32(a, o, n) \ |
90 | OSCompareAndSwap(o, n, (volatile UInt32 *)a) | |
91 | ||
92 | #define atomic_set_32(a, n) do { \ | |
93 | while (!atomic_test_set_32(a, *a, n)) \ | |
94 | ; \ | |
95 | } while (0) | |
96 | ||
97 | #define atomic_test_set_64(a, o, n) \ | |
98 | OSCompareAndSwap64(o, n, (volatile UInt64 *)a) | |
99 | ||
6d2010ae | 100 | #define atomic_set_64(a, n) do { \ |
39037602 | 101 | while (!atomic_test_set_64(a, *a, n)) \ |
6d2010ae A |
102 | ; \ |
103 | } while (0) | |
104 | ||
105 | #if defined(__LP64__) | |
106 | #define atomic_get_64(n, a) do { \ | |
107 | (n) = *(a); \ | |
108 | } while (0) | |
2d21ac55 | 109 | #else |
6d2010ae A |
110 | #define atomic_get_64(n, a) do { \ |
111 | (n) = atomic_add_64_ov(a, 0); \ | |
112 | } while (0) | |
113 | #endif /* __LP64__ */ | |
114 | ||
39037602 A |
115 | #define atomic_test_set_ptr(a, o, n) \ |
116 | OSCompareAndSwapPtr(o, n, (void * volatile *)a) | |
117 | ||
118 | #define atomic_set_ptr(a, n) do { \ | |
119 | while (!atomic_test_set_ptr(a, *a, n)) \ | |
120 | ; \ | |
121 | } while (0) | |
122 | ||
39236c6e A |
123 | #define atomic_or_8_ov(a, n) \ |
124 | ((u_int8_t) OSBitOrAtomic8(n, (volatile UInt8 *)a)) | |
125 | ||
126 | #define atomic_or_8(a, n) \ | |
127 | ((void) atomic_or_8_ov(a, n)) | |
128 | ||
129 | #define atomic_bitset_8(a, n) \ | |
130 | atomic_or_8(a, n) | |
131 | ||
132 | #define atomic_or_16_ov(a, n) \ | |
133 | ((u_int16_t) OSBitOrAtomic16(n, (volatile UInt16 *)a)) | |
134 | ||
135 | #define atomic_or_16(a, n) \ | |
136 | ((void) atomic_or_16_ov(a, n)) | |
137 | ||
138 | #define atomic_bitset_16(a, n) \ | |
139 | atomic_or_16(a, n) | |
140 | ||
141 | #define atomic_or_32_ov(a, n) \ | |
142 | ((u_int32_t) OSBitOrAtomic(n, (volatile UInt32 *)a)) | |
143 | ||
144 | #define atomic_or_32(a, n) \ | |
145 | ((void) atomic_or_32_ov(a, n)) | |
146 | ||
147 | #define atomic_bitset_32(a, n) \ | |
148 | atomic_or_32(a, n) | |
149 | ||
150 | #define atomic_and_8_ov(a, n) \ | |
151 | ((u_int8_t) OSBitAndAtomic8(n, (volatile UInt8 *)a)) | |
152 | ||
153 | #define atomic_and_8(a, n) \ | |
154 | ((void) atomic_and_8_ov(a, n)) | |
155 | ||
156 | #define atomic_bitclear_8(a, n) \ | |
157 | atomic_and_8(a, ~(n)) | |
158 | ||
159 | #define atomic_and_16_ov(a, n) \ | |
160 | ((u_int16_t) OSBitAndAtomic16(n, (volatile UInt16 *)a)) | |
161 | ||
162 | #define atomic_and_16(a, n) \ | |
163 | ((void) atomic_and_16_ov(a, n)) | |
164 | ||
165 | #define atomic_bitclear_16(a, n) \ | |
166 | atomic_and_16(a, ~(n)) | |
167 | ||
168 | #define atomic_and_32_ov(a, n) \ | |
169 | ((u_int32_t) OSBitAndAtomic(n, (volatile UInt32 *)a)) | |
170 | ||
171 | #define atomic_and_32(a, n) \ | |
172 | ((void) atomic_and_32_ov(a, n)) | |
173 | ||
174 | #define atomic_bitclear_32(a, n) \ | |
175 | atomic_and_32(a, ~(n)) | |
176 | ||
39037602 A |
177 | #define membar_sync OSMemoryBarrier |
178 | ||
39236c6e A |
179 | /* |
180 | * Use CPU_CACHE_LINE_SIZE instead of MAX_CPU_CACHE_LINE_SIZE, unless | |
181 | * wasting space is of no concern. | |
182 | */ | |
39037602 | 183 | #define MAX_CPU_CACHE_LINE_SIZE 128 |
39236c6e | 184 | #define CPU_CACHE_LINE_SIZE mcache_cache_line_size() |
2d21ac55 A |
185 | |
186 | #ifndef IS_P2ALIGNED | |
187 | #define IS_P2ALIGNED(v, a) \ | |
188 | ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) | |
189 | #endif /* IS_P2ALIGNED */ | |
190 | ||
191 | #ifndef P2ROUNDUP | |
192 | #define P2ROUNDUP(x, align) \ | |
39236c6e | 193 | (-(-((uintptr_t)(x)) & -((uintptr_t)align))) |
2d21ac55 A |
194 | #endif /* P2ROUNDUP */ |
195 | ||
196 | #ifndef P2ROUNDDOWN | |
197 | #define P2ROUNDDOWN(x, align) \ | |
198 | (((uintptr_t)(x)) & ~((uintptr_t)(align) - 1)) | |
199 | #endif /* P2ROUNDDOWN */ | |
200 | ||
201 | #define MCACHE_FREE_PATTERN 0xdeadbeefdeadbeefULL | |
202 | #define MCACHE_UNINITIALIZED_PATTERN 0xbaddcafebaddcafeULL | |
203 | ||
204 | /* | |
205 | * mcache allocation request flags. | |
206 | * | |
207 | * MCR_NOSLEEP and MCR_FAILOK are mutually exclusive. The latter is used | |
208 | * by the mbuf allocator to handle the implementation of several caches that | |
209 | * involve multiple layers of mcache. It implies a best effort blocking | |
210 | * allocation request; if the request cannot be satisfied, the caller will | |
211 | * be blocked until further notice, similar to MCR_SLEEP, except that upon | |
212 | * a wake up it will return immediately to the caller regardless of whether | |
213 | * the request can been fulfilled. | |
214 | * | |
215 | * MCR_TRYHARD implies a non-blocking allocation request, regardless of | |
216 | * whether MCR_NOSLEEP is set. It informs the allocator that the request | |
217 | * should not cause the calling thread to block, and that it must have | |
218 | * exhausted all possible schemes to fulfill the request, including doing | |
219 | * reclaims and/or purges, before returning to the caller. | |
220 | * | |
221 | * Regular mcache clients should only use MCR_SLEEP or MCR_NOSLEEP. | |
222 | */ | |
223 | #define MCR_SLEEP 0x0000 /* same as M_WAITOK */ | |
224 | #define MCR_NOSLEEP 0x0001 /* same as M_NOWAIT */ | |
225 | #define MCR_FAILOK 0x0100 /* private, for internal use only */ | |
226 | #define MCR_TRYHARD 0x0200 /* private, for internal use only */ | |
227 | #define MCR_USR1 0x1000 /* private, for internal use only */ | |
228 | ||
229 | #define MCR_NONBLOCKING (MCR_NOSLEEP | MCR_FAILOK | MCR_TRYHARD) | |
230 | ||
231 | /* | |
232 | * Generic one-way linked list element structure. This is used to handle | |
233 | * mcache_alloc_ext() requests in order to chain the allocated objects | |
234 | * together before returning them to the caller. | |
235 | */ | |
236 | typedef struct mcache_obj { | |
237 | struct mcache_obj *obj_next; | |
238 | } mcache_obj_t; | |
239 | ||
240 | typedef struct mcache_bkt { | |
241 | void *bkt_next; /* next bucket in list */ | |
242 | void *bkt_obj[1]; /* one or more objects */ | |
243 | } mcache_bkt_t; | |
244 | ||
245 | typedef struct mcache_bktlist { | |
246 | mcache_bkt_t *bl_list; /* bucket list */ | |
247 | u_int32_t bl_total; /* number of buckets */ | |
248 | u_int32_t bl_min; /* min since last update */ | |
249 | u_int32_t bl_reaplimit; /* max reapable buckets */ | |
250 | u_int64_t bl_alloc; /* allocations from this list */ | |
251 | } mcache_bktlist_t; | |
252 | ||
253 | typedef struct mcache_bkttype { | |
254 | int bt_bktsize; /* bucket size (number of elements) */ | |
255 | size_t bt_minbuf; /* all smaller buffers qualify */ | |
256 | size_t bt_maxbuf; /* no larger bfufers qualify */ | |
257 | struct mcache *bt_cache; /* bucket cache */ | |
258 | } mcache_bkttype_t; | |
259 | ||
260 | typedef struct mcache_cpu { | |
261 | decl_lck_mtx_data(, cc_lock); | |
262 | mcache_bkt_t *cc_filled; /* the currently filled bucket */ | |
263 | mcache_bkt_t *cc_pfilled; /* the previously filled bucket */ | |
264 | u_int64_t cc_alloc; /* allocations from this cpu */ | |
265 | u_int64_t cc_free; /* frees to this cpu */ | |
266 | int cc_objs; /* number of objects in filled bkt */ | |
267 | int cc_pobjs; /* number of objects in previous bkt */ | |
268 | int cc_bktsize; /* number of elements in a full bkt */ | |
39236c6e | 269 | } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE), packed)) mcache_cpu_t; |
2d21ac55 A |
270 | |
271 | typedef unsigned int (*mcache_allocfn_t)(void *, mcache_obj_t ***, | |
272 | unsigned int, int); | |
273 | typedef void (*mcache_freefn_t)(void *, mcache_obj_t *, boolean_t); | |
274 | typedef void (*mcache_auditfn_t)(void *, mcache_obj_t *, boolean_t); | |
6d2010ae | 275 | typedef void (*mcache_logfn_t)(u_int32_t, mcache_obj_t *, boolean_t); |
2d21ac55 A |
276 | typedef void (*mcache_notifyfn_t)(void *, u_int32_t); |
277 | ||
278 | typedef struct mcache { | |
279 | /* | |
280 | * Cache properties | |
281 | */ | |
282 | LIST_ENTRY(mcache) mc_list; /* cache linkage */ | |
283 | char mc_name[32]; /* cache name */ | |
284 | struct zone *mc_slab_zone; /* backend zone allocator */ | |
285 | mcache_allocfn_t mc_slab_alloc; /* slab layer allocate callback */ | |
286 | mcache_freefn_t mc_slab_free; /* slab layer free callback */ | |
287 | mcache_auditfn_t mc_slab_audit; /* slab layer audit callback */ | |
6d2010ae | 288 | mcache_logfn_t mc_slab_log; /* slab layer log callback */ |
2d21ac55 A |
289 | mcache_notifyfn_t mc_slab_notify; /* slab layer notify callback */ |
290 | void *mc_private; /* opaque arg to callbacks */ | |
291 | size_t mc_bufsize; /* object size */ | |
292 | size_t mc_align; /* object alignment */ | |
293 | u_int32_t mc_flags; /* cache creation flags */ | |
294 | u_int32_t mc_purge_cnt; /* # of purges requested by slab */ | |
295 | u_int32_t mc_enable_cnt; /* # of reenables due to purges */ | |
296 | u_int32_t mc_waiter_cnt; /* # of slab layer waiters */ | |
297 | u_int32_t mc_wretry_cnt; /* # of wait retries */ | |
298 | u_int32_t mc_nwretry_cnt; /* # of no-wait retry attempts */ | |
299 | u_int32_t mc_nwfail_cnt; /* # of no-wait retries that failed */ | |
300 | decl_lck_mtx_data(, mc_sync_lock); /* protects purges and reenables */ | |
301 | lck_attr_t *mc_sync_lock_attr; | |
302 | lck_grp_t *mc_sync_lock_grp; | |
303 | lck_grp_attr_t *mc_sync_lock_grp_attr; | |
304 | /* | |
305 | * Keep CPU and buckets layers lock statistics separate. | |
306 | */ | |
307 | lck_attr_t *mc_cpu_lock_attr; | |
308 | lck_grp_t *mc_cpu_lock_grp; | |
309 | lck_grp_attr_t *mc_cpu_lock_grp_attr; | |
310 | ||
311 | /* | |
312 | * Bucket layer common to all CPUs | |
313 | */ | |
314 | decl_lck_mtx_data(, mc_bkt_lock); | |
315 | lck_attr_t *mc_bkt_lock_attr; | |
316 | lck_grp_t *mc_bkt_lock_grp; | |
317 | lck_grp_attr_t *mc_bkt_lock_grp_attr; | |
318 | mcache_bkttype_t *cache_bkttype; /* bucket type */ | |
319 | mcache_bktlist_t mc_full; /* full buckets */ | |
320 | mcache_bktlist_t mc_empty; /* empty buckets */ | |
321 | size_t mc_chunksize; /* bufsize + alignment */ | |
322 | u_int32_t mc_bkt_contention; /* lock contention count */ | |
323 | u_int32_t mc_bkt_contention_prev; /* previous snapshot */ | |
324 | ||
325 | /* | |
326 | * Per-CPU layer, aligned at cache line boundary | |
327 | */ | |
39037602 A |
328 | mcache_cpu_t mc_cpu[1] |
329 | __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE))); | |
2d21ac55 A |
330 | } mcache_t; |
331 | ||
332 | #define MCACHE_ALIGN 8 /* default guaranteed alignment */ | |
333 | ||
334 | /* Valid values for mc_flags */ | |
335 | #define MCF_VERIFY 0x00000001 /* enable verification */ | |
6d2010ae | 336 | #define MCF_TRACE 0x00000002 /* enable transaction auditing */ |
2d21ac55 | 337 | #define MCF_NOCPUCACHE 0x00000010 /* disable CPU layer caching */ |
6d2010ae | 338 | #define MCF_NOLEAKLOG 0x00000100 /* disable leak logging */ |
316670eb | 339 | #define MCF_EXPLEAKLOG 0x00000200 /* expose leak info to user space */ |
2d21ac55 | 340 | |
6d2010ae | 341 | #define MCF_DEBUG (MCF_VERIFY | MCF_TRACE) |
316670eb A |
342 | #define MCF_FLAGS_MASK \ |
343 | (MCF_DEBUG | MCF_NOCPUCACHE | MCF_NOLEAKLOG | MCF_EXPLEAKLOG) | |
2d21ac55 A |
344 | |
345 | /* Valid values for notify callback */ | |
346 | #define MCN_RETRYALLOC 0x00000001 /* Allocation should be retried */ | |
347 | ||
348 | #define MCACHE_STACK_DEPTH 16 | |
349 | ||
fe8ab488 A |
350 | #define MCA_TRN_MAX 2 /* Number of transactions to record */ |
351 | ||
2d21ac55 A |
352 | typedef struct mcache_audit { |
353 | struct mcache_audit *mca_next; /* next audit struct */ | |
354 | void *mca_addr; /* address of buffer */ | |
355 | mcache_t *mca_cache; /* parent cache of the buffer */ | |
39236c6e A |
356 | size_t mca_contents_size; /* size of saved contents */ |
357 | void *mca_contents; /* user-specific saved contents */ | |
2d21ac55 A |
358 | void *mca_uptr; /* user-specific pointer */ |
359 | uint32_t mca_uflags; /* user-specific flags */ | |
fe8ab488 A |
360 | uint32_t mca_next_trn; |
361 | struct mca_trn { | |
362 | struct thread *mca_thread; /* thread doing transaction */ | |
363 | uint32_t mca_tstamp; | |
364 | uint16_t mca_depth; | |
365 | void *mca_stack[MCACHE_STACK_DEPTH]; | |
366 | } mca_trns[MCA_TRN_MAX]; | |
2d21ac55 A |
367 | } mcache_audit_t; |
368 | ||
369 | __private_extern__ int assfail(const char *, const char *, int); | |
370 | __private_extern__ void mcache_init(void); | |
371 | __private_extern__ unsigned int mcache_getflags(void); | |
39236c6e | 372 | __private_extern__ unsigned int mcache_cache_line_size(void); |
2d21ac55 A |
373 | __private_extern__ mcache_t *mcache_create(const char *, size_t, |
374 | size_t, u_int32_t, int); | |
375 | __private_extern__ void *mcache_alloc(mcache_t *, int); | |
376 | __private_extern__ void mcache_free(mcache_t *, void *); | |
377 | __private_extern__ mcache_t *mcache_create_ext(const char *, size_t, | |
6d2010ae A |
378 | mcache_allocfn_t, mcache_freefn_t, mcache_auditfn_t, mcache_logfn_t, |
379 | mcache_notifyfn_t, void *, u_int32_t, int); | |
2d21ac55 A |
380 | __private_extern__ void mcache_destroy(mcache_t *); |
381 | __private_extern__ unsigned int mcache_alloc_ext(mcache_t *, mcache_obj_t **, | |
382 | unsigned int, int); | |
383 | __private_extern__ void mcache_free_ext(mcache_t *, mcache_obj_t *); | |
384 | __private_extern__ void mcache_reap(void); | |
fe8ab488 | 385 | __private_extern__ boolean_t mcache_purge_cache(mcache_t *, boolean_t); |
2d21ac55 A |
386 | __private_extern__ void mcache_waiter_inc(mcache_t *); |
387 | __private_extern__ void mcache_waiter_dec(mcache_t *); | |
388 | __private_extern__ boolean_t mcache_bkt_isempty(mcache_t *); | |
389 | ||
39236c6e A |
390 | __private_extern__ void mcache_buffer_log(mcache_audit_t *, void *, mcache_t *, |
391 | struct timeval *); | |
2d21ac55 A |
392 | __private_extern__ void mcache_set_pattern(u_int64_t, void *, size_t); |
393 | __private_extern__ void *mcache_verify_pattern(u_int64_t, void *, size_t); | |
394 | __private_extern__ void *mcache_verify_set_pattern(u_int64_t, u_int64_t, | |
395 | void *, size_t); | |
396 | __private_extern__ void mcache_audit_free_verify(mcache_audit_t *, | |
397 | void *, size_t, size_t); | |
398 | __private_extern__ void mcache_audit_free_verify_set(mcache_audit_t *, | |
399 | void *, size_t, size_t); | |
400 | __private_extern__ char *mcache_dump_mca(mcache_audit_t *); | |
401 | __private_extern__ void mcache_audit_panic(mcache_audit_t *, void *, size_t, | |
402 | int64_t, int64_t); | |
403 | ||
fe8ab488 | 404 | extern int32_t total_sbmb_cnt; |
39037602 | 405 | extern int32_t total_sbmb_cnt_floor; |
fe8ab488 A |
406 | extern int32_t total_sbmb_cnt_peak; |
407 | extern int64_t sbmb_limreached; | |
39236c6e | 408 | extern mcache_t *mcache_audit_cache; |
2d21ac55 A |
409 | |
410 | #ifdef __cplusplus | |
411 | } | |
412 | #endif | |
413 | ||
414 | #endif /* KERNEL_PRIVATE */ | |
415 | ||
416 | #endif /* _SYS_MCACHE_H */ |