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 #if POPCOUNT_THE_COMPRESSED_DATA
54 boolean_t popcount_c_segs
= TRUE
;
56 static inline uint32_t vmc_pop(uintptr_t ins
, int sz
) {
59 if (__probable(popcount_c_segs
== FALSE
)) {
65 uint64_t *ins64
= (uint64_t *) ins
;
66 uint64_t *ins642
= (uint64_t *) (ins
+ 8);
67 rv1
= __builtin_popcountll(*ins64
);
68 rv2
= __builtin_popcountll(*ins642
);
75 uint32_t *ins32
= (uint32_t *) ins
;
76 rv
+= __builtin_popcount(*ins32
);
82 char *ins8
= (char *)ins
;
83 rv
+= __builtin_popcount(*ins8
);
92 * vm_compressor_mode has a heirarchy of control to set its value.
93 * boot-args are checked first, then device-tree, and finally
94 * the default value that is defined below. See vm_fault_init() for
95 * the boot-arg & device-tree code.
101 int vm_compressor_mode
= VM_PAGER_FREEZER_DEFAULT
;
103 void *freezer_chead
; /* The chead used to track c_segs allocated for the exclusive use of holding just one task's compressed memory.*/
104 char *freezer_compressor_scratch_buf
= NULL
;
106 #define VM_MAX_FREEZER_CSEG_SWAP_COUNT 64 /* The maximum number of c_segs holding just one task's compressed memory that can be swapped out to disk.*/
107 extern int c_freezer_swapout_count
; /* This count keeps track of the # of c_segs 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.*/
109 #else /* CONFIG_FREEZE */
110 int vm_compressor_mode
= VM_PAGER_NOT_CONFIGURED
;
111 #endif /* CONFIG_FREEZE */
115 #else /* CONFIG_EMBEDDED */
116 int vm_compressor_mode
= VM_PAGER_COMPRESSOR_WITH_SWAP
;
119 #endif /* CONFIG_EMBEDDED */
121 int vm_compressor_is_active
= 0;
122 int vm_compression_limit
= 0;
123 int vm_compressor_available
= 0;
125 extern void vm_pageout_io_throttle(void);
127 #if CHECKSUM_THE_DATA || CHECKSUM_THE_SWAP || CHECKSUM_THE_COMPRESSED_DATA
128 extern unsigned int hash_string(char *cp
, int len
);
129 static unsigned int vmc_hash(char *, int);
130 boolean_t checksum_c_segs
= TRUE
;
132 unsigned int vmc_hash(char *cp
, int len
) {
133 if (__probable(checksum_c_segs
== FALSE
)) {
136 return hash_string(cp
, len
);
140 #define UNPACK_C_SIZE(cs) ((cs->c_size == (PAGE_SIZE-1)) ? PAGE_SIZE : cs->c_size)
141 #define PACK_C_SIZE(cs, size) (cs->c_size = ((size == PAGE_SIZE) ? PAGE_SIZE - 1 : size))
144 struct c_sv_hash_entry
{
147 uint32_t c_sv_he_ref
;
148 uint32_t c_sv_he_data
;
150 uint64_t c_sv_he_record
;
155 #define he_ref c_sv_he_un.c_sv_he.c_sv_he_ref
156 #define he_data c_sv_he_un.c_sv_he.c_sv_he_data
157 #define he_record c_sv_he_un.c_sv_he_record
159 #define C_SV_HASH_MAX_MISS 32
160 #define C_SV_HASH_SIZE ((1 << 10))
161 #define C_SV_HASH_MASK ((1 << 10) - 1)
162 #define C_SV_CSEG_ID ((1 << 22) - 1)
172 #define C_SLOT_PACK_PTR(ptr) (((uintptr_t)ptr - (uintptr_t) KERNEL_PMAP_HEAP_RANGE_START) >> 2)
173 #define C_SLOT_UNPACK_PTR(cslot) ((uintptr_t)(cslot->c_packed_ptr << 2) + (uintptr_t) KERNEL_PMAP_HEAP_RANGE_START)
176 uint32_t c_segment_count
= 0;
177 uint32_t c_segment_count_max
= 0;
179 uint64_t c_generation_id
= 0;
180 uint64_t c_generation_id_flush_barrier
;
183 #define HIBERNATE_FLUSHING_SECS_TO_COMPLETE 120
185 boolean_t hibernate_no_swapspace
= FALSE
;
186 clock_sec_t hibernate_flushing_deadline
= 0;
189 #if RECORD_THE_COMPRESSED_DATA
190 char *c_compressed_record_sbuf
;
191 char *c_compressed_record_ebuf
;
192 char *c_compressed_record_cptr
;
196 queue_head_t c_age_list_head
;
197 queue_head_t c_swapout_list_head
;
198 queue_head_t c_swappedin_list_head
;
199 queue_head_t c_swappedout_list_head
;
200 queue_head_t c_swappedout_sparse_list_head
;
201 queue_head_t c_major_list_head
;
202 queue_head_t c_filling_list_head
;
203 queue_head_t c_bad_list_head
;
205 uint32_t c_age_count
= 0;
206 uint32_t c_swapout_count
= 0;
207 uint32_t c_swappedin_count
= 0;
208 uint32_t c_swappedout_count
= 0;
209 uint32_t c_swappedout_sparse_count
= 0;
210 uint32_t c_major_count
= 0;
211 uint32_t c_filling_count
= 0;
212 uint32_t c_empty_count
= 0;
213 uint32_t c_bad_count
= 0;
216 queue_head_t c_minor_list_head
;
217 uint32_t c_minor_count
= 0;
219 int c_overage_swapped_count
= 0;
220 int c_overage_swapped_limit
= 0;
222 int c_seg_fixed_array_len
;
223 union c_segu
*c_segments
;
224 vm_offset_t c_buffers
;
225 vm_size_t c_buffers_size
;
226 caddr_t c_segments_next_page
;
227 boolean_t c_segments_busy
;
228 uint32_t c_segments_available
;
229 uint32_t c_segments_limit
;
230 uint32_t c_segments_nearing_limit
;
232 uint32_t c_segment_svp_in_hash
;
233 uint32_t c_segment_svp_hash_succeeded
;
234 uint32_t c_segment_svp_hash_failed
;
235 uint32_t c_segment_svp_zero_compressions
;
236 uint32_t c_segment_svp_nonzero_compressions
;
237 uint32_t c_segment_svp_zero_decompressions
;
238 uint32_t c_segment_svp_nonzero_decompressions
;
240 uint32_t c_segment_noncompressible_pages
;
242 uint32_t c_segment_pages_compressed
;
243 uint32_t c_segment_pages_compressed_limit
;
244 uint32_t c_segment_pages_compressed_nearing_limit
;
245 uint32_t c_free_segno_head
= (uint32_t)-1;
247 uint32_t vm_compressor_minorcompact_threshold_divisor
= 10;
248 uint32_t vm_compressor_majorcompact_threshold_divisor
= 10;
249 uint32_t vm_compressor_unthrottle_threshold_divisor
= 10;
250 uint32_t vm_compressor_catchup_threshold_divisor
= 10;
252 #define C_SEGMENTS_PER_PAGE (PAGE_SIZE / sizeof(union c_segu))
255 lck_grp_attr_t vm_compressor_lck_grp_attr
;
256 lck_attr_t vm_compressor_lck_attr
;
257 lck_grp_t vm_compressor_lck_grp
;
258 lck_mtx_t
*c_list_lock
;
259 lck_rw_t c_master_lock
;
260 boolean_t decompressions_blocked
= FALSE
;
262 zone_t compressor_segment_zone
;
263 int c_compressor_swap_trigger
= 0;
265 uint32_t compressor_cpus
;
266 char *compressor_scratch_bufs
;
267 char *kdp_compressor_scratch_buf
;
268 char *kdp_compressor_decompressed_page
;
269 addr64_t kdp_compressor_decompressed_page_paddr
;
270 ppnum_t kdp_compressor_decompressed_page_ppnum
;
272 clock_sec_t start_of_sample_period_sec
= 0;
273 clock_nsec_t start_of_sample_period_nsec
= 0;
274 clock_sec_t start_of_eval_period_sec
= 0;
275 clock_nsec_t start_of_eval_period_nsec
= 0;
276 uint32_t sample_period_decompression_count
= 0;
277 uint32_t sample_period_compression_count
= 0;
278 uint32_t last_eval_decompression_count
= 0;
279 uint32_t last_eval_compression_count
= 0;
281 #define DECOMPRESSION_SAMPLE_MAX_AGE (60 * 30)
283 boolean_t vm_swapout_ripe_segments
= FALSE
;
284 uint32_t vm_ripe_target_age
= (60 * 60 * 48);
286 uint32_t swapout_target_age
= 0;
287 uint32_t age_of_decompressions_during_sample_period
[DECOMPRESSION_SAMPLE_MAX_AGE
];
288 uint32_t overage_decompressions_during_sample_period
= 0;
289 uint32_t vm_compressor_pages_grabbed
= 0;
292 void do_fastwake_warmup(queue_head_t
*, boolean_t
);
293 boolean_t fastwake_warmup
= FALSE
;
294 boolean_t fastwake_recording_in_progress
= FALSE
;
295 clock_sec_t dont_trim_until_ts
= 0;
297 uint64_t c_segment_warmup_count
;
298 uint64_t first_c_segment_to_warm_generation_id
= 0;
299 uint64_t last_c_segment_to_warm_generation_id
= 0;
300 boolean_t hibernate_flushing
= FALSE
;
302 int64_t c_segment_input_bytes
__attribute__((aligned(8))) = 0;
303 int64_t c_segment_compressed_bytes
__attribute__((aligned(8))) = 0;
304 int64_t compressor_bytes_used
__attribute__((aligned(8))) = 0;
307 struct c_sv_hash_entry c_segment_sv_hash_table
[C_SV_HASH_SIZE
] __attribute__ ((aligned (8)));
309 static boolean_t
compressor_needs_to_swap(void);
310 static void vm_compressor_swap_trigger_thread(void);
311 static void vm_compressor_do_delayed_compactions(boolean_t
);
312 static void vm_compressor_compact_and_swap(boolean_t
);
313 static void vm_compressor_age_swapped_in_segments(boolean_t
);
316 static void vm_compressor_take_paging_space_action(void);
319 void compute_swapout_target_age(void);
321 boolean_t
c_seg_major_compact(c_segment_t
, c_segment_t
);
322 boolean_t
c_seg_major_compact_ok(c_segment_t
, c_segment_t
);
324 int c_seg_minor_compaction_and_unlock(c_segment_t
, boolean_t
);
325 int c_seg_do_minor_compaction_and_unlock(c_segment_t
, boolean_t
, boolean_t
, boolean_t
);
326 void c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
);
328 void c_seg_move_to_sparse_list(c_segment_t
);
329 void c_seg_insert_into_q(queue_head_t
*, c_segment_t
);
331 uint64_t vm_available_memory(void);
332 uint64_t vm_compressor_pages_compressed(void);
335 * indicate the need to do a major compaction if
336 * the overall set of in-use compression segments
337 * becomes sparse... on systems that support pressure
338 * driven swapping, this will also cause swapouts to
341 static inline boolean_t
vm_compressor_needs_to_major_compact()
343 uint32_t incore_seg_count
;
345 incore_seg_count
= c_segment_count
- c_swappedout_count
- c_swappedout_sparse_count
;
347 if ((c_segment_count
>= (c_segments_nearing_limit
/ 8)) &&
348 ((incore_seg_count
* C_SEG_MAX_PAGES
) - VM_PAGE_COMPRESSOR_COUNT
) >
349 ((incore_seg_count
/ 8) * C_SEG_MAX_PAGES
))
356 vm_available_memory(void)
358 return (((uint64_t)AVAILABLE_NON_COMPRESSED_MEMORY
) * PAGE_SIZE_64
);
363 vm_compressor_pages_compressed(void)
365 return (c_segment_pages_compressed
* PAGE_SIZE_64
);
370 vm_compressor_low_on_space(void)
372 if ((c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) ||
373 (c_segment_count
> c_segments_nearing_limit
))
381 vm_compressor_out_of_space(void)
383 if ((c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) ||
384 (c_segment_count
>= c_segments_limit
))
392 vm_wants_task_throttled(task_t task
)
394 if (task
== kernel_task
)
397 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
398 if ((vm_compressor_low_on_space() || HARD_THROTTLE_LIMIT_REACHED()) &&
399 (unsigned int)pmap_compressed(task
->map
->pmap
) > (c_segment_pages_compressed
/ 4))
406 #if DEVELOPMENT || DEBUG
407 boolean_t kill_on_no_paging_space
= FALSE
; /* On compressor/swap exhaustion, kill the largest process regardless of
408 * its chosen process policy. Controlled by a boot-arg of the same name. */
409 #endif /* DEVELOPMENT || DEBUG */
413 static uint32_t no_paging_space_action_in_progress
= 0;
414 extern void memorystatus_send_low_swap_note(void);
417 vm_compressor_take_paging_space_action(void)
419 if (no_paging_space_action_in_progress
== 0) {
421 if (OSCompareAndSwap(0, 1, (UInt32
*)&no_paging_space_action_in_progress
)) {
423 if (no_paging_space_action()) {
424 #if DEVELOPMENT || DEBUG
425 if (kill_on_no_paging_space
== TRUE
) {
427 * Since we are choosing to always kill a process, we don't need the
428 * "out of application memory" dialog box in this mode. And, hence we won't
431 no_paging_space_action_in_progress
= 0;
434 #endif /* DEVELOPMENT || DEBUG */
435 memorystatus_send_low_swap_note();
438 no_paging_space_action_in_progress
= 0;
442 #endif /* !CONFIG_EMBEDDED */
446 vm_compressor_init_locks(void)
448 lck_grp_attr_setdefault(&vm_compressor_lck_grp_attr
);
449 lck_grp_init(&vm_compressor_lck_grp
, "vm_compressor", &vm_compressor_lck_grp_attr
);
450 lck_attr_setdefault(&vm_compressor_lck_attr
);
452 lck_rw_init(&c_master_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
457 vm_decompressor_lock(void)
459 PAGE_REPLACEMENT_ALLOWED(TRUE
);
461 decompressions_blocked
= TRUE
;
463 PAGE_REPLACEMENT_ALLOWED(FALSE
);
467 vm_decompressor_unlock(void)
469 PAGE_REPLACEMENT_ALLOWED(TRUE
);
471 decompressions_blocked
= FALSE
;
473 PAGE_REPLACEMENT_ALLOWED(FALSE
);
475 thread_wakeup((event_t
)&decompressions_blocked
);
478 static inline void cslot_copy(c_slot_t cdst
, c_slot_t csrc
) {
479 #if CHECKSUM_THE_DATA
480 cdst
->c_hash_data
= csrc
->c_hash_data
;
482 #if CHECKSUM_THE_COMPRESSED_DATA
483 cdst
->c_hash_compressed_data
= csrc
->c_hash_compressed_data
;
485 #if POPCOUNT_THE_COMPRESSED_DATA
486 cdst
->c_pop_cdata
= csrc
->c_pop_cdata
;
488 cdst
->c_size
= csrc
->c_size
;
489 cdst
->c_packed_ptr
= csrc
->c_packed_ptr
;
490 #if defined(__arm__) || defined(__arm64__)
491 cdst
->c_codec
= csrc
->c_codec
;
495 vm_map_t compressor_map
;
496 uint64_t compressor_pool_max_size
;
497 uint64_t compressor_pool_size
;
498 uint32_t compressor_pool_multiplier
;
500 #if DEVELOPMENT || DEBUG
502 * Compressor segments are write-protected in development/debug
503 * kernels to help debug memory corruption.
504 * In cases where performance is a concern, this can be disabled
505 * via the boot-arg "-disable_cseg_write_protection".
507 boolean_t write_protect_c_segs
= TRUE
;
508 int vm_compressor_test_seg_wp
;
509 uint32_t vm_ktrace_enabled
;
510 #endif /* DEVELOPMENT || DEBUG */
513 vm_compressor_init(void)
516 struct c_slot cs_dummy
;
517 c_slot_t cs
= &cs_dummy
;
518 int c_segment_min_size
;
519 int c_segment_padded_size
;
521 kern_return_t retval
= KERN_SUCCESS
;
522 vm_offset_t start_addr
= 0;
523 vm_size_t c_segments_arr_size
= 0, compressor_submap_size
= 0;
524 vm_map_kernel_flags_t vmk_flags
;
525 #if RECORD_THE_COMPRESSED_DATA
526 vm_size_t c_compressed_record_sbuf_size
= 0;
527 #endif /* RECORD_THE_COMPRESSED_DATA */
529 #if DEVELOPMENT || DEBUG
530 char bootarg_name
[32];
531 if (PE_parse_boot_argn("-kill_on_no_paging_space", bootarg_name
, sizeof (bootarg_name
))) {
532 kill_on_no_paging_space
= TRUE
;
534 if (PE_parse_boot_argn("-disable_cseg_write_protection", bootarg_name
, sizeof (bootarg_name
))) {
535 write_protect_c_segs
= FALSE
;
538 PE_parse_boot_argn("vm_compressor_validation", &vmcval
, sizeof(vmcval
));
540 if (kern_feature_override(KF_COMPRSV_OVRD
)) {
544 #if POPCOUNT_THE_COMPRESSED_DATA
545 popcount_c_segs
= FALSE
;
547 #if CHECKSUM_THE_DATA || CHECKSUM_THE_COMPRESSED_DATA
548 checksum_c_segs
= FALSE
;
550 write_protect_c_segs
= FALSE
;
552 #endif /* DEVELOPMENT || DEBUG */
555 * ensure that any pointer that gets created from
556 * the vm_page zone can be packed properly
558 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_min_address
);
560 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_min_address
)
561 panic("C_SLOT_UNPACK_PTR failed on zone_map_min_address - %p", (void *)zone_map_min_address
);
563 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_max_address
);
565 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_max_address
)
566 panic("C_SLOT_UNPACK_PTR failed on zone_map_max_address - %p", (void *)zone_map_max_address
);
569 assert((C_SEGMENTS_PER_PAGE
* sizeof(union c_segu
)) == PAGE_SIZE
);
571 PE_parse_boot_argn("vm_compression_limit", &vm_compression_limit
, sizeof (vm_compression_limit
));
573 #ifdef CONFIG_EMBEDDED
574 vm_compressor_minorcompact_threshold_divisor
= 20;
575 vm_compressor_majorcompact_threshold_divisor
= 30;
576 vm_compressor_unthrottle_threshold_divisor
= 40;
577 vm_compressor_catchup_threshold_divisor
= 60;
579 if (max_mem
<= (3ULL * 1024ULL * 1024ULL * 1024ULL)) {
580 vm_compressor_minorcompact_threshold_divisor
= 11;
581 vm_compressor_majorcompact_threshold_divisor
= 13;
582 vm_compressor_unthrottle_threshold_divisor
= 20;
583 vm_compressor_catchup_threshold_divisor
= 35;
585 vm_compressor_minorcompact_threshold_divisor
= 20;
586 vm_compressor_majorcompact_threshold_divisor
= 25;
587 vm_compressor_unthrottle_threshold_divisor
= 35;
588 vm_compressor_catchup_threshold_divisor
= 50;
592 * vm_page_init_lck_grp is now responsible for calling vm_compressor_init_locks
593 * c_master_lock needs to be available early so that "vm_page_find_contiguous" can
594 * use PAGE_REPLACEMENT_ALLOWED to coordinate with the compressor.
597 c_list_lock
= lck_mtx_alloc_init(&vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
599 queue_init(&c_bad_list_head
);
600 queue_init(&c_age_list_head
);
601 queue_init(&c_minor_list_head
);
602 queue_init(&c_major_list_head
);
603 queue_init(&c_filling_list_head
);
604 queue_init(&c_swapout_list_head
);
605 queue_init(&c_swappedin_list_head
);
606 queue_init(&c_swappedout_list_head
);
607 queue_init(&c_swappedout_sparse_list_head
);
609 c_free_segno_head
= -1;
610 c_segments_available
= 0;
612 if (vm_compression_limit
)
613 compressor_pool_size
= (uint64_t)vm_compression_limit
* PAGE_SIZE_64
;
615 compressor_pool_max_size
= C_SEG_MAX_LIMIT
;
616 compressor_pool_max_size
*= C_SEG_BUFSIZE
;
618 #if defined(__x86_64__)
620 if (vm_compression_limit
== 0) {
622 if (max_mem
<= (4ULL * 1024ULL * 1024ULL * 1024ULL))
623 compressor_pool_size
= 16ULL * max_mem
;
624 else if (max_mem
<= (8ULL * 1024ULL * 1024ULL * 1024ULL))
625 compressor_pool_size
= 8ULL * max_mem
;
626 else if (max_mem
<= (32ULL * 1024ULL * 1024ULL * 1024ULL))
627 compressor_pool_size
= 4ULL * max_mem
;
629 compressor_pool_size
= 2ULL * max_mem
;
631 if (max_mem
<= (8ULL * 1024ULL * 1024ULL * 1024ULL))
632 compressor_pool_multiplier
= 1;
633 else if (max_mem
<= (32ULL * 1024ULL * 1024ULL * 1024ULL))
634 compressor_pool_multiplier
= 2;
636 compressor_pool_multiplier
= 4;
638 #elif defined(__arm__)
640 #define VM_RESERVE_SIZE (1024 * 1024 * 256)
641 #define MAX_COMPRESSOR_POOL_SIZE (1024 * 1024 * 450)
643 if (compressor_pool_max_size
> MAX_COMPRESSOR_POOL_SIZE
)
644 compressor_pool_max_size
= MAX_COMPRESSOR_POOL_SIZE
;
646 if (vm_compression_limit
== 0)
647 compressor_pool_size
= ((kernel_map
->max_offset
- kernel_map
->min_offset
) - kernel_map
->size
) - VM_RESERVE_SIZE
;
648 compressor_pool_multiplier
= 1;
650 if (compressor_pool_max_size
> max_mem
)
651 compressor_pool_max_size
= max_mem
;
653 if (vm_compression_limit
== 0)
654 compressor_pool_size
= max_mem
;
655 compressor_pool_multiplier
= 1;
657 if (compressor_pool_size
> compressor_pool_max_size
)
658 compressor_pool_size
= compressor_pool_max_size
;
661 c_segments_limit
= (uint32_t)(compressor_pool_size
/ (vm_size_t
)(C_SEG_ALLOCSIZE
));
662 c_segments_nearing_limit
= (uint32_t)(((uint64_t)c_segments_limit
* 98ULL) / 100ULL);
664 c_segment_pages_compressed_limit
= (c_segments_limit
* (C_SEG_BUFSIZE
/ PAGE_SIZE
) * compressor_pool_multiplier
);
666 if (c_segment_pages_compressed_limit
< (uint32_t)(max_mem
/ PAGE_SIZE
))
667 c_segment_pages_compressed_limit
= (uint32_t)(max_mem
/ PAGE_SIZE
);
669 c_segment_pages_compressed_nearing_limit
= (uint32_t)(((uint64_t)c_segment_pages_compressed_limit
* 98ULL) / 100ULL);
672 * Submap needs space for:
675 * - swap reclaimations -- C_SEG_BUFSIZE
677 c_segments_arr_size
= vm_map_round_page((sizeof(union c_segu
) * c_segments_limit
), VM_MAP_PAGE_MASK(kernel_map
));
678 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
));
680 compressor_submap_size
= c_segments_arr_size
+ c_buffers_size
+ C_SEG_BUFSIZE
;
682 #if RECORD_THE_COMPRESSED_DATA
683 c_compressed_record_sbuf_size
= (vm_size_t
)C_SEG_ALLOCSIZE
+ (PAGE_SIZE
* 2);
684 compressor_submap_size
+= c_compressed_record_sbuf_size
;
685 #endif /* RECORD_THE_COMPRESSED_DATA */
687 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
688 vmk_flags
.vmkf_permanent
= TRUE
;
689 retval
= kmem_suballoc(kernel_map
, &start_addr
, compressor_submap_size
,
690 FALSE
, VM_FLAGS_ANYWHERE
, vmk_flags
, VM_KERN_MEMORY_COMPRESSOR
,
693 if (retval
!= KERN_SUCCESS
) {
695 panic("vm_compressor_init: kmem_suballoc failed - 0x%llx", (uint64_t)compressor_submap_size
);
697 compressor_pool_size
= compressor_pool_size
/ 2;
699 kprintf("retrying creation of the compressor submap at 0x%llx bytes\n", compressor_pool_size
);
702 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
)
703 panic("vm_compressor_init: kernel_memory_allocate failed - c_segments\n");
704 if (kernel_memory_allocate(compressor_map
, &c_buffers
, c_buffers_size
, 0, KMA_COMPRESSOR
| KMA_VAONLY
| KMA_PERMANENT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
)
705 panic("vm_compressor_init: kernel_memory_allocate failed - c_buffers\n");
708 c_segment_min_size
= sizeof(struct c_segment
) + (C_SEG_SLOT_VAR_ARRAY_MIN_LEN
* sizeof(struct c_slot
));
710 for (c_segment_padded_size
= 128; c_segment_padded_size
< c_segment_min_size
; c_segment_padded_size
= c_segment_padded_size
<< 1);
712 compressor_segment_zone
= zinit(c_segment_padded_size
, c_segments_limit
* c_segment_padded_size
, PAGE_SIZE
, "compressor_segment");
713 zone_change(compressor_segment_zone
, Z_CALLERACCT
, FALSE
);
714 zone_change(compressor_segment_zone
, Z_NOENCRYPT
, TRUE
);
716 c_seg_fixed_array_len
= (c_segment_padded_size
- sizeof(struct c_segment
)) / sizeof(struct c_slot
);
718 c_segments_busy
= FALSE
;
720 c_segments_next_page
= (caddr_t
)c_segments
;
721 vm_compressor_algorithm_init();
724 host_basic_info_data_t hinfo
;
725 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
728 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
730 compressor_cpus
= hinfo
.max_cpus
;
731 compressor_scratch_bufs
= kalloc_tag(compressor_cpus
* vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
733 kdp_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
734 kdp_compressor_decompressed_page
= kalloc_tag(PAGE_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
735 kdp_compressor_decompressed_page_paddr
= kvtophys((vm_offset_t
)kdp_compressor_decompressed_page
);
736 kdp_compressor_decompressed_page_ppnum
= (ppnum_t
) atop(kdp_compressor_decompressed_page_paddr
);
739 freezer_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_encode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
742 #if RECORD_THE_COMPRESSED_DATA
743 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
)
744 panic("vm_compressor_init: kernel_memory_allocate failed - c_compressed_record_sbuf\n");
746 c_compressed_record_cptr
= c_compressed_record_sbuf
;
747 c_compressed_record_ebuf
= c_compressed_record_sbuf
+ c_compressed_record_sbuf_size
;
750 if (kernel_thread_start_priority((thread_continue_t
)vm_compressor_swap_trigger_thread
, NULL
,
751 BASEPRI_VM
, &thread
) != KERN_SUCCESS
) {
752 panic("vm_compressor_swap_trigger_thread: create failed");
754 thread_deallocate(thread
);
756 if (vm_pageout_internal_start() != KERN_SUCCESS
) {
757 panic("vm_compressor_init: Failed to start the internal pageout thread.\n");
759 if (VM_CONFIG_SWAP_IS_PRESENT
)
760 vm_compressor_swap_init();
762 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE
)
763 vm_compressor_is_active
= 1;
766 memorystatus_freeze_enabled
= TRUE
;
767 #endif /* CONFIG_FREEZE */
769 vm_compressor_available
= 1;
771 vm_page_reactivate_all_throttled();
775 #if VALIDATE_C_SEGMENTS
778 c_seg_validate(c_segment_t c_seg
, boolean_t must_be_compact
)
782 uint32_t c_rounded_size
;
786 if (c_seg
->c_firstemptyslot
< c_seg
->c_nextslot
) {
787 c_indx
= c_seg
->c_firstemptyslot
;
788 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
791 panic("c_seg_validate: no slot backing c_firstemptyslot");
794 panic("c_seg_validate: c_firstemptyslot has non-zero size (%d)\n", cs
->c_size
);
798 for (c_indx
= 0; c_indx
< c_seg
->c_nextslot
; c_indx
++) {
800 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
802 c_size
= UNPACK_C_SIZE(cs
);
804 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
806 bytes_used
+= c_rounded_size
;
808 #if CHECKSUM_THE_COMPRESSED_DATA
810 if (c_size
&& cs
->c_hash_compressed_data
!= (csvhash
= vmc_hash((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))) {
811 addr64_t csvphys
= kvtophys((vm_offset_t
)&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
812 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
);
817 if (bytes_used
!= c_seg
->c_bytes_used
)
818 panic("c_seg_validate: bytes_used mismatch - found %d, segment has %d\n", bytes_used
, c_seg
->c_bytes_used
);
820 if (c_seg
->c_bytes_used
> C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
821 panic("c_seg_validate: c_bytes_used > c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
822 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
824 if (must_be_compact
) {
825 if (c_seg
->c_bytes_used
!= C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
826 panic("c_seg_validate: c_bytes_used doesn't match c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
827 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
835 c_seg_need_delayed_compaction(c_segment_t c_seg
, boolean_t c_list_lock_held
)
837 boolean_t clear_busy
= FALSE
;
839 if (c_list_lock_held
== FALSE
) {
840 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
843 lck_mtx_unlock_always(&c_seg
->c_lock
);
844 lck_mtx_lock_spin_always(c_list_lock
);
845 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
850 assert(c_seg
->c_state
!= C_IS_FILLING
);
852 if (!c_seg
->c_on_minorcompact_q
&& !(C_SEG_IS_ON_DISK_OR_SOQ(c_seg
))) {
853 queue_enter(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
854 c_seg
->c_on_minorcompact_q
= 1;
857 if (c_list_lock_held
== FALSE
)
858 lck_mtx_unlock_always(c_list_lock
);
860 if (clear_busy
== TRUE
)
861 C_SEG_WAKEUP_DONE(c_seg
);
865 unsigned int c_seg_moved_to_sparse_list
= 0;
868 c_seg_move_to_sparse_list(c_segment_t c_seg
)
870 boolean_t clear_busy
= FALSE
;
872 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
875 lck_mtx_unlock_always(&c_seg
->c_lock
);
876 lck_mtx_lock_spin_always(c_list_lock
);
877 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
881 c_seg_switch_state(c_seg
, C_ON_SWAPPEDOUTSPARSE_Q
, FALSE
);
883 c_seg_moved_to_sparse_list
++;
885 lck_mtx_unlock_always(c_list_lock
);
887 if (clear_busy
== TRUE
)
888 C_SEG_WAKEUP_DONE(c_seg
);
893 c_seg_insert_into_q(queue_head_t
*qhead
, c_segment_t c_seg
)
895 c_segment_t c_seg_next
;
897 if (queue_empty(qhead
)) {
898 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
900 c_seg_next
= (c_segment_t
)queue_first(qhead
);
904 if (c_seg
->c_generation_id
< c_seg_next
->c_generation_id
) {
905 queue_insert_before(qhead
, c_seg
, c_seg_next
, c_segment_t
, c_age_list
);
908 c_seg_next
= (c_segment_t
) queue_next(&c_seg_next
->c_age_list
);
910 if (queue_end(qhead
, (queue_entry_t
) c_seg_next
)) {
911 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
919 int try_minor_compaction_failed
= 0;
920 int try_minor_compaction_succeeded
= 0;
923 c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
)
926 assert(c_seg
->c_on_minorcompact_q
);
928 * c_seg is currently on the delayed minor compaction
929 * queue and we have c_seg locked... if we can get the
930 * c_list_lock w/o blocking (if we blocked we could deadlock
931 * because the lock order is c_list_lock then c_seg's lock)
932 * we'll pull it from the delayed list and free it directly
934 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
936 * c_list_lock is held, we need to bail
938 try_minor_compaction_failed
++;
940 lck_mtx_unlock_always(&c_seg
->c_lock
);
942 try_minor_compaction_succeeded
++;
945 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, FALSE
);
951 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
)
955 assert(c_seg
->c_busy
);
956 assert(!C_SEG_IS_ON_DISK_OR_SOQ(c_seg
));
959 * check for the case that can occur when we are not swapping
960 * and this segment has been major compacted in the past
961 * and moved to the majorcompact q to remove it from further
962 * consideration... if the occupancy falls too low we need
963 * to put it back on the age_q so that it will be considered
964 * in the next major compaction sweep... if we don't do this
965 * we will eventually run into the c_segments_limit
967 if (c_seg
->c_state
== C_ON_MAJORCOMPACT_Q
&& C_SEG_SHOULD_MAJORCOMPACT_NOW(c_seg
)) {
969 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
971 if (!c_seg
->c_on_minorcompact_q
) {
972 if (clear_busy
== TRUE
)
973 C_SEG_WAKEUP_DONE(c_seg
);
975 lck_mtx_unlock_always(&c_seg
->c_lock
);
979 queue_remove(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
980 c_seg
->c_on_minorcompact_q
= 0;
983 lck_mtx_unlock_always(c_list_lock
);
985 if (disallow_page_replacement
== TRUE
) {
986 lck_mtx_unlock_always(&c_seg
->c_lock
);
988 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
990 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
992 c_seg_freed
= c_seg_minor_compaction_and_unlock(c_seg
, clear_busy
);
994 if (disallow_page_replacement
== TRUE
)
995 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
997 if (need_list_lock
== TRUE
)
998 lck_mtx_lock_spin_always(c_list_lock
);
1000 return (c_seg_freed
);
1005 c_seg_wait_on_busy(c_segment_t c_seg
)
1007 c_seg
->c_wanted
= 1;
1008 assert_wait((event_t
) (c_seg
), THREAD_UNINT
);
1010 lck_mtx_unlock_always(&c_seg
->c_lock
);
1011 thread_block(THREAD_CONTINUE_NULL
);
1016 c_seg_switch_state(c_segment_t c_seg
, int new_state
, boolean_t insert_head
)
1018 int old_state
= c_seg
->c_state
;
1020 #if __i386__ || __x86_64__
1021 if (new_state
!= C_IS_FILLING
)
1022 LCK_MTX_ASSERT(&c_seg
->c_lock
, LCK_MTX_ASSERT_OWNED
);
1023 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
1025 switch (old_state
) {
1028 assert(new_state
== C_IS_FILLING
|| new_state
== C_IS_FREE
);
1034 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_ON_SWAPOUT_Q
);
1036 queue_remove(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
1041 assert(new_state
== C_ON_SWAPOUT_Q
|| new_state
== C_ON_MAJORCOMPACT_Q
||
1042 new_state
== C_IS_FREE
);
1044 queue_remove(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
1048 case C_ON_SWAPPEDIN_Q
:
1049 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
1051 queue_remove(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
1052 c_swappedin_count
--;
1055 case C_ON_SWAPOUT_Q
:
1056 assert(new_state
== C_ON_SWAPPEDOUT_Q
|| new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
1057 new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
|| new_state
== C_IS_EMPTY
);
1059 queue_remove(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1060 thread_wakeup((event_t
)&compaction_swapper_running
);
1064 case C_ON_SWAPPEDOUT_Q
:
1065 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
1066 new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
1067 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
1069 queue_remove(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1070 c_swappedout_count
--;
1073 case C_ON_SWAPPEDOUTSPARSE_Q
:
1074 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
1075 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
1077 queue_remove(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1078 c_swappedout_sparse_count
--;
1081 case C_ON_MAJORCOMPACT_Q
:
1082 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
1084 queue_remove(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1089 assert(new_state
== C_IS_FREE
);
1091 queue_remove(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1096 panic("c_seg %p has bad c_state = %d\n", c_seg
, old_state
);
1101 assert(old_state
!= C_IS_FILLING
);
1106 assert(old_state
== C_ON_SWAPOUT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1112 assert(old_state
== C_IS_EMPTY
);
1114 queue_enter(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
1119 assert(old_state
== C_IS_FILLING
|| old_state
== C_ON_SWAPPEDIN_Q
|| old_state
== C_ON_SWAPOUT_Q
||
1120 old_state
== C_ON_MAJORCOMPACT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1122 if (old_state
== C_IS_FILLING
)
1123 queue_enter(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
1125 if (!queue_empty(&c_age_list_head
)) {
1126 c_segment_t c_first
;
1128 c_first
= (c_segment_t
)queue_first(&c_age_list_head
);
1129 c_seg
->c_creation_ts
= c_first
->c_creation_ts
;
1131 queue_enter_first(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
1136 case C_ON_SWAPPEDIN_Q
:
1137 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1139 if (insert_head
== TRUE
)
1140 queue_enter_first(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
1142 queue_enter(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
1143 c_swappedin_count
++;
1146 case C_ON_SWAPOUT_Q
:
1147 assert(old_state
== C_ON_AGE_Q
|| old_state
== C_IS_FILLING
);
1149 if (insert_head
== TRUE
)
1150 queue_enter_first(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1152 queue_enter(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1156 case C_ON_SWAPPEDOUT_Q
:
1157 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
);
1159 if (insert_head
== TRUE
)
1160 queue_enter_first(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1162 queue_enter(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
1163 c_swappedout_count
++;
1166 case C_ON_SWAPPEDOUTSPARSE_Q
:
1167 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
);
1169 if (insert_head
== TRUE
)
1170 queue_enter_first(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1172 queue_enter(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1174 c_swappedout_sparse_count
++;
1177 case C_ON_MAJORCOMPACT_Q
:
1178 assert(c_seg
->c_state
== C_ON_AGE_Q
);
1180 if (insert_head
== TRUE
)
1181 queue_enter_first(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1183 queue_enter(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1188 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1190 if (insert_head
== TRUE
)
1191 queue_enter_first(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1193 queue_enter(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1198 panic("c_seg %p requesting bad c_state = %d\n", c_seg
, new_state
);
1200 c_seg
->c_state
= new_state
;
1206 c_seg_free(c_segment_t c_seg
)
1208 assert(c_seg
->c_busy
);
1210 lck_mtx_unlock_always(&c_seg
->c_lock
);
1211 lck_mtx_lock_spin_always(c_list_lock
);
1212 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1214 c_seg_free_locked(c_seg
);
1219 c_seg_free_locked(c_segment_t c_seg
)
1222 int pages_populated
= 0;
1223 int32_t *c_buffer
= NULL
;
1224 uint64_t c_swap_handle
= 0;
1226 assert(c_seg
->c_busy
);
1227 assert(c_seg
->c_slots_used
== 0);
1228 assert(!c_seg
->c_on_minorcompact_q
);
1229 assert(!c_seg
->c_busy_swapping
);
1231 if (c_seg
->c_overage_swap
== TRUE
) {
1232 c_overage_swapped_count
--;
1233 c_seg
->c_overage_swap
= FALSE
;
1235 if ( !(C_SEG_IS_ONDISK(c_seg
)))
1236 c_buffer
= c_seg
->c_store
.c_buffer
;
1238 c_swap_handle
= c_seg
->c_store
.c_swap_handle
;
1240 c_seg_switch_state(c_seg
, C_IS_FREE
, FALSE
);
1242 lck_mtx_unlock_always(c_list_lock
);
1245 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
1246 c_seg
->c_store
.c_buffer
= NULL
;
1248 c_seg
->c_store
.c_swap_handle
= (uint64_t)-1;
1250 lck_mtx_unlock_always(&c_seg
->c_lock
);
1253 if (pages_populated
)
1254 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
1256 } else if (c_swap_handle
) {
1258 * Free swap space on disk.
1260 vm_swap_free(c_swap_handle
);
1262 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1264 * c_seg must remain busy until
1265 * after the call to vm_swap_free
1267 C_SEG_WAKEUP_DONE(c_seg
);
1268 lck_mtx_unlock_always(&c_seg
->c_lock
);
1270 segno
= c_seg
->c_mysegno
;
1272 lck_mtx_lock_spin_always(c_list_lock
);
1274 * because the c_buffer is now associated with the segno,
1275 * we can't put the segno back on the free list until
1276 * after we have depopulated the c_buffer range, or
1277 * we run the risk of depopulating a range that is
1278 * now being used in one of the compressor heads
1280 c_segments
[segno
].c_segno
= c_free_segno_head
;
1281 c_free_segno_head
= segno
;
1284 lck_mtx_unlock_always(c_list_lock
);
1286 lck_mtx_destroy(&c_seg
->c_lock
, &vm_compressor_lck_grp
);
1288 if (c_seg
->c_slot_var_array_len
)
1289 kfree(c_seg
->c_slot_var_array
, sizeof(struct c_slot
) * c_seg
->c_slot_var_array_len
);
1291 zfree(compressor_segment_zone
, c_seg
);
1294 #if DEVELOPMENT || DEBUG
1295 int c_seg_trim_page_count
= 0;
1299 c_seg_trim_tail(c_segment_t c_seg
)
1304 uint32_t c_rounded_size
;
1305 uint16_t current_nextslot
;
1306 uint32_t current_populated_offset
;
1308 if (c_seg
->c_bytes_used
== 0)
1310 current_nextslot
= c_seg
->c_nextslot
;
1311 current_populated_offset
= c_seg
->c_populated_offset
;
1313 while (c_seg
->c_nextslot
) {
1315 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, (c_seg
->c_nextslot
- 1));
1317 c_size
= UNPACK_C_SIZE(cs
);
1320 if (current_nextslot
!= c_seg
->c_nextslot
) {
1321 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1322 c_offset
= cs
->c_offset
+ C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1324 c_seg
->c_nextoffset
= c_offset
;
1325 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) &
1326 ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1328 if (c_seg
->c_firstemptyslot
> c_seg
->c_nextslot
)
1329 c_seg
->c_firstemptyslot
= c_seg
->c_nextslot
;
1330 #if DEVELOPMENT || DEBUG
1331 c_seg_trim_page_count
+= ((round_page_32(C_SEG_OFFSET_TO_BYTES(current_populated_offset
)) -
1332 round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) /
1338 c_seg
->c_nextslot
--;
1340 assert(c_seg
->c_nextslot
);
1345 c_seg_minor_compaction_and_unlock(c_segment_t c_seg
, boolean_t clear_busy
)
1347 c_slot_mapping_t slot_ptr
;
1348 uint32_t c_offset
= 0;
1349 uint32_t old_populated_offset
;
1350 uint32_t c_rounded_size
;
1357 assert(c_seg
->c_busy
);
1359 #if VALIDATE_C_SEGMENTS
1360 c_seg_validate(c_seg
, FALSE
);
1362 if (c_seg
->c_bytes_used
== 0) {
1366 lck_mtx_unlock_always(&c_seg
->c_lock
);
1368 if (c_seg
->c_firstemptyslot
>= c_seg
->c_nextslot
|| C_SEG_UNUSED_BYTES(c_seg
) < PAGE_SIZE
)
1371 /* TODO: assert first emptyslot's c_size is actually 0 */
1373 #if DEVELOPMENT || DEBUG
1374 C_SEG_MAKE_WRITEABLE(c_seg
);
1377 #if VALIDATE_C_SEGMENTS
1378 c_seg
->c_was_minor_compacted
++;
1380 c_indx
= c_seg
->c_firstemptyslot
;
1381 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1383 old_populated_offset
= c_seg
->c_populated_offset
;
1384 c_offset
= c_dst
->c_offset
;
1386 for (i
= c_indx
+ 1; i
< c_seg
->c_nextslot
&& c_offset
< c_seg
->c_nextoffset
; i
++) {
1388 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg
, i
);
1390 c_size
= UNPACK_C_SIZE(c_src
);
1395 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1396 /* N.B.: This memcpy may be an overlapping copy */
1397 memcpy(&c_seg
->c_store
.c_buffer
[c_offset
], &c_seg
->c_store
.c_buffer
[c_src
->c_offset
], c_rounded_size
);
1399 cslot_copy(c_dst
, c_src
);
1400 c_dst
->c_offset
= c_offset
;
1402 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1403 slot_ptr
->s_cindx
= c_indx
;
1405 c_offset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1406 PACK_C_SIZE(c_src
, 0);
1409 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1411 c_seg
->c_firstemptyslot
= c_indx
;
1412 c_seg
->c_nextslot
= c_indx
;
1413 c_seg
->c_nextoffset
= c_offset
;
1414 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1415 c_seg
->c_bytes_unused
= 0;
1417 #if VALIDATE_C_SEGMENTS
1418 c_seg_validate(c_seg
, TRUE
);
1420 if (old_populated_offset
> c_seg
->c_populated_offset
) {
1424 gc_size
= C_SEG_OFFSET_TO_BYTES(old_populated_offset
- c_seg
->c_populated_offset
);
1425 gc_ptr
= &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
];
1427 kernel_memory_depopulate(compressor_map
, (vm_offset_t
)gc_ptr
, gc_size
, KMA_COMPRESSOR
);
1430 #if DEVELOPMENT || DEBUG
1431 C_SEG_WRITE_PROTECT(c_seg
);
1435 if (clear_busy
== TRUE
) {
1436 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1437 C_SEG_WAKEUP_DONE(c_seg
);
1438 lck_mtx_unlock_always(&c_seg
->c_lock
);
1445 c_seg_alloc_nextslot(c_segment_t c_seg
)
1447 struct c_slot
*old_slot_array
= NULL
;
1448 struct c_slot
*new_slot_array
= NULL
;
1452 if (c_seg
->c_nextslot
< c_seg_fixed_array_len
)
1455 if ((c_seg
->c_nextslot
- c_seg_fixed_array_len
) >= c_seg
->c_slot_var_array_len
) {
1457 oldlen
= c_seg
->c_slot_var_array_len
;
1458 old_slot_array
= c_seg
->c_slot_var_array
;
1461 newlen
= C_SEG_SLOT_VAR_ARRAY_MIN_LEN
;
1463 newlen
= oldlen
* 2;
1465 new_slot_array
= (struct c_slot
*)kalloc(sizeof(struct c_slot
) * newlen
);
1467 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1470 memcpy((char *)new_slot_array
, (char *)old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1472 c_seg
->c_slot_var_array_len
= newlen
;
1473 c_seg
->c_slot_var_array
= new_slot_array
;
1475 lck_mtx_unlock_always(&c_seg
->c_lock
);
1478 kfree(old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1485 uint64_t asked_permission
;
1486 uint64_t compactions
;
1487 uint64_t moved_slots
;
1488 uint64_t moved_bytes
;
1489 uint64_t wasted_space_in_swapouts
;
1490 uint64_t count_of_swapouts
;
1491 uint64_t count_of_freed_segs
;
1492 } c_seg_major_compact_stats
;
1495 #define C_MAJOR_COMPACTION_SIZE_APPROPRIATE ((C_SEG_BUFSIZE * 90) / 100)
1499 c_seg_major_compact_ok(
1500 c_segment_t c_seg_dst
,
1501 c_segment_t c_seg_src
)
1504 c_seg_major_compact_stats
.asked_permission
++;
1506 if (c_seg_src
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
&&
1507 c_seg_dst
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
)
1510 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1512 * destination segment is full... can't compact
1522 c_seg_major_compact(
1523 c_segment_t c_seg_dst
,
1524 c_segment_t c_seg_src
)
1526 c_slot_mapping_t slot_ptr
;
1527 uint32_t c_rounded_size
;
1533 boolean_t keep_compacting
= TRUE
;
1536 * segments are not locked but they are both marked c_busy
1537 * which keeps c_decompress from working on them...
1538 * we can safely allocate new pages, move compressed data
1539 * from c_seg_src to c_seg_dst and update both c_segment's
1540 * state w/o holding the master lock
1542 #if DEVELOPMENT || DEBUG
1543 C_SEG_MAKE_WRITEABLE(c_seg_dst
);
1546 #if VALIDATE_C_SEGMENTS
1547 c_seg_dst
->c_was_major_compacted
++;
1548 c_seg_src
->c_was_major_donor
++;
1550 c_seg_major_compact_stats
.compactions
++;
1552 dst_slot
= c_seg_dst
->c_nextslot
;
1554 for (i
= 0; i
< c_seg_src
->c_nextslot
; i
++) {
1556 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, i
);
1558 c_size
= UNPACK_C_SIZE(c_src
);
1561 /* BATCH: move what we have so far; */
1565 if (C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
- c_seg_dst
->c_nextoffset
) < (unsigned) c_size
) {
1566 int size_to_populate
;
1569 size_to_populate
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
);
1571 if (size_to_populate
== 0) {
1573 keep_compacting
= FALSE
;
1576 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
1577 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
1579 kernel_memory_populate(compressor_map
,
1580 (vm_offset_t
) &c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_populated_offset
],
1583 VM_KERN_MEMORY_COMPRESSOR
);
1585 c_seg_dst
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
1586 assert(C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
) <= C_SEG_BUFSIZE
);
1588 c_seg_alloc_nextslot(c_seg_dst
);
1590 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
1592 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
);
1594 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1596 c_seg_major_compact_stats
.moved_slots
++;
1597 c_seg_major_compact_stats
.moved_bytes
+= c_size
;
1599 cslot_copy(c_dst
, c_src
);
1600 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
1602 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
1603 c_seg_dst
->c_firstemptyslot
++;
1604 c_seg_dst
->c_slots_used
++;
1605 c_seg_dst
->c_nextslot
++;
1606 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
1607 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1609 PACK_C_SIZE(c_src
, 0);
1611 c_seg_src
->c_bytes_used
-= c_rounded_size
;
1612 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
1613 c_seg_src
->c_firstemptyslot
= 0;
1615 assert(c_seg_src
->c_slots_used
);
1616 c_seg_src
->c_slots_used
--;
1618 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1619 /* dest segment is now full */
1620 keep_compacting
= FALSE
;
1624 #if DEVELOPMENT || DEBUG
1625 C_SEG_WRITE_PROTECT(c_seg_dst
);
1627 if (dst_slot
< c_seg_dst
->c_nextslot
) {
1629 PAGE_REPLACEMENT_ALLOWED(TRUE
);
1631 * we've now locked out c_decompress from
1632 * converting the slot passed into it into
1633 * a c_segment_t which allows us to use
1634 * the backptr to change which c_segment and
1635 * index the slot points to
1637 while (dst_slot
< c_seg_dst
->c_nextslot
) {
1639 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
1641 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1642 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
1643 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
1644 slot_ptr
->s_cindx
= dst_slot
++;
1646 PAGE_REPLACEMENT_ALLOWED(FALSE
);
1648 return (keep_compacting
);
1653 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
)
1656 uint64_t start_msecs
;
1658 end_msecs
= (end_sec
* 1000) + end_nsec
/ 1000000;
1659 start_msecs
= (start_sec
* 1000) + start_nsec
/ 1000000;
1661 return (end_msecs
- start_msecs
);
1666 uint32_t compressor_eval_period_in_msecs
= 250;
1667 uint32_t compressor_sample_min_in_msecs
= 500;
1668 uint32_t compressor_sample_max_in_msecs
= 10000;
1669 uint32_t compressor_thrashing_threshold_per_10msecs
= 50;
1670 uint32_t compressor_thrashing_min_per_10msecs
= 20;
1672 /* When true, reset sample data next chance we get. */
1673 static boolean_t compressor_need_sample_reset
= FALSE
;
1675 extern uint32_t vm_page_filecache_min
;
1679 compute_swapout_target_age(void)
1681 clock_sec_t cur_ts_sec
;
1682 clock_nsec_t cur_ts_nsec
;
1683 uint32_t min_operations_needed_in_this_sample
;
1684 uint64_t elapsed_msecs_in_eval
;
1685 uint64_t elapsed_msecs_in_sample
;
1686 boolean_t need_eval_reset
= FALSE
;
1688 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
1690 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
);
1692 if (compressor_need_sample_reset
||
1693 elapsed_msecs_in_sample
>= compressor_sample_max_in_msecs
) {
1694 compressor_need_sample_reset
= TRUE
;
1695 need_eval_reset
= TRUE
;
1698 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
);
1700 if (elapsed_msecs_in_eval
< compressor_eval_period_in_msecs
)
1702 need_eval_reset
= TRUE
;
1704 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_START
, elapsed_msecs_in_eval
, sample_period_compression_count
, sample_period_decompression_count
, 0, 0);
1706 min_operations_needed_in_this_sample
= (compressor_thrashing_min_per_10msecs
* (uint32_t)elapsed_msecs_in_eval
) / 10;
1708 if ((sample_period_compression_count
- last_eval_compression_count
) < min_operations_needed_in_this_sample
||
1709 (sample_period_decompression_count
- last_eval_decompression_count
) < min_operations_needed_in_this_sample
) {
1711 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_compression_count
- last_eval_compression_count
,
1712 sample_period_decompression_count
- last_eval_decompression_count
, 0, 1, 0);
1714 swapout_target_age
= 0;
1716 compressor_need_sample_reset
= TRUE
;
1717 need_eval_reset
= TRUE
;
1720 last_eval_compression_count
= sample_period_compression_count
;
1721 last_eval_decompression_count
= sample_period_decompression_count
;
1723 if (elapsed_msecs_in_sample
< compressor_sample_min_in_msecs
) {
1725 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, 0, 0, 5, 0);
1728 if (sample_period_decompression_count
> ((compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10)) {
1730 uint64_t running_total
;
1731 uint64_t working_target
;
1732 uint64_t aging_target
;
1733 uint32_t oldest_age_of_csegs_sampled
= 0;
1734 uint64_t working_set_approximation
= 0;
1736 swapout_target_age
= 0;
1738 working_target
= (sample_period_decompression_count
/ 100) * 95; /* 95 percent */
1739 aging_target
= (sample_period_decompression_count
/ 100) * 1; /* 1 percent */
1742 for (oldest_age_of_csegs_sampled
= 0; oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
; oldest_age_of_csegs_sampled
++) {
1744 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1746 working_set_approximation
+= oldest_age_of_csegs_sampled
* age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1748 if (running_total
>= working_target
)
1751 if (oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
) {
1753 working_set_approximation
= (working_set_approximation
* 1000) / elapsed_msecs_in_sample
;
1755 if (working_set_approximation
< VM_PAGE_COMPRESSOR_COUNT
) {
1757 running_total
= overage_decompressions_during_sample_period
;
1759 for (oldest_age_of_csegs_sampled
= DECOMPRESSION_SAMPLE_MAX_AGE
- 1; oldest_age_of_csegs_sampled
; oldest_age_of_csegs_sampled
--) {
1760 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1762 if (running_total
>= aging_target
)
1765 swapout_target_age
= (uint32_t)cur_ts_sec
- oldest_age_of_csegs_sampled
;
1767 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 2, 0);
1769 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 0, 3, 0);
1772 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_target
, running_total
, 0, 4, 0);
1774 compressor_need_sample_reset
= TRUE
;
1775 need_eval_reset
= TRUE
;
1777 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_decompression_count
, (compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10, 0, 6, 0);
1779 if (compressor_need_sample_reset
== TRUE
) {
1780 bzero(age_of_decompressions_during_sample_period
, sizeof(age_of_decompressions_during_sample_period
));
1781 overage_decompressions_during_sample_period
= 0;
1783 start_of_sample_period_sec
= cur_ts_sec
;
1784 start_of_sample_period_nsec
= cur_ts_nsec
;
1785 sample_period_decompression_count
= 0;
1786 sample_period_compression_count
= 0;
1787 last_eval_decompression_count
= 0;
1788 last_eval_compression_count
= 0;
1789 compressor_need_sample_reset
= FALSE
;
1791 if (need_eval_reset
== TRUE
) {
1792 start_of_eval_period_sec
= cur_ts_sec
;
1793 start_of_eval_period_nsec
= cur_ts_nsec
;
1798 int compaction_swapper_init_now
= 0;
1799 int compaction_swapper_running
= 0;
1800 int compaction_swapper_awakened
= 0;
1801 int compaction_swapper_abort
= 0;
1805 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
1806 boolean_t
memorystatus_kill_on_FC_thrashing(boolean_t
);
1807 int compressor_thrashing_induced_jetsam
= 0;
1808 int filecache_thrashing_induced_jetsam
= 0;
1809 static boolean_t vm_compressor_thrashing_detected
= FALSE
;
1810 #endif /* CONFIG_JETSAM */
1813 compressor_needs_to_swap(void)
1815 boolean_t should_swap
= FALSE
;
1817 if (vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
) {
1823 clock_get_system_nanotime(&now
, &nsec
);
1826 lck_mtx_lock_spin_always(c_list_lock
);
1828 if ( !queue_empty(&c_age_list_head
)) {
1829 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1831 age
= now
- c_seg
->c_creation_ts
;
1833 lck_mtx_unlock_always(c_list_lock
);
1835 if (age
>= vm_ripe_target_age
)
1838 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
1839 if (COMPRESSOR_NEEDS_TO_SWAP()) {
1842 if (VM_PAGE_Q_THROTTLED(&vm_pageout_queue_external
) && vm_page_anonymous_count
< (vm_page_inactive_count
/ 20)) {
1845 if (vm_page_free_count
< (vm_page_free_reserved
- (COMPRESSOR_FREE_RESERVED_LIMIT
* 2)))
1848 compute_swapout_target_age();
1850 if (swapout_target_age
) {
1853 lck_mtx_lock_spin_always(c_list_lock
);
1855 if (!queue_empty(&c_age_list_head
)) {
1857 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1859 if (c_seg
->c_creation_ts
> swapout_target_age
)
1860 swapout_target_age
= 0;
1862 lck_mtx_unlock_always(c_list_lock
);
1864 #if CONFIG_PHANTOM_CACHE
1865 if (vm_phantom_cache_check_pressure())
1868 if (swapout_target_age
)
1872 if (should_swap
|| vm_compressor_low_on_space() == TRUE
) {
1874 if (vm_compressor_thrashing_detected
== FALSE
) {
1875 vm_compressor_thrashing_detected
= TRUE
;
1877 if (swapout_target_age
|| vm_compressor_low_on_space() == TRUE
) {
1878 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
1879 compressor_thrashing_induced_jetsam
++;
1881 memorystatus_kill_on_FC_thrashing(TRUE
/* async */);
1882 filecache_thrashing_induced_jetsam
++;
1886 * let the jetsam take precedence over
1887 * any major compactions we might have
1888 * been able to do... otherwise we run
1889 * the risk of doing major compactions
1890 * on segments we're about to free up
1891 * due to the jetsam activity.
1893 should_swap
= FALSE
;
1896 #endif /* CONFIG_JETSAM */
1898 if (should_swap
== FALSE
) {
1900 * vm_compressor_needs_to_major_compact returns true only if we're
1901 * about to run out of available compressor segments... in this
1902 * case, we absolutely need to run a major compaction even if
1903 * we've just kicked off a jetsam or we don't otherwise need to
1904 * swap... terminating objects releases
1905 * pages back to the uncompressed cache, but does not guarantee
1906 * that we will free up even a single compression segment
1908 should_swap
= vm_compressor_needs_to_major_compact();
1912 * returning TRUE when swap_supported == FALSE
1913 * will cause the major compaction engine to
1914 * run, but will not trigger any swapping...
1915 * segments that have been major compacted
1916 * will be moved to the majorcompact queue
1918 return (should_swap
);
1923 * This function is called from the jetsam thread after killing something to
1924 * mitigate thrashing.
1926 * We need to restart our thrashing detection heuristics since memory pressure
1927 * has potentially changed significantly, and we don't want to detect on old
1928 * data from before the jetsam.
1931 vm_thrashing_jetsam_done(void)
1933 vm_compressor_thrashing_detected
= FALSE
;
1935 /* Were we compressor-thrashing or filecache-thrashing? */
1936 if (swapout_target_age
) {
1937 swapout_target_age
= 0;
1938 compressor_need_sample_reset
= TRUE
;
1940 #if CONFIG_PHANTOM_CACHE
1942 vm_phantom_cache_restart_sample();
1946 #endif /* CONFIG_JETSAM */
1948 uint32_t vm_wake_compactor_swapper_calls
= 0;
1949 uint32_t vm_run_compactor_already_running
= 0;
1950 uint32_t vm_run_compactor_empty_minor_q
= 0;
1951 uint32_t vm_run_compactor_did_compact
= 0;
1952 uint32_t vm_run_compactor_waited
= 0;
1955 vm_run_compactor(void)
1957 if (c_segment_count
== 0)
1960 lck_mtx_lock_spin_always(c_list_lock
);
1962 if (c_minor_count
== 0) {
1963 vm_run_compactor_empty_minor_q
++;
1965 lck_mtx_unlock_always(c_list_lock
);
1968 if (compaction_swapper_running
) {
1970 if (vm_restricted_to_single_processor
== FALSE
) {
1971 vm_run_compactor_already_running
++;
1973 lck_mtx_unlock_always(c_list_lock
);
1976 vm_run_compactor_waited
++;
1978 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1980 lck_mtx_unlock_always(c_list_lock
);
1982 thread_block(THREAD_CONTINUE_NULL
);
1986 vm_run_compactor_did_compact
++;
1988 fastwake_warmup
= FALSE
;
1989 compaction_swapper_running
= 1;
1991 vm_compressor_do_delayed_compactions(FALSE
);
1993 compaction_swapper_running
= 0;
1995 lck_mtx_unlock_always(c_list_lock
);
1997 thread_wakeup((event_t
)&compaction_swapper_running
);
2002 vm_wake_compactor_swapper(void)
2004 if (compaction_swapper_running
|| compaction_swapper_awakened
|| c_segment_count
== 0)
2007 if (c_minor_count
|| vm_compressor_needs_to_major_compact()) {
2009 lck_mtx_lock_spin_always(c_list_lock
);
2011 fastwake_warmup
= FALSE
;
2013 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2015 vm_wake_compactor_swapper_calls
++;
2017 compaction_swapper_awakened
= 1;
2018 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2020 lck_mtx_unlock_always(c_list_lock
);
2026 vm_consider_swapping()
2028 c_segment_t c_seg
, c_seg_next
;
2032 assert(VM_CONFIG_SWAP_IS_PRESENT
);
2034 lck_mtx_lock_spin_always(c_list_lock
);
2036 compaction_swapper_abort
= 1;
2038 while (compaction_swapper_running
) {
2039 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2041 lck_mtx_unlock_always(c_list_lock
);
2043 thread_block(THREAD_CONTINUE_NULL
);
2045 lck_mtx_lock_spin_always(c_list_lock
);
2047 compaction_swapper_abort
= 0;
2048 compaction_swapper_running
= 1;
2050 vm_swapout_ripe_segments
= TRUE
;
2052 if (!queue_empty(&c_major_list_head
)) {
2054 clock_get_system_nanotime(&now
, &nsec
);
2056 c_seg
= (c_segment_t
)queue_first(&c_major_list_head
);
2058 while (!queue_end(&c_major_list_head
, (queue_entry_t
)c_seg
)) {
2060 if (c_overage_swapped_count
>= c_overage_swapped_limit
)
2063 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2065 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2067 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2069 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2071 lck_mtx_unlock_always(&c_seg
->c_lock
);
2076 vm_compressor_compact_and_swap(FALSE
);
2078 compaction_swapper_running
= 0;
2080 vm_swapout_ripe_segments
= FALSE
;
2082 lck_mtx_unlock_always(c_list_lock
);
2084 thread_wakeup((event_t
)&compaction_swapper_running
);
2089 vm_consider_waking_compactor_swapper(void)
2091 boolean_t need_wakeup
= FALSE
;
2093 if (c_segment_count
== 0)
2096 if (compaction_swapper_running
|| compaction_swapper_awakened
)
2099 if (!compaction_swapper_inited
&& !compaction_swapper_init_now
) {
2100 compaction_swapper_init_now
= 1;
2104 if (c_minor_count
&& (COMPRESSOR_NEEDS_TO_MINOR_COMPACT())) {
2108 } else if (compressor_needs_to_swap()) {
2112 } else if (c_minor_count
) {
2113 uint64_t total_bytes
;
2115 total_bytes
= compressor_object
->resident_page_count
* PAGE_SIZE_64
;
2117 if ((total_bytes
- compressor_bytes_used
) > total_bytes
/ 10)
2120 if (need_wakeup
== TRUE
) {
2122 lck_mtx_lock_spin_always(c_list_lock
);
2124 fastwake_warmup
= FALSE
;
2126 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2127 memoryshot(VM_WAKEUP_COMPACTOR_SWAPPER
, DBG_FUNC_NONE
);
2129 compaction_swapper_awakened
= 1;
2130 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2132 lck_mtx_unlock_always(c_list_lock
);
2137 #define C_SWAPOUT_LIMIT 4
2138 #define DELAYED_COMPACTIONS_PER_PASS 30
2141 vm_compressor_do_delayed_compactions(boolean_t flush_all
)
2144 int number_compacted
= 0;
2145 boolean_t needs_to_swap
= FALSE
;
2148 #if !CONFIG_EMBEDDED
2149 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
2150 #endif /* !CONFIG_EMBEDDED */
2152 while (!queue_empty(&c_minor_list_head
) && needs_to_swap
== FALSE
) {
2154 c_seg
= (c_segment_t
)queue_first(&c_minor_list_head
);
2156 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2158 if (c_seg
->c_busy
) {
2160 lck_mtx_unlock_always(c_list_lock
);
2161 c_seg_wait_on_busy(c_seg
);
2162 lck_mtx_lock_spin_always(c_list_lock
);
2168 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, TRUE
);
2170 if (VM_CONFIG_SWAP_IS_ACTIVE
&& (number_compacted
++ > DELAYED_COMPACTIONS_PER_PASS
)) {
2172 if ((flush_all
== TRUE
|| compressor_needs_to_swap() == TRUE
) && c_swapout_count
< C_SWAPOUT_LIMIT
)
2173 needs_to_swap
= TRUE
;
2175 number_compacted
= 0;
2177 lck_mtx_lock_spin_always(c_list_lock
);
2182 #define C_SEGMENT_SWAPPEDIN_AGE_LIMIT 10
2185 vm_compressor_age_swapped_in_segments(boolean_t flush_all
)
2191 clock_get_system_nanotime(&now
, &nsec
);
2193 while (!queue_empty(&c_swappedin_list_head
)) {
2195 c_seg
= (c_segment_t
)queue_first(&c_swappedin_list_head
);
2197 if (flush_all
== FALSE
&& (now
- c_seg
->c_swappedin_ts
) < C_SEGMENT_SWAPPEDIN_AGE_LIMIT
)
2200 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2202 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2204 lck_mtx_unlock_always(&c_seg
->c_lock
);
2209 extern int vm_num_swap_files
;
2210 extern int vm_num_pinned_swap_files
;
2211 extern int vm_swappin_enabled
;
2213 extern unsigned int vm_swapfile_total_segs_used
;
2214 extern unsigned int vm_swapfile_total_segs_alloced
;
2218 vm_compressor_flush(void)
2220 uint64_t vm_swap_put_failures_at_start
;
2221 wait_result_t wait_result
= 0;
2222 AbsoluteTime startTime
, endTime
;
2223 clock_sec_t now_sec
;
2224 clock_nsec_t now_nsec
;
2227 HIBLOG("vm_compressor_flush - starting\n");
2229 clock_get_uptime(&startTime
);
2231 lck_mtx_lock_spin_always(c_list_lock
);
2233 fastwake_warmup
= FALSE
;
2234 compaction_swapper_abort
= 1;
2236 while (compaction_swapper_running
) {
2237 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2239 lck_mtx_unlock_always(c_list_lock
);
2241 thread_block(THREAD_CONTINUE_NULL
);
2243 lck_mtx_lock_spin_always(c_list_lock
);
2245 compaction_swapper_abort
= 0;
2246 compaction_swapper_running
= 1;
2248 hibernate_flushing
= TRUE
;
2249 hibernate_no_swapspace
= FALSE
;
2250 c_generation_id_flush_barrier
= c_generation_id
+ 1000;
2252 clock_get_system_nanotime(&now_sec
, &now_nsec
);
2253 hibernate_flushing_deadline
= now_sec
+ HIBERNATE_FLUSHING_SECS_TO_COMPLETE
;
2255 vm_swap_put_failures_at_start
= vm_swap_put_failures
;
2257 vm_compressor_compact_and_swap(TRUE
);
2259 while (!queue_empty(&c_swapout_list_head
)) {
2261 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 5000, 1000*NSEC_PER_USEC
);
2263 lck_mtx_unlock_always(c_list_lock
);
2265 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2267 lck_mtx_lock_spin_always(c_list_lock
);
2269 if (wait_result
== THREAD_TIMED_OUT
)
2272 hibernate_flushing
= FALSE
;
2273 compaction_swapper_running
= 0;
2275 if (vm_swap_put_failures
> vm_swap_put_failures_at_start
)
2276 HIBLOG("vm_compressor_flush failed to clean %llu segments - vm_page_compressor_count(%d)\n",
2277 vm_swap_put_failures
- vm_swap_put_failures_at_start
, VM_PAGE_COMPRESSOR_COUNT
);
2279 lck_mtx_unlock_always(c_list_lock
);
2281 thread_wakeup((event_t
)&compaction_swapper_running
);
2283 clock_get_uptime(&endTime
);
2284 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2285 absolutetime_to_nanoseconds(endTime
, &nsec
);
2287 HIBLOG("vm_compressor_flush completed - took %qd msecs - vm_num_swap_files = %d, vm_num_pinned_swap_files = %d, vm_swappin_enabled = %d\n",
2288 nsec
/ 1000000ULL, vm_num_swap_files
, vm_num_pinned_swap_files
, vm_swappin_enabled
);
2292 int compaction_swap_trigger_thread_awakened
= 0;
2295 vm_compressor_swap_trigger_thread(void)
2297 current_thread()->options
|= TH_OPT_VMPRIV
;
2300 * compaction_swapper_init_now is set when the first call to
2301 * vm_consider_waking_compactor_swapper is made from
2302 * vm_pageout_scan... since this function is called upon
2303 * thread creation, we want to make sure to delay adjusting
2304 * the tuneables until we are awakened via vm_pageout_scan
2305 * so that we are at a point where the vm_swapfile_open will
2306 * be operating on the correct directory (in case the default
2307 * of /var/vm/ is overridden by the dymanic_pager
2309 if (compaction_swapper_init_now
) {
2310 vm_compaction_swapper_do_init();
2312 if (vm_restricted_to_single_processor
== TRUE
)
2313 thread_vm_bind_group_add();
2314 thread_set_thread_name(current_thread(), "VM_cswap_trigger");
2315 compaction_swapper_init_now
= 0;
2317 lck_mtx_lock_spin_always(c_list_lock
);
2319 compaction_swap_trigger_thread_awakened
++;
2320 compaction_swapper_awakened
= 0;
2322 if (compaction_swapper_running
== 0) {
2324 compaction_swapper_running
= 1;
2326 vm_compressor_compact_and_swap(FALSE
);
2328 compaction_swapper_running
= 0;
2330 assert_wait((event_t
)&c_compressor_swap_trigger
, THREAD_UNINT
);
2332 if (compaction_swapper_running
== 0)
2333 thread_wakeup((event_t
)&compaction_swapper_running
);
2335 lck_mtx_unlock_always(c_list_lock
);
2337 thread_block((thread_continue_t
)vm_compressor_swap_trigger_thread
);
2344 vm_compressor_record_warmup_start(void)
2348 lck_mtx_lock_spin_always(c_list_lock
);
2350 if (first_c_segment_to_warm_generation_id
== 0) {
2351 if (!queue_empty(&c_age_list_head
)) {
2353 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2355 first_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2357 first_c_segment_to_warm_generation_id
= 0;
2359 fastwake_recording_in_progress
= TRUE
;
2361 lck_mtx_unlock_always(c_list_lock
);
2366 vm_compressor_record_warmup_end(void)
2370 lck_mtx_lock_spin_always(c_list_lock
);
2372 if (fastwake_recording_in_progress
== TRUE
) {
2374 if (!queue_empty(&c_age_list_head
)) {
2376 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2378 last_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2380 last_c_segment_to_warm_generation_id
= first_c_segment_to_warm_generation_id
;
2382 fastwake_recording_in_progress
= FALSE
;
2384 HIBLOG("vm_compressor_record_warmup (%qd - %qd)\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2386 lck_mtx_unlock_always(c_list_lock
);
2390 #define DELAY_TRIM_ON_WAKE_SECS 25
2393 vm_compressor_delay_trim(void)
2398 clock_get_system_nanotime(&sec
, &nsec
);
2399 dont_trim_until_ts
= sec
+ DELAY_TRIM_ON_WAKE_SECS
;
2404 vm_compressor_do_warmup(void)
2406 lck_mtx_lock_spin_always(c_list_lock
);
2408 if (first_c_segment_to_warm_generation_id
== last_c_segment_to_warm_generation_id
) {
2409 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2411 lck_mtx_unlock_always(c_list_lock
);
2415 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2417 fastwake_warmup
= TRUE
;
2419 compaction_swapper_awakened
= 1;
2420 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2422 lck_mtx_unlock_always(c_list_lock
);
2426 do_fastwake_warmup_all(void)
2429 lck_mtx_lock_spin_always(c_list_lock
);
2431 if (queue_empty(&c_swappedout_list_head
) && queue_empty(&c_swappedout_sparse_list_head
)) {
2433 lck_mtx_unlock_always(c_list_lock
);
2437 fastwake_warmup
= TRUE
;
2439 do_fastwake_warmup(&c_swappedout_list_head
, TRUE
);
2441 do_fastwake_warmup(&c_swappedout_sparse_list_head
, TRUE
);
2443 fastwake_warmup
= FALSE
;
2445 lck_mtx_unlock_always(c_list_lock
);
2450 do_fastwake_warmup(queue_head_t
*c_queue
, boolean_t consider_all_cseg
)
2452 c_segment_t c_seg
= NULL
;
2453 AbsoluteTime startTime
, endTime
;
2457 HIBLOG("vm_compressor_fastwake_warmup (%qd - %qd) - starting\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2459 clock_get_uptime(&startTime
);
2461 lck_mtx_unlock_always(c_list_lock
);
2463 proc_set_thread_policy(current_thread(),
2464 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER2
);
2466 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2468 lck_mtx_lock_spin_always(c_list_lock
);
2470 while (!queue_empty(c_queue
) && fastwake_warmup
== TRUE
) {
2472 c_seg
= (c_segment_t
) queue_first(c_queue
);
2474 if (consider_all_cseg
== FALSE
) {
2475 if (c_seg
->c_generation_id
< first_c_segment_to_warm_generation_id
||
2476 c_seg
->c_generation_id
> last_c_segment_to_warm_generation_id
)
2479 if (vm_page_free_count
< (AVAILABLE_MEMORY
/ 4))
2483 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2484 lck_mtx_unlock_always(c_list_lock
);
2486 if (c_seg
->c_busy
) {
2487 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2488 c_seg_wait_on_busy(c_seg
);
2489 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2491 if (c_seg_swapin(c_seg
, TRUE
, FALSE
) == 0)
2492 lck_mtx_unlock_always(&c_seg
->c_lock
);
2493 c_segment_warmup_count
++;
2495 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2496 vm_pageout_io_throttle();
2497 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2499 lck_mtx_lock_spin_always(c_list_lock
);
2501 lck_mtx_unlock_always(c_list_lock
);
2503 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2505 proc_set_thread_policy(current_thread(),
2506 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER0
);
2508 clock_get_uptime(&endTime
);
2509 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2510 absolutetime_to_nanoseconds(endTime
, &nsec
);
2512 HIBLOG("vm_compressor_fastwake_warmup completed - took %qd msecs\n", nsec
/ 1000000ULL);
2514 lck_mtx_lock_spin_always(c_list_lock
);
2516 if (consider_all_cseg
== FALSE
) {
2517 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2523 vm_compressor_compact_and_swap(boolean_t flush_all
)
2525 c_segment_t c_seg
, c_seg_next
;
2526 boolean_t keep_compacting
;
2531 if (fastwake_warmup
== TRUE
) {
2532 uint64_t starting_warmup_count
;
2534 starting_warmup_count
= c_segment_warmup_count
;
2536 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 11) | DBG_FUNC_START
, c_segment_warmup_count
,
2537 first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
, 0, 0);
2538 do_fastwake_warmup(&c_swappedout_list_head
, FALSE
);
2539 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);
2541 fastwake_warmup
= FALSE
;
2545 * it's possible for the c_age_list_head to be empty if we
2546 * hit our limits for growing the compressor pool and we subsequently
2547 * hibernated... on the next hibernation we could see the queue as
2548 * empty and not proceeed even though we have a bunch of segments on
2549 * the swapped in queue that need to be dealt with.
2551 vm_compressor_do_delayed_compactions(flush_all
);
2553 vm_compressor_age_swapped_in_segments(flush_all
);
2556 * we only need to grab the timestamp once per
2557 * invocation of this function since the
2558 * timescale we're interested in is measured
2561 clock_get_system_nanotime(&now
, &nsec
);
2563 while (!queue_empty(&c_age_list_head
) && compaction_swapper_abort
== 0) {
2565 if (hibernate_flushing
== TRUE
) {
2568 if (hibernate_should_abort()) {
2569 HIBLOG("vm_compressor_flush - hibernate_should_abort returned TRUE\n");
2572 if (hibernate_no_swapspace
== TRUE
) {
2573 HIBLOG("vm_compressor_flush - out of swap space\n");
2576 if (vm_swap_files_pinned() == FALSE
) {
2577 HIBLOG("vm_compressor_flush - unpinned swap files\n");
2580 if (hibernate_in_progress_with_pinned_swap
== TRUE
&&
2581 (vm_swapfile_total_segs_alloced
== vm_swapfile_total_segs_used
)) {
2582 HIBLOG("vm_compressor_flush - out of pinned swap space\n");
2585 clock_get_system_nanotime(&sec
, &nsec
);
2587 if (sec
> hibernate_flushing_deadline
) {
2588 HIBLOG("vm_compressor_flush - failed to finish before deadline\n");
2592 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2594 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 100, 1000*NSEC_PER_USEC
);
2596 lck_mtx_unlock_always(c_list_lock
);
2598 thread_block(THREAD_CONTINUE_NULL
);
2600 lck_mtx_lock_spin_always(c_list_lock
);
2605 vm_compressor_do_delayed_compactions(flush_all
);
2607 vm_compressor_age_swapped_in_segments(flush_all
);
2609 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2611 * we timed out on the above thread_block
2612 * let's loop around and try again
2613 * the timeout allows us to continue
2614 * to do minor compactions to make
2615 * more memory available
2621 * Swap out segments?
2623 if (flush_all
== FALSE
) {
2624 boolean_t needs_to_swap
;
2626 lck_mtx_unlock_always(c_list_lock
);
2628 needs_to_swap
= compressor_needs_to_swap();
2630 #if !CONFIG_EMBEDDED
2631 if (needs_to_swap
== TRUE
&& vm_swap_low_on_space())
2632 vm_compressor_take_paging_space_action();
2633 #endif /* !CONFIG_EMBEDDED */
2635 lck_mtx_lock_spin_always(c_list_lock
);
2637 if (needs_to_swap
== FALSE
)
2640 if (queue_empty(&c_age_list_head
))
2642 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
2644 assert(c_seg
->c_state
== C_ON_AGE_Q
);
2646 if (flush_all
== TRUE
&& c_seg
->c_generation_id
> c_generation_id_flush_barrier
)
2649 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2651 if (c_seg
->c_busy
) {
2653 lck_mtx_unlock_always(c_list_lock
);
2654 c_seg_wait_on_busy(c_seg
);
2655 lck_mtx_lock_spin_always(c_list_lock
);
2661 if (c_seg_do_minor_compaction_and_unlock(c_seg
, FALSE
, TRUE
, TRUE
)) {
2663 * found an empty c_segment and freed it
2664 * so go grab the next guy in the queue
2666 c_seg_major_compact_stats
.count_of_freed_segs
++;
2672 keep_compacting
= TRUE
;
2674 while (keep_compacting
== TRUE
) {
2676 assert(c_seg
->c_busy
);
2678 /* look for another segment to consolidate */
2680 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2682 if (queue_end(&c_age_list_head
, (queue_entry_t
)c_seg_next
))
2685 assert(c_seg_next
->c_state
== C_ON_AGE_Q
);
2687 if (c_seg_major_compact_ok(c_seg
, c_seg_next
) == FALSE
)
2690 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2692 if (c_seg_next
->c_busy
) {
2694 lck_mtx_unlock_always(c_list_lock
);
2695 c_seg_wait_on_busy(c_seg_next
);
2696 lck_mtx_lock_spin_always(c_list_lock
);
2700 /* grab that segment */
2701 C_SEG_BUSY(c_seg_next
);
2703 if (c_seg_do_minor_compaction_and_unlock(c_seg_next
, FALSE
, TRUE
, TRUE
)) {
2705 * found an empty c_segment and freed it
2706 * so we can't continue to use c_seg_next
2708 c_seg_major_compact_stats
.count_of_freed_segs
++;
2712 /* unlock the list ... */
2713 lck_mtx_unlock_always(c_list_lock
);
2715 /* do the major compaction */
2717 keep_compacting
= c_seg_major_compact(c_seg
, c_seg_next
);
2719 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2721 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2723 * run a minor compaction on the donor segment
2724 * since we pulled at least some of it's
2725 * data into our target... if we've emptied
2726 * it, now is a good time to free it which
2727 * c_seg_minor_compaction_and_unlock also takes care of
2729 * by passing TRUE, we ask for c_busy to be cleared
2730 * and c_wanted to be taken care of
2732 if (c_seg_minor_compaction_and_unlock(c_seg_next
, TRUE
))
2733 c_seg_major_compact_stats
.count_of_freed_segs
++;
2735 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2737 /* relock the list */
2738 lck_mtx_lock_spin_always(c_list_lock
);
2740 } /* major compaction */
2742 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2744 assert(c_seg
->c_busy
);
2745 assert(!c_seg
->c_on_minorcompact_q
);
2747 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
2749 * This mode of putting a generic c_seg on the swapout list is
2750 * only supported when we have general swapping enabled
2752 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2754 if ((vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
)) {
2756 assert(VM_CONFIG_SWAP_IS_PRESENT
);
2758 * we are running compressor sweeps with swap-behind
2759 * make sure the c_seg has aged enough before swapping it
2762 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2763 c_seg
->c_overage_swap
= TRUE
;
2764 c_overage_swapped_count
++;
2765 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2769 if (c_seg
->c_state
== C_ON_AGE_Q
) {
2771 * this c_seg didn't get moved to the swapout queue
2772 * so we need to move it out of the way...
2773 * we just did a major compaction on it so put it
2776 c_seg_switch_state(c_seg
, C_ON_MAJORCOMPACT_Q
, FALSE
);
2778 c_seg_major_compact_stats
.wasted_space_in_swapouts
+= C_SEG_BUFSIZE
- c_seg
->c_bytes_used
;
2779 c_seg_major_compact_stats
.count_of_swapouts
++;
2781 C_SEG_WAKEUP_DONE(c_seg
);
2783 lck_mtx_unlock_always(&c_seg
->c_lock
);
2785 if (c_swapout_count
) {
2786 lck_mtx_unlock_always(c_list_lock
);
2788 thread_wakeup((event_t
)&c_swapout_list_head
);
2790 lck_mtx_lock_spin_always(c_list_lock
);
2797 c_seg_allocate(c_segment_t
*current_chead
)
2801 int size_to_populate
;
2803 #if !CONFIG_EMBEDDED
2804 if (vm_compressor_low_on_space())
2805 vm_compressor_take_paging_space_action();
2806 #endif /* !CONFIG_EMBEDDED */
2808 if ( (c_seg
= *current_chead
) == NULL
) {
2811 lck_mtx_lock_spin_always(c_list_lock
);
2813 while (c_segments_busy
== TRUE
) {
2814 assert_wait((event_t
) (&c_segments_busy
), THREAD_UNINT
);
2816 lck_mtx_unlock_always(c_list_lock
);
2818 thread_block(THREAD_CONTINUE_NULL
);
2820 lck_mtx_lock_spin_always(c_list_lock
);
2822 if (c_free_segno_head
== (uint32_t)-1) {
2823 uint32_t c_segments_available_new
;
2825 if (c_segments_available
>= c_segments_limit
|| c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) {
2826 lck_mtx_unlock_always(c_list_lock
);
2830 c_segments_busy
= TRUE
;
2831 lck_mtx_unlock_always(c_list_lock
);
2833 kernel_memory_populate(compressor_map
, (vm_offset_t
)c_segments_next_page
,
2834 PAGE_SIZE
, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
);
2835 c_segments_next_page
+= PAGE_SIZE
;
2837 c_segments_available_new
= c_segments_available
+ C_SEGMENTS_PER_PAGE
;
2839 if (c_segments_available_new
> c_segments_limit
)
2840 c_segments_available_new
= c_segments_limit
;
2842 for (c_segno
= c_segments_available
+ 1; c_segno
< c_segments_available_new
; c_segno
++)
2843 c_segments
[c_segno
- 1].c_segno
= c_segno
;
2845 lck_mtx_lock_spin_always(c_list_lock
);
2847 c_segments
[c_segno
- 1].c_segno
= c_free_segno_head
;
2848 c_free_segno_head
= c_segments_available
;
2849 c_segments_available
= c_segments_available_new
;
2851 c_segments_busy
= FALSE
;
2852 thread_wakeup((event_t
) (&c_segments_busy
));
2854 c_segno
= c_free_segno_head
;
2855 assert(c_segno
>= 0 && c_segno
< c_segments_limit
);
2857 c_free_segno_head
= (uint32_t)c_segments
[c_segno
].c_segno
;
2860 * do the rest of the bookkeeping now while we're still behind
2861 * the list lock and grab our generation id now into a local
2862 * so that we can install it once we have the c_seg allocated
2865 if (c_segment_count
> c_segment_count_max
)
2866 c_segment_count_max
= c_segment_count
;
2868 lck_mtx_unlock_always(c_list_lock
);
2870 c_seg
= (c_segment_t
)zalloc(compressor_segment_zone
);
2871 bzero((char *)c_seg
, sizeof(struct c_segment
));
2873 c_seg
->c_store
.c_buffer
= (int32_t *)C_SEG_BUFFER_ADDRESS(c_segno
);
2875 lck_mtx_init(&c_seg
->c_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
2877 c_seg
->c_state
= C_IS_EMPTY
;
2878 c_seg
->c_firstemptyslot
= C_SLOT_MAX_INDEX
;
2879 c_seg
->c_mysegno
= c_segno
;
2881 lck_mtx_lock_spin_always(c_list_lock
);
2883 c_seg_switch_state(c_seg
, C_IS_FILLING
, FALSE
);
2884 c_segments
[c_segno
].c_seg
= c_seg
;
2885 assert(c_segments
[c_segno
].c_segno
> c_segments_available
);
2886 lck_mtx_unlock_always(c_list_lock
);
2888 *current_chead
= c_seg
;
2890 #if DEVELOPMENT || DEBUG
2891 C_SEG_MAKE_WRITEABLE(c_seg
);
2895 c_seg_alloc_nextslot(c_seg
);
2897 size_to_populate
= C_SEG_ALLOCSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
);
2899 if (size_to_populate
) {
2901 min_needed
= PAGE_SIZE
+ (C_SEG_ALLOCSIZE
- C_SEG_BUFSIZE
);
2903 if (C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
) < (unsigned) min_needed
) {
2905 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
2906 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
2907 vm_compressor_pages_grabbed
+= size_to_populate
/ PAGE_SIZE
;
2909 kernel_memory_populate(compressor_map
,
2910 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
],
2913 VM_KERN_MEMORY_COMPRESSOR
);
2915 size_to_populate
= 0;
2917 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2919 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2921 if (size_to_populate
)
2922 c_seg
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
2929 c_current_seg_filled(c_segment_t c_seg
, c_segment_t
*current_chead
)
2931 uint32_t unused_bytes
;
2932 uint32_t offset_to_depopulate
;
2933 int new_state
= C_ON_AGE_Q
;
2937 unused_bytes
= trunc_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
));
2939 #ifndef _OPEN_SOURCE
2940 /* TODO: The HW codec can generate, lazily, a '2nd page not mapped'
2941 * exception. So on such a platform, or platforms where we're confident
2942 * the codec does not require a buffer page to absorb trailing writes,
2943 * we can create an unmapped hole at the tail of the segment, rather
2944 * than a populated mapping. This will also guarantee that the codec
2945 * does not overwrite valid data past the edge of the segment and
2946 * thus eliminate the depopulation overhead.
2950 offset_to_depopulate
= C_SEG_BYTES_TO_OFFSET(round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_nextoffset
)));
2953 * release the extra physical page(s) at the end of the segment
2955 lck_mtx_unlock_always(&c_seg
->c_lock
);
2957 kernel_memory_depopulate(
2959 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[offset_to_depopulate
],
2963 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2965 c_seg
->c_populated_offset
= offset_to_depopulate
;
2967 assert(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
) <= C_SEG_BUFSIZE
);
2969 #if DEVELOPMENT || DEBUG
2971 boolean_t c_seg_was_busy
= FALSE
;
2973 if ( !c_seg
->c_busy
)
2976 c_seg_was_busy
= TRUE
;
2978 lck_mtx_unlock_always(&c_seg
->c_lock
);
2980 C_SEG_WRITE_PROTECT(c_seg
);
2982 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2984 if (c_seg_was_busy
== FALSE
)
2985 C_SEG_WAKEUP_DONE(c_seg
);
2990 if (current_chead
== (c_segment_t
*)&freezer_chead
&&
2991 VM_CONFIG_SWAP_IS_PRESENT
&&
2992 VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
&&
2993 c_freezer_swapout_count
< VM_MAX_FREEZER_CSEG_SWAP_COUNT
) {
2994 new_state
= C_ON_SWAPOUT_Q
;
2996 #endif /* CONFIG_FREEZE */
2998 clock_get_system_nanotime(&sec
, &nsec
);
2999 c_seg
->c_creation_ts
= (uint32_t)sec
;
3001 lck_mtx_lock_spin_always(c_list_lock
);
3003 c_seg
->c_generation_id
= c_generation_id
++;
3004 c_seg_switch_state(c_seg
, new_state
, FALSE
);
3007 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
3008 c_freezer_swapout_count
++;
3009 #endif /* CONFIG_FREEZE */
3011 if (c_seg
->c_state
== C_ON_AGE_Q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
3012 c_seg_need_delayed_compaction(c_seg
, TRUE
);
3014 lck_mtx_unlock_always(c_list_lock
);
3017 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
3018 thread_wakeup((event_t
)&c_swapout_list_head
);
3019 #endif /* CONFIG_FREEZE */
3021 *current_chead
= NULL
;
3026 * returns with c_seg locked
3029 c_seg_swapin_requeue(c_segment_t c_seg
, boolean_t has_data
, boolean_t minor_compact_ok
, boolean_t age_on_swapin_q
)
3034 clock_get_system_nanotime(&sec
, &nsec
);
3036 lck_mtx_lock_spin_always(c_list_lock
);
3037 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3039 assert(c_seg
->c_busy_swapping
);
3040 assert(c_seg
->c_busy
);
3042 c_seg
->c_busy_swapping
= 0;
3044 if (c_seg
->c_overage_swap
== TRUE
) {
3045 c_overage_swapped_count
--;
3046 c_seg
->c_overage_swap
= FALSE
;
3048 if (has_data
== TRUE
) {
3049 if (age_on_swapin_q
== TRUE
)
3050 c_seg_switch_state(c_seg
, C_ON_SWAPPEDIN_Q
, FALSE
);
3052 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
3054 if (minor_compact_ok
== TRUE
&& !c_seg
->c_on_minorcompact_q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
3055 c_seg_need_delayed_compaction(c_seg
, TRUE
);
3057 c_seg
->c_store
.c_buffer
= (int32_t*) NULL
;
3058 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3060 c_seg_switch_state(c_seg
, C_ON_BAD_Q
, FALSE
);
3062 c_seg
->c_swappedin_ts
= (uint32_t)sec
;
3064 lck_mtx_unlock_always(c_list_lock
);
3070 * c_seg has to be locked and is returned locked if the c_seg isn't freed
3071 * PAGE_REPLACMENT_DISALLOWED has to be TRUE on entry and is returned TRUE
3072 * c_seg_swapin returns 1 if the c_seg was freed, 0 otherwise
3076 c_seg_swapin(c_segment_t c_seg
, boolean_t force_minor_compaction
, boolean_t age_on_swapin_q
)
3078 vm_offset_t addr
= 0;
3079 uint32_t io_size
= 0;
3082 assert(C_SEG_IS_ONDISK(c_seg
));
3084 #if !CHECKSUM_THE_SWAP
3085 c_seg_trim_tail(c_seg
);
3087 io_size
= round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
));
3088 f_offset
= c_seg
->c_store
.c_swap_handle
;
3091 c_seg
->c_busy_swapping
= 1;
3094 * This thread is likely going to block for I/O.
3095 * Make sure it is ready to run when the I/O completes because
3096 * it needs to clear the busy bit on the c_seg so that other
3097 * waiting threads can make progress too. To do that, boost
3098 * the rwlock_count so that the priority is boosted.
3100 set_thread_rwlock_boost();
3101 lck_mtx_unlock_always(&c_seg
->c_lock
);
3103 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3105 addr
= (vm_offset_t
)C_SEG_BUFFER_ADDRESS(c_seg
->c_mysegno
);
3106 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
3108 kernel_memory_populate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
, VM_KERN_MEMORY_COMPRESSOR
);
3110 if (vm_swap_get(c_seg
, f_offset
, io_size
) != KERN_SUCCESS
) {
3111 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3113 kernel_memory_depopulate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
);
3115 c_seg_swapin_requeue(c_seg
, FALSE
, TRUE
, age_on_swapin_q
);
3118 vm_swap_decrypt(c_seg
);
3119 #endif /* ENCRYPTED_SWAP */
3121 #if CHECKSUM_THE_SWAP
3122 if (c_seg
->cseg_swap_size
!= io_size
)
3123 panic("swapin size doesn't match swapout size");
3125 if (c_seg
->cseg_hash
!= vmc_hash((char*) c_seg
->c_store
.c_buffer
, (int)io_size
)) {
3126 panic("c_seg_swapin - Swap hash mismatch\n");
3128 #endif /* CHECKSUM_THE_SWAP */
3130 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3132 c_seg_swapin_requeue(c_seg
, TRUE
, force_minor_compaction
== TRUE
? FALSE
: TRUE
, age_on_swapin_q
);
3134 OSAddAtomic64(c_seg
->c_bytes_used
, &compressor_bytes_used
);
3136 if (force_minor_compaction
== TRUE
) {
3137 if (c_seg_minor_compaction_and_unlock(c_seg
, FALSE
)) {
3139 * c_seg was completely empty so it was freed,
3140 * so be careful not to reference it again
3142 * Drop the rwlock_count so that the thread priority
3143 * is returned back to where it is supposed to be.
3145 clear_thread_rwlock_boost();
3149 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3152 C_SEG_WAKEUP_DONE(c_seg
);
3155 * Drop the rwlock_count so that the thread priority
3156 * is returned back to where it is supposed to be.
3158 clear_thread_rwlock_boost();
3165 c_segment_sv_hash_drop_ref(int hash_indx
)
3167 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
3171 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_indx
].he_record
;
3173 n_sv_he
.he_ref
= o_sv_he
.he_ref
- 1;
3174 n_sv_he
.he_data
= o_sv_he
.he_data
;
3176 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
) {
3177 if (n_sv_he
.he_ref
== 0)
3178 OSAddAtomic(-1, &c_segment_svp_in_hash
);
3186 c_segment_sv_hash_insert(uint32_t data
)
3190 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
3191 boolean_t got_ref
= FALSE
;
3194 OSAddAtomic(1, &c_segment_svp_zero_compressions
);
3196 OSAddAtomic(1, &c_segment_svp_nonzero_compressions
);
3198 hash_sindx
= data
& C_SV_HASH_MASK
;
3200 for (misses
= 0; misses
< C_SV_HASH_MAX_MISS
; misses
++)
3202 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
3204 while (o_sv_he
.he_data
== data
|| o_sv_he
.he_ref
== 0) {
3205 n_sv_he
.he_ref
= o_sv_he
.he_ref
+ 1;
3206 n_sv_he
.he_data
= data
;
3208 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
) {
3209 if (n_sv_he
.he_ref
== 1)
3210 OSAddAtomic(1, &c_segment_svp_in_hash
);
3214 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
3216 if (got_ref
== TRUE
)
3220 if (hash_sindx
== C_SV_HASH_SIZE
)
3223 if (got_ref
== FALSE
)
3226 return (hash_sindx
);
3230 #if RECORD_THE_COMPRESSED_DATA
3233 c_compressed_record_data(char *src
, int c_size
)
3235 if ((c_compressed_record_cptr
+ c_size
+ 4) >= c_compressed_record_ebuf
)
3236 panic("c_compressed_record_cptr >= c_compressed_record_ebuf");
3238 *(int *)((void *)c_compressed_record_cptr
) = c_size
;
3240 c_compressed_record_cptr
+= 4;
3242 memcpy(c_compressed_record_cptr
, src
, c_size
);
3243 c_compressed_record_cptr
+= c_size
;
3249 c_compress_page(char *src
, c_slot_mapping_t slot_ptr
, c_segment_t
*current_chead
, char *scratch_buf
)
3252 int c_rounded_size
= 0;
3257 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_START
, *current_chead
, 0, 0, 0, 0);
3259 if ((c_seg
= c_seg_allocate(current_chead
)) == NULL
) {
3263 * returns with c_seg lock held
3264 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3265 * c_nextslot has been allocated and
3266 * c_store.c_buffer populated
3268 assert(c_seg
->c_state
== C_IS_FILLING
);
3270 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_seg
->c_nextslot
);
3272 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(slot_ptr
);
3273 assert(slot_ptr
== (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(cs
));
3275 cs
->c_offset
= c_seg
->c_nextoffset
;
3277 max_csize
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)cs
->c_offset
);
3279 if (max_csize
> PAGE_SIZE
)
3280 max_csize
= PAGE_SIZE
;
3282 #if CHECKSUM_THE_DATA
3283 cs
->c_hash_data
= vmc_hash(src
, PAGE_SIZE
);
3285 boolean_t incomp_copy
= FALSE
;
3286 int max_csize_adj
= (max_csize
- 4);
3288 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3289 #if defined(__arm__) || defined(__arm64__)
3290 uint16_t ccodec
= CINVALID
;
3292 if (max_csize
>= C_SEG_OFFSET_ALIGNMENT_BOUNDARY
) {
3293 c_size
= metacompressor((const uint8_t *) src
,
3294 (uint8_t *) &c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3295 max_csize_adj
, &ccodec
,
3296 scratch_buf
, &incomp_copy
);
3297 #if C_SEG_OFFSET_ALIGNMENT_BOUNDARY > 4
3298 if (c_size
> max_csize_adj
) {
3305 assert(ccodec
== CCWK
|| ccodec
== CCLZ4
);
3306 cs
->c_codec
= ccodec
;
3309 #if defined(__arm__) || defined(__arm64__)
3312 #if defined(__arm64__)
3313 __unreachable_ok_push
3314 if (PAGE_SIZE
== 4096)
3315 c_size
= WKdm_compress_4k((WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3316 (WK_word
*)(uintptr_t)scratch_buf
, max_csize_adj
);
3318 c_size
= WKdm_compress_16k((WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3319 (WK_word
*)(uintptr_t)scratch_buf
, max_csize_adj
);
3321 __unreachable_ok_pop
3323 c_size
= WKdm_compress_new((const WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3324 (WK_word
*)(uintptr_t)scratch_buf
, max_csize_adj
);
3327 assertf(((c_size
<= max_csize_adj
) && (c_size
>= -1)),
3328 "c_size invalid (%d, %d), cur compressions: %d", c_size
, max_csize_adj
, c_segment_pages_compressed
);
3331 if (max_csize
< PAGE_SIZE
) {
3332 c_current_seg_filled(c_seg
, current_chead
);
3333 assert(*current_chead
== NULL
);
3335 lck_mtx_unlock_always(&c_seg
->c_lock
);
3336 /* TODO: it may be worth requiring codecs to distinguish
3337 * between incompressible inputs and failures due to
3338 * budget exhaustion.
3340 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3345 if (incomp_copy
== FALSE
) {
3346 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3349 OSAddAtomic(1, &c_segment_noncompressible_pages
);
3351 } else if (c_size
== 0) {
3355 * special case - this is a page completely full of a single 32 bit value
3357 hash_index
= c_segment_sv_hash_insert(*(uint32_t *)(uintptr_t)src
);
3359 if (hash_index
!= -1) {
3360 slot_ptr
->s_cindx
= hash_index
;
3361 slot_ptr
->s_cseg
= C_SV_CSEG_ID
;
3363 OSAddAtomic(1, &c_segment_svp_hash_succeeded
);
3364 #if RECORD_THE_COMPRESSED_DATA
3365 c_compressed_record_data(src
, 4);
3367 goto sv_compression
;
3371 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3373 OSAddAtomic(1, &c_segment_svp_hash_failed
);
3376 #if RECORD_THE_COMPRESSED_DATA
3377 c_compressed_record_data((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3379 #if CHECKSUM_THE_COMPRESSED_DATA
3380 cs
->c_hash_compressed_data
= vmc_hash((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3382 #if POPCOUNT_THE_COMPRESSED_DATA
3383 cs
->c_pop_cdata
= vmc_pop((uintptr_t) &c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3385 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3387 PACK_C_SIZE(cs
, c_size
);
3388 c_seg
->c_bytes_used
+= c_rounded_size
;
3389 c_seg
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3390 c_seg
->c_slots_used
++;
3392 slot_ptr
->s_cindx
= c_seg
->c_nextslot
++;
3393 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3394 slot_ptr
->s_cseg
= c_seg
->c_mysegno
+ 1;
3397 if (c_seg
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3398 c_current_seg_filled(c_seg
, current_chead
);
3399 assert(*current_chead
== NULL
);
3401 lck_mtx_unlock_always(&c_seg
->c_lock
);
3403 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3405 #if RECORD_THE_COMPRESSED_DATA
3406 if ((c_compressed_record_cptr
- c_compressed_record_sbuf
) >= C_SEG_ALLOCSIZE
) {
3407 c_compressed_record_write(c_compressed_record_sbuf
, (int)(c_compressed_record_cptr
- c_compressed_record_sbuf
));
3408 c_compressed_record_cptr
= c_compressed_record_sbuf
;
3412 OSAddAtomic64(c_size
, &c_segment_compressed_bytes
);
3413 OSAddAtomic64(c_rounded_size
, &compressor_bytes_used
);
3415 OSAddAtomic64(PAGE_SIZE
, &c_segment_input_bytes
);
3417 OSAddAtomic(1, &c_segment_pages_compressed
);
3418 OSAddAtomic(1, &sample_period_compression_count
);
3420 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_END
, *current_chead
, c_size
, c_segment_input_bytes
, c_segment_compressed_bytes
, 0);
3425 static inline void sv_decompress(int32_t *ddst
, int32_t pattern
) {
3427 memset_word(ddst
, pattern
, PAGE_SIZE
/ sizeof(int32_t));
3431 /* Unroll the pattern fill loop 4x to encourage the
3432 * compiler to emit NEON stores, cf.
3433 * <rdar://problem/25839866> Loop autovectorization
3435 * We use separate loops for each PAGE_SIZE
3436 * to allow the autovectorizer to engage, as PAGE_SIZE
3437 * is currently not a constant.
3440 __unreachable_ok_push
3441 if (PAGE_SIZE
== 4096) {
3442 for (i
= 0; i
< (4096U / sizeof(int32_t)); i
+= 4) {
3449 assert(PAGE_SIZE
== 16384);
3450 for (i
= 0; i
< (int)(16384U / sizeof(int32_t)); i
+= 4) {
3457 __unreachable_ok_pop
3462 c_decompress_page(char *dst
, volatile c_slot_mapping_t slot_ptr
, int flags
, int *zeroslot
)
3471 boolean_t need_unlock
= TRUE
;
3472 boolean_t consider_defragmenting
= FALSE
;
3473 boolean_t kdp_mode
= FALSE
;
3475 if (__improbable(flags
& C_KDP
)) {
3477 panic("C_KDP passed to decompress page from outside of debugger context");
3480 assert((flags
& C_KEEP
) == C_KEEP
);
3481 assert((flags
& C_DONT_BLOCK
) == C_DONT_BLOCK
);
3483 if ((flags
& (C_DONT_BLOCK
| C_KEEP
)) != (C_DONT_BLOCK
| C_KEEP
)) {
3492 if (__probable(!kdp_mode
)) {
3493 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3495 if (kdp_lck_rw_lock_is_acquired_exclusive(&c_master_lock
)) {
3502 * if hibernation is enabled, it indicates (via a call
3503 * to 'vm_decompressor_lock' that no further
3504 * decompressions are allowed once it reaches
3505 * the point of flushing all of the currently dirty
3506 * anonymous memory through the compressor and out
3507 * to disk... in this state we allow freeing of compressed
3508 * pages and must honor the C_DONT_BLOCK case
3510 if (__improbable(dst
&& decompressions_blocked
== TRUE
)) {
3511 if (flags
& C_DONT_BLOCK
) {
3513 if (__probable(!kdp_mode
)) {
3514 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3521 * it's safe to atomically assert and block behind the
3522 * lock held in shared mode because "decompressions_blocked" is
3523 * only set and cleared and the thread_wakeup done when the lock
3524 * is held exclusively
3526 assert_wait((event_t
)&decompressions_blocked
, THREAD_UNINT
);
3528 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3530 thread_block(THREAD_CONTINUE_NULL
);
3535 /* s_cseg is actually "segno+1" */
3536 c_segno
= slot_ptr
->s_cseg
- 1;
3538 if (__improbable(c_segno
>= c_segments_available
))
3539 panic("c_decompress_page: c_segno %d >= c_segments_available %d, slot_ptr(%p), slot_data(%x)",
3540 c_segno
, c_segments_available
, slot_ptr
, *(int *)((void *)slot_ptr
));
3542 if (__improbable(c_segments
[c_segno
].c_segno
< c_segments_available
))
3543 panic("c_decompress_page: c_segno %d is free, slot_ptr(%p), slot_data(%x)",
3544 c_segno
, slot_ptr
, *(int *)((void *)slot_ptr
));
3546 c_seg
= c_segments
[c_segno
].c_seg
;
3548 if (__probable(!kdp_mode
)) {
3549 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3551 if (kdp_lck_mtx_lock_spin_is_acquired(&c_seg
->c_lock
)) {
3556 assert(c_seg
->c_state
!= C_IS_EMPTY
&& c_seg
->c_state
!= C_IS_FREE
);
3558 if (dst
== NULL
&& c_seg
->c_busy_swapping
) {
3559 assert(c_seg
->c_busy
);
3561 goto bypass_busy_check
;
3563 if (flags
& C_DONT_BLOCK
) {
3564 if (c_seg
->c_busy
|| (C_SEG_IS_ONDISK(c_seg
) && dst
)) {
3571 if (c_seg
->c_busy
) {
3573 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3575 c_seg_wait_on_busy(c_seg
);
3581 c_indx
= slot_ptr
->s_cindx
;
3583 if (__improbable(c_indx
>= c_seg
->c_nextslot
))
3584 panic("c_decompress_page: c_indx %d >= c_nextslot %d, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3585 c_indx
, c_seg
->c_nextslot
, c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3587 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3589 c_size
= UNPACK_C_SIZE(cs
);
3591 if (__improbable(c_size
== 0))
3592 panic("c_decompress_page: c_size == 0, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3593 c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3595 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3598 uint32_t age_of_cseg
;
3599 clock_sec_t cur_ts_sec
;
3600 clock_nsec_t cur_ts_nsec
;
3602 if (C_SEG_IS_ONDISK(c_seg
)) {
3603 assert(kdp_mode
== FALSE
);
3604 retval
= c_seg_swapin(c_seg
, FALSE
, TRUE
);
3605 assert(retval
== 0);
3609 if (c_seg
->c_state
== C_ON_BAD_Q
) {
3610 assert(c_seg
->c_store
.c_buffer
== NULL
);
3617 #if POPCOUNT_THE_COMPRESSED_DATA
3619 uintptr_t csvaddr
= (uintptr_t) &c_seg
->c_store
.c_buffer
[cs
->c_offset
];
3620 if (cs
->c_pop_cdata
!= (csvpop
= vmc_pop(csvaddr
, c_size
))) {
3621 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
);
3625 #if CHECKSUM_THE_COMPRESSED_DATA
3627 if (cs
->c_hash_compressed_data
!= (csvhash
= vmc_hash((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))) {
3628 panic("Compressed data doesn't match original %p %p %u %u %u", c_seg
, cs
, c_size
, cs
->c_hash_compressed_data
, csvhash
);
3631 if (c_rounded_size
== PAGE_SIZE
) {
3633 * page wasn't compressible... just copy it out
3635 memcpy(dst
, &c_seg
->c_store
.c_buffer
[cs
->c_offset
], PAGE_SIZE
);
3636 } else if (c_size
== 4) {
3641 * page was populated with a single value
3642 * that didn't fit into our fast hash
3643 * so we packed it in as a single non-compressed value
3644 * that we need to populate the page with
3646 dptr
= (int32_t *)(uintptr_t)dst
;
3647 data
= *(int32_t *)(&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
3648 sv_decompress(dptr
, data
);
3653 if (__probable(!kdp_mode
)) {
3655 * we're behind the c_seg lock held in spin mode
3656 * which means pre-emption is disabled... therefore
3657 * the following sequence is atomic and safe
3659 my_cpu_no
= cpu_number();
3661 assert(my_cpu_no
< compressor_cpus
);
3663 scratch_buf
= &compressor_scratch_bufs
[my_cpu_no
* vm_compressor_get_decode_scratch_size()];
3665 scratch_buf
= kdp_compressor_scratch_buf
;
3668 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3669 #if defined(__arm__) || defined(__arm64__)
3670 uint16_t c_codec
= cs
->c_codec
;
3671 metadecompressor((const uint8_t *) &c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3672 (uint8_t *)dst
, c_size
, c_codec
, (void *)scratch_buf
);
3675 #if defined(__arm64__)
3676 __unreachable_ok_push
3677 if (PAGE_SIZE
== 4096)
3678 WKdm_decompress_4k((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3679 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3681 WKdm_decompress_16k((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3682 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3684 __unreachable_ok_pop
3686 WKdm_decompress_new((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3687 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3692 #if CHECKSUM_THE_DATA
3693 if (cs
->c_hash_data
!= vmc_hash(dst
, PAGE_SIZE
)) {
3694 #if defined(__arm__) || defined(__arm64__)
3695 int32_t *dinput
= &c_seg
->c_store
.c_buffer
[cs
->c_offset
];
3696 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));
3698 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
);
3702 if (c_seg
->c_swappedin_ts
== 0 && !kdp_mode
) {
3704 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
3706 age_of_cseg
= (uint32_t)cur_ts_sec
- c_seg
->c_creation_ts
;
3707 if (age_of_cseg
< DECOMPRESSION_SAMPLE_MAX_AGE
)
3708 OSAddAtomic(1, &age_of_decompressions_during_sample_period
[age_of_cseg
]);
3710 OSAddAtomic(1, &overage_decompressions_during_sample_period
);
3712 OSAddAtomic(1, &sample_period_decompression_count
);
3715 if (flags
& C_KEEP
) {
3719 assert(kdp_mode
== FALSE
);
3721 c_seg
->c_bytes_unused
+= c_rounded_size
;
3722 c_seg
->c_bytes_used
-= c_rounded_size
;
3724 assert(c_seg
->c_slots_used
);
3725 c_seg
->c_slots_used
--;
3729 if (c_indx
< c_seg
->c_firstemptyslot
)
3730 c_seg
->c_firstemptyslot
= c_indx
;
3732 OSAddAtomic(-1, &c_segment_pages_compressed
);
3734 if (c_seg
->c_state
!= C_ON_BAD_Q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
3736 * C_SEG_IS_ONDISK == TRUE can occur when we're doing a
3737 * free of a compressed page (i.e. dst == NULL)
3739 OSAddAtomic64(-c_rounded_size
, &compressor_bytes_used
);
3741 if (c_seg
->c_busy_swapping
) {
3743 * bypass case for c_busy_swapping...
3744 * let the swapin/swapout paths deal with putting
3745 * the c_seg on the minor compaction queue if needed
3747 assert(c_seg
->c_busy
);
3750 assert(!c_seg
->c_busy
);
3752 if (c_seg
->c_state
!= C_IS_FILLING
) {
3753 if (c_seg
->c_bytes_used
== 0) {
3754 if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3755 int pages_populated
;
3757 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
3758 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3760 if (pages_populated
) {
3762 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3763 assert(c_seg
->c_store
.c_buffer
!= NULL
);
3766 lck_mtx_unlock_always(&c_seg
->c_lock
);
3768 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_seg
->c_store
.c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
3770 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3771 C_SEG_WAKEUP_DONE(c_seg
);
3773 if (!c_seg
->c_on_minorcompact_q
&& c_seg
->c_state
!= C_ON_SWAPOUT_Q
)
3774 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3776 if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
) {
3778 c_seg_move_to_sparse_list(c_seg
);
3779 consider_defragmenting
= TRUE
;
3782 } else if (c_seg
->c_on_minorcompact_q
) {
3784 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3785 assert(!C_SEG_IS_ON_DISK_OR_SOQ(c_seg
));
3787 if (C_SEG_SHOULD_MINORCOMPACT_NOW(c_seg
)) {
3788 c_seg_try_minor_compaction_and_unlock(c_seg
);
3789 need_unlock
= FALSE
;
3791 } else if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3793 if (c_seg
->c_state
!= C_ON_BAD_Q
&& c_seg
->c_state
!= C_ON_SWAPOUT_Q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
) {
3794 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3796 } else if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
&& C_SEG_ONDISK_IS_SPARSE(c_seg
)) {
3798 c_seg_move_to_sparse_list(c_seg
);
3799 consider_defragmenting
= TRUE
;
3803 if (__improbable(kdp_mode
)) {
3807 if (need_unlock
== TRUE
)
3808 lck_mtx_unlock_always(&c_seg
->c_lock
);
3810 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3812 if (consider_defragmenting
== TRUE
)
3813 vm_swap_consider_defragmenting();
3816 if ((c_minor_count
&& COMPRESSOR_NEEDS_TO_MINOR_COMPACT()) || vm_compressor_needs_to_major_compact())
3817 vm_wake_compactor_swapper();
3825 vm_compressor_get(ppnum_t pn
, int *slot
, int flags
)
3827 c_slot_mapping_t slot_ptr
;
3833 dst
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3834 #elif __arm__ || __arm64__
3835 dst
= (char *) phystokv((pmap_paddr_t
)pn
<< PAGE_SHIFT
);
3837 #error "unsupported architecture"
3839 slot_ptr
= (c_slot_mapping_t
)slot
;
3841 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3846 * page was populated with a single value
3847 * that found a home in our hash table
3848 * grab that value from the hash and populate the page
3849 * that we need to populate the page with
3851 dptr
= (int32_t *)(uintptr_t)dst
;
3852 data
= c_segment_sv_hash_table
[slot_ptr
->s_cindx
].he_data
;
3854 memset_word(dptr
, data
, PAGE_SIZE
/ sizeof(int32_t));
3859 for (i
= 0; i
< (int)(PAGE_SIZE
/ sizeof(int32_t)); i
++)
3863 if ( !(flags
& C_KEEP
)) {
3864 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3866 OSAddAtomic(-1, &c_segment_pages_compressed
);
3870 OSAddAtomic(1, &c_segment_svp_nonzero_decompressions
);
3872 OSAddAtomic(1, &c_segment_svp_zero_decompressions
);
3877 retval
= c_decompress_page(dst
, slot_ptr
, flags
, &zeroslot
);
3880 * zeroslot will be set to 0 by c_decompress_page if (flags & C_KEEP)
3881 * or (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be TRUE
3887 * returns 0 if we successfully decompressed a page from a segment already in memory
3888 * returns 1 if we had to first swap in the segment, before successfully decompressing the page
3889 * returns -1 if we encountered an error swapping in the segment - decompression failed
3890 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be true
3897 vm_compressor_free(int *slot
, int flags
)
3899 c_slot_mapping_t slot_ptr
;
3903 assert(flags
== 0 || flags
== C_DONT_BLOCK
);
3905 slot_ptr
= (c_slot_mapping_t
)slot
;
3907 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3909 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3910 OSAddAtomic(-1, &c_segment_pages_compressed
);
3915 retval
= c_decompress_page(NULL
, slot_ptr
, flags
, &zeroslot
);
3917 * returns 0 if we successfully freed the specified compressed page
3918 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' set
3924 assert(retval
== -2);
3931 vm_compressor_put(ppnum_t pn
, int *slot
, void **current_chead
, char *scratch_buf
)
3937 src
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3938 #elif __arm__ || __arm64__
3939 src
= (char *) phystokv((pmap_paddr_t
)pn
<< PAGE_SHIFT
);
3941 #error "unsupported architecture"
3944 retval
= c_compress_page(src
, (c_slot_mapping_t
)slot
, (c_segment_t
*)current_chead
, scratch_buf
);
3950 vm_compressor_transfer(
3954 c_slot_mapping_t dst_slot
, src_slot
;
3959 src_slot
= (c_slot_mapping_t
) src_slot_p
;
3961 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3962 *dst_slot_p
= *src_slot_p
;
3966 dst_slot
= (c_slot_mapping_t
) dst_slot_p
;
3968 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3969 /* get segment for src_slot */
3970 c_seg
= c_segments
[src_slot
->s_cseg
-1].c_seg
;
3972 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3973 /* wait if it's busy */
3974 if (c_seg
->c_busy
&& !c_seg
->c_busy_swapping
) {
3975 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3976 c_seg_wait_on_busy(c_seg
);
3979 /* find the c_slot */
3980 c_indx
= src_slot
->s_cindx
;
3981 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3982 /* point the c_slot back to dst_slot instead of src_slot */
3983 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(dst_slot
);
3985 *dst_slot_p
= *src_slot_p
;
3987 lck_mtx_unlock_always(&c_seg
->c_lock
);
3988 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3993 int freezer_finished_filling
= 0;
3996 vm_compressor_finished_filling(
3997 void **current_chead
)
4001 if ((c_seg
= *(c_segment_t
*)current_chead
) == NULL
)
4004 assert(c_seg
->c_state
== C_IS_FILLING
);
4006 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
4008 c_current_seg_filled(c_seg
, (c_segment_t
*)current_chead
);
4010 lck_mtx_unlock_always(&c_seg
->c_lock
);
4012 freezer_finished_filling
++;
4017 * This routine is used to transfer the compressed chunks from
4018 * the c_seg/cindx pointed to by slot_p into a new c_seg headed
4019 * by the current_chead and a new cindx within that c_seg.
4021 * Currently, this routine is only used by the "freezer backed by
4022 * compressor with swap" mode to create a series of c_segs that
4023 * only contain compressed data belonging to one task. So, we
4024 * move a task's previously compressed data into a set of new
4025 * c_segs which will also hold the task's yet to be compressed data.
4029 vm_compressor_relocate(
4030 void **current_chead
,
4033 c_slot_mapping_t slot_ptr
;
4034 c_slot_mapping_t src_slot
;
4035 uint32_t c_rounded_size
;
4041 c_segment_t c_seg_dst
= NULL
;
4042 c_segment_t c_seg_src
= NULL
;
4043 kern_return_t kr
= KERN_SUCCESS
;
4046 src_slot
= (c_slot_mapping_t
) slot_p
;
4048 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
4050 * no need to relocate... this is a page full of a single
4051 * value which is hashed to a single entry not contained
4058 c_seg_dst
= c_seg_allocate((c_segment_t
*)current_chead
);
4060 * returns with c_seg lock held
4061 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
4062 * c_nextslot has been allocated and
4063 * c_store.c_buffer populated
4065 if (c_seg_dst
== NULL
) {
4067 * Out of compression segments?
4069 kr
= KERN_RESOURCE_SHORTAGE
;
4073 assert(c_seg_dst
->c_busy
== 0);
4075 C_SEG_BUSY(c_seg_dst
);
4077 dst_slot
= c_seg_dst
->c_nextslot
;
4079 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
4082 c_seg_src
= c_segments
[src_slot
->s_cseg
- 1].c_seg
;
4084 assert(c_seg_dst
!= c_seg_src
);
4086 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
4088 if (C_SEG_IS_ONDISK(c_seg_src
)) {
4091 * A "thaw" can mark a process as eligible for
4092 * another freeze cycle without bringing any of
4093 * its swapped out c_segs back from disk (because
4094 * that is done on-demand).
4096 * If the src c_seg we find for our pre-compressed
4097 * data is already on-disk, then we are dealing
4098 * with an app's data that is already packed and
4099 * swapped out. Don't do anything.
4102 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4104 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4111 if (c_seg_src
->c_busy
) {
4113 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4114 c_seg_wait_on_busy(c_seg_src
);
4118 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4123 C_SEG_BUSY(c_seg_src
);
4125 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4127 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4129 /* find the c_slot */
4130 c_indx
= src_slot
->s_cindx
;
4132 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, c_indx
);
4134 c_size
= UNPACK_C_SIZE(c_src
);
4138 if (c_size
> (uint32_t)(C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)c_seg_dst
->c_nextoffset
))) {
4140 * This segment is full. We need a new one.
4143 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4145 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
4146 C_SEG_WAKEUP_DONE(c_seg_src
);
4147 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4151 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
4153 assert(c_seg_dst
->c_busy
);
4154 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
4155 assert(!c_seg_dst
->c_on_minorcompact_q
);
4157 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
4158 assert(*current_chead
== NULL
);
4160 C_SEG_WAKEUP_DONE(c_seg_dst
);
4162 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
4166 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4171 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
4173 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
);
4174 //is platform alignment actually necessary since wkdm aligns its output?
4175 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
4177 cslot_copy(c_dst
, c_src
);
4178 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
4180 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
4181 c_seg_dst
->c_firstemptyslot
++;
4183 c_seg_dst
->c_slots_used
++;
4184 c_seg_dst
->c_nextslot
++;
4185 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
4186 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
4189 PACK_C_SIZE(c_src
, 0);
4191 c_seg_src
->c_bytes_used
-= c_rounded_size
;
4192 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
4194 assert(c_seg_src
->c_slots_used
);
4195 c_seg_src
->c_slots_used
--;
4197 if (c_indx
< c_seg_src
->c_firstemptyslot
) {
4198 c_seg_src
->c_firstemptyslot
= c_indx
;
4201 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
4203 PAGE_REPLACEMENT_ALLOWED(TRUE
);
4204 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
4205 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
4206 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
4207 slot_ptr
->s_cindx
= dst_slot
;
4209 PAGE_REPLACEMENT_ALLOWED(FALSE
);
4214 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
4216 C_SEG_WAKEUP_DONE(c_seg_src
);
4218 if (c_seg_src
->c_bytes_used
== 0 && c_seg_src
->c_state
!= C_IS_FILLING
) {
4219 if (!c_seg_src
->c_on_minorcompact_q
)
4220 c_seg_need_delayed_compaction(c_seg_src
, FALSE
);
4223 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
4228 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
4230 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
4232 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
4234 * Nearing or exceeded maximum slot and offset capacity.
4236 assert(c_seg_dst
->c_busy
);
4237 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
4238 assert(!c_seg_dst
->c_on_minorcompact_q
);
4240 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
4241 assert(*current_chead
== NULL
);
4244 C_SEG_WAKEUP_DONE(c_seg_dst
);
4246 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
4250 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
4255 #endif /* CONFIG_FREEZE */