]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/vm_compressor.h
xnu-3789.31.2.tar.gz
[apple/xnu.git] / osfmk / vm / vm_compressor.h
CommitLineData
39236c6e 1/*
39037602 2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
39236c6e
A
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 <kern/kalloc.h>
30#include <vm/vm_compressor_pager.h>
31#include <vm/vm_kern.h>
32#include <vm/vm_page.h>
33#include <vm/vm_protos.h>
34#include <vm/WKdm_new.h>
35#include <vm/vm_object.h>
39037602 36#include <vm/vm_map.h>
39236c6e
A
37#include <machine/pmap.h>
38#include <kern/locks.h>
39
40#include <sys/kdebug.h>
41
39236c6e
A
42#define C_SEG_OFFSET_BITS 16
43#define C_SEG_BUFSIZE (1024 * 256)
3e170ce0 44#define C_SEG_MAX_PAGES (C_SEG_BUFSIZE / PAGE_SIZE)
39236c6e 45
3e170ce0
A
46#define C_SEG_OFF_LIMIT (C_SEG_BYTES_TO_OFFSET((C_SEG_BUFSIZE - 128)))
47#define C_SEG_ALLOCSIZE (C_SEG_BUFSIZE)
48#define C_SEG_MAX_POPULATE_SIZE (4 * PAGE_SIZE)
39236c6e 49
39037602
A
50#if DEBUG || COMPRESSOR_INTEGRITY_CHECKS
51#define ENABLE_SWAP_CHECKS 1
52#define ENABLE_COMPRESSOR_CHECKS 1
53#else
54#define ENABLE_SWAP_CHECKS 0
55#define ENABLE_COMPRESSOR_CHECKS 0
56#endif
39236c6e 57
39037602
A
58#define CHECKSUM_THE_SWAP ENABLE_SWAP_CHECKS /* Debug swap data */
59#define CHECKSUM_THE_DATA ENABLE_COMPRESSOR_CHECKS /* Debug compressor/decompressor data */
60#define CHECKSUM_THE_COMPRESSED_DATA ENABLE_COMPRESSOR_CHECKS /* Debug compressor/decompressor compressed data */
61#define VALIDATE_C_SEGMENTS ENABLE_COMPRESSOR_CHECKS /* Debug compaction */
3e170ce0
A
62
63#define RECORD_THE_COMPRESSED_DATA 0
64
3e170ce0
A
65struct c_slot {
66 uint64_t c_offset:C_SEG_OFFSET_BITS,
39037602 67 c_size:12,
3e170ce0
A
68 c_packed_ptr:36;
69#if CHECKSUM_THE_DATA
70 unsigned int c_hash_data;
71#endif
72#if CHECKSUM_THE_COMPRESSED_DATA
73 unsigned int c_hash_compressed_data;
74#endif
75
76};
77
78#define C_IS_EMPTY 0
79#define C_IS_FREE 1
80#define C_IS_FILLING 2
81#define C_ON_AGE_Q 3
82#define C_ON_SWAPOUT_Q 4
83#define C_ON_SWAPPEDOUT_Q 5
84#define C_ON_SWAPPEDOUTSPARSE_Q 6
85#define C_ON_SWAPPEDIN_Q 7
86#define C_ON_MAJORCOMPACT_Q 8
87#define C_ON_BAD_Q 9
88
39236c6e
A
89
90struct c_segment {
39236c6e 91 lck_mtx_t c_lock;
39236c6e
A
92 queue_chain_t c_age_list;
93 queue_chain_t c_list;
94
95 uint64_t c_generation_id;
96 int32_t c_bytes_used;
97 int32_t c_bytes_unused;
98
99#define C_SEG_MAX_LIMIT (1 << 19) /* this needs to track the size of c_mysegno */
100 uint32_t c_mysegno:19,
39236c6e
A
101 c_busy:1,
102 c_busy_swapping:1,
103 c_wanted:1,
3e170ce0
A
104 c_on_minorcompact_q:1, /* can also be on the age_q, the majorcompact_q or the swappedin_q */
105
106 c_state:4, /* what state is the segment in which dictates which q to find it on */
107 c_overage_swap:1,
108 c_reserved:4;
109
39236c6e
A
110 uint16_t c_firstemptyslot;
111 uint16_t c_nextslot;
112 uint32_t c_nextoffset;
113 uint32_t c_populated_offset;
114
115 uint32_t c_creation_ts;
116 uint32_t c_swappedin_ts;
117
118 union {
119 int32_t *c_buffer;
120 uint64_t c_swap_handle;
121 } c_store;
122
39236c6e
A
123#if VALIDATE_C_SEGMENTS
124 uint32_t c_was_minor_compacted;
125 uint32_t c_was_major_compacted;
126 uint32_t c_was_major_donor;
127#endif
128#if CHECKSUM_THE_SWAP
129 unsigned int cseg_hash;
130 unsigned int cseg_swap_size;
131#endif /* CHECKSUM_THE_SWAP */
132
fe8ab488
A
133#if MACH_ASSERT
134 thread_t c_busy_for_thread;
135#endif /* MACH_ASSERT */
136
3e170ce0
A
137 int c_slot_var_array_len;
138 struct c_slot *c_slot_var_array;
139 struct c_slot c_slot_fixed_array[0];
39236c6e
A
140};
141
3e170ce0
A
142#define C_SEG_SLOT_VAR_ARRAY_MIN_LEN C_SEG_MAX_PAGES
143
144extern int c_seg_fixed_array_len;
145extern vm_offset_t c_buffers;
146#define C_SEG_BUFFER_ADDRESS(c_segno) ((c_buffers + ((uint64_t)c_segno * (uint64_t)C_SEG_ALLOCSIZE)))
39236c6e 147
3e170ce0 148#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]))
39236c6e
A
149
150#define C_SEG_OFFSET_TO_BYTES(off) ((off) * (int) sizeof(int32_t))
151#define C_SEG_BYTES_TO_OFFSET(bytes) ((bytes) / (int) sizeof(int32_t))
152
153#define C_SEG_UNUSED_BYTES(cseg) (cseg->c_bytes_unused + (C_SEG_OFFSET_TO_BYTES(cseg->c_populated_offset - cseg->c_nextoffset)))
154
155#define C_SEG_OFFSET_ALIGNMENT_MASK 0x3
156
157#define C_SEG_ONDISK_IS_SPARSE(cseg) ((cseg->c_bytes_used < (C_SEG_BUFSIZE / 2)) ? 1 : 0)
3e170ce0
A
158#define C_SEG_SHOULD_MINORCOMPACT(cseg) ((C_SEG_UNUSED_BYTES(cseg) >= (C_SEG_BUFSIZE / 3)) ? 1 : 0)
159#define C_SEG_SHOULD_MAJORCOMPACT(cseg) (((cseg->c_bytes_unused + (C_SEG_BUFSIZE - C_SEG_OFFSET_TO_BYTES(c_seg->c_nextoffset))) >= (C_SEG_BUFSIZE / 8)) ? 1 : 0)
160
161#define C_SEG_IS_ONDISK(cseg) ((cseg->c_state == C_ON_SWAPPEDOUT_Q || cseg->c_state == C_ON_SWAPPEDOUTSPARSE_Q))
162
39236c6e
A
163
164#define C_SEG_WAKEUP_DONE(cseg) \
165 MACRO_BEGIN \
fe8ab488 166 assert((cseg)->c_busy); \
39236c6e 167 (cseg)->c_busy = 0; \
fe8ab488
A
168 assert((cseg)->c_busy_for_thread != NULL); \
169 assert((((cseg)->c_busy_for_thread = NULL), TRUE)); \
39236c6e
A
170 if ((cseg)->c_wanted) { \
171 (cseg)->c_wanted = 0; \
172 thread_wakeup((event_t) (cseg)); \
173 } \
174 MACRO_END
175
fe8ab488
A
176#define C_SEG_BUSY(cseg) \
177 MACRO_BEGIN \
178 assert((cseg)->c_busy == 0); \
179 (cseg)->c_busy = 1; \
180 assert((cseg)->c_busy_for_thread == NULL); \
181 assert((((cseg)->c_busy_for_thread = current_thread()), TRUE)); \
182 MACRO_END
183
184
39037602
A
185#if DEVELOPMENT || DEBUG
186extern vm_map_t compressor_map;
187
188#define C_SEG_MAKE_WRITEABLE(cseg) \
189 MACRO_BEGIN \
190 vm_map_protect(compressor_map, \
191 (vm_map_offset_t)cseg->c_store.c_buffer, \
192 (vm_map_offset_t)&cseg->c_store.c_buffer[C_SEG_BYTES_TO_OFFSET(C_SEG_ALLOCSIZE)],\
193 VM_PROT_READ | VM_PROT_WRITE, \
194 0); \
195 MACRO_END
196
197#define C_SEG_WRITE_PROTECT(cseg) \
198 MACRO_BEGIN \
199 vm_map_protect(compressor_map, \
200 (vm_map_offset_t)cseg->c_store.c_buffer, \
201 (vm_map_offset_t)&cseg->c_store.c_buffer[C_SEG_BYTES_TO_OFFSET(C_SEG_ALLOCSIZE)],\
202 VM_PROT_READ, \
203 0); \
204 MACRO_END
205#endif
39236c6e
A
206
207typedef struct c_segment *c_segment_t;
208typedef struct c_slot *c_slot_t;
209
210uint64_t vm_compressor_total_compressions(void);
211void vm_wake_compactor_swapper(void);
39037602 212void vm_run_compactor(void);
fe8ab488 213void vm_thrashing_jetsam_done(void);
39236c6e 214void vm_consider_waking_compactor_swapper(void);
3e170ce0 215void vm_consider_swapping(void);
39236c6e
A
216void vm_compressor_flush(void);
217void c_seg_free(c_segment_t);
218void c_seg_free_locked(c_segment_t);
219void c_seg_insert_into_age_q(c_segment_t);
39037602 220void c_seg_need_delayed_compaction(c_segment_t, boolean_t);
39236c6e
A
221
222void vm_decompressor_lock(void);
223void vm_decompressor_unlock(void);
224
8a3053a0 225void vm_compressor_delay_trim(void);
39236c6e
A
226void vm_compressor_do_warmup(void);
227void vm_compressor_record_warmup_start(void);
228void vm_compressor_record_warmup_end(void);
229
fe8ab488 230int vm_wants_task_throttled(task_t);
39236c6e 231
39037602 232extern void vm_compaction_swapper_do_init(void);
fe8ab488 233extern void vm_compressor_swap_init(void);
39236c6e
A
234extern void vm_compressor_init_locks(void);
235extern lck_rw_t c_master_lock;
236
fe8ab488 237#if ENCRYPTED_SWAP
39236c6e 238extern void vm_swap_decrypt(c_segment_t);
fe8ab488 239#endif /* ENCRYPTED_SWAP */
39236c6e 240
fe8ab488 241extern int vm_swap_low_on_space(void);
39037602 242extern kern_return_t vm_swap_get(c_segment_t, uint64_t, uint64_t);
39236c6e
A
243extern void vm_swap_free(uint64_t);
244extern void vm_swap_consider_defragmenting(void);
245
39037602
A
246extern void c_seg_swapin_requeue(c_segment_t, boolean_t, boolean_t, boolean_t);
247extern int c_seg_swapin(c_segment_t, boolean_t, boolean_t);
39236c6e
A
248extern void c_seg_wait_on_busy(c_segment_t);
249extern void c_seg_trim_tail(c_segment_t);
3e170ce0 250extern void c_seg_switch_state(c_segment_t, int, boolean_t);
39236c6e
A
251
252extern boolean_t fastwake_recording_in_progress;
39037602 253extern int compaction_swapper_inited;
39236c6e
A
254extern int compaction_swapper_running;
255extern uint64_t vm_swap_put_failures;
256
3e170ce0
A
257extern int c_overage_swapped_count;
258extern int c_overage_swapped_limit;
259
39236c6e
A
260extern queue_head_t c_minor_list_head;
261extern queue_head_t c_age_list_head;
262extern queue_head_t c_swapout_list_head;
263extern queue_head_t c_swappedout_list_head;
264extern queue_head_t c_swappedout_sparse_list_head;
265
266extern uint32_t c_age_count;
267extern uint32_t c_swapout_count;
268extern uint32_t c_swappedout_count;
269extern uint32_t c_swappedout_sparse_count;
270
271extern int64_t compressor_bytes_used;
272extern uint64_t first_c_segment_to_warm_generation_id;
273extern uint64_t last_c_segment_to_warm_generation_id;
274extern boolean_t hibernate_flushing;
275extern boolean_t hibernate_no_swapspace;
39037602 276extern boolean_t hibernate_in_progress_with_pinned_swap;
39236c6e
A
277extern uint32_t swapout_target_age;
278
279extern void c_seg_insert_into_q(queue_head_t *, c_segment_t);
280
281extern uint32_t vm_compressor_minorcompact_threshold_divisor;
282extern uint32_t vm_compressor_majorcompact_threshold_divisor;
283extern uint32_t vm_compressor_unthrottle_threshold_divisor;
284extern uint32_t vm_compressor_catchup_threshold_divisor;
fe8ab488 285extern uint64_t vm_compressor_compute_elapsed_msecs(clock_sec_t, clock_nsec_t, clock_sec_t, clock_nsec_t);
39236c6e
A
286
287#define PAGE_REPLACEMENT_DISALLOWED(enable) (enable == TRUE ? lck_rw_lock_shared(&c_master_lock) : lck_rw_done(&c_master_lock))
288#define PAGE_REPLACEMENT_ALLOWED(enable) (enable == TRUE ? lck_rw_lock_exclusive(&c_master_lock) : lck_rw_done(&c_master_lock))
289
290
291#define AVAILABLE_NON_COMPRESSED_MEMORY (vm_page_active_count + vm_page_inactive_count + vm_page_free_count + vm_page_speculative_count)
292#define AVAILABLE_MEMORY (AVAILABLE_NON_COMPRESSED_MEMORY + VM_PAGE_COMPRESSOR_COUNT)
39037602
A
293/* TODO, there may be a minor optimisation opportunity to replace these divisions
294 * with multiplies and shifts
295 */
39236c6e
A
296
297#define VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD (((AVAILABLE_MEMORY) * 10) / (vm_compressor_minorcompact_threshold_divisor ? vm_compressor_minorcompact_threshold_divisor : 1))
298#define VM_PAGE_COMPRESSOR_SWAP_THRESHOLD (((AVAILABLE_MEMORY) * 10) / (vm_compressor_majorcompact_threshold_divisor ? vm_compressor_majorcompact_threshold_divisor : 1))
299#define VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD (((AVAILABLE_MEMORY) * 10) / (vm_compressor_unthrottle_threshold_divisor ? vm_compressor_unthrottle_threshold_divisor : 1))
300#define VM_PAGE_COMPRESSOR_SWAP_CATCHUP_THRESHOLD (((AVAILABLE_MEMORY) * 10) / (vm_compressor_catchup_threshold_divisor ? vm_compressor_catchup_threshold_divisor : 1))
301
302#define COMPRESSOR_NEEDS_TO_SWAP() ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_THRESHOLD) ? 1 : 0)
303
304#define VM_PAGEOUT_SCAN_NEEDS_TO_THROTTLE() \
3e170ce0 305 (vm_compressor_mode == VM_PAGER_COMPRESSOR_WITH_SWAP && \
39236c6e
A
306 ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_CATCHUP_THRESHOLD) ? 1 : 0))
307#define HARD_THROTTLE_LIMIT_REACHED() ((AVAILABLE_NON_COMPRESSED_MEMORY < (VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD) / 2) ? 1 : 0)
308#define SWAPPER_NEEDS_TO_UNTHROTTLE() ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD) ? 1 : 0)
309#define COMPRESSOR_NEEDS_TO_MINOR_COMPACT() ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD) ? 1 : 0)
39236c6e 310
39236c6e 311
3e170ce0 312#define COMPRESSOR_FREE_RESERVED_LIMIT 128
39236c6e 313
39037602
A
314uint32_t vm_compressor_get_encode_scratch_size(void);
315uint32_t vm_compressor_get_decode_scratch_size(void);
39236c6e 316
39037602 317#define COMPRESSOR_SCRATCH_BUF_SIZE vm_compressor_get_encode_scratch_size()
39236c6e 318
3e170ce0
A
319#if RECORD_THE_COMPRESSED_DATA
320extern void c_compressed_record_init(void);
321extern void c_compressed_record_write(char *, int);
322#endif
323
39236c6e 324extern lck_mtx_t *c_list_lock;