xref: /xnu-8020.140.41/osfmk/vm/vm_compressor.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <vm/vm_compressor_pager.h>
30 #include <vm/vm_kern.h>
31 #include <vm/vm_page.h>
32 #include <vm/vm_protos.h>
33 #include <vm/WKdm_new.h>
34 #include <vm/vm_object.h>
35 #include <vm/vm_map.h>
36 #include <machine/pmap.h>
37 #include <kern/locks.h>
38 
39 #include <sys/kdebug.h>
40 
41 #if defined(__arm64__)
42 #include <arm/proc_reg.h>
43 #endif
44 
45 #define C_SEG_OFFSET_BITS       16
46 
47 #define C_SEG_MAX_POPULATE_SIZE (4 * PAGE_SIZE)
48 
49 #if defined(__arm64__) && (DEVELOPMENT || DEBUG)
50 
51 #if defined(PLATFORM_WatchOS)
52 #define VALIDATE_C_SEGMENTS (1)
53 #endif
54 #endif /* defined(__arm64__) && (DEVELOPMENT || DEBUG) */
55 
56 
57 #if DEBUG || COMPRESSOR_INTEGRITY_CHECKS
58 #define ENABLE_SWAP_CHECKS 1
59 #define ENABLE_COMPRESSOR_CHECKS 1
60 #define POPCOUNT_THE_COMPRESSED_DATA (1)
61 #else
62 #define ENABLE_SWAP_CHECKS 0
63 #define ENABLE_COMPRESSOR_CHECKS 0
64 #endif
65 
66 #define CHECKSUM_THE_SWAP               ENABLE_SWAP_CHECKS              /* Debug swap data */
67 #define CHECKSUM_THE_DATA               ENABLE_COMPRESSOR_CHECKS        /* Debug compressor/decompressor data */
68 #define CHECKSUM_THE_COMPRESSED_DATA    ENABLE_COMPRESSOR_CHECKS        /* Debug compressor/decompressor compressed data */
69 
70 #ifndef VALIDATE_C_SEGMENTS
71 #define VALIDATE_C_SEGMENTS             ENABLE_COMPRESSOR_CHECKS        /* Debug compaction */
72 #endif
73 
74 #define RECORD_THE_COMPRESSED_DATA      0
75 
76 /*
77  * The c_slot structure embeds a packed pointer to a c_slot_mapping
78  * (32bits) which we ideally want to span as much VA space as possible
79  * to not limit zalloc in how it sets itself up.
80  */
81 #if !defined(__LP64__)                  /* no packing */
82 #define C_SLOT_PACKED_PTR_BITS          32
83 #define C_SLOT_PACKED_PTR_SHIFT         0
84 #define C_SLOT_PACKED_PTR_BASE          0
85 
86 #define C_SLOT_C_SIZE_BITS              12
87 #define C_SLOT_C_CODEC_BITS             1
88 #define C_SLOT_C_POPCOUNT_BITS          0
89 #define C_SLOT_C_PADDING_BITS           3
90 
91 #elif __APPLE_WKDM_POPCNT_EXTENSIONS__               /* no packing */
92 #define C_SLOT_PACKED_PTR_BITS          47
93 #define C_SLOT_PACKED_PTR_SHIFT         0
94 #define C_SLOT_PACKED_PTR_BASE          ((uintptr_t)KERNEL_PMAP_HEAP_RANGE_START)
95 
96 #define C_SLOT_C_SIZE_BITS              14
97 #define C_SLOT_C_CODEC_BITS             1
98 #define C_SLOT_C_POPCOUNT_BITS          18
99 #define C_SLOT_C_PADDING_BITS           0
100 
101 #elif defined(__arm64__)                /* 32G from the heap start */
102 #define C_SLOT_PACKED_PTR_BITS          33
103 #define C_SLOT_PACKED_PTR_SHIFT         2
104 #define C_SLOT_PACKED_PTR_BASE          ((uintptr_t)KERNEL_PMAP_HEAP_RANGE_START)
105 
106 #define C_SLOT_C_SIZE_BITS              14
107 #define C_SLOT_C_CODEC_BITS             1
108 #define C_SLOT_C_POPCOUNT_BITS          0
109 #define C_SLOT_C_PADDING_BITS           0
110 
111 #elif defined(__x86_64__)               /* 256G from the heap start */
112 #define C_SLOT_PACKED_PTR_BITS          36
113 #define C_SLOT_PACKED_PTR_SHIFT         2
114 #define C_SLOT_PACKED_PTR_BASE          ((uintptr_t)KERNEL_PMAP_HEAP_RANGE_START)
115 
116 #define C_SLOT_C_SIZE_BITS              12
117 #define C_SLOT_C_CODEC_BITS             0 /* not used */
118 #define C_SLOT_C_POPCOUNT_BITS          0
119 #define C_SLOT_C_PADDING_BITS           0
120 
121 #else
122 #error vm_compressor parameters undefined for this architecture
123 #endif
124 
125 /*
126  * Popcounts needs to represent both 0 and full which requires
127  * (8 ^ C_SLOT_C_SIZE_BITS) + 1 values and (C_SLOT_C_SIZE_BITS + 4) bits.
128  *
129  * We us the (2 * (8 ^ C_SLOT_C_SIZE_BITS) - 1) value to mean "unknown".
130  */
131 #define C_SLOT_NO_POPCOUNT              ((16u << C_SLOT_C_SIZE_BITS) - 1)
132 
133 static_assert((C_SEG_OFFSET_BITS + C_SLOT_C_SIZE_BITS +
134     C_SLOT_C_CODEC_BITS + C_SLOT_C_POPCOUNT_BITS +
135     C_SLOT_C_PADDING_BITS + C_SLOT_PACKED_PTR_BITS) % 32 == 0);
136 
137 struct c_slot {
138 	uint64_t        c_offset:C_SEG_OFFSET_BITS;
139 	uint64_t        c_size:C_SLOT_C_SIZE_BITS;
140 #if C_SLOT_C_CODEC_BITS
141 	uint64_t        c_codec:C_SLOT_C_CODEC_BITS;
142 #endif
143 #if C_SLOT_C_POPCOUNT_BITS
144 	/*
145 	 * This value may not agree with c_pop_cdata, as it may be the
146 	 * population count of the uncompressed data.
147 	 *
148 	 * This value must be C_SLOT_NO_POPCOUNT when the compression algorithm
149 	 * cannot provide it.
150 	 */
151 	uint32_t        c_inline_popcount:C_SLOT_C_POPCOUNT_BITS;
152 #endif
153 #if C_SLOT_C_PADDING_BITS
154 	uint64_t        c_padding:C_SLOT_C_PADDING_BITS;
155 #endif
156 	uint64_t        c_packed_ptr:C_SLOT_PACKED_PTR_BITS;
157 
158 	/* debugging fields, typically not present on release kernels */
159 #if CHECKSUM_THE_DATA
160 	unsigned int    c_hash_data;
161 #endif
162 #if CHECKSUM_THE_COMPRESSED_DATA
163 	unsigned int    c_hash_compressed_data;
164 #endif
165 #if POPCOUNT_THE_COMPRESSED_DATA
166 	unsigned int    c_pop_cdata;
167 #endif
168 } __attribute__((packed, aligned(4)));
169 
170 #define C_IS_EMPTY              0
171 #define C_IS_FREE               1
172 #define C_IS_FILLING            2
173 #define C_ON_AGE_Q              3
174 #define C_ON_SWAPOUT_Q          4
175 #define C_ON_SWAPPEDOUT_Q       5
176 #define C_ON_SWAPPEDOUTSPARSE_Q 6
177 #define C_ON_SWAPPEDIN_Q        7
178 #define C_ON_MAJORCOMPACT_Q     8
179 #define C_ON_BAD_Q              9
180 #define C_ON_SWAPIO_Q          10
181 
182 
183 struct c_segment {
184 	lck_mtx_t       c_lock;
185 	queue_chain_t   c_age_list;
186 	queue_chain_t   c_list;
187 
188 #if CONFIG_FREEZE
189 	queue_chain_t   c_task_list_next_cseg;
190 	task_t          c_task_owner;
191 #endif /* CONFIG_FREEZE */
192 
193 #define C_SEG_MAX_LIMIT         (UINT_MAX)       /* this needs to track the size of c_mysegno */
194 	uint32_t        c_mysegno;
195 
196 	uint32_t        c_creation_ts;
197 	uint64_t        c_generation_id;
198 
199 	int32_t         c_bytes_used;
200 	int32_t         c_bytes_unused;
201 	uint32_t        c_slots_used;
202 
203 	uint16_t        c_firstemptyslot;
204 	uint16_t        c_nextslot;
205 	uint32_t        c_nextoffset;
206 	uint32_t        c_populated_offset;
207 
208 	union {
209 		int32_t *c_buffer;
210 		uint64_t c_swap_handle;
211 	} c_store;
212 
213 #if     VALIDATE_C_SEGMENTS
214 	uint32_t        c_was_minor_compacted;
215 	uint32_t        c_was_major_compacted;
216 	uint32_t        c_was_major_donor;
217 #endif
218 #if CHECKSUM_THE_SWAP
219 	unsigned int    cseg_hash;
220 	unsigned int    cseg_swap_size;
221 #endif /* CHECKSUM_THE_SWAP */
222 
223 	thread_t        c_busy_for_thread;
224 	uint32_t        c_agedin_ts;
225 	uint32_t        c_swappedin_ts;
226 	bool            c_swappedin;
227 	/*
228 	 * Do not pull c_swappedin above into the bitfield below.
229 	 * We update it without always taking the segment
230 	 * lock and rely on the segment being busy instead.
231 	 * The bitfield needs the segment lock. So updating
232 	 * this state, if in the bitfield, without the lock
233 	 * will race with the updates to the other fields and
234 	 * result in a mess.
235 	 */
236 	uint32_t        c_busy:1,
237 	    c_busy_swapping:1,
238 	    c_wanted:1,
239 	    c_on_minorcompact_q:1,              /* can also be on the age_q, the majorcompact_q or the swappedin_q */
240 
241 	    c_state:4,                          /* what state is the segment in which dictates which q to find it on */
242 	    c_overage_swap:1,
243 	    c_reserved:23;
244 	int             c_slot_var_array_len;
245 	struct  c_slot  *c_slot_var_array;
246 	struct  c_slot  c_slot_fixed_array[0];
247 };
248 
249 
250 struct  c_slot_mapping {
251 	uint32_t        s_cseg:22,      /* segment number + 1 */
252 	    s_cindx:10;                 /* index in the segment */
253 };
254 #define C_SLOT_MAX_INDEX        (1 << 10)
255 
256 typedef struct c_slot_mapping *c_slot_mapping_t;
257 
258 
259 extern  int             c_seg_fixed_array_len;
260 extern  vm_offset_t     c_buffers;
261 #define C_SEG_BUFFER_ADDRESS(c_segno)   ((c_buffers + ((uint64_t)c_segno * (uint64_t)c_seg_allocsize)))
262 
263 #define C_SEG_SLOT_FROM_INDEX(cseg, index)      (index < c_seg_fixed_array_len ? &(cseg->c_slot_fixed_array[index]) : &(cseg->c_slot_var_array[index - c_seg_fixed_array_len]))
264 
265 #define C_SEG_OFFSET_TO_BYTES(off)      ((off) * (int) sizeof(int32_t))
266 #define C_SEG_BYTES_TO_OFFSET(bytes)    ((bytes) / (int) sizeof(int32_t))
267 
268 #define C_SEG_UNUSED_BYTES(cseg)        (cseg->c_bytes_unused + (C_SEG_OFFSET_TO_BYTES(cseg->c_populated_offset - cseg->c_nextoffset)))
269 //todo opensource
270 
271 #ifndef __PLATFORM_WKDM_ALIGNMENT_MASK__
272 #define C_SEG_OFFSET_ALIGNMENT_MASK     0x3ULL
273 #define C_SEG_OFFSET_ALIGNMENT_BOUNDARY 0x4
274 #else
275 #define C_SEG_OFFSET_ALIGNMENT_MASK     __PLATFORM_WKDM_ALIGNMENT_MASK__
276 #define C_SEG_OFFSET_ALIGNMENT_BOUNDARY __PLATFORM_WKDM_ALIGNMENT_BOUNDARY__
277 #endif
278 
279 #define C_SEG_SHOULD_MINORCOMPACT_NOW(cseg)     ((C_SEG_UNUSED_BYTES(cseg) >= (c_seg_bufsize / 4)) ? 1 : 0)
280 
281 /*
282  * the decsion to force a c_seg to be major compacted is based on 2 criteria
283  * 1) is the c_seg buffer almost empty (i.e. we have a chance to merge it with another c_seg)
284  * 2) are there at least a minimum number of slots unoccupied so that we have a chance
285  *    of combining this c_seg with another one.
286  */
287 #define C_SEG_SHOULD_MAJORCOMPACT_NOW(cseg)                                                                                     \
288 	((((cseg->c_bytes_unused + (c_seg_bufsize - C_SEG_OFFSET_TO_BYTES(c_seg->c_nextoffset))) >= (c_seg_bufsize / 8)) &&     \
289 	  ((C_SLOT_MAX_INDEX - cseg->c_slots_used) > (c_seg_bufsize / PAGE_SIZE))) \
290 	? 1 : 0)
291 
292 #define C_SEG_ONDISK_IS_SPARSE(cseg)    ((cseg->c_bytes_used < cseg->c_bytes_unused) ? 1 : 0)
293 #define C_SEG_IS_ONDISK(cseg)           ((cseg->c_state == C_ON_SWAPPEDOUT_Q || cseg->c_state == C_ON_SWAPPEDOUTSPARSE_Q))
294 #define C_SEG_IS_ON_DISK_OR_SOQ(cseg)   ((cseg->c_state == C_ON_SWAPPEDOUT_Q || \
295 	                                  cseg->c_state == C_ON_SWAPPEDOUTSPARSE_Q || \
296 	                                  cseg->c_state == C_ON_SWAPOUT_Q || \
297 	                                  cseg->c_state == C_ON_SWAPIO_Q))
298 
299 
300 #define C_SEG_WAKEUP_DONE(cseg)                         \
301 	MACRO_BEGIN                                     \
302 	assert((cseg)->c_busy);                         \
303 	(cseg)->c_busy = 0;                             \
304 	assert((cseg)->c_busy_for_thread != NULL);      \
305 	(cseg)->c_busy_for_thread = NULL;               \
306 	if ((cseg)->c_wanted) {                         \
307 	        (cseg)->c_wanted = 0;                   \
308 	        thread_wakeup((event_t) (cseg));        \
309 	}                                               \
310 	MACRO_END
311 
312 #define C_SEG_BUSY(cseg)                                \
313 	MACRO_BEGIN                                     \
314 	assert((cseg)->c_busy == 0);                    \
315 	(cseg)->c_busy = 1;                             \
316 	assert((cseg)->c_busy_for_thread == NULL);      \
317 	(cseg)->c_busy_for_thread = current_thread();   \
318 	MACRO_END
319 
320 
321 extern vm_map_t compressor_map;
322 
323 #if DEVELOPMENT || DEBUG
324 extern boolean_t write_protect_c_segs;
325 extern int vm_compressor_test_seg_wp;
326 
327 #define C_SEG_MAKE_WRITEABLE(cseg)                      \
328 	MACRO_BEGIN                                     \
329 	if (write_protect_c_segs) {                     \
330 	        vm_map_protect(compressor_map,                  \
331 	                       (vm_map_offset_t)cseg->c_store.c_buffer,         \
332 	                       (vm_map_offset_t)&cseg->c_store.c_buffer[C_SEG_BYTES_TO_OFFSET(c_seg_allocsize)],\
333 	                       VM_PROT_READ | VM_PROT_WRITE,    \
334 	                       0);                              \
335 	}                               \
336 	MACRO_END
337 
338 #define C_SEG_WRITE_PROTECT(cseg)                       \
339 	MACRO_BEGIN                                     \
340 	if (write_protect_c_segs) {                     \
341 	        vm_map_protect(compressor_map,                  \
342 	                       (vm_map_offset_t)cseg->c_store.c_buffer,         \
343 	                       (vm_map_offset_t)&cseg->c_store.c_buffer[C_SEG_BYTES_TO_OFFSET(c_seg_allocsize)],\
344 	                       VM_PROT_READ,                    \
345 	                       0);                              \
346 	}                                                       \
347 	if (vm_compressor_test_seg_wp) {                                \
348 	        volatile uint32_t vmtstmp = *(volatile uint32_t *)cseg->c_store.c_buffer; \
349 	        *(volatile uint32_t *)cseg->c_store.c_buffer = 0xDEADABCD; \
350 	        (void) vmtstmp;                                         \
351 	}                                                               \
352 	MACRO_END
353 #endif
354 
355 typedef struct c_segment *c_segment_t;
356 typedef struct c_slot   *c_slot_t;
357 
358 uint64_t vm_compressor_total_compressions(void);
359 void vm_wake_compactor_swapper(void);
360 void vm_run_compactor(void);
361 void vm_thrashing_jetsam_done(void);
362 void vm_consider_waking_compactor_swapper(void);
363 void vm_consider_swapping(void);
364 void vm_compressor_flush(void);
365 void c_seg_free(c_segment_t);
366 void c_seg_free_locked(c_segment_t);
367 void c_seg_insert_into_age_q(c_segment_t);
368 void c_seg_need_delayed_compaction(c_segment_t, boolean_t);
369 void c_seg_update_task_owner(c_segment_t, task_t);
370 
371 void vm_decompressor_lock(void);
372 void vm_decompressor_unlock(void);
373 
374 void vm_compressor_delay_trim(void);
375 void vm_compressor_do_warmup(void);
376 void vm_compressor_record_warmup_start(void);
377 void vm_compressor_record_warmup_end(void);
378 
379 int                     vm_wants_task_throttled(task_t);
380 
381 extern void             vm_compaction_swapper_do_init(void);
382 extern void             vm_compressor_swap_init(void);
383 extern lck_rw_t         c_master_lock;
384 
385 #if ENCRYPTED_SWAP
386 extern void             vm_swap_decrypt(c_segment_t);
387 #endif /* ENCRYPTED_SWAP */
388 
389 extern int              vm_swap_low_on_space(void);
390 extern int              vm_swap_out_of_space(void);
391 extern kern_return_t    vm_swap_get(c_segment_t, uint64_t, uint64_t);
392 extern void             vm_swap_free(uint64_t);
393 extern void             vm_swap_consider_defragmenting(int);
394 
395 extern void             c_seg_swapin_requeue(c_segment_t, boolean_t, boolean_t, boolean_t);
396 extern int              c_seg_swapin(c_segment_t, boolean_t, boolean_t);
397 extern void             c_seg_wait_on_busy(c_segment_t);
398 extern void             c_seg_trim_tail(c_segment_t);
399 extern void             c_seg_switch_state(c_segment_t, int, boolean_t);
400 
401 extern boolean_t        fastwake_recording_in_progress;
402 extern int              compaction_swapper_inited;
403 extern int              compaction_swapper_running;
404 extern uint64_t         vm_swap_put_failures;
405 
406 extern int              c_overage_swapped_count;
407 extern int              c_overage_swapped_limit;
408 
409 extern queue_head_t     c_minor_list_head;
410 extern queue_head_t     c_age_list_head;
411 extern queue_head_t     c_swapout_list_head;
412 extern queue_head_t     c_swappedout_list_head;
413 extern queue_head_t     c_swappedout_sparse_list_head;
414 
415 extern uint32_t         c_age_count;
416 extern uint32_t         c_swapout_count;
417 extern uint32_t         c_swappedout_count;
418 extern uint32_t         c_swappedout_sparse_count;
419 
420 extern int64_t          compressor_bytes_used;
421 extern uint64_t         first_c_segment_to_warm_generation_id;
422 extern uint64_t         last_c_segment_to_warm_generation_id;
423 extern boolean_t        hibernate_flushing;
424 extern boolean_t        hibernate_no_swapspace;
425 extern boolean_t        hibernate_in_progress_with_pinned_swap;
426 extern boolean_t        hibernate_flush_timed_out;
427 extern uint32_t         swapout_target_age;
428 
429 extern void c_seg_insert_into_q(queue_head_t *, c_segment_t);
430 
431 extern uint32_t vm_compressor_minorcompact_threshold_divisor;
432 extern uint32_t vm_compressor_majorcompact_threshold_divisor;
433 extern uint32_t vm_compressor_unthrottle_threshold_divisor;
434 extern uint32_t vm_compressor_catchup_threshold_divisor;
435 
436 extern uint32_t vm_compressor_minorcompact_threshold_divisor_overridden;
437 extern uint32_t vm_compressor_majorcompact_threshold_divisor_overridden;
438 extern uint32_t vm_compressor_unthrottle_threshold_divisor_overridden;
439 extern uint32_t vm_compressor_catchup_threshold_divisor_overridden;
440 
441 extern uint64_t vm_compressor_compute_elapsed_msecs(clock_sec_t, clock_nsec_t, clock_sec_t, clock_nsec_t);
442 
443 extern void kdp_compressor_busy_find_owner(event64_t wait_event, thread_waitinfo_t *waitinfo);
444 
445 #define PAGE_REPLACEMENT_DISALLOWED(enable)     (enable == TRUE ? lck_rw_lock_shared(&c_master_lock) : lck_rw_done(&c_master_lock))
446 #define PAGE_REPLACEMENT_ALLOWED(enable)        (enable == TRUE ? lck_rw_lock_exclusive(&c_master_lock) : lck_rw_done(&c_master_lock))
447 
448 
449 #define AVAILABLE_NON_COMPRESSED_MEMORY         (vm_page_active_count + vm_page_inactive_count + vm_page_free_count + vm_page_speculative_count)
450 #define AVAILABLE_MEMORY                        (AVAILABLE_NON_COMPRESSED_MEMORY + VM_PAGE_COMPRESSOR_COUNT)
451 
452 /*
453  * TODO, there may be a minor optimisation opportunity to replace these divisions
454  * with multiplies and shifts
455  *
456  * By multiplying by 10, the divisors can have more precision w/o resorting to floating point... a divisor specified as 25 is in reality a divide by 2.5
457  * By multiplying by 9, you get a number ~11% smaller which allows us to have another limit point derived from the same base
458  * By multiplying by 11, you get a number ~10% bigger which allows us to generate a reset limit derived from the same base which is useful for hysteresis
459  */
460 
461 #define VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD            (((AVAILABLE_MEMORY) * 10) / (vm_compressor_minorcompact_threshold_divisor ? vm_compressor_minorcompact_threshold_divisor : 10))
462 #define VM_PAGE_COMPRESSOR_SWAP_THRESHOLD               (((AVAILABLE_MEMORY) * 10) / (vm_compressor_majorcompact_threshold_divisor ? vm_compressor_majorcompact_threshold_divisor : 10))
463 
464 #define VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD    (((AVAILABLE_MEMORY) * 10) / (vm_compressor_unthrottle_threshold_divisor ? vm_compressor_unthrottle_threshold_divisor : 10))
465 #define VM_PAGE_COMPRESSOR_SWAP_RETHROTTLE_THRESHOLD    (((AVAILABLE_MEMORY) * 11) / (vm_compressor_unthrottle_threshold_divisor ? vm_compressor_unthrottle_threshold_divisor : 11))
466 
467 #define VM_PAGE_COMPRESSOR_SWAP_HAS_CAUGHTUP_THRESHOLD  (((AVAILABLE_MEMORY) * 11) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 11))
468 #define VM_PAGE_COMPRESSOR_SWAP_CATCHUP_THRESHOLD       (((AVAILABLE_MEMORY) * 10) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 10))
469 #define VM_PAGE_COMPRESSOR_HARD_THROTTLE_THRESHOLD      (((AVAILABLE_MEMORY) * 9) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 9))
470 
471 #if !XNU_TARGET_OS_OSX
472 #define AVAILABLE_NON_COMPRESSED_MIN                    20000
473 #define COMPRESSOR_NEEDS_TO_SWAP()              (((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_THRESHOLD) || \
474 	                                          (AVAILABLE_NON_COMPRESSED_MEMORY < AVAILABLE_NON_COMPRESSED_MIN)) ? 1 : 0)
475 #else /* !XNU_TARGET_OS_OSX */
476 #define COMPRESSOR_NEEDS_TO_SWAP()              ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_THRESHOLD) ? 1 : 0)
477 #endif /* !XNU_TARGET_OS_OSX */
478 
479 #define HARD_THROTTLE_LIMIT_REACHED()           ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_HARD_THROTTLE_THRESHOLD) ? 1 : 0)
480 #define SWAPPER_NEEDS_TO_UNTHROTTLE()           ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD) ? 1 : 0)
481 #define SWAPPER_NEEDS_TO_RETHROTTLE()           ((AVAILABLE_NON_COMPRESSED_MEMORY > VM_PAGE_COMPRESSOR_SWAP_RETHROTTLE_THRESHOLD) ? 1 : 0)
482 #define SWAPPER_NEEDS_TO_CATCHUP()              ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_CATCHUP_THRESHOLD) ? 1 : 0)
483 #define SWAPPER_HAS_CAUGHTUP()                  ((AVAILABLE_NON_COMPRESSED_MEMORY > VM_PAGE_COMPRESSOR_SWAP_HAS_CAUGHTUP_THRESHOLD) ? 1 : 0)
484 #define COMPRESSOR_NEEDS_TO_MINOR_COMPACT()     ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD) ? 1 : 0)
485 
486 
487 #if !XNU_TARGET_OS_OSX
488 #define COMPRESSOR_FREE_RESERVED_LIMIT          28
489 #else /* !XNU_TARGET_OS_OSX */
490 #define COMPRESSOR_FREE_RESERVED_LIMIT          128
491 #endif /* !XNU_TARGET_OS_OSX */
492 
493 uint32_t vm_compressor_get_encode_scratch_size(void) __pure2;
494 uint32_t vm_compressor_get_decode_scratch_size(void) __pure2;
495 
496 #define COMPRESSOR_SCRATCH_BUF_SIZE vm_compressor_get_encode_scratch_size()
497 
498 #if RECORD_THE_COMPRESSED_DATA
499 extern void      c_compressed_record_init(void);
500 extern void      c_compressed_record_write(char *, int);
501 #endif
502 
503 extern lck_mtx_t c_list_lock_storage;
504 #define          c_list_lock (&c_list_lock_storage)
505 
506 #if DEVELOPMENT || DEBUG
507 extern uint32_t vm_ktrace_enabled;
508 
509 #define VMKDBG(x, ...)          \
510 MACRO_BEGIN                     \
511 if (vm_ktrace_enabled) {        \
512 	KDBG(x, ## __VA_ARGS__);\
513 }                               \
514 MACRO_END
515 #endif
516