xref: /xnu-11417.140.69/osfmk/vm/vm_compressor_xnu.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
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_XNU_H_
29 #define _VM_VM_COMPRESSOR_XNU_H_
30 
31 #ifdef MACH_KERNEL_PRIVATE
32 
33 #include <vm/vm_kern.h>
34 #include <vm/vm_page.h>
35 #include <vm/vm_protos.h>
36 #include <vm/WKdm_new.h>
37 #include <vm/vm_object_xnu.h>
38 #include <vm/vm_map.h>
39 #include <machine/pmap.h>
40 #include <kern/locks.h>
41 
42 #include <sys/kdebug.h>
43 
44 #if defined(__arm64__)
45 #include <arm64/proc_reg.h>
46 #endif
47 
48 
49 #define C_SEG_OFFSET_BITS       16
50 
51 #define C_SEG_MAX_POPULATE_SIZE (4 * PAGE_SIZE)
52 
53 #if defined(__arm64__) && (DEVELOPMENT || DEBUG)
54 
55 #if defined(XNU_PLATFORM_WatchOS)
56 #define VALIDATE_C_SEGMENTS (1)
57 #endif
58 #endif /* defined(__arm64__) && (DEVELOPMENT || DEBUG) */
59 
60 
61 #if DEBUG || COMPRESSOR_INTEGRITY_CHECKS
62 #define ENABLE_SWAP_CHECKS 1
63 #define ENABLE_COMPRESSOR_CHECKS 1
64 #define POPCOUNT_THE_COMPRESSED_DATA (1)
65 #else
66 #define ENABLE_SWAP_CHECKS 0
67 #define ENABLE_COMPRESSOR_CHECKS 0
68 #endif
69 
70 #define CHECKSUM_THE_SWAP               ENABLE_SWAP_CHECKS              /* Debug swap data */
71 #define CHECKSUM_THE_DATA               ENABLE_COMPRESSOR_CHECKS        /* Debug compressor/decompressor data */
72 #define CHECKSUM_THE_COMPRESSED_DATA    ENABLE_COMPRESSOR_CHECKS        /* Debug compressor/decompressor compressed data */
73 
74 #ifndef VALIDATE_C_SEGMENTS
75 #define VALIDATE_C_SEGMENTS             ENABLE_COMPRESSOR_CHECKS        /* Debug compaction */
76 #endif
77 
78 #define RECORD_THE_COMPRESSED_DATA      0
79 #define TRACK_C_SEGMENT_UTILIZATION     0
80 
81 /*
82  * The c_slot structure embeds a packed pointer to a c_slot_mapping
83  * (32bits) which we ideally want to span as much VA space as possible
84  * to not limit zalloc in how it sets itself up.
85  */
86 #if !defined(__LP64__)                  /* no packing */
87 #define C_SLOT_PACKED_PTR_BITS          32
88 #define C_SLOT_PACKED_PTR_SHIFT         0
89 #define C_SLOT_PACKED_PTR_BASE          0
90 
91 #define C_SLOT_C_SIZE_BITS              12
92 #define C_SLOT_C_CODEC_BITS             1
93 #define C_SLOT_C_POPCOUNT_BITS          0
94 #define C_SLOT_C_PADDING_BITS           3
95 
96 #elif defined(__arm64__)                /* 32G from the heap start */
97 #define C_SLOT_PACKED_PTR_BITS          33
98 #define C_SLOT_PACKED_PTR_SHIFT         2
99 #define C_SLOT_PACKED_PTR_BASE          ((uintptr_t)KERNEL_PMAP_HEAP_RANGE_START)
100 
101 #define C_SLOT_C_SIZE_BITS              14
102 #define C_SLOT_C_CODEC_BITS             1
103 #define C_SLOT_C_POPCOUNT_BITS          0
104 #define C_SLOT_C_PADDING_BITS           0
105 
106 #elif defined(__x86_64__)               /* 256G from the heap start */
107 #define C_SLOT_PACKED_PTR_BITS          36
108 #define C_SLOT_PACKED_PTR_SHIFT         2
109 #define C_SLOT_PACKED_PTR_BASE          ((uintptr_t)KERNEL_PMAP_HEAP_RANGE_START)
110 
111 #define C_SLOT_C_SIZE_BITS              12
112 #define C_SLOT_C_CODEC_BITS             0 /* not used */
113 #define C_SLOT_C_POPCOUNT_BITS          0
114 #define C_SLOT_C_PADDING_BITS           0
115 
116 #else
117 #error vm_compressor parameters undefined for this architecture
118 #endif
119 
120 /*
121  * Popcounts needs to represent both 0 and full which requires
122  * (8 ^ C_SLOT_C_SIZE_BITS) + 1 values and (C_SLOT_C_SIZE_BITS + 4) bits.
123  *
124  * We us the (2 * (8 ^ C_SLOT_C_SIZE_BITS) - 1) value to mean "unknown".
125  */
126 #define C_SLOT_NO_POPCOUNT              ((16u << C_SLOT_C_SIZE_BITS) - 1)
127 
128 static_assert((C_SEG_OFFSET_BITS + C_SLOT_C_SIZE_BITS +
129     C_SLOT_C_CODEC_BITS + C_SLOT_C_POPCOUNT_BITS +
130     C_SLOT_C_PADDING_BITS + C_SLOT_PACKED_PTR_BITS) % 32 == 0);
131 
132 struct c_slot {
133 	uint64_t        c_offset:C_SEG_OFFSET_BITS __kernel_ptr_semantics;
134 	/* 0 means it's an empty slot
135 	 * 4 means it's a short-value that did not fit in the hash
136 	 * [5 : PAGE_SIZE-1] means it is normally compressed
137 	 * PAGE_SIZE means it was incompressible (see tag:WK-INCOMPRESSIBLE) */
138 	uint64_t        c_size:C_SLOT_C_SIZE_BITS;
139 #if C_SLOT_C_CODEC_BITS
140 	uint64_t        c_codec:C_SLOT_C_CODEC_BITS;
141 #endif
142 #if C_SLOT_C_POPCOUNT_BITS
143 	/*
144 	 * This value may not agree with c_pop_cdata, as it may be the
145 	 * population count of the uncompressed data.
146 	 *
147 	 * This value must be C_SLOT_NO_POPCOUNT when the compression algorithm
148 	 * cannot provide it.
149 	 */
150 	uint32_t        c_inline_popcount:C_SLOT_C_POPCOUNT_BITS;
151 #endif
152 #if C_SLOT_C_PADDING_BITS
153 	uint64_t        c_padding:C_SLOT_C_PADDING_BITS;
154 #endif
155 	uint64_t        c_packed_ptr:C_SLOT_PACKED_PTR_BITS __kernel_ptr_semantics; /* points back to the c_slot_mapping_t in the pager */
156 
157 	/* debugging fields, typically not present on release kernels */
158 #if CHECKSUM_THE_DATA
159 	unsigned int    c_hash_data;
160 #endif
161 #if CHECKSUM_THE_COMPRESSED_DATA
162 	unsigned int    c_hash_compressed_data;
163 #endif
164 #if POPCOUNT_THE_COMPRESSED_DATA
165 	unsigned int    c_pop_cdata;
166 #endif
167 } __attribute__((packed, aligned(4)));
168 
169 #define C_IS_EMPTY              0  /* segment was just allocated and is going to start filling */
170 #define C_IS_FREE               1  /* segment is unused, went to the free-list, unallocated */
171 #define C_IS_FILLING            2
172 #define C_ON_AGE_Q              3
173 #define C_ON_SWAPOUT_Q          4
174 #define C_ON_SWAPPEDOUT_Q       5
175 #define C_ON_SWAPPEDOUTSPARSE_Q 6  /* segment is swapped-out but some of its slots were freed */
176 #define C_ON_SWAPPEDIN_Q        7
177 #define C_ON_MAJORCOMPACT_Q     8  /* we just did major compaction on this segment */
178 #define C_ON_BAD_Q              9
179 #define C_ON_SWAPIO_Q          10
180 
181 
182 struct c_segment {
183 	lck_mtx_t       c_lock;
184 	queue_chain_t   c_age_list;  /* chain of the main queue this c_segment is in */
185 	queue_chain_t   c_list;      /* chain of c_minor_list_head, if c_on_minorcompact_q==1 */
186 
187 #if CONFIG_FREEZE
188 	queue_chain_t   c_task_list_next_cseg;
189 	task_t          c_task_owner;
190 #endif /* CONFIG_FREEZE */
191 
192 #define C_SEG_MAX_LIMIT         (UINT_MAX)       /* this needs to track the size of c_mysegno */
193 	uint32_t        c_mysegno;  /* my index in c_segments */
194 
195 	uint32_t        c_creation_ts;  /* time filling the segment has finished, used for checking if segment reached ripe age */
196 	uint64_t        c_generation_id;  /* a unique id of a single lifetime of a segment */
197 
198 	int32_t         c_bytes_used;
199 	int32_t         c_bytes_unused;
200 	uint32_t        c_slots_used;
201 
202 	uint16_t        c_firstemptyslot;  /* index of lowest empty slot. used for instance in minor compaction to not have to start from 0 */
203 	uint16_t        c_nextslot;        /* index of the next available slot in either c_slot_fixed_array or c_slot_var_array */
204 	uint32_t        c_nextoffset;      /* next available position in the buffer space pointed by c_store.c_buffer */
205 	uint32_t        c_populated_offset; /* how much of the segment is populated from it's beginning */
206 	/* c_nextoffset and c_populated_offset count ints, not bytes
207 	 * Invariants: - (c_nextoffset <= c_populated_offset) always
208 	 *             - c_nextoffset is rounded to WKDM alignment
209 	 *             - c_populated_offset is in quanta of PAGE_SIZE/sizeof(int) */
210 
211 	union {
212 		int32_t *c_buffer;
213 		uint64_t c_swap_handle;  /* this is populated if C_SEG_IS_ONDISK()  */
214 	} c_store;
215 
216 #if     VALIDATE_C_SEGMENTS
217 	uint32_t        c_was_minor_compacted;
218 	uint32_t        c_was_major_compacted;
219 	uint32_t        c_was_major_donor;
220 #endif
221 #if CHECKSUM_THE_SWAP
222 	unsigned int    cseg_hash;
223 	unsigned int    cseg_swap_size;
224 #endif /* CHECKSUM_THE_SWAP */
225 
226 	thread_t        c_busy_for_thread;
227 	uint32_t        c_agedin_ts;  /* time the seg got to age_q after being swapped in. used for stats*/
228 	uint32_t        c_swappedin_ts;
229 	bool            c_swappedin;
230 #if TRACK_C_SEGMENT_UTILIZATION
231 	uint32_t        c_decompressions_since_swapin;
232 #endif /* TRACK_C_SEGMENT_UTILIZATION */
233 	/*
234 	 * Do not pull c_swappedin above into the bitfield below.
235 	 * We update it without always taking the segment
236 	 * lock and rely on the segment being busy instead.
237 	 * The bitfield needs the segment lock. So updating
238 	 * this state, if in the bitfield, without the lock
239 	 * will race with the updates to the other fields and
240 	 * result in a mess.
241 	 */
242 	uint32_t        c_busy:1,
243 	    c_busy_swapping:1,
244 	    c_wanted:1,
245 	    c_on_minorcompact_q:1,              /* can also be on the age_q, the majorcompact_q or the swappedin_q */
246 
247 	    c_state:4,                          /* what state is the segment in which dictates which q to find it on */
248 	    c_overage_swap:1,
249 	    c_has_donated_pages:1,
250 #if CONFIG_FREEZE
251 	    c_has_freezer_pages:1,
252 	    c_reserved:21;
253 #else /* CONFIG_FREEZE */
254 	c_reserved:22;
255 #endif /* CONFIG_FREEZE */
256 
257 	int             c_slot_var_array_len;  /* length of the allocated c_slot_var_array */
258 	struct  c_slot  *c_slot_var_array;     /* see C_SEG_SLOT_FROM_INDEX() */
259 	struct  c_slot  c_slot_fixed_array[0];
260 };
261 
262 /*
263  * the pager holds a buffer of this 32 bit sized object, one for each page in the vm_object,
264  * to refer to a specific slot in a specific segment in the compressor
265  */
266 struct  c_slot_mapping {
267 #if !CONFIG_TRACK_UNMODIFIED_ANON_PAGES
268 	uint32_t        s_cseg:22,      /* segment number + 1 */
269 	    s_cindx:10;                 /* index of slot in the segment, see also C_SLOT_MAX_INDEX */
270 	/* in the case of a single-value (sv) page, s_cseg==C_SV_CSEG_ID and s_cindx is the
271 	 * index into c_segment_sv_hash_table
272 	 */
273 #else /* !CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
274 	uint32_t        s_cseg:21,      /* segment number + 1 */
275 	    s_cindx:10,                 /* index in the segment */
276 	    s_uncompressed:1;           /* This bit indicates that the page resides uncompressed in a swapfile.
277 	                                 * This can happen in 2 ways:-
278 	                                 * 1) Page used to be in the compressor, got decompressed, was not
279 	                                 * modified, and so was pushed uncompressed to a different swapfile on disk.
280 	                                 * 2) Page was in its uncompressed form in a swapfile on disk. It got swapped in
281 	                                 * but was not modified. As we are about to reclaim it, we notice that this bit
282 	                                 * is set in its current slot. And so we can safely toss this clean anonymous page
283 	                                 * because its copy exists on disk.
284 	                                 */
285 #endif /* !CONFIG_TRACK_UNMODIFIED_ANON_PAGES */
286 };
287 #define C_SLOT_MAX_INDEX        (1 << 10)
288 
289 typedef struct c_slot_mapping *c_slot_mapping_t;
290 
291 
292 extern  int             c_seg_fixed_array_len;
293 extern  vm_offset_t     c_buffers;
294 extern _Atomic uint64_t c_segment_compressed_bytes;
295 
296 #define C_SEG_BUFFER_ADDRESS(c_segno)   ((c_buffers + ((uint64_t)c_segno * (uint64_t)c_seg_allocsize)))
297 
298 #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]))
299 
300 #define C_SEG_OFFSET_TO_BYTES(off)      ((off) * (int) sizeof(int32_t))
301 #define C_SEG_BYTES_TO_OFFSET(bytes)    ((bytes) / (int) sizeof(int32_t))
302 
303 #define C_SEG_UNUSED_BYTES(cseg)        (cseg->c_bytes_unused + (C_SEG_OFFSET_TO_BYTES(cseg->c_populated_offset - cseg->c_nextoffset)))
304 
305 #ifndef __PLATFORM_WKDM_ALIGNMENT_MASK__
306 #define C_SEG_OFFSET_ALIGNMENT_MASK     0x3ULL
307 #define C_SEG_OFFSET_ALIGNMENT_BOUNDARY 0x4
308 #else
309 #define C_SEG_OFFSET_ALIGNMENT_MASK     __PLATFORM_WKDM_ALIGNMENT_MASK__
310 #define C_SEG_OFFSET_ALIGNMENT_BOUNDARY __PLATFORM_WKDM_ALIGNMENT_BOUNDARY__
311 #endif
312 
313 /* round an offset/size up to the next multiple the wkdm write alignment (64 byte) */
314 #define C_SEG_ROUND_TO_ALIGNMENT(offset) \
315 	(((offset) + C_SEG_OFFSET_ALIGNMENT_MASK) & ~C_SEG_OFFSET_ALIGNMENT_MASK)
316 
317 #define C_SEG_SHOULD_MINORCOMPACT_NOW(cseg)     ((C_SEG_UNUSED_BYTES(cseg) >= (c_seg_bufsize / 4)) ? 1 : 0)
318 
319 /*
320  * the decsion to force a c_seg to be major compacted is based on 2 criteria
321  * 1) is the c_seg buffer almost empty (i.e. we have a chance to merge it with another c_seg)
322  * 2) are there at least a minimum number of slots unoccupied so that we have a chance
323  *    of combining this c_seg with another one.
324  */
325 #define C_SEG_SHOULD_MAJORCOMPACT_NOW(cseg)                                                                                     \
326 	((((cseg->c_bytes_unused + (c_seg_bufsize - C_SEG_OFFSET_TO_BYTES(c_seg->c_nextoffset))) >= (c_seg_bufsize / 8)) &&     \
327 	  ((C_SLOT_MAX_INDEX - cseg->c_slots_used) > (c_seg_bufsize / PAGE_SIZE))) \
328 	? 1 : 0)
329 
330 #define C_SEG_ONDISK_IS_SPARSE(cseg)    ((cseg->c_bytes_used < cseg->c_bytes_unused) ? 1 : 0)
331 #define C_SEG_IS_ONDISK(cseg)           ((cseg->c_state == C_ON_SWAPPEDOUT_Q || cseg->c_state == C_ON_SWAPPEDOUTSPARSE_Q))
332 #define C_SEG_IS_ON_DISK_OR_SOQ(cseg)   ((cseg->c_state == C_ON_SWAPPEDOUT_Q || \
333 	                                  cseg->c_state == C_ON_SWAPPEDOUTSPARSE_Q || \
334 	                                  cseg->c_state == C_ON_SWAPOUT_Q || \
335 	                                  cseg->c_state == C_ON_SWAPIO_Q))
336 
337 
338 #define C_SEG_WAKEUP_DONE(cseg)                         \
339 	MACRO_BEGIN                                     \
340 	assert((cseg)->c_busy);                         \
341 	(cseg)->c_busy = 0;                             \
342 	assert((cseg)->c_busy_for_thread != NULL);      \
343 	(cseg)->c_busy_for_thread = NULL;               \
344 	if ((cseg)->c_wanted) {                         \
345 	        (cseg)->c_wanted = 0;                   \
346 	        thread_wakeup((event_t) (cseg));        \
347 	}                                               \
348 	MACRO_END
349 
350 #define C_SEG_BUSY(cseg)                                \
351 	MACRO_BEGIN                                     \
352 	assert((cseg)->c_busy == 0);                    \
353 	(cseg)->c_busy = 1;                             \
354 	assert((cseg)->c_busy_for_thread == NULL);      \
355 	(cseg)->c_busy_for_thread = current_thread();   \
356 	MACRO_END
357 
358 
359 extern vm_map_t compressor_map;
360 
361 #if DEVELOPMENT || DEBUG
362 extern boolean_t write_protect_c_segs;
363 extern int vm_compressor_test_seg_wp;
364 
365 #define C_SEG_MAKE_WRITEABLE(cseg)                      \
366 	MACRO_BEGIN                                     \
367 	if (write_protect_c_segs) {                     \
368 	        vm_map_protect(compressor_map,                  \
369 	                       (vm_map_offset_t)cseg->c_store.c_buffer,         \
370 	                       (vm_map_offset_t)&cseg->c_store.c_buffer[C_SEG_BYTES_TO_OFFSET(c_seg_allocsize)],\
371 	                       0, VM_PROT_READ | VM_PROT_WRITE);    \
372 	}                               \
373 	MACRO_END
374 
375 #define C_SEG_WRITE_PROTECT(cseg)                       \
376 	MACRO_BEGIN                                     \
377 	if (write_protect_c_segs) {                     \
378 	        vm_map_protect(compressor_map,                  \
379 	                       (vm_map_offset_t)cseg->c_store.c_buffer,         \
380 	                       (vm_map_offset_t)&cseg->c_store.c_buffer[C_SEG_BYTES_TO_OFFSET(c_seg_allocsize)],\
381 	                       0, VM_PROT_READ);                    \
382 	}                                                       \
383 	if (vm_compressor_test_seg_wp) {                                \
384 	        volatile uint32_t vmtstmp = *(volatile uint32_t *)cseg->c_store.c_buffer; \
385 	        *(volatile uint32_t *)cseg->c_store.c_buffer = 0xDEADABCD; \
386 	        (void) vmtstmp;                                         \
387 	}                                                               \
388 	MACRO_END
389 #endif /* DEVELOPMENT || DEBUG */
390 
391 typedef struct c_segment *c_segment_t;
392 typedef struct c_slot   *c_slot_t;
393 
394 void vm_decompressor_lock(void);
395 void vm_decompressor_unlock(void);
396 void vm_compressor_delay_trim(void);
397 void vm_compressor_do_warmup(void);
398 
399 extern kern_return_t    vm_swap_get(c_segment_t, uint64_t, uint64_t);
400 
401 extern uint32_t         c_age_count;
402 extern uint32_t         c_early_swapout_count, c_regular_swapout_count, c_late_swapout_count;
403 extern uint32_t         c_swappedout_count;
404 extern uint32_t         c_swappedout_sparse_count;
405 
406 extern _Atomic uint64_t compressor_bytes_used;
407 extern uint32_t         swapout_target_age;
408 
409 extern uint32_t vm_compressor_minorcompact_threshold_divisor;
410 extern uint32_t vm_compressor_majorcompact_threshold_divisor;
411 extern uint32_t vm_compressor_unthrottle_threshold_divisor;
412 extern uint32_t vm_compressor_catchup_threshold_divisor;
413 
414 extern uint32_t vm_compressor_minorcompact_threshold_divisor_overridden;
415 extern uint32_t vm_compressor_majorcompact_threshold_divisor_overridden;
416 extern uint32_t vm_compressor_unthrottle_threshold_divisor_overridden;
417 extern uint32_t vm_compressor_catchup_threshold_divisor_overridden;
418 
419 struct vm_compressor_kdp_state {
420 	char           *kc_scratch_bufs;
421 	char           *kc_decompressed_pages;
422 	addr64_t       *kc_decompressed_pages_paddr;
423 	ppnum_t        *kc_decompressed_pages_ppnum;
424 	char           *kc_panic_scratch_buf;
425 	char           *kc_panic_decompressed_page;
426 	addr64_t        kc_panic_decompressed_page_paddr;
427 	ppnum_t         kc_panic_decompressed_page_ppnum;
428 };
429 extern struct vm_compressor_kdp_state vm_compressor_kdp_state;
430 
431 extern void kdp_compressor_busy_find_owner(event64_t wait_event, thread_waitinfo_t *waitinfo);
432 extern kern_return_t vm_compressor_kdp_init(void);
433 extern void vm_compressor_kdp_teardown(void);
434 
435 /*
436  * TODO, there may be a minor optimisation opportunity to replace these divisions
437  * with multiplies and shifts
438  *
439  * 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
440  * By multiplying by 9, you get a number ~11% smaller which allows us to have another limit point derived from the same base
441  * 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
442  */
443 
444 #define VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD            (((AVAILABLE_MEMORY) * 10) / (vm_compressor_minorcompact_threshold_divisor ? vm_compressor_minorcompact_threshold_divisor : 10))
445 #define VM_PAGE_COMPRESSOR_SWAP_THRESHOLD               (((AVAILABLE_MEMORY) * 10) / (vm_compressor_majorcompact_threshold_divisor ? vm_compressor_majorcompact_threshold_divisor : 10))
446 
447 #define VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD    (((AVAILABLE_MEMORY) * 10) / (vm_compressor_unthrottle_threshold_divisor ? vm_compressor_unthrottle_threshold_divisor : 10))
448 #define VM_PAGE_COMPRESSOR_SWAP_RETHROTTLE_THRESHOLD    (((AVAILABLE_MEMORY) * 11) / (vm_compressor_unthrottle_threshold_divisor ? vm_compressor_unthrottle_threshold_divisor : 11))
449 
450 #define VM_PAGE_COMPRESSOR_SWAP_HAS_CAUGHTUP_THRESHOLD  (((AVAILABLE_MEMORY) * 11) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 11))
451 #define VM_PAGE_COMPRESSOR_SWAP_CATCHUP_THRESHOLD       (((AVAILABLE_MEMORY) * 10) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 10))
452 #define VM_PAGE_COMPRESSOR_HARD_THROTTLE_THRESHOLD      (((AVAILABLE_MEMORY) * 9) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 9))
453 
454 #if !XNU_TARGET_OS_OSX
455 #define AVAILABLE_NON_COMPRESSED_MIN                    20000
456 #define COMPRESSOR_NEEDS_TO_SWAP()              (((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_THRESHOLD) || \
457 	                                          (AVAILABLE_NON_COMPRESSED_MEMORY < AVAILABLE_NON_COMPRESSED_MIN)) ? 1 : 0)
458 #else /* !XNU_TARGET_OS_OSX */
459 #define COMPRESSOR_NEEDS_TO_SWAP()              ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_THRESHOLD) ? 1 : 0)
460 #endif /* !XNU_TARGET_OS_OSX */
461 
462 #define HARD_THROTTLE_LIMIT_REACHED()           ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_HARD_THROTTLE_THRESHOLD) ? 1 : 0)
463 #define SWAPPER_NEEDS_TO_UNTHROTTLE()           ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD) ? 1 : 0)
464 #define SWAPPER_NEEDS_TO_RETHROTTLE()           ((AVAILABLE_NON_COMPRESSED_MEMORY > VM_PAGE_COMPRESSOR_SWAP_RETHROTTLE_THRESHOLD) ? 1 : 0)
465 #define SWAPPER_NEEDS_TO_CATCHUP()              ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_CATCHUP_THRESHOLD) ? 1 : 0)
466 #define SWAPPER_HAS_CAUGHTUP()                  ((AVAILABLE_NON_COMPRESSED_MEMORY > VM_PAGE_COMPRESSOR_SWAP_HAS_CAUGHTUP_THRESHOLD) ? 1 : 0)
467 
468 
469 #if !XNU_TARGET_OS_OSX
470 #define COMPRESSOR_FREE_RESERVED_LIMIT          28
471 #else /* !XNU_TARGET_OS_OSX */
472 #define COMPRESSOR_FREE_RESERVED_LIMIT          128
473 #endif /* !XNU_TARGET_OS_OSX */
474 
475 #define COMPRESSOR_SCRATCH_BUF_SIZE vm_compressor_get_encode_scratch_size()
476 
477 extern lck_mtx_t c_list_lock_storage;
478 #define          c_list_lock (&c_list_lock_storage)
479 
480 #if DEVELOPMENT || DEBUG
481 extern uint32_t vm_ktrace_enabled;
482 
483 #define VMKDBG(x, ...)          \
484 MACRO_BEGIN                     \
485 if (vm_ktrace_enabled) {        \
486 	KDBG(x, ## __VA_ARGS__);\
487 }                               \
488 MACRO_END
489 
490 extern bool compressor_running_perf_test;
491 extern uint64_t compressor_perf_test_pages_processed;
492 #endif /* DEVELOPMENT || DEBUG */
493 
494 #endif /* MACH_KERNEL_PRIVATE */
495 
496 extern bool vm_swap_low_on_space(void);
497 extern bool vm_swap_out_of_space(void);
498 
499 #define HIBERNATE_FLUSHING_SECS_TO_COMPLETE     120
500 
501 #if DEVELOPMENT || DEBUG
502 int do_cseg_wedge_thread(void);
503 int do_cseg_unwedge_thread(void);
504 #endif /* DEVELOPMENT || DEBUG */
505 
506 #if CONFIG_FREEZE
507 void task_disown_frozen_csegs(task_t owner_task);
508 #endif /* CONFIG_FREEZE */
509 
510 void vm_wake_compactor_swapper(void);
511 extern void             vm_swap_consider_defragmenting(int);
512 void vm_run_compactor(void);
513 void vm_thrashing_jetsam_done(void);
514 
515 uint32_t vm_compression_ratio(void);
516 uint32_t vm_compressor_pool_size(void);
517 uint32_t vm_compressor_fragmentation_level(void);
518 uint32_t vm_compressor_incore_fragmentation_wasted_pages(void);
519 bool vm_compressor_is_thrashing(void);
520 bool vm_compressor_swapout_is_ripe(void);
521 uint32_t vm_compressor_pages_compressed(void);
522 void vm_compressor_process_special_swapped_in_segments(void);
523 
524 #if DEVELOPMENT || DEBUG
525 __enum_closed_decl(vm_c_serialize_add_data_t, uint32_t, {
526 	VM_C_SERIALIZE_DATA_NONE,
527 });
528 kern_return_t vm_compressor_serialize_segment_debug_info(int segno, char *buf, size_t *size, vm_c_serialize_add_data_t with_data);
529 #endif /* DEVELOPMENT || DEBUG */
530 
531 extern bool vm_compressor_low_on_space(void);
532 extern bool vm_compressor_compressed_pages_nearing_limit(void);
533 extern bool vm_compressor_out_of_space(void);
534 
535 
536 #endif /* _VM_VM_COMPRESSOR_XNU_H_ */
537