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