2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <vm/vm_compressor.h>
31 #if CONFIG_PHANTOM_CACHE
32 #include <vm/vm_phantom_cache.h>
35 #include <vm/vm_map.h>
36 #include <vm/vm_pageout.h>
37 #include <vm/memory_object.h>
38 #include <vm/vm_compressor_algorithms.h>
39 #include <vm/vm_fault.h>
40 #include <vm/vm_protos.h>
41 #include <mach/mach_host.h> /* for host_info() */
42 #include <kern/ledger.h>
43 #include <kern/policy_internal.h>
44 #include <kern/thread_group.h>
45 #include <san/kasan.h>
48 #include <i386/misc_protos.h>
51 #include <IOKit/IOHibernatePrivate.h>
53 extern boolean_t vm_darkwake_mode
;
55 #if POPCOUNT_THE_COMPRESSED_DATA
56 boolean_t popcount_c_segs
= TRUE
;
58 static inline uint32_t
59 vmc_pop(uintptr_t ins
, int sz
)
63 if (__probable(popcount_c_segs
== FALSE
)) {
69 uint64_t *ins64
= (uint64_t *) ins
;
70 uint64_t *ins642
= (uint64_t *) (ins
+ 8);
71 rv1
= __builtin_popcountll(*ins64
);
72 rv2
= __builtin_popcountll(*ins642
);
79 uint32_t *ins32
= (uint32_t *) ins
;
80 rv
+= __builtin_popcount(*ins32
);
86 char *ins8
= (char *)ins
;
87 rv
+= __builtin_popcount(*ins8
);
95 #if VALIDATE_C_SEGMENTS
96 boolean_t validate_c_segs
= TRUE
;
99 * vm_compressor_mode has a heirarchy of control to set its value.
100 * boot-args are checked first, then device-tree, and finally
101 * the default value that is defined below. See vm_fault_init() for
102 * the boot-arg & device-tree code.
108 int vm_compressor_mode
= VM_PAGER_FREEZER_DEFAULT
;
110 void *freezer_chead
; /* The chead used to track c_segs allocated for the exclusive use of holding just one task's compressed memory.*/
111 char *freezer_compressor_scratch_buf
= NULL
;
113 extern int c_freezer_swapout_page_count
; /* This count keeps track of the # of compressed pages holding just one task's compressed memory on the swapout queue. This count is used during each freeze i.e. on a per-task basis.*/
115 #else /* CONFIG_FREEZE */
116 int vm_compressor_mode
= VM_PAGER_NOT_CONFIGURED
;
117 #endif /* CONFIG_FREEZE */
121 #else /* CONFIG_EMBEDDED */
122 int vm_compressor_mode
= VM_PAGER_COMPRESSOR_WITH_SWAP
;
125 #endif /* CONFIG_EMBEDDED */
127 int vm_compressor_is_active
= 0;
128 int vm_compression_limit
= 0;
129 int vm_compressor_available
= 0;
131 extern void vm_pageout_io_throttle(void);
133 #if CHECKSUM_THE_DATA || CHECKSUM_THE_SWAP || CHECKSUM_THE_COMPRESSED_DATA
134 extern unsigned int hash_string(char *cp
, int len
);
135 static unsigned int vmc_hash(char *, int);
136 boolean_t checksum_c_segs
= TRUE
;
139 vmc_hash(char *cp
, int len
)
141 if (__probable(checksum_c_segs
== FALSE
)) {
144 return hash_string(cp
, len
);
148 #define UNPACK_C_SIZE(cs) ((cs->c_size == (PAGE_SIZE-1)) ? PAGE_SIZE : cs->c_size)
149 #define PACK_C_SIZE(cs, size) (cs->c_size = ((size == PAGE_SIZE) ? PAGE_SIZE - 1 : size))
152 struct c_sv_hash_entry
{
155 uint32_t c_sv_he_ref
;
156 uint32_t c_sv_he_data
;
158 uint64_t c_sv_he_record
;
162 #define he_ref c_sv_he_un.c_sv_he.c_sv_he_ref
163 #define he_data c_sv_he_un.c_sv_he.c_sv_he_data
164 #define he_record c_sv_he_un.c_sv_he_record
166 #define C_SV_HASH_MAX_MISS 32
167 #define C_SV_HASH_SIZE ((1 << 10))
168 #define C_SV_HASH_MASK ((1 << 10) - 1)
169 #define C_SV_CSEG_ID ((1 << 22) - 1)
179 #define C_SLOT_PACK_PTR(ptr) (((uintptr_t)ptr - (uintptr_t) KERNEL_PMAP_HEAP_RANGE_START) >> 2)
180 #define C_SLOT_UNPACK_PTR(cslot) ((uintptr_t)(cslot->c_packed_ptr << 2) + (uintptr_t) KERNEL_PMAP_HEAP_RANGE_START)
183 uint32_t c_segment_count
= 0;
184 uint32_t c_segment_count_max
= 0;
186 uint64_t c_generation_id
= 0;
187 uint64_t c_generation_id_flush_barrier
;
190 #define HIBERNATE_FLUSHING_SECS_TO_COMPLETE 120
192 boolean_t hibernate_no_swapspace
= FALSE
;
193 clock_sec_t hibernate_flushing_deadline
= 0;
196 #if RECORD_THE_COMPRESSED_DATA
197 char *c_compressed_record_sbuf
;
198 char *c_compressed_record_ebuf
;
199 char *c_compressed_record_cptr
;
203 queue_head_t c_age_list_head
;
204 queue_head_t c_swappedin_list_head
;
205 queue_head_t c_swapout_list_head
;
206 queue_head_t c_swapio_list_head
;
207 queue_head_t c_swappedout_list_head
;
208 queue_head_t c_swappedout_sparse_list_head
;
209 queue_head_t c_major_list_head
;
210 queue_head_t c_filling_list_head
;
211 queue_head_t c_bad_list_head
;
213 uint32_t c_age_count
= 0;
214 uint32_t c_swappedin_count
= 0;
215 uint32_t c_swapout_count
= 0;
216 uint32_t c_swapio_count
= 0;
217 uint32_t c_swappedout_count
= 0;
218 uint32_t c_swappedout_sparse_count
= 0;
219 uint32_t c_major_count
= 0;
220 uint32_t c_filling_count
= 0;
221 uint32_t c_empty_count
= 0;
222 uint32_t c_bad_count
= 0;
225 queue_head_t c_minor_list_head
;
226 uint32_t c_minor_count
= 0;
228 int c_overage_swapped_count
= 0;
229 int c_overage_swapped_limit
= 0;
231 int c_seg_fixed_array_len
;
232 union c_segu
*c_segments
;
233 vm_offset_t c_buffers
;
234 vm_size_t c_buffers_size
;
235 caddr_t c_segments_next_page
;
236 boolean_t c_segments_busy
;
237 uint32_t c_segments_available
;
238 uint32_t c_segments_limit
;
239 uint32_t c_segments_nearing_limit
;
241 uint32_t c_segment_svp_in_hash
;
242 uint32_t c_segment_svp_hash_succeeded
;
243 uint32_t c_segment_svp_hash_failed
;
244 uint32_t c_segment_svp_zero_compressions
;
245 uint32_t c_segment_svp_nonzero_compressions
;
246 uint32_t c_segment_svp_zero_decompressions
;
247 uint32_t c_segment_svp_nonzero_decompressions
;
249 uint32_t c_segment_noncompressible_pages
;
251 uint32_t c_segment_pages_compressed
;
252 uint32_t c_segment_pages_compressed_limit
;
253 uint32_t c_segment_pages_compressed_nearing_limit
;
254 uint32_t c_free_segno_head
= (uint32_t)-1;
256 uint32_t vm_compressor_minorcompact_threshold_divisor
= 10;
257 uint32_t vm_compressor_majorcompact_threshold_divisor
= 10;
258 uint32_t vm_compressor_unthrottle_threshold_divisor
= 10;
259 uint32_t vm_compressor_catchup_threshold_divisor
= 10;
261 uint32_t vm_compressor_minorcompact_threshold_divisor_overridden
= 0;
262 uint32_t vm_compressor_majorcompact_threshold_divisor_overridden
= 0;
263 uint32_t vm_compressor_unthrottle_threshold_divisor_overridden
= 0;
264 uint32_t vm_compressor_catchup_threshold_divisor_overridden
= 0;
266 #define C_SEGMENTS_PER_PAGE (PAGE_SIZE / sizeof(union c_segu))
269 lck_grp_attr_t vm_compressor_lck_grp_attr
;
270 lck_attr_t vm_compressor_lck_attr
;
271 lck_grp_t vm_compressor_lck_grp
;
272 lck_mtx_t
*c_list_lock
;
273 lck_rw_t c_master_lock
;
274 boolean_t decompressions_blocked
= FALSE
;
276 zone_t compressor_segment_zone
;
277 int c_compressor_swap_trigger
= 0;
279 uint32_t compressor_cpus
;
280 char *compressor_scratch_bufs
;
281 char *kdp_compressor_scratch_buf
;
282 char *kdp_compressor_decompressed_page
;
283 addr64_t kdp_compressor_decompressed_page_paddr
;
284 ppnum_t kdp_compressor_decompressed_page_ppnum
;
286 clock_sec_t start_of_sample_period_sec
= 0;
287 clock_nsec_t start_of_sample_period_nsec
= 0;
288 clock_sec_t start_of_eval_period_sec
= 0;
289 clock_nsec_t start_of_eval_period_nsec
= 0;
290 uint32_t sample_period_decompression_count
= 0;
291 uint32_t sample_period_compression_count
= 0;
292 uint32_t last_eval_decompression_count
= 0;
293 uint32_t last_eval_compression_count
= 0;
295 #define DECOMPRESSION_SAMPLE_MAX_AGE (60 * 30)
297 boolean_t vm_swapout_ripe_segments
= FALSE
;
298 uint32_t vm_ripe_target_age
= (60 * 60 * 48);
300 uint32_t swapout_target_age
= 0;
301 uint32_t age_of_decompressions_during_sample_period
[DECOMPRESSION_SAMPLE_MAX_AGE
];
302 uint32_t overage_decompressions_during_sample_period
= 0;
305 void do_fastwake_warmup(queue_head_t
*, boolean_t
);
306 boolean_t fastwake_warmup
= FALSE
;
307 boolean_t fastwake_recording_in_progress
= FALSE
;
308 clock_sec_t dont_trim_until_ts
= 0;
310 uint64_t c_segment_warmup_count
;
311 uint64_t first_c_segment_to_warm_generation_id
= 0;
312 uint64_t last_c_segment_to_warm_generation_id
= 0;
313 boolean_t hibernate_flushing
= FALSE
;
315 int64_t c_segment_input_bytes
__attribute__((aligned(8))) = 0;
316 int64_t c_segment_compressed_bytes
__attribute__((aligned(8))) = 0;
317 int64_t compressor_bytes_used
__attribute__((aligned(8))) = 0;
320 struct c_sv_hash_entry c_segment_sv_hash_table
[C_SV_HASH_SIZE
] __attribute__ ((aligned(8)));
322 static boolean_t
compressor_needs_to_swap(void);
323 static void vm_compressor_swap_trigger_thread(void);
324 static void vm_compressor_do_delayed_compactions(boolean_t
);
325 static void vm_compressor_compact_and_swap(boolean_t
);
326 static void vm_compressor_age_swapped_in_segments(boolean_t
);
329 static void vm_compressor_take_paging_space_action(void);
332 void compute_swapout_target_age(void);
334 boolean_t
c_seg_major_compact(c_segment_t
, c_segment_t
);
335 boolean_t
c_seg_major_compact_ok(c_segment_t
, c_segment_t
);
337 int c_seg_minor_compaction_and_unlock(c_segment_t
, boolean_t
);
338 int c_seg_do_minor_compaction_and_unlock(c_segment_t
, boolean_t
, boolean_t
, boolean_t
);
339 void c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
);
341 void c_seg_move_to_sparse_list(c_segment_t
);
342 void c_seg_insert_into_q(queue_head_t
*, c_segment_t
);
344 uint64_t vm_available_memory(void);
345 uint64_t vm_compressor_pages_compressed(void);
348 * indicate the need to do a major compaction if
349 * the overall set of in-use compression segments
350 * becomes sparse... on systems that support pressure
351 * driven swapping, this will also cause swapouts to
354 static inline boolean_t
355 vm_compressor_needs_to_major_compact()
357 uint32_t incore_seg_count
;
359 incore_seg_count
= c_segment_count
- c_swappedout_count
- c_swappedout_sparse_count
;
361 if ((c_segment_count
>= (c_segments_nearing_limit
/ 8)) &&
362 ((incore_seg_count
* C_SEG_MAX_PAGES
) - VM_PAGE_COMPRESSOR_COUNT
) >
363 ((incore_seg_count
/ 8) * C_SEG_MAX_PAGES
)) {
371 vm_available_memory(void)
373 return ((uint64_t)AVAILABLE_NON_COMPRESSED_MEMORY
) * PAGE_SIZE_64
;
378 vm_compressor_pages_compressed(void)
380 return c_segment_pages_compressed
* PAGE_SIZE_64
;
385 vm_compressor_low_on_space(void)
387 if ((c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) ||
388 (c_segment_count
> c_segments_nearing_limit
)) {
397 vm_compressor_out_of_space(void)
399 if ((c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) ||
400 (c_segment_count
>= c_segments_limit
)) {
409 vm_wants_task_throttled(task_t task
)
411 if (task
== kernel_task
) {
415 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
416 if ((vm_compressor_low_on_space() || HARD_THROTTLE_LIMIT_REACHED()) &&
417 (unsigned int)pmap_compressed(task
->map
->pmap
) > (c_segment_pages_compressed
/ 4)) {
425 #if DEVELOPMENT || DEBUG
426 boolean_t kill_on_no_paging_space
= FALSE
; /* On compressor/swap exhaustion, kill the largest process regardless of
427 * its chosen process policy. Controlled by a boot-arg of the same name. */
428 #endif /* DEVELOPMENT || DEBUG */
432 static uint32_t no_paging_space_action_in_progress
= 0;
433 extern void memorystatus_send_low_swap_note(void);
436 vm_compressor_take_paging_space_action(void)
438 if (no_paging_space_action_in_progress
== 0) {
439 if (OSCompareAndSwap(0, 1, (UInt32
*)&no_paging_space_action_in_progress
)) {
440 if (no_paging_space_action()) {
441 #if DEVELOPMENT || DEBUG
442 if (kill_on_no_paging_space
== TRUE
) {
444 * Since we are choosing to always kill a process, we don't need the
445 * "out of application memory" dialog box in this mode. And, hence we won't
448 no_paging_space_action_in_progress
= 0;
451 #endif /* DEVELOPMENT || DEBUG */
452 memorystatus_send_low_swap_note();
455 no_paging_space_action_in_progress
= 0;
459 #endif /* !CONFIG_EMBEDDED */
463 vm_compressor_init_locks(void)
465 lck_grp_attr_setdefault(&vm_compressor_lck_grp_attr
);
466 lck_grp_init(&vm_compressor_lck_grp
, "vm_compressor", &vm_compressor_lck_grp_attr
);
467 lck_attr_setdefault(&vm_compressor_lck_attr
);
469 lck_rw_init(&c_master_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
474 vm_decompressor_lock(void)
476 PAGE_REPLACEMENT_ALLOWED(TRUE
);
478 decompressions_blocked
= TRUE
;
480 PAGE_REPLACEMENT_ALLOWED(FALSE
);
484 vm_decompressor_unlock(void)
486 PAGE_REPLACEMENT_ALLOWED(TRUE
);
488 decompressions_blocked
= FALSE
;
490 PAGE_REPLACEMENT_ALLOWED(FALSE
);
492 thread_wakeup((event_t
)&decompressions_blocked
);
496 cslot_copy(c_slot_t cdst
, c_slot_t csrc
)
498 #if CHECKSUM_THE_DATA
499 cdst
->c_hash_data
= csrc
->c_hash_data
;
501 #if CHECKSUM_THE_COMPRESSED_DATA
502 cdst
->c_hash_compressed_data
= csrc
->c_hash_compressed_data
;
504 #if POPCOUNT_THE_COMPRESSED_DATA
505 cdst
->c_pop_cdata
= csrc
->c_pop_cdata
;
507 cdst
->c_size
= csrc
->c_size
;
508 cdst
->c_packed_ptr
= csrc
->c_packed_ptr
;
509 #if defined(__arm__) || defined(__arm64__)
510 cdst
->c_codec
= csrc
->c_codec
;
514 vm_map_t compressor_map
;
515 uint64_t compressor_pool_max_size
;
516 uint64_t compressor_pool_size
;
517 uint32_t compressor_pool_multiplier
;
519 #if DEVELOPMENT || DEBUG
521 * Compressor segments are write-protected in development/debug
522 * kernels to help debug memory corruption.
523 * In cases where performance is a concern, this can be disabled
524 * via the boot-arg "-disable_cseg_write_protection".
526 boolean_t write_protect_c_segs
= TRUE
;
527 int vm_compressor_test_seg_wp
;
528 uint32_t vm_ktrace_enabled
;
529 #endif /* DEVELOPMENT || DEBUG */
532 vm_compressor_init(void)
535 struct c_slot cs_dummy
;
536 c_slot_t cs
= &cs_dummy
;
537 int c_segment_min_size
;
538 int c_segment_padded_size
;
540 kern_return_t retval
= KERN_SUCCESS
;
541 vm_offset_t start_addr
= 0;
542 vm_size_t c_segments_arr_size
= 0, compressor_submap_size
= 0;
543 vm_map_kernel_flags_t vmk_flags
;
544 #if RECORD_THE_COMPRESSED_DATA
545 vm_size_t c_compressed_record_sbuf_size
= 0;
546 #endif /* RECORD_THE_COMPRESSED_DATA */
548 #if DEVELOPMENT || DEBUG
549 char bootarg_name
[32];
550 if (PE_parse_boot_argn("-kill_on_no_paging_space", bootarg_name
, sizeof(bootarg_name
))) {
551 kill_on_no_paging_space
= TRUE
;
553 if (PE_parse_boot_argn("-disable_cseg_write_protection", bootarg_name
, sizeof(bootarg_name
))) {
554 write_protect_c_segs
= FALSE
;
557 PE_parse_boot_argn("vm_compressor_validation", &vmcval
, sizeof(vmcval
));
559 if (kern_feature_override(KF_COMPRSV_OVRD
)) {
563 #if POPCOUNT_THE_COMPRESSED_DATA
564 popcount_c_segs
= FALSE
;
566 #if CHECKSUM_THE_DATA || CHECKSUM_THE_COMPRESSED_DATA
567 checksum_c_segs
= FALSE
;
569 #if VALIDATE_C_SEGMENTS
570 validate_c_segs
= FALSE
;
572 write_protect_c_segs
= FALSE
;
574 #endif /* DEVELOPMENT || DEBUG */
577 * ensure that any pointer that gets created from
578 * the vm_page zone can be packed properly
580 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_min_address
);
582 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_min_address
) {
583 panic("C_SLOT_UNPACK_PTR failed on zone_map_min_address - %p", (void *)zone_map_min_address
);
586 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_max_address
);
588 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_max_address
) {
589 panic("C_SLOT_UNPACK_PTR failed on zone_map_max_address - %p", (void *)zone_map_max_address
);
593 assert((C_SEGMENTS_PER_PAGE
* sizeof(union c_segu
)) == PAGE_SIZE
);
595 PE_parse_boot_argn("vm_compression_limit", &vm_compression_limit
, sizeof(vm_compression_limit
));
597 #ifdef CONFIG_EMBEDDED
598 vm_compressor_minorcompact_threshold_divisor
= 20;
599 vm_compressor_majorcompact_threshold_divisor
= 30;
600 vm_compressor_unthrottle_threshold_divisor
= 40;
601 vm_compressor_catchup_threshold_divisor
= 60;
603 if (max_mem
<= (3ULL * 1024ULL * 1024ULL * 1024ULL)) {
604 vm_compressor_minorcompact_threshold_divisor
= 11;
605 vm_compressor_majorcompact_threshold_divisor
= 13;
606 vm_compressor_unthrottle_threshold_divisor
= 20;
607 vm_compressor_catchup_threshold_divisor
= 35;
609 vm_compressor_minorcompact_threshold_divisor
= 20;
610 vm_compressor_majorcompact_threshold_divisor
= 25;
611 vm_compressor_unthrottle_threshold_divisor
= 35;
612 vm_compressor_catchup_threshold_divisor
= 50;
616 * vm_page_init_lck_grp is now responsible for calling vm_compressor_init_locks
617 * c_master_lock needs to be available early so that "vm_page_find_contiguous" can
618 * use PAGE_REPLACEMENT_ALLOWED to coordinate with the compressor.
621 c_list_lock
= lck_mtx_alloc_init(&vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
623 queue_init(&c_bad_list_head
);
624 queue_init(&c_age_list_head
);
625 queue_init(&c_minor_list_head
);
626 queue_init(&c_major_list_head
);
627 queue_init(&c_filling_list_head
);
628 queue_init(&c_swapout_list_head
);
629 queue_init(&c_swapio_list_head
);
630 queue_init(&c_swappedin_list_head
);
631 queue_init(&c_swappedout_list_head
);
632 queue_init(&c_swappedout_sparse_list_head
);
634 c_free_segno_head
= -1;
635 c_segments_available
= 0;
637 if (vm_compression_limit
) {
638 compressor_pool_size
= (uint64_t)vm_compression_limit
* PAGE_SIZE_64
;
641 compressor_pool_max_size
= C_SEG_MAX_LIMIT
;
642 compressor_pool_max_size
*= C_SEG_BUFSIZE
;
644 #if defined(__x86_64__)
646 if (vm_compression_limit
== 0) {
647 if (max_mem
<= (4ULL * 1024ULL * 1024ULL * 1024ULL)) {
648 compressor_pool_size
= 16ULL * max_mem
;
649 } else if (max_mem
<= (8ULL * 1024ULL * 1024ULL * 1024ULL)) {
650 compressor_pool_size
= 8ULL * max_mem
;
651 } else if (max_mem
<= (32ULL * 1024ULL * 1024ULL * 1024ULL)) {
652 compressor_pool_size
= 4ULL * max_mem
;
654 compressor_pool_size
= 2ULL * max_mem
;
657 if (max_mem
<= (8ULL * 1024ULL * 1024ULL * 1024ULL)) {
658 compressor_pool_multiplier
= 1;
659 } else if (max_mem
<= (32ULL * 1024ULL * 1024ULL * 1024ULL)) {
660 compressor_pool_multiplier
= 2;
662 compressor_pool_multiplier
= 4;
665 #elif defined(__arm__)
667 #define VM_RESERVE_SIZE (1024 * 1024 * 256)
668 #define MAX_COMPRESSOR_POOL_SIZE (1024 * 1024 * 450)
670 if (compressor_pool_max_size
> MAX_COMPRESSOR_POOL_SIZE
) {
671 compressor_pool_max_size
= MAX_COMPRESSOR_POOL_SIZE
;
674 if (vm_compression_limit
== 0) {
675 compressor_pool_size
= ((kernel_map
->max_offset
- kernel_map
->min_offset
) - kernel_map
->size
) - VM_RESERVE_SIZE
;
677 compressor_pool_multiplier
= 1;
679 if (compressor_pool_max_size
> max_mem
) {
680 compressor_pool_max_size
= max_mem
;
683 if (vm_compression_limit
== 0) {
684 compressor_pool_size
= max_mem
;
686 compressor_pool_multiplier
= 1;
688 if (compressor_pool_size
> compressor_pool_max_size
) {
689 compressor_pool_size
= compressor_pool_max_size
;
693 c_segments_limit
= (uint32_t)(compressor_pool_size
/ (vm_size_t
)(C_SEG_ALLOCSIZE
));
694 c_segments_nearing_limit
= (uint32_t)(((uint64_t)c_segments_limit
* 98ULL) / 100ULL);
696 c_segment_pages_compressed_limit
= (c_segments_limit
* (C_SEG_BUFSIZE
/ PAGE_SIZE
) * compressor_pool_multiplier
);
698 if (c_segment_pages_compressed_limit
< (uint32_t)(max_mem
/ PAGE_SIZE
)) {
699 c_segment_pages_compressed_limit
= (uint32_t)(max_mem
/ PAGE_SIZE
);
702 c_segment_pages_compressed_nearing_limit
= (uint32_t)(((uint64_t)c_segment_pages_compressed_limit
* 98ULL) / 100ULL);
705 * Submap needs space for:
708 * - swap reclaimations -- C_SEG_BUFSIZE
710 c_segments_arr_size
= vm_map_round_page((sizeof(union c_segu
) * c_segments_limit
), VM_MAP_PAGE_MASK(kernel_map
));
711 c_buffers_size
= vm_map_round_page(((vm_size_t
)C_SEG_ALLOCSIZE
* (vm_size_t
)c_segments_limit
), VM_MAP_PAGE_MASK(kernel_map
));
713 compressor_submap_size
= c_segments_arr_size
+ c_buffers_size
+ C_SEG_BUFSIZE
;
715 #if RECORD_THE_COMPRESSED_DATA
716 c_compressed_record_sbuf_size
= (vm_size_t
)C_SEG_ALLOCSIZE
+ (PAGE_SIZE
* 2);
717 compressor_submap_size
+= c_compressed_record_sbuf_size
;
718 #endif /* RECORD_THE_COMPRESSED_DATA */
720 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
721 vmk_flags
.vmkf_permanent
= TRUE
;
722 retval
= kmem_suballoc(kernel_map
, &start_addr
, compressor_submap_size
,
723 FALSE
, VM_FLAGS_ANYWHERE
, vmk_flags
, VM_KERN_MEMORY_COMPRESSOR
,
726 if (retval
!= KERN_SUCCESS
) {
727 if (++attempts
> 3) {
728 panic("vm_compressor_init: kmem_suballoc failed - 0x%llx", (uint64_t)compressor_submap_size
);
731 compressor_pool_size
= compressor_pool_size
/ 2;
733 kprintf("retrying creation of the compressor submap at 0x%llx bytes\n", compressor_pool_size
);
736 if (kernel_memory_allocate(compressor_map
, (vm_offset_t
*)(&c_segments
), (sizeof(union c_segu
) * c_segments_limit
), 0, KMA_KOBJECT
| KMA_VAONLY
| KMA_PERMANENT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
) {
737 panic("vm_compressor_init: kernel_memory_allocate failed - c_segments\n");
739 if (kernel_memory_allocate(compressor_map
, &c_buffers
, c_buffers_size
, 0, KMA_COMPRESSOR
| KMA_VAONLY
| KMA_PERMANENT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
) {
740 panic("vm_compressor_init: kernel_memory_allocate failed - c_buffers\n");
744 c_segment_min_size
= sizeof(struct c_segment
) + (C_SEG_SLOT_VAR_ARRAY_MIN_LEN
* sizeof(struct c_slot
));
746 for (c_segment_padded_size
= 128; c_segment_padded_size
< c_segment_min_size
; c_segment_padded_size
= c_segment_padded_size
<< 1) {
750 compressor_segment_zone
= zinit(c_segment_padded_size
, c_segments_limit
* c_segment_padded_size
, PAGE_SIZE
, "compressor_segment");
751 zone_change(compressor_segment_zone
, Z_CALLERACCT
, FALSE
);
752 zone_change(compressor_segment_zone
, Z_NOENCRYPT
, TRUE
);
754 c_seg_fixed_array_len
= (c_segment_padded_size
- sizeof(struct c_segment
)) / sizeof(struct c_slot
);
756 c_segments_busy
= FALSE
;
758 c_segments_next_page
= (caddr_t
)c_segments
;
759 vm_compressor_algorithm_init();
762 host_basic_info_data_t hinfo
;
763 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
766 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
768 compressor_cpus
= hinfo
.max_cpus
;
769 compressor_scratch_bufs
= kalloc_tag(compressor_cpus
* vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
771 kdp_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
774 * kdp_compressor_decompressed_page must be page aligned because we access
775 * it through the physical apperture by page number. kalloc() does not
776 * guarantee alignment.
779 if (kernel_memory_allocate(kernel_map
, &addr
, PAGE_SIZE
, 0, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
) {
780 panic("vm_compressor_init: kernel_memory_allocate failed - kdp_compressor_decompressed_page\n");
782 assert((addr
& PAGE_MASK
) == 0);
783 kdp_compressor_decompressed_page
= (void *)addr
;
784 kdp_compressor_decompressed_page_paddr
= kvtophys((vm_offset_t
)kdp_compressor_decompressed_page
);
785 kdp_compressor_decompressed_page_ppnum
= (ppnum_t
) atop(kdp_compressor_decompressed_page_paddr
);
788 freezer_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_encode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
791 #if RECORD_THE_COMPRESSED_DATA
792 if (kernel_memory_allocate(compressor_map
, (vm_offset_t
*)&c_compressed_record_sbuf
, c_compressed_record_sbuf_size
, 0, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
) {
793 panic("vm_compressor_init: kernel_memory_allocate failed - c_compressed_record_sbuf\n");
796 c_compressed_record_cptr
= c_compressed_record_sbuf
;
797 c_compressed_record_ebuf
= c_compressed_record_sbuf
+ c_compressed_record_sbuf_size
;
800 if (kernel_thread_start_priority((thread_continue_t
)vm_compressor_swap_trigger_thread
, NULL
,
801 BASEPRI_VM
, &thread
) != KERN_SUCCESS
) {
802 panic("vm_compressor_swap_trigger_thread: create failed");
804 thread_deallocate(thread
);
806 if (vm_pageout_internal_start() != KERN_SUCCESS
) {
807 panic("vm_compressor_init: Failed to start the internal pageout thread.\n");
809 if (VM_CONFIG_SWAP_IS_PRESENT
) {
810 vm_compressor_swap_init();
813 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE
) {
814 vm_compressor_is_active
= 1;
818 memorystatus_freeze_enabled
= TRUE
;
819 #endif /* CONFIG_FREEZE */
821 vm_compressor_available
= 1;
823 vm_page_reactivate_all_throttled();
827 #if VALIDATE_C_SEGMENTS
830 c_seg_validate(c_segment_t c_seg
, boolean_t must_be_compact
)
834 uint32_t c_rounded_size
;
838 if (__probable(validate_c_segs
== FALSE
)) {
841 if (c_seg
->c_firstemptyslot
< c_seg
->c_nextslot
) {
842 c_indx
= c_seg
->c_firstemptyslot
;
843 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
846 panic("c_seg_validate: no slot backing c_firstemptyslot");
850 panic("c_seg_validate: c_firstemptyslot has non-zero size (%d)\n", cs
->c_size
);
855 for (c_indx
= 0; c_indx
< c_seg
->c_nextslot
; c_indx
++) {
856 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
858 c_size
= UNPACK_C_SIZE(cs
);
860 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
862 bytes_used
+= c_rounded_size
;
864 #if CHECKSUM_THE_COMPRESSED_DATA
866 if (c_size
&& cs
->c_hash_compressed_data
!= (csvhash
= vmc_hash((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))) {
867 addr64_t csvphys
= kvtophys((vm_offset_t
)&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
868 panic("Compressed data doesn't match original %p phys: 0x%llx %d %p %d %d 0x%x 0x%x", c_seg
, csvphys
, cs
->c_offset
, cs
, c_indx
, c_size
, cs
->c_hash_compressed_data
, csvhash
);
871 #if POPCOUNT_THE_COMPRESSED_DATA
874 uintptr_t csvaddr
= (uintptr_t) &c_seg
->c_store
.c_buffer
[cs
->c_offset
];
875 if (cs
->c_pop_cdata
!= (csvpop
= vmc_pop(csvaddr
, c_size
))) {
876 panic("Compressed data popcount doesn't match original, bit distance: %d %p (phys: %p) %p %p 0x%llx 0x%x 0x%x 0x%x", (csvpop
- cs
->c_pop_cdata
), (void *)csvaddr
, (void *) kvtophys(csvaddr
), c_seg
, cs
, cs
->c_offset
, c_size
, csvpop
, cs
->c_pop_cdata
);
882 if (bytes_used
!= c_seg
->c_bytes_used
) {
883 panic("c_seg_validate: bytes_used mismatch - found %d, segment has %d\n", bytes_used
, c_seg
->c_bytes_used
);
886 if (c_seg
->c_bytes_used
> C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
)) {
887 panic("c_seg_validate: c_bytes_used > c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
888 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
891 if (must_be_compact
) {
892 if (c_seg
->c_bytes_used
!= C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
)) {
893 panic("c_seg_validate: c_bytes_used doesn't match c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
894 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
903 c_seg_need_delayed_compaction(c_segment_t c_seg
, boolean_t c_list_lock_held
)
905 boolean_t clear_busy
= FALSE
;
907 if (c_list_lock_held
== FALSE
) {
908 if (!lck_mtx_try_lock_spin_always(c_list_lock
)) {
911 lck_mtx_unlock_always(&c_seg
->c_lock
);
912 lck_mtx_lock_spin_always(c_list_lock
);
913 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
918 assert(c_seg
->c_state
!= C_IS_FILLING
);
920 if (!c_seg
->c_on_minorcompact_q
&& !(C_SEG_IS_ON_DISK_OR_SOQ(c_seg
))) {
921 queue_enter(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
922 c_seg
->c_on_minorcompact_q
= 1;
925 if (c_list_lock_held
== FALSE
) {
926 lck_mtx_unlock_always(c_list_lock
);
929 if (clear_busy
== TRUE
) {
930 C_SEG_WAKEUP_DONE(c_seg
);
935 unsigned int c_seg_moved_to_sparse_list
= 0;
938 c_seg_move_to_sparse_list(c_segment_t c_seg
)
940 boolean_t clear_busy
= FALSE
;
942 if (!lck_mtx_try_lock_spin_always(c_list_lock
)) {
945 lck_mtx_unlock_always(&c_seg
->c_lock
);
946 lck_mtx_lock_spin_always(c_list_lock
);
947 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
951 c_seg_switch_state(c_seg
, C_ON_SWAPPEDOUTSPARSE_Q
, FALSE
);
953 c_seg_moved_to_sparse_list
++;
955 lck_mtx_unlock_always(c_list_lock
);
957 if (clear_busy
== TRUE
) {
958 C_SEG_WAKEUP_DONE(c_seg
);
964 c_seg_insert_into_q(queue_head_t
*qhead
, c_segment_t c_seg
)
966 c_segment_t c_seg_next
;
968 if (queue_empty(qhead
)) {
969 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
971 c_seg_next
= (c_segment_t
)queue_first(qhead
);
974 if (c_seg
->c_generation_id
< c_seg_next
->c_generation_id
) {
975 queue_insert_before(qhead
, c_seg
, c_seg_next
, c_segment_t
, c_age_list
);
978 c_seg_next
= (c_segment_t
) queue_next(&c_seg_next
->c_age_list
);
980 if (queue_end(qhead
, (queue_entry_t
) c_seg_next
)) {
981 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
989 int try_minor_compaction_failed
= 0;
990 int try_minor_compaction_succeeded
= 0;
993 c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
)
995 assert(c_seg
->c_on_minorcompact_q
);
997 * c_seg is currently on the delayed minor compaction
998 * queue and we have c_seg locked... if we can get the
999 * c_list_lock w/o blocking (if we blocked we could deadlock
1000 * because the lock order is c_list_lock then c_seg's lock)
1001 * we'll pull it from the delayed list and free it directly
1003 if (!lck_mtx_try_lock_spin_always(c_list_lock
)) {
1005 * c_list_lock is held, we need to bail
1007 try_minor_compaction_failed
++;
1009 lck_mtx_unlock_always(&c_seg
->c_lock
);
1011 try_minor_compaction_succeeded
++;
1014 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, FALSE
);
1020 c_seg_do_minor_compaction_and_unlock(c_segment_t c_seg
, boolean_t clear_busy
, boolean_t need_list_lock
, boolean_t disallow_page_replacement
)
1024 assert(c_seg
->c_busy
);
1025 assert(!C_SEG_IS_ON_DISK_OR_SOQ(c_seg
));
1028 * check for the case that can occur when we are not swapping
1029 * and this segment has been major compacted in the past
1030 * and moved to the majorcompact q to remove it from further
1031 * consideration... if the occupancy falls too low we need
1032 * to put it back on the age_q so that it will be considered
1033 * in the next major compaction sweep... if we don't do this
1034 * we will eventually run into the c_segments_limit
1036 if (c_seg
->c_state
== C_ON_MAJORCOMPACT_Q
&& C_SEG_SHOULD_MAJORCOMPACT_NOW(c_seg
)) {
1037 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
1039 if (!c_seg
->c_on_minorcompact_q
) {
1040 if (clear_busy
== TRUE
) {
1041 C_SEG_WAKEUP_DONE(c_seg
);
1044 lck_mtx_unlock_always(&c_seg
->c_lock
);
1048 queue_remove(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
1049 c_seg
->c_on_minorcompact_q
= 0;
1052 lck_mtx_unlock_always(c_list_lock
);
1054 if (disallow_page_replacement
== TRUE
) {
1055 lck_mtx_unlock_always(&c_seg
->c_lock
);
1057 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
1059 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1061 c_seg_freed
= c_seg_minor_compaction_and_unlock(c_seg
, clear_busy
);
1063 if (disallow_page_replacement
== TRUE
) {
1064 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
1067 if (need_list_lock
== TRUE
) {
1068 lck_mtx_lock_spin_always(c_list_lock
);
1076 c_seg_wait_on_busy(c_segment_t c_seg
)
1078 c_seg
->c_wanted
= 1;
1079 assert_wait((event_t
) (c_seg
), THREAD_UNINT
);
1081 lck_mtx_unlock_always(&c_seg
->c_lock
);
1082 thread_block(THREAD_CONTINUE_NULL
);
1087 c_seg_switch_state(c_segment_t c_seg
, int new_state
, boolean_t insert_head
)
1089 int old_state
= c_seg
->c_state
;
1091 #if __i386__ || __x86_64__
1092 if (new_state
!= C_IS_FILLING
) {
1093 LCK_MTX_ASSERT(&c_seg
->c_lock
, LCK_MTX_ASSERT_OWNED
);
1095 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
1097 switch (old_state
) {
1099 assert(new_state
== C_IS_FILLING
|| new_state
== C_IS_FREE
);
1105 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_ON_SWAPOUT_Q
);
1107 queue_remove(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
1112 assert(new_state
== C_ON_SWAPOUT_Q
|| new_state
== C_ON_MAJORCOMPACT_Q
||
1113 new_state
== C_IS_FREE
);
1115 queue_remove(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
1119 case C_ON_SWAPPEDIN_Q
:
1120 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
1122 queue_remove(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
1123 c_swappedin_count
--;
1126 case C_ON_SWAPOUT_Q
:
1127 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
|| new_state
== C_IS_EMPTY
|| new_state
== C_ON_SWAPIO_Q
);
1129 queue_remove(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1130 thread_wakeup((event_t
)&compaction_swapper_running
);
1135 assert(new_state
== C_ON_SWAPPEDOUT_Q
|| new_state
== C_ON_SWAPPEDOUTSPARSE_Q
|| new_state
== C_ON_AGE_Q
);
1137 queue_remove(&c_swapio_list_head
, c_seg
, c_segment_t
, c_age_list
);
1141 case C_ON_SWAPPEDOUT_Q
:
1142 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
1143 new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
1144 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
1146 queue_remove(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1147 c_swappedout_count
--;
1150 case C_ON_SWAPPEDOUTSPARSE_Q
:
1151 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
1152 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
1154 queue_remove(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1155 c_swappedout_sparse_count
--;
1158 case C_ON_MAJORCOMPACT_Q
:
1159 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
1161 queue_remove(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1166 assert(new_state
== C_IS_FREE
);
1168 queue_remove(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1173 panic("c_seg %p has bad c_state = %d\n", c_seg
, old_state
);
1176 switch (new_state
) {
1178 assert(old_state
!= C_IS_FILLING
);
1183 assert(old_state
== C_ON_SWAPOUT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1189 assert(old_state
== C_IS_EMPTY
);
1191 queue_enter(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
1196 assert(old_state
== C_IS_FILLING
|| old_state
== C_ON_SWAPPEDIN_Q
||
1197 old_state
== C_ON_SWAPOUT_Q
|| old_state
== C_ON_SWAPIO_Q
||
1198 old_state
== C_ON_MAJORCOMPACT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1200 if (old_state
== C_IS_FILLING
) {
1201 queue_enter(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
1203 if (!queue_empty(&c_age_list_head
)) {
1204 c_segment_t c_first
;
1206 c_first
= (c_segment_t
)queue_first(&c_age_list_head
);
1207 c_seg
->c_creation_ts
= c_first
->c_creation_ts
;
1209 queue_enter_first(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
1214 case C_ON_SWAPPEDIN_Q
:
1215 assert(old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1217 if (insert_head
== TRUE
) {
1218 queue_enter_first(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
1220 queue_enter(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
1222 c_swappedin_count
++;
1225 case C_ON_SWAPOUT_Q
:
1226 assert(old_state
== C_ON_AGE_Q
|| old_state
== C_IS_FILLING
);
1228 if (insert_head
== TRUE
) {
1229 queue_enter_first(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1231 queue_enter(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1237 assert(old_state
== C_ON_SWAPOUT_Q
);
1239 if (insert_head
== TRUE
) {
1240 queue_enter_first(&c_swapio_list_head
, c_seg
, c_segment_t
, c_age_list
);
1242 queue_enter(&c_swapio_list_head
, c_seg
, c_segment_t
, c_age_list
);
1247 case C_ON_SWAPPEDOUT_Q
:
1248 assert(old_state
== C_ON_SWAPIO_Q
);
1250 if (insert_head
== TRUE
) {
1251 queue_enter_first(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1253 queue_enter(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1255 c_swappedout_count
++;
1258 case C_ON_SWAPPEDOUTSPARSE_Q
:
1259 assert(old_state
== C_ON_SWAPIO_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
);
1261 if (insert_head
== TRUE
) {
1262 queue_enter_first(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1264 queue_enter(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1267 c_swappedout_sparse_count
++;
1270 case C_ON_MAJORCOMPACT_Q
:
1271 assert(old_state
== C_ON_AGE_Q
);
1273 if (insert_head
== TRUE
) {
1274 queue_enter_first(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1276 queue_enter(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1282 assert(old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1284 if (insert_head
== TRUE
) {
1285 queue_enter_first(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1287 queue_enter(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1293 panic("c_seg %p requesting bad c_state = %d\n", c_seg
, new_state
);
1295 c_seg
->c_state
= new_state
;
1301 c_seg_free(c_segment_t c_seg
)
1303 assert(c_seg
->c_busy
);
1305 lck_mtx_unlock_always(&c_seg
->c_lock
);
1306 lck_mtx_lock_spin_always(c_list_lock
);
1307 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1309 c_seg_free_locked(c_seg
);
1314 c_seg_free_locked(c_segment_t c_seg
)
1317 int pages_populated
= 0;
1318 int32_t *c_buffer
= NULL
;
1319 uint64_t c_swap_handle
= 0;
1321 assert(c_seg
->c_busy
);
1322 assert(c_seg
->c_slots_used
== 0);
1323 assert(!c_seg
->c_on_minorcompact_q
);
1324 assert(!c_seg
->c_busy_swapping
);
1326 if (c_seg
->c_overage_swap
== TRUE
) {
1327 c_overage_swapped_count
--;
1328 c_seg
->c_overage_swap
= FALSE
;
1330 if (!(C_SEG_IS_ONDISK(c_seg
))) {
1331 c_buffer
= c_seg
->c_store
.c_buffer
;
1333 c_swap_handle
= c_seg
->c_store
.c_swap_handle
;
1336 c_seg_switch_state(c_seg
, C_IS_FREE
, FALSE
);
1338 lck_mtx_unlock_always(c_list_lock
);
1341 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
1342 c_seg
->c_store
.c_buffer
= NULL
;
1344 c_seg
->c_store
.c_swap_handle
= (uint64_t)-1;
1347 lck_mtx_unlock_always(&c_seg
->c_lock
);
1350 if (pages_populated
) {
1351 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
1353 } else if (c_swap_handle
) {
1355 * Free swap space on disk.
1357 vm_swap_free(c_swap_handle
);
1359 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1361 * c_seg must remain busy until
1362 * after the call to vm_swap_free
1364 C_SEG_WAKEUP_DONE(c_seg
);
1365 lck_mtx_unlock_always(&c_seg
->c_lock
);
1367 segno
= c_seg
->c_mysegno
;
1369 lck_mtx_lock_spin_always(c_list_lock
);
1371 * because the c_buffer is now associated with the segno,
1372 * we can't put the segno back on the free list until
1373 * after we have depopulated the c_buffer range, or
1374 * we run the risk of depopulating a range that is
1375 * now being used in one of the compressor heads
1377 c_segments
[segno
].c_segno
= c_free_segno_head
;
1378 c_free_segno_head
= segno
;
1381 lck_mtx_unlock_always(c_list_lock
);
1383 lck_mtx_destroy(&c_seg
->c_lock
, &vm_compressor_lck_grp
);
1385 if (c_seg
->c_slot_var_array_len
) {
1386 kfree(c_seg
->c_slot_var_array
, sizeof(struct c_slot
) * c_seg
->c_slot_var_array_len
);
1389 zfree(compressor_segment_zone
, c_seg
);
1392 #if DEVELOPMENT || DEBUG
1393 int c_seg_trim_page_count
= 0;
1397 c_seg_trim_tail(c_segment_t c_seg
)
1402 uint32_t c_rounded_size
;
1403 uint16_t current_nextslot
;
1404 uint32_t current_populated_offset
;
1406 if (c_seg
->c_bytes_used
== 0) {
1409 current_nextslot
= c_seg
->c_nextslot
;
1410 current_populated_offset
= c_seg
->c_populated_offset
;
1412 while (c_seg
->c_nextslot
) {
1413 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, (c_seg
->c_nextslot
- 1));
1415 c_size
= UNPACK_C_SIZE(cs
);
1418 if (current_nextslot
!= c_seg
->c_nextslot
) {
1419 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1420 c_offset
= cs
->c_offset
+ C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1422 c_seg
->c_nextoffset
= c_offset
;
1423 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) &
1424 ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1426 if (c_seg
->c_firstemptyslot
> c_seg
->c_nextslot
) {
1427 c_seg
->c_firstemptyslot
= c_seg
->c_nextslot
;
1429 #if DEVELOPMENT || DEBUG
1430 c_seg_trim_page_count
+= ((round_page_32(C_SEG_OFFSET_TO_BYTES(current_populated_offset
)) -
1431 round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) /
1437 c_seg
->c_nextslot
--;
1439 assert(c_seg
->c_nextslot
);
1444 c_seg_minor_compaction_and_unlock(c_segment_t c_seg
, boolean_t clear_busy
)
1446 c_slot_mapping_t slot_ptr
;
1447 uint32_t c_offset
= 0;
1448 uint32_t old_populated_offset
;
1449 uint32_t c_rounded_size
;
1456 assert(c_seg
->c_busy
);
1458 #if VALIDATE_C_SEGMENTS
1459 c_seg_validate(c_seg
, FALSE
);
1461 if (c_seg
->c_bytes_used
== 0) {
1465 lck_mtx_unlock_always(&c_seg
->c_lock
);
1467 if (c_seg
->c_firstemptyslot
>= c_seg
->c_nextslot
|| C_SEG_UNUSED_BYTES(c_seg
) < PAGE_SIZE
) {
1471 /* TODO: assert first emptyslot's c_size is actually 0 */
1473 #if DEVELOPMENT || DEBUG
1474 C_SEG_MAKE_WRITEABLE(c_seg
);
1477 #if VALIDATE_C_SEGMENTS
1478 c_seg
->c_was_minor_compacted
++;
1480 c_indx
= c_seg
->c_firstemptyslot
;
1481 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1483 old_populated_offset
= c_seg
->c_populated_offset
;
1484 c_offset
= c_dst
->c_offset
;
1486 for (i
= c_indx
+ 1; i
< c_seg
->c_nextslot
&& c_offset
< c_seg
->c_nextoffset
; i
++) {
1487 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg
, i
);
1489 c_size
= UNPACK_C_SIZE(c_src
);
1495 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1496 /* N.B.: This memcpy may be an overlapping copy */
1497 memcpy(&c_seg
->c_store
.c_buffer
[c_offset
], &c_seg
->c_store
.c_buffer
[c_src
->c_offset
], c_rounded_size
);
1499 cslot_copy(c_dst
, c_src
);
1500 c_dst
->c_offset
= c_offset
;
1502 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1503 slot_ptr
->s_cindx
= c_indx
;
1505 c_offset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1506 PACK_C_SIZE(c_src
, 0);
1509 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1511 c_seg
->c_firstemptyslot
= c_indx
;
1512 c_seg
->c_nextslot
= c_indx
;
1513 c_seg
->c_nextoffset
= c_offset
;
1514 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1515 c_seg
->c_bytes_unused
= 0;
1517 #if VALIDATE_C_SEGMENTS
1518 c_seg_validate(c_seg
, TRUE
);
1520 if (old_populated_offset
> c_seg
->c_populated_offset
) {
1524 gc_size
= C_SEG_OFFSET_TO_BYTES(old_populated_offset
- c_seg
->c_populated_offset
);
1525 gc_ptr
= &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
];
1527 kernel_memory_depopulate(compressor_map
, (vm_offset_t
)gc_ptr
, gc_size
, KMA_COMPRESSOR
);
1530 #if DEVELOPMENT || DEBUG
1531 C_SEG_WRITE_PROTECT(c_seg
);
1535 if (clear_busy
== TRUE
) {
1536 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1537 C_SEG_WAKEUP_DONE(c_seg
);
1538 lck_mtx_unlock_always(&c_seg
->c_lock
);
1545 c_seg_alloc_nextslot(c_segment_t c_seg
)
1547 struct c_slot
*old_slot_array
= NULL
;
1548 struct c_slot
*new_slot_array
= NULL
;
1552 if (c_seg
->c_nextslot
< c_seg_fixed_array_len
) {
1556 if ((c_seg
->c_nextslot
- c_seg_fixed_array_len
) >= c_seg
->c_slot_var_array_len
) {
1557 oldlen
= c_seg
->c_slot_var_array_len
;
1558 old_slot_array
= c_seg
->c_slot_var_array
;
1561 newlen
= C_SEG_SLOT_VAR_ARRAY_MIN_LEN
;
1563 newlen
= oldlen
* 2;
1566 new_slot_array
= (struct c_slot
*)kalloc(sizeof(struct c_slot
) * newlen
);
1568 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1570 if (old_slot_array
) {
1571 memcpy((char *)new_slot_array
, (char *)old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1574 c_seg
->c_slot_var_array_len
= newlen
;
1575 c_seg
->c_slot_var_array
= new_slot_array
;
1577 lck_mtx_unlock_always(&c_seg
->c_lock
);
1579 if (old_slot_array
) {
1580 kfree(old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1588 uint64_t asked_permission
;
1589 uint64_t compactions
;
1590 uint64_t moved_slots
;
1591 uint64_t moved_bytes
;
1592 uint64_t wasted_space_in_swapouts
;
1593 uint64_t count_of_swapouts
;
1594 uint64_t count_of_freed_segs
;
1595 } c_seg_major_compact_stats
;
1598 #define C_MAJOR_COMPACTION_SIZE_APPROPRIATE ((C_SEG_BUFSIZE * 90) / 100)
1602 c_seg_major_compact_ok(
1603 c_segment_t c_seg_dst
,
1604 c_segment_t c_seg_src
)
1606 c_seg_major_compact_stats
.asked_permission
++;
1608 if (c_seg_src
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
&&
1609 c_seg_dst
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
) {
1613 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1615 * destination segment is full... can't compact
1625 c_seg_major_compact(
1626 c_segment_t c_seg_dst
,
1627 c_segment_t c_seg_src
)
1629 c_slot_mapping_t slot_ptr
;
1630 uint32_t c_rounded_size
;
1636 boolean_t keep_compacting
= TRUE
;
1639 * segments are not locked but they are both marked c_busy
1640 * which keeps c_decompress from working on them...
1641 * we can safely allocate new pages, move compressed data
1642 * from c_seg_src to c_seg_dst and update both c_segment's
1643 * state w/o holding the master lock
1645 #if DEVELOPMENT || DEBUG
1646 C_SEG_MAKE_WRITEABLE(c_seg_dst
);
1649 #if VALIDATE_C_SEGMENTS
1650 c_seg_dst
->c_was_major_compacted
++;
1651 c_seg_src
->c_was_major_donor
++;
1653 c_seg_major_compact_stats
.compactions
++;
1655 dst_slot
= c_seg_dst
->c_nextslot
;
1657 for (i
= 0; i
< c_seg_src
->c_nextslot
; i
++) {
1658 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, i
);
1660 c_size
= UNPACK_C_SIZE(c_src
);
1663 /* BATCH: move what we have so far; */
1667 if (C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
- c_seg_dst
->c_nextoffset
) < (unsigned) c_size
) {
1668 int size_to_populate
;
1671 size_to_populate
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
);
1673 if (size_to_populate
== 0) {
1675 keep_compacting
= FALSE
;
1678 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
) {
1679 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
1682 kernel_memory_populate(compressor_map
,
1683 (vm_offset_t
) &c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_populated_offset
],
1686 VM_KERN_MEMORY_COMPRESSOR
);
1688 c_seg_dst
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
1689 assert(C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
) <= C_SEG_BUFSIZE
);
1691 c_seg_alloc_nextslot(c_seg_dst
);
1693 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
1695 memcpy(&c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_nextoffset
], &c_seg_src
->c_store
.c_buffer
[c_src
->c_offset
], c_size
);
1697 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1699 c_seg_major_compact_stats
.moved_slots
++;
1700 c_seg_major_compact_stats
.moved_bytes
+= c_size
;
1702 cslot_copy(c_dst
, c_src
);
1703 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
1705 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
) {
1706 c_seg_dst
->c_firstemptyslot
++;
1708 c_seg_dst
->c_slots_used
++;
1709 c_seg_dst
->c_nextslot
++;
1710 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
1711 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1713 PACK_C_SIZE(c_src
, 0);
1715 c_seg_src
->c_bytes_used
-= c_rounded_size
;
1716 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
1717 c_seg_src
->c_firstemptyslot
= 0;
1719 assert(c_seg_src
->c_slots_used
);
1720 c_seg_src
->c_slots_used
--;
1722 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1723 /* dest segment is now full */
1724 keep_compacting
= FALSE
;
1728 #if DEVELOPMENT || DEBUG
1729 C_SEG_WRITE_PROTECT(c_seg_dst
);
1731 if (dst_slot
< c_seg_dst
->c_nextslot
) {
1732 PAGE_REPLACEMENT_ALLOWED(TRUE
);
1734 * we've now locked out c_decompress from
1735 * converting the slot passed into it into
1736 * a c_segment_t which allows us to use
1737 * the backptr to change which c_segment and
1738 * index the slot points to
1740 while (dst_slot
< c_seg_dst
->c_nextslot
) {
1741 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
1743 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1744 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
1745 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
1746 slot_ptr
->s_cindx
= dst_slot
++;
1748 PAGE_REPLACEMENT_ALLOWED(FALSE
);
1750 return keep_compacting
;
1755 vm_compressor_compute_elapsed_msecs(clock_sec_t end_sec
, clock_nsec_t end_nsec
, clock_sec_t start_sec
, clock_nsec_t start_nsec
)
1758 uint64_t start_msecs
;
1760 end_msecs
= (end_sec
* 1000) + end_nsec
/ 1000000;
1761 start_msecs
= (start_sec
* 1000) + start_nsec
/ 1000000;
1763 return end_msecs
- start_msecs
;
1768 uint32_t compressor_eval_period_in_msecs
= 250;
1769 uint32_t compressor_sample_min_in_msecs
= 500;
1770 uint32_t compressor_sample_max_in_msecs
= 10000;
1771 uint32_t compressor_thrashing_threshold_per_10msecs
= 50;
1772 uint32_t compressor_thrashing_min_per_10msecs
= 20;
1774 /* When true, reset sample data next chance we get. */
1775 static boolean_t compressor_need_sample_reset
= FALSE
;
1779 compute_swapout_target_age(void)
1781 clock_sec_t cur_ts_sec
;
1782 clock_nsec_t cur_ts_nsec
;
1783 uint32_t min_operations_needed_in_this_sample
;
1784 uint64_t elapsed_msecs_in_eval
;
1785 uint64_t elapsed_msecs_in_sample
;
1786 boolean_t need_eval_reset
= FALSE
;
1788 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
1790 elapsed_msecs_in_sample
= vm_compressor_compute_elapsed_msecs(cur_ts_sec
, cur_ts_nsec
, start_of_sample_period_sec
, start_of_sample_period_nsec
);
1792 if (compressor_need_sample_reset
||
1793 elapsed_msecs_in_sample
>= compressor_sample_max_in_msecs
) {
1794 compressor_need_sample_reset
= TRUE
;
1795 need_eval_reset
= TRUE
;
1798 elapsed_msecs_in_eval
= vm_compressor_compute_elapsed_msecs(cur_ts_sec
, cur_ts_nsec
, start_of_eval_period_sec
, start_of_eval_period_nsec
);
1800 if (elapsed_msecs_in_eval
< compressor_eval_period_in_msecs
) {
1803 need_eval_reset
= TRUE
;
1805 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_START
, elapsed_msecs_in_eval
, sample_period_compression_count
, sample_period_decompression_count
, 0, 0);
1807 min_operations_needed_in_this_sample
= (compressor_thrashing_min_per_10msecs
* (uint32_t)elapsed_msecs_in_eval
) / 10;
1809 if ((sample_period_compression_count
- last_eval_compression_count
) < min_operations_needed_in_this_sample
||
1810 (sample_period_decompression_count
- last_eval_decompression_count
) < min_operations_needed_in_this_sample
) {
1811 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_compression_count
- last_eval_compression_count
,
1812 sample_period_decompression_count
- last_eval_decompression_count
, 0, 1, 0);
1814 swapout_target_age
= 0;
1816 compressor_need_sample_reset
= TRUE
;
1817 need_eval_reset
= TRUE
;
1820 last_eval_compression_count
= sample_period_compression_count
;
1821 last_eval_decompression_count
= sample_period_decompression_count
;
1823 if (elapsed_msecs_in_sample
< compressor_sample_min_in_msecs
) {
1824 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, 0, 0, 5, 0);
1827 if (sample_period_decompression_count
> ((compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10)) {
1828 uint64_t running_total
;
1829 uint64_t working_target
;
1830 uint64_t aging_target
;
1831 uint32_t oldest_age_of_csegs_sampled
= 0;
1832 uint64_t working_set_approximation
= 0;
1834 swapout_target_age
= 0;
1836 working_target
= (sample_period_decompression_count
/ 100) * 95; /* 95 percent */
1837 aging_target
= (sample_period_decompression_count
/ 100) * 1; /* 1 percent */
1840 for (oldest_age_of_csegs_sampled
= 0; oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
; oldest_age_of_csegs_sampled
++) {
1841 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1843 working_set_approximation
+= oldest_age_of_csegs_sampled
* age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1845 if (running_total
>= working_target
) {
1849 if (oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
) {
1850 working_set_approximation
= (working_set_approximation
* 1000) / elapsed_msecs_in_sample
;
1852 if (working_set_approximation
< VM_PAGE_COMPRESSOR_COUNT
) {
1853 running_total
= overage_decompressions_during_sample_period
;
1855 for (oldest_age_of_csegs_sampled
= DECOMPRESSION_SAMPLE_MAX_AGE
- 1; oldest_age_of_csegs_sampled
; oldest_age_of_csegs_sampled
--) {
1856 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1858 if (running_total
>= aging_target
) {
1862 swapout_target_age
= (uint32_t)cur_ts_sec
- oldest_age_of_csegs_sampled
;
1864 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 2, 0);
1866 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 0, 3, 0);
1869 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_target
, running_total
, 0, 4, 0);
1872 compressor_need_sample_reset
= TRUE
;
1873 need_eval_reset
= TRUE
;
1875 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_decompression_count
, (compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10, 0, 6, 0);
1878 if (compressor_need_sample_reset
== TRUE
) {
1879 bzero(age_of_decompressions_during_sample_period
, sizeof(age_of_decompressions_during_sample_period
));
1880 overage_decompressions_during_sample_period
= 0;
1882 start_of_sample_period_sec
= cur_ts_sec
;
1883 start_of_sample_period_nsec
= cur_ts_nsec
;
1884 sample_period_decompression_count
= 0;
1885 sample_period_compression_count
= 0;
1886 last_eval_decompression_count
= 0;
1887 last_eval_compression_count
= 0;
1888 compressor_need_sample_reset
= FALSE
;
1890 if (need_eval_reset
== TRUE
) {
1891 start_of_eval_period_sec
= cur_ts_sec
;
1892 start_of_eval_period_nsec
= cur_ts_nsec
;
1897 int compaction_swapper_init_now
= 0;
1898 int compaction_swapper_running
= 0;
1899 int compaction_swapper_awakened
= 0;
1900 int compaction_swapper_abort
= 0;
1904 boolean_t
memorystatus_kill_on_VM_compressor_thrashing(boolean_t
);
1905 boolean_t
memorystatus_kill_on_VM_compressor_space_shortage(boolean_t
);
1906 boolean_t
memorystatus_kill_on_FC_thrashing(boolean_t
);
1907 int compressor_thrashing_induced_jetsam
= 0;
1908 int filecache_thrashing_induced_jetsam
= 0;
1909 static boolean_t vm_compressor_thrashing_detected
= FALSE
;
1910 #endif /* CONFIG_JETSAM */
1913 compressor_needs_to_swap(void)
1915 boolean_t should_swap
= FALSE
;
1917 if (vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
) {
1923 clock_get_system_nanotime(&now
, &nsec
);
1926 lck_mtx_lock_spin_always(c_list_lock
);
1928 if (!queue_empty(&c_age_list_head
)) {
1929 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1931 age
= now
- c_seg
->c_creation_ts
;
1933 lck_mtx_unlock_always(c_list_lock
);
1935 if (age
>= vm_ripe_target_age
) {
1939 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
1940 if (COMPRESSOR_NEEDS_TO_SWAP()) {
1943 if (VM_PAGE_Q_THROTTLED(&vm_pageout_queue_external
) && vm_page_anonymous_count
< (vm_page_inactive_count
/ 20)) {
1946 if (vm_page_free_count
< (vm_page_free_reserved
- (COMPRESSOR_FREE_RESERVED_LIMIT
* 2))) {
1950 compute_swapout_target_age();
1952 if (swapout_target_age
) {
1955 lck_mtx_lock_spin_always(c_list_lock
);
1957 if (!queue_empty(&c_age_list_head
)) {
1958 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1960 if (c_seg
->c_creation_ts
> swapout_target_age
) {
1961 swapout_target_age
= 0;
1964 lck_mtx_unlock_always(c_list_lock
);
1966 #if CONFIG_PHANTOM_CACHE
1967 if (vm_phantom_cache_check_pressure()) {
1971 if (swapout_target_age
) {
1976 if (should_swap
|| vm_compressor_low_on_space() == TRUE
) {
1977 if (vm_compressor_thrashing_detected
== FALSE
) {
1978 vm_compressor_thrashing_detected
= TRUE
;
1980 if (swapout_target_age
|| vm_compressor_low_on_space() == TRUE
) {
1981 if (swapout_target_age
) {
1982 /* The compressor is thrashing. */
1983 memorystatus_kill_on_VM_compressor_thrashing(TRUE
/* async */);
1985 /* The compressor is running low on space. */
1986 memorystatus_kill_on_VM_compressor_space_shortage(TRUE
/* async */);
1988 compressor_thrashing_induced_jetsam
++;
1990 memorystatus_kill_on_FC_thrashing(TRUE
/* async */);
1991 filecache_thrashing_induced_jetsam
++;
1995 * let the jetsam take precedence over
1996 * any major compactions we might have
1997 * been able to do... otherwise we run
1998 * the risk of doing major compactions
1999 * on segments we're about to free up
2000 * due to the jetsam activity.
2002 should_swap
= FALSE
;
2005 #endif /* CONFIG_JETSAM */
2007 if (should_swap
== FALSE
) {
2009 * vm_compressor_needs_to_major_compact returns true only if we're
2010 * about to run out of available compressor segments... in this
2011 * case, we absolutely need to run a major compaction even if
2012 * we've just kicked off a jetsam or we don't otherwise need to
2013 * swap... terminating objects releases
2014 * pages back to the uncompressed cache, but does not guarantee
2015 * that we will free up even a single compression segment
2017 should_swap
= vm_compressor_needs_to_major_compact();
2021 * returning TRUE when swap_supported == FALSE
2022 * will cause the major compaction engine to
2023 * run, but will not trigger any swapping...
2024 * segments that have been major compacted
2025 * will be moved to the majorcompact queue
2032 * This function is called from the jetsam thread after killing something to
2033 * mitigate thrashing.
2035 * We need to restart our thrashing detection heuristics since memory pressure
2036 * has potentially changed significantly, and we don't want to detect on old
2037 * data from before the jetsam.
2040 vm_thrashing_jetsam_done(void)
2042 vm_compressor_thrashing_detected
= FALSE
;
2044 /* Were we compressor-thrashing or filecache-thrashing? */
2045 if (swapout_target_age
) {
2046 swapout_target_age
= 0;
2047 compressor_need_sample_reset
= TRUE
;
2049 #if CONFIG_PHANTOM_CACHE
2051 vm_phantom_cache_restart_sample();
2055 #endif /* CONFIG_JETSAM */
2057 uint32_t vm_wake_compactor_swapper_calls
= 0;
2058 uint32_t vm_run_compactor_already_running
= 0;
2059 uint32_t vm_run_compactor_empty_minor_q
= 0;
2060 uint32_t vm_run_compactor_did_compact
= 0;
2061 uint32_t vm_run_compactor_waited
= 0;
2064 vm_run_compactor(void)
2066 if (c_segment_count
== 0) {
2070 lck_mtx_lock_spin_always(c_list_lock
);
2072 if (c_minor_count
== 0) {
2073 vm_run_compactor_empty_minor_q
++;
2075 lck_mtx_unlock_always(c_list_lock
);
2078 if (compaction_swapper_running
) {
2079 if (vm_pageout_state
.vm_restricted_to_single_processor
== FALSE
) {
2080 vm_run_compactor_already_running
++;
2082 lck_mtx_unlock_always(c_list_lock
);
2085 vm_run_compactor_waited
++;
2087 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2089 lck_mtx_unlock_always(c_list_lock
);
2091 thread_block(THREAD_CONTINUE_NULL
);
2095 vm_run_compactor_did_compact
++;
2097 fastwake_warmup
= FALSE
;
2098 compaction_swapper_running
= 1;
2100 vm_compressor_do_delayed_compactions(FALSE
);
2102 compaction_swapper_running
= 0;
2104 lck_mtx_unlock_always(c_list_lock
);
2106 thread_wakeup((event_t
)&compaction_swapper_running
);
2111 vm_wake_compactor_swapper(void)
2113 if (compaction_swapper_running
|| compaction_swapper_awakened
|| c_segment_count
== 0) {
2117 if (c_minor_count
|| vm_compressor_needs_to_major_compact()) {
2118 lck_mtx_lock_spin_always(c_list_lock
);
2120 fastwake_warmup
= FALSE
;
2122 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2123 vm_wake_compactor_swapper_calls
++;
2125 compaction_swapper_awakened
= 1;
2126 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2128 lck_mtx_unlock_always(c_list_lock
);
2134 vm_consider_swapping()
2136 c_segment_t c_seg
, c_seg_next
;
2140 assert(VM_CONFIG_SWAP_IS_PRESENT
);
2142 lck_mtx_lock_spin_always(c_list_lock
);
2144 compaction_swapper_abort
= 1;
2146 while (compaction_swapper_running
) {
2147 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2149 lck_mtx_unlock_always(c_list_lock
);
2151 thread_block(THREAD_CONTINUE_NULL
);
2153 lck_mtx_lock_spin_always(c_list_lock
);
2155 compaction_swapper_abort
= 0;
2156 compaction_swapper_running
= 1;
2158 vm_swapout_ripe_segments
= TRUE
;
2160 if (!queue_empty(&c_major_list_head
)) {
2161 clock_get_system_nanotime(&now
, &nsec
);
2163 c_seg
= (c_segment_t
)queue_first(&c_major_list_head
);
2165 while (!queue_end(&c_major_list_head
, (queue_entry_t
)c_seg
)) {
2166 if (c_overage_swapped_count
>= c_overage_swapped_limit
) {
2170 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2172 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2173 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2175 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2177 lck_mtx_unlock_always(&c_seg
->c_lock
);
2182 vm_compressor_compact_and_swap(FALSE
);
2184 compaction_swapper_running
= 0;
2186 vm_swapout_ripe_segments
= FALSE
;
2188 lck_mtx_unlock_always(c_list_lock
);
2190 thread_wakeup((event_t
)&compaction_swapper_running
);
2195 vm_consider_waking_compactor_swapper(void)
2197 boolean_t need_wakeup
= FALSE
;
2199 if (c_segment_count
== 0) {
2203 if (compaction_swapper_running
|| compaction_swapper_awakened
) {
2207 if (!compaction_swapper_inited
&& !compaction_swapper_init_now
) {
2208 compaction_swapper_init_now
= 1;
2212 if (c_minor_count
&& (COMPRESSOR_NEEDS_TO_MINOR_COMPACT())) {
2214 } else if (compressor_needs_to_swap()) {
2216 } else if (c_minor_count
) {
2217 uint64_t total_bytes
;
2219 total_bytes
= compressor_object
->resident_page_count
* PAGE_SIZE_64
;
2221 if ((total_bytes
- compressor_bytes_used
) > total_bytes
/ 10) {
2225 if (need_wakeup
== TRUE
) {
2226 lck_mtx_lock_spin_always(c_list_lock
);
2228 fastwake_warmup
= FALSE
;
2230 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2231 memoryshot(VM_WAKEUP_COMPACTOR_SWAPPER
, DBG_FUNC_NONE
);
2233 compaction_swapper_awakened
= 1;
2234 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2236 lck_mtx_unlock_always(c_list_lock
);
2241 #define C_SWAPOUT_LIMIT 4
2242 #define DELAYED_COMPACTIONS_PER_PASS 30
2245 vm_compressor_do_delayed_compactions(boolean_t flush_all
)
2248 int number_compacted
= 0;
2249 boolean_t needs_to_swap
= FALSE
;
2252 #if !CONFIG_EMBEDDED
2253 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
2254 #endif /* !CONFIG_EMBEDDED */
2256 while (!queue_empty(&c_minor_list_head
) && needs_to_swap
== FALSE
) {
2257 c_seg
= (c_segment_t
)queue_first(&c_minor_list_head
);
2259 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2261 if (c_seg
->c_busy
) {
2262 lck_mtx_unlock_always(c_list_lock
);
2263 c_seg_wait_on_busy(c_seg
);
2264 lck_mtx_lock_spin_always(c_list_lock
);
2270 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, TRUE
);
2272 if (VM_CONFIG_SWAP_IS_ACTIVE
&& (number_compacted
++ > DELAYED_COMPACTIONS_PER_PASS
)) {
2273 if ((flush_all
== TRUE
|| compressor_needs_to_swap() == TRUE
) && c_swapout_count
< C_SWAPOUT_LIMIT
) {
2274 needs_to_swap
= TRUE
;
2277 number_compacted
= 0;
2279 lck_mtx_lock_spin_always(c_list_lock
);
2284 #define C_SEGMENT_SWAPPEDIN_AGE_LIMIT 10
2287 vm_compressor_age_swapped_in_segments(boolean_t flush_all
)
2293 clock_get_system_nanotime(&now
, &nsec
);
2295 while (!queue_empty(&c_swappedin_list_head
)) {
2296 c_seg
= (c_segment_t
)queue_first(&c_swappedin_list_head
);
2298 if (flush_all
== FALSE
&& (now
- c_seg
->c_swappedin_ts
) < C_SEGMENT_SWAPPEDIN_AGE_LIMIT
) {
2302 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2304 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2306 lck_mtx_unlock_always(&c_seg
->c_lock
);
2311 extern int vm_num_swap_files
;
2312 extern int vm_num_pinned_swap_files
;
2313 extern int vm_swappin_enabled
;
2315 extern unsigned int vm_swapfile_total_segs_used
;
2316 extern unsigned int vm_swapfile_total_segs_alloced
;
2320 vm_compressor_flush(void)
2322 uint64_t vm_swap_put_failures_at_start
;
2323 wait_result_t wait_result
= 0;
2324 AbsoluteTime startTime
, endTime
;
2325 clock_sec_t now_sec
;
2326 clock_nsec_t now_nsec
;
2329 HIBLOG("vm_compressor_flush - starting\n");
2331 clock_get_uptime(&startTime
);
2333 lck_mtx_lock_spin_always(c_list_lock
);
2335 fastwake_warmup
= FALSE
;
2336 compaction_swapper_abort
= 1;
2338 while (compaction_swapper_running
) {
2339 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2341 lck_mtx_unlock_always(c_list_lock
);
2343 thread_block(THREAD_CONTINUE_NULL
);
2345 lck_mtx_lock_spin_always(c_list_lock
);
2347 compaction_swapper_abort
= 0;
2348 compaction_swapper_running
= 1;
2350 hibernate_flushing
= TRUE
;
2351 hibernate_no_swapspace
= FALSE
;
2352 c_generation_id_flush_barrier
= c_generation_id
+ 1000;
2354 clock_get_system_nanotime(&now_sec
, &now_nsec
);
2355 hibernate_flushing_deadline
= now_sec
+ HIBERNATE_FLUSHING_SECS_TO_COMPLETE
;
2357 vm_swap_put_failures_at_start
= vm_swap_put_failures
;
2359 vm_compressor_compact_and_swap(TRUE
);
2361 while (!queue_empty(&c_swapout_list_head
)) {
2362 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 5000, 1000 * NSEC_PER_USEC
);
2364 lck_mtx_unlock_always(c_list_lock
);
2366 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2368 lck_mtx_lock_spin_always(c_list_lock
);
2370 if (wait_result
== THREAD_TIMED_OUT
) {
2374 hibernate_flushing
= FALSE
;
2375 compaction_swapper_running
= 0;
2377 if (vm_swap_put_failures
> vm_swap_put_failures_at_start
) {
2378 HIBLOG("vm_compressor_flush failed to clean %llu segments - vm_page_compressor_count(%d)\n",
2379 vm_swap_put_failures
- vm_swap_put_failures_at_start
, VM_PAGE_COMPRESSOR_COUNT
);
2382 lck_mtx_unlock_always(c_list_lock
);
2384 thread_wakeup((event_t
)&compaction_swapper_running
);
2386 clock_get_uptime(&endTime
);
2387 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2388 absolutetime_to_nanoseconds(endTime
, &nsec
);
2390 HIBLOG("vm_compressor_flush completed - took %qd msecs - vm_num_swap_files = %d, vm_num_pinned_swap_files = %d, vm_swappin_enabled = %d\n",
2391 nsec
/ 1000000ULL, vm_num_swap_files
, vm_num_pinned_swap_files
, vm_swappin_enabled
);
2395 int compaction_swap_trigger_thread_awakened
= 0;
2398 vm_compressor_swap_trigger_thread(void)
2400 current_thread()->options
|= TH_OPT_VMPRIV
;
2403 * compaction_swapper_init_now is set when the first call to
2404 * vm_consider_waking_compactor_swapper is made from
2405 * vm_pageout_scan... since this function is called upon
2406 * thread creation, we want to make sure to delay adjusting
2407 * the tuneables until we are awakened via vm_pageout_scan
2408 * so that we are at a point where the vm_swapfile_open will
2409 * be operating on the correct directory (in case the default
2410 * of /var/vm/ is overridden by the dymanic_pager
2412 if (compaction_swapper_init_now
) {
2413 vm_compaction_swapper_do_init();
2415 if (vm_pageout_state
.vm_restricted_to_single_processor
== TRUE
) {
2416 thread_vm_bind_group_add();
2418 thread_set_thread_name(current_thread(), "VM_cswap_trigger");
2419 compaction_swapper_init_now
= 0;
2421 lck_mtx_lock_spin_always(c_list_lock
);
2423 compaction_swap_trigger_thread_awakened
++;
2424 compaction_swapper_awakened
= 0;
2426 if (compaction_swapper_running
== 0) {
2427 compaction_swapper_running
= 1;
2429 vm_compressor_compact_and_swap(FALSE
);
2431 compaction_swapper_running
= 0;
2433 assert_wait((event_t
)&c_compressor_swap_trigger
, THREAD_UNINT
);
2435 if (compaction_swapper_running
== 0) {
2436 thread_wakeup((event_t
)&compaction_swapper_running
);
2439 lck_mtx_unlock_always(c_list_lock
);
2441 thread_block((thread_continue_t
)vm_compressor_swap_trigger_thread
);
2448 vm_compressor_record_warmup_start(void)
2452 lck_mtx_lock_spin_always(c_list_lock
);
2454 if (first_c_segment_to_warm_generation_id
== 0) {
2455 if (!queue_empty(&c_age_list_head
)) {
2456 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2458 first_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2460 first_c_segment_to_warm_generation_id
= 0;
2463 fastwake_recording_in_progress
= TRUE
;
2465 lck_mtx_unlock_always(c_list_lock
);
2470 vm_compressor_record_warmup_end(void)
2474 lck_mtx_lock_spin_always(c_list_lock
);
2476 if (fastwake_recording_in_progress
== TRUE
) {
2477 if (!queue_empty(&c_age_list_head
)) {
2478 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2480 last_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2482 last_c_segment_to_warm_generation_id
= first_c_segment_to_warm_generation_id
;
2485 fastwake_recording_in_progress
= FALSE
;
2487 HIBLOG("vm_compressor_record_warmup (%qd - %qd)\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2489 lck_mtx_unlock_always(c_list_lock
);
2493 #define DELAY_TRIM_ON_WAKE_SECS 25
2496 vm_compressor_delay_trim(void)
2501 clock_get_system_nanotime(&sec
, &nsec
);
2502 dont_trim_until_ts
= sec
+ DELAY_TRIM_ON_WAKE_SECS
;
2507 vm_compressor_do_warmup(void)
2509 lck_mtx_lock_spin_always(c_list_lock
);
2511 if (first_c_segment_to_warm_generation_id
== last_c_segment_to_warm_generation_id
) {
2512 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2514 lck_mtx_unlock_always(c_list_lock
);
2518 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2519 fastwake_warmup
= TRUE
;
2521 compaction_swapper_awakened
= 1;
2522 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2524 lck_mtx_unlock_always(c_list_lock
);
2528 do_fastwake_warmup_all(void)
2530 lck_mtx_lock_spin_always(c_list_lock
);
2532 if (queue_empty(&c_swappedout_list_head
) && queue_empty(&c_swappedout_sparse_list_head
)) {
2533 lck_mtx_unlock_always(c_list_lock
);
2537 fastwake_warmup
= TRUE
;
2539 do_fastwake_warmup(&c_swappedout_list_head
, TRUE
);
2541 do_fastwake_warmup(&c_swappedout_sparse_list_head
, TRUE
);
2543 fastwake_warmup
= FALSE
;
2545 lck_mtx_unlock_always(c_list_lock
);
2549 do_fastwake_warmup(queue_head_t
*c_queue
, boolean_t consider_all_cseg
)
2551 c_segment_t c_seg
= NULL
;
2552 AbsoluteTime startTime
, endTime
;
2556 HIBLOG("vm_compressor_fastwake_warmup (%qd - %qd) - starting\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2558 clock_get_uptime(&startTime
);
2560 lck_mtx_unlock_always(c_list_lock
);
2562 proc_set_thread_policy(current_thread(),
2563 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER2
);
2565 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2567 lck_mtx_lock_spin_always(c_list_lock
);
2569 while (!queue_empty(c_queue
) && fastwake_warmup
== TRUE
) {
2570 c_seg
= (c_segment_t
) queue_first(c_queue
);
2572 if (consider_all_cseg
== FALSE
) {
2573 if (c_seg
->c_generation_id
< first_c_segment_to_warm_generation_id
||
2574 c_seg
->c_generation_id
> last_c_segment_to_warm_generation_id
) {
2578 if (vm_page_free_count
< (AVAILABLE_MEMORY
/ 4)) {
2583 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2584 lck_mtx_unlock_always(c_list_lock
);
2586 if (c_seg
->c_busy
) {
2587 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2588 c_seg_wait_on_busy(c_seg
);
2589 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2591 if (c_seg_swapin(c_seg
, TRUE
, FALSE
) == 0) {
2592 lck_mtx_unlock_always(&c_seg
->c_lock
);
2594 c_segment_warmup_count
++;
2596 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2597 vm_pageout_io_throttle();
2598 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2600 lck_mtx_lock_spin_always(c_list_lock
);
2602 lck_mtx_unlock_always(c_list_lock
);
2604 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2606 proc_set_thread_policy(current_thread(),
2607 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER0
);
2609 clock_get_uptime(&endTime
);
2610 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2611 absolutetime_to_nanoseconds(endTime
, &nsec
);
2613 HIBLOG("vm_compressor_fastwake_warmup completed - took %qd msecs\n", nsec
/ 1000000ULL);
2615 lck_mtx_lock_spin_always(c_list_lock
);
2617 if (consider_all_cseg
== FALSE
) {
2618 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2624 vm_compressor_compact_and_swap(boolean_t flush_all
)
2626 c_segment_t c_seg
, c_seg_next
;
2627 boolean_t keep_compacting
;
2632 if (fastwake_warmup
== TRUE
) {
2633 uint64_t starting_warmup_count
;
2635 starting_warmup_count
= c_segment_warmup_count
;
2637 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 11) | DBG_FUNC_START
, c_segment_warmup_count
,
2638 first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
, 0, 0);
2639 do_fastwake_warmup(&c_swappedout_list_head
, FALSE
);
2640 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 11) | DBG_FUNC_END
, c_segment_warmup_count
, c_segment_warmup_count
- starting_warmup_count
, 0, 0, 0);
2642 fastwake_warmup
= FALSE
;
2646 * it's possible for the c_age_list_head to be empty if we
2647 * hit our limits for growing the compressor pool and we subsequently
2648 * hibernated... on the next hibernation we could see the queue as
2649 * empty and not proceeed even though we have a bunch of segments on
2650 * the swapped in queue that need to be dealt with.
2652 vm_compressor_do_delayed_compactions(flush_all
);
2654 vm_compressor_age_swapped_in_segments(flush_all
);
2657 * we only need to grab the timestamp once per
2658 * invocation of this function since the
2659 * timescale we're interested in is measured
2662 clock_get_system_nanotime(&now
, &nsec
);
2664 while (!queue_empty(&c_age_list_head
) && compaction_swapper_abort
== 0) {
2665 if (hibernate_flushing
== TRUE
) {
2668 if (hibernate_should_abort()) {
2669 HIBLOG("vm_compressor_flush - hibernate_should_abort returned TRUE\n");
2672 if (hibernate_no_swapspace
== TRUE
) {
2673 HIBLOG("vm_compressor_flush - out of swap space\n");
2676 if (vm_swap_files_pinned() == FALSE
) {
2677 HIBLOG("vm_compressor_flush - unpinned swap files\n");
2680 if (hibernate_in_progress_with_pinned_swap
== TRUE
&&
2681 (vm_swapfile_total_segs_alloced
== vm_swapfile_total_segs_used
)) {
2682 HIBLOG("vm_compressor_flush - out of pinned swap space\n");
2685 clock_get_system_nanotime(&sec
, &nsec
);
2687 if (sec
> hibernate_flushing_deadline
) {
2688 HIBLOG("vm_compressor_flush - failed to finish before deadline\n");
2692 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2693 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 100, 1000 * NSEC_PER_USEC
);
2695 lck_mtx_unlock_always(c_list_lock
);
2697 thread_block(THREAD_CONTINUE_NULL
);
2699 lck_mtx_lock_spin_always(c_list_lock
);
2704 vm_compressor_do_delayed_compactions(flush_all
);
2706 vm_compressor_age_swapped_in_segments(flush_all
);
2708 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2710 * we timed out on the above thread_block
2711 * let's loop around and try again
2712 * the timeout allows us to continue
2713 * to do minor compactions to make
2714 * more memory available
2720 * Swap out segments?
2722 if (flush_all
== FALSE
) {
2723 boolean_t needs_to_swap
;
2725 lck_mtx_unlock_always(c_list_lock
);
2727 needs_to_swap
= compressor_needs_to_swap();
2729 #if !CONFIG_EMBEDDED
2730 if (needs_to_swap
== TRUE
&& vm_swap_low_on_space()) {
2731 vm_compressor_take_paging_space_action();
2733 #endif /* !CONFIG_EMBEDDED */
2735 lck_mtx_lock_spin_always(c_list_lock
);
2737 if (needs_to_swap
== FALSE
) {
2741 if (queue_empty(&c_age_list_head
)) {
2744 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
2746 assert(c_seg
->c_state
== C_ON_AGE_Q
);
2748 if (flush_all
== TRUE
&& c_seg
->c_generation_id
> c_generation_id_flush_barrier
) {
2752 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2754 if (c_seg
->c_busy
) {
2755 lck_mtx_unlock_always(c_list_lock
);
2756 c_seg_wait_on_busy(c_seg
);
2757 lck_mtx_lock_spin_always(c_list_lock
);
2763 if (c_seg_do_minor_compaction_and_unlock(c_seg
, FALSE
, TRUE
, TRUE
)) {
2765 * found an empty c_segment and freed it
2766 * so go grab the next guy in the queue
2768 c_seg_major_compact_stats
.count_of_freed_segs
++;
2774 keep_compacting
= TRUE
;
2776 while (keep_compacting
== TRUE
) {
2777 assert(c_seg
->c_busy
);
2779 /* look for another segment to consolidate */
2781 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2783 if (queue_end(&c_age_list_head
, (queue_entry_t
)c_seg_next
)) {
2787 assert(c_seg_next
->c_state
== C_ON_AGE_Q
);
2789 if (c_seg_major_compact_ok(c_seg
, c_seg_next
) == FALSE
) {
2793 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2795 if (c_seg_next
->c_busy
) {
2796 lck_mtx_unlock_always(c_list_lock
);
2797 c_seg_wait_on_busy(c_seg_next
);
2798 lck_mtx_lock_spin_always(c_list_lock
);
2802 /* grab that segment */
2803 C_SEG_BUSY(c_seg_next
);
2805 if (c_seg_do_minor_compaction_and_unlock(c_seg_next
, FALSE
, TRUE
, TRUE
)) {
2807 * found an empty c_segment and freed it
2808 * so we can't continue to use c_seg_next
2810 c_seg_major_compact_stats
.count_of_freed_segs
++;
2814 /* unlock the list ... */
2815 lck_mtx_unlock_always(c_list_lock
);
2817 /* do the major compaction */
2819 keep_compacting
= c_seg_major_compact(c_seg
, c_seg_next
);
2821 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2823 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2825 * run a minor compaction on the donor segment
2826 * since we pulled at least some of it's
2827 * data into our target... if we've emptied
2828 * it, now is a good time to free it which
2829 * c_seg_minor_compaction_and_unlock also takes care of
2831 * by passing TRUE, we ask for c_busy to be cleared
2832 * and c_wanted to be taken care of
2834 if (c_seg_minor_compaction_and_unlock(c_seg_next
, TRUE
)) {
2835 c_seg_major_compact_stats
.count_of_freed_segs
++;
2838 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2840 /* relock the list */
2841 lck_mtx_lock_spin_always(c_list_lock
);
2842 } /* major compaction */
2844 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2846 assert(c_seg
->c_busy
);
2847 assert(!c_seg
->c_on_minorcompact_q
);
2849 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
2851 * This mode of putting a generic c_seg on the swapout list is
2852 * only supported when we have general swapping enabled
2854 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2856 if ((vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
)) {
2857 assert(VM_CONFIG_SWAP_IS_PRESENT
);
2859 * we are running compressor sweeps with swap-behind
2860 * make sure the c_seg has aged enough before swapping it
2863 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2864 c_seg
->c_overage_swap
= TRUE
;
2865 c_overage_swapped_count
++;
2866 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2870 if (c_seg
->c_state
== C_ON_AGE_Q
) {
2872 * this c_seg didn't get moved to the swapout queue
2873 * so we need to move it out of the way...
2874 * we just did a major compaction on it so put it
2877 c_seg_switch_state(c_seg
, C_ON_MAJORCOMPACT_Q
, FALSE
);
2879 c_seg_major_compact_stats
.wasted_space_in_swapouts
+= C_SEG_BUFSIZE
- c_seg
->c_bytes_used
;
2880 c_seg_major_compact_stats
.count_of_swapouts
++;
2882 C_SEG_WAKEUP_DONE(c_seg
);
2884 lck_mtx_unlock_always(&c_seg
->c_lock
);
2886 if (c_swapout_count
) {
2887 lck_mtx_unlock_always(c_list_lock
);
2889 thread_wakeup((event_t
)&c_swapout_list_head
);
2891 lck_mtx_lock_spin_always(c_list_lock
);
2898 c_seg_allocate(c_segment_t
*current_chead
)
2902 int size_to_populate
;
2904 #if !CONFIG_EMBEDDED
2905 if (vm_compressor_low_on_space()) {
2906 vm_compressor_take_paging_space_action();
2908 #endif /* !CONFIG_EMBEDDED */
2910 if ((c_seg
= *current_chead
) == NULL
) {
2913 lck_mtx_lock_spin_always(c_list_lock
);
2915 while (c_segments_busy
== TRUE
) {
2916 assert_wait((event_t
) (&c_segments_busy
), THREAD_UNINT
);
2918 lck_mtx_unlock_always(c_list_lock
);
2920 thread_block(THREAD_CONTINUE_NULL
);
2922 lck_mtx_lock_spin_always(c_list_lock
);
2924 if (c_free_segno_head
== (uint32_t)-1) {
2925 uint32_t c_segments_available_new
;
2927 if (c_segments_available
>= c_segments_limit
|| c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) {
2928 lck_mtx_unlock_always(c_list_lock
);
2932 c_segments_busy
= TRUE
;
2933 lck_mtx_unlock_always(c_list_lock
);
2935 kernel_memory_populate(compressor_map
, (vm_offset_t
)c_segments_next_page
,
2936 PAGE_SIZE
, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
);
2937 c_segments_next_page
+= PAGE_SIZE
;
2939 c_segments_available_new
= c_segments_available
+ C_SEGMENTS_PER_PAGE
;
2941 if (c_segments_available_new
> c_segments_limit
) {
2942 c_segments_available_new
= c_segments_limit
;
2945 for (c_segno
= c_segments_available
+ 1; c_segno
< c_segments_available_new
; c_segno
++) {
2946 c_segments
[c_segno
- 1].c_segno
= c_segno
;
2949 lck_mtx_lock_spin_always(c_list_lock
);
2951 c_segments
[c_segno
- 1].c_segno
= c_free_segno_head
;
2952 c_free_segno_head
= c_segments_available
;
2953 c_segments_available
= c_segments_available_new
;
2955 c_segments_busy
= FALSE
;
2956 thread_wakeup((event_t
) (&c_segments_busy
));
2958 c_segno
= c_free_segno_head
;
2959 assert(c_segno
>= 0 && c_segno
< c_segments_limit
);
2961 c_free_segno_head
= (uint32_t)c_segments
[c_segno
].c_segno
;
2964 * do the rest of the bookkeeping now while we're still behind
2965 * the list lock and grab our generation id now into a local
2966 * so that we can install it once we have the c_seg allocated
2969 if (c_segment_count
> c_segment_count_max
) {
2970 c_segment_count_max
= c_segment_count
;
2973 lck_mtx_unlock_always(c_list_lock
);
2975 c_seg
= (c_segment_t
)zalloc(compressor_segment_zone
);
2976 bzero((char *)c_seg
, sizeof(struct c_segment
));
2978 c_seg
->c_store
.c_buffer
= (int32_t *)C_SEG_BUFFER_ADDRESS(c_segno
);
2980 lck_mtx_init(&c_seg
->c_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
2982 c_seg
->c_state
= C_IS_EMPTY
;
2983 c_seg
->c_firstemptyslot
= C_SLOT_MAX_INDEX
;
2984 c_seg
->c_mysegno
= c_segno
;
2986 lck_mtx_lock_spin_always(c_list_lock
);
2988 c_seg_switch_state(c_seg
, C_IS_FILLING
, FALSE
);
2989 c_segments
[c_segno
].c_seg
= c_seg
;
2990 assert(c_segments
[c_segno
].c_segno
> c_segments_available
);
2991 lck_mtx_unlock_always(c_list_lock
);
2993 *current_chead
= c_seg
;
2995 #if DEVELOPMENT || DEBUG
2996 C_SEG_MAKE_WRITEABLE(c_seg
);
2999 c_seg_alloc_nextslot(c_seg
);
3001 size_to_populate
= C_SEG_ALLOCSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
);
3003 if (size_to_populate
) {
3004 min_needed
= PAGE_SIZE
+ (C_SEG_ALLOCSIZE
- C_SEG_BUFSIZE
);
3006 if (C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
) < (unsigned) min_needed
) {
3007 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
) {
3008 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
3011 OSAddAtomic64(size_to_populate
/ PAGE_SIZE
, &vm_pageout_vminfo
.vm_compressor_pages_grabbed
);
3013 kernel_memory_populate(compressor_map
,
3014 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
],
3017 VM_KERN_MEMORY_COMPRESSOR
);
3019 size_to_populate
= 0;
3022 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3024 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3026 if (size_to_populate
) {
3027 c_seg
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
3033 #if DEVELOPMENT || DEBUG
3035 extern boolean_t memorystatus_freeze_to_memory
;
3036 #endif /* CONFIG_FREEZE */
3037 #endif /* DEVELOPMENT || DEBUG */
3040 c_current_seg_filled(c_segment_t c_seg
, c_segment_t
*current_chead
)
3042 uint32_t unused_bytes
;
3043 uint32_t offset_to_depopulate
;
3044 int new_state
= C_ON_AGE_Q
;
3047 boolean_t head_insert
= FALSE
;
3049 unused_bytes
= trunc_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
));
3051 #ifndef _OPEN_SOURCE
3052 /* TODO: The HW codec can generate, lazily, a '2nd page not mapped'
3053 * exception. So on such a platform, or platforms where we're confident
3054 * the codec does not require a buffer page to absorb trailing writes,
3055 * we can create an unmapped hole at the tail of the segment, rather
3056 * than a populated mapping. This will also guarantee that the codec
3057 * does not overwrite valid data past the edge of the segment and
3058 * thus eliminate the depopulation overhead.
3062 offset_to_depopulate
= C_SEG_BYTES_TO_OFFSET(round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_nextoffset
)));
3065 * release the extra physical page(s) at the end of the segment
3067 lck_mtx_unlock_always(&c_seg
->c_lock
);
3069 kernel_memory_depopulate(
3071 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[offset_to_depopulate
],
3075 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3077 c_seg
->c_populated_offset
= offset_to_depopulate
;
3079 assert(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
) <= C_SEG_BUFSIZE
);
3081 #if DEVELOPMENT || DEBUG
3083 boolean_t c_seg_was_busy
= FALSE
;
3085 if (!c_seg
->c_busy
) {
3088 c_seg_was_busy
= TRUE
;
3091 lck_mtx_unlock_always(&c_seg
->c_lock
);
3093 C_SEG_WRITE_PROTECT(c_seg
);
3095 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3097 if (c_seg_was_busy
== FALSE
) {
3098 C_SEG_WAKEUP_DONE(c_seg
);
3104 if (current_chead
== (c_segment_t
*)&freezer_chead
&&
3105 VM_CONFIG_SWAP_IS_PRESENT
&&
3106 VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
3107 #if DEVELOPMENT || DEBUG
3108 && !memorystatus_freeze_to_memory
3109 #endif /* DEVELOPMENT || DEBUG */
3111 new_state
= C_ON_SWAPOUT_Q
;
3113 #endif /* CONFIG_FREEZE */
3115 if (vm_darkwake_mode
== TRUE
) {
3116 new_state
= C_ON_SWAPOUT_Q
;
3120 clock_get_system_nanotime(&sec
, &nsec
);
3121 c_seg
->c_creation_ts
= (uint32_t)sec
;
3123 lck_mtx_lock_spin_always(c_list_lock
);
3125 c_seg
->c_generation_id
= c_generation_id
++;
3126 c_seg_switch_state(c_seg
, new_state
, head_insert
);
3129 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
) {
3131 * darkwake and freezer can't co-exist together
3132 * We'll need to fix this accounting as a start.
3134 assert(vm_darkwake_mode
== FALSE
);
3135 c_freezer_swapout_page_count
+= (C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
)) / PAGE_SIZE_64
;
3137 #endif /* CONFIG_FREEZE */
3139 if (c_seg
->c_state
== C_ON_AGE_Q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
) {
3140 c_seg_need_delayed_compaction(c_seg
, TRUE
);
3143 lck_mtx_unlock_always(c_list_lock
);
3145 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
) {
3146 thread_wakeup((event_t
)&c_swapout_list_head
);
3149 *current_chead
= NULL
;
3154 * returns with c_seg locked
3157 c_seg_swapin_requeue(c_segment_t c_seg
, boolean_t has_data
, boolean_t minor_compact_ok
, boolean_t age_on_swapin_q
)
3162 clock_get_system_nanotime(&sec
, &nsec
);
3164 lck_mtx_lock_spin_always(c_list_lock
);
3165 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3167 assert(c_seg
->c_busy_swapping
);
3168 assert(c_seg
->c_busy
);
3170 c_seg
->c_busy_swapping
= 0;
3172 if (c_seg
->c_overage_swap
== TRUE
) {
3173 c_overage_swapped_count
--;
3174 c_seg
->c_overage_swap
= FALSE
;
3176 if (has_data
== TRUE
) {
3177 if (age_on_swapin_q
== TRUE
) {
3178 c_seg_switch_state(c_seg
, C_ON_SWAPPEDIN_Q
, FALSE
);
3180 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
3183 if (minor_compact_ok
== TRUE
&& !c_seg
->c_on_minorcompact_q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
) {
3184 c_seg_need_delayed_compaction(c_seg
, TRUE
);
3187 c_seg
->c_store
.c_buffer
= (int32_t*) NULL
;
3188 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3190 c_seg_switch_state(c_seg
, C_ON_BAD_Q
, FALSE
);
3192 c_seg
->c_swappedin_ts
= (uint32_t)sec
;
3194 lck_mtx_unlock_always(c_list_lock
);
3200 * c_seg has to be locked and is returned locked if the c_seg isn't freed
3201 * PAGE_REPLACMENT_DISALLOWED has to be TRUE on entry and is returned TRUE
3202 * c_seg_swapin returns 1 if the c_seg was freed, 0 otherwise
3206 c_seg_swapin(c_segment_t c_seg
, boolean_t force_minor_compaction
, boolean_t age_on_swapin_q
)
3208 vm_offset_t addr
= 0;
3209 uint32_t io_size
= 0;
3212 assert(C_SEG_IS_ONDISK(c_seg
));
3214 #if !CHECKSUM_THE_SWAP
3215 c_seg_trim_tail(c_seg
);
3217 io_size
= round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
));
3218 f_offset
= c_seg
->c_store
.c_swap_handle
;
3221 c_seg
->c_busy_swapping
= 1;
3224 * This thread is likely going to block for I/O.
3225 * Make sure it is ready to run when the I/O completes because
3226 * it needs to clear the busy bit on the c_seg so that other
3227 * waiting threads can make progress too. To do that, boost
3228 * the rwlock_count so that the priority is boosted.
3230 set_thread_rwlock_boost();
3231 lck_mtx_unlock_always(&c_seg
->c_lock
);
3233 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3235 addr
= (vm_offset_t
)C_SEG_BUFFER_ADDRESS(c_seg
->c_mysegno
);
3236 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
3238 kernel_memory_populate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
, VM_KERN_MEMORY_COMPRESSOR
);
3240 if (vm_swap_get(c_seg
, f_offset
, io_size
) != KERN_SUCCESS
) {
3241 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3243 kernel_memory_depopulate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
);
3245 c_seg_swapin_requeue(c_seg
, FALSE
, TRUE
, age_on_swapin_q
);
3248 vm_swap_decrypt(c_seg
);
3249 #endif /* ENCRYPTED_SWAP */
3251 #if CHECKSUM_THE_SWAP
3252 if (c_seg
->cseg_swap_size
!= io_size
) {
3253 panic("swapin size doesn't match swapout size");
3256 if (c_seg
->cseg_hash
!= vmc_hash((char*) c_seg
->c_store
.c_buffer
, (int)io_size
)) {
3257 panic("c_seg_swapin - Swap hash mismatch\n");
3259 #endif /* CHECKSUM_THE_SWAP */
3261 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3263 c_seg_swapin_requeue(c_seg
, TRUE
, force_minor_compaction
== TRUE
? FALSE
: TRUE
, age_on_swapin_q
);
3265 OSAddAtomic64(c_seg
->c_bytes_used
, &compressor_bytes_used
);
3267 if (force_minor_compaction
== TRUE
) {
3268 if (c_seg_minor_compaction_and_unlock(c_seg
, FALSE
)) {
3270 * c_seg was completely empty so it was freed,
3271 * so be careful not to reference it again
3273 * Drop the rwlock_count so that the thread priority
3274 * is returned back to where it is supposed to be.
3276 clear_thread_rwlock_boost();
3280 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3283 C_SEG_WAKEUP_DONE(c_seg
);
3286 * Drop the rwlock_count so that the thread priority
3287 * is returned back to where it is supposed to be.
3289 clear_thread_rwlock_boost();
3296 c_segment_sv_hash_drop_ref(int hash_indx
)
3298 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
3301 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_indx
].he_record
;
3303 n_sv_he
.he_ref
= o_sv_he
.he_ref
- 1;
3304 n_sv_he
.he_data
= o_sv_he
.he_data
;
3306 if (OSCompareAndSwap64((UInt64
)o_sv_he
.he_record
, (UInt64
)n_sv_he
.he_record
, (UInt64
*) &c_segment_sv_hash_table
[hash_indx
].he_record
) == TRUE
) {
3307 if (n_sv_he
.he_ref
== 0) {
3308 OSAddAtomic(-1, &c_segment_svp_in_hash
);
3317 c_segment_sv_hash_insert(uint32_t data
)
3321 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
3322 boolean_t got_ref
= FALSE
;
3325 OSAddAtomic(1, &c_segment_svp_zero_compressions
);
3327 OSAddAtomic(1, &c_segment_svp_nonzero_compressions
);
3330 hash_sindx
= data
& C_SV_HASH_MASK
;
3332 for (misses
= 0; misses
< C_SV_HASH_MAX_MISS
; misses
++) {
3333 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
3335 while (o_sv_he
.he_data
== data
|| o_sv_he
.he_ref
== 0) {
3336 n_sv_he
.he_ref
= o_sv_he
.he_ref
+ 1;
3337 n_sv_he
.he_data
= data
;
3339 if (OSCompareAndSwap64((UInt64
)o_sv_he
.he_record
, (UInt64
)n_sv_he
.he_record
, (UInt64
*) &c_segment_sv_hash_table
[hash_sindx
].he_record
) == TRUE
) {
3340 if (n_sv_he
.he_ref
== 1) {
3341 OSAddAtomic(1, &c_segment_svp_in_hash
);
3346 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
3348 if (got_ref
== TRUE
) {
3353 if (hash_sindx
== C_SV_HASH_SIZE
) {
3357 if (got_ref
== FALSE
) {
3365 #if RECORD_THE_COMPRESSED_DATA
3368 c_compressed_record_data(char *src
, int c_size
)
3370 if ((c_compressed_record_cptr
+ c_size
+ 4) >= c_compressed_record_ebuf
) {
3371 panic("c_compressed_record_cptr >= c_compressed_record_ebuf");
3374 *(int *)((void *)c_compressed_record_cptr
) = c_size
;
3376 c_compressed_record_cptr
+= 4;
3378 memcpy(c_compressed_record_cptr
, src
, c_size
);
3379 c_compressed_record_cptr
+= c_size
;
3385 c_compress_page(char *src
, c_slot_mapping_t slot_ptr
, c_segment_t
*current_chead
, char *scratch_buf
)
3388 int c_rounded_size
= 0;
3393 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_START
, *current_chead
, 0, 0, 0, 0);
3395 if ((c_seg
= c_seg_allocate(current_chead
)) == NULL
) {
3399 * returns with c_seg lock held
3400 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3401 * c_nextslot has been allocated and
3402 * c_store.c_buffer populated
3404 assert(c_seg
->c_state
== C_IS_FILLING
);
3406 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_seg
->c_nextslot
);
3408 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(slot_ptr
);
3409 assert(slot_ptr
== (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(cs
));
3411 cs
->c_offset
= c_seg
->c_nextoffset
;
3413 max_csize
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)cs
->c_offset
);
3415 if (max_csize
> PAGE_SIZE
) {
3416 max_csize
= PAGE_SIZE
;
3419 #if CHECKSUM_THE_DATA
3420 cs
->c_hash_data
= vmc_hash(src
, PAGE_SIZE
);
3422 boolean_t incomp_copy
= FALSE
;
3423 int max_csize_adj
= (max_csize
- 4);
3425 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3426 #if defined(__arm__) || defined(__arm64__)
3427 uint16_t ccodec
= CINVALID
;
3429 if (max_csize
>= C_SEG_OFFSET_ALIGNMENT_BOUNDARY
) {
3430 c_size
= metacompressor((const uint8_t *) src
,
3431 (uint8_t *) &c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3432 max_csize_adj
, &ccodec
,
3433 scratch_buf
, &incomp_copy
);
3434 #if C_SEG_OFFSET_ALIGNMENT_BOUNDARY > 4
3435 if (c_size
> max_csize_adj
) {
3442 assert(ccodec
== CCWK
|| ccodec
== CCLZ4
);
3443 cs
->c_codec
= ccodec
;
3446 #if defined(__arm__) || defined(__arm64__)
3449 #if defined(__arm64__)
3450 __unreachable_ok_push
3451 if (PAGE_SIZE
== 4096) {
3452 c_size
= WKdm_compress_4k((WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3453 (WK_word
*)(uintptr_t)scratch_buf
, max_csize_adj
);
3455 c_size
= WKdm_compress_16k((WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3456 (WK_word
*)(uintptr_t)scratch_buf
, max_csize_adj
);
3458 __unreachable_ok_pop
3460 c_size
= WKdm_compress_new((const WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3461 (WK_word
*)(uintptr_t)scratch_buf
, max_csize_adj
);
3464 assertf(((c_size
<= max_csize_adj
) && (c_size
>= -1)),
3465 "c_size invalid (%d, %d), cur compressions: %d", c_size
, max_csize_adj
, c_segment_pages_compressed
);
3468 if (max_csize
< PAGE_SIZE
) {
3469 c_current_seg_filled(c_seg
, current_chead
);
3470 assert(*current_chead
== NULL
);
3472 lck_mtx_unlock_always(&c_seg
->c_lock
);
3473 /* TODO: it may be worth requiring codecs to distinguish
3474 * between incompressible inputs and failures due to
3475 * budget exhaustion.
3477 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3482 if (incomp_copy
== FALSE
) {
3483 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3486 OSAddAtomic(1, &c_segment_noncompressible_pages
);
3487 } else if (c_size
== 0) {
3491 * special case - this is a page completely full of a single 32 bit value
3493 hash_index
= c_segment_sv_hash_insert(*(uint32_t *)(uintptr_t)src
);
3495 if (hash_index
!= -1) {
3496 slot_ptr
->s_cindx
= hash_index
;
3497 slot_ptr
->s_cseg
= C_SV_CSEG_ID
;
3499 OSAddAtomic(1, &c_segment_svp_hash_succeeded
);
3500 #if RECORD_THE_COMPRESSED_DATA
3501 c_compressed_record_data(src
, 4);
3503 goto sv_compression
;
3507 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3509 OSAddAtomic(1, &c_segment_svp_hash_failed
);
3512 #if RECORD_THE_COMPRESSED_DATA
3513 c_compressed_record_data((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3515 #if CHECKSUM_THE_COMPRESSED_DATA
3516 cs
->c_hash_compressed_data
= vmc_hash((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3518 #if POPCOUNT_THE_COMPRESSED_DATA
3519 cs
->c_pop_cdata
= vmc_pop((uintptr_t) &c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3521 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3523 PACK_C_SIZE(cs
, c_size
);
3524 c_seg
->c_bytes_used
+= c_rounded_size
;
3525 c_seg
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3526 c_seg
->c_slots_used
++;
3528 slot_ptr
->s_cindx
= c_seg
->c_nextslot
++;
3529 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3530 slot_ptr
->s_cseg
= c_seg
->c_mysegno
+ 1;
3533 if (c_seg
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3534 c_current_seg_filled(c_seg
, current_chead
);
3535 assert(*current_chead
== NULL
);
3537 lck_mtx_unlock_always(&c_seg
->c_lock
);
3539 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3541 #if RECORD_THE_COMPRESSED_DATA
3542 if ((c_compressed_record_cptr
- c_compressed_record_sbuf
) >= C_SEG_ALLOCSIZE
) {
3543 c_compressed_record_write(c_compressed_record_sbuf
, (int)(c_compressed_record_cptr
- c_compressed_record_sbuf
));
3544 c_compressed_record_cptr
= c_compressed_record_sbuf
;
3548 OSAddAtomic64(c_size
, &c_segment_compressed_bytes
);
3549 OSAddAtomic64(c_rounded_size
, &compressor_bytes_used
);
3551 OSAddAtomic64(PAGE_SIZE
, &c_segment_input_bytes
);
3553 OSAddAtomic(1, &c_segment_pages_compressed
);
3554 OSAddAtomic(1, &sample_period_compression_count
);
3556 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_END
, *current_chead
, c_size
, c_segment_input_bytes
, c_segment_compressed_bytes
, 0);
3562 sv_decompress(int32_t *ddst
, int32_t pattern
)
3565 memset_word(ddst
, pattern
, PAGE_SIZE
/ sizeof(int32_t));
3569 /* Unroll the pattern fill loop 4x to encourage the
3570 * compiler to emit NEON stores, cf.
3571 * <rdar://problem/25839866> Loop autovectorization
3573 * We use separate loops for each PAGE_SIZE
3574 * to allow the autovectorizer to engage, as PAGE_SIZE
3575 * is currently not a constant.
3578 __unreachable_ok_push
3579 if (PAGE_SIZE
== 4096) {
3580 for (i
= 0; i
< (4096U / sizeof(int32_t)); i
+= 4) {
3587 assert(PAGE_SIZE
== 16384);
3588 for (i
= 0; i
< (int)(16384U / sizeof(int32_t)); i
+= 4) {
3595 __unreachable_ok_pop
3600 c_decompress_page(char *dst
, volatile c_slot_mapping_t slot_ptr
, int flags
, int *zeroslot
)
3609 boolean_t need_unlock
= TRUE
;
3610 boolean_t consider_defragmenting
= FALSE
;
3611 boolean_t kdp_mode
= FALSE
;
3613 if (__improbable(flags
& C_KDP
)) {
3615 panic("C_KDP passed to decompress page from outside of debugger context");
3618 assert((flags
& C_KEEP
) == C_KEEP
);
3619 assert((flags
& C_DONT_BLOCK
) == C_DONT_BLOCK
);
3621 if ((flags
& (C_DONT_BLOCK
| C_KEEP
)) != (C_DONT_BLOCK
| C_KEEP
)) {
3630 if (__probable(!kdp_mode
)) {
3631 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3633 if (kdp_lck_rw_lock_is_acquired_exclusive(&c_master_lock
)) {
3640 * if hibernation is enabled, it indicates (via a call
3641 * to 'vm_decompressor_lock' that no further
3642 * decompressions are allowed once it reaches
3643 * the point of flushing all of the currently dirty
3644 * anonymous memory through the compressor and out
3645 * to disk... in this state we allow freeing of compressed
3646 * pages and must honor the C_DONT_BLOCK case
3648 if (__improbable(dst
&& decompressions_blocked
== TRUE
)) {
3649 if (flags
& C_DONT_BLOCK
) {
3650 if (__probable(!kdp_mode
)) {
3651 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3658 * it's safe to atomically assert and block behind the
3659 * lock held in shared mode because "decompressions_blocked" is
3660 * only set and cleared and the thread_wakeup done when the lock
3661 * is held exclusively
3663 assert_wait((event_t
)&decompressions_blocked
, THREAD_UNINT
);
3665 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3667 thread_block(THREAD_CONTINUE_NULL
);
3672 /* s_cseg is actually "segno+1" */
3673 c_segno
= slot_ptr
->s_cseg
- 1;
3675 if (__improbable(c_segno
>= c_segments_available
)) {
3676 panic("c_decompress_page: c_segno %d >= c_segments_available %d, slot_ptr(%p), slot_data(%x)",
3677 c_segno
, c_segments_available
, slot_ptr
, *(int *)((void *)slot_ptr
));
3680 if (__improbable(c_segments
[c_segno
].c_segno
< c_segments_available
)) {
3681 panic("c_decompress_page: c_segno %d is free, slot_ptr(%p), slot_data(%x)",
3682 c_segno
, slot_ptr
, *(int *)((void *)slot_ptr
));
3685 c_seg
= c_segments
[c_segno
].c_seg
;
3687 if (__probable(!kdp_mode
)) {
3688 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3690 if (kdp_lck_mtx_lock_spin_is_acquired(&c_seg
->c_lock
)) {
3695 assert(c_seg
->c_state
!= C_IS_EMPTY
&& c_seg
->c_state
!= C_IS_FREE
);
3697 if (dst
== NULL
&& c_seg
->c_busy_swapping
) {
3698 assert(c_seg
->c_busy
);
3700 goto bypass_busy_check
;
3702 if (flags
& C_DONT_BLOCK
) {
3703 if (c_seg
->c_busy
|| (C_SEG_IS_ONDISK(c_seg
) && dst
)) {
3710 if (c_seg
->c_busy
) {
3711 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3713 c_seg_wait_on_busy(c_seg
);
3719 c_indx
= slot_ptr
->s_cindx
;
3721 if (__improbable(c_indx
>= c_seg
->c_nextslot
)) {
3722 panic("c_decompress_page: c_indx %d >= c_nextslot %d, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3723 c_indx
, c_seg
->c_nextslot
, c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3726 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3728 c_size
= UNPACK_C_SIZE(cs
);
3730 if (__improbable(c_size
== 0)) {
3731 panic("c_decompress_page: c_size == 0, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3732 c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3735 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3738 uint32_t age_of_cseg
;
3739 clock_sec_t cur_ts_sec
;
3740 clock_nsec_t cur_ts_nsec
;
3742 if (C_SEG_IS_ONDISK(c_seg
)) {
3743 assert(kdp_mode
== FALSE
);
3744 retval
= c_seg_swapin(c_seg
, FALSE
, TRUE
);
3745 assert(retval
== 0);
3749 if (c_seg
->c_state
== C_ON_BAD_Q
) {
3750 assert(c_seg
->c_store
.c_buffer
== NULL
);
3757 #if POPCOUNT_THE_COMPRESSED_DATA
3759 uintptr_t csvaddr
= (uintptr_t) &c_seg
->c_store
.c_buffer
[cs
->c_offset
];
3760 if (cs
->c_pop_cdata
!= (csvpop
= vmc_pop(csvaddr
, c_size
))) {
3761 panic("Compressed data popcount doesn't match original, bit distance: %d %p (phys: %p) %p %p 0x%llx 0x%x 0x%x 0x%x", (csvpop
- cs
->c_pop_cdata
), (void *)csvaddr
, (void *) kvtophys(csvaddr
), c_seg
, cs
, cs
->c_offset
, c_size
, csvpop
, cs
->c_pop_cdata
);
3765 #if CHECKSUM_THE_COMPRESSED_DATA
3767 if (cs
->c_hash_compressed_data
!= (csvhash
= vmc_hash((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))) {
3768 panic("Compressed data doesn't match original %p %p %u %u %u", c_seg
, cs
, c_size
, cs
->c_hash_compressed_data
, csvhash
);
3771 if (c_rounded_size
== PAGE_SIZE
) {
3773 * page wasn't compressible... just copy it out
3775 memcpy(dst
, &c_seg
->c_store
.c_buffer
[cs
->c_offset
], PAGE_SIZE
);
3776 } else if (c_size
== 4) {
3781 * page was populated with a single value
3782 * that didn't fit into our fast hash
3783 * so we packed it in as a single non-compressed value
3784 * that we need to populate the page with
3786 dptr
= (int32_t *)(uintptr_t)dst
;
3787 data
= *(int32_t *)(&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
3788 sv_decompress(dptr
, data
);
3793 if (__probable(!kdp_mode
)) {
3795 * we're behind the c_seg lock held in spin mode
3796 * which means pre-emption is disabled... therefore
3797 * the following sequence is atomic and safe
3799 my_cpu_no
= cpu_number();
3801 assert(my_cpu_no
< compressor_cpus
);
3803 scratch_buf
= &compressor_scratch_bufs
[my_cpu_no
* vm_compressor_get_decode_scratch_size()];
3805 scratch_buf
= kdp_compressor_scratch_buf
;
3808 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3809 #if defined(__arm__) || defined(__arm64__)
3810 uint16_t c_codec
= cs
->c_codec
;
3811 metadecompressor((const uint8_t *) &c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3812 (uint8_t *)dst
, c_size
, c_codec
, (void *)scratch_buf
);
3815 #if defined(__arm64__)
3816 __unreachable_ok_push
3817 if (PAGE_SIZE
== 4096) {
3818 WKdm_decompress_4k((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3819 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3821 WKdm_decompress_16k((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3822 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3824 __unreachable_ok_pop
3826 WKdm_decompress_new((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3827 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3832 #if CHECKSUM_THE_DATA
3833 if (cs
->c_hash_data
!= vmc_hash(dst
, PAGE_SIZE
)) {
3834 #if defined(__arm__) || defined(__arm64__)
3835 int32_t *dinput
= &c_seg
->c_store
.c_buffer
[cs
->c_offset
];
3836 panic("decompressed data doesn't match original cs: %p, hash: 0x%x, offset: %d, c_size: %d, c_rounded_size: %d, codec: %d, header: 0x%x 0x%x 0x%x", cs
, cs
->c_hash_data
, cs
->c_offset
, c_size
, c_rounded_size
, cs
->c_codec
, *dinput
, *(dinput
+ 1), *(dinput
+ 2));
3838 panic("decompressed data doesn't match original cs: %p, hash: %d, offset: 0x%x, c_size: %d", cs
, cs
->c_hash_data
, cs
->c_offset
, c_size
);
3842 if (c_seg
->c_swappedin_ts
== 0 && !kdp_mode
) {
3843 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
3845 age_of_cseg
= (uint32_t)cur_ts_sec
- c_seg
->c_creation_ts
;
3846 if (age_of_cseg
< DECOMPRESSION_SAMPLE_MAX_AGE
) {
3847 OSAddAtomic(1, &age_of_decompressions_during_sample_period
[age_of_cseg
]);
3849 OSAddAtomic(1, &overage_decompressions_during_sample_period
);
3852 OSAddAtomic(1, &sample_period_decompression_count
);
3855 if (flags
& C_KEEP
) {
3859 assert(kdp_mode
== FALSE
);
3861 c_seg
->c_bytes_unused
+= c_rounded_size
;
3862 c_seg
->c_bytes_used
-= c_rounded_size
;
3864 assert(c_seg
->c_slots_used
);
3865 c_seg
->c_slots_used
--;
3869 if (c_indx
< c_seg
->c_firstemptyslot
) {
3870 c_seg
->c_firstemptyslot
= c_indx
;
3873 OSAddAtomic(-1, &c_segment_pages_compressed
);
3875 if (c_seg
->c_state
!= C_ON_BAD_Q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
3877 * C_SEG_IS_ONDISK == TRUE can occur when we're doing a
3878 * free of a compressed page (i.e. dst == NULL)
3880 OSAddAtomic64(-c_rounded_size
, &compressor_bytes_used
);
3882 if (c_seg
->c_busy_swapping
) {
3884 * bypass case for c_busy_swapping...
3885 * let the swapin/swapout paths deal with putting
3886 * the c_seg on the minor compaction queue if needed
3888 assert(c_seg
->c_busy
);
3891 assert(!c_seg
->c_busy
);
3893 if (c_seg
->c_state
!= C_IS_FILLING
) {
3894 if (c_seg
->c_bytes_used
== 0) {
3895 if (!(C_SEG_IS_ONDISK(c_seg
))) {
3896 int pages_populated
;
3898 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
3899 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3901 if (pages_populated
) {
3902 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3903 assert(c_seg
->c_store
.c_buffer
!= NULL
);
3906 lck_mtx_unlock_always(&c_seg
->c_lock
);
3908 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_seg
->c_store
.c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
3910 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3911 C_SEG_WAKEUP_DONE(c_seg
);
3913 if (!c_seg
->c_on_minorcompact_q
&& c_seg
->c_state
!= C_ON_SWAPOUT_Q
&& c_seg
->c_state
!= C_ON_SWAPIO_Q
) {
3914 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3917 if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
) {
3918 c_seg_move_to_sparse_list(c_seg
);
3919 consider_defragmenting
= TRUE
;
3922 } else if (c_seg
->c_on_minorcompact_q
) {
3923 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3924 assert(!C_SEG_IS_ON_DISK_OR_SOQ(c_seg
));
3926 if (C_SEG_SHOULD_MINORCOMPACT_NOW(c_seg
)) {
3927 c_seg_try_minor_compaction_and_unlock(c_seg
);
3928 need_unlock
= FALSE
;
3930 } else if (!(C_SEG_IS_ONDISK(c_seg
))) {
3931 if (c_seg
->c_state
!= C_ON_BAD_Q
&& c_seg
->c_state
!= C_ON_SWAPOUT_Q
&& c_seg
->c_state
!= C_ON_SWAPIO_Q
&&
3932 C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
) {
3933 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3935 } else if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
&& C_SEG_ONDISK_IS_SPARSE(c_seg
)) {
3936 c_seg_move_to_sparse_list(c_seg
);
3937 consider_defragmenting
= TRUE
;
3941 if (__improbable(kdp_mode
)) {
3945 if (need_unlock
== TRUE
) {
3946 lck_mtx_unlock_always(&c_seg
->c_lock
);
3949 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3951 if (consider_defragmenting
== TRUE
) {
3952 vm_swap_consider_defragmenting(VM_SWAP_FLAGS_NONE
);
3956 if ((c_minor_count
&& COMPRESSOR_NEEDS_TO_MINOR_COMPACT()) || vm_compressor_needs_to_major_compact()) {
3957 vm_wake_compactor_swapper();
3966 vm_compressor_get(ppnum_t pn
, int *slot
, int flags
)
3968 c_slot_mapping_t slot_ptr
;
3973 dst
= pmap_map_compressor_page(pn
);
3974 slot_ptr
= (c_slot_mapping_t
)slot
;
3976 assert(dst
!= NULL
);
3978 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3983 * page was populated with a single value
3984 * that found a home in our hash table
3985 * grab that value from the hash and populate the page
3986 * that we need to populate the page with
3988 dptr
= (int32_t *)(uintptr_t)dst
;
3989 data
= c_segment_sv_hash_table
[slot_ptr
->s_cindx
].he_data
;
3991 memset_word(dptr
, data
, PAGE_SIZE
/ sizeof(int32_t));
3996 for (i
= 0; i
< (int)(PAGE_SIZE
/ sizeof(int32_t)); i
++) {
4001 if (!(flags
& C_KEEP
)) {
4002 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
4004 OSAddAtomic(-1, &c_segment_pages_compressed
);
4008 OSAddAtomic(1, &c_segment_svp_nonzero_decompressions
);
4010 OSAddAtomic(1, &c_segment_svp_zero_decompressions
);
4013 pmap_unmap_compressor_page(pn
, dst
);
4017 retval
= c_decompress_page(dst
, slot_ptr
, flags
, &zeroslot
);
4020 * zeroslot will be set to 0 by c_decompress_page if (flags & C_KEEP)
4021 * or (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be TRUE
4027 pmap_unmap_compressor_page(pn
, dst
);
4030 * returns 0 if we successfully decompressed a page from a segment already in memory
4031 * returns 1 if we had to first swap in the segment, before successfully decompressing the page
4032 * returns -1 if we encountered an error swapping in the segment - decompression failed
4033 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be true
4040 vm_compressor_free(int *slot
, int flags
)
4042 c_slot_mapping_t slot_ptr
;
4046 assert(flags
== 0 || flags
== C_DONT_BLOCK
);
4048 slot_ptr
= (c_slot_mapping_t
)slot
;
4050 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
4051 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
4052 OSAddAtomic(-1, &c_segment_pages_compressed
);
4057 retval
= c_decompress_page(NULL
, slot_ptr
, flags
, &zeroslot
);
4059 * returns 0 if we successfully freed the specified compressed page
4060 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' set
4066 assert(retval
== -2);
4074 vm_compressor_put(ppnum_t pn
, int *slot
, void **current_chead
, char *scratch_buf
)
4079 src
= pmap_map_compressor_page(pn
);
4080 assert(src
!= NULL
);
4082 retval
= c_compress_page(src
, (c_slot_mapping_t
)slot
, (c_segment_t
*)current_chead
, scratch_buf
);
4083 pmap_unmap_compressor_page(pn
, src
);
4089 vm_compressor_transfer(
4093 c_slot_mapping_t dst_slot
, src_slot
;
4098 src_slot
= (c_slot_mapping_t
) src_slot_p
;
4100 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
4101 *dst_slot_p
= *src_slot_p
;
4105 dst_slot
= (c_slot_mapping_t
) dst_slot_p
;
4107 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4108 /* get segment for src_slot */
4109 c_seg
= c_segments
[src_slot
->s_cseg
- 1].c_seg
;
4111 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
4112 /* wait if it's busy */
4113 if (c_seg
->c_busy
&& !c_seg
->c_busy_swapping
) {
4114 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4115 c_seg_wait_on_busy(c_seg
);
4118 /* find the c_slot */
4119 c_indx
= src_slot
->s_cindx
;
4120 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
4121 /* point the c_slot back to dst_slot instead of src_slot */
4122 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(dst_slot
);
4124 *dst_slot_p
= *src_slot_p
;
4126 lck_mtx_unlock_always(&c_seg
->c_lock
);
4127 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4132 int freezer_finished_filling
= 0;
4135 vm_compressor_finished_filling(
4136 void **current_chead
)
4140 if ((c_seg
= *(c_segment_t
*)current_chead
) == NULL
) {
4144 assert(c_seg
->c_state
== C_IS_FILLING
);
4146 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
4148 c_current_seg_filled(c_seg
, (c_segment_t
*)current_chead
);
4150 lck_mtx_unlock_always(&c_seg
->c_lock
);
4152 freezer_finished_filling
++;
4157 * This routine is used to transfer the compressed chunks from
4158 * the c_seg/cindx pointed to by slot_p into a new c_seg headed
4159 * by the current_chead and a new cindx within that c_seg.
4161 * Currently, this routine is only used by the "freezer backed by
4162 * compressor with swap" mode to create a series of c_segs that
4163 * only contain compressed data belonging to one task. So, we
4164 * move a task's previously compressed data into a set of new
4165 * c_segs which will also hold the task's yet to be compressed data.
4169 vm_compressor_relocate(
4170 void **current_chead
,
4173 c_slot_mapping_t slot_ptr
;
4174 c_slot_mapping_t src_slot
;
4175 uint32_t c_rounded_size
;
4181 c_segment_t c_seg_dst
= NULL
;
4182 c_segment_t c_seg_src
= NULL
;
4183 kern_return_t kr
= KERN_SUCCESS
;
4186 src_slot
= (c_slot_mapping_t
) slot_p
;
4188 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
4190 * no need to relocate... this is a page full of a single
4191 * value which is hashed to a single entry not contained
4198 c_seg_dst
= c_seg_allocate((c_segment_t
*)current_chead
);
4200 * returns with c_seg lock held
4201 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
4202 * c_nextslot has been allocated and
4203 * c_store.c_buffer populated
4205 if (c_seg_dst
== NULL
) {
4207 * Out of compression segments?
4209 kr
= KERN_RESOURCE_SHORTAGE
;
4213 assert(c_seg_dst
->c_busy
== 0);
4215 C_SEG_BUSY(c_seg_dst
);
4217 dst_slot
= c_seg_dst
->c_nextslot
;
4219 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
4222 c_seg_src
= c_segments
[src_slot
->s_cseg
- 1].c_seg
;
4224 assert(c_seg_dst
!= c_seg_src
);
4226 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
4228 if (C_SEG_IS_ONDISK(c_seg_src
)) {
4230 * A "thaw" can mark a process as eligible for
4231 * another freeze cycle without bringing any of
4232 * its swapped out c_segs back from disk (because
4233 * that is done on-demand).
4235 * If the src c_seg we find for our pre-compressed
4236 * data is already on-disk, then we are dealing
4237 * with an app's data that is already packed and
4238 * swapped out. Don't do anything.
4241 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4243 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4250 if (c_seg_src
->c_busy
) {
4251 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4252 c_seg_wait_on_busy(c_seg_src
);
4256 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4261 C_SEG_BUSY(c_seg_src
);
4263 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4265 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4267 /* find the c_slot */
4268 c_indx
= src_slot
->s_cindx
;
4270 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, c_indx
);
4272 c_size
= UNPACK_C_SIZE(c_src
);
4276 if (c_size
> (uint32_t)(C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)c_seg_dst
->c_nextoffset
))) {
4278 * This segment is full. We need a new one.
4281 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4283 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
4284 C_SEG_WAKEUP_DONE(c_seg_src
);
4285 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4289 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
4291 assert(c_seg_dst
->c_busy
);
4292 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
4293 assert(!c_seg_dst
->c_on_minorcompact_q
);
4295 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
4296 assert(*current_chead
== NULL
);
4298 C_SEG_WAKEUP_DONE(c_seg_dst
);
4300 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
4304 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4309 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
4311 memcpy(&c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_nextoffset
], &c_seg_src
->c_store
.c_buffer
[c_src
->c_offset
], c_size
);
4312 //is platform alignment actually necessary since wkdm aligns its output?
4313 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
4315 cslot_copy(c_dst
, c_src
);
4316 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
4318 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
) {
4319 c_seg_dst
->c_firstemptyslot
++;
4322 c_seg_dst
->c_slots_used
++;
4323 c_seg_dst
->c_nextslot
++;
4324 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
4325 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
4328 PACK_C_SIZE(c_src
, 0);
4330 c_seg_src
->c_bytes_used
-= c_rounded_size
;
4331 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
4333 assert(c_seg_src
->c_slots_used
);
4334 c_seg_src
->c_slots_used
--;
4336 if (c_indx
< c_seg_src
->c_firstemptyslot
) {
4337 c_seg_src
->c_firstemptyslot
= c_indx
;
4340 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
4342 PAGE_REPLACEMENT_ALLOWED(TRUE
);
4343 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
4344 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
4345 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
4346 slot_ptr
->s_cindx
= dst_slot
;
4348 PAGE_REPLACEMENT_ALLOWED(FALSE
);
4352 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
4354 C_SEG_WAKEUP_DONE(c_seg_src
);
4356 if (c_seg_src
->c_bytes_used
== 0 && c_seg_src
->c_state
!= C_IS_FILLING
) {
4357 if (!c_seg_src
->c_on_minorcompact_q
) {
4358 c_seg_need_delayed_compaction(c_seg_src
, FALSE
);
4362 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4366 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4368 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
4370 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
4372 * Nearing or exceeded maximum slot and offset capacity.
4374 assert(c_seg_dst
->c_busy
);
4375 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
4376 assert(!c_seg_dst
->c_on_minorcompact_q
);
4378 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
4379 assert(*current_chead
== NULL
);
4382 C_SEG_WAKEUP_DONE(c_seg_dst
);
4384 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
4388 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4393 #endif /* CONFIG_FREEZE */