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 <mach/mach_host.h> /* for host_info() */
41 #include <kern/ledger.h>
42 #include <kern/policy_internal.h>
44 #include <i386/misc_protos.h>
46 #include <IOKit/IOHibernatePrivate.h>
49 * vm_compressor_mode has a heirarchy of control to set its value.
50 * boot-args are checked first, then device-tree, and finally
51 * the default value that is defined below. See vm_fault_init() for
52 * the boot-arg & device-tree code.
55 int vm_compressor_mode
= VM_PAGER_COMPRESSOR_WITH_SWAP
;
59 int vm_compressor_is_active
= 0;
60 int vm_compression_limit
= 0;
61 int vm_compressor_available
= 0;
63 extern void vm_pageout_io_throttle(void);
65 #if CHECKSUM_THE_DATA || CHECKSUM_THE_SWAP || CHECKSUM_THE_COMPRESSED_DATA
66 extern unsigned int hash_string(char *cp
, int len
);
69 #define UNPACK_C_SIZE(cs) ((cs->c_size == (PAGE_SIZE-1)) ? PAGE_SIZE : cs->c_size)
70 #define PACK_C_SIZE(cs, size) (cs->c_size = ((size == PAGE_SIZE) ? PAGE_SIZE - 1 : size))
73 struct c_sv_hash_entry
{
77 uint32_t c_sv_he_data
;
79 uint64_t c_sv_he_record
;
84 #define he_ref c_sv_he_un.c_sv_he.c_sv_he_ref
85 #define he_data c_sv_he_un.c_sv_he.c_sv_he_data
86 #define he_record c_sv_he_un.c_sv_he_record
88 #define C_SV_HASH_MAX_MISS 32
89 #define C_SV_HASH_SIZE ((1 << 10))
90 #define C_SV_HASH_MASK ((1 << 10) - 1)
91 #define C_SV_CSEG_ID ((1 << 22) - 1)
94 struct c_slot_mapping
{
95 uint32_t s_cseg
:22, /* segment number + 1 */
96 s_cindx
:10; /* index in the segment */
98 #define C_SLOT_MAX_INDEX (1 << 10)
100 typedef struct c_slot_mapping
*c_slot_mapping_t
;
110 #define C_SLOT_PACK_PTR(ptr) (((uintptr_t)ptr - (uintptr_t) KERNEL_PMAP_HEAP_RANGE_START) >> 2)
111 #define C_SLOT_UNPACK_PTR(cslot) ((uintptr_t)(cslot->c_packed_ptr << 2) + (uintptr_t) KERNEL_PMAP_HEAP_RANGE_START)
114 uint32_t c_segment_count
= 0;
115 uint32_t c_segment_count_max
= 0;
117 uint64_t c_generation_id
= 0;
118 uint64_t c_generation_id_flush_barrier
;
121 #define HIBERNATE_FLUSHING_SECS_TO_COMPLETE 120
123 boolean_t hibernate_no_swapspace
= FALSE
;
124 clock_sec_t hibernate_flushing_deadline
= 0;
127 #if RECORD_THE_COMPRESSED_DATA
128 char *c_compressed_record_sbuf
;
129 char *c_compressed_record_ebuf
;
130 char *c_compressed_record_cptr
;
134 queue_head_t c_age_list_head
;
135 queue_head_t c_swapout_list_head
;
136 queue_head_t c_swappedin_list_head
;
137 queue_head_t c_swappedout_list_head
;
138 queue_head_t c_swappedout_sparse_list_head
;
139 queue_head_t c_major_list_head
;
140 queue_head_t c_filling_list_head
;
141 queue_head_t c_bad_list_head
;
143 uint32_t c_age_count
= 0;
144 uint32_t c_swapout_count
= 0;
145 uint32_t c_swappedin_count
= 0;
146 uint32_t c_swappedout_count
= 0;
147 uint32_t c_swappedout_sparse_count
= 0;
148 uint32_t c_major_count
= 0;
149 uint32_t c_filling_count
= 0;
150 uint32_t c_empty_count
= 0;
151 uint32_t c_bad_count
= 0;
154 queue_head_t c_minor_list_head
;
155 uint32_t c_minor_count
= 0;
157 int c_overage_swapped_count
= 0;
158 int c_overage_swapped_limit
= 0;
160 int c_seg_fixed_array_len
;
161 union c_segu
*c_segments
;
162 vm_offset_t c_buffers
;
163 vm_size_t c_buffers_size
;
164 caddr_t c_segments_next_page
;
165 boolean_t c_segments_busy
;
166 uint32_t c_segments_available
;
167 uint32_t c_segments_limit
;
168 uint32_t c_segments_nearing_limit
;
170 uint32_t c_segment_svp_in_hash
;
171 uint32_t c_segment_svp_hash_succeeded
;
172 uint32_t c_segment_svp_hash_failed
;
173 uint32_t c_segment_svp_zero_compressions
;
174 uint32_t c_segment_svp_nonzero_compressions
;
175 uint32_t c_segment_svp_zero_decompressions
;
176 uint32_t c_segment_svp_nonzero_decompressions
;
178 uint32_t c_segment_noncompressible_pages
;
180 uint32_t c_segment_pages_compressed
;
181 uint32_t c_segment_pages_compressed_limit
;
182 uint32_t c_segment_pages_compressed_nearing_limit
;
183 uint32_t c_free_segno_head
= (uint32_t)-1;
185 uint32_t vm_compressor_minorcompact_threshold_divisor
= 10;
186 uint32_t vm_compressor_majorcompact_threshold_divisor
= 10;
187 uint32_t vm_compressor_unthrottle_threshold_divisor
= 10;
188 uint32_t vm_compressor_catchup_threshold_divisor
= 10;
190 #define C_SEGMENTS_PER_PAGE (PAGE_SIZE / sizeof(union c_segu))
193 lck_grp_attr_t vm_compressor_lck_grp_attr
;
194 lck_attr_t vm_compressor_lck_attr
;
195 lck_grp_t vm_compressor_lck_grp
;
196 lck_mtx_t
*c_list_lock
;
197 lck_rw_t c_master_lock
;
198 boolean_t decompressions_blocked
= FALSE
;
200 zone_t compressor_segment_zone
;
201 int c_compressor_swap_trigger
= 0;
203 uint32_t compressor_cpus
;
204 char *compressor_scratch_bufs
;
205 char *kdp_compressor_scratch_buf
;
206 char *kdp_compressor_decompressed_page
;
207 addr64_t kdp_compressor_decompressed_page_paddr
;
208 ppnum_t kdp_compressor_decompressed_page_ppnum
;
210 clock_sec_t start_of_sample_period_sec
= 0;
211 clock_nsec_t start_of_sample_period_nsec
= 0;
212 clock_sec_t start_of_eval_period_sec
= 0;
213 clock_nsec_t start_of_eval_period_nsec
= 0;
214 uint32_t sample_period_decompression_count
= 0;
215 uint32_t sample_period_compression_count
= 0;
216 uint32_t last_eval_decompression_count
= 0;
217 uint32_t last_eval_compression_count
= 0;
219 #define DECOMPRESSION_SAMPLE_MAX_AGE (60 * 30)
221 boolean_t vm_swapout_ripe_segments
= FALSE
;
222 uint32_t vm_ripe_target_age
= (60 * 60 * 48);
224 uint32_t swapout_target_age
= 0;
225 uint32_t age_of_decompressions_during_sample_period
[DECOMPRESSION_SAMPLE_MAX_AGE
];
226 uint32_t overage_decompressions_during_sample_period
= 0;
228 void do_fastwake_warmup(queue_head_t
*, boolean_t
);
229 boolean_t fastwake_warmup
= FALSE
;
230 boolean_t fastwake_recording_in_progress
= FALSE
;
231 clock_sec_t dont_trim_until_ts
= 0;
233 uint64_t c_segment_warmup_count
;
234 uint64_t first_c_segment_to_warm_generation_id
= 0;
235 uint64_t last_c_segment_to_warm_generation_id
= 0;
236 boolean_t hibernate_flushing
= FALSE
;
238 int64_t c_segment_input_bytes
__attribute__((aligned(8))) = 0;
239 int64_t c_segment_compressed_bytes
__attribute__((aligned(8))) = 0;
240 int64_t compressor_bytes_used
__attribute__((aligned(8))) = 0;
243 struct c_sv_hash_entry c_segment_sv_hash_table
[C_SV_HASH_SIZE
] __attribute__ ((aligned (8)));
246 static boolean_t
compressor_needs_to_swap(void);
247 static void vm_compressor_swap_trigger_thread(void);
248 static void vm_compressor_do_delayed_compactions(boolean_t
);
249 static void vm_compressor_compact_and_swap(boolean_t
);
250 static void vm_compressor_age_swapped_in_segments(boolean_t
);
252 static void vm_compressor_take_paging_space_action(void);
254 boolean_t
vm_compressor_low_on_space(void);
256 void compute_swapout_target_age(void);
258 boolean_t
c_seg_major_compact(c_segment_t
, c_segment_t
);
259 boolean_t
c_seg_major_compact_ok(c_segment_t
, c_segment_t
);
261 int c_seg_minor_compaction_and_unlock(c_segment_t
, boolean_t
);
262 int c_seg_do_minor_compaction_and_unlock(c_segment_t
, boolean_t
, boolean_t
, boolean_t
);
263 void c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
);
265 void c_seg_move_to_sparse_list(c_segment_t
);
266 void c_seg_insert_into_q(queue_head_t
*, c_segment_t
);
268 uint64_t vm_available_memory(void);
269 uint64_t vm_compressor_pages_compressed(void);
272 * indicate the need to do a major compaction if
273 * the overall set of in-use compression segments
274 * becomes sparse... on systems that support pressure
275 * driven swapping, this will also cause swapouts to
278 static inline boolean_t
vm_compressor_needs_to_major_compact()
280 uint32_t incore_seg_count
;
282 incore_seg_count
= c_segment_count
- c_swappedout_count
- c_swappedout_sparse_count
;
284 if ((c_segment_count
>= (c_segments_nearing_limit
/ 8)) &&
285 ((incore_seg_count
* C_SEG_MAX_PAGES
) - VM_PAGE_COMPRESSOR_COUNT
) >
286 ((incore_seg_count
/ 8) * C_SEG_MAX_PAGES
))
293 vm_available_memory(void)
295 return (((uint64_t)AVAILABLE_NON_COMPRESSED_MEMORY
) * PAGE_SIZE_64
);
300 vm_compressor_pages_compressed(void)
302 return (c_segment_pages_compressed
* PAGE_SIZE_64
);
307 vm_compressor_low_on_space(void)
309 if ((c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) ||
310 (c_segment_count
> c_segments_nearing_limit
))
318 vm_wants_task_throttled(task_t task
)
320 if (task
== kernel_task
)
323 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
324 if ((vm_compressor_low_on_space() || HARD_THROTTLE_LIMIT_REACHED()) &&
325 (unsigned int)pmap_compressed(task
->map
->pmap
) > (c_segment_pages_compressed
/ 4))
332 #if DEVELOPMENT || DEBUG
333 boolean_t kill_on_no_paging_space
= FALSE
; /* On compressor/swap exhaustion, kill the largest process regardless of
334 * its chosen process policy. Controlled by a boot-arg of the same name. */
335 #endif /* DEVELOPMENT || DEBUG */
338 static uint32_t no_paging_space_action_in_progress
= 0;
339 extern void memorystatus_send_low_swap_note(void);
342 vm_compressor_take_paging_space_action(void)
344 if (no_paging_space_action_in_progress
== 0) {
346 if (OSCompareAndSwap(0, 1, (UInt32
*)&no_paging_space_action_in_progress
)) {
348 if (no_paging_space_action()) {
349 #if DEVELOPMENT || DEBUG
350 if (kill_on_no_paging_space
== TRUE
) {
352 * Since we are choosing to always kill a process, we don't need the
353 * "out of application memory" dialog box in this mode. And, hence we won't
356 no_paging_space_action_in_progress
= 0;
359 #endif /* DEVELOPMENT || DEBUG */
360 memorystatus_send_low_swap_note();
363 no_paging_space_action_in_progress
= 0;
370 vm_compressor_init_locks(void)
372 lck_grp_attr_setdefault(&vm_compressor_lck_grp_attr
);
373 lck_grp_init(&vm_compressor_lck_grp
, "vm_compressor", &vm_compressor_lck_grp_attr
);
374 lck_attr_setdefault(&vm_compressor_lck_attr
);
376 lck_rw_init(&c_master_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
381 vm_decompressor_lock(void)
383 PAGE_REPLACEMENT_ALLOWED(TRUE
);
385 decompressions_blocked
= TRUE
;
387 PAGE_REPLACEMENT_ALLOWED(FALSE
);
391 vm_decompressor_unlock(void)
393 PAGE_REPLACEMENT_ALLOWED(TRUE
);
395 decompressions_blocked
= FALSE
;
397 PAGE_REPLACEMENT_ALLOWED(FALSE
);
399 thread_wakeup((event_t
)&decompressions_blocked
);
402 static inline void cslot_copy(c_slot_t cdst
, c_slot_t csrc
) {
403 #if CHECKSUM_THE_DATA
404 cdst
->c_hash_data
= csrc
->c_hash_data
;
406 #if CHECKSUM_THE_COMPRESSED_DATA
407 cdst
->c_hash_compressed_data
= csrc
->c_hash_compressed_data
;
409 cdst
->c_size
= csrc
->c_size
;
410 cdst
->c_packed_ptr
= csrc
->c_packed_ptr
;
413 vm_map_t compressor_map
;
416 vm_compressor_init(void)
419 struct c_slot cs_dummy
;
420 c_slot_t cs
= &cs_dummy
;
421 int c_segment_min_size
;
422 int c_segment_padded_size
;
423 kern_return_t retval
= KERN_SUCCESS
;
424 vm_offset_t start_addr
= 0;
425 vm_size_t c_segments_arr_size
= 0, compressor_submap_size
= 0;
426 #if RECORD_THE_COMPRESSED_DATA
427 vm_size_t c_compressed_record_sbuf_size
= 0;
428 #endif /* RECORD_THE_COMPRESSED_DATA */
430 #if DEVELOPMENT || DEBUG
431 char bootarg_name
[32];
432 if (PE_parse_boot_argn("-kill_on_no_paging_space", bootarg_name
, sizeof (bootarg_name
))) {
433 kill_on_no_paging_space
= TRUE
;
435 #endif /* DEVELOPMENT || DEBUG */
438 * ensure that any pointer that gets created from
439 * the vm_page zone can be packed properly
441 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_min_address
);
443 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_min_address
)
444 panic("C_SLOT_UNPACK_PTR failed on zone_map_min_address - %p", (void *)zone_map_min_address
);
446 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_max_address
);
448 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_max_address
)
449 panic("C_SLOT_UNPACK_PTR failed on zone_map_max_address - %p", (void *)zone_map_max_address
);
452 assert((C_SEGMENTS_PER_PAGE
* sizeof(union c_segu
)) == PAGE_SIZE
);
454 PE_parse_boot_argn("vm_compression_limit", &vm_compression_limit
, sizeof (vm_compression_limit
));
456 if (max_mem
<= (3ULL * 1024ULL * 1024ULL * 1024ULL)) {
457 vm_compressor_minorcompact_threshold_divisor
= 11;
458 vm_compressor_majorcompact_threshold_divisor
= 13;
459 vm_compressor_unthrottle_threshold_divisor
= 20;
460 vm_compressor_catchup_threshold_divisor
= 35;
462 vm_compressor_minorcompact_threshold_divisor
= 20;
463 vm_compressor_majorcompact_threshold_divisor
= 25;
464 vm_compressor_unthrottle_threshold_divisor
= 35;
465 vm_compressor_catchup_threshold_divisor
= 50;
468 * vm_page_init_lck_grp is now responsible for calling vm_compressor_init_locks
469 * c_master_lock needs to be available early so that "vm_page_find_contiguous" can
470 * use PAGE_REPLACEMENT_ALLOWED to coordinate with the compressor.
473 c_list_lock
= lck_mtx_alloc_init(&vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
475 queue_init(&c_bad_list_head
);
476 queue_init(&c_age_list_head
);
477 queue_init(&c_minor_list_head
);
478 queue_init(&c_major_list_head
);
479 queue_init(&c_filling_list_head
);
480 queue_init(&c_swapout_list_head
);
481 queue_init(&c_swappedin_list_head
);
482 queue_init(&c_swappedout_list_head
);
483 queue_init(&c_swappedout_sparse_list_head
);
485 c_segment_min_size
= sizeof(struct c_segment
) + (C_SEG_SLOT_VAR_ARRAY_MIN_LEN
* sizeof(struct c_slot
));
487 for (c_segment_padded_size
= 128; c_segment_padded_size
< c_segment_min_size
; c_segment_padded_size
= c_segment_padded_size
<< 1);
489 compressor_segment_zone
= zinit(c_segment_padded_size
, 128000 * c_segment_padded_size
, PAGE_SIZE
, "compressor_segment");
490 zone_change(compressor_segment_zone
, Z_CALLERACCT
, FALSE
);
491 zone_change(compressor_segment_zone
, Z_NOENCRYPT
, TRUE
);
493 c_seg_fixed_array_len
= (c_segment_padded_size
- sizeof(struct c_segment
)) / sizeof(struct c_slot
);
495 c_free_segno_head
= -1;
496 c_segments_available
= 0;
498 if (vm_compression_limit
== 0) {
499 c_segment_pages_compressed_limit
= (uint32_t)((max_mem
/ PAGE_SIZE
)) * vm_scale
;
501 #define OLD_SWAP_LIMIT (1024 * 1024 * 16)
502 #define MAX_SWAP_LIMIT (1024 * 1024 * 128)
504 if (c_segment_pages_compressed_limit
> (OLD_SWAP_LIMIT
))
505 c_segment_pages_compressed_limit
= OLD_SWAP_LIMIT
;
507 if (c_segment_pages_compressed_limit
< (uint32_t)(max_mem
/ PAGE_SIZE_64
))
508 c_segment_pages_compressed_limit
= (uint32_t)(max_mem
/ PAGE_SIZE_64
);
510 if (vm_compression_limit
< MAX_SWAP_LIMIT
)
511 c_segment_pages_compressed_limit
= vm_compression_limit
;
513 c_segment_pages_compressed_limit
= MAX_SWAP_LIMIT
;
515 if ((c_segments_limit
= c_segment_pages_compressed_limit
/ (C_SEG_BUFSIZE
/ PAGE_SIZE
)) > C_SEG_MAX_LIMIT
)
516 c_segments_limit
= C_SEG_MAX_LIMIT
;
518 c_segment_pages_compressed_nearing_limit
= (c_segment_pages_compressed_limit
* 98) / 100;
519 c_segments_nearing_limit
= (c_segments_limit
* 98) / 100;
521 c_segments_busy
= FALSE
;
524 * Submap needs space for:
527 * - swap reclaimations -- C_SEG_BUFSIZE
529 c_segments_arr_size
= vm_map_round_page((sizeof(union c_segu
) * c_segments_limit
),VM_MAP_PAGE_MASK(kernel_map
));
530 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
));
532 compressor_submap_size
= c_segments_arr_size
+ c_buffers_size
+ C_SEG_BUFSIZE
;
534 #if RECORD_THE_COMPRESSED_DATA
535 c_compressed_record_sbuf_size
= (vm_size_t
)C_SEG_ALLOCSIZE
+ (PAGE_SIZE
* 2);
536 compressor_submap_size
+= c_compressed_record_sbuf_size
;
537 #endif /* RECORD_THE_COMPRESSED_DATA */
539 retval
= kmem_suballoc(kernel_map
, &start_addr
, compressor_submap_size
,
540 FALSE
, VM_FLAGS_ANYWHERE
| VM_FLAGS_PERMANENT
| VM_MAKE_TAG(0),
543 if (retval
!= KERN_SUCCESS
)
544 panic("vm_compressor_init: kmem_suballoc failed");
546 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
)
547 panic("vm_compressor_init: kernel_memory_allocate failed - c_segments\n");
548 if (kernel_memory_allocate(compressor_map
, &c_buffers
, c_buffers_size
, 0, KMA_COMPRESSOR
| KMA_VAONLY
| KMA_PERMANENT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
)
549 panic("vm_compressor_init: kernel_memory_allocate failed - c_buffers\n");
551 c_segments_next_page
= (caddr_t
)c_segments
;
552 vm_compressor_algorithm_init();
555 host_basic_info_data_t hinfo
;
556 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
559 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
561 compressor_cpus
= hinfo
.max_cpus
;
562 compressor_scratch_bufs
= kalloc_tag(compressor_cpus
* vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
564 kdp_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
565 kdp_compressor_decompressed_page
= kalloc_tag(PAGE_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
566 kdp_compressor_decompressed_page_paddr
= kvtophys((vm_offset_t
)kdp_compressor_decompressed_page
);
567 kdp_compressor_decompressed_page_ppnum
= (ppnum_t
) atop(kdp_compressor_decompressed_page_paddr
);
570 freezer_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_encode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
573 #if RECORD_THE_COMPRESSED_DATA
574 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
)
575 panic("vm_compressor_init: kernel_memory_allocate failed - c_compressed_record_sbuf\n");
577 c_compressed_record_cptr
= c_compressed_record_sbuf
;
578 c_compressed_record_ebuf
= c_compressed_record_sbuf
+ c_compressed_record_sbuf_size
;
581 if (kernel_thread_start_priority((thread_continue_t
)vm_compressor_swap_trigger_thread
, NULL
,
582 BASEPRI_PREEMPT
- 1, &thread
) != KERN_SUCCESS
) {
583 panic("vm_compressor_swap_trigger_thread: create failed");
585 thread_deallocate(thread
);
587 if (vm_pageout_internal_start() != KERN_SUCCESS
) {
588 panic("vm_compressor_init: Failed to start the internal pageout thread.\n");
590 if (VM_CONFIG_SWAP_IS_PRESENT
)
591 vm_compressor_swap_init();
593 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE
)
594 vm_compressor_is_active
= 1;
597 memorystatus_freeze_enabled
= TRUE
;
598 #endif /* CONFIG_FREEZE */
600 vm_compressor_available
= 1;
602 vm_page_reactivate_all_throttled();
606 #if VALIDATE_C_SEGMENTS
609 c_seg_validate(c_segment_t c_seg
, boolean_t must_be_compact
)
613 int32_t bytes_unused
;
614 uint32_t c_rounded_size
;
618 if (c_seg
->c_firstemptyslot
< c_seg
->c_nextslot
) {
619 c_indx
= c_seg
->c_firstemptyslot
;
620 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
623 panic("c_seg_validate: no slot backing c_firstemptyslot");
626 panic("c_seg_validate: c_firstemptyslot has non-zero size (%d)\n", cs
->c_size
);
631 for (c_indx
= 0; c_indx
< c_seg
->c_nextslot
; c_indx
++) {
633 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
635 c_size
= UNPACK_C_SIZE(cs
);
637 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
639 bytes_used
+= c_rounded_size
;
641 #if CHECKSUM_THE_COMPRESSED_DATA
642 if (c_size
&& cs
->c_hash_compressed_data
!= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))
643 panic("compressed data doesn't match original");
647 if (bytes_used
!= c_seg
->c_bytes_used
)
648 panic("c_seg_validate: bytes_used mismatch - found %d, segment has %d\n", bytes_used
, c_seg
->c_bytes_used
);
650 if (c_seg
->c_bytes_used
> C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
651 panic("c_seg_validate: c_bytes_used > c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
652 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
654 if (must_be_compact
) {
655 if (c_seg
->c_bytes_used
!= C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
656 panic("c_seg_validate: c_bytes_used doesn't match c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
657 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
665 c_seg_need_delayed_compaction(c_segment_t c_seg
, boolean_t c_list_lock_held
)
667 boolean_t clear_busy
= FALSE
;
669 if (c_list_lock_held
== FALSE
) {
670 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
673 lck_mtx_unlock_always(&c_seg
->c_lock
);
674 lck_mtx_lock_spin_always(c_list_lock
);
675 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
680 assert(c_seg
->c_state
!= C_IS_FILLING
);
682 if (!c_seg
->c_on_minorcompact_q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
683 queue_enter(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
684 c_seg
->c_on_minorcompact_q
= 1;
687 if (c_list_lock_held
== FALSE
)
688 lck_mtx_unlock_always(c_list_lock
);
690 if (clear_busy
== TRUE
)
691 C_SEG_WAKEUP_DONE(c_seg
);
695 unsigned int c_seg_moved_to_sparse_list
= 0;
698 c_seg_move_to_sparse_list(c_segment_t c_seg
)
700 boolean_t clear_busy
= FALSE
;
702 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
705 lck_mtx_unlock_always(&c_seg
->c_lock
);
706 lck_mtx_lock_spin_always(c_list_lock
);
707 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
711 c_seg_switch_state(c_seg
, C_ON_SWAPPEDOUTSPARSE_Q
, FALSE
);
713 c_seg_moved_to_sparse_list
++;
715 lck_mtx_unlock_always(c_list_lock
);
717 if (clear_busy
== TRUE
)
718 C_SEG_WAKEUP_DONE(c_seg
);
723 c_seg_insert_into_q(queue_head_t
*qhead
, c_segment_t c_seg
)
725 c_segment_t c_seg_next
;
727 if (queue_empty(qhead
)) {
728 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
730 c_seg_next
= (c_segment_t
)queue_first(qhead
);
734 if (c_seg
->c_generation_id
< c_seg_next
->c_generation_id
) {
735 queue_insert_before(qhead
, c_seg
, c_seg_next
, c_segment_t
, c_age_list
);
738 c_seg_next
= (c_segment_t
) queue_next(&c_seg_next
->c_age_list
);
740 if (queue_end(qhead
, (queue_entry_t
) c_seg_next
)) {
741 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
749 int try_minor_compaction_failed
= 0;
750 int try_minor_compaction_succeeded
= 0;
753 c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
)
756 assert(c_seg
->c_on_minorcompact_q
);
758 * c_seg is currently on the delayed minor compaction
759 * queue and we have c_seg locked... if we can get the
760 * c_list_lock w/o blocking (if we blocked we could deadlock
761 * because the lock order is c_list_lock then c_seg's lock)
762 * we'll pull it from the delayed list and free it directly
764 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
766 * c_list_lock is held, we need to bail
768 try_minor_compaction_failed
++;
770 lck_mtx_unlock_always(&c_seg
->c_lock
);
772 try_minor_compaction_succeeded
++;
775 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, FALSE
);
781 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
)
785 assert(c_seg
->c_busy
);
788 * check for the case that can occur when we are not swapping
789 * and this segment has been major compacted in the past
790 * and moved to the majorcompact q to remove it from further
791 * consideration... if the occupancy falls too low we need
792 * to put it back on the age_q so that it will be considered
793 * in the next major compaction sweep... if we don't do this
794 * we will eventually run into the c_segments_limit
796 if (c_seg
->c_state
== C_ON_MAJORCOMPACT_Q
&& C_SEG_SHOULD_MAJORCOMPACT(c_seg
)) {
798 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
800 if (!c_seg
->c_on_minorcompact_q
) {
801 if (clear_busy
== TRUE
)
802 C_SEG_WAKEUP_DONE(c_seg
);
804 lck_mtx_unlock_always(&c_seg
->c_lock
);
808 queue_remove(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
809 c_seg
->c_on_minorcompact_q
= 0;
812 lck_mtx_unlock_always(c_list_lock
);
814 if (disallow_page_replacement
== TRUE
) {
815 lck_mtx_unlock_always(&c_seg
->c_lock
);
817 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
819 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
821 c_seg_freed
= c_seg_minor_compaction_and_unlock(c_seg
, clear_busy
);
823 if (disallow_page_replacement
== TRUE
)
824 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
826 if (need_list_lock
== TRUE
)
827 lck_mtx_lock_spin_always(c_list_lock
);
829 return (c_seg_freed
);
834 c_seg_wait_on_busy(c_segment_t c_seg
)
837 assert_wait((event_t
) (c_seg
), THREAD_UNINT
);
839 lck_mtx_unlock_always(&c_seg
->c_lock
);
840 thread_block(THREAD_CONTINUE_NULL
);
845 c_seg_switch_state(c_segment_t c_seg
, int new_state
, boolean_t insert_head
)
847 int old_state
= c_seg
->c_state
;
849 #if __i386__ || __x86_64__
850 if (new_state
!= C_IS_FILLING
)
851 LCK_MTX_ASSERT(&c_seg
->c_lock
, LCK_MTX_ASSERT_OWNED
);
852 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
857 assert(new_state
== C_IS_FILLING
|| new_state
== C_IS_FREE
);
863 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_ON_SWAPOUT_Q
);
865 queue_remove(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
870 assert(new_state
== C_ON_SWAPOUT_Q
|| new_state
== C_ON_MAJORCOMPACT_Q
||
871 new_state
== C_IS_FREE
);
873 queue_remove(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
877 case C_ON_SWAPPEDIN_Q
:
878 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
880 queue_remove(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
885 assert(new_state
== C_ON_SWAPPEDOUT_Q
|| new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
886 new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
|| new_state
== C_IS_EMPTY
);
888 queue_remove(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
889 thread_wakeup((event_t
)&compaction_swapper_running
);
893 case C_ON_SWAPPEDOUT_Q
:
894 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
895 new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
896 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
898 queue_remove(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
899 c_swappedout_count
--;
902 case C_ON_SWAPPEDOUTSPARSE_Q
:
903 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
904 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
906 queue_remove(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
907 c_swappedout_sparse_count
--;
910 case C_ON_MAJORCOMPACT_Q
:
911 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
913 queue_remove(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
918 assert(new_state
== C_IS_FREE
);
920 queue_remove(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
925 panic("c_seg %p has bad c_state = %d\n", c_seg
, old_state
);
930 assert(old_state
!= C_IS_FILLING
);
935 assert(old_state
== C_ON_SWAPOUT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
941 assert(old_state
== C_IS_EMPTY
);
943 queue_enter(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
948 assert(old_state
== C_IS_FILLING
|| old_state
== C_ON_SWAPPEDIN_Q
|| old_state
== C_ON_SWAPOUT_Q
||
949 old_state
== C_ON_MAJORCOMPACT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
951 if (old_state
== C_IS_FILLING
)
952 queue_enter(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
954 if (!queue_empty(&c_age_list_head
)) {
957 c_first
= (c_segment_t
)queue_first(&c_age_list_head
);
958 c_seg
->c_creation_ts
= c_first
->c_creation_ts
;
960 queue_enter_first(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
965 case C_ON_SWAPPEDIN_Q
:
966 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
968 if (insert_head
== TRUE
)
969 queue_enter_first(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
971 queue_enter(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
976 assert(old_state
== C_ON_AGE_Q
|| old_state
== C_IS_FILLING
);
978 if (insert_head
== TRUE
)
979 queue_enter_first(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
981 queue_enter(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
985 case C_ON_SWAPPEDOUT_Q
:
986 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
);
988 if (insert_head
== TRUE
)
989 queue_enter_first(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
991 queue_enter(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
992 c_swappedout_count
++;
995 case C_ON_SWAPPEDOUTSPARSE_Q
:
996 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
);
998 if (insert_head
== TRUE
)
999 queue_enter_first(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1001 queue_enter(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
1003 c_swappedout_sparse_count
++;
1006 case C_ON_MAJORCOMPACT_Q
:
1007 assert(c_seg
->c_state
== C_ON_AGE_Q
);
1009 if (insert_head
== TRUE
)
1010 queue_enter_first(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1012 queue_enter(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
1017 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
1019 if (insert_head
== TRUE
)
1020 queue_enter_first(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1022 queue_enter(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1027 panic("c_seg %p requesting bad c_state = %d\n", c_seg
, new_state
);
1029 c_seg
->c_state
= new_state
;
1035 c_seg_free(c_segment_t c_seg
)
1037 assert(c_seg
->c_busy
);
1039 lck_mtx_unlock_always(&c_seg
->c_lock
);
1040 lck_mtx_lock_spin_always(c_list_lock
);
1041 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1043 c_seg_free_locked(c_seg
);
1048 c_seg_free_locked(c_segment_t c_seg
)
1051 int pages_populated
= 0;
1052 int32_t *c_buffer
= NULL
;
1053 uint64_t c_swap_handle
= 0;
1055 assert(c_seg
->c_busy
);
1056 assert(!c_seg
->c_on_minorcompact_q
);
1057 assert(!c_seg
->c_busy_swapping
);
1059 if (c_seg
->c_overage_swap
== TRUE
) {
1060 c_overage_swapped_count
--;
1061 c_seg
->c_overage_swap
= FALSE
;
1063 if ( !(C_SEG_IS_ONDISK(c_seg
)))
1064 c_buffer
= c_seg
->c_store
.c_buffer
;
1066 c_swap_handle
= c_seg
->c_store
.c_swap_handle
;
1068 c_seg_switch_state(c_seg
, C_IS_FREE
, FALSE
);
1070 lck_mtx_unlock_always(c_list_lock
);
1073 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
1074 c_seg
->c_store
.c_buffer
= NULL
;
1076 c_seg
->c_store
.c_swap_handle
= (uint64_t)-1;
1078 lck_mtx_unlock_always(&c_seg
->c_lock
);
1081 if (pages_populated
)
1082 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
1084 } else if (c_swap_handle
) {
1086 * Free swap space on disk.
1088 vm_swap_free(c_swap_handle
);
1090 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1092 C_SEG_WAKEUP_DONE(c_seg
);
1093 lck_mtx_unlock_always(&c_seg
->c_lock
);
1095 segno
= c_seg
->c_mysegno
;
1097 lck_mtx_lock_spin_always(c_list_lock
);
1099 * because the c_buffer is now associated with the segno,
1100 * we can't put the segno back on the free list until
1101 * after we have depopulated the c_buffer range, or
1102 * we run the risk of depopulating a range that is
1103 * now being used in one of the compressor heads
1105 c_segments
[segno
].c_segno
= c_free_segno_head
;
1106 c_free_segno_head
= segno
;
1109 lck_mtx_unlock_always(c_list_lock
);
1111 lck_mtx_destroy(&c_seg
->c_lock
, &vm_compressor_lck_grp
);
1113 if (c_seg
->c_slot_var_array_len
)
1114 kfree(c_seg
->c_slot_var_array
, sizeof(struct c_slot
) * c_seg
->c_slot_var_array_len
);
1116 zfree(compressor_segment_zone
, c_seg
);
1120 int c_seg_trim_page_count
= 0;
1123 c_seg_trim_tail(c_segment_t c_seg
)
1128 uint32_t c_rounded_size
;
1129 uint16_t current_nextslot
;
1130 uint32_t current_populated_offset
;
1132 if (c_seg
->c_bytes_used
== 0)
1134 current_nextslot
= c_seg
->c_nextslot
;
1135 current_populated_offset
= c_seg
->c_populated_offset
;
1137 while (c_seg
->c_nextslot
) {
1139 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, (c_seg
->c_nextslot
- 1));
1141 c_size
= UNPACK_C_SIZE(cs
);
1144 if (current_nextslot
!= c_seg
->c_nextslot
) {
1145 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1146 c_offset
= cs
->c_offset
+ C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1148 c_seg
->c_nextoffset
= c_offset
;
1149 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1151 if (c_seg
->c_firstemptyslot
> c_seg
->c_nextslot
)
1152 c_seg
->c_firstemptyslot
= c_seg
->c_nextslot
;
1154 c_seg_trim_page_count
+= ((round_page_32(C_SEG_OFFSET_TO_BYTES(current_populated_offset
)) -
1155 round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
);
1159 c_seg
->c_nextslot
--;
1161 assert(c_seg
->c_nextslot
);
1166 c_seg_minor_compaction_and_unlock(c_segment_t c_seg
, boolean_t clear_busy
)
1168 c_slot_mapping_t slot_ptr
;
1169 uint32_t c_offset
= 0;
1170 uint32_t old_populated_offset
;
1171 uint32_t c_rounded_size
;
1178 assert(c_seg
->c_busy
);
1180 #if VALIDATE_C_SEGMENTS
1181 c_seg_validate(c_seg
, FALSE
);
1183 if (c_seg
->c_bytes_used
== 0) {
1187 lck_mtx_unlock_always(&c_seg
->c_lock
);
1189 if (c_seg
->c_firstemptyslot
>= c_seg
->c_nextslot
|| C_SEG_UNUSED_BYTES(c_seg
) < PAGE_SIZE
)
1192 #if DEVELOPMENT || DEBUG
1193 C_SEG_MAKE_WRITEABLE(c_seg
);
1196 #if VALIDATE_C_SEGMENTS
1197 c_seg
->c_was_minor_compacted
++;
1199 c_indx
= c_seg
->c_firstemptyslot
;
1200 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1202 old_populated_offset
= c_seg
->c_populated_offset
;
1203 c_offset
= c_dst
->c_offset
;
1205 for (i
= c_indx
+ 1; i
< c_seg
->c_nextslot
&& c_offset
< c_seg
->c_nextoffset
; i
++) {
1207 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg
, i
);
1209 c_size
= UNPACK_C_SIZE(c_src
);
1214 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1216 memcpy(&c_seg
->c_store
.c_buffer
[c_offset
], &c_seg
->c_store
.c_buffer
[c_src
->c_offset
], c_rounded_size
);
1218 cslot_copy(c_dst
, c_src
);
1219 c_dst
->c_offset
= c_offset
;
1221 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1222 slot_ptr
->s_cindx
= c_indx
;
1224 c_offset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1225 PACK_C_SIZE(c_src
, 0);
1228 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1230 c_seg
->c_firstemptyslot
= c_indx
;
1231 c_seg
->c_nextslot
= c_indx
;
1232 c_seg
->c_nextoffset
= c_offset
;
1233 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1234 c_seg
->c_bytes_unused
= 0;
1236 #if VALIDATE_C_SEGMENTS
1237 c_seg_validate(c_seg
, TRUE
);
1239 if (old_populated_offset
> c_seg
->c_populated_offset
) {
1243 gc_size
= C_SEG_OFFSET_TO_BYTES(old_populated_offset
- c_seg
->c_populated_offset
);
1244 gc_ptr
= &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
];
1246 kernel_memory_depopulate(compressor_map
, (vm_offset_t
)gc_ptr
, gc_size
, KMA_COMPRESSOR
);
1249 #if DEVELOPMENT || DEBUG
1250 C_SEG_WRITE_PROTECT(c_seg
);
1254 if (clear_busy
== TRUE
) {
1255 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1256 C_SEG_WAKEUP_DONE(c_seg
);
1257 lck_mtx_unlock_always(&c_seg
->c_lock
);
1264 c_seg_alloc_nextslot(c_segment_t c_seg
)
1266 struct c_slot
*old_slot_array
= NULL
;
1267 struct c_slot
*new_slot_array
= NULL
;
1271 if (c_seg
->c_nextslot
< c_seg_fixed_array_len
)
1274 if ((c_seg
->c_nextslot
- c_seg_fixed_array_len
) >= c_seg
->c_slot_var_array_len
) {
1276 oldlen
= c_seg
->c_slot_var_array_len
;
1277 old_slot_array
= c_seg
->c_slot_var_array
;
1280 newlen
= C_SEG_SLOT_VAR_ARRAY_MIN_LEN
;
1282 newlen
= oldlen
* 2;
1284 new_slot_array
= (struct c_slot
*)kalloc(sizeof(struct c_slot
) * newlen
);
1286 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1289 memcpy((char *)new_slot_array
, (char *)old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1291 c_seg
->c_slot_var_array_len
= newlen
;
1292 c_seg
->c_slot_var_array
= new_slot_array
;
1294 lck_mtx_unlock_always(&c_seg
->c_lock
);
1297 kfree(old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1304 uint64_t asked_permission
;
1305 uint64_t compactions
;
1306 uint64_t moved_slots
;
1307 uint64_t moved_bytes
;
1308 uint64_t wasted_space_in_swapouts
;
1309 uint64_t count_of_swapouts
;
1310 uint64_t count_of_freed_segs
;
1311 } c_seg_major_compact_stats
;
1314 #define C_MAJOR_COMPACTION_SIZE_APPROPRIATE ((C_SEG_BUFSIZE * 90) / 100)
1318 c_seg_major_compact_ok(
1319 c_segment_t c_seg_dst
,
1320 c_segment_t c_seg_src
)
1323 c_seg_major_compact_stats
.asked_permission
++;
1325 if (c_seg_src
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
&&
1326 c_seg_dst
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
)
1329 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1331 * destination segment is full... can't compact
1341 c_seg_major_compact(
1342 c_segment_t c_seg_dst
,
1343 c_segment_t c_seg_src
)
1345 c_slot_mapping_t slot_ptr
;
1346 uint32_t c_rounded_size
;
1352 boolean_t keep_compacting
= TRUE
;
1355 * segments are not locked but they are both marked c_busy
1356 * which keeps c_decompress from working on them...
1357 * we can safely allocate new pages, move compressed data
1358 * from c_seg_src to c_seg_dst and update both c_segment's
1359 * state w/o holding the master lock
1361 #if DEVELOPMENT || DEBUG
1362 C_SEG_MAKE_WRITEABLE(c_seg_dst
);
1365 #if VALIDATE_C_SEGMENTS
1366 c_seg_dst
->c_was_major_compacted
++;
1367 c_seg_src
->c_was_major_donor
++;
1369 c_seg_major_compact_stats
.compactions
++;
1371 dst_slot
= c_seg_dst
->c_nextslot
;
1373 for (i
= 0; i
< c_seg_src
->c_nextslot
; i
++) {
1375 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, i
);
1377 c_size
= UNPACK_C_SIZE(c_src
);
1380 /* BATCH: move what we have so far; */
1384 if (C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
- c_seg_dst
->c_nextoffset
) < (unsigned) c_size
) {
1385 int size_to_populate
;
1388 size_to_populate
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
);
1390 if (size_to_populate
== 0) {
1392 keep_compacting
= FALSE
;
1395 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
1396 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
1398 kernel_memory_populate(compressor_map
,
1399 (vm_offset_t
) &c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_populated_offset
],
1402 VM_KERN_MEMORY_COMPRESSOR
);
1404 c_seg_dst
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
1405 assert(C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
) <= C_SEG_BUFSIZE
);
1407 c_seg_alloc_nextslot(c_seg_dst
);
1409 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
1411 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
);
1413 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1415 c_seg_major_compact_stats
.moved_slots
++;
1416 c_seg_major_compact_stats
.moved_bytes
+= c_size
;
1418 cslot_copy(c_dst
, c_src
);
1419 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
1421 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
1422 c_seg_dst
->c_firstemptyslot
++;
1423 c_seg_dst
->c_nextslot
++;
1424 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
1425 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1427 PACK_C_SIZE(c_src
, 0);
1429 c_seg_src
->c_bytes_used
-= c_rounded_size
;
1430 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
1431 c_seg_src
->c_firstemptyslot
= 0;
1433 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1434 /* dest segment is now full */
1435 keep_compacting
= FALSE
;
1439 #if DEVELOPMENT || DEBUG
1440 C_SEG_WRITE_PROTECT(c_seg_dst
);
1442 if (dst_slot
< c_seg_dst
->c_nextslot
) {
1444 PAGE_REPLACEMENT_ALLOWED(TRUE
);
1446 * we've now locked out c_decompress from
1447 * converting the slot passed into it into
1448 * a c_segment_t which allows us to use
1449 * the backptr to change which c_segment and
1450 * index the slot points to
1452 while (dst_slot
< c_seg_dst
->c_nextslot
) {
1454 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
1456 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1457 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
1458 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
1459 slot_ptr
->s_cindx
= dst_slot
++;
1461 PAGE_REPLACEMENT_ALLOWED(FALSE
);
1463 return (keep_compacting
);
1468 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
)
1471 uint64_t start_msecs
;
1473 end_msecs
= (end_sec
* 1000) + end_nsec
/ 1000000;
1474 start_msecs
= (start_sec
* 1000) + start_nsec
/ 1000000;
1476 return (end_msecs
- start_msecs
);
1481 uint32_t compressor_eval_period_in_msecs
= 250;
1482 uint32_t compressor_sample_min_in_msecs
= 500;
1483 uint32_t compressor_sample_max_in_msecs
= 10000;
1484 uint32_t compressor_thrashing_threshold_per_10msecs
= 50;
1485 uint32_t compressor_thrashing_min_per_10msecs
= 20;
1487 /* When true, reset sample data next chance we get. */
1488 static boolean_t compressor_need_sample_reset
= FALSE
;
1490 extern uint32_t vm_page_filecache_min
;
1494 compute_swapout_target_age(void)
1496 clock_sec_t cur_ts_sec
;
1497 clock_nsec_t cur_ts_nsec
;
1498 uint32_t min_operations_needed_in_this_sample
;
1499 uint64_t elapsed_msecs_in_eval
;
1500 uint64_t elapsed_msecs_in_sample
;
1501 boolean_t need_eval_reset
= FALSE
;
1503 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
1505 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
);
1507 if (compressor_need_sample_reset
||
1508 elapsed_msecs_in_sample
>= compressor_sample_max_in_msecs
) {
1509 compressor_need_sample_reset
= TRUE
;
1510 need_eval_reset
= TRUE
;
1513 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
);
1515 if (elapsed_msecs_in_eval
< compressor_eval_period_in_msecs
)
1517 need_eval_reset
= TRUE
;
1519 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_START
, elapsed_msecs_in_eval
, sample_period_compression_count
, sample_period_decompression_count
, 0, 0);
1521 min_operations_needed_in_this_sample
= (compressor_thrashing_min_per_10msecs
* (uint32_t)elapsed_msecs_in_eval
) / 10;
1523 if ((sample_period_compression_count
- last_eval_compression_count
) < min_operations_needed_in_this_sample
||
1524 (sample_period_decompression_count
- last_eval_decompression_count
) < min_operations_needed_in_this_sample
) {
1526 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_compression_count
- last_eval_compression_count
,
1527 sample_period_decompression_count
- last_eval_decompression_count
, 0, 1, 0);
1529 swapout_target_age
= 0;
1531 compressor_need_sample_reset
= TRUE
;
1532 need_eval_reset
= TRUE
;
1535 last_eval_compression_count
= sample_period_compression_count
;
1536 last_eval_decompression_count
= sample_period_decompression_count
;
1538 if (elapsed_msecs_in_sample
< compressor_sample_min_in_msecs
) {
1540 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, 0, 0, 5, 0);
1543 if (sample_period_decompression_count
> ((compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10)) {
1545 uint64_t running_total
;
1546 uint64_t working_target
;
1547 uint64_t aging_target
;
1548 uint32_t oldest_age_of_csegs_sampled
= 0;
1549 uint64_t working_set_approximation
= 0;
1551 swapout_target_age
= 0;
1553 working_target
= (sample_period_decompression_count
/ 100) * 95; /* 95 percent */
1554 aging_target
= (sample_period_decompression_count
/ 100) * 1; /* 1 percent */
1557 for (oldest_age_of_csegs_sampled
= 0; oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
; oldest_age_of_csegs_sampled
++) {
1559 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1561 working_set_approximation
+= oldest_age_of_csegs_sampled
* age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1563 if (running_total
>= working_target
)
1566 if (oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
) {
1568 working_set_approximation
= (working_set_approximation
* 1000) / elapsed_msecs_in_sample
;
1570 if (working_set_approximation
< VM_PAGE_COMPRESSOR_COUNT
) {
1572 running_total
= overage_decompressions_during_sample_period
;
1574 for (oldest_age_of_csegs_sampled
= DECOMPRESSION_SAMPLE_MAX_AGE
- 1; oldest_age_of_csegs_sampled
; oldest_age_of_csegs_sampled
--) {
1575 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1577 if (running_total
>= aging_target
)
1580 swapout_target_age
= (uint32_t)cur_ts_sec
- oldest_age_of_csegs_sampled
;
1582 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 2, 0);
1584 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 0, 3, 0);
1587 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_target
, running_total
, 0, 4, 0);
1589 compressor_need_sample_reset
= TRUE
;
1590 need_eval_reset
= TRUE
;
1592 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_decompression_count
, (compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10, 0, 6, 0);
1594 if (compressor_need_sample_reset
== TRUE
) {
1595 bzero(age_of_decompressions_during_sample_period
, sizeof(age_of_decompressions_during_sample_period
));
1596 overage_decompressions_during_sample_period
= 0;
1598 start_of_sample_period_sec
= cur_ts_sec
;
1599 start_of_sample_period_nsec
= cur_ts_nsec
;
1600 sample_period_decompression_count
= 0;
1601 sample_period_compression_count
= 0;
1602 last_eval_decompression_count
= 0;
1603 last_eval_compression_count
= 0;
1604 compressor_need_sample_reset
= FALSE
;
1606 if (need_eval_reset
== TRUE
) {
1607 start_of_eval_period_sec
= cur_ts_sec
;
1608 start_of_eval_period_nsec
= cur_ts_nsec
;
1613 int compaction_swapper_init_now
= 0;
1614 int compaction_swapper_running
= 0;
1615 int compaction_swapper_awakened
= 0;
1616 int compaction_swapper_abort
= 0;
1620 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
1621 boolean_t
memorystatus_kill_on_FC_thrashing(boolean_t
);
1622 int compressor_thrashing_induced_jetsam
= 0;
1623 int filecache_thrashing_induced_jetsam
= 0;
1624 static boolean_t vm_compressor_thrashing_detected
= FALSE
;
1625 #endif /* CONFIG_JETSAM */
1628 compressor_needs_to_swap(void)
1630 boolean_t should_swap
= FALSE
;
1632 if (vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
) {
1638 clock_get_system_nanotime(&now
, &nsec
);
1641 lck_mtx_lock_spin_always(c_list_lock
);
1643 if ( !queue_empty(&c_age_list_head
)) {
1644 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1646 age
= now
- c_seg
->c_creation_ts
;
1648 lck_mtx_unlock_always(c_list_lock
);
1650 if (age
>= vm_ripe_target_age
)
1653 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
1654 if (COMPRESSOR_NEEDS_TO_SWAP()) {
1657 if (VM_PAGE_Q_THROTTLED(&vm_pageout_queue_external
) && vm_page_anonymous_count
< (vm_page_inactive_count
/ 20)) {
1660 if (vm_page_free_count
< (vm_page_free_reserved
- (COMPRESSOR_FREE_RESERVED_LIMIT
* 2)))
1663 compute_swapout_target_age();
1665 if (swapout_target_age
) {
1668 lck_mtx_lock_spin_always(c_list_lock
);
1670 if (!queue_empty(&c_age_list_head
)) {
1672 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1674 if (c_seg
->c_creation_ts
> swapout_target_age
)
1675 swapout_target_age
= 0;
1677 lck_mtx_unlock_always(c_list_lock
);
1679 #if CONFIG_PHANTOM_CACHE
1680 if (vm_phantom_cache_check_pressure())
1683 if (swapout_target_age
)
1687 if (should_swap
|| c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) {
1689 if (vm_compressor_thrashing_detected
== FALSE
) {
1690 vm_compressor_thrashing_detected
= TRUE
;
1692 if (swapout_target_age
|| c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) {
1693 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
1694 compressor_thrashing_induced_jetsam
++;
1696 memorystatus_kill_on_FC_thrashing(TRUE
/* async */);
1697 filecache_thrashing_induced_jetsam
++;
1701 * let the jetsam take precedence over
1702 * any major compactions we might have
1703 * been able to do... otherwise we run
1704 * the risk of doing major compactions
1705 * on segments we're about to free up
1706 * due to the jetsam activity.
1708 should_swap
= FALSE
;
1711 #endif /* CONFIG_JETSAM */
1713 if (should_swap
== FALSE
) {
1715 * vm_compressor_needs_to_major_compact returns true only if we're
1716 * about to run out of available compressor segments... in this
1717 * case, we absolutely need to run a major compaction even if
1718 * we've just kicked off a jetsam or we don't otherwise need to
1719 * swap... terminating objects releases
1720 * pages back to the uncompressed cache, but does not guarantee
1721 * that we will free up even a single compression segment
1723 should_swap
= vm_compressor_needs_to_major_compact();
1727 * returning TRUE when swap_supported == FALSE
1728 * will cause the major compaction engine to
1729 * run, but will not trigger any swapping...
1730 * segments that have been major compacted
1731 * will be moved to the majorcompact queue
1733 return (should_swap
);
1738 * This function is called from the jetsam thread after killing something to
1739 * mitigate thrashing.
1741 * We need to restart our thrashing detection heuristics since memory pressure
1742 * has potentially changed significantly, and we don't want to detect on old
1743 * data from before the jetsam.
1746 vm_thrashing_jetsam_done(void)
1748 vm_compressor_thrashing_detected
= FALSE
;
1750 /* Were we compressor-thrashing or filecache-thrashing? */
1751 if (swapout_target_age
) {
1752 swapout_target_age
= 0;
1753 compressor_need_sample_reset
= TRUE
;
1755 #if CONFIG_PHANTOM_CACHE
1757 vm_phantom_cache_restart_sample();
1761 #endif /* CONFIG_JETSAM */
1763 uint32_t vm_wake_compactor_swapper_calls
= 0;
1764 uint32_t vm_run_compactor_already_running
= 0;
1765 uint32_t vm_run_compactor_empty_minor_q
= 0;
1766 uint32_t vm_run_compactor_did_compact
= 0;
1767 uint32_t vm_run_compactor_waited
= 0;
1770 vm_run_compactor(void)
1772 if (c_segment_count
== 0)
1775 lck_mtx_lock_spin_always(c_list_lock
);
1777 if (c_minor_count
== 0) {
1778 vm_run_compactor_empty_minor_q
++;
1780 lck_mtx_unlock_always(c_list_lock
);
1783 if (compaction_swapper_running
) {
1785 if (vm_restricted_to_single_processor
== FALSE
) {
1786 vm_run_compactor_already_running
++;
1788 lck_mtx_unlock_always(c_list_lock
);
1791 vm_run_compactor_waited
++;
1793 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1795 lck_mtx_unlock_always(c_list_lock
);
1797 thread_block(THREAD_CONTINUE_NULL
);
1801 vm_run_compactor_did_compact
++;
1803 fastwake_warmup
= FALSE
;
1804 compaction_swapper_running
= 1;
1806 vm_compressor_do_delayed_compactions(FALSE
);
1808 compaction_swapper_running
= 0;
1810 lck_mtx_unlock_always(c_list_lock
);
1812 thread_wakeup((event_t
)&compaction_swapper_running
);
1817 vm_wake_compactor_swapper(void)
1819 if (compaction_swapper_running
|| compaction_swapper_awakened
|| c_segment_count
== 0)
1822 if (c_minor_count
|| vm_compressor_needs_to_major_compact()) {
1824 lck_mtx_lock_spin_always(c_list_lock
);
1826 fastwake_warmup
= FALSE
;
1828 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
1830 vm_wake_compactor_swapper_calls
++;
1832 compaction_swapper_awakened
= 1;
1833 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
1835 lck_mtx_unlock_always(c_list_lock
);
1841 vm_consider_swapping()
1843 c_segment_t c_seg
, c_seg_next
;
1847 assert(VM_CONFIG_SWAP_IS_PRESENT
);
1849 lck_mtx_lock_spin_always(c_list_lock
);
1851 compaction_swapper_abort
= 1;
1853 while (compaction_swapper_running
) {
1854 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1856 lck_mtx_unlock_always(c_list_lock
);
1858 thread_block(THREAD_CONTINUE_NULL
);
1860 lck_mtx_lock_spin_always(c_list_lock
);
1862 compaction_swapper_abort
= 0;
1863 compaction_swapper_running
= 1;
1865 vm_swapout_ripe_segments
= TRUE
;
1867 if (!queue_empty(&c_major_list_head
)) {
1869 clock_get_system_nanotime(&now
, &nsec
);
1871 c_seg
= (c_segment_t
)queue_first(&c_major_list_head
);
1873 while (!queue_end(&c_major_list_head
, (queue_entry_t
)c_seg
)) {
1875 if (c_overage_swapped_count
>= c_overage_swapped_limit
)
1878 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
1880 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
1882 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1884 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
1886 lck_mtx_unlock_always(&c_seg
->c_lock
);
1891 vm_compressor_compact_and_swap(FALSE
);
1893 compaction_swapper_running
= 0;
1895 vm_swapout_ripe_segments
= FALSE
;
1897 lck_mtx_unlock_always(c_list_lock
);
1899 thread_wakeup((event_t
)&compaction_swapper_running
);
1904 vm_consider_waking_compactor_swapper(void)
1906 boolean_t need_wakeup
= FALSE
;
1908 if (c_segment_count
== 0)
1911 if (compaction_swapper_running
|| compaction_swapper_awakened
)
1914 if (!compaction_swapper_inited
&& !compaction_swapper_init_now
) {
1915 compaction_swapper_init_now
= 1;
1919 if (c_minor_count
&& (COMPRESSOR_NEEDS_TO_MINOR_COMPACT())) {
1923 } else if (compressor_needs_to_swap()) {
1927 } else if (c_minor_count
) {
1928 uint64_t total_bytes
;
1930 total_bytes
= compressor_object
->resident_page_count
* PAGE_SIZE_64
;
1932 if ((total_bytes
- compressor_bytes_used
) > total_bytes
/ 10)
1935 if (need_wakeup
== TRUE
) {
1937 lck_mtx_lock_spin_always(c_list_lock
);
1939 fastwake_warmup
= FALSE
;
1941 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
1942 memoryshot(VM_WAKEUP_COMPACTOR_SWAPPER
, DBG_FUNC_NONE
);
1944 compaction_swapper_awakened
= 1;
1945 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
1947 lck_mtx_unlock_always(c_list_lock
);
1952 #define C_SWAPOUT_LIMIT 4
1953 #define DELAYED_COMPACTIONS_PER_PASS 30
1956 vm_compressor_do_delayed_compactions(boolean_t flush_all
)
1959 int number_compacted
= 0;
1960 boolean_t needs_to_swap
= FALSE
;
1963 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
1965 while (!queue_empty(&c_minor_list_head
) && needs_to_swap
== FALSE
) {
1967 c_seg
= (c_segment_t
)queue_first(&c_minor_list_head
);
1969 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1971 if (c_seg
->c_busy
) {
1973 lck_mtx_unlock_always(c_list_lock
);
1974 c_seg_wait_on_busy(c_seg
);
1975 lck_mtx_lock_spin_always(c_list_lock
);
1981 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, TRUE
);
1983 if (VM_CONFIG_SWAP_IS_ACTIVE
&& (number_compacted
++ > DELAYED_COMPACTIONS_PER_PASS
)) {
1985 if ((flush_all
== TRUE
|| compressor_needs_to_swap() == TRUE
) && c_swapout_count
< C_SWAPOUT_LIMIT
)
1986 needs_to_swap
= TRUE
;
1988 number_compacted
= 0;
1990 lck_mtx_lock_spin_always(c_list_lock
);
1995 #define C_SEGMENT_SWAPPEDIN_AGE_LIMIT 10
1998 vm_compressor_age_swapped_in_segments(boolean_t flush_all
)
2004 clock_get_system_nanotime(&now
, &nsec
);
2006 while (!queue_empty(&c_swappedin_list_head
)) {
2008 c_seg
= (c_segment_t
)queue_first(&c_swappedin_list_head
);
2010 if (flush_all
== FALSE
&& (now
- c_seg
->c_swappedin_ts
) < C_SEGMENT_SWAPPEDIN_AGE_LIMIT
)
2013 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2015 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2017 lck_mtx_unlock_always(&c_seg
->c_lock
);
2022 extern int vm_num_swap_files
;
2023 extern int vm_num_pinned_swap_files
;
2024 extern int vm_swappin_enabled
;
2026 extern unsigned int vm_swapfile_total_segs_used
;
2027 extern unsigned int vm_swapfile_total_segs_alloced
;
2031 vm_compressor_flush(void)
2033 uint64_t vm_swap_put_failures_at_start
;
2034 wait_result_t wait_result
= 0;
2035 AbsoluteTime startTime
, endTime
;
2036 clock_sec_t now_sec
;
2037 clock_nsec_t now_nsec
;
2040 HIBLOG("vm_compressor_flush - starting\n");
2042 clock_get_uptime(&startTime
);
2044 lck_mtx_lock_spin_always(c_list_lock
);
2046 fastwake_warmup
= FALSE
;
2047 compaction_swapper_abort
= 1;
2049 while (compaction_swapper_running
) {
2050 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2052 lck_mtx_unlock_always(c_list_lock
);
2054 thread_block(THREAD_CONTINUE_NULL
);
2056 lck_mtx_lock_spin_always(c_list_lock
);
2058 compaction_swapper_abort
= 0;
2059 compaction_swapper_running
= 1;
2061 hibernate_flushing
= TRUE
;
2062 hibernate_no_swapspace
= FALSE
;
2063 c_generation_id_flush_barrier
= c_generation_id
+ 1000;
2065 clock_get_system_nanotime(&now_sec
, &now_nsec
);
2066 hibernate_flushing_deadline
= now_sec
+ HIBERNATE_FLUSHING_SECS_TO_COMPLETE
;
2068 vm_swap_put_failures_at_start
= vm_swap_put_failures
;
2070 vm_compressor_compact_and_swap(TRUE
);
2072 while (!queue_empty(&c_swapout_list_head
)) {
2074 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 5000, 1000*NSEC_PER_USEC
);
2076 lck_mtx_unlock_always(c_list_lock
);
2078 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2080 lck_mtx_lock_spin_always(c_list_lock
);
2082 if (wait_result
== THREAD_TIMED_OUT
)
2085 hibernate_flushing
= FALSE
;
2086 compaction_swapper_running
= 0;
2088 if (vm_swap_put_failures
> vm_swap_put_failures_at_start
)
2089 HIBLOG("vm_compressor_flush failed to clean %llu segments - vm_page_compressor_count(%d)\n",
2090 vm_swap_put_failures
- vm_swap_put_failures_at_start
, VM_PAGE_COMPRESSOR_COUNT
);
2092 lck_mtx_unlock_always(c_list_lock
);
2094 thread_wakeup((event_t
)&compaction_swapper_running
);
2096 clock_get_uptime(&endTime
);
2097 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2098 absolutetime_to_nanoseconds(endTime
, &nsec
);
2100 HIBLOG("vm_compressor_flush completed - took %qd msecs - vm_num_swap_files = %d, vm_num_pinned_swap_files = %d, vm_swappin_enabled = %d\n",
2101 nsec
/ 1000000ULL, vm_num_swap_files
, vm_num_pinned_swap_files
, vm_swappin_enabled
);
2105 int compaction_swap_trigger_thread_awakened
= 0;
2108 vm_compressor_swap_trigger_thread(void)
2110 current_thread()->options
|= TH_OPT_VMPRIV
;
2113 * compaction_swapper_init_now is set when the first call to
2114 * vm_consider_waking_compactor_swapper is made from
2115 * vm_pageout_scan... since this function is called upon
2116 * thread creation, we want to make sure to delay adjusting
2117 * the tuneables until we are awakened via vm_pageout_scan
2118 * so that we are at a point where the vm_swapfile_open will
2119 * be operating on the correct directory (in case the default
2120 * of /var/vm/ is overridden by the dymanic_pager
2122 if (compaction_swapper_init_now
) {
2123 vm_compaction_swapper_do_init();
2125 if (vm_restricted_to_single_processor
== TRUE
)
2126 thread_vm_bind_group_add();
2128 compaction_swapper_init_now
= 0;
2130 lck_mtx_lock_spin_always(c_list_lock
);
2132 compaction_swap_trigger_thread_awakened
++;
2133 compaction_swapper_awakened
= 0;
2135 if (compaction_swapper_running
== 0) {
2137 compaction_swapper_running
= 1;
2139 vm_compressor_compact_and_swap(FALSE
);
2141 compaction_swapper_running
= 0;
2143 assert_wait((event_t
)&c_compressor_swap_trigger
, THREAD_UNINT
);
2145 if (compaction_swapper_running
== 0)
2146 thread_wakeup((event_t
)&compaction_swapper_running
);
2148 lck_mtx_unlock_always(c_list_lock
);
2150 thread_block((thread_continue_t
)vm_compressor_swap_trigger_thread
);
2157 vm_compressor_record_warmup_start(void)
2161 lck_mtx_lock_spin_always(c_list_lock
);
2163 if (first_c_segment_to_warm_generation_id
== 0) {
2164 if (!queue_empty(&c_age_list_head
)) {
2166 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2168 first_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2170 first_c_segment_to_warm_generation_id
= 0;
2172 fastwake_recording_in_progress
= TRUE
;
2174 lck_mtx_unlock_always(c_list_lock
);
2179 vm_compressor_record_warmup_end(void)
2183 lck_mtx_lock_spin_always(c_list_lock
);
2185 if (fastwake_recording_in_progress
== TRUE
) {
2187 if (!queue_empty(&c_age_list_head
)) {
2189 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2191 last_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2193 last_c_segment_to_warm_generation_id
= first_c_segment_to_warm_generation_id
;
2195 fastwake_recording_in_progress
= FALSE
;
2197 HIBLOG("vm_compressor_record_warmup (%qd - %qd)\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2199 lck_mtx_unlock_always(c_list_lock
);
2203 #define DELAY_TRIM_ON_WAKE_SECS 25
2206 vm_compressor_delay_trim(void)
2211 clock_get_system_nanotime(&sec
, &nsec
);
2212 dont_trim_until_ts
= sec
+ DELAY_TRIM_ON_WAKE_SECS
;
2217 vm_compressor_do_warmup(void)
2219 lck_mtx_lock_spin_always(c_list_lock
);
2221 if (first_c_segment_to_warm_generation_id
== last_c_segment_to_warm_generation_id
) {
2222 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2224 lck_mtx_unlock_always(c_list_lock
);
2228 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2230 fastwake_warmup
= TRUE
;
2232 compaction_swapper_awakened
= 1;
2233 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2235 lck_mtx_unlock_always(c_list_lock
);
2239 do_fastwake_warmup_all(void)
2242 lck_mtx_lock_spin_always(c_list_lock
);
2244 if (queue_empty(&c_swappedout_list_head
) && queue_empty(&c_swappedout_sparse_list_head
)) {
2246 lck_mtx_unlock_always(c_list_lock
);
2250 fastwake_warmup
= TRUE
;
2252 do_fastwake_warmup(&c_swappedout_list_head
, TRUE
);
2254 do_fastwake_warmup(&c_swappedout_sparse_list_head
, TRUE
);
2256 fastwake_warmup
= FALSE
;
2258 lck_mtx_unlock_always(c_list_lock
);
2263 do_fastwake_warmup(queue_head_t
*c_queue
, boolean_t consider_all_cseg
)
2265 c_segment_t c_seg
= NULL
;
2266 AbsoluteTime startTime
, endTime
;
2270 HIBLOG("vm_compressor_fastwake_warmup (%qd - %qd) - starting\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2272 clock_get_uptime(&startTime
);
2274 lck_mtx_unlock_always(c_list_lock
);
2276 proc_set_thread_policy(current_thread(),
2277 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER2
);
2279 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2281 lck_mtx_lock_spin_always(c_list_lock
);
2283 while (!queue_empty(c_queue
) && fastwake_warmup
== TRUE
) {
2285 c_seg
= (c_segment_t
) queue_first(c_queue
);
2287 if (consider_all_cseg
== FALSE
) {
2288 if (c_seg
->c_generation_id
< first_c_segment_to_warm_generation_id
||
2289 c_seg
->c_generation_id
> last_c_segment_to_warm_generation_id
)
2292 if (vm_page_free_count
< (AVAILABLE_MEMORY
/ 4))
2296 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2297 lck_mtx_unlock_always(c_list_lock
);
2299 if (c_seg
->c_busy
) {
2300 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2301 c_seg_wait_on_busy(c_seg
);
2302 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2304 if (c_seg_swapin(c_seg
, TRUE
, FALSE
) == 0)
2305 lck_mtx_unlock_always(&c_seg
->c_lock
);
2306 c_segment_warmup_count
++;
2308 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2309 vm_pageout_io_throttle();
2310 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2312 lck_mtx_lock_spin_always(c_list_lock
);
2314 lck_mtx_unlock_always(c_list_lock
);
2316 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2318 proc_set_thread_policy(current_thread(),
2319 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER0
);
2321 clock_get_uptime(&endTime
);
2322 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2323 absolutetime_to_nanoseconds(endTime
, &nsec
);
2325 HIBLOG("vm_compressor_fastwake_warmup completed - took %qd msecs\n", nsec
/ 1000000ULL);
2327 lck_mtx_lock_spin_always(c_list_lock
);
2329 if (consider_all_cseg
== FALSE
) {
2330 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2336 vm_compressor_compact_and_swap(boolean_t flush_all
)
2338 c_segment_t c_seg
, c_seg_next
;
2339 boolean_t keep_compacting
;
2344 if (fastwake_warmup
== TRUE
) {
2345 uint64_t starting_warmup_count
;
2347 starting_warmup_count
= c_segment_warmup_count
;
2349 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 11) | DBG_FUNC_START
, c_segment_warmup_count
,
2350 first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
, 0, 0);
2351 do_fastwake_warmup(&c_swappedout_list_head
, FALSE
);
2352 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);
2354 fastwake_warmup
= FALSE
;
2358 * it's possible for the c_age_list_head to be empty if we
2359 * hit our limits for growing the compressor pool and we subsequently
2360 * hibernated... on the next hibernation we could see the queue as
2361 * empty and not proceeed even though we have a bunch of segments on
2362 * the swapped in queue that need to be dealt with.
2364 vm_compressor_do_delayed_compactions(flush_all
);
2366 vm_compressor_age_swapped_in_segments(flush_all
);
2369 * we only need to grab the timestamp once per
2370 * invocation of this function since the
2371 * timescale we're interested in is measured
2374 clock_get_system_nanotime(&now
, &nsec
);
2376 while (!queue_empty(&c_age_list_head
) && compaction_swapper_abort
== 0) {
2378 if (hibernate_flushing
== TRUE
) {
2381 if (hibernate_should_abort()) {
2382 HIBLOG("vm_compressor_flush - hibernate_should_abort returned TRUE\n");
2385 if (hibernate_no_swapspace
== TRUE
) {
2386 HIBLOG("vm_compressor_flush - out of swap space\n");
2389 if (vm_swap_files_pinned() == FALSE
) {
2390 HIBLOG("vm_compressor_flush - unpinned swap files\n");
2393 if (hibernate_in_progress_with_pinned_swap
== TRUE
&&
2394 (vm_swapfile_total_segs_alloced
== vm_swapfile_total_segs_used
)) {
2395 HIBLOG("vm_compressor_flush - out of pinned swap space\n");
2398 clock_get_system_nanotime(&sec
, &nsec
);
2400 if (sec
> hibernate_flushing_deadline
) {
2401 HIBLOG("vm_compressor_flush - failed to finish before deadline\n");
2405 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2407 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 100, 1000*NSEC_PER_USEC
);
2409 lck_mtx_unlock_always(c_list_lock
);
2411 thread_block(THREAD_CONTINUE_NULL
);
2413 lck_mtx_lock_spin_always(c_list_lock
);
2418 vm_compressor_do_delayed_compactions(flush_all
);
2420 vm_compressor_age_swapped_in_segments(flush_all
);
2422 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2424 * we timed out on the above thread_block
2425 * let's loop around and try again
2426 * the timeout allows us to continue
2427 * to do minor compactions to make
2428 * more memory available
2434 * Swap out segments?
2436 if (flush_all
== FALSE
) {
2437 boolean_t needs_to_swap
;
2439 lck_mtx_unlock_always(c_list_lock
);
2441 needs_to_swap
= compressor_needs_to_swap();
2443 if (needs_to_swap
== TRUE
&& vm_swap_low_on_space())
2444 vm_compressor_take_paging_space_action();
2446 lck_mtx_lock_spin_always(c_list_lock
);
2448 if (needs_to_swap
== FALSE
)
2451 if (queue_empty(&c_age_list_head
))
2453 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
2455 assert(c_seg
->c_state
== C_ON_AGE_Q
);
2457 if (flush_all
== TRUE
&& c_seg
->c_generation_id
> c_generation_id_flush_barrier
)
2460 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2462 if (c_seg
->c_busy
) {
2464 lck_mtx_unlock_always(c_list_lock
);
2465 c_seg_wait_on_busy(c_seg
);
2466 lck_mtx_lock_spin_always(c_list_lock
);
2472 if (c_seg_do_minor_compaction_and_unlock(c_seg
, FALSE
, TRUE
, TRUE
)) {
2474 * found an empty c_segment and freed it
2475 * so go grab the next guy in the queue
2477 c_seg_major_compact_stats
.count_of_freed_segs
++;
2483 keep_compacting
= TRUE
;
2485 while (keep_compacting
== TRUE
) {
2487 assert(c_seg
->c_busy
);
2489 /* look for another segment to consolidate */
2491 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2493 if (queue_end(&c_age_list_head
, (queue_entry_t
)c_seg_next
))
2496 assert(c_seg_next
->c_state
== C_ON_AGE_Q
);
2498 if (c_seg_major_compact_ok(c_seg
, c_seg_next
) == FALSE
)
2501 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2503 if (c_seg_next
->c_busy
) {
2505 lck_mtx_unlock_always(c_list_lock
);
2506 c_seg_wait_on_busy(c_seg_next
);
2507 lck_mtx_lock_spin_always(c_list_lock
);
2511 /* grab that segment */
2512 C_SEG_BUSY(c_seg_next
);
2514 if (c_seg_do_minor_compaction_and_unlock(c_seg_next
, FALSE
, TRUE
, TRUE
)) {
2516 * found an empty c_segment and freed it
2517 * so we can't continue to use c_seg_next
2519 c_seg_major_compact_stats
.count_of_freed_segs
++;
2523 /* unlock the list ... */
2524 lck_mtx_unlock_always(c_list_lock
);
2526 /* do the major compaction */
2528 keep_compacting
= c_seg_major_compact(c_seg
, c_seg_next
);
2530 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2532 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2534 * run a minor compaction on the donor segment
2535 * since we pulled at least some of it's
2536 * data into our target... if we've emptied
2537 * it, now is a good time to free it which
2538 * c_seg_minor_compaction_and_unlock also takes care of
2540 * by passing TRUE, we ask for c_busy to be cleared
2541 * and c_wanted to be taken care of
2543 if (c_seg_minor_compaction_and_unlock(c_seg_next
, TRUE
))
2544 c_seg_major_compact_stats
.count_of_freed_segs
++;
2546 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2548 /* relock the list */
2549 lck_mtx_lock_spin_always(c_list_lock
);
2551 } /* major compaction */
2553 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2555 assert(c_seg
->c_busy
);
2556 assert(!c_seg
->c_on_minorcompact_q
);
2558 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
2560 * This mode of putting a generic c_seg on the swapout list is
2561 * only supported when we have general swapping enabled
2563 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2565 if ((vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
)) {
2567 assert(VM_CONFIG_SWAP_IS_PRESENT
);
2569 * we are running compressor sweeps with swap-behind
2570 * make sure the c_seg has aged enough before swapping it
2573 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2574 c_seg
->c_overage_swap
= TRUE
;
2575 c_overage_swapped_count
++;
2576 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2580 if (c_seg
->c_state
== C_ON_AGE_Q
) {
2582 * this c_seg didn't get moved to the swapout queue
2583 * so we need to move it out of the way...
2584 * we just did a major compaction on it so put it
2587 c_seg_switch_state(c_seg
, C_ON_MAJORCOMPACT_Q
, FALSE
);
2589 c_seg_major_compact_stats
.wasted_space_in_swapouts
+= C_SEG_BUFSIZE
- c_seg
->c_bytes_used
;
2590 c_seg_major_compact_stats
.count_of_swapouts
++;
2592 C_SEG_WAKEUP_DONE(c_seg
);
2594 lck_mtx_unlock_always(&c_seg
->c_lock
);
2596 if (c_swapout_count
) {
2597 lck_mtx_unlock_always(c_list_lock
);
2599 thread_wakeup((event_t
)&c_swapout_list_head
);
2601 lck_mtx_lock_spin_always(c_list_lock
);
2608 c_seg_allocate(c_segment_t
*current_chead
)
2612 int size_to_populate
;
2614 if (vm_compressor_low_on_space())
2615 vm_compressor_take_paging_space_action();
2617 if ( (c_seg
= *current_chead
) == NULL
) {
2620 lck_mtx_lock_spin_always(c_list_lock
);
2622 while (c_segments_busy
== TRUE
) {
2623 assert_wait((event_t
) (&c_segments_busy
), THREAD_UNINT
);
2625 lck_mtx_unlock_always(c_list_lock
);
2627 thread_block(THREAD_CONTINUE_NULL
);
2629 lck_mtx_lock_spin_always(c_list_lock
);
2631 if (c_free_segno_head
== (uint32_t)-1) {
2632 uint32_t c_segments_available_new
;
2634 if (c_segments_available
>= c_segments_limit
|| c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) {
2635 lck_mtx_unlock_always(c_list_lock
);
2639 c_segments_busy
= TRUE
;
2640 lck_mtx_unlock_always(c_list_lock
);
2642 kernel_memory_populate(compressor_map
, (vm_offset_t
)c_segments_next_page
,
2643 PAGE_SIZE
, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
);
2644 c_segments_next_page
+= PAGE_SIZE
;
2646 c_segments_available_new
= c_segments_available
+ C_SEGMENTS_PER_PAGE
;
2648 if (c_segments_available_new
> c_segments_limit
)
2649 c_segments_available_new
= c_segments_limit
;
2651 for (c_segno
= c_segments_available
+ 1; c_segno
< c_segments_available_new
; c_segno
++)
2652 c_segments
[c_segno
- 1].c_segno
= c_segno
;
2654 lck_mtx_lock_spin_always(c_list_lock
);
2656 c_segments
[c_segno
- 1].c_segno
= c_free_segno_head
;
2657 c_free_segno_head
= c_segments_available
;
2658 c_segments_available
= c_segments_available_new
;
2660 c_segments_busy
= FALSE
;
2661 thread_wakeup((event_t
) (&c_segments_busy
));
2663 c_segno
= c_free_segno_head
;
2664 assert(c_segno
>= 0 && c_segno
< c_segments_limit
);
2666 c_free_segno_head
= (uint32_t)c_segments
[c_segno
].c_segno
;
2669 * do the rest of the bookkeeping now while we're still behind
2670 * the list lock and grab our generation id now into a local
2671 * so that we can install it once we have the c_seg allocated
2674 if (c_segment_count
> c_segment_count_max
)
2675 c_segment_count_max
= c_segment_count
;
2677 lck_mtx_unlock_always(c_list_lock
);
2679 c_seg
= (c_segment_t
)zalloc(compressor_segment_zone
);
2680 bzero((char *)c_seg
, sizeof(struct c_segment
));
2682 c_seg
->c_store
.c_buffer
= (int32_t *)C_SEG_BUFFER_ADDRESS(c_segno
);
2684 lck_mtx_init(&c_seg
->c_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
2686 c_seg
->c_state
= C_IS_EMPTY
;
2687 c_seg
->c_firstemptyslot
= C_SLOT_MAX_INDEX
;
2688 c_seg
->c_mysegno
= c_segno
;
2690 lck_mtx_lock_spin_always(c_list_lock
);
2692 c_seg_switch_state(c_seg
, C_IS_FILLING
, FALSE
);
2693 c_segments
[c_segno
].c_seg
= c_seg
;
2694 assert(c_segments
[c_segno
].c_segno
> c_segments_available
);
2695 lck_mtx_unlock_always(c_list_lock
);
2697 *current_chead
= c_seg
;
2699 #if DEVELOPMENT || DEBUG
2700 C_SEG_MAKE_WRITEABLE(c_seg
);
2704 c_seg_alloc_nextslot(c_seg
);
2706 size_to_populate
= C_SEG_ALLOCSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
);
2708 if (size_to_populate
) {
2710 min_needed
= PAGE_SIZE
+ (C_SEG_ALLOCSIZE
- C_SEG_BUFSIZE
);
2712 if (C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
) < (unsigned) min_needed
) {
2714 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
2715 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
2717 kernel_memory_populate(compressor_map
,
2718 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
],
2721 VM_KERN_MEMORY_COMPRESSOR
);
2723 size_to_populate
= 0;
2725 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2727 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2729 if (size_to_populate
)
2730 c_seg
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
2737 c_current_seg_filled(c_segment_t c_seg
, c_segment_t
*current_chead
)
2739 uint32_t unused_bytes
;
2740 uint32_t offset_to_depopulate
;
2741 int new_state
= C_ON_AGE_Q
;
2745 unused_bytes
= trunc_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
));
2749 offset_to_depopulate
= C_SEG_BYTES_TO_OFFSET(round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_nextoffset
)));
2752 * release the extra physical page(s) at the end of the segment
2754 lck_mtx_unlock_always(&c_seg
->c_lock
);
2756 kernel_memory_depopulate(
2758 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[offset_to_depopulate
],
2762 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2764 c_seg
->c_populated_offset
= offset_to_depopulate
;
2766 assert(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
) <= C_SEG_BUFSIZE
);
2768 #if DEVELOPMENT || DEBUG
2770 boolean_t c_seg_was_busy
= FALSE
;
2772 if ( !c_seg
->c_busy
)
2775 c_seg_was_busy
= TRUE
;
2777 lck_mtx_unlock_always(&c_seg
->c_lock
);
2779 C_SEG_WRITE_PROTECT(c_seg
);
2781 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2783 if (c_seg_was_busy
== FALSE
)
2784 C_SEG_WAKEUP_DONE(c_seg
);
2789 if (current_chead
== (c_segment_t
*)&freezer_chead
&&
2790 VM_CONFIG_SWAP_IS_PRESENT
&&
2791 VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
&&
2792 c_freezer_swapout_count
< VM_MAX_FREEZER_CSEG_SWAP_COUNT
) {
2793 new_state
= C_ON_SWAPOUT_Q
;
2795 #endif /* CONFIG_FREEZE */
2797 clock_get_system_nanotime(&sec
, &nsec
);
2798 c_seg
->c_creation_ts
= (uint32_t)sec
;
2800 lck_mtx_lock_spin_always(c_list_lock
);
2803 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
2804 c_freezer_swapout_count
++;
2805 #endif /* CONFIG_FREEZE */
2807 c_seg
->c_generation_id
= c_generation_id
++;
2808 c_seg_switch_state(c_seg
, new_state
, FALSE
);
2810 if (c_seg
->c_state
== C_ON_AGE_Q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
2811 c_seg_need_delayed_compaction(c_seg
, TRUE
);
2813 lck_mtx_unlock_always(c_list_lock
);
2816 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
2817 thread_wakeup((event_t
)&c_swapout_list_head
);
2818 #endif /* CONFIG_FREEZE */
2820 *current_chead
= NULL
;
2825 * returns with c_seg locked
2828 c_seg_swapin_requeue(c_segment_t c_seg
, boolean_t has_data
, boolean_t minor_compact_ok
, boolean_t age_on_swapin_q
)
2833 clock_get_system_nanotime(&sec
, &nsec
);
2835 lck_mtx_lock_spin_always(c_list_lock
);
2836 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2838 assert(c_seg
->c_busy_swapping
);
2839 assert(c_seg
->c_busy
);
2841 c_seg
->c_busy_swapping
= 0;
2843 if (c_seg
->c_overage_swap
== TRUE
) {
2844 c_overage_swapped_count
--;
2845 c_seg
->c_overage_swap
= FALSE
;
2847 if (has_data
== TRUE
) {
2848 if (age_on_swapin_q
== TRUE
)
2849 c_seg_switch_state(c_seg
, C_ON_SWAPPEDIN_Q
, FALSE
);
2851 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2853 if (minor_compact_ok
== TRUE
&& !c_seg
->c_on_minorcompact_q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
2854 c_seg_need_delayed_compaction(c_seg
, TRUE
);
2856 c_seg
->c_store
.c_buffer
= (int32_t*) NULL
;
2857 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
2859 c_seg_switch_state(c_seg
, C_ON_BAD_Q
, FALSE
);
2861 c_seg
->c_swappedin_ts
= (uint32_t)sec
;
2863 lck_mtx_unlock_always(c_list_lock
);
2869 * c_seg has to be locked and is returned locked if the c_seg isn't freed
2870 * PAGE_REPLACMENT_DISALLOWED has to be TRUE on entry and is returned TRUE
2871 * c_seg_swapin returns 1 if the c_seg was freed, 0 otherwise
2875 c_seg_swapin(c_segment_t c_seg
, boolean_t force_minor_compaction
, boolean_t age_on_swapin_q
)
2877 vm_offset_t addr
= 0;
2878 uint32_t io_size
= 0;
2881 assert(C_SEG_IS_ONDISK(c_seg
));
2883 #if !CHECKSUM_THE_SWAP
2884 c_seg_trim_tail(c_seg
);
2886 io_size
= round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
));
2887 f_offset
= c_seg
->c_store
.c_swap_handle
;
2890 c_seg
->c_busy_swapping
= 1;
2893 * This thread is likely going to block for I/O.
2894 * Make sure it is ready to run when the I/O completes because
2895 * it needs to clear the busy bit on the c_seg so that other
2896 * waiting threads can make progress too. To do that, boost
2897 * the rwlock_count so that the priority is boosted.
2899 set_thread_rwlock_boost();
2900 lck_mtx_unlock_always(&c_seg
->c_lock
);
2902 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2904 addr
= (vm_offset_t
)C_SEG_BUFFER_ADDRESS(c_seg
->c_mysegno
);
2905 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
2907 kernel_memory_populate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
, VM_KERN_MEMORY_COMPRESSOR
);
2909 if (vm_swap_get(c_seg
, f_offset
, io_size
) != KERN_SUCCESS
) {
2910 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2912 c_seg
->c_store
.c_swap_handle
= f_offset
;
2914 kernel_memory_depopulate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
);
2916 c_seg_swapin_requeue(c_seg
, FALSE
, TRUE
, age_on_swapin_q
);
2918 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
2920 vm_swap_decrypt(c_seg
);
2921 #endif /* ENCRYPTED_SWAP */
2923 #if CHECKSUM_THE_SWAP
2924 if (c_seg
->cseg_swap_size
!= io_size
)
2925 panic("swapin size doesn't match swapout size");
2927 if (c_seg
->cseg_hash
!= hash_string((char*) c_seg
->c_store
.c_buffer
, (int)io_size
)) {
2928 panic("c_seg_swapin - Swap hash mismatch\n");
2930 #endif /* CHECKSUM_THE_SWAP */
2932 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2934 c_seg_swapin_requeue(c_seg
, TRUE
, force_minor_compaction
== TRUE
? FALSE
: TRUE
, age_on_swapin_q
);
2936 OSAddAtomic64(c_seg
->c_bytes_used
, &compressor_bytes_used
);
2938 if (force_minor_compaction
== TRUE
) {
2939 if (c_seg_minor_compaction_and_unlock(c_seg
, FALSE
)) {
2941 * Drop the rwlock_count so that the thread priority
2942 * is returned back to where it is supposed to be.
2944 clear_thread_rwlock_boost();
2948 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2951 C_SEG_WAKEUP_DONE(c_seg
);
2954 * Drop the rwlock_count so that the thread priority
2955 * is returned back to where it is supposed to be.
2957 clear_thread_rwlock_boost();
2964 c_segment_sv_hash_drop_ref(int hash_indx
)
2966 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
2970 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_indx
].he_record
;
2972 n_sv_he
.he_ref
= o_sv_he
.he_ref
- 1;
2973 n_sv_he
.he_data
= o_sv_he
.he_data
;
2975 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
) {
2976 if (n_sv_he
.he_ref
== 0)
2977 OSAddAtomic(-1, &c_segment_svp_in_hash
);
2985 c_segment_sv_hash_insert(uint32_t data
)
2989 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
2990 boolean_t got_ref
= FALSE
;
2993 OSAddAtomic(1, &c_segment_svp_zero_compressions
);
2995 OSAddAtomic(1, &c_segment_svp_nonzero_compressions
);
2997 hash_sindx
= data
& C_SV_HASH_MASK
;
2999 for (misses
= 0; misses
< C_SV_HASH_MAX_MISS
; misses
++)
3001 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
3003 while (o_sv_he
.he_data
== data
|| o_sv_he
.he_ref
== 0) {
3004 n_sv_he
.he_ref
= o_sv_he
.he_ref
+ 1;
3005 n_sv_he
.he_data
= data
;
3007 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
) {
3008 if (n_sv_he
.he_ref
== 1)
3009 OSAddAtomic(1, &c_segment_svp_in_hash
);
3013 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
3015 if (got_ref
== TRUE
)
3019 if (hash_sindx
== C_SV_HASH_SIZE
)
3022 if (got_ref
== FALSE
)
3025 return (hash_sindx
);
3029 #if RECORD_THE_COMPRESSED_DATA
3032 c_compressed_record_data(char *src
, int c_size
)
3034 if ((c_compressed_record_cptr
+ c_size
+ 4) >= c_compressed_record_ebuf
)
3035 panic("c_compressed_record_cptr >= c_compressed_record_ebuf");
3037 *(int *)((void *)c_compressed_record_cptr
) = c_size
;
3039 c_compressed_record_cptr
+= 4;
3041 memcpy(c_compressed_record_cptr
, src
, c_size
);
3042 c_compressed_record_cptr
+= c_size
;
3048 c_compress_page(char *src
, c_slot_mapping_t slot_ptr
, c_segment_t
*current_chead
, char *scratch_buf
)
3051 int c_rounded_size
= 0;
3056 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_START
, *current_chead
, 0, 0, 0, 0);
3058 if ((c_seg
= c_seg_allocate(current_chead
)) == NULL
)
3061 * returns with c_seg lock held
3062 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3063 * c_nextslot has been allocated and
3064 * c_store.c_buffer populated
3066 assert(c_seg
->c_state
== C_IS_FILLING
);
3068 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_seg
->c_nextslot
);
3070 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(slot_ptr
);
3071 assert(slot_ptr
== (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(cs
));
3073 cs
->c_offset
= c_seg
->c_nextoffset
;
3075 max_csize
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)cs
->c_offset
);
3077 if (max_csize
> PAGE_SIZE
)
3078 max_csize
= PAGE_SIZE
;
3080 #if CHECKSUM_THE_DATA
3081 cs
->c_hash_data
= hash_string(src
, PAGE_SIZE
);
3084 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3086 c_size
= WKdm_compress_new((const WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3087 (WK_word
*)(uintptr_t)scratch_buf
, max_csize
- 4);
3089 assert(c_size
<= (max_csize
- 4) && c_size
>= -1);
3093 if (max_csize
< PAGE_SIZE
) {
3094 c_current_seg_filled(c_seg
, current_chead
);
3095 assert(*current_chead
== NULL
);
3097 lck_mtx_unlock_always(&c_seg
->c_lock
);
3099 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3104 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3106 OSAddAtomic(1, &c_segment_noncompressible_pages
);
3108 } else if (c_size
== 0) {
3112 * special case - this is a page completely full of a single 32 bit value
3114 hash_index
= c_segment_sv_hash_insert(*(uint32_t *)(uintptr_t)src
);
3116 if (hash_index
!= -1) {
3117 slot_ptr
->s_cindx
= hash_index
;
3118 slot_ptr
->s_cseg
= C_SV_CSEG_ID
;
3120 OSAddAtomic(1, &c_segment_svp_hash_succeeded
);
3121 #if RECORD_THE_COMPRESSED_DATA
3122 c_compressed_record_data(src
, 4);
3124 goto sv_compression
;
3128 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3130 OSAddAtomic(1, &c_segment_svp_hash_failed
);
3133 #if RECORD_THE_COMPRESSED_DATA
3134 c_compressed_record_data((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3137 #if CHECKSUM_THE_COMPRESSED_DATA
3138 cs
->c_hash_compressed_data
= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3140 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3142 PACK_C_SIZE(cs
, c_size
);
3143 c_seg
->c_bytes_used
+= c_rounded_size
;
3144 c_seg
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3146 slot_ptr
->s_cindx
= c_seg
->c_nextslot
++;
3147 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3148 slot_ptr
->s_cseg
= c_seg
->c_mysegno
+ 1;
3151 if (c_seg
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3152 c_current_seg_filled(c_seg
, current_chead
);
3153 assert(*current_chead
== NULL
);
3155 lck_mtx_unlock_always(&c_seg
->c_lock
);
3157 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3159 #if RECORD_THE_COMPRESSED_DATA
3160 if ((c_compressed_record_cptr
- c_compressed_record_sbuf
) >= C_SEG_ALLOCSIZE
) {
3161 c_compressed_record_write(c_compressed_record_sbuf
, (int)(c_compressed_record_cptr
- c_compressed_record_sbuf
));
3162 c_compressed_record_cptr
= c_compressed_record_sbuf
;
3166 OSAddAtomic64(c_size
, &c_segment_compressed_bytes
);
3167 OSAddAtomic64(c_rounded_size
, &compressor_bytes_used
);
3169 OSAddAtomic64(PAGE_SIZE
, &c_segment_input_bytes
);
3171 OSAddAtomic(1, &c_segment_pages_compressed
);
3172 OSAddAtomic(1, &sample_period_compression_count
);
3174 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_END
, *current_chead
, c_size
, c_segment_input_bytes
, c_segment_compressed_bytes
, 0);
3179 static inline void sv_decompress(int32_t *ddst
, int32_t pattern
) {
3181 memset_word(ddst
, pattern
, PAGE_SIZE
/ sizeof(int32_t));
3185 /* Unroll the pattern fill loop 4x to encourage the
3186 * compiler to emit NEON stores, cf.
3187 * <rdar://problem/25839866> Loop autovectorization
3189 * We use separate loops for each PAGE_SIZE
3190 * to allow the autovectorizer to engage, as PAGE_SIZE
3191 * is currently not a constant.
3194 if (PAGE_SIZE
== 4096) {
3195 for (i
= 0; i
< (4096U / sizeof(int32_t)); i
+= 4) {
3202 assert(PAGE_SIZE
== 16384);
3203 for (i
= 0; i
< (int)(16384U / sizeof(int32_t)); i
+= 4) {
3214 c_decompress_page(char *dst
, volatile c_slot_mapping_t slot_ptr
, int flags
, int *zeroslot
)
3223 boolean_t need_unlock
= TRUE
;
3224 boolean_t consider_defragmenting
= FALSE
;
3225 boolean_t kdp_mode
= FALSE
;
3227 if (__improbable(flags
& C_KDP
)) {
3229 panic("C_KDP passed to decompress page from outside of debugger context");
3232 assert((flags
& C_KEEP
) == C_KEEP
);
3233 assert((flags
& C_DONT_BLOCK
) == C_DONT_BLOCK
);
3235 if ((flags
& (C_DONT_BLOCK
| C_KEEP
)) != (C_DONT_BLOCK
| C_KEEP
)) {
3244 if (__probable(!kdp_mode
)) {
3245 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3247 if (kdp_lck_rw_lock_is_acquired_exclusive(&c_master_lock
)) {
3254 * if hibernation is enabled, it indicates (via a call
3255 * to 'vm_decompressor_lock' that no further
3256 * decompressions are allowed once it reaches
3257 * the point of flushing all of the currently dirty
3258 * anonymous memory through the compressor and out
3259 * to disk... in this state we allow freeing of compressed
3260 * pages and must honor the C_DONT_BLOCK case
3262 if (dst
&& decompressions_blocked
== TRUE
) {
3263 if (flags
& C_DONT_BLOCK
) {
3265 if (__probable(!kdp_mode
)) {
3266 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3273 * it's safe to atomically assert and block behind the
3274 * lock held in shared mode because "decompressions_blocked" is
3275 * only set and cleared and the thread_wakeup done when the lock
3276 * is held exclusively
3278 assert_wait((event_t
)&decompressions_blocked
, THREAD_UNINT
);
3280 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3282 thread_block(THREAD_CONTINUE_NULL
);
3287 /* s_cseg is actually "segno+1" */
3288 c_segno
= slot_ptr
->s_cseg
- 1;
3290 if (__improbable(c_segno
>= c_segments_available
))
3291 panic("c_decompress_page: c_segno %d >= c_segments_available %d, slot_ptr(%p), slot_data(%x)",
3292 c_segno
, c_segments_available
, slot_ptr
, *(int *)((void *)slot_ptr
));
3294 if (__improbable(c_segments
[c_segno
].c_segno
< c_segments_available
))
3295 panic("c_decompress_page: c_segno %d is free, slot_ptr(%p), slot_data(%x)",
3296 c_segno
, slot_ptr
, *(int *)((void *)slot_ptr
));
3298 c_seg
= c_segments
[c_segno
].c_seg
;
3300 if (__probable(!kdp_mode
)) {
3301 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3303 if (kdp_lck_mtx_lock_spin_is_acquired(&c_seg
->c_lock
)) {
3308 assert(c_seg
->c_state
!= C_IS_EMPTY
&& c_seg
->c_state
!= C_IS_FREE
);
3310 if (dst
== NULL
&& c_seg
->c_busy_swapping
) {
3311 assert(c_seg
->c_busy
);
3313 goto bypass_busy_check
;
3315 if (flags
& C_DONT_BLOCK
) {
3316 if (c_seg
->c_busy
|| (C_SEG_IS_ONDISK(c_seg
) && dst
)) {
3323 if (c_seg
->c_busy
) {
3325 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3327 c_seg_wait_on_busy(c_seg
);
3333 c_indx
= slot_ptr
->s_cindx
;
3335 if (__improbable(c_indx
>= c_seg
->c_nextslot
))
3336 panic("c_decompress_page: c_indx %d >= c_nextslot %d, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3337 c_indx
, c_seg
->c_nextslot
, c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3339 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3341 c_size
= UNPACK_C_SIZE(cs
);
3343 if (__improbable(c_size
== 0))
3344 panic("c_decompress_page: c_size == 0, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3345 c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3347 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3350 uint32_t age_of_cseg
;
3351 clock_sec_t cur_ts_sec
;
3352 clock_nsec_t cur_ts_nsec
;
3354 if (C_SEG_IS_ONDISK(c_seg
)) {
3355 assert(kdp_mode
== FALSE
);
3356 retval
= c_seg_swapin(c_seg
, FALSE
, TRUE
);
3357 assert(retval
== 0);
3361 if (c_seg
->c_state
== C_ON_BAD_Q
) {
3362 assert(c_seg
->c_store
.c_buffer
== NULL
);
3365 goto c_seg_invalid_data
;
3367 #if CHECKSUM_THE_COMPRESSED_DATA
3368 if (cs
->c_hash_compressed_data
!= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))
3369 panic("compressed data doesn't match original hash: 0x%x, seg: %p, offset: %d, c_size: %d", cs
->c_hash_compressed_data
, c_seg
, cs
->c_offset
, c_size
);
3371 if (c_rounded_size
== PAGE_SIZE
) {
3373 * page wasn't compressible... just copy it out
3375 memcpy(dst
, &c_seg
->c_store
.c_buffer
[cs
->c_offset
], PAGE_SIZE
);
3376 } else if (c_size
== 4) {
3381 * page was populated with a single value
3382 * that didn't fit into our fast hash
3383 * so we packed it in as a single non-compressed value
3384 * that we need to populate the page with
3386 dptr
= (int32_t *)(uintptr_t)dst
;
3387 data
= *(int32_t *)(&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
3388 sv_decompress(dptr
, data
);
3393 if (__probable(!kdp_mode
)) {
3395 * we're behind the c_seg lock held in spin mode
3396 * which means pre-emption is disabled... therefore
3397 * the following sequence is atomic and safe
3399 my_cpu_no
= cpu_number();
3401 assert(my_cpu_no
< compressor_cpus
);
3403 scratch_buf
= &compressor_scratch_bufs
[my_cpu_no
* vm_compressor_get_decode_scratch_size()];
3405 scratch_buf
= kdp_compressor_scratch_buf
;
3408 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3410 WKdm_decompress_new((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3411 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3415 #if CHECKSUM_THE_DATA
3416 if (cs
->c_hash_data
!= hash_string(dst
, PAGE_SIZE
))
3417 panic("decompressed data doesn't match original cs: %p, hash: %d, offset: %d, c_size: %d", cs
, cs
->c_hash_data
, cs
->c_offset
, c_size
);
3420 if (c_seg
->c_swappedin_ts
== 0 && !kdp_mode
) {
3422 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
3424 age_of_cseg
= (uint32_t)cur_ts_sec
- c_seg
->c_creation_ts
;
3425 if (age_of_cseg
< DECOMPRESSION_SAMPLE_MAX_AGE
)
3426 OSAddAtomic(1, &age_of_decompressions_during_sample_period
[age_of_cseg
]);
3428 OSAddAtomic(1, &overage_decompressions_during_sample_period
);
3430 OSAddAtomic(1, &sample_period_decompression_count
);
3435 if (flags
& C_KEEP
) {
3439 assert(kdp_mode
== FALSE
);
3441 c_seg
->c_bytes_unused
+= c_rounded_size
;
3442 c_seg
->c_bytes_used
-= c_rounded_size
;
3445 if (c_indx
< c_seg
->c_firstemptyslot
)
3446 c_seg
->c_firstemptyslot
= c_indx
;
3448 OSAddAtomic(-1, &c_segment_pages_compressed
);
3450 if (c_seg
->c_state
!= C_ON_BAD_Q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
3452 * C_SEG_IS_ONDISK == TRUE can occur when we're doing a
3453 * free of a compressed page (i.e. dst == NULL)
3455 OSAddAtomic64(-c_rounded_size
, &compressor_bytes_used
);
3457 if (c_seg
->c_busy_swapping
) {
3459 * bypass case for c_busy_swapping...
3460 * let the swapin/swapout paths deal with putting
3461 * the c_seg on the minor compaction queue if needed
3463 assert(c_seg
->c_busy
);
3466 assert(!c_seg
->c_busy
);
3468 if (c_seg
->c_state
!= C_IS_FILLING
) {
3469 if (c_seg
->c_bytes_used
== 0) {
3470 if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3471 int pages_populated
;
3473 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
3474 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3476 if (pages_populated
) {
3478 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3479 assert(c_seg
->c_store
.c_buffer
!= NULL
);
3482 lck_mtx_unlock_always(&c_seg
->c_lock
);
3484 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_seg
->c_store
.c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
3486 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3487 C_SEG_WAKEUP_DONE(c_seg
);
3489 if (!c_seg
->c_on_minorcompact_q
&& c_seg
->c_state
!= C_ON_SWAPOUT_Q
)
3490 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3492 if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
) {
3494 c_seg_move_to_sparse_list(c_seg
);
3495 consider_defragmenting
= TRUE
;
3498 } else if (c_seg
->c_on_minorcompact_q
) {
3500 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3502 if (C_SEG_SHOULD_MINORCOMPACT(c_seg
)) {
3503 c_seg_try_minor_compaction_and_unlock(c_seg
);
3504 need_unlock
= FALSE
;
3506 } else if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3508 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
) {
3509 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3511 } else if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
&& C_SEG_ONDISK_IS_SPARSE(c_seg
)) {
3513 c_seg_move_to_sparse_list(c_seg
);
3514 consider_defragmenting
= TRUE
;
3518 if (__improbable(kdp_mode
)) {
3522 if (need_unlock
== TRUE
)
3523 lck_mtx_unlock_always(&c_seg
->c_lock
);
3525 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3527 if (consider_defragmenting
== TRUE
)
3528 vm_swap_consider_defragmenting();
3536 vm_compressor_get(ppnum_t pn
, int *slot
, int flags
)
3538 c_slot_mapping_t slot_ptr
;
3544 dst
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3546 #error "unsupported architecture"
3548 slot_ptr
= (c_slot_mapping_t
)slot
;
3550 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3555 * page was populated with a single value
3556 * that found a home in our hash table
3557 * grab that value from the hash and populate the page
3558 * that we need to populate the page with
3560 dptr
= (int32_t *)(uintptr_t)dst
;
3561 data
= c_segment_sv_hash_table
[slot_ptr
->s_cindx
].he_data
;
3563 memset_word(dptr
, data
, PAGE_SIZE
/ sizeof(int32_t));
3568 for (i
= 0; i
< (int)(PAGE_SIZE
/ sizeof(int32_t)); i
++)
3572 if ( !(flags
& C_KEEP
)) {
3573 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3575 OSAddAtomic(-1, &c_segment_pages_compressed
);
3579 OSAddAtomic(1, &c_segment_svp_nonzero_decompressions
);
3581 OSAddAtomic(1, &c_segment_svp_zero_decompressions
);
3586 retval
= c_decompress_page(dst
, slot_ptr
, flags
, &zeroslot
);
3589 * zeroslot will be set to 0 by c_decompress_page if (flags & C_KEEP)
3590 * or (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be TRUE
3596 * returns 0 if we successfully decompressed a page from a segment already in memory
3597 * returns 1 if we had to first swap in the segment, before successfully decompressing the page
3598 * returns -1 if we encountered an error swapping in the segment - decompression failed
3599 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be true
3606 vm_compressor_free(int *slot
, int flags
)
3608 c_slot_mapping_t slot_ptr
;
3612 assert(flags
== 0 || flags
== C_DONT_BLOCK
);
3614 slot_ptr
= (c_slot_mapping_t
)slot
;
3616 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3618 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3619 OSAddAtomic(-1, &c_segment_pages_compressed
);
3624 retval
= c_decompress_page(NULL
, slot_ptr
, flags
, &zeroslot
);
3626 * returns 0 if we successfully freed the specified compressed page
3627 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' set
3633 assert(retval
== -2);
3640 vm_compressor_put(ppnum_t pn
, int *slot
, void **current_chead
, char *scratch_buf
)
3646 src
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3648 #error "unsupported architecture"
3650 retval
= c_compress_page(src
, (c_slot_mapping_t
)slot
, (c_segment_t
*)current_chead
, scratch_buf
);
3656 vm_compressor_transfer(
3660 c_slot_mapping_t dst_slot
, src_slot
;
3665 src_slot
= (c_slot_mapping_t
) src_slot_p
;
3667 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3668 *dst_slot_p
= *src_slot_p
;
3672 dst_slot
= (c_slot_mapping_t
) dst_slot_p
;
3674 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3675 /* get segment for src_slot */
3676 c_seg
= c_segments
[src_slot
->s_cseg
-1].c_seg
;
3678 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3679 /* wait if it's busy */
3680 if (c_seg
->c_busy
&& !c_seg
->c_busy_swapping
) {
3681 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3682 c_seg_wait_on_busy(c_seg
);
3685 /* find the c_slot */
3686 c_indx
= src_slot
->s_cindx
;
3687 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3688 /* point the c_slot back to dst_slot instead of src_slot */
3689 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(dst_slot
);
3691 *dst_slot_p
= *src_slot_p
;
3693 lck_mtx_unlock_always(&c_seg
->c_lock
);
3694 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3699 int freezer_finished_filling
= 0;
3702 vm_compressor_finished_filling(
3703 void **current_chead
)
3707 if ((c_seg
= *(c_segment_t
*)current_chead
) == NULL
)
3710 assert(c_seg
->c_state
== C_IS_FILLING
);
3712 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3714 c_current_seg_filled(c_seg
, (c_segment_t
*)current_chead
);
3716 lck_mtx_unlock_always(&c_seg
->c_lock
);
3718 freezer_finished_filling
++;
3723 * This routine is used to transfer the compressed chunks from
3724 * the c_seg/cindx pointed to by slot_p into a new c_seg headed
3725 * by the current_chead and a new cindx within that c_seg.
3727 * Currently, this routine is only used by the "freezer backed by
3728 * compressor with swap" mode to create a series of c_segs that
3729 * only contain compressed data belonging to one task. So, we
3730 * move a task's previously compressed data into a set of new
3731 * c_segs which will also hold the task's yet to be compressed data.
3735 vm_compressor_relocate(
3736 void **current_chead
,
3739 c_slot_mapping_t slot_ptr
;
3740 c_slot_mapping_t src_slot
;
3741 uint32_t c_rounded_size
;
3747 c_segment_t c_seg_dst
= NULL
;
3748 c_segment_t c_seg_src
= NULL
;
3749 kern_return_t kr
= KERN_SUCCESS
;
3752 src_slot
= (c_slot_mapping_t
) slot_p
;
3754 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3756 * no need to relocate... this is a page full of a single
3757 * value which is hashed to a single entry not contained
3764 c_seg_dst
= c_seg_allocate((c_segment_t
*)current_chead
);
3766 * returns with c_seg lock held
3767 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3768 * c_nextslot has been allocated and
3769 * c_store.c_buffer populated
3771 if (c_seg_dst
== NULL
) {
3773 * Out of compression segments?
3775 kr
= KERN_RESOURCE_SHORTAGE
;
3779 assert(c_seg_dst
->c_busy
== 0);
3781 C_SEG_BUSY(c_seg_dst
);
3783 dst_slot
= c_seg_dst
->c_nextslot
;
3785 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3788 c_seg_src
= c_segments
[src_slot
->s_cseg
- 1].c_seg
;
3790 assert(c_seg_dst
!= c_seg_src
);
3792 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3794 if (C_SEG_IS_ONDISK(c_seg_src
)) {
3797 * A "thaw" can mark a process as eligible for
3798 * another freeze cycle without bringing any of
3799 * its swapped out c_segs back from disk (because
3800 * that is done on-demand).
3802 * If the src c_seg we find for our pre-compressed
3803 * data is already on-disk, then we are dealing
3804 * with an app's data that is already packed and
3805 * swapped out. Don't do anything.
3808 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3810 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3817 if (c_seg_src
->c_busy
) {
3819 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3820 c_seg_wait_on_busy(c_seg_src
);
3824 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3829 C_SEG_BUSY(c_seg_src
);
3831 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3833 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3835 /* find the c_slot */
3836 c_indx
= src_slot
->s_cindx
;
3838 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, c_indx
);
3840 c_size
= UNPACK_C_SIZE(c_src
);
3844 if (c_size
> (uint32_t)(C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)c_seg_dst
->c_nextoffset
))) {
3846 * This segment is full. We need a new one.
3849 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3851 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3852 C_SEG_WAKEUP_DONE(c_seg_src
);
3853 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3857 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
3859 assert(c_seg_dst
->c_busy
);
3860 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
3861 assert(!c_seg_dst
->c_on_minorcompact_q
);
3863 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
3864 assert(*current_chead
== NULL
);
3866 C_SEG_WAKEUP_DONE(c_seg_dst
);
3868 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3872 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3877 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
3879 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
);
3881 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3883 cslot_copy(c_dst
, c_src
);
3884 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
3886 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
3887 c_seg_dst
->c_firstemptyslot
++;
3889 c_seg_dst
->c_nextslot
++;
3890 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
3891 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3894 PACK_C_SIZE(c_src
, 0);
3896 c_seg_src
->c_bytes_used
-= c_rounded_size
;
3897 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
3899 if (c_indx
< c_seg_src
->c_firstemptyslot
) {
3900 c_seg_src
->c_firstemptyslot
= c_indx
;
3903 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
3905 PAGE_REPLACEMENT_ALLOWED(TRUE
);
3906 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
3907 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3908 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
3909 slot_ptr
->s_cindx
= dst_slot
;
3911 PAGE_REPLACEMENT_ALLOWED(FALSE
);
3916 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3918 C_SEG_WAKEUP_DONE(c_seg_src
);
3920 if (c_seg_src
->c_bytes_used
== 0 && c_seg_src
->c_state
!= C_IS_FILLING
) {
3921 if (!c_seg_src
->c_on_minorcompact_q
)
3922 c_seg_need_delayed_compaction(c_seg_src
, FALSE
);
3925 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3930 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3932 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
3934 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3936 * Nearing or exceeded maximum slot and offset capacity.
3938 assert(c_seg_dst
->c_busy
);
3939 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
3940 assert(!c_seg_dst
->c_on_minorcompact_q
);
3942 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
3943 assert(*current_chead
== NULL
);
3946 C_SEG_WAKEUP_DONE(c_seg_dst
);
3948 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3952 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3957 #endif /* CONFIG_FREEZE */