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(void);
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))
333 static uint32_t no_paging_space_action_in_progress
= 0;
334 extern void memorystatus_send_low_swap_note(void);
337 vm_compressor_take_paging_space_action(void)
339 if (no_paging_space_action_in_progress
== 0) {
341 if (OSCompareAndSwap(0, 1, (UInt32
*)&no_paging_space_action_in_progress
)) {
343 if (no_paging_space_action()) {
344 memorystatus_send_low_swap_note();
347 no_paging_space_action_in_progress
= 0;
354 vm_compressor_init_locks(void)
356 lck_grp_attr_setdefault(&vm_compressor_lck_grp_attr
);
357 lck_grp_init(&vm_compressor_lck_grp
, "vm_compressor", &vm_compressor_lck_grp_attr
);
358 lck_attr_setdefault(&vm_compressor_lck_attr
);
360 lck_rw_init(&c_master_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
365 vm_decompressor_lock(void)
367 PAGE_REPLACEMENT_ALLOWED(TRUE
);
369 decompressions_blocked
= TRUE
;
371 PAGE_REPLACEMENT_ALLOWED(FALSE
);
375 vm_decompressor_unlock(void)
377 PAGE_REPLACEMENT_ALLOWED(TRUE
);
379 decompressions_blocked
= FALSE
;
381 PAGE_REPLACEMENT_ALLOWED(FALSE
);
383 thread_wakeup((event_t
)&decompressions_blocked
);
386 static inline void cslot_copy(c_slot_t cdst
, c_slot_t csrc
) {
387 #if CHECKSUM_THE_DATA
388 cdst
->c_hash_data
= csrc
->c_hash_data
;
390 #if CHECKSUM_THE_COMPRESSED_DATA
391 cdst
->c_hash_compressed_data
= csrc
->c_hash_compressed_data
;
393 cdst
->c_size
= csrc
->c_size
;
394 cdst
->c_packed_ptr
= csrc
->c_packed_ptr
;
397 vm_map_t compressor_map
;
400 vm_compressor_init(void)
403 struct c_slot cs_dummy
;
404 c_slot_t cs
= &cs_dummy
;
405 int c_segment_min_size
;
406 int c_segment_padded_size
;
407 kern_return_t retval
= KERN_SUCCESS
;
408 vm_offset_t start_addr
= 0;
409 vm_size_t c_segments_arr_size
= 0, compressor_submap_size
= 0;
410 #if RECORD_THE_COMPRESSED_DATA
411 vm_size_t c_compressed_record_sbuf_size
= 0;
412 #endif /* RECORD_THE_COMPRESSED_DATA */
415 * ensure that any pointer that gets created from
416 * the vm_page zone can be packed properly
418 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_min_address
);
420 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_min_address
)
421 panic("C_SLOT_UNPACK_PTR failed on zone_map_min_address - %p", (void *)zone_map_min_address
);
423 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_max_address
);
425 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_max_address
)
426 panic("C_SLOT_UNPACK_PTR failed on zone_map_max_address - %p", (void *)zone_map_max_address
);
429 assert((C_SEGMENTS_PER_PAGE
* sizeof(union c_segu
)) == PAGE_SIZE
);
431 PE_parse_boot_argn("vm_compression_limit", &vm_compression_limit
, sizeof (vm_compression_limit
));
433 if (max_mem
<= (3ULL * 1024ULL * 1024ULL * 1024ULL)) {
434 vm_compressor_minorcompact_threshold_divisor
= 11;
435 vm_compressor_majorcompact_threshold_divisor
= 13;
436 vm_compressor_unthrottle_threshold_divisor
= 20;
437 vm_compressor_catchup_threshold_divisor
= 35;
439 vm_compressor_minorcompact_threshold_divisor
= 20;
440 vm_compressor_majorcompact_threshold_divisor
= 25;
441 vm_compressor_unthrottle_threshold_divisor
= 35;
442 vm_compressor_catchup_threshold_divisor
= 50;
445 * vm_page_init_lck_grp is now responsible for calling vm_compressor_init_locks
446 * c_master_lock needs to be available early so that "vm_page_find_contiguous" can
447 * use PAGE_REPLACEMENT_ALLOWED to coordinate with the compressor.
450 c_list_lock
= lck_mtx_alloc_init(&vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
452 queue_init(&c_bad_list_head
);
453 queue_init(&c_age_list_head
);
454 queue_init(&c_minor_list_head
);
455 queue_init(&c_major_list_head
);
456 queue_init(&c_filling_list_head
);
457 queue_init(&c_swapout_list_head
);
458 queue_init(&c_swappedin_list_head
);
459 queue_init(&c_swappedout_list_head
);
460 queue_init(&c_swappedout_sparse_list_head
);
462 c_segment_min_size
= sizeof(struct c_segment
) + (C_SEG_SLOT_VAR_ARRAY_MIN_LEN
* sizeof(struct c_slot
));
464 for (c_segment_padded_size
= 128; c_segment_padded_size
< c_segment_min_size
; c_segment_padded_size
= c_segment_padded_size
<< 1);
466 compressor_segment_zone
= zinit(c_segment_padded_size
, 128000 * c_segment_padded_size
, PAGE_SIZE
, "compressor_segment");
467 zone_change(compressor_segment_zone
, Z_CALLERACCT
, FALSE
);
468 zone_change(compressor_segment_zone
, Z_NOENCRYPT
, TRUE
);
470 c_seg_fixed_array_len
= (c_segment_padded_size
- sizeof(struct c_segment
)) / sizeof(struct c_slot
);
472 c_free_segno_head
= -1;
473 c_segments_available
= 0;
475 if (vm_compression_limit
== 0) {
476 c_segment_pages_compressed_limit
= (uint32_t)((max_mem
/ PAGE_SIZE
)) * vm_scale
;
478 #define OLD_SWAP_LIMIT (1024 * 1024 * 16)
479 #define MAX_SWAP_LIMIT (1024 * 1024 * 128)
481 if (c_segment_pages_compressed_limit
> (OLD_SWAP_LIMIT
))
482 c_segment_pages_compressed_limit
= OLD_SWAP_LIMIT
;
484 if (c_segment_pages_compressed_limit
< (uint32_t)(max_mem
/ PAGE_SIZE_64
))
485 c_segment_pages_compressed_limit
= (uint32_t)(max_mem
/ PAGE_SIZE_64
);
487 if (vm_compression_limit
< MAX_SWAP_LIMIT
)
488 c_segment_pages_compressed_limit
= vm_compression_limit
;
490 c_segment_pages_compressed_limit
= MAX_SWAP_LIMIT
;
492 if ((c_segments_limit
= c_segment_pages_compressed_limit
/ (C_SEG_BUFSIZE
/ PAGE_SIZE
)) > C_SEG_MAX_LIMIT
)
493 c_segments_limit
= C_SEG_MAX_LIMIT
;
495 c_segment_pages_compressed_nearing_limit
= (c_segment_pages_compressed_limit
* 98) / 100;
496 c_segments_nearing_limit
= (c_segments_limit
* 98) / 100;
498 c_segments_busy
= FALSE
;
501 * Submap needs space for:
504 * - swap reclaimations -- C_SEG_BUFSIZE
506 c_segments_arr_size
= vm_map_round_page((sizeof(union c_segu
) * c_segments_limit
),VM_MAP_PAGE_MASK(kernel_map
));
507 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
));
509 compressor_submap_size
= c_segments_arr_size
+ c_buffers_size
+ C_SEG_BUFSIZE
;
511 #if RECORD_THE_COMPRESSED_DATA
512 c_compressed_record_sbuf_size
= (vm_size_t
)C_SEG_ALLOCSIZE
+ (PAGE_SIZE
* 2);
513 compressor_submap_size
+= c_compressed_record_sbuf_size
;
514 #endif /* RECORD_THE_COMPRESSED_DATA */
516 retval
= kmem_suballoc(kernel_map
, &start_addr
, compressor_submap_size
,
517 FALSE
, VM_FLAGS_ANYWHERE
| VM_FLAGS_PERMANENT
| VM_MAKE_TAG(0),
520 if (retval
!= KERN_SUCCESS
)
521 panic("vm_compressor_init: kmem_suballoc failed");
523 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
)
524 panic("vm_compressor_init: kernel_memory_allocate failed - c_segments\n");
525 if (kernel_memory_allocate(compressor_map
, &c_buffers
, c_buffers_size
, 0, KMA_COMPRESSOR
| KMA_VAONLY
| KMA_PERMANENT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
)
526 panic("vm_compressor_init: kernel_memory_allocate failed - c_buffers\n");
528 c_segments_next_page
= (caddr_t
)c_segments
;
529 vm_compressor_algorithm_init();
532 host_basic_info_data_t hinfo
;
533 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
536 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
538 compressor_cpus
= hinfo
.max_cpus
;
539 compressor_scratch_bufs
= kalloc_tag(compressor_cpus
* vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
541 kdp_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_decode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
542 kdp_compressor_decompressed_page
= kalloc_tag(PAGE_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
543 kdp_compressor_decompressed_page_paddr
= kvtophys((vm_offset_t
)kdp_compressor_decompressed_page
);
544 kdp_compressor_decompressed_page_ppnum
= (ppnum_t
) atop(kdp_compressor_decompressed_page_paddr
);
547 freezer_compressor_scratch_buf
= kalloc_tag(vm_compressor_get_encode_scratch_size(), VM_KERN_MEMORY_COMPRESSOR
);
550 #if RECORD_THE_COMPRESSED_DATA
551 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
)
552 panic("vm_compressor_init: kernel_memory_allocate failed - c_compressed_record_sbuf\n");
554 c_compressed_record_cptr
= c_compressed_record_sbuf
;
555 c_compressed_record_ebuf
= c_compressed_record_sbuf
+ c_compressed_record_sbuf_size
;
558 if (kernel_thread_start_priority((thread_continue_t
)vm_compressor_swap_trigger_thread
, NULL
,
559 BASEPRI_PREEMPT
- 1, &thread
) != KERN_SUCCESS
) {
560 panic("vm_compressor_swap_trigger_thread: create failed");
562 thread_deallocate(thread
);
564 if (vm_pageout_internal_start() != KERN_SUCCESS
) {
565 panic("vm_compressor_init: Failed to start the internal pageout thread.\n");
567 if (VM_CONFIG_SWAP_IS_PRESENT
)
568 vm_compressor_swap_init();
570 if (VM_CONFIG_COMPRESSOR_IS_ACTIVE
)
571 vm_compressor_is_active
= 1;
574 memorystatus_freeze_enabled
= TRUE
;
575 #endif /* CONFIG_FREEZE */
577 vm_compressor_available
= 1;
579 vm_page_reactivate_all_throttled();
583 #if VALIDATE_C_SEGMENTS
586 c_seg_validate(c_segment_t c_seg
, boolean_t must_be_compact
)
590 int32_t bytes_unused
;
591 uint32_t c_rounded_size
;
595 if (c_seg
->c_firstemptyslot
< c_seg
->c_nextslot
) {
596 c_indx
= c_seg
->c_firstemptyslot
;
597 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
600 panic("c_seg_validate: no slot backing c_firstemptyslot");
603 panic("c_seg_validate: c_firstemptyslot has non-zero size (%d)\n", cs
->c_size
);
608 for (c_indx
= 0; c_indx
< c_seg
->c_nextslot
; c_indx
++) {
610 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
612 c_size
= UNPACK_C_SIZE(cs
);
614 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
616 bytes_used
+= c_rounded_size
;
618 #if CHECKSUM_THE_COMPRESSED_DATA
619 if (c_size
&& cs
->c_hash_compressed_data
!= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))
620 panic("compressed data doesn't match original");
624 if (bytes_used
!= c_seg
->c_bytes_used
)
625 panic("c_seg_validate: bytes_used mismatch - found %d, segment has %d\n", bytes_used
, c_seg
->c_bytes_used
);
627 if (c_seg
->c_bytes_used
> C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
628 panic("c_seg_validate: c_bytes_used > c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
629 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
631 if (must_be_compact
) {
632 if (c_seg
->c_bytes_used
!= C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
633 panic("c_seg_validate: c_bytes_used doesn't match c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
634 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
642 c_seg_need_delayed_compaction(c_segment_t c_seg
, boolean_t c_list_lock_held
)
644 boolean_t clear_busy
= FALSE
;
646 if (c_list_lock_held
== FALSE
) {
647 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
650 lck_mtx_unlock_always(&c_seg
->c_lock
);
651 lck_mtx_lock_spin_always(c_list_lock
);
652 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
657 assert(c_seg
->c_state
!= C_IS_FILLING
);
659 if (!c_seg
->c_on_minorcompact_q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
660 queue_enter(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
661 c_seg
->c_on_minorcompact_q
= 1;
664 if (c_list_lock_held
== FALSE
)
665 lck_mtx_unlock_always(c_list_lock
);
667 if (clear_busy
== TRUE
)
668 C_SEG_WAKEUP_DONE(c_seg
);
672 unsigned int c_seg_moved_to_sparse_list
= 0;
675 c_seg_move_to_sparse_list(c_segment_t c_seg
)
677 boolean_t clear_busy
= FALSE
;
679 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
682 lck_mtx_unlock_always(&c_seg
->c_lock
);
683 lck_mtx_lock_spin_always(c_list_lock
);
684 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
688 c_seg_switch_state(c_seg
, C_ON_SWAPPEDOUTSPARSE_Q
, FALSE
);
690 c_seg_moved_to_sparse_list
++;
692 lck_mtx_unlock_always(c_list_lock
);
694 if (clear_busy
== TRUE
)
695 C_SEG_WAKEUP_DONE(c_seg
);
700 c_seg_insert_into_q(queue_head_t
*qhead
, c_segment_t c_seg
)
702 c_segment_t c_seg_next
;
704 if (queue_empty(qhead
)) {
705 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
707 c_seg_next
= (c_segment_t
)queue_first(qhead
);
711 if (c_seg
->c_generation_id
< c_seg_next
->c_generation_id
) {
712 queue_insert_before(qhead
, c_seg
, c_seg_next
, c_segment_t
, c_age_list
);
715 c_seg_next
= (c_segment_t
) queue_next(&c_seg_next
->c_age_list
);
717 if (queue_end(qhead
, (queue_entry_t
) c_seg_next
)) {
718 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
726 int try_minor_compaction_failed
= 0;
727 int try_minor_compaction_succeeded
= 0;
730 c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
)
733 assert(c_seg
->c_on_minorcompact_q
);
735 * c_seg is currently on the delayed minor compaction
736 * queue and we have c_seg locked... if we can get the
737 * c_list_lock w/o blocking (if we blocked we could deadlock
738 * because the lock order is c_list_lock then c_seg's lock)
739 * we'll pull it from the delayed list and free it directly
741 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
743 * c_list_lock is held, we need to bail
745 try_minor_compaction_failed
++;
747 lck_mtx_unlock_always(&c_seg
->c_lock
);
749 try_minor_compaction_succeeded
++;
752 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, FALSE
);
758 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
)
762 assert(c_seg
->c_busy
);
765 * check for the case that can occur when we are not swapping
766 * and this segment has been major compacted in the past
767 * and moved to the majorcompact q to remove it from further
768 * consideration... if the occupancy falls too low we need
769 * to put it back on the age_q so that it will be considered
770 * in the next major compaction sweep... if we don't do this
771 * we will eventually run into the c_segments_limit
773 if (c_seg
->c_state
== C_ON_MAJORCOMPACT_Q
&& C_SEG_SHOULD_MAJORCOMPACT(c_seg
)) {
775 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
777 if (!c_seg
->c_on_minorcompact_q
) {
778 if (clear_busy
== TRUE
)
779 C_SEG_WAKEUP_DONE(c_seg
);
781 lck_mtx_unlock_always(&c_seg
->c_lock
);
785 queue_remove(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
786 c_seg
->c_on_minorcompact_q
= 0;
789 lck_mtx_unlock_always(c_list_lock
);
791 if (disallow_page_replacement
== TRUE
) {
792 lck_mtx_unlock_always(&c_seg
->c_lock
);
794 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
796 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
798 c_seg_freed
= c_seg_minor_compaction_and_unlock(c_seg
, clear_busy
);
800 if (disallow_page_replacement
== TRUE
)
801 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
803 if (need_list_lock
== TRUE
)
804 lck_mtx_lock_spin_always(c_list_lock
);
806 return (c_seg_freed
);
811 c_seg_wait_on_busy(c_segment_t c_seg
)
814 assert_wait((event_t
) (c_seg
), THREAD_UNINT
);
816 lck_mtx_unlock_always(&c_seg
->c_lock
);
817 thread_block(THREAD_CONTINUE_NULL
);
822 c_seg_switch_state(c_segment_t c_seg
, int new_state
, boolean_t insert_head
)
824 int old_state
= c_seg
->c_state
;
826 #if __i386__ || __x86_64__
827 if (new_state
!= C_IS_FILLING
)
828 LCK_MTX_ASSERT(&c_seg
->c_lock
, LCK_MTX_ASSERT_OWNED
);
829 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
834 assert(new_state
== C_IS_FILLING
|| new_state
== C_IS_FREE
);
840 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_ON_SWAPOUT_Q
);
842 queue_remove(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
847 assert(new_state
== C_ON_SWAPOUT_Q
|| new_state
== C_ON_MAJORCOMPACT_Q
||
848 new_state
== C_IS_FREE
);
850 queue_remove(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
854 case C_ON_SWAPPEDIN_Q
:
855 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
857 queue_remove(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
862 assert(new_state
== C_ON_SWAPPEDOUT_Q
|| new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
863 new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
|| new_state
== C_IS_EMPTY
);
865 queue_remove(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
866 thread_wakeup((event_t
)&compaction_swapper_running
);
870 case C_ON_SWAPPEDOUT_Q
:
871 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
872 new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
873 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
875 queue_remove(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
876 c_swappedout_count
--;
879 case C_ON_SWAPPEDOUTSPARSE_Q
:
880 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_AGE_Q
||
881 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
883 queue_remove(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
884 c_swappedout_sparse_count
--;
887 case C_ON_MAJORCOMPACT_Q
:
888 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
890 queue_remove(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
895 assert(new_state
== C_IS_FREE
);
897 queue_remove(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
902 panic("c_seg %p has bad c_state = %d\n", c_seg
, old_state
);
907 assert(old_state
!= C_IS_FILLING
);
912 assert(old_state
== C_ON_SWAPOUT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
918 assert(old_state
== C_IS_EMPTY
);
920 queue_enter(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
925 assert(old_state
== C_IS_FILLING
|| old_state
== C_ON_SWAPPEDIN_Q
|| old_state
== C_ON_SWAPOUT_Q
||
926 old_state
== C_ON_MAJORCOMPACT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
928 if (old_state
== C_IS_FILLING
)
929 queue_enter(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
931 if (!queue_empty(&c_age_list_head
)) {
934 c_first
= (c_segment_t
)queue_first(&c_age_list_head
);
935 c_seg
->c_creation_ts
= c_first
->c_creation_ts
;
937 queue_enter_first(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
942 case C_ON_SWAPPEDIN_Q
:
943 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
945 if (insert_head
== TRUE
)
946 queue_enter_first(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
948 queue_enter(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
953 assert(old_state
== C_ON_AGE_Q
|| old_state
== C_IS_FILLING
);
955 if (insert_head
== TRUE
)
956 queue_enter_first(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
958 queue_enter(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
962 case C_ON_SWAPPEDOUT_Q
:
963 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
);
965 if (insert_head
== TRUE
)
966 queue_enter_first(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
968 queue_enter(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
969 c_swappedout_count
++;
972 case C_ON_SWAPPEDOUTSPARSE_Q
:
973 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
);
975 if (insert_head
== TRUE
)
976 queue_enter_first(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
978 queue_enter(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
980 c_swappedout_sparse_count
++;
983 case C_ON_MAJORCOMPACT_Q
:
984 assert(c_seg
->c_state
== C_ON_AGE_Q
);
986 if (insert_head
== TRUE
)
987 queue_enter_first(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
989 queue_enter(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
994 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
996 if (insert_head
== TRUE
)
997 queue_enter_first(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
999 queue_enter(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
1004 panic("c_seg %p requesting bad c_state = %d\n", c_seg
, new_state
);
1006 c_seg
->c_state
= new_state
;
1012 c_seg_free(c_segment_t c_seg
)
1014 assert(c_seg
->c_busy
);
1016 lck_mtx_unlock_always(&c_seg
->c_lock
);
1017 lck_mtx_lock_spin_always(c_list_lock
);
1018 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1020 c_seg_free_locked(c_seg
);
1025 c_seg_free_locked(c_segment_t c_seg
)
1028 int pages_populated
= 0;
1029 int32_t *c_buffer
= NULL
;
1030 uint64_t c_swap_handle
= 0;
1032 assert(c_seg
->c_busy
);
1033 assert(!c_seg
->c_on_minorcompact_q
);
1034 assert(!c_seg
->c_busy_swapping
);
1036 if (c_seg
->c_overage_swap
== TRUE
) {
1037 c_overage_swapped_count
--;
1038 c_seg
->c_overage_swap
= FALSE
;
1040 if ( !(C_SEG_IS_ONDISK(c_seg
)))
1041 c_buffer
= c_seg
->c_store
.c_buffer
;
1043 c_swap_handle
= c_seg
->c_store
.c_swap_handle
;
1045 c_seg_switch_state(c_seg
, C_IS_FREE
, FALSE
);
1047 lck_mtx_unlock_always(c_list_lock
);
1050 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
1051 c_seg
->c_store
.c_buffer
= NULL
;
1053 c_seg
->c_store
.c_swap_handle
= (uint64_t)-1;
1055 lck_mtx_unlock_always(&c_seg
->c_lock
);
1058 if (pages_populated
)
1059 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
1061 } else if (c_swap_handle
) {
1063 * Free swap space on disk.
1065 vm_swap_free(c_swap_handle
);
1067 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1069 C_SEG_WAKEUP_DONE(c_seg
);
1070 lck_mtx_unlock_always(&c_seg
->c_lock
);
1072 segno
= c_seg
->c_mysegno
;
1074 lck_mtx_lock_spin_always(c_list_lock
);
1076 * because the c_buffer is now associated with the segno,
1077 * we can't put the segno back on the free list until
1078 * after we have depopulated the c_buffer range, or
1079 * we run the risk of depopulating a range that is
1080 * now being used in one of the compressor heads
1082 c_segments
[segno
].c_segno
= c_free_segno_head
;
1083 c_free_segno_head
= segno
;
1086 lck_mtx_unlock_always(c_list_lock
);
1088 lck_mtx_destroy(&c_seg
->c_lock
, &vm_compressor_lck_grp
);
1090 if (c_seg
->c_slot_var_array_len
)
1091 kfree(c_seg
->c_slot_var_array
, sizeof(struct c_slot
) * c_seg
->c_slot_var_array_len
);
1093 zfree(compressor_segment_zone
, c_seg
);
1097 int c_seg_trim_page_count
= 0;
1100 c_seg_trim_tail(c_segment_t c_seg
)
1105 uint32_t c_rounded_size
;
1106 uint16_t current_nextslot
;
1107 uint32_t current_populated_offset
;
1109 if (c_seg
->c_bytes_used
== 0)
1111 current_nextslot
= c_seg
->c_nextslot
;
1112 current_populated_offset
= c_seg
->c_populated_offset
;
1114 while (c_seg
->c_nextslot
) {
1116 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, (c_seg
->c_nextslot
- 1));
1118 c_size
= UNPACK_C_SIZE(cs
);
1121 if (current_nextslot
!= c_seg
->c_nextslot
) {
1122 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1123 c_offset
= cs
->c_offset
+ C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1125 c_seg
->c_nextoffset
= c_offset
;
1126 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1128 if (c_seg
->c_firstemptyslot
> c_seg
->c_nextslot
)
1129 c_seg
->c_firstemptyslot
= c_seg
->c_nextslot
;
1131 c_seg_trim_page_count
+= ((round_page_32(C_SEG_OFFSET_TO_BYTES(current_populated_offset
)) -
1132 round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
);
1136 c_seg
->c_nextslot
--;
1138 assert(c_seg
->c_nextslot
);
1143 c_seg_minor_compaction_and_unlock(c_segment_t c_seg
, boolean_t clear_busy
)
1145 c_slot_mapping_t slot_ptr
;
1146 uint32_t c_offset
= 0;
1147 uint32_t old_populated_offset
;
1148 uint32_t c_rounded_size
;
1155 assert(c_seg
->c_busy
);
1157 #if VALIDATE_C_SEGMENTS
1158 c_seg_validate(c_seg
, FALSE
);
1160 if (c_seg
->c_bytes_used
== 0) {
1164 lck_mtx_unlock_always(&c_seg
->c_lock
);
1166 if (c_seg
->c_firstemptyslot
>= c_seg
->c_nextslot
|| C_SEG_UNUSED_BYTES(c_seg
) < PAGE_SIZE
)
1169 #if DEVELOPMENT || DEBUG
1170 C_SEG_MAKE_WRITEABLE(c_seg
);
1173 #if VALIDATE_C_SEGMENTS
1174 c_seg
->c_was_minor_compacted
++;
1176 c_indx
= c_seg
->c_firstemptyslot
;
1177 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1179 old_populated_offset
= c_seg
->c_populated_offset
;
1180 c_offset
= c_dst
->c_offset
;
1182 for (i
= c_indx
+ 1; i
< c_seg
->c_nextslot
&& c_offset
< c_seg
->c_nextoffset
; i
++) {
1184 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg
, i
);
1186 c_size
= UNPACK_C_SIZE(c_src
);
1191 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1193 memcpy(&c_seg
->c_store
.c_buffer
[c_offset
], &c_seg
->c_store
.c_buffer
[c_src
->c_offset
], c_rounded_size
);
1195 cslot_copy(c_dst
, c_src
);
1196 c_dst
->c_offset
= c_offset
;
1198 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1199 slot_ptr
->s_cindx
= c_indx
;
1201 c_offset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1202 PACK_C_SIZE(c_src
, 0);
1205 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1207 c_seg
->c_firstemptyslot
= c_indx
;
1208 c_seg
->c_nextslot
= c_indx
;
1209 c_seg
->c_nextoffset
= c_offset
;
1210 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1211 c_seg
->c_bytes_unused
= 0;
1213 #if VALIDATE_C_SEGMENTS
1214 c_seg_validate(c_seg
, TRUE
);
1216 if (old_populated_offset
> c_seg
->c_populated_offset
) {
1220 gc_size
= C_SEG_OFFSET_TO_BYTES(old_populated_offset
- c_seg
->c_populated_offset
);
1221 gc_ptr
= &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
];
1223 kernel_memory_depopulate(compressor_map
, (vm_offset_t
)gc_ptr
, gc_size
, KMA_COMPRESSOR
);
1226 #if DEVELOPMENT || DEBUG
1227 C_SEG_WRITE_PROTECT(c_seg
);
1231 if (clear_busy
== TRUE
) {
1232 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1233 C_SEG_WAKEUP_DONE(c_seg
);
1234 lck_mtx_unlock_always(&c_seg
->c_lock
);
1241 c_seg_alloc_nextslot(c_segment_t c_seg
)
1243 struct c_slot
*old_slot_array
= NULL
;
1244 struct c_slot
*new_slot_array
= NULL
;
1248 if (c_seg
->c_nextslot
< c_seg_fixed_array_len
)
1251 if ((c_seg
->c_nextslot
- c_seg_fixed_array_len
) >= c_seg
->c_slot_var_array_len
) {
1253 oldlen
= c_seg
->c_slot_var_array_len
;
1254 old_slot_array
= c_seg
->c_slot_var_array
;
1257 newlen
= C_SEG_SLOT_VAR_ARRAY_MIN_LEN
;
1259 newlen
= oldlen
* 2;
1261 new_slot_array
= (struct c_slot
*)kalloc(sizeof(struct c_slot
) * newlen
);
1263 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1266 memcpy((char *)new_slot_array
, (char *)old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1268 c_seg
->c_slot_var_array_len
= newlen
;
1269 c_seg
->c_slot_var_array
= new_slot_array
;
1271 lck_mtx_unlock_always(&c_seg
->c_lock
);
1274 kfree(old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1281 uint64_t asked_permission
;
1282 uint64_t compactions
;
1283 uint64_t moved_slots
;
1284 uint64_t moved_bytes
;
1285 uint64_t wasted_space_in_swapouts
;
1286 uint64_t count_of_swapouts
;
1287 uint64_t count_of_freed_segs
;
1288 } c_seg_major_compact_stats
;
1291 #define C_MAJOR_COMPACTION_SIZE_APPROPRIATE ((C_SEG_BUFSIZE * 90) / 100)
1295 c_seg_major_compact_ok(
1296 c_segment_t c_seg_dst
,
1297 c_segment_t c_seg_src
)
1300 c_seg_major_compact_stats
.asked_permission
++;
1302 if (c_seg_src
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
&&
1303 c_seg_dst
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
)
1306 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1308 * destination segment is full... can't compact
1318 c_seg_major_compact(
1319 c_segment_t c_seg_dst
,
1320 c_segment_t c_seg_src
)
1322 c_slot_mapping_t slot_ptr
;
1323 uint32_t c_rounded_size
;
1329 boolean_t keep_compacting
= TRUE
;
1332 * segments are not locked but they are both marked c_busy
1333 * which keeps c_decompress from working on them...
1334 * we can safely allocate new pages, move compressed data
1335 * from c_seg_src to c_seg_dst and update both c_segment's
1336 * state w/o holding the master lock
1338 #if DEVELOPMENT || DEBUG
1339 C_SEG_MAKE_WRITEABLE(c_seg_dst
);
1342 #if VALIDATE_C_SEGMENTS
1343 c_seg_dst
->c_was_major_compacted
++;
1344 c_seg_src
->c_was_major_donor
++;
1346 c_seg_major_compact_stats
.compactions
++;
1348 dst_slot
= c_seg_dst
->c_nextslot
;
1350 for (i
= 0; i
< c_seg_src
->c_nextslot
; i
++) {
1352 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, i
);
1354 c_size
= UNPACK_C_SIZE(c_src
);
1357 /* BATCH: move what we have so far; */
1361 if (C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
- c_seg_dst
->c_nextoffset
) < (unsigned) c_size
) {
1362 int size_to_populate
;
1365 size_to_populate
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
);
1367 if (size_to_populate
== 0) {
1369 keep_compacting
= FALSE
;
1372 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
1373 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
1375 kernel_memory_populate(compressor_map
,
1376 (vm_offset_t
) &c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_populated_offset
],
1379 VM_KERN_MEMORY_COMPRESSOR
);
1381 c_seg_dst
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
1382 assert(C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
) <= C_SEG_BUFSIZE
);
1384 c_seg_alloc_nextslot(c_seg_dst
);
1386 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
1388 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
);
1390 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1392 c_seg_major_compact_stats
.moved_slots
++;
1393 c_seg_major_compact_stats
.moved_bytes
+= c_size
;
1395 cslot_copy(c_dst
, c_src
);
1396 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
1398 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
1399 c_seg_dst
->c_firstemptyslot
++;
1400 c_seg_dst
->c_nextslot
++;
1401 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
1402 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1404 PACK_C_SIZE(c_src
, 0);
1406 c_seg_src
->c_bytes_used
-= c_rounded_size
;
1407 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
1408 c_seg_src
->c_firstemptyslot
= 0;
1410 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1411 /* dest segment is now full */
1412 keep_compacting
= FALSE
;
1416 #if DEVELOPMENT || DEBUG
1417 C_SEG_WRITE_PROTECT(c_seg_dst
);
1419 if (dst_slot
< c_seg_dst
->c_nextslot
) {
1421 PAGE_REPLACEMENT_ALLOWED(TRUE
);
1423 * we've now locked out c_decompress from
1424 * converting the slot passed into it into
1425 * a c_segment_t which allows us to use
1426 * the backptr to change which c_segment and
1427 * index the slot points to
1429 while (dst_slot
< c_seg_dst
->c_nextslot
) {
1431 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
1433 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1434 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
1435 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
1436 slot_ptr
->s_cindx
= dst_slot
++;
1438 PAGE_REPLACEMENT_ALLOWED(FALSE
);
1440 return (keep_compacting
);
1445 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
)
1448 uint64_t start_msecs
;
1450 end_msecs
= (end_sec
* 1000) + end_nsec
/ 1000000;
1451 start_msecs
= (start_sec
* 1000) + start_nsec
/ 1000000;
1453 return (end_msecs
- start_msecs
);
1458 uint32_t compressor_eval_period_in_msecs
= 250;
1459 uint32_t compressor_sample_min_in_msecs
= 500;
1460 uint32_t compressor_sample_max_in_msecs
= 10000;
1461 uint32_t compressor_thrashing_threshold_per_10msecs
= 50;
1462 uint32_t compressor_thrashing_min_per_10msecs
= 20;
1464 /* When true, reset sample data next chance we get. */
1465 static boolean_t compressor_need_sample_reset
= FALSE
;
1467 extern uint32_t vm_page_filecache_min
;
1471 compute_swapout_target_age(void)
1473 clock_sec_t cur_ts_sec
;
1474 clock_nsec_t cur_ts_nsec
;
1475 uint32_t min_operations_needed_in_this_sample
;
1476 uint64_t elapsed_msecs_in_eval
;
1477 uint64_t elapsed_msecs_in_sample
;
1478 boolean_t need_eval_reset
= FALSE
;
1480 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
1482 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
);
1484 if (compressor_need_sample_reset
||
1485 elapsed_msecs_in_sample
>= compressor_sample_max_in_msecs
) {
1486 compressor_need_sample_reset
= TRUE
;
1487 need_eval_reset
= TRUE
;
1490 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
);
1492 if (elapsed_msecs_in_eval
< compressor_eval_period_in_msecs
)
1494 need_eval_reset
= TRUE
;
1496 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_START
, elapsed_msecs_in_eval
, sample_period_compression_count
, sample_period_decompression_count
, 0, 0);
1498 min_operations_needed_in_this_sample
= (compressor_thrashing_min_per_10msecs
* (uint32_t)elapsed_msecs_in_eval
) / 10;
1500 if ((sample_period_compression_count
- last_eval_compression_count
) < min_operations_needed_in_this_sample
||
1501 (sample_period_decompression_count
- last_eval_decompression_count
) < min_operations_needed_in_this_sample
) {
1503 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_compression_count
- last_eval_compression_count
,
1504 sample_period_decompression_count
- last_eval_decompression_count
, 0, 1, 0);
1506 swapout_target_age
= 0;
1508 compressor_need_sample_reset
= TRUE
;
1509 need_eval_reset
= TRUE
;
1512 last_eval_compression_count
= sample_period_compression_count
;
1513 last_eval_decompression_count
= sample_period_decompression_count
;
1515 if (elapsed_msecs_in_sample
< compressor_sample_min_in_msecs
) {
1517 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, 0, 0, 5, 0);
1520 if (sample_period_decompression_count
> ((compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10)) {
1522 uint64_t running_total
;
1523 uint64_t working_target
;
1524 uint64_t aging_target
;
1525 uint32_t oldest_age_of_csegs_sampled
= 0;
1526 uint64_t working_set_approximation
= 0;
1528 swapout_target_age
= 0;
1530 working_target
= (sample_period_decompression_count
/ 100) * 95; /* 95 percent */
1531 aging_target
= (sample_period_decompression_count
/ 100) * 1; /* 1 percent */
1534 for (oldest_age_of_csegs_sampled
= 0; oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
; oldest_age_of_csegs_sampled
++) {
1536 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1538 working_set_approximation
+= oldest_age_of_csegs_sampled
* age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1540 if (running_total
>= working_target
)
1543 if (oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
) {
1545 working_set_approximation
= (working_set_approximation
* 1000) / elapsed_msecs_in_sample
;
1547 if (working_set_approximation
< VM_PAGE_COMPRESSOR_COUNT
) {
1549 running_total
= overage_decompressions_during_sample_period
;
1551 for (oldest_age_of_csegs_sampled
= DECOMPRESSION_SAMPLE_MAX_AGE
- 1; oldest_age_of_csegs_sampled
; oldest_age_of_csegs_sampled
--) {
1552 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1554 if (running_total
>= aging_target
)
1557 swapout_target_age
= (uint32_t)cur_ts_sec
- oldest_age_of_csegs_sampled
;
1559 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 2, 0);
1561 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 0, 3, 0);
1564 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_target
, running_total
, 0, 4, 0);
1566 compressor_need_sample_reset
= TRUE
;
1567 need_eval_reset
= TRUE
;
1569 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_decompression_count
, (compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10, 0, 6, 0);
1571 if (compressor_need_sample_reset
== TRUE
) {
1572 bzero(age_of_decompressions_during_sample_period
, sizeof(age_of_decompressions_during_sample_period
));
1573 overage_decompressions_during_sample_period
= 0;
1575 start_of_sample_period_sec
= cur_ts_sec
;
1576 start_of_sample_period_nsec
= cur_ts_nsec
;
1577 sample_period_decompression_count
= 0;
1578 sample_period_compression_count
= 0;
1579 last_eval_decompression_count
= 0;
1580 last_eval_compression_count
= 0;
1581 compressor_need_sample_reset
= FALSE
;
1583 if (need_eval_reset
== TRUE
) {
1584 start_of_eval_period_sec
= cur_ts_sec
;
1585 start_of_eval_period_nsec
= cur_ts_nsec
;
1590 int compaction_swapper_init_now
= 0;
1591 int compaction_swapper_running
= 0;
1592 int compaction_swapper_awakened
= 0;
1593 int compaction_swapper_abort
= 0;
1597 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
1598 boolean_t
memorystatus_kill_on_FC_thrashing(boolean_t
);
1599 int compressor_thrashing_induced_jetsam
= 0;
1600 int filecache_thrashing_induced_jetsam
= 0;
1601 static boolean_t vm_compressor_thrashing_detected
= FALSE
;
1602 #endif /* CONFIG_JETSAM */
1605 compressor_needs_to_swap(void)
1607 boolean_t should_swap
= FALSE
;
1609 if (vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
) {
1615 clock_get_system_nanotime(&now
, &nsec
);
1618 lck_mtx_lock_spin_always(c_list_lock
);
1620 if ( !queue_empty(&c_age_list_head
)) {
1621 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1623 age
= now
- c_seg
->c_creation_ts
;
1625 lck_mtx_unlock_always(c_list_lock
);
1627 if (age
>= vm_ripe_target_age
)
1630 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
1631 if (COMPRESSOR_NEEDS_TO_SWAP()) {
1634 if (VM_PAGE_Q_THROTTLED(&vm_pageout_queue_external
) && vm_page_anonymous_count
< (vm_page_inactive_count
/ 20)) {
1637 if (vm_page_free_count
< (vm_page_free_reserved
- (COMPRESSOR_FREE_RESERVED_LIMIT
* 2)))
1640 compute_swapout_target_age();
1642 if (swapout_target_age
) {
1645 lck_mtx_lock_spin_always(c_list_lock
);
1647 if (!queue_empty(&c_age_list_head
)) {
1649 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1651 if (c_seg
->c_creation_ts
> swapout_target_age
)
1652 swapout_target_age
= 0;
1654 lck_mtx_unlock_always(c_list_lock
);
1656 #if CONFIG_PHANTOM_CACHE
1657 if (vm_phantom_cache_check_pressure())
1660 if (swapout_target_age
)
1664 if (should_swap
|| c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) {
1666 if (vm_compressor_thrashing_detected
== FALSE
) {
1667 vm_compressor_thrashing_detected
= TRUE
;
1669 if (swapout_target_age
|| c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) {
1670 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
1671 compressor_thrashing_induced_jetsam
++;
1673 memorystatus_kill_on_FC_thrashing(TRUE
/* async */);
1674 filecache_thrashing_induced_jetsam
++;
1678 * let the jetsam take precedence over
1679 * any major compactions we might have
1680 * been able to do... otherwise we run
1681 * the risk of doing major compactions
1682 * on segments we're about to free up
1683 * due to the jetsam activity.
1685 should_swap
= FALSE
;
1688 #endif /* CONFIG_JETSAM */
1690 if (should_swap
== FALSE
) {
1692 * vm_compressor_needs_to_major_compact returns true only if we're
1693 * about to run out of available compressor segments... in this
1694 * case, we absolutely need to run a major compaction even if
1695 * we've just kicked off a jetsam or we don't otherwise need to
1696 * swap... terminating objects releases
1697 * pages back to the uncompressed cache, but does not guarantee
1698 * that we will free up even a single compression segment
1700 should_swap
= vm_compressor_needs_to_major_compact();
1704 * returning TRUE when swap_supported == FALSE
1705 * will cause the major compaction engine to
1706 * run, but will not trigger any swapping...
1707 * segments that have been major compacted
1708 * will be moved to the majorcompact queue
1710 return (should_swap
);
1715 * This function is called from the jetsam thread after killing something to
1716 * mitigate thrashing.
1718 * We need to restart our thrashing detection heuristics since memory pressure
1719 * has potentially changed significantly, and we don't want to detect on old
1720 * data from before the jetsam.
1723 vm_thrashing_jetsam_done(void)
1725 vm_compressor_thrashing_detected
= FALSE
;
1727 /* Were we compressor-thrashing or filecache-thrashing? */
1728 if (swapout_target_age
) {
1729 swapout_target_age
= 0;
1730 compressor_need_sample_reset
= TRUE
;
1732 #if CONFIG_PHANTOM_CACHE
1734 vm_phantom_cache_restart_sample();
1738 #endif /* CONFIG_JETSAM */
1740 uint32_t vm_wake_compactor_swapper_calls
= 0;
1741 uint32_t vm_run_compactor_already_running
= 0;
1742 uint32_t vm_run_compactor_empty_minor_q
= 0;
1743 uint32_t vm_run_compactor_did_compact
= 0;
1744 uint32_t vm_run_compactor_waited
= 0;
1747 vm_run_compactor(void)
1749 if (c_segment_count
== 0)
1752 lck_mtx_lock_spin_always(c_list_lock
);
1754 if (c_minor_count
== 0) {
1755 vm_run_compactor_empty_minor_q
++;
1757 lck_mtx_unlock_always(c_list_lock
);
1760 if (compaction_swapper_running
) {
1762 if (vm_restricted_to_single_processor
== FALSE
) {
1763 vm_run_compactor_already_running
++;
1765 lck_mtx_unlock_always(c_list_lock
);
1768 vm_run_compactor_waited
++;
1770 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1772 lck_mtx_unlock_always(c_list_lock
);
1774 thread_block(THREAD_CONTINUE_NULL
);
1778 vm_run_compactor_did_compact
++;
1780 fastwake_warmup
= FALSE
;
1781 compaction_swapper_running
= 1;
1783 vm_compressor_do_delayed_compactions(FALSE
);
1785 compaction_swapper_running
= 0;
1787 lck_mtx_unlock_always(c_list_lock
);
1789 thread_wakeup((event_t
)&compaction_swapper_running
);
1794 vm_wake_compactor_swapper(void)
1796 if (compaction_swapper_running
|| compaction_swapper_awakened
|| c_segment_count
== 0)
1799 if (c_minor_count
|| vm_compressor_needs_to_major_compact()) {
1801 lck_mtx_lock_spin_always(c_list_lock
);
1803 fastwake_warmup
= FALSE
;
1805 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
1807 vm_wake_compactor_swapper_calls
++;
1809 compaction_swapper_awakened
= 1;
1810 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
1812 lck_mtx_unlock_always(c_list_lock
);
1818 vm_consider_swapping()
1820 c_segment_t c_seg
, c_seg_next
;
1824 assert(VM_CONFIG_SWAP_IS_PRESENT
);
1826 lck_mtx_lock_spin_always(c_list_lock
);
1828 compaction_swapper_abort
= 1;
1830 while (compaction_swapper_running
) {
1831 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1833 lck_mtx_unlock_always(c_list_lock
);
1835 thread_block(THREAD_CONTINUE_NULL
);
1837 lck_mtx_lock_spin_always(c_list_lock
);
1839 compaction_swapper_abort
= 0;
1840 compaction_swapper_running
= 1;
1842 vm_swapout_ripe_segments
= TRUE
;
1844 if (!queue_empty(&c_major_list_head
)) {
1846 clock_get_system_nanotime(&now
, &nsec
);
1848 c_seg
= (c_segment_t
)queue_first(&c_major_list_head
);
1850 while (!queue_end(&c_major_list_head
, (queue_entry_t
)c_seg
)) {
1852 if (c_overage_swapped_count
>= c_overage_swapped_limit
)
1855 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
1857 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
1859 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1861 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
1863 lck_mtx_unlock_always(&c_seg
->c_lock
);
1868 vm_compressor_compact_and_swap(FALSE
);
1870 compaction_swapper_running
= 0;
1872 vm_swapout_ripe_segments
= FALSE
;
1874 lck_mtx_unlock_always(c_list_lock
);
1876 thread_wakeup((event_t
)&compaction_swapper_running
);
1881 vm_consider_waking_compactor_swapper(void)
1883 boolean_t need_wakeup
= FALSE
;
1885 if (c_segment_count
== 0)
1888 if (compaction_swapper_running
|| compaction_swapper_awakened
)
1891 if (!compaction_swapper_inited
&& !compaction_swapper_init_now
) {
1892 compaction_swapper_init_now
= 1;
1896 if (c_minor_count
&& (COMPRESSOR_NEEDS_TO_MINOR_COMPACT())) {
1900 } else if (compressor_needs_to_swap()) {
1904 } else if (c_minor_count
) {
1905 uint64_t total_bytes
;
1907 total_bytes
= compressor_object
->resident_page_count
* PAGE_SIZE_64
;
1909 if ((total_bytes
- compressor_bytes_used
) > total_bytes
/ 10)
1912 if (need_wakeup
== TRUE
) {
1914 lck_mtx_lock_spin_always(c_list_lock
);
1916 fastwake_warmup
= FALSE
;
1918 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
1919 memoryshot(VM_WAKEUP_COMPACTOR_SWAPPER
, DBG_FUNC_NONE
);
1921 compaction_swapper_awakened
= 1;
1922 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
1924 lck_mtx_unlock_always(c_list_lock
);
1929 #define C_SWAPOUT_LIMIT 4
1930 #define DELAYED_COMPACTIONS_PER_PASS 30
1933 vm_compressor_do_delayed_compactions(boolean_t flush_all
)
1936 int number_compacted
= 0;
1937 boolean_t needs_to_swap
= FALSE
;
1940 LCK_MTX_ASSERT(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
1942 while (!queue_empty(&c_minor_list_head
) && needs_to_swap
== FALSE
) {
1944 c_seg
= (c_segment_t
)queue_first(&c_minor_list_head
);
1946 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1948 if (c_seg
->c_busy
) {
1950 lck_mtx_unlock_always(c_list_lock
);
1951 c_seg_wait_on_busy(c_seg
);
1952 lck_mtx_lock_spin_always(c_list_lock
);
1958 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, TRUE
);
1960 if (VM_CONFIG_SWAP_IS_ACTIVE
&& (number_compacted
++ > DELAYED_COMPACTIONS_PER_PASS
)) {
1962 if ((flush_all
== TRUE
|| compressor_needs_to_swap() == TRUE
) && c_swapout_count
< C_SWAPOUT_LIMIT
)
1963 needs_to_swap
= TRUE
;
1965 number_compacted
= 0;
1967 lck_mtx_lock_spin_always(c_list_lock
);
1972 #define C_SEGMENT_SWAPPEDIN_AGE_LIMIT 10
1975 vm_compressor_age_swapped_in_segments(boolean_t flush_all
)
1981 clock_get_system_nanotime(&now
, &nsec
);
1983 while (!queue_empty(&c_swappedin_list_head
)) {
1985 c_seg
= (c_segment_t
)queue_first(&c_swappedin_list_head
);
1987 if (flush_all
== FALSE
&& (now
- c_seg
->c_swappedin_ts
) < C_SEGMENT_SWAPPEDIN_AGE_LIMIT
)
1990 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1992 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
1994 lck_mtx_unlock_always(&c_seg
->c_lock
);
1999 extern int vm_num_swap_files
;
2000 extern int vm_num_pinned_swap_files
;
2001 extern int vm_swappin_enabled
;
2003 extern unsigned int vm_swapfile_total_segs_used
;
2004 extern unsigned int vm_swapfile_total_segs_alloced
;
2008 vm_compressor_flush(void)
2010 uint64_t vm_swap_put_failures_at_start
;
2011 wait_result_t wait_result
= 0;
2012 AbsoluteTime startTime
, endTime
;
2013 clock_sec_t now_sec
;
2014 clock_nsec_t now_nsec
;
2017 HIBLOG("vm_compressor_flush - starting\n");
2019 clock_get_uptime(&startTime
);
2021 lck_mtx_lock_spin_always(c_list_lock
);
2023 fastwake_warmup
= FALSE
;
2024 compaction_swapper_abort
= 1;
2026 while (compaction_swapper_running
) {
2027 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
2029 lck_mtx_unlock_always(c_list_lock
);
2031 thread_block(THREAD_CONTINUE_NULL
);
2033 lck_mtx_lock_spin_always(c_list_lock
);
2035 compaction_swapper_abort
= 0;
2036 compaction_swapper_running
= 1;
2038 hibernate_flushing
= TRUE
;
2039 hibernate_no_swapspace
= FALSE
;
2040 c_generation_id_flush_barrier
= c_generation_id
+ 1000;
2042 clock_get_system_nanotime(&now_sec
, &now_nsec
);
2043 hibernate_flushing_deadline
= now_sec
+ HIBERNATE_FLUSHING_SECS_TO_COMPLETE
;
2045 vm_swap_put_failures_at_start
= vm_swap_put_failures
;
2047 vm_compressor_compact_and_swap(TRUE
);
2049 while (!queue_empty(&c_swapout_list_head
)) {
2051 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 5000, 1000*NSEC_PER_USEC
);
2053 lck_mtx_unlock_always(c_list_lock
);
2055 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
2057 lck_mtx_lock_spin_always(c_list_lock
);
2059 if (wait_result
== THREAD_TIMED_OUT
)
2062 hibernate_flushing
= FALSE
;
2063 compaction_swapper_running
= 0;
2065 if (vm_swap_put_failures
> vm_swap_put_failures_at_start
)
2066 HIBLOG("vm_compressor_flush failed to clean %llu segments - vm_page_compressor_count(%d)\n",
2067 vm_swap_put_failures
- vm_swap_put_failures_at_start
, VM_PAGE_COMPRESSOR_COUNT
);
2069 lck_mtx_unlock_always(c_list_lock
);
2071 thread_wakeup((event_t
)&compaction_swapper_running
);
2073 clock_get_uptime(&endTime
);
2074 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2075 absolutetime_to_nanoseconds(endTime
, &nsec
);
2077 HIBLOG("vm_compressor_flush completed - took %qd msecs - vm_num_swap_files = %d, vm_num_pinned_swap_files = %d, vm_swappin_enabled = %d\n",
2078 nsec
/ 1000000ULL, vm_num_swap_files
, vm_num_pinned_swap_files
, vm_swappin_enabled
);
2082 int compaction_swap_trigger_thread_awakened
= 0;
2085 vm_compressor_swap_trigger_thread(void)
2087 current_thread()->options
|= TH_OPT_VMPRIV
;
2090 * compaction_swapper_init_now is set when the first call to
2091 * vm_consider_waking_compactor_swapper is made from
2092 * vm_pageout_scan... since this function is called upon
2093 * thread creation, we want to make sure to delay adjusting
2094 * the tuneables until we are awakened via vm_pageout_scan
2095 * so that we are at a point where the vm_swapfile_open will
2096 * be operating on the correct directory (in case the default
2097 * of /var/vm/ is overridden by the dymanic_pager
2099 if (compaction_swapper_init_now
) {
2100 vm_compaction_swapper_do_init();
2102 if (vm_restricted_to_single_processor
== TRUE
)
2103 thread_vm_bind_group_add();
2105 compaction_swapper_init_now
= 0;
2107 lck_mtx_lock_spin_always(c_list_lock
);
2109 compaction_swap_trigger_thread_awakened
++;
2110 compaction_swapper_awakened
= 0;
2112 if (compaction_swapper_running
== 0) {
2114 compaction_swapper_running
= 1;
2116 vm_compressor_compact_and_swap(FALSE
);
2118 compaction_swapper_running
= 0;
2120 assert_wait((event_t
)&c_compressor_swap_trigger
, THREAD_UNINT
);
2122 if (compaction_swapper_running
== 0)
2123 thread_wakeup((event_t
)&compaction_swapper_running
);
2125 lck_mtx_unlock_always(c_list_lock
);
2127 thread_block((thread_continue_t
)vm_compressor_swap_trigger_thread
);
2134 vm_compressor_record_warmup_start(void)
2138 lck_mtx_lock_spin_always(c_list_lock
);
2140 if (first_c_segment_to_warm_generation_id
== 0) {
2141 if (!queue_empty(&c_age_list_head
)) {
2143 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2145 first_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2147 first_c_segment_to_warm_generation_id
= 0;
2149 fastwake_recording_in_progress
= TRUE
;
2151 lck_mtx_unlock_always(c_list_lock
);
2156 vm_compressor_record_warmup_end(void)
2160 lck_mtx_lock_spin_always(c_list_lock
);
2162 if (fastwake_recording_in_progress
== TRUE
) {
2164 if (!queue_empty(&c_age_list_head
)) {
2166 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2168 last_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2170 last_c_segment_to_warm_generation_id
= first_c_segment_to_warm_generation_id
;
2172 fastwake_recording_in_progress
= FALSE
;
2174 HIBLOG("vm_compressor_record_warmup (%qd - %qd)\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2176 lck_mtx_unlock_always(c_list_lock
);
2180 #define DELAY_TRIM_ON_WAKE_SECS 25
2183 vm_compressor_delay_trim(void)
2188 clock_get_system_nanotime(&sec
, &nsec
);
2189 dont_trim_until_ts
= sec
+ DELAY_TRIM_ON_WAKE_SECS
;
2194 vm_compressor_do_warmup(void)
2196 lck_mtx_lock_spin_always(c_list_lock
);
2198 if (first_c_segment_to_warm_generation_id
== last_c_segment_to_warm_generation_id
) {
2199 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2201 lck_mtx_unlock_always(c_list_lock
);
2205 if (compaction_swapper_running
== 0 && compaction_swapper_awakened
== 0) {
2207 fastwake_warmup
= TRUE
;
2209 compaction_swapper_awakened
= 1;
2210 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2212 lck_mtx_unlock_always(c_list_lock
);
2217 do_fastwake_warmup(void)
2219 c_segment_t c_seg
= NULL
;
2220 AbsoluteTime startTime
, endTime
;
2224 HIBLOG("vm_compressor_fastwake_warmup (%qd - %qd) - starting\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2226 clock_get_uptime(&startTime
);
2228 lck_mtx_unlock_always(c_list_lock
);
2230 proc_set_thread_policy(current_thread(),
2231 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER2
);
2233 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2235 lck_mtx_lock_spin_always(c_list_lock
);
2237 while (!queue_empty(&c_swappedout_list_head
) && fastwake_warmup
== TRUE
) {
2239 c_seg
= (c_segment_t
) queue_first(&c_swappedout_list_head
);
2241 if (c_seg
->c_generation_id
< first_c_segment_to_warm_generation_id
||
2242 c_seg
->c_generation_id
> last_c_segment_to_warm_generation_id
)
2245 if (vm_page_free_count
< (AVAILABLE_MEMORY
/ 4))
2248 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2249 lck_mtx_unlock_always(c_list_lock
);
2251 if (c_seg
->c_busy
) {
2252 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2253 c_seg_wait_on_busy(c_seg
);
2254 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2256 if (c_seg_swapin(c_seg
, TRUE
, FALSE
) == 0)
2257 lck_mtx_unlock_always(&c_seg
->c_lock
);
2258 c_segment_warmup_count
++;
2260 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2261 vm_pageout_io_throttle();
2262 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2264 lck_mtx_lock_spin_always(c_list_lock
);
2266 lck_mtx_unlock_always(c_list_lock
);
2268 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2270 proc_set_thread_policy(current_thread(),
2271 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER0
);
2273 clock_get_uptime(&endTime
);
2274 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2275 absolutetime_to_nanoseconds(endTime
, &nsec
);
2277 HIBLOG("vm_compressor_fastwake_warmup completed - took %qd msecs\n", nsec
/ 1000000ULL);
2279 lck_mtx_lock_spin_always(c_list_lock
);
2281 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2286 vm_compressor_compact_and_swap(boolean_t flush_all
)
2288 c_segment_t c_seg
, c_seg_next
;
2289 boolean_t keep_compacting
;
2294 if (fastwake_warmup
== TRUE
) {
2295 uint64_t starting_warmup_count
;
2297 starting_warmup_count
= c_segment_warmup_count
;
2299 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 11) | DBG_FUNC_START
, c_segment_warmup_count
,
2300 first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
, 0, 0);
2301 do_fastwake_warmup();
2302 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);
2304 fastwake_warmup
= FALSE
;
2308 * it's possible for the c_age_list_head to be empty if we
2309 * hit our limits for growing the compressor pool and we subsequently
2310 * hibernated... on the next hibernation we could see the queue as
2311 * empty and not proceeed even though we have a bunch of segments on
2312 * the swapped in queue that need to be dealt with.
2314 vm_compressor_do_delayed_compactions(flush_all
);
2316 vm_compressor_age_swapped_in_segments(flush_all
);
2319 * we only need to grab the timestamp once per
2320 * invocation of this function since the
2321 * timescale we're interested in is measured
2324 clock_get_system_nanotime(&now
, &nsec
);
2326 while (!queue_empty(&c_age_list_head
) && compaction_swapper_abort
== 0) {
2328 if (hibernate_flushing
== TRUE
) {
2331 if (hibernate_should_abort()) {
2332 HIBLOG("vm_compressor_flush - hibernate_should_abort returned TRUE\n");
2335 if (hibernate_no_swapspace
== TRUE
) {
2336 HIBLOG("vm_compressor_flush - out of swap space\n");
2339 if (vm_swap_files_pinned() == FALSE
) {
2340 HIBLOG("vm_compressor_flush - unpinned swap files\n");
2343 if (hibernate_in_progress_with_pinned_swap
== TRUE
&&
2344 (vm_swapfile_total_segs_alloced
== vm_swapfile_total_segs_used
)) {
2345 HIBLOG("vm_compressor_flush - out of pinned swap space\n");
2348 clock_get_system_nanotime(&sec
, &nsec
);
2350 if (sec
> hibernate_flushing_deadline
) {
2351 HIBLOG("vm_compressor_flush - failed to finish before deadline\n");
2355 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2357 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 100, 1000*NSEC_PER_USEC
);
2359 lck_mtx_unlock_always(c_list_lock
);
2361 thread_block(THREAD_CONTINUE_NULL
);
2363 lck_mtx_lock_spin_always(c_list_lock
);
2368 vm_compressor_do_delayed_compactions(flush_all
);
2370 vm_compressor_age_swapped_in_segments(flush_all
);
2372 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2374 * we timed out on the above thread_block
2375 * let's loop around and try again
2376 * the timeout allows us to continue
2377 * to do minor compactions to make
2378 * more memory available
2384 * Swap out segments?
2386 if (flush_all
== FALSE
) {
2387 boolean_t needs_to_swap
;
2389 lck_mtx_unlock_always(c_list_lock
);
2391 needs_to_swap
= compressor_needs_to_swap();
2393 if (needs_to_swap
== TRUE
&& vm_swap_low_on_space())
2394 vm_compressor_take_paging_space_action();
2396 lck_mtx_lock_spin_always(c_list_lock
);
2398 if (needs_to_swap
== FALSE
)
2401 if (queue_empty(&c_age_list_head
))
2403 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
2405 assert(c_seg
->c_state
== C_ON_AGE_Q
);
2407 if (flush_all
== TRUE
&& c_seg
->c_generation_id
> c_generation_id_flush_barrier
)
2410 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2412 if (c_seg
->c_busy
) {
2414 lck_mtx_unlock_always(c_list_lock
);
2415 c_seg_wait_on_busy(c_seg
);
2416 lck_mtx_lock_spin_always(c_list_lock
);
2422 if (c_seg_do_minor_compaction_and_unlock(c_seg
, FALSE
, TRUE
, TRUE
)) {
2424 * found an empty c_segment and freed it
2425 * so go grab the next guy in the queue
2427 c_seg_major_compact_stats
.count_of_freed_segs
++;
2433 keep_compacting
= TRUE
;
2435 while (keep_compacting
== TRUE
) {
2437 assert(c_seg
->c_busy
);
2439 /* look for another segment to consolidate */
2441 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2443 if (queue_end(&c_age_list_head
, (queue_entry_t
)c_seg_next
))
2446 assert(c_seg_next
->c_state
== C_ON_AGE_Q
);
2448 if (c_seg_major_compact_ok(c_seg
, c_seg_next
) == FALSE
)
2451 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2453 if (c_seg_next
->c_busy
) {
2455 lck_mtx_unlock_always(c_list_lock
);
2456 c_seg_wait_on_busy(c_seg_next
);
2457 lck_mtx_lock_spin_always(c_list_lock
);
2461 /* grab that segment */
2462 C_SEG_BUSY(c_seg_next
);
2464 if (c_seg_do_minor_compaction_and_unlock(c_seg_next
, FALSE
, TRUE
, TRUE
)) {
2466 * found an empty c_segment and freed it
2467 * so we can't continue to use c_seg_next
2469 c_seg_major_compact_stats
.count_of_freed_segs
++;
2473 /* unlock the list ... */
2474 lck_mtx_unlock_always(c_list_lock
);
2476 /* do the major compaction */
2478 keep_compacting
= c_seg_major_compact(c_seg
, c_seg_next
);
2480 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2482 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2484 * run a minor compaction on the donor segment
2485 * since we pulled at least some of it's
2486 * data into our target... if we've emptied
2487 * it, now is a good time to free it which
2488 * c_seg_minor_compaction_and_unlock also takes care of
2490 * by passing TRUE, we ask for c_busy to be cleared
2491 * and c_wanted to be taken care of
2493 if (c_seg_minor_compaction_and_unlock(c_seg_next
, TRUE
))
2494 c_seg_major_compact_stats
.count_of_freed_segs
++;
2496 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2498 /* relock the list */
2499 lck_mtx_lock_spin_always(c_list_lock
);
2501 } /* major compaction */
2503 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2505 assert(c_seg
->c_busy
);
2506 assert(!c_seg
->c_on_minorcompact_q
);
2508 if (VM_CONFIG_SWAP_IS_ACTIVE
) {
2510 * This mode of putting a generic c_seg on the swapout list is
2511 * only supported when we have general swapping enabled
2513 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2515 if ((vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
)) {
2517 assert(VM_CONFIG_SWAP_IS_PRESENT
);
2519 * we are running compressor sweeps with swap-behind
2520 * make sure the c_seg has aged enough before swapping it
2523 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2524 c_seg
->c_overage_swap
= TRUE
;
2525 c_overage_swapped_count
++;
2526 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2530 if (c_seg
->c_state
== C_ON_AGE_Q
) {
2532 * this c_seg didn't get moved to the swapout queue
2533 * so we need to move it out of the way...
2534 * we just did a major compaction on it so put it
2537 c_seg_switch_state(c_seg
, C_ON_MAJORCOMPACT_Q
, FALSE
);
2539 c_seg_major_compact_stats
.wasted_space_in_swapouts
+= C_SEG_BUFSIZE
- c_seg
->c_bytes_used
;
2540 c_seg_major_compact_stats
.count_of_swapouts
++;
2542 C_SEG_WAKEUP_DONE(c_seg
);
2544 lck_mtx_unlock_always(&c_seg
->c_lock
);
2546 if (c_swapout_count
) {
2547 lck_mtx_unlock_always(c_list_lock
);
2549 thread_wakeup((event_t
)&c_swapout_list_head
);
2551 lck_mtx_lock_spin_always(c_list_lock
);
2558 c_seg_allocate(c_segment_t
*current_chead
)
2562 int size_to_populate
;
2564 if (vm_compressor_low_on_space())
2565 vm_compressor_take_paging_space_action();
2567 if ( (c_seg
= *current_chead
) == NULL
) {
2570 lck_mtx_lock_spin_always(c_list_lock
);
2572 while (c_segments_busy
== TRUE
) {
2573 assert_wait((event_t
) (&c_segments_busy
), THREAD_UNINT
);
2575 lck_mtx_unlock_always(c_list_lock
);
2577 thread_block(THREAD_CONTINUE_NULL
);
2579 lck_mtx_lock_spin_always(c_list_lock
);
2581 if (c_free_segno_head
== (uint32_t)-1) {
2582 uint32_t c_segments_available_new
;
2584 if (c_segments_available
>= c_segments_limit
|| c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) {
2585 lck_mtx_unlock_always(c_list_lock
);
2589 c_segments_busy
= TRUE
;
2590 lck_mtx_unlock_always(c_list_lock
);
2592 kernel_memory_populate(compressor_map
, (vm_offset_t
)c_segments_next_page
,
2593 PAGE_SIZE
, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
);
2594 c_segments_next_page
+= PAGE_SIZE
;
2596 c_segments_available_new
= c_segments_available
+ C_SEGMENTS_PER_PAGE
;
2598 if (c_segments_available_new
> c_segments_limit
)
2599 c_segments_available_new
= c_segments_limit
;
2601 for (c_segno
= c_segments_available
+ 1; c_segno
< c_segments_available_new
; c_segno
++)
2602 c_segments
[c_segno
- 1].c_segno
= c_segno
;
2604 lck_mtx_lock_spin_always(c_list_lock
);
2606 c_segments
[c_segno
- 1].c_segno
= c_free_segno_head
;
2607 c_free_segno_head
= c_segments_available
;
2608 c_segments_available
= c_segments_available_new
;
2610 c_segments_busy
= FALSE
;
2611 thread_wakeup((event_t
) (&c_segments_busy
));
2613 c_segno
= c_free_segno_head
;
2614 assert(c_segno
>= 0 && c_segno
< c_segments_limit
);
2616 c_free_segno_head
= (uint32_t)c_segments
[c_segno
].c_segno
;
2619 * do the rest of the bookkeeping now while we're still behind
2620 * the list lock and grab our generation id now into a local
2621 * so that we can install it once we have the c_seg allocated
2624 if (c_segment_count
> c_segment_count_max
)
2625 c_segment_count_max
= c_segment_count
;
2627 lck_mtx_unlock_always(c_list_lock
);
2629 c_seg
= (c_segment_t
)zalloc(compressor_segment_zone
);
2630 bzero((char *)c_seg
, sizeof(struct c_segment
));
2632 c_seg
->c_store
.c_buffer
= (int32_t *)C_SEG_BUFFER_ADDRESS(c_segno
);
2634 lck_mtx_init(&c_seg
->c_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
2636 c_seg
->c_state
= C_IS_EMPTY
;
2637 c_seg
->c_firstemptyslot
= C_SLOT_MAX_INDEX
;
2638 c_seg
->c_mysegno
= c_segno
;
2640 lck_mtx_lock_spin_always(c_list_lock
);
2642 c_seg_switch_state(c_seg
, C_IS_FILLING
, FALSE
);
2643 c_segments
[c_segno
].c_seg
= c_seg
;
2644 assert(c_segments
[c_segno
].c_segno
> c_segments_available
);
2645 lck_mtx_unlock_always(c_list_lock
);
2647 *current_chead
= c_seg
;
2649 #if DEVELOPMENT || DEBUG
2650 C_SEG_MAKE_WRITEABLE(c_seg
);
2654 c_seg_alloc_nextslot(c_seg
);
2656 size_to_populate
= C_SEG_ALLOCSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
);
2658 if (size_to_populate
) {
2660 min_needed
= PAGE_SIZE
+ (C_SEG_ALLOCSIZE
- C_SEG_BUFSIZE
);
2662 if (C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
) < (unsigned) min_needed
) {
2664 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
2665 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
2667 kernel_memory_populate(compressor_map
,
2668 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
],
2671 VM_KERN_MEMORY_COMPRESSOR
);
2673 size_to_populate
= 0;
2675 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2677 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2679 if (size_to_populate
)
2680 c_seg
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
2687 c_current_seg_filled(c_segment_t c_seg
, c_segment_t
*current_chead
)
2689 uint32_t unused_bytes
;
2690 uint32_t offset_to_depopulate
;
2691 int new_state
= C_ON_AGE_Q
;
2695 unused_bytes
= trunc_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
));
2699 offset_to_depopulate
= C_SEG_BYTES_TO_OFFSET(round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_nextoffset
)));
2702 * release the extra physical page(s) at the end of the segment
2704 lck_mtx_unlock_always(&c_seg
->c_lock
);
2706 kernel_memory_depopulate(
2708 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[offset_to_depopulate
],
2712 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2714 c_seg
->c_populated_offset
= offset_to_depopulate
;
2716 assert(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
) <= C_SEG_BUFSIZE
);
2718 #if DEVELOPMENT || DEBUG
2720 boolean_t c_seg_was_busy
= FALSE
;
2722 if ( !c_seg
->c_busy
)
2725 c_seg_was_busy
= TRUE
;
2727 lck_mtx_unlock_always(&c_seg
->c_lock
);
2729 C_SEG_WRITE_PROTECT(c_seg
);
2731 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2733 if (c_seg_was_busy
== FALSE
)
2734 C_SEG_WAKEUP_DONE(c_seg
);
2739 if (current_chead
== (c_segment_t
*)&freezer_chead
&&
2740 VM_CONFIG_SWAP_IS_PRESENT
&&
2741 VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
&&
2742 c_freezer_swapout_count
< VM_MAX_FREEZER_CSEG_SWAP_COUNT
) {
2743 new_state
= C_ON_SWAPOUT_Q
;
2745 #endif /* CONFIG_FREEZE */
2747 clock_get_system_nanotime(&sec
, &nsec
);
2748 c_seg
->c_creation_ts
= (uint32_t)sec
;
2750 lck_mtx_lock_spin_always(c_list_lock
);
2753 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
2754 c_freezer_swapout_count
++;
2755 #endif /* CONFIG_FREEZE */
2757 c_seg
->c_generation_id
= c_generation_id
++;
2758 c_seg_switch_state(c_seg
, new_state
, FALSE
);
2760 if (c_seg
->c_state
== C_ON_AGE_Q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
2761 c_seg_need_delayed_compaction(c_seg
, TRUE
);
2763 lck_mtx_unlock_always(c_list_lock
);
2766 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
2767 thread_wakeup((event_t
)&c_swapout_list_head
);
2768 #endif /* CONFIG_FREEZE */
2770 *current_chead
= NULL
;
2775 * returns with c_seg locked
2778 c_seg_swapin_requeue(c_segment_t c_seg
, boolean_t has_data
, boolean_t minor_compact_ok
, boolean_t age_on_swapin_q
)
2783 clock_get_system_nanotime(&sec
, &nsec
);
2785 lck_mtx_lock_spin_always(c_list_lock
);
2786 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2788 assert(c_seg
->c_busy_swapping
);
2789 assert(c_seg
->c_busy
);
2791 c_seg
->c_busy_swapping
= 0;
2793 if (c_seg
->c_overage_swap
== TRUE
) {
2794 c_overage_swapped_count
--;
2795 c_seg
->c_overage_swap
= FALSE
;
2797 if (has_data
== TRUE
) {
2798 if (age_on_swapin_q
== TRUE
)
2799 c_seg_switch_state(c_seg
, C_ON_SWAPPEDIN_Q
, FALSE
);
2801 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
2803 if (minor_compact_ok
== TRUE
&& !c_seg
->c_on_minorcompact_q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
2804 c_seg_need_delayed_compaction(c_seg
, TRUE
);
2806 c_seg
->c_store
.c_buffer
= (int32_t*) NULL
;
2807 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
2809 c_seg_switch_state(c_seg
, C_ON_BAD_Q
, FALSE
);
2811 c_seg
->c_swappedin_ts
= (uint32_t)sec
;
2813 lck_mtx_unlock_always(c_list_lock
);
2819 * c_seg has to be locked and is returned locked if the c_seg isn't freed
2820 * PAGE_REPLACMENT_DISALLOWED has to be TRUE on entry and is returned TRUE
2821 * c_seg_swapin returns 1 if the c_seg was freed, 0 otherwise
2825 c_seg_swapin(c_segment_t c_seg
, boolean_t force_minor_compaction
, boolean_t age_on_swapin_q
)
2827 vm_offset_t addr
= 0;
2828 uint32_t io_size
= 0;
2831 assert(C_SEG_IS_ONDISK(c_seg
));
2833 #if !CHECKSUM_THE_SWAP
2834 c_seg_trim_tail(c_seg
);
2836 io_size
= round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
));
2837 f_offset
= c_seg
->c_store
.c_swap_handle
;
2840 c_seg
->c_busy_swapping
= 1;
2843 * This thread is likely going to block for I/O.
2844 * Make sure it is ready to run when the I/O completes because
2845 * it needs to clear the busy bit on the c_seg so that other
2846 * waiting threads can make progress too. To do that, boost
2847 * the rwlock_count so that the priority is boosted.
2849 set_thread_rwlock_boost();
2850 lck_mtx_unlock_always(&c_seg
->c_lock
);
2852 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2854 addr
= (vm_offset_t
)C_SEG_BUFFER_ADDRESS(c_seg
->c_mysegno
);
2855 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
2857 kernel_memory_populate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
, VM_KERN_MEMORY_COMPRESSOR
);
2859 if (vm_swap_get(c_seg
, f_offset
, io_size
) != KERN_SUCCESS
) {
2860 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2862 c_seg
->c_store
.c_swap_handle
= f_offset
;
2864 kernel_memory_depopulate(compressor_map
, addr
, io_size
, KMA_COMPRESSOR
);
2866 c_seg_swapin_requeue(c_seg
, FALSE
, TRUE
, age_on_swapin_q
);
2868 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
2870 vm_swap_decrypt(c_seg
);
2871 #endif /* ENCRYPTED_SWAP */
2873 #if CHECKSUM_THE_SWAP
2874 if (c_seg
->cseg_swap_size
!= io_size
)
2875 panic("swapin size doesn't match swapout size");
2877 if (c_seg
->cseg_hash
!= hash_string((char*) c_seg
->c_store
.c_buffer
, (int)io_size
)) {
2878 panic("c_seg_swapin - Swap hash mismatch\n");
2880 #endif /* CHECKSUM_THE_SWAP */
2882 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2884 c_seg_swapin_requeue(c_seg
, TRUE
, force_minor_compaction
== TRUE
? FALSE
: TRUE
, age_on_swapin_q
);
2886 OSAddAtomic64(c_seg
->c_bytes_used
, &compressor_bytes_used
);
2888 if (force_minor_compaction
== TRUE
) {
2889 if (c_seg_minor_compaction_and_unlock(c_seg
, FALSE
)) {
2891 * Drop the rwlock_count so that the thread priority
2892 * is returned back to where it is supposed to be.
2894 clear_thread_rwlock_boost();
2898 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2901 C_SEG_WAKEUP_DONE(c_seg
);
2904 * Drop the rwlock_count so that the thread priority
2905 * is returned back to where it is supposed to be.
2907 clear_thread_rwlock_boost();
2914 c_segment_sv_hash_drop_ref(int hash_indx
)
2916 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
2920 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_indx
].he_record
;
2922 n_sv_he
.he_ref
= o_sv_he
.he_ref
- 1;
2923 n_sv_he
.he_data
= o_sv_he
.he_data
;
2925 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
) {
2926 if (n_sv_he
.he_ref
== 0)
2927 OSAddAtomic(-1, &c_segment_svp_in_hash
);
2935 c_segment_sv_hash_insert(uint32_t data
)
2939 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
2940 boolean_t got_ref
= FALSE
;
2943 OSAddAtomic(1, &c_segment_svp_zero_compressions
);
2945 OSAddAtomic(1, &c_segment_svp_nonzero_compressions
);
2947 hash_sindx
= data
& C_SV_HASH_MASK
;
2949 for (misses
= 0; misses
< C_SV_HASH_MAX_MISS
; misses
++)
2951 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
2953 while (o_sv_he
.he_data
== data
|| o_sv_he
.he_ref
== 0) {
2954 n_sv_he
.he_ref
= o_sv_he
.he_ref
+ 1;
2955 n_sv_he
.he_data
= data
;
2957 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
) {
2958 if (n_sv_he
.he_ref
== 1)
2959 OSAddAtomic(1, &c_segment_svp_in_hash
);
2963 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
2965 if (got_ref
== TRUE
)
2969 if (hash_sindx
== C_SV_HASH_SIZE
)
2972 if (got_ref
== FALSE
)
2975 return (hash_sindx
);
2979 #if RECORD_THE_COMPRESSED_DATA
2982 c_compressed_record_data(char *src
, int c_size
)
2984 if ((c_compressed_record_cptr
+ c_size
+ 4) >= c_compressed_record_ebuf
)
2985 panic("c_compressed_record_cptr >= c_compressed_record_ebuf");
2987 *(int *)((void *)c_compressed_record_cptr
) = c_size
;
2989 c_compressed_record_cptr
+= 4;
2991 memcpy(c_compressed_record_cptr
, src
, c_size
);
2992 c_compressed_record_cptr
+= c_size
;
2998 c_compress_page(char *src
, c_slot_mapping_t slot_ptr
, c_segment_t
*current_chead
, char *scratch_buf
)
3001 int c_rounded_size
= 0;
3006 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_START
, *current_chead
, 0, 0, 0, 0);
3008 if ((c_seg
= c_seg_allocate(current_chead
)) == NULL
)
3011 * returns with c_seg lock held
3012 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3013 * c_nextslot has been allocated and
3014 * c_store.c_buffer populated
3016 assert(c_seg
->c_state
== C_IS_FILLING
);
3018 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_seg
->c_nextslot
);
3020 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(slot_ptr
);
3021 assert(slot_ptr
== (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(cs
));
3023 cs
->c_offset
= c_seg
->c_nextoffset
;
3025 max_csize
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)cs
->c_offset
);
3027 if (max_csize
> PAGE_SIZE
)
3028 max_csize
= PAGE_SIZE
;
3030 #if CHECKSUM_THE_DATA
3031 cs
->c_hash_data
= hash_string(src
, PAGE_SIZE
);
3034 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3036 c_size
= WKdm_compress_new((const WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3037 (WK_word
*)(uintptr_t)scratch_buf
, max_csize
- 4);
3039 assert(c_size
<= (max_csize
- 4) && c_size
>= -1);
3043 if (max_csize
< PAGE_SIZE
) {
3044 c_current_seg_filled(c_seg
, current_chead
);
3045 assert(*current_chead
== NULL
);
3047 lck_mtx_unlock_always(&c_seg
->c_lock
);
3049 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3054 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3056 OSAddAtomic(1, &c_segment_noncompressible_pages
);
3058 } else if (c_size
== 0) {
3062 * special case - this is a page completely full of a single 32 bit value
3064 hash_index
= c_segment_sv_hash_insert(*(uint32_t *)(uintptr_t)src
);
3066 if (hash_index
!= -1) {
3067 slot_ptr
->s_cindx
= hash_index
;
3068 slot_ptr
->s_cseg
= C_SV_CSEG_ID
;
3070 OSAddAtomic(1, &c_segment_svp_hash_succeeded
);
3071 #if RECORD_THE_COMPRESSED_DATA
3072 c_compressed_record_data(src
, 4);
3074 goto sv_compression
;
3078 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
3080 OSAddAtomic(1, &c_segment_svp_hash_failed
);
3083 #if RECORD_THE_COMPRESSED_DATA
3084 c_compressed_record_data((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3087 #if CHECKSUM_THE_COMPRESSED_DATA
3088 cs
->c_hash_compressed_data
= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
3090 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3092 PACK_C_SIZE(cs
, c_size
);
3093 c_seg
->c_bytes_used
+= c_rounded_size
;
3094 c_seg
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3096 slot_ptr
->s_cindx
= c_seg
->c_nextslot
++;
3097 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3098 slot_ptr
->s_cseg
= c_seg
->c_mysegno
+ 1;
3101 if (c_seg
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3102 c_current_seg_filled(c_seg
, current_chead
);
3103 assert(*current_chead
== NULL
);
3105 lck_mtx_unlock_always(&c_seg
->c_lock
);
3107 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3109 #if RECORD_THE_COMPRESSED_DATA
3110 if ((c_compressed_record_cptr
- c_compressed_record_sbuf
) >= C_SEG_ALLOCSIZE
) {
3111 c_compressed_record_write(c_compressed_record_sbuf
, (int)(c_compressed_record_cptr
- c_compressed_record_sbuf
));
3112 c_compressed_record_cptr
= c_compressed_record_sbuf
;
3116 OSAddAtomic64(c_size
, &c_segment_compressed_bytes
);
3117 OSAddAtomic64(c_rounded_size
, &compressor_bytes_used
);
3119 OSAddAtomic64(PAGE_SIZE
, &c_segment_input_bytes
);
3121 OSAddAtomic(1, &c_segment_pages_compressed
);
3122 OSAddAtomic(1, &sample_period_compression_count
);
3124 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_END
, *current_chead
, c_size
, c_segment_input_bytes
, c_segment_compressed_bytes
, 0);
3129 static inline void sv_decompress(int32_t *ddst
, int32_t pattern
) {
3131 memset_word(ddst
, pattern
, PAGE_SIZE
/ sizeof(int32_t));
3135 /* Unroll the pattern fill loop 4x to encourage the
3136 * compiler to emit NEON stores, cf.
3137 * <rdar://problem/25839866> Loop autovectorization
3139 * We use separate loops for each PAGE_SIZE
3140 * to allow the autovectorizer to engage, as PAGE_SIZE
3141 * is currently not a constant.
3144 if (PAGE_SIZE
== 4096) {
3145 for (i
= 0; i
< (4096U / sizeof(int32_t)); i
+= 4) {
3152 assert(PAGE_SIZE
== 16384);
3153 for (i
= 0; i
< (int)(16384U / sizeof(int32_t)); i
+= 4) {
3164 c_decompress_page(char *dst
, volatile c_slot_mapping_t slot_ptr
, int flags
, int *zeroslot
)
3173 boolean_t need_unlock
= TRUE
;
3174 boolean_t consider_defragmenting
= FALSE
;
3175 boolean_t kdp_mode
= FALSE
;
3177 if (__improbable(flags
& C_KDP
)) {
3179 panic("C_KDP passed to decompress page from outside of debugger context");
3182 assert((flags
& C_KEEP
) == C_KEEP
);
3183 assert((flags
& C_DONT_BLOCK
) == C_DONT_BLOCK
);
3185 if ((flags
& (C_DONT_BLOCK
| C_KEEP
)) != (C_DONT_BLOCK
| C_KEEP
)) {
3194 if (__probable(!kdp_mode
)) {
3195 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3197 if (kdp_lck_rw_lock_is_acquired_exclusive(&c_master_lock
)) {
3204 * if hibernation is enabled, it indicates (via a call
3205 * to 'vm_decompressor_lock' that no further
3206 * decompressions are allowed once it reaches
3207 * the point of flushing all of the currently dirty
3208 * anonymous memory through the compressor and out
3209 * to disk... in this state we allow freeing of compressed
3210 * pages and must honor the C_DONT_BLOCK case
3212 if (dst
&& decompressions_blocked
== TRUE
) {
3213 if (flags
& C_DONT_BLOCK
) {
3215 if (__probable(!kdp_mode
)) {
3216 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3223 * it's safe to atomically assert and block behind the
3224 * lock held in shared mode because "decompressions_blocked" is
3225 * only set and cleared and the thread_wakeup done when the lock
3226 * is held exclusively
3228 assert_wait((event_t
)&decompressions_blocked
, THREAD_UNINT
);
3230 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3232 thread_block(THREAD_CONTINUE_NULL
);
3237 /* s_cseg is actually "segno+1" */
3238 c_segno
= slot_ptr
->s_cseg
- 1;
3240 if (__improbable(c_segno
>= c_segments_available
))
3241 panic("c_decompress_page: c_segno %d >= c_segments_available %d, slot_ptr(%p), slot_data(%x)",
3242 c_segno
, c_segments_available
, slot_ptr
, *(int *)((void *)slot_ptr
));
3244 if (__improbable(c_segments
[c_segno
].c_segno
< c_segments_available
))
3245 panic("c_decompress_page: c_segno %d is free, slot_ptr(%p), slot_data(%x)",
3246 c_segno
, slot_ptr
, *(int *)((void *)slot_ptr
));
3248 c_seg
= c_segments
[c_segno
].c_seg
;
3250 if (__probable(!kdp_mode
)) {
3251 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3253 if (kdp_lck_mtx_lock_spin_is_acquired(&c_seg
->c_lock
)) {
3258 assert(c_seg
->c_state
!= C_IS_EMPTY
&& c_seg
->c_state
!= C_IS_FREE
);
3260 if (dst
== NULL
&& c_seg
->c_busy_swapping
) {
3261 assert(c_seg
->c_busy
);
3263 goto bypass_busy_check
;
3265 if (flags
& C_DONT_BLOCK
) {
3266 if (c_seg
->c_busy
|| (C_SEG_IS_ONDISK(c_seg
) && dst
)) {
3273 if (c_seg
->c_busy
) {
3275 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3277 c_seg_wait_on_busy(c_seg
);
3283 c_indx
= slot_ptr
->s_cindx
;
3285 if (__improbable(c_indx
>= c_seg
->c_nextslot
))
3286 panic("c_decompress_page: c_indx %d >= c_nextslot %d, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3287 c_indx
, c_seg
->c_nextslot
, c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3289 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3291 c_size
= UNPACK_C_SIZE(cs
);
3293 if (__improbable(c_size
== 0))
3294 panic("c_decompress_page: c_size == 0, c_seg(%p), slot_ptr(%p), slot_data(%x)",
3295 c_seg
, slot_ptr
, *(int *)((void *)slot_ptr
));
3297 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3300 uint32_t age_of_cseg
;
3301 clock_sec_t cur_ts_sec
;
3302 clock_nsec_t cur_ts_nsec
;
3304 if (C_SEG_IS_ONDISK(c_seg
)) {
3305 assert(kdp_mode
== FALSE
);
3306 retval
= c_seg_swapin(c_seg
, FALSE
, TRUE
);
3307 assert(retval
== 0);
3311 if (c_seg
->c_state
== C_ON_BAD_Q
) {
3312 assert(c_seg
->c_store
.c_buffer
== NULL
);
3315 goto c_seg_invalid_data
;
3317 #if CHECKSUM_THE_COMPRESSED_DATA
3318 if (cs
->c_hash_compressed_data
!= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))
3319 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
);
3321 if (c_rounded_size
== PAGE_SIZE
) {
3323 * page wasn't compressible... just copy it out
3325 memcpy(dst
, &c_seg
->c_store
.c_buffer
[cs
->c_offset
], PAGE_SIZE
);
3326 } else if (c_size
== 4) {
3331 * page was populated with a single value
3332 * that didn't fit into our fast hash
3333 * so we packed it in as a single non-compressed value
3334 * that we need to populate the page with
3336 dptr
= (int32_t *)(uintptr_t)dst
;
3337 data
= *(int32_t *)(&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
3338 sv_decompress(dptr
, data
);
3343 if (__probable(!kdp_mode
)) {
3345 * we're behind the c_seg lock held in spin mode
3346 * which means pre-emption is disabled... therefore
3347 * the following sequence is atomic and safe
3349 my_cpu_no
= cpu_number();
3351 assert(my_cpu_no
< compressor_cpus
);
3353 scratch_buf
= &compressor_scratch_bufs
[my_cpu_no
* vm_compressor_get_decode_scratch_size()];
3355 scratch_buf
= kdp_compressor_scratch_buf
;
3358 if (vm_compressor_algorithm() != VM_COMPRESSOR_DEFAULT_CODEC
) {
3360 WKdm_decompress_new((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3361 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3365 #if CHECKSUM_THE_DATA
3366 if (cs
->c_hash_data
!= hash_string(dst
, PAGE_SIZE
))
3367 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
);
3370 if (c_seg
->c_swappedin_ts
== 0 && !kdp_mode
) {
3372 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
3374 age_of_cseg
= (uint32_t)cur_ts_sec
- c_seg
->c_creation_ts
;
3375 if (age_of_cseg
< DECOMPRESSION_SAMPLE_MAX_AGE
)
3376 OSAddAtomic(1, &age_of_decompressions_during_sample_period
[age_of_cseg
]);
3378 OSAddAtomic(1, &overage_decompressions_during_sample_period
);
3380 OSAddAtomic(1, &sample_period_decompression_count
);
3385 if (flags
& C_KEEP
) {
3389 assert(kdp_mode
== FALSE
);
3391 c_seg
->c_bytes_unused
+= c_rounded_size
;
3392 c_seg
->c_bytes_used
-= c_rounded_size
;
3395 if (c_indx
< c_seg
->c_firstemptyslot
)
3396 c_seg
->c_firstemptyslot
= c_indx
;
3398 OSAddAtomic(-1, &c_segment_pages_compressed
);
3400 if (c_seg
->c_state
!= C_ON_BAD_Q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
3402 * C_SEG_IS_ONDISK == TRUE can occur when we're doing a
3403 * free of a compressed page (i.e. dst == NULL)
3405 OSAddAtomic64(-c_rounded_size
, &compressor_bytes_used
);
3407 if (c_seg
->c_busy_swapping
) {
3409 * bypass case for c_busy_swapping...
3410 * let the swapin/swapout paths deal with putting
3411 * the c_seg on the minor compaction queue if needed
3413 assert(c_seg
->c_busy
);
3416 assert(!c_seg
->c_busy
);
3418 if (c_seg
->c_state
!= C_IS_FILLING
) {
3419 if (c_seg
->c_bytes_used
== 0) {
3420 if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3421 int pages_populated
;
3423 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
3424 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3426 if (pages_populated
) {
3428 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3429 assert(c_seg
->c_store
.c_buffer
!= NULL
);
3432 lck_mtx_unlock_always(&c_seg
->c_lock
);
3434 kernel_memory_depopulate(compressor_map
, (vm_offset_t
) c_seg
->c_store
.c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
3436 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3437 C_SEG_WAKEUP_DONE(c_seg
);
3439 if (!c_seg
->c_on_minorcompact_q
&& c_seg
->c_state
!= C_ON_SWAPOUT_Q
)
3440 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3442 if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
) {
3444 c_seg_move_to_sparse_list(c_seg
);
3445 consider_defragmenting
= TRUE
;
3448 } else if (c_seg
->c_on_minorcompact_q
) {
3450 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3452 if (C_SEG_SHOULD_MINORCOMPACT(c_seg
)) {
3453 c_seg_try_minor_compaction_and_unlock(c_seg
);
3454 need_unlock
= FALSE
;
3456 } else if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3458 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
) {
3459 c_seg_need_delayed_compaction(c_seg
, FALSE
);
3461 } else if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
&& C_SEG_ONDISK_IS_SPARSE(c_seg
)) {
3463 c_seg_move_to_sparse_list(c_seg
);
3464 consider_defragmenting
= TRUE
;
3468 if (__improbable(kdp_mode
)) {
3472 if (need_unlock
== TRUE
)
3473 lck_mtx_unlock_always(&c_seg
->c_lock
);
3475 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3477 if (consider_defragmenting
== TRUE
)
3478 vm_swap_consider_defragmenting();
3486 vm_compressor_get(ppnum_t pn
, int *slot
, int flags
)
3488 c_slot_mapping_t slot_ptr
;
3494 dst
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3496 #error "unsupported architecture"
3498 slot_ptr
= (c_slot_mapping_t
)slot
;
3500 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3505 * page was populated with a single value
3506 * that found a home in our hash table
3507 * grab that value from the hash and populate the page
3508 * that we need to populate the page with
3510 dptr
= (int32_t *)(uintptr_t)dst
;
3511 data
= c_segment_sv_hash_table
[slot_ptr
->s_cindx
].he_data
;
3513 memset_word(dptr
, data
, PAGE_SIZE
/ sizeof(int32_t));
3518 for (i
= 0; i
< (int)(PAGE_SIZE
/ sizeof(int32_t)); i
++)
3522 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3524 if ( !(flags
& C_KEEP
)) {
3525 OSAddAtomic(-1, &c_segment_pages_compressed
);
3529 OSAddAtomic(1, &c_segment_svp_nonzero_decompressions
);
3531 OSAddAtomic(1, &c_segment_svp_zero_decompressions
);
3536 retval
= c_decompress_page(dst
, slot_ptr
, flags
, &zeroslot
);
3539 * zeroslot will be set to 0 by c_decompress_page if (flags & C_KEEP)
3540 * or (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be TRUE
3546 * returns 0 if we successfully decompressed a page from a segment already in memory
3547 * returns 1 if we had to first swap in the segment, before successfully decompressing the page
3548 * returns -1 if we encountered an error swapping in the segment - decompression failed
3549 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be true
3556 vm_compressor_free(int *slot
, int flags
)
3558 c_slot_mapping_t slot_ptr
;
3562 assert(flags
== 0 || flags
== C_DONT_BLOCK
);
3564 slot_ptr
= (c_slot_mapping_t
)slot
;
3566 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3568 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3569 OSAddAtomic(-1, &c_segment_pages_compressed
);
3574 retval
= c_decompress_page(NULL
, slot_ptr
, flags
, &zeroslot
);
3576 * returns 0 if we successfully freed the specified compressed page
3577 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' set
3583 assert(retval
== -2);
3590 vm_compressor_put(ppnum_t pn
, int *slot
, void **current_chead
, char *scratch_buf
)
3596 src
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3598 #error "unsupported architecture"
3600 retval
= c_compress_page(src
, (c_slot_mapping_t
)slot
, (c_segment_t
*)current_chead
, scratch_buf
);
3606 vm_compressor_transfer(
3610 c_slot_mapping_t dst_slot
, src_slot
;
3615 src_slot
= (c_slot_mapping_t
) src_slot_p
;
3617 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3618 *dst_slot_p
= *src_slot_p
;
3622 dst_slot
= (c_slot_mapping_t
) dst_slot_p
;
3624 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3625 /* get segment for src_slot */
3626 c_seg
= c_segments
[src_slot
->s_cseg
-1].c_seg
;
3628 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3629 /* wait if it's busy */
3630 if (c_seg
->c_busy
&& !c_seg
->c_busy_swapping
) {
3631 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3632 c_seg_wait_on_busy(c_seg
);
3635 /* find the c_slot */
3636 c_indx
= src_slot
->s_cindx
;
3637 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3638 /* point the c_slot back to dst_slot instead of src_slot */
3639 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(dst_slot
);
3641 *dst_slot_p
= *src_slot_p
;
3643 lck_mtx_unlock_always(&c_seg
->c_lock
);
3644 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3649 int freezer_finished_filling
= 0;
3652 vm_compressor_finished_filling(
3653 void **current_chead
)
3657 if ((c_seg
= *(c_segment_t
*)current_chead
) == NULL
)
3660 assert(c_seg
->c_state
== C_IS_FILLING
);
3662 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3664 c_current_seg_filled(c_seg
, (c_segment_t
*)current_chead
);
3666 lck_mtx_unlock_always(&c_seg
->c_lock
);
3668 freezer_finished_filling
++;
3673 * This routine is used to transfer the compressed chunks from
3674 * the c_seg/cindx pointed to by slot_p into a new c_seg headed
3675 * by the current_chead and a new cindx within that c_seg.
3677 * Currently, this routine is only used by the "freezer backed by
3678 * compressor with swap" mode to create a series of c_segs that
3679 * only contain compressed data belonging to one task. So, we
3680 * move a task's previously compressed data into a set of new
3681 * c_segs which will also hold the task's yet to be compressed data.
3685 vm_compressor_relocate(
3686 void **current_chead
,
3689 c_slot_mapping_t slot_ptr
;
3690 c_slot_mapping_t src_slot
;
3691 uint32_t c_rounded_size
;
3697 c_segment_t c_seg_dst
= NULL
;
3698 c_segment_t c_seg_src
= NULL
;
3699 kern_return_t kr
= KERN_SUCCESS
;
3702 src_slot
= (c_slot_mapping_t
) slot_p
;
3704 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3706 * no need to relocate... this is a page full of a single
3707 * value which is hashed to a single entry not contained
3714 c_seg_dst
= c_seg_allocate((c_segment_t
*)current_chead
);
3716 * returns with c_seg lock held
3717 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3718 * c_nextslot has been allocated and
3719 * c_store.c_buffer populated
3721 if (c_seg_dst
== NULL
) {
3723 * Out of compression segments?
3725 kr
= KERN_RESOURCE_SHORTAGE
;
3729 assert(c_seg_dst
->c_busy
== 0);
3731 C_SEG_BUSY(c_seg_dst
);
3733 dst_slot
= c_seg_dst
->c_nextslot
;
3735 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3738 c_seg_src
= c_segments
[src_slot
->s_cseg
- 1].c_seg
;
3740 assert(c_seg_dst
!= c_seg_src
);
3742 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3744 if (C_SEG_IS_ONDISK(c_seg_src
)) {
3747 * A "thaw" can mark a process as eligible for
3748 * another freeze cycle without bringing any of
3749 * its swapped out c_segs back from disk (because
3750 * that is done on-demand).
3752 * If the src c_seg we find for our pre-compressed
3753 * data is already on-disk, then we are dealing
3754 * with an app's data that is already packed and
3755 * swapped out. Don't do anything.
3758 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3760 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3767 if (c_seg_src
->c_busy
) {
3769 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3770 c_seg_wait_on_busy(c_seg_src
);
3774 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3779 C_SEG_BUSY(c_seg_src
);
3781 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3783 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3785 /* find the c_slot */
3786 c_indx
= src_slot
->s_cindx
;
3788 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, c_indx
);
3790 c_size
= UNPACK_C_SIZE(c_src
);
3794 if (c_size
> (uint32_t)(C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)c_seg_dst
->c_nextoffset
))) {
3796 * This segment is full. We need a new one.
3799 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3801 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3802 C_SEG_WAKEUP_DONE(c_seg_src
);
3803 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3807 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
3809 assert(c_seg_dst
->c_busy
);
3810 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
3811 assert(!c_seg_dst
->c_on_minorcompact_q
);
3813 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
3814 assert(*current_chead
== NULL
);
3816 C_SEG_WAKEUP_DONE(c_seg_dst
);
3818 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3822 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3827 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
3829 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
);
3831 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3833 cslot_copy(c_dst
, c_src
);
3834 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
3836 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
3837 c_seg_dst
->c_firstemptyslot
++;
3839 c_seg_dst
->c_nextslot
++;
3840 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
3841 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3844 PACK_C_SIZE(c_src
, 0);
3846 c_seg_src
->c_bytes_used
-= c_rounded_size
;
3847 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
3849 if (c_indx
< c_seg_src
->c_firstemptyslot
) {
3850 c_seg_src
->c_firstemptyslot
= c_indx
;
3853 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
3855 PAGE_REPLACEMENT_ALLOWED(TRUE
);
3856 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
3857 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3858 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
3859 slot_ptr
->s_cindx
= dst_slot
;
3861 PAGE_REPLACEMENT_ALLOWED(FALSE
);
3866 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3868 C_SEG_WAKEUP_DONE(c_seg_src
);
3870 if (c_seg_src
->c_bytes_used
== 0 && c_seg_src
->c_state
!= C_IS_FILLING
) {
3871 if (!c_seg_src
->c_on_minorcompact_q
)
3872 c_seg_need_delayed_compaction(c_seg_src
, FALSE
);
3875 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3880 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3882 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
3884 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3886 * Nearing or exceeded maximum slot and offset capacity.
3888 assert(c_seg_dst
->c_busy
);
3889 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
3890 assert(!c_seg_dst
->c_on_minorcompact_q
);
3892 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
3893 assert(*current_chead
== NULL
);
3896 C_SEG_WAKEUP_DONE(c_seg_dst
);
3898 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3902 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3907 #endif /* CONFIG_FREEZE */