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 <mach/mach_host.h> /* for host_info() */
39 #include <kern/ledger.h>
41 #include <i386/misc_protos.h>
43 #include <default_pager/default_pager_alerts.h>
44 #include <default_pager/default_pager_object_server.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.
56 int vm_compressor_mode
= VM_PAGER_COMPRESSOR_WITH_SWAP
;
60 int vm_compressor_is_active
= 0;
61 int vm_compression_limit
= 0;
62 int vm_compressor_available
= 0;
64 extern boolean_t vm_swap_up
;
65 extern void vm_pageout_io_throttle(void);
66 extern int not_in_kdp
;
68 #if CHECKSUM_THE_DATA || CHECKSUM_THE_SWAP || CHECKSUM_THE_COMPRESSED_DATA
69 extern unsigned int hash_string(char *cp
, int len
);
72 #define UNPACK_C_SIZE(cs) ((cs->c_size == (PAGE_SIZE-1)) ? PAGE_SIZE : cs->c_size)
73 #define PACK_C_SIZE(cs, size) (cs->c_size = ((size == PAGE_SIZE) ? PAGE_SIZE - 1 : size))
76 struct c_sv_hash_entry
{
80 uint32_t c_sv_he_data
;
82 uint64_t c_sv_he_record
;
87 #define he_ref c_sv_he_un.c_sv_he.c_sv_he_ref
88 #define he_data c_sv_he_un.c_sv_he.c_sv_he_data
89 #define he_record c_sv_he_un.c_sv_he_record
91 #define C_SV_HASH_MAX_MISS 32
92 #define C_SV_HASH_SIZE ((1 << 10))
93 #define C_SV_HASH_MASK ((1 << 10) - 1)
94 #define C_SV_CSEG_ID ((1 << 22) - 1)
97 struct c_slot_mapping
{
98 uint32_t s_cseg
:22, /* segment number + 1 */
99 s_cindx
:10; /* index in the segment */
101 #define C_SLOT_MAX_INDEX (1 << 10)
103 typedef struct c_slot_mapping
*c_slot_mapping_t
;
113 #define C_SLOT_PACK_PTR(ptr) (((uintptr_t)ptr - (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS) >> 2)
114 #define C_SLOT_UNPACK_PTR(cslot) ((uintptr_t)(cslot->c_packed_ptr << 2) + (uintptr_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS)
117 uint32_t c_segment_count
= 0;
118 uint32_t c_segment_count_max
= 0;
120 uint64_t c_generation_id
= 0;
121 uint64_t c_generation_id_flush_barrier
;
124 #define HIBERNATE_FLUSHING_SECS_TO_COMPLETE 120
126 boolean_t hibernate_no_swapspace
= FALSE
;
127 clock_sec_t hibernate_flushing_deadline
= 0;
130 #if RECORD_THE_COMPRESSED_DATA
131 char *c_compressed_record_sbuf
;
132 char *c_compressed_record_ebuf
;
133 char *c_compressed_record_cptr
;
137 queue_head_t c_age_list_head
;
138 queue_head_t c_swapout_list_head
;
139 queue_head_t c_swappedin_list_head
;
140 queue_head_t c_swappedout_list_head
;
141 queue_head_t c_swappedout_sparse_list_head
;
142 queue_head_t c_major_list_head
;
143 queue_head_t c_filling_list_head
;
144 queue_head_t c_bad_list_head
;
146 uint32_t c_age_count
= 0;
147 uint32_t c_swapout_count
= 0;
148 uint32_t c_swappedin_count
= 0;
149 uint32_t c_swappedout_count
= 0;
150 uint32_t c_swappedout_sparse_count
= 0;
151 uint32_t c_major_count
= 0;
152 uint32_t c_filling_count
= 0;
153 uint32_t c_empty_count
= 0;
154 uint32_t c_bad_count
= 0;
157 queue_head_t c_minor_list_head
;
158 uint32_t c_minor_count
= 0;
160 int c_overage_swapped_count
= 0;
161 int c_overage_swapped_limit
= 0;
163 int c_seg_fixed_array_len
;
164 union c_segu
*c_segments
;
165 vm_offset_t c_buffers
;
166 vm_size_t c_buffers_size
;
167 caddr_t c_segments_next_page
;
168 boolean_t c_segments_busy
;
169 uint32_t c_segments_available
;
170 uint32_t c_segments_limit
;
171 uint32_t c_segments_nearing_limit
;
173 uint32_t c_segment_svp_in_hash
;
174 uint32_t c_segment_svp_hash_succeeded
;
175 uint32_t c_segment_svp_hash_failed
;
176 uint32_t c_segment_svp_zero_compressions
;
177 uint32_t c_segment_svp_nonzero_compressions
;
178 uint32_t c_segment_svp_zero_decompressions
;
179 uint32_t c_segment_svp_nonzero_decompressions
;
181 uint32_t c_segment_noncompressible_pages
;
183 uint32_t c_segment_pages_compressed
;
184 uint32_t c_segment_pages_compressed_limit
;
185 uint32_t c_segment_pages_compressed_nearing_limit
;
186 uint32_t c_free_segno_head
= (uint32_t)-1;
188 uint32_t vm_compressor_minorcompact_threshold_divisor
= 10;
189 uint32_t vm_compressor_majorcompact_threshold_divisor
= 10;
190 uint32_t vm_compressor_unthrottle_threshold_divisor
= 10;
191 uint32_t vm_compressor_catchup_threshold_divisor
= 10;
193 #define C_SEGMENTS_PER_PAGE (PAGE_SIZE / sizeof(union c_segu))
196 lck_grp_attr_t vm_compressor_lck_grp_attr
;
197 lck_attr_t vm_compressor_lck_attr
;
198 lck_grp_t vm_compressor_lck_grp
;
200 #if __i386__ || __x86_64__
201 lck_mtx_t
*c_list_lock
;
202 #else /* __i386__ || __x86_64__ */
203 lck_spin_t
*c_list_lock
;
204 #endif /* __i386__ || __x86_64__ */
206 lck_rw_t c_master_lock
;
207 boolean_t decompressions_blocked
= FALSE
;
209 zone_t compressor_segment_zone
;
210 int c_compressor_swap_trigger
= 0;
212 uint32_t compressor_cpus
;
213 char *compressor_scratch_bufs
;
214 char *kdp_compressor_scratch_buf
;
215 char *kdp_compressor_decompressed_page
;
216 addr64_t kdp_compressor_decompressed_page_paddr
;
217 ppnum_t kdp_compressor_decompressed_page_ppnum
;
219 clock_sec_t start_of_sample_period_sec
= 0;
220 clock_nsec_t start_of_sample_period_nsec
= 0;
221 clock_sec_t start_of_eval_period_sec
= 0;
222 clock_nsec_t start_of_eval_period_nsec
= 0;
223 uint32_t sample_period_decompression_count
= 0;
224 uint32_t sample_period_compression_count
= 0;
225 uint32_t last_eval_decompression_count
= 0;
226 uint32_t last_eval_compression_count
= 0;
228 #define DECOMPRESSION_SAMPLE_MAX_AGE (60 * 30)
230 boolean_t vm_swapout_ripe_segments
= FALSE
;
231 uint32_t vm_ripe_target_age
= (60 * 60 * 48);
233 uint32_t swapout_target_age
= 0;
234 uint32_t age_of_decompressions_during_sample_period
[DECOMPRESSION_SAMPLE_MAX_AGE
];
235 uint32_t overage_decompressions_during_sample_period
= 0;
237 void do_fastwake_warmup(void);
238 boolean_t fastwake_warmup
= FALSE
;
239 boolean_t fastwake_recording_in_progress
= FALSE
;
240 clock_sec_t dont_trim_until_ts
= 0;
242 uint64_t c_segment_warmup_count
;
243 uint64_t first_c_segment_to_warm_generation_id
= 0;
244 uint64_t last_c_segment_to_warm_generation_id
= 0;
245 boolean_t hibernate_flushing
= FALSE
;
247 int64_t c_segment_input_bytes
__attribute__((aligned(8))) = 0;
248 int64_t c_segment_compressed_bytes
__attribute__((aligned(8))) = 0;
249 int64_t compressor_bytes_used
__attribute__((aligned(8))) = 0;
252 struct c_sv_hash_entry c_segment_sv_hash_table
[C_SV_HASH_SIZE
] __attribute__ ((aligned (8)));
255 static boolean_t
compressor_needs_to_swap(void);
256 static void vm_compressor_swap_trigger_thread(void);
257 static void vm_compressor_do_delayed_compactions(boolean_t
);
258 static void vm_compressor_compact_and_swap(boolean_t
);
259 static void vm_compressor_age_swapped_in_segments(boolean_t
);
261 static void vm_compressor_take_paging_space_action(void);
263 boolean_t
vm_compressor_low_on_space(void);
265 void compute_swapout_target_age(void);
267 boolean_t
c_seg_major_compact(c_segment_t
, c_segment_t
);
268 boolean_t
c_seg_major_compact_ok(c_segment_t
, c_segment_t
);
270 int c_seg_minor_compaction_and_unlock(c_segment_t
, boolean_t
);
271 int c_seg_do_minor_compaction_and_unlock(c_segment_t
, boolean_t
, boolean_t
, boolean_t
);
272 void c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
);
273 void c_seg_need_delayed_compaction(c_segment_t
);
275 void c_seg_move_to_sparse_list(c_segment_t
);
276 void c_seg_insert_into_q(queue_head_t
*, c_segment_t
);
278 uint64_t vm_available_memory(void);
279 uint64_t vm_compressor_pages_compressed(void);
281 extern unsigned int dp_pages_free
, dp_pages_reserve
;
284 vm_available_memory(void)
286 return (((uint64_t)AVAILABLE_NON_COMPRESSED_MEMORY
) * PAGE_SIZE_64
);
291 vm_compressor_pages_compressed(void)
293 return (c_segment_pages_compressed
* PAGE_SIZE_64
);
298 vm_compression_available(void)
300 if ( !(COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
))
303 if (c_segments_available
>= c_segments_limit
|| c_segment_pages_compressed
>= c_segment_pages_compressed_limit
)
311 vm_compressor_low_on_space(void)
313 if ((c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) ||
314 (c_segment_count
> c_segments_nearing_limit
))
322 vm_wants_task_throttled(task_t task
)
324 if (task
== kernel_task
)
327 if (vm_compressor_mode
== COMPRESSED_PAGER_IS_ACTIVE
|| vm_compressor_mode
== DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
328 if ((vm_compressor_low_on_space() || HARD_THROTTLE_LIMIT_REACHED()) &&
329 (unsigned int)pmap_compressed(task
->map
->pmap
) > (c_segment_pages_compressed
/ 4))
332 if (((dp_pages_free
+ dp_pages_reserve
< 2000) && VM_DYNAMIC_PAGING_ENABLED(memory_manager_default
)) &&
333 get_task_resident_size(task
) > (((AVAILABLE_NON_COMPRESSED_MEMORY
) * PAGE_SIZE
) / 5))
341 static uint32_t no_paging_space_action_in_progress
= 0;
342 extern void memorystatus_send_low_swap_note(void);
345 vm_compressor_take_paging_space_action(void)
347 if (no_paging_space_action_in_progress
== 0) {
349 if (OSCompareAndSwap(0, 1, (UInt32
*)&no_paging_space_action_in_progress
)) {
351 if (no_paging_space_action()) {
352 memorystatus_send_low_swap_note();
355 no_paging_space_action_in_progress
= 0;
363 vm_compressor_init_locks(void)
365 lck_grp_attr_setdefault(&vm_compressor_lck_grp_attr
);
366 lck_grp_init(&vm_compressor_lck_grp
, "vm_compressor", &vm_compressor_lck_grp_attr
);
367 lck_attr_setdefault(&vm_compressor_lck_attr
);
369 lck_rw_init(&c_master_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
374 vm_decompressor_lock(void)
376 PAGE_REPLACEMENT_ALLOWED(TRUE
);
378 decompressions_blocked
= TRUE
;
380 PAGE_REPLACEMENT_ALLOWED(FALSE
);
384 vm_decompressor_unlock(void)
386 PAGE_REPLACEMENT_ALLOWED(TRUE
);
388 decompressions_blocked
= FALSE
;
390 PAGE_REPLACEMENT_ALLOWED(FALSE
);
392 thread_wakeup((event_t
)&decompressions_blocked
);
398 vm_compressor_init(void)
401 struct c_slot cs_dummy
;
402 c_slot_t cs
= &cs_dummy
;
403 int c_segment_min_size
;
404 int c_segment_padded_size
;
407 * ensure that any pointer that gets created from
408 * the vm_page zone can be packed properly
410 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_min_address
);
412 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_min_address
)
413 panic("C_SLOT_UNPACK_PTR failed on zone_map_min_address - %p", (void *)zone_map_min_address
);
415 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(zone_map_max_address
);
417 if (C_SLOT_UNPACK_PTR(cs
) != (uintptr_t)zone_map_max_address
)
418 panic("C_SLOT_UNPACK_PTR failed on zone_map_max_address - %p", (void *)zone_map_max_address
);
421 assert((C_SEGMENTS_PER_PAGE
* sizeof(union c_segu
)) == PAGE_SIZE
);
423 PE_parse_boot_argn("vm_compression_limit", &vm_compression_limit
, sizeof (vm_compression_limit
));
425 if (max_mem
<= (3ULL * 1024ULL * 1024ULL * 1024ULL)) {
426 vm_compressor_minorcompact_threshold_divisor
= 11;
427 vm_compressor_majorcompact_threshold_divisor
= 13;
428 vm_compressor_unthrottle_threshold_divisor
= 20;
429 vm_compressor_catchup_threshold_divisor
= 35;
431 vm_compressor_minorcompact_threshold_divisor
= 20;
432 vm_compressor_majorcompact_threshold_divisor
= 25;
433 vm_compressor_unthrottle_threshold_divisor
= 35;
434 vm_compressor_catchup_threshold_divisor
= 50;
437 * vm_page_init_lck_grp is now responsible for calling vm_compressor_init_locks
438 * c_master_lock needs to be available early so that "vm_page_find_contiguous" can
439 * use PAGE_REPLACEMENT_ALLOWED to coordinate with the compressor.
442 #if __i386__ || __x86_64__
443 c_list_lock
= lck_mtx_alloc_init(&vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
444 #else /* __i386__ || __x86_64__ */
445 c_list_lock
= lck_spin_alloc_init(&vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
446 #endif /* __i386__ || __x86_64__ */
449 queue_init(&c_bad_list_head
);
450 queue_init(&c_age_list_head
);
451 queue_init(&c_minor_list_head
);
452 queue_init(&c_major_list_head
);
453 queue_init(&c_filling_list_head
);
454 queue_init(&c_swapout_list_head
);
455 queue_init(&c_swappedin_list_head
);
456 queue_init(&c_swappedout_list_head
);
457 queue_init(&c_swappedout_sparse_list_head
);
459 c_segment_min_size
= sizeof(struct c_segment
) + (C_SEG_SLOT_VAR_ARRAY_MIN_LEN
* sizeof(struct c_slot
));
461 for (c_segment_padded_size
= 128; c_segment_padded_size
< c_segment_min_size
; c_segment_padded_size
= c_segment_padded_size
<< 1);
463 compressor_segment_zone
= zinit(c_segment_padded_size
, 128000 * c_segment_padded_size
, PAGE_SIZE
, "compressor_segment");
464 zone_change(compressor_segment_zone
, Z_CALLERACCT
, FALSE
);
465 zone_change(compressor_segment_zone
, Z_NOENCRYPT
, TRUE
);
467 c_seg_fixed_array_len
= (c_segment_padded_size
- sizeof(struct c_segment
)) / sizeof(struct c_slot
);
469 c_free_segno_head
= -1;
470 c_segments_available
= 0;
472 if (vm_compression_limit
== 0) {
473 c_segment_pages_compressed_limit
= (uint32_t)((max_mem
/ PAGE_SIZE
)) * vm_scale
;
475 #define OLD_SWAP_LIMIT (1024 * 1024 * 16)
476 #define MAX_SWAP_LIMIT (1024 * 1024 * 128)
478 if (c_segment_pages_compressed_limit
> (OLD_SWAP_LIMIT
))
479 c_segment_pages_compressed_limit
= OLD_SWAP_LIMIT
;
481 if (c_segment_pages_compressed_limit
< (uint32_t)(max_mem
/ PAGE_SIZE_64
))
482 c_segment_pages_compressed_limit
= (uint32_t)(max_mem
/ PAGE_SIZE_64
);
484 if (vm_compression_limit
< MAX_SWAP_LIMIT
)
485 c_segment_pages_compressed_limit
= vm_compression_limit
;
487 c_segment_pages_compressed_limit
= MAX_SWAP_LIMIT
;
489 if ((c_segments_limit
= c_segment_pages_compressed_limit
/ (C_SEG_BUFSIZE
/ PAGE_SIZE
)) > C_SEG_MAX_LIMIT
)
490 c_segments_limit
= C_SEG_MAX_LIMIT
;
492 c_segment_pages_compressed_nearing_limit
= (c_segment_pages_compressed_limit
* 98) / 100;
493 c_segments_nearing_limit
= (c_segments_limit
* 98) / 100;
495 c_segments_busy
= FALSE
;
497 if (kernel_memory_allocate(kernel_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
)
498 panic("vm_compressor_init: kernel_memory_allocate failed - c_segments\n");
499 c_buffers_size
= (vm_size_t
)C_SEG_ALLOCSIZE
* (vm_size_t
)c_segments_limit
;
500 if (kernel_memory_allocate(kernel_map
, &c_buffers
, c_buffers_size
, 0, KMA_COMPRESSOR
| KMA_VAONLY
| KMA_PERMANENT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
)
501 panic("vm_compressor_init: kernel_memory_allocate failed - c_buffers\n");
503 c_segments_next_page
= (caddr_t
)c_segments
;
506 host_basic_info_data_t hinfo
;
507 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
510 host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
512 compressor_cpus
= hinfo
.max_cpus
;
514 compressor_scratch_bufs
= kalloc_tag(compressor_cpus
* WKdm_SCRATCH_BUF_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
516 kdp_compressor_scratch_buf
= kalloc_tag(WKdm_SCRATCH_BUF_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
517 kdp_compressor_decompressed_page
= kalloc_tag(PAGE_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
518 kdp_compressor_decompressed_page_paddr
= kvtophys((vm_offset_t
)kdp_compressor_decompressed_page
);
519 kdp_compressor_decompressed_page_ppnum
= (ppnum_t
) atop(kdp_compressor_decompressed_page_paddr
);
522 freezer_compressor_scratch_buf
= kalloc_tag(WKdm_SCRATCH_BUF_SIZE
, VM_KERN_MEMORY_COMPRESSOR
);
525 #if RECORD_THE_COMPRESSED_DATA
526 if (kernel_memory_allocate(kernel_map
, (vm_offset_t
*)&c_compressed_record_sbuf
, (vm_size_t
)C_SEG_ALLOCSIZE
+ (PAGE_SIZE
* 2), 0, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
) != KERN_SUCCESS
)
527 panic("vm_compressor_init: kernel_memory_allocate failed - c_compressed_record_sbuf\n");
529 c_compressed_record_cptr
= c_compressed_record_sbuf
;
530 c_compressed_record_ebuf
= c_compressed_record_sbuf
+ C_SEG_ALLOCSIZE
+ (PAGE_SIZE
* 2);
533 if (kernel_thread_start_priority((thread_continue_t
)vm_compressor_swap_trigger_thread
, NULL
,
534 BASEPRI_PREEMPT
- 1, &thread
) != KERN_SUCCESS
) {
535 panic("vm_compressor_swap_trigger_thread: create failed");
537 thread_deallocate(thread
);
539 assert(default_pager_init_flag
== 0);
541 if (vm_pageout_internal_start() != KERN_SUCCESS
) {
542 panic("vm_compressor_init: Failed to start the internal pageout thread.\n");
544 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
)
545 vm_compressor_swap_init();
547 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
)
548 vm_compressor_is_active
= 1;
551 memorystatus_freeze_enabled
= TRUE
;
552 #endif /* CONFIG_FREEZE */
554 default_pager_init_flag
= 1;
555 vm_compressor_available
= 1;
557 vm_page_reactivate_all_throttled();
561 #if VALIDATE_C_SEGMENTS
564 c_seg_validate(c_segment_t c_seg
, boolean_t must_be_compact
)
568 int32_t bytes_unused
;
569 uint32_t c_rounded_size
;
573 if (c_seg
->c_firstemptyslot
< c_seg
->c_nextslot
) {
574 c_indx
= c_seg
->c_firstemptyslot
;
575 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
578 panic("c_seg_validate: no slot backing c_firstemptyslot");
581 panic("c_seg_validate: c_firstemptyslot has non-zero size (%d)\n", cs
->c_size
);
586 for (c_indx
= 0; c_indx
< c_seg
->c_nextslot
; c_indx
++) {
588 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
590 c_size
= UNPACK_C_SIZE(cs
);
592 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
594 bytes_used
+= c_rounded_size
;
596 #if CHECKSUM_THE_COMPRESSED_DATA
597 if (c_size
&& cs
->c_hash_compressed_data
!= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))
598 panic("compressed data doesn't match original");
602 if (bytes_used
!= c_seg
->c_bytes_used
)
603 panic("c_seg_validate: bytes_used mismatch - found %d, segment has %d\n", bytes_used
, c_seg
->c_bytes_used
);
605 if (c_seg
->c_bytes_used
> C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
606 panic("c_seg_validate: c_bytes_used > c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
607 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
609 if (must_be_compact
) {
610 if (c_seg
->c_bytes_used
!= C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
))
611 panic("c_seg_validate: c_bytes_used doesn't match c_nextoffset - c_nextoffset = %d, c_bytes_used = %d\n",
612 (int32_t)C_SEG_OFFSET_TO_BYTES((int32_t)c_seg
->c_nextoffset
), c_seg
->c_bytes_used
);
620 c_seg_need_delayed_compaction(c_segment_t c_seg
)
622 boolean_t clear_busy
= FALSE
;
624 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
627 lck_mtx_unlock_always(&c_seg
->c_lock
);
628 lck_mtx_lock_spin_always(c_list_lock
);
629 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
633 assert(c_seg
->c_state
!= C_IS_FILLING
);
635 if (!c_seg
->c_on_minorcompact_q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
636 queue_enter(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
637 c_seg
->c_on_minorcompact_q
= 1;
640 lck_mtx_unlock_always(c_list_lock
);
642 if (clear_busy
== TRUE
)
643 C_SEG_WAKEUP_DONE(c_seg
);
647 unsigned int c_seg_moved_to_sparse_list
= 0;
650 c_seg_move_to_sparse_list(c_segment_t c_seg
)
652 boolean_t clear_busy
= FALSE
;
654 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
657 lck_mtx_unlock_always(&c_seg
->c_lock
);
658 lck_mtx_lock_spin_always(c_list_lock
);
659 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
663 c_seg_switch_state(c_seg
, C_ON_SWAPPEDOUTSPARSE_Q
, FALSE
);
665 c_seg_moved_to_sparse_list
++;
667 lck_mtx_unlock_always(c_list_lock
);
669 if (clear_busy
== TRUE
)
670 C_SEG_WAKEUP_DONE(c_seg
);
675 c_seg_insert_into_q(queue_head_t
*qhead
, c_segment_t c_seg
)
677 c_segment_t c_seg_next
;
679 if (queue_empty(qhead
)) {
680 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
682 c_seg_next
= (c_segment_t
)queue_first(qhead
);
686 if (c_seg
->c_generation_id
< c_seg_next
->c_generation_id
) {
687 queue_insert_before(qhead
, c_seg
, c_seg_next
, c_segment_t
, c_age_list
);
690 c_seg_next
= (c_segment_t
) queue_next(&c_seg_next
->c_age_list
);
692 if (queue_end(qhead
, (queue_entry_t
) c_seg_next
)) {
693 queue_enter(qhead
, c_seg
, c_segment_t
, c_age_list
);
701 int try_minor_compaction_failed
= 0;
702 int try_minor_compaction_succeeded
= 0;
705 c_seg_try_minor_compaction_and_unlock(c_segment_t c_seg
)
708 assert(c_seg
->c_on_minorcompact_q
);
710 * c_seg is currently on the delayed minor compaction
711 * queue and we have c_seg locked... if we can get the
712 * c_list_lock w/o blocking (if we blocked we could deadlock
713 * because the lock order is c_list_lock then c_seg's lock)
714 * we'll pull it from the delayed list and free it directly
716 if ( !lck_mtx_try_lock_spin_always(c_list_lock
)) {
718 * c_list_lock is held, we need to bail
720 try_minor_compaction_failed
++;
722 lck_mtx_unlock_always(&c_seg
->c_lock
);
724 try_minor_compaction_succeeded
++;
727 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, FALSE
);
733 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
)
737 assert(c_seg
->c_busy
);
740 * check for the case that can occur when we are not swapping
741 * and this segment has been major compacted in the past
742 * and moved to the majorcompact q to remove it from further
743 * consideration... if the occupancy falls too low we need
744 * to put it back on the age_q so that it will be considered
745 * in the next major compaction sweep... if we don't do this
746 * we will eventually run into the c_segments_limit
748 if (c_seg
->c_state
== C_ON_MAJORCOMPACT_Q
&& C_SEG_SHOULD_MAJORCOMPACT(c_seg
)) {
750 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
752 if (!c_seg
->c_on_minorcompact_q
) {
753 if (clear_busy
== TRUE
)
754 C_SEG_WAKEUP_DONE(c_seg
);
756 lck_mtx_unlock_always(&c_seg
->c_lock
);
760 queue_remove(&c_minor_list_head
, c_seg
, c_segment_t
, c_list
);
761 c_seg
->c_on_minorcompact_q
= 0;
764 lck_mtx_unlock_always(c_list_lock
);
766 if (disallow_page_replacement
== TRUE
) {
767 lck_mtx_unlock_always(&c_seg
->c_lock
);
769 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
771 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
773 c_seg_freed
= c_seg_minor_compaction_and_unlock(c_seg
, clear_busy
);
775 if (disallow_page_replacement
== TRUE
)
776 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
778 if (need_list_lock
== TRUE
)
779 lck_mtx_lock_spin_always(c_list_lock
);
781 return (c_seg_freed
);
786 c_seg_wait_on_busy(c_segment_t c_seg
)
789 assert_wait((event_t
) (c_seg
), THREAD_UNINT
);
791 lck_mtx_unlock_always(&c_seg
->c_lock
);
792 thread_block(THREAD_CONTINUE_NULL
);
797 c_seg_switch_state(c_segment_t c_seg
, int new_state
, boolean_t insert_head
)
799 int old_state
= c_seg
->c_state
;
801 #if DEVELOPMENT || DEBUG
802 #if __i386__ || __x86_64__
803 if (new_state
!= C_IS_FILLING
)
804 lck_mtx_assert(&c_seg
->c_lock
, LCK_MTX_ASSERT_OWNED
);
805 lck_mtx_assert(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
811 assert(new_state
== C_IS_FILLING
|| new_state
== C_IS_FREE
);
817 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_ON_SWAPOUT_Q
);
819 queue_remove(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
824 assert(new_state
== C_ON_SWAPOUT_Q
|| new_state
== C_ON_MAJORCOMPACT_Q
||
825 new_state
== C_IS_FREE
);
827 queue_remove(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
831 case C_ON_SWAPPEDIN_Q
:
832 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
834 queue_remove(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
839 assert(new_state
== C_ON_SWAPPEDOUT_Q
|| new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
840 new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
|| new_state
== C_IS_EMPTY
);
842 queue_remove(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
843 thread_wakeup((event_t
)&compaction_swapper_running
);
847 case C_ON_SWAPPEDOUT_Q
:
848 assert(new_state
== C_ON_SWAPPEDIN_Q
|| new_state
== C_ON_SWAPPEDOUTSPARSE_Q
||
849 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
851 queue_remove(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
852 c_swappedout_count
--;
855 case C_ON_SWAPPEDOUTSPARSE_Q
:
856 assert(new_state
== C_ON_SWAPPEDIN_Q
||
857 new_state
== C_ON_BAD_Q
|| new_state
== C_IS_EMPTY
|| new_state
== C_IS_FREE
);
859 queue_remove(&c_swappedout_sparse_list_head
, c_seg
, c_segment_t
, c_age_list
);
860 c_swappedout_sparse_count
--;
863 case C_ON_MAJORCOMPACT_Q
:
864 assert(new_state
== C_ON_AGE_Q
|| new_state
== C_IS_FREE
);
866 queue_remove(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
871 assert(new_state
== C_IS_FREE
);
873 queue_remove(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
878 panic("c_seg %p has bad c_state = %d\n", c_seg
, old_state
);
883 assert(old_state
!= C_IS_FILLING
);
888 assert(old_state
== C_ON_SWAPOUT_Q
|| old_state
== C_ON_SWAPPEDOUT_Q
|| old_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
894 assert(old_state
== C_IS_EMPTY
);
896 queue_enter(&c_filling_list_head
, c_seg
, c_segment_t
, c_age_list
);
901 assert(old_state
== C_IS_FILLING
|| old_state
== C_ON_SWAPPEDIN_Q
||
902 old_state
== C_ON_MAJORCOMPACT_Q
|| old_state
== C_ON_SWAPOUT_Q
);
904 if (old_state
== C_IS_FILLING
)
905 queue_enter(&c_age_list_head
, c_seg
, c_segment_t
, c_age_list
);
907 c_seg_insert_into_q(&c_age_list_head
, c_seg
);
911 case C_ON_SWAPPEDIN_Q
:
912 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
914 if (insert_head
== TRUE
)
915 queue_enter_first(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
917 queue_enter(&c_swappedin_list_head
, c_seg
, c_segment_t
, c_age_list
);
922 assert(old_state
== C_ON_AGE_Q
|| old_state
== C_IS_FILLING
);
924 if (insert_head
== TRUE
)
925 queue_enter_first(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
927 queue_enter(&c_swapout_list_head
, c_seg
, c_segment_t
, c_age_list
);
931 case C_ON_SWAPPEDOUT_Q
:
932 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
);
934 if (insert_head
== TRUE
)
935 queue_enter_first(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
937 queue_enter(&c_swappedout_list_head
, c_seg
, c_segment_t
, c_age_list
);
938 c_swappedout_count
++;
941 case C_ON_SWAPPEDOUTSPARSE_Q
:
942 assert(c_seg
->c_state
== C_ON_SWAPOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
);
944 c_seg_insert_into_q(&c_swappedout_sparse_list_head
, c_seg
);
945 c_swappedout_sparse_count
++;
948 case C_ON_MAJORCOMPACT_Q
:
949 assert(c_seg
->c_state
== C_ON_AGE_Q
);
951 if (insert_head
== TRUE
)
952 queue_enter_first(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
954 queue_enter(&c_major_list_head
, c_seg
, c_segment_t
, c_age_list
);
959 assert(c_seg
->c_state
== C_ON_SWAPPEDOUT_Q
|| c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
961 if (insert_head
== TRUE
)
962 queue_enter_first(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
964 queue_enter(&c_bad_list_head
, c_seg
, c_segment_t
, c_age_list
);
969 panic("c_seg %p requesting bad c_state = %d\n", c_seg
, new_state
);
971 c_seg
->c_state
= new_state
;
977 c_seg_free(c_segment_t c_seg
)
979 assert(c_seg
->c_busy
);
981 lck_mtx_unlock_always(&c_seg
->c_lock
);
982 lck_mtx_lock_spin_always(c_list_lock
);
983 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
985 c_seg_free_locked(c_seg
);
990 c_seg_free_locked(c_segment_t c_seg
)
993 int pages_populated
= 0;
994 int32_t *c_buffer
= NULL
;
995 uint64_t c_swap_handle
= 0;
997 assert(c_seg
->c_busy
);
998 assert(!c_seg
->c_on_minorcompact_q
);
999 assert(!c_seg
->c_busy_swapping
);
1001 if (c_seg
->c_overage_swap
== TRUE
) {
1002 c_overage_swapped_count
--;
1003 c_seg
->c_overage_swap
= FALSE
;
1005 if ( !(C_SEG_IS_ONDISK(c_seg
)))
1006 c_buffer
= c_seg
->c_store
.c_buffer
;
1008 c_swap_handle
= c_seg
->c_store
.c_swap_handle
;
1010 c_seg_switch_state(c_seg
, C_IS_FREE
, FALSE
);
1012 lck_mtx_unlock_always(c_list_lock
);
1015 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
1016 c_seg
->c_store
.c_buffer
= NULL
;
1018 c_seg
->c_store
.c_swap_handle
= (uint64_t)-1;
1020 lck_mtx_unlock_always(&c_seg
->c_lock
);
1023 if (pages_populated
)
1024 kernel_memory_depopulate(kernel_map
, (vm_offset_t
) c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
1026 } else if (c_swap_handle
) {
1028 * Free swap space on disk.
1030 vm_swap_free(c_swap_handle
);
1032 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1034 C_SEG_WAKEUP_DONE(c_seg
);
1035 lck_mtx_unlock_always(&c_seg
->c_lock
);
1037 segno
= c_seg
->c_mysegno
;
1039 lck_mtx_lock_spin_always(c_list_lock
);
1041 * because the c_buffer is now associated with the segno,
1042 * we can't put the segno back on the free list until
1043 * after we have depopulated the c_buffer range, or
1044 * we run the risk of depopulating a range that is
1045 * now being used in one of the compressor heads
1047 c_segments
[segno
].c_segno
= c_free_segno_head
;
1048 c_free_segno_head
= segno
;
1051 lck_mtx_unlock_always(c_list_lock
);
1053 #if __i386__ || __x86_64__
1054 lck_mtx_destroy(&c_seg
->c_lock
, &vm_compressor_lck_grp
);
1055 #else /* __i386__ || __x86_64__ */
1056 lck_spin_destroy(&c_seg
->c_lock
, &vm_compressor_lck_grp
);
1057 #endif /* __i386__ || __x86_64__ */
1059 if (c_seg
->c_slot_var_array_len
)
1060 kfree(c_seg
->c_slot_var_array
, sizeof(struct c_slot
) * c_seg
->c_slot_var_array_len
);
1062 zfree(compressor_segment_zone
, c_seg
);
1066 int c_seg_trim_page_count
= 0;
1069 c_seg_trim_tail(c_segment_t c_seg
)
1074 uint32_t c_rounded_size
;
1075 uint16_t current_nextslot
;
1076 uint32_t current_populated_offset
;
1078 if (c_seg
->c_bytes_used
== 0)
1080 current_nextslot
= c_seg
->c_nextslot
;
1081 current_populated_offset
= c_seg
->c_populated_offset
;
1083 while (c_seg
->c_nextslot
) {
1085 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, (c_seg
->c_nextslot
- 1));
1087 c_size
= UNPACK_C_SIZE(cs
);
1090 if (current_nextslot
!= c_seg
->c_nextslot
) {
1091 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1092 c_offset
= cs
->c_offset
+ C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1094 c_seg
->c_nextoffset
= c_offset
;
1095 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1097 if (c_seg
->c_firstemptyslot
> c_seg
->c_nextslot
)
1098 c_seg
->c_firstemptyslot
= c_seg
->c_nextslot
;
1100 c_seg_trim_page_count
+= ((round_page_32(C_SEG_OFFSET_TO_BYTES(current_populated_offset
)) -
1101 round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
);
1105 c_seg
->c_nextslot
--;
1107 assert(c_seg
->c_nextslot
);
1112 c_seg_minor_compaction_and_unlock(c_segment_t c_seg
, boolean_t clear_busy
)
1114 c_slot_mapping_t slot_ptr
;
1115 uint32_t c_offset
= 0;
1116 uint32_t old_populated_offset
;
1117 uint32_t c_rounded_size
;
1123 boolean_t need_unlock
= TRUE
;
1125 assert(c_seg
->c_busy
);
1127 #if VALIDATE_C_SEGMENTS
1128 c_seg_validate(c_seg
, FALSE
);
1130 if (c_seg
->c_bytes_used
== 0) {
1134 if (c_seg
->c_firstemptyslot
>= c_seg
->c_nextslot
|| C_SEG_UNUSED_BYTES(c_seg
) < PAGE_SIZE
)
1137 #if VALIDATE_C_SEGMENTS
1138 c_seg
->c_was_minor_compacted
++;
1140 c_indx
= c_seg
->c_firstemptyslot
;
1141 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1143 old_populated_offset
= c_seg
->c_populated_offset
;
1144 c_offset
= c_dst
->c_offset
;
1146 for (i
= c_indx
+ 1; i
< c_seg
->c_nextslot
&& c_offset
< c_seg
->c_nextoffset
; i
++) {
1148 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg
, i
);
1150 c_size
= UNPACK_C_SIZE(c_src
);
1155 memcpy(&c_seg
->c_store
.c_buffer
[c_offset
], &c_seg
->c_store
.c_buffer
[c_src
->c_offset
], c_size
);
1157 #if CHECKSUM_THE_DATA
1158 c_dst
->c_hash_data
= c_src
->c_hash_data
;
1160 #if CHECKSUM_THE_COMPRESSED_DATA
1161 c_dst
->c_hash_compressed_data
= c_src
->c_hash_compressed_data
;
1163 c_dst
->c_size
= c_src
->c_size
;
1164 c_dst
->c_packed_ptr
= c_src
->c_packed_ptr
;
1165 c_dst
->c_offset
= c_offset
;
1167 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1168 slot_ptr
->s_cindx
= c_indx
;
1170 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1172 c_offset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1173 PACK_C_SIZE(c_src
, 0);
1176 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
1178 c_seg
->c_firstemptyslot
= c_indx
;
1179 c_seg
->c_nextslot
= c_indx
;
1180 c_seg
->c_nextoffset
= c_offset
;
1181 c_seg
->c_populated_offset
= (c_offset
+ (C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1)) & ~(C_SEG_BYTES_TO_OFFSET(PAGE_SIZE
) - 1);
1182 c_seg
->c_bytes_unused
= 0;
1184 #if VALIDATE_C_SEGMENTS
1185 c_seg_validate(c_seg
, TRUE
);
1188 if (old_populated_offset
> c_seg
->c_populated_offset
) {
1192 gc_size
= C_SEG_OFFSET_TO_BYTES(old_populated_offset
- c_seg
->c_populated_offset
);
1193 gc_ptr
= &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
];
1195 lck_mtx_unlock_always(&c_seg
->c_lock
);
1197 kernel_memory_depopulate(kernel_map
, (vm_offset_t
)gc_ptr
, gc_size
, KMA_COMPRESSOR
);
1199 if (clear_busy
== TRUE
)
1200 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1202 need_unlock
= FALSE
;
1205 if (need_unlock
== TRUE
) {
1206 if (clear_busy
== TRUE
)
1207 C_SEG_WAKEUP_DONE(c_seg
);
1209 lck_mtx_unlock_always(&c_seg
->c_lock
);
1216 c_seg_alloc_nextslot(c_segment_t c_seg
)
1218 struct c_slot
*old_slot_array
= NULL
;
1219 struct c_slot
*new_slot_array
= NULL
;
1223 if (c_seg
->c_nextslot
< c_seg_fixed_array_len
)
1226 if ((c_seg
->c_nextslot
- c_seg_fixed_array_len
) >= c_seg
->c_slot_var_array_len
) {
1228 oldlen
= c_seg
->c_slot_var_array_len
;
1229 old_slot_array
= c_seg
->c_slot_var_array
;
1232 newlen
= C_SEG_SLOT_VAR_ARRAY_MIN_LEN
;
1234 newlen
= oldlen
* 2;
1236 new_slot_array
= (struct c_slot
*)kalloc(sizeof(struct c_slot
) * newlen
);
1238 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1241 memcpy((char *)new_slot_array
, (char *)old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1243 c_seg
->c_slot_var_array_len
= newlen
;
1244 c_seg
->c_slot_var_array
= new_slot_array
;
1246 lck_mtx_unlock_always(&c_seg
->c_lock
);
1249 kfree(old_slot_array
, sizeof(struct c_slot
) * oldlen
);
1256 uint64_t asked_permission
;
1257 uint64_t compactions
;
1258 uint64_t moved_slots
;
1259 uint64_t moved_bytes
;
1260 uint64_t wasted_space_in_swapouts
;
1261 uint64_t count_of_swapouts
;
1262 uint64_t count_of_freed_segs
;
1263 } c_seg_major_compact_stats
;
1266 #define C_MAJOR_COMPACTION_SIZE_APPROPRIATE ((C_SEG_BUFSIZE * 90) / 100)
1270 c_seg_major_compact_ok(
1271 c_segment_t c_seg_dst
,
1272 c_segment_t c_seg_src
)
1275 c_seg_major_compact_stats
.asked_permission
++;
1277 if (c_seg_src
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
&&
1278 c_seg_dst
->c_bytes_used
>= C_MAJOR_COMPACTION_SIZE_APPROPRIATE
)
1281 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1283 * destination segment is full... can't compact
1293 c_seg_major_compact(
1294 c_segment_t c_seg_dst
,
1295 c_segment_t c_seg_src
)
1297 c_slot_mapping_t slot_ptr
;
1298 uint32_t c_rounded_size
;
1304 boolean_t keep_compacting
= TRUE
;
1307 * segments are not locked but they are both marked c_busy
1308 * which keeps c_decompress from working on them...
1309 * we can safely allocate new pages, move compressed data
1310 * from c_seg_src to c_seg_dst and update both c_segment's
1311 * state w/o holding the master lock
1314 #if VALIDATE_C_SEGMENTS
1315 c_seg_dst
->c_was_major_compacted
++;
1316 c_seg_src
->c_was_major_donor
++;
1318 c_seg_major_compact_stats
.compactions
++;
1320 dst_slot
= c_seg_dst
->c_nextslot
;
1322 for (i
= 0; i
< c_seg_src
->c_nextslot
; i
++) {
1324 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, i
);
1326 c_size
= UNPACK_C_SIZE(c_src
);
1329 /* BATCH: move what we have so far; */
1333 if (C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
- c_seg_dst
->c_nextoffset
) < (unsigned) c_size
) {
1334 int size_to_populate
;
1337 size_to_populate
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
);
1339 if (size_to_populate
== 0) {
1341 keep_compacting
= FALSE
;
1344 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
1345 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
1347 kernel_memory_populate(kernel_map
,
1348 (vm_offset_t
) &c_seg_dst
->c_store
.c_buffer
[c_seg_dst
->c_populated_offset
],
1351 VM_KERN_MEMORY_COMPRESSOR
);
1353 c_seg_dst
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
1354 assert(C_SEG_OFFSET_TO_BYTES(c_seg_dst
->c_populated_offset
) <= C_SEG_BUFSIZE
);
1356 c_seg_alloc_nextslot(c_seg_dst
);
1358 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
1360 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
);
1362 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
1364 c_seg_major_compact_stats
.moved_slots
++;
1365 c_seg_major_compact_stats
.moved_bytes
+= c_size
;
1367 #if CHECKSUM_THE_DATA
1368 c_dst
->c_hash_data
= c_src
->c_hash_data
;
1370 #if CHECKSUM_THE_COMPRESSED_DATA
1371 c_dst
->c_hash_compressed_data
= c_src
->c_hash_compressed_data
;
1373 c_dst
->c_size
= c_src
->c_size
;
1374 c_dst
->c_packed_ptr
= c_src
->c_packed_ptr
;
1375 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
1377 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
1378 c_seg_dst
->c_firstemptyslot
++;
1379 c_seg_dst
->c_nextslot
++;
1380 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
1381 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
1383 PACK_C_SIZE(c_src
, 0);
1385 c_seg_src
->c_bytes_used
-= c_rounded_size
;
1386 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
1387 c_seg_src
->c_firstemptyslot
= 0;
1389 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
1390 /* dest segment is now full */
1391 keep_compacting
= FALSE
;
1395 if (dst_slot
< c_seg_dst
->c_nextslot
) {
1397 PAGE_REPLACEMENT_ALLOWED(TRUE
);
1399 * we've now locked out c_decompress from
1400 * converting the slot passed into it into
1401 * a c_segment_t which allows us to use
1402 * the backptr to change which c_segment and
1403 * index the slot points to
1405 while (dst_slot
< c_seg_dst
->c_nextslot
) {
1407 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
1409 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
1410 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
1411 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
1412 slot_ptr
->s_cindx
= dst_slot
++;
1414 PAGE_REPLACEMENT_ALLOWED(FALSE
);
1416 return (keep_compacting
);
1421 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
)
1424 uint64_t start_msecs
;
1426 end_msecs
= (end_sec
* 1000) + end_nsec
/ 1000000;
1427 start_msecs
= (start_sec
* 1000) + start_nsec
/ 1000000;
1429 return (end_msecs
- start_msecs
);
1434 uint32_t compressor_eval_period_in_msecs
= 250;
1435 uint32_t compressor_sample_min_in_msecs
= 500;
1436 uint32_t compressor_sample_max_in_msecs
= 10000;
1437 uint32_t compressor_thrashing_threshold_per_10msecs
= 50;
1438 uint32_t compressor_thrashing_min_per_10msecs
= 20;
1440 /* When true, reset sample data next chance we get. */
1441 static boolean_t compressor_need_sample_reset
= FALSE
;
1443 extern uint32_t vm_page_filecache_min
;
1447 compute_swapout_target_age(void)
1449 clock_sec_t cur_ts_sec
;
1450 clock_nsec_t cur_ts_nsec
;
1451 uint32_t min_operations_needed_in_this_sample
;
1452 uint64_t elapsed_msecs_in_eval
;
1453 uint64_t elapsed_msecs_in_sample
;
1454 boolean_t need_eval_reset
= FALSE
;
1456 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
1458 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
);
1460 if (compressor_need_sample_reset
||
1461 elapsed_msecs_in_sample
>= compressor_sample_max_in_msecs
) {
1462 compressor_need_sample_reset
= TRUE
;
1463 need_eval_reset
= TRUE
;
1466 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
);
1468 if (elapsed_msecs_in_eval
< compressor_eval_period_in_msecs
)
1470 need_eval_reset
= TRUE
;
1472 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_START
, elapsed_msecs_in_eval
, sample_period_compression_count
, sample_period_decompression_count
, 0, 0);
1474 min_operations_needed_in_this_sample
= (compressor_thrashing_min_per_10msecs
* (uint32_t)elapsed_msecs_in_eval
) / 10;
1476 if ((sample_period_compression_count
- last_eval_compression_count
) < min_operations_needed_in_this_sample
||
1477 (sample_period_decompression_count
- last_eval_decompression_count
) < min_operations_needed_in_this_sample
) {
1479 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_compression_count
- last_eval_compression_count
,
1480 sample_period_decompression_count
- last_eval_decompression_count
, 0, 1, 0);
1482 swapout_target_age
= 0;
1484 compressor_need_sample_reset
= TRUE
;
1485 need_eval_reset
= TRUE
;
1488 last_eval_compression_count
= sample_period_compression_count
;
1489 last_eval_decompression_count
= sample_period_decompression_count
;
1491 if (elapsed_msecs_in_sample
< compressor_sample_min_in_msecs
) {
1493 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, 0, 0, 5, 0);
1496 if (sample_period_decompression_count
> ((compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10)) {
1498 uint64_t running_total
;
1499 uint64_t working_target
;
1500 uint64_t aging_target
;
1501 uint32_t oldest_age_of_csegs_sampled
= 0;
1502 uint64_t working_set_approximation
= 0;
1504 swapout_target_age
= 0;
1506 working_target
= (sample_period_decompression_count
/ 100) * 95; /* 95 percent */
1507 aging_target
= (sample_period_decompression_count
/ 100) * 1; /* 1 percent */
1510 for (oldest_age_of_csegs_sampled
= 0; oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
; oldest_age_of_csegs_sampled
++) {
1512 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1514 working_set_approximation
+= oldest_age_of_csegs_sampled
* age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1516 if (running_total
>= working_target
)
1519 if (oldest_age_of_csegs_sampled
< DECOMPRESSION_SAMPLE_MAX_AGE
) {
1521 working_set_approximation
= (working_set_approximation
* 1000) / elapsed_msecs_in_sample
;
1523 if (working_set_approximation
< VM_PAGE_COMPRESSOR_COUNT
) {
1525 running_total
= overage_decompressions_during_sample_period
;
1527 for (oldest_age_of_csegs_sampled
= DECOMPRESSION_SAMPLE_MAX_AGE
- 1; oldest_age_of_csegs_sampled
; oldest_age_of_csegs_sampled
--) {
1528 running_total
+= age_of_decompressions_during_sample_period
[oldest_age_of_csegs_sampled
];
1530 if (running_total
>= aging_target
)
1533 swapout_target_age
= (uint32_t)cur_ts_sec
- oldest_age_of_csegs_sampled
;
1535 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, swapout_target_age
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 2, 0);
1537 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_set_approximation
, VM_PAGE_COMPRESSOR_COUNT
, 0, 3, 0);
1540 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, working_target
, running_total
, 0, 4, 0);
1542 compressor_need_sample_reset
= TRUE
;
1543 need_eval_reset
= TRUE
;
1545 KERNEL_DEBUG(0xe0400020 | DBG_FUNC_END
, sample_period_decompression_count
, (compressor_thrashing_threshold_per_10msecs
* elapsed_msecs_in_sample
) / 10, 0, 6, 0);
1547 if (compressor_need_sample_reset
== TRUE
) {
1548 bzero(age_of_decompressions_during_sample_period
, sizeof(age_of_decompressions_during_sample_period
));
1549 overage_decompressions_during_sample_period
= 0;
1551 start_of_sample_period_sec
= cur_ts_sec
;
1552 start_of_sample_period_nsec
= cur_ts_nsec
;
1553 sample_period_decompression_count
= 0;
1554 sample_period_compression_count
= 0;
1555 last_eval_decompression_count
= 0;
1556 last_eval_compression_count
= 0;
1557 compressor_need_sample_reset
= FALSE
;
1559 if (need_eval_reset
== TRUE
) {
1560 start_of_eval_period_sec
= cur_ts_sec
;
1561 start_of_eval_period_nsec
= cur_ts_nsec
;
1566 int compaction_swapper_inited
= 0;
1567 int compaction_swapper_init_now
= 0;
1568 int compaction_swapper_running
= 0;
1569 int compaction_swapper_abort
= 0;
1573 boolean_t
memorystatus_kill_on_VM_thrashing(boolean_t
);
1574 boolean_t
memorystatus_kill_on_FC_thrashing(boolean_t
);
1575 int compressor_thrashing_induced_jetsam
= 0;
1576 int filecache_thrashing_induced_jetsam
= 0;
1577 static boolean_t vm_compressor_thrashing_detected
= FALSE
;
1578 #endif /* CONFIG_JETSAM */
1581 compressor_needs_to_swap(void)
1583 boolean_t should_swap
= FALSE
;
1585 if (vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
) {
1591 clock_get_system_nanotime(&now
, &nsec
);
1594 lck_mtx_lock_spin_always(c_list_lock
);
1596 if ( !queue_empty(&c_age_list_head
)) {
1597 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1599 age
= now
- c_seg
->c_creation_ts
;
1601 lck_mtx_unlock_always(c_list_lock
);
1603 if (age
>= vm_ripe_target_age
)
1606 if ((vm_compressor_mode
== VM_PAGER_COMPRESSOR_WITH_SWAP
) && vm_swap_up
== TRUE
) {
1607 if (COMPRESSOR_NEEDS_TO_SWAP()) {
1610 if (VM_PAGE_Q_THROTTLED(&vm_pageout_queue_external
) && vm_page_anonymous_count
< (vm_page_inactive_count
/ 20)) {
1613 if (vm_page_free_count
< (vm_page_free_reserved
- (COMPRESSOR_FREE_RESERVED_LIMIT
* 2)))
1616 compute_swapout_target_age();
1618 if (swapout_target_age
) {
1621 lck_mtx_lock_spin_always(c_list_lock
);
1623 if (!queue_empty(&c_age_list_head
)) {
1625 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
1627 if (c_seg
->c_creation_ts
> swapout_target_age
)
1628 swapout_target_age
= 0;
1630 lck_mtx_unlock_always(c_list_lock
);
1632 #if CONFIG_PHANTOM_CACHE
1633 if (vm_phantom_cache_check_pressure())
1636 if (swapout_target_age
)
1640 if (should_swap
|| c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) {
1642 if (vm_compressor_thrashing_detected
== FALSE
) {
1643 vm_compressor_thrashing_detected
= TRUE
;
1645 if (swapout_target_age
|| c_segment_pages_compressed
> c_segment_pages_compressed_nearing_limit
) {
1646 memorystatus_kill_on_VM_thrashing(TRUE
/* async */);
1647 compressor_thrashing_induced_jetsam
++;
1649 memorystatus_kill_on_FC_thrashing(TRUE
/* async */);
1650 filecache_thrashing_induced_jetsam
++;
1654 * let the jetsam take precedence over
1655 * any major compactions we might have
1656 * been able to do... otherwise we run
1657 * the risk of doing major compactions
1658 * on segments we're about to free up
1659 * due to the jetsam activity.
1661 should_swap
= FALSE
;
1664 #endif /* CONFIG_JETSAM */
1666 if (should_swap
== FALSE
) {
1668 * COMPRESSOR_NEEDS_TO_MAJOR_COMPACT returns true only if we're
1669 * about to run out of available compressor segments... in this
1670 * case, we absolutely need to run a major compaction even if
1671 * we've just kicked off a jetsam or we don't otherwise need to
1672 * swap... terminating objects releases
1673 * pages back to the uncompressed cache, but does not guarantee
1674 * that we will free up even a single compression segment
1676 should_swap
= COMPRESSOR_NEEDS_TO_MAJOR_COMPACT();
1680 * returning TRUE when swap_supported == FALSE
1681 * will cause the major compaction engine to
1682 * run, but will not trigger any swapping...
1683 * segments that have been major compacted
1684 * will be moved to the majorcompact queue
1686 return (should_swap
);
1691 * This function is called from the jetsam thread after killing something to
1692 * mitigate thrashing.
1694 * We need to restart our thrashing detection heuristics since memory pressure
1695 * has potentially changed significantly, and we don't want to detect on old
1696 * data from before the jetsam.
1699 vm_thrashing_jetsam_done(void)
1701 vm_compressor_thrashing_detected
= FALSE
;
1703 /* Were we compressor-thrashing or filecache-thrashing? */
1704 if (swapout_target_age
) {
1705 swapout_target_age
= 0;
1706 compressor_need_sample_reset
= TRUE
;
1708 #if CONFIG_PHANTOM_CACHE
1710 vm_phantom_cache_restart_sample();
1714 #endif /* CONFIG_JETSAM */
1716 uint32_t vm_wake_compactor_swapper_calls
= 0;
1719 vm_wake_compactor_swapper(void)
1721 if (compaction_swapper_running
|| c_segment_count
== 0)
1724 if (c_minor_count
|| COMPRESSOR_NEEDS_TO_MAJOR_COMPACT()) {
1726 lck_mtx_lock_spin_always(c_list_lock
);
1728 fastwake_warmup
= FALSE
;
1730 if (compaction_swapper_running
== 0) {
1732 vm_wake_compactor_swapper_calls
++;
1734 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
1736 compaction_swapper_running
= 1;
1738 lck_mtx_unlock_always(c_list_lock
);
1744 vm_consider_swapping()
1746 c_segment_t c_seg
, c_seg_next
;
1751 lck_mtx_lock_spin_always(c_list_lock
);
1753 compaction_swapper_abort
= 1;
1755 while (compaction_swapper_running
) {
1756 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1758 lck_mtx_unlock_always(c_list_lock
);
1760 thread_block(THREAD_CONTINUE_NULL
);
1762 lck_mtx_lock_spin_always(c_list_lock
);
1764 compaction_swapper_abort
= 0;
1765 compaction_swapper_running
= 1;
1767 vm_swapout_ripe_segments
= TRUE
;
1769 if (!queue_empty(&c_major_list_head
)) {
1771 clock_get_system_nanotime(&now
, &nsec
);
1773 c_seg
= (c_segment_t
)queue_first(&c_major_list_head
);
1775 while (!queue_end(&c_major_list_head
, (queue_entry_t
)c_seg
)) {
1777 if (c_overage_swapped_count
>= c_overage_swapped_limit
)
1780 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
1782 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
1784 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1786 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
1788 lck_mtx_unlock_always(&c_seg
->c_lock
);
1793 vm_compressor_compact_and_swap(FALSE
);
1795 compaction_swapper_running
= 0;
1797 vm_swapout_ripe_segments
= FALSE
;
1799 lck_mtx_unlock_always(c_list_lock
);
1804 vm_consider_waking_compactor_swapper(void)
1806 boolean_t need_wakeup
= FALSE
;
1808 if (compaction_swapper_running
)
1811 if (c_segment_count
== 0)
1814 if (!compaction_swapper_inited
&& !compaction_swapper_init_now
) {
1815 compaction_swapper_init_now
= 1;
1819 if (c_minor_count
&& (COMPRESSOR_NEEDS_TO_MINOR_COMPACT())) {
1823 } else if (compressor_needs_to_swap()) {
1827 } else if (c_minor_count
) {
1828 uint64_t total_bytes
;
1830 total_bytes
= compressor_object
->resident_page_count
* PAGE_SIZE_64
;
1832 if ((total_bytes
- compressor_bytes_used
) > total_bytes
/ 10)
1835 if (need_wakeup
== TRUE
) {
1837 lck_mtx_lock_spin_always(c_list_lock
);
1839 fastwake_warmup
= FALSE
;
1841 if (compaction_swapper_running
== 0) {
1842 memoryshot(VM_WAKEUP_COMPACTOR_SWAPPER
, DBG_FUNC_NONE
);
1844 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
1846 compaction_swapper_running
= 1;
1848 lck_mtx_unlock_always(c_list_lock
);
1853 #define C_SWAPOUT_LIMIT 4
1854 #define DELAYED_COMPACTIONS_PER_PASS 30
1857 vm_compressor_do_delayed_compactions(boolean_t flush_all
)
1860 int number_compacted
= 0;
1861 boolean_t needs_to_swap
= FALSE
;
1864 lck_mtx_assert(c_list_lock
, LCK_MTX_ASSERT_OWNED
);
1866 while (!queue_empty(&c_minor_list_head
) && needs_to_swap
== FALSE
) {
1868 c_seg
= (c_segment_t
)queue_first(&c_minor_list_head
);
1870 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1872 if (c_seg
->c_busy
) {
1874 lck_mtx_unlock_always(c_list_lock
);
1875 c_seg_wait_on_busy(c_seg
);
1876 lck_mtx_lock_spin_always(c_list_lock
);
1882 c_seg_do_minor_compaction_and_unlock(c_seg
, TRUE
, FALSE
, TRUE
);
1884 if (vm_swap_up
== TRUE
&& (number_compacted
++ > DELAYED_COMPACTIONS_PER_PASS
)) {
1886 if ((flush_all
== TRUE
|| compressor_needs_to_swap() == TRUE
) && c_swapout_count
< C_SWAPOUT_LIMIT
)
1887 needs_to_swap
= TRUE
;
1889 number_compacted
= 0;
1891 lck_mtx_lock_spin_always(c_list_lock
);
1896 #define C_SEGMENT_SWAPPEDIN_AGE_LIMIT 10
1899 vm_compressor_age_swapped_in_segments(boolean_t flush_all
)
1905 clock_get_system_nanotime(&now
, &nsec
);
1907 while (!queue_empty(&c_swappedin_list_head
)) {
1909 c_seg
= (c_segment_t
)queue_first(&c_swappedin_list_head
);
1911 if (flush_all
== FALSE
&& (now
- c_seg
->c_swappedin_ts
) < C_SEGMENT_SWAPPEDIN_AGE_LIMIT
)
1914 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
1916 c_seg_switch_state(c_seg
, C_ON_AGE_Q
, FALSE
);
1918 lck_mtx_unlock_always(&c_seg
->c_lock
);
1924 vm_compressor_flush(void)
1926 uint64_t vm_swap_put_failures_at_start
;
1927 wait_result_t wait_result
= 0;
1928 AbsoluteTime startTime
, endTime
;
1929 clock_sec_t now_sec
;
1930 clock_nsec_t now_nsec
;
1933 HIBLOG("vm_compressor_flush - starting\n");
1935 clock_get_uptime(&startTime
);
1937 lck_mtx_lock_spin_always(c_list_lock
);
1939 fastwake_warmup
= FALSE
;
1940 compaction_swapper_abort
= 1;
1942 while (compaction_swapper_running
) {
1943 assert_wait((event_t
)&compaction_swapper_running
, THREAD_UNINT
);
1945 lck_mtx_unlock_always(c_list_lock
);
1947 thread_block(THREAD_CONTINUE_NULL
);
1949 lck_mtx_lock_spin_always(c_list_lock
);
1951 compaction_swapper_abort
= 0;
1952 compaction_swapper_running
= 1;
1954 hibernate_flushing
= TRUE
;
1955 hibernate_no_swapspace
= FALSE
;
1956 c_generation_id_flush_barrier
= c_generation_id
+ 1000;
1958 clock_get_system_nanotime(&now_sec
, &now_nsec
);
1959 hibernate_flushing_deadline
= now_sec
+ HIBERNATE_FLUSHING_SECS_TO_COMPLETE
;
1961 vm_swap_put_failures_at_start
= vm_swap_put_failures
;
1963 vm_compressor_compact_and_swap(TRUE
);
1965 while (!queue_empty(&c_swapout_list_head
)) {
1967 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 5000, 1000*NSEC_PER_USEC
);
1969 lck_mtx_unlock_always(c_list_lock
);
1971 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1973 lck_mtx_lock_spin_always(c_list_lock
);
1975 if (wait_result
== THREAD_TIMED_OUT
)
1978 hibernate_flushing
= FALSE
;
1979 compaction_swapper_running
= 0;
1981 if (vm_swap_put_failures
> vm_swap_put_failures_at_start
)
1982 HIBLOG("vm_compressor_flush failed to clean %llu segments - vm_page_compressor_count(%d)\n",
1983 vm_swap_put_failures
- vm_swap_put_failures_at_start
, VM_PAGE_COMPRESSOR_COUNT
);
1985 lck_mtx_unlock_always(c_list_lock
);
1987 clock_get_uptime(&endTime
);
1988 SUB_ABSOLUTETIME(&endTime
, &startTime
);
1989 absolutetime_to_nanoseconds(endTime
, &nsec
);
1991 HIBLOG("vm_compressor_flush completed - took %qd msecs\n", nsec
/ 1000000ULL);
1995 extern void vm_swap_file_set_tuneables(void);
1996 int compaction_swap_trigger_thread_awakened
= 0;
2000 vm_compressor_swap_trigger_thread(void)
2002 current_thread()->options
|= TH_OPT_VMPRIV
;
2005 * compaction_swapper_init_now is set when the first call to
2006 * vm_consider_waking_compactor_swapper is made from
2007 * vm_pageout_scan... since this function is called upon
2008 * thread creation, we want to make sure to delay adjusting
2009 * the tuneables until we are awakened via vm_pageout_scan
2010 * so that we are at a point where the vm_swapfile_open will
2011 * be operating on the correct directory (in case the default
2012 * of /var/vm/ is overridden by the dymanic_pager
2014 if (compaction_swapper_init_now
&& !compaction_swapper_inited
) {
2015 if (vm_compressor_mode
== VM_PAGER_COMPRESSOR_WITH_SWAP
)
2016 vm_swap_file_set_tuneables();
2018 if (vm_restricted_to_single_processor
== TRUE
)
2019 thread_vm_bind_group_add();
2021 compaction_swapper_inited
= 1;
2023 lck_mtx_lock_spin_always(c_list_lock
);
2025 compaction_swap_trigger_thread_awakened
++;
2027 vm_compressor_compact_and_swap(FALSE
);
2029 assert_wait((event_t
)&c_compressor_swap_trigger
, THREAD_UNINT
);
2031 compaction_swapper_running
= 0;
2032 thread_wakeup((event_t
)&compaction_swapper_running
);
2034 lck_mtx_unlock_always(c_list_lock
);
2036 thread_block((thread_continue_t
)vm_compressor_swap_trigger_thread
);
2043 vm_compressor_record_warmup_start(void)
2047 lck_mtx_lock_spin_always(c_list_lock
);
2049 if (first_c_segment_to_warm_generation_id
== 0) {
2050 if (!queue_empty(&c_age_list_head
)) {
2052 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2054 first_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2056 first_c_segment_to_warm_generation_id
= 0;
2058 fastwake_recording_in_progress
= TRUE
;
2060 lck_mtx_unlock_always(c_list_lock
);
2065 vm_compressor_record_warmup_end(void)
2069 lck_mtx_lock_spin_always(c_list_lock
);
2071 if (fastwake_recording_in_progress
== TRUE
) {
2073 if (!queue_empty(&c_age_list_head
)) {
2075 c_seg
= (c_segment_t
)queue_last(&c_age_list_head
);
2077 last_c_segment_to_warm_generation_id
= c_seg
->c_generation_id
;
2079 last_c_segment_to_warm_generation_id
= first_c_segment_to_warm_generation_id
;
2081 fastwake_recording_in_progress
= FALSE
;
2083 HIBLOG("vm_compressor_record_warmup (%qd - %qd)\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2085 lck_mtx_unlock_always(c_list_lock
);
2089 #define DELAY_TRIM_ON_WAKE_SECS 4
2092 vm_compressor_delay_trim(void)
2097 clock_get_system_nanotime(&sec
, &nsec
);
2098 dont_trim_until_ts
= sec
+ DELAY_TRIM_ON_WAKE_SECS
;
2103 vm_compressor_do_warmup(void)
2105 lck_mtx_lock_spin_always(c_list_lock
);
2107 if (first_c_segment_to_warm_generation_id
== last_c_segment_to_warm_generation_id
) {
2108 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2110 lck_mtx_unlock_always(c_list_lock
);
2114 if (compaction_swapper_running
== 0) {
2116 fastwake_warmup
= TRUE
;
2117 compaction_swapper_running
= 1;
2118 thread_wakeup((event_t
)&c_compressor_swap_trigger
);
2120 lck_mtx_unlock_always(c_list_lock
);
2125 do_fastwake_warmup(void)
2127 uint64_t my_thread_id
;
2128 c_segment_t c_seg
= NULL
;
2129 AbsoluteTime startTime
, endTime
;
2133 HIBLOG("vm_compressor_fastwake_warmup (%qd - %qd) - starting\n", first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
);
2135 clock_get_uptime(&startTime
);
2137 lck_mtx_unlock_always(c_list_lock
);
2139 my_thread_id
= current_thread()->thread_id
;
2140 proc_set_task_policy_thread(kernel_task
, my_thread_id
,
2141 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER2
);
2143 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2145 lck_mtx_lock_spin_always(c_list_lock
);
2147 while (!queue_empty(&c_swappedout_list_head
) && fastwake_warmup
== TRUE
) {
2149 c_seg
= (c_segment_t
) queue_first(&c_swappedout_list_head
);
2151 if (c_seg
->c_generation_id
< first_c_segment_to_warm_generation_id
||
2152 c_seg
->c_generation_id
> last_c_segment_to_warm_generation_id
)
2155 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2156 lck_mtx_unlock_always(c_list_lock
);
2158 if (c_seg
->c_busy
) {
2159 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2160 c_seg_wait_on_busy(c_seg
);
2161 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2163 c_seg_swapin(c_seg
, TRUE
);
2165 lck_mtx_unlock_always(&c_seg
->c_lock
);
2166 c_segment_warmup_count
++;
2168 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2169 vm_pageout_io_throttle();
2170 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2172 lck_mtx_lock_spin_always(c_list_lock
);
2174 lck_mtx_unlock_always(c_list_lock
);
2176 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2178 proc_set_task_policy_thread(kernel_task
, my_thread_id
,
2179 TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER0
);
2181 clock_get_uptime(&endTime
);
2182 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2183 absolutetime_to_nanoseconds(endTime
, &nsec
);
2185 HIBLOG("vm_compressor_fastwake_warmup completed - took %qd msecs\n", nsec
/ 1000000ULL);
2187 lck_mtx_lock_spin_always(c_list_lock
);
2189 first_c_segment_to_warm_generation_id
= last_c_segment_to_warm_generation_id
= 0;
2194 vm_compressor_compact_and_swap(boolean_t flush_all
)
2196 c_segment_t c_seg
, c_seg_next
;
2197 boolean_t keep_compacting
;
2202 if (fastwake_warmup
== TRUE
) {
2203 uint64_t starting_warmup_count
;
2205 starting_warmup_count
= c_segment_warmup_count
;
2207 KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE
, 11) | DBG_FUNC_START
, c_segment_warmup_count
,
2208 first_c_segment_to_warm_generation_id
, last_c_segment_to_warm_generation_id
, 0, 0);
2209 do_fastwake_warmup();
2210 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);
2212 fastwake_warmup
= FALSE
;
2216 * it's possible for the c_age_list_head to be empty if we
2217 * hit our limits for growing the compressor pool and we subsequently
2218 * hibernated... on the next hibernation we could see the queue as
2219 * empty and not proceeed even though we have a bunch of segments on
2220 * the swapped in queue that need to be dealt with.
2222 vm_compressor_do_delayed_compactions(flush_all
);
2224 vm_compressor_age_swapped_in_segments(flush_all
);
2227 * we only need to grab the timestamp once per
2228 * invocation of this function since the
2229 * timescale we're interested in is measured
2232 clock_get_system_nanotime(&now
, &nsec
);
2234 while (!queue_empty(&c_age_list_head
) && compaction_swapper_abort
== 0) {
2236 if (hibernate_flushing
== TRUE
) {
2239 if (hibernate_should_abort()) {
2240 HIBLOG("vm_compressor_flush - hibernate_should_abort returned TRUE\n");
2243 if (hibernate_no_swapspace
== TRUE
) {
2244 HIBLOG("vm_compressor_flush - out of swap space\n");
2247 clock_get_system_nanotime(&sec
, &nsec
);
2249 if (sec
> hibernate_flushing_deadline
) {
2250 HIBLOG("vm_compressor_flush - failed to finish before deadline\n");
2254 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2256 assert_wait_timeout((event_t
) &compaction_swapper_running
, THREAD_INTERRUPTIBLE
, 100, 1000*NSEC_PER_USEC
);
2258 lck_mtx_unlock_always(c_list_lock
);
2260 thread_block(THREAD_CONTINUE_NULL
);
2262 lck_mtx_lock_spin_always(c_list_lock
);
2267 vm_compressor_do_delayed_compactions(flush_all
);
2269 vm_compressor_age_swapped_in_segments(flush_all
);
2271 if (c_swapout_count
>= C_SWAPOUT_LIMIT
) {
2273 * we timed out on the above thread_block
2274 * let's loop around and try again
2275 * the timeout allows us to continue
2276 * to do minor compactions to make
2277 * more memory available
2283 * Swap out segments?
2285 if (flush_all
== FALSE
) {
2286 boolean_t needs_to_swap
;
2288 lck_mtx_unlock_always(c_list_lock
);
2290 needs_to_swap
= compressor_needs_to_swap();
2292 if (needs_to_swap
== TRUE
&& vm_swap_low_on_space())
2293 vm_compressor_take_paging_space_action();
2295 lck_mtx_lock_spin_always(c_list_lock
);
2297 if (needs_to_swap
== FALSE
)
2300 if (queue_empty(&c_age_list_head
))
2302 c_seg
= (c_segment_t
) queue_first(&c_age_list_head
);
2304 assert(c_seg
->c_state
== C_ON_AGE_Q
);
2306 if (flush_all
== TRUE
&& c_seg
->c_generation_id
> c_generation_id_flush_barrier
)
2309 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2311 if (c_seg
->c_busy
) {
2313 lck_mtx_unlock_always(c_list_lock
);
2314 c_seg_wait_on_busy(c_seg
);
2315 lck_mtx_lock_spin_always(c_list_lock
);
2321 if (c_seg_do_minor_compaction_and_unlock(c_seg
, FALSE
, TRUE
, TRUE
)) {
2323 * found an empty c_segment and freed it
2324 * so go grab the next guy in the queue
2326 c_seg_major_compact_stats
.count_of_freed_segs
++;
2332 keep_compacting
= TRUE
;
2334 while (keep_compacting
== TRUE
) {
2336 assert(c_seg
->c_busy
);
2338 /* look for another segment to consolidate */
2340 c_seg_next
= (c_segment_t
) queue_next(&c_seg
->c_age_list
);
2342 if (queue_end(&c_age_list_head
, (queue_entry_t
)c_seg_next
))
2345 assert(c_seg_next
->c_state
== C_ON_AGE_Q
);
2347 if (c_seg_major_compact_ok(c_seg
, c_seg_next
) == FALSE
)
2350 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2352 if (c_seg_next
->c_busy
) {
2354 lck_mtx_unlock_always(c_list_lock
);
2355 c_seg_wait_on_busy(c_seg_next
);
2356 lck_mtx_lock_spin_always(c_list_lock
);
2360 /* grab that segment */
2361 C_SEG_BUSY(c_seg_next
);
2363 if (c_seg_do_minor_compaction_and_unlock(c_seg_next
, FALSE
, TRUE
, TRUE
)) {
2365 * found an empty c_segment and freed it
2366 * so we can't continue to use c_seg_next
2368 c_seg_major_compact_stats
.count_of_freed_segs
++;
2372 /* unlock the list ... */
2373 lck_mtx_unlock_always(c_list_lock
);
2375 /* do the major compaction */
2377 keep_compacting
= c_seg_major_compact(c_seg
, c_seg_next
);
2379 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2381 lck_mtx_lock_spin_always(&c_seg_next
->c_lock
);
2383 * run a minor compaction on the donor segment
2384 * since we pulled at least some of it's
2385 * data into our target... if we've emptied
2386 * it, now is a good time to free it which
2387 * c_seg_minor_compaction_and_unlock also takes care of
2389 * by passing TRUE, we ask for c_busy to be cleared
2390 * and c_wanted to be taken care of
2392 if (c_seg_minor_compaction_and_unlock(c_seg_next
, TRUE
))
2393 c_seg_major_compact_stats
.count_of_freed_segs
++;
2395 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2397 /* relock the list */
2398 lck_mtx_lock_spin_always(c_list_lock
);
2400 } /* major compaction */
2402 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2404 assert(c_seg
->c_busy
);
2405 assert(!c_seg
->c_on_minorcompact_q
);
2407 if (vm_swap_up
== TRUE
) {
2409 * This mode of putting a generic c_seg on the swapout list is
2410 * only supported when we have general swap ON i.e.
2411 * we compress pages into c_segs as we process them off
2412 * the paging queues in vm_pageout_scan().
2414 if (COMPRESSED_PAGER_IS_SWAPBACKED
)
2415 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2417 if ((vm_swapout_ripe_segments
== TRUE
&& c_overage_swapped_count
< c_overage_swapped_limit
)) {
2419 * we are running compressor sweeps with swap-behind
2420 * make sure the c_seg has aged enough before swapping it
2423 if ((now
- c_seg
->c_creation_ts
) >= vm_ripe_target_age
) {
2424 c_seg
->c_overage_swap
= TRUE
;
2425 c_overage_swapped_count
++;
2426 c_seg_switch_state(c_seg
, C_ON_SWAPOUT_Q
, FALSE
);
2431 if (c_seg
->c_state
== C_ON_AGE_Q
) {
2433 * this c_seg didn't get moved to the swapout queue
2434 * so we need to move it out of the way...
2435 * we just did a major compaction on it so put it
2438 c_seg_switch_state(c_seg
, C_ON_MAJORCOMPACT_Q
, FALSE
);
2440 c_seg_major_compact_stats
.wasted_space_in_swapouts
+= C_SEG_BUFSIZE
- c_seg
->c_bytes_used
;
2441 c_seg_major_compact_stats
.count_of_swapouts
++;
2443 C_SEG_WAKEUP_DONE(c_seg
);
2445 lck_mtx_unlock_always(&c_seg
->c_lock
);
2447 if (c_swapout_count
) {
2448 lck_mtx_unlock_always(c_list_lock
);
2450 thread_wakeup((event_t
)&c_swapout_list_head
);
2452 lck_mtx_lock_spin_always(c_list_lock
);
2459 c_seg_allocate(c_segment_t
*current_chead
)
2463 int size_to_populate
;
2465 if (vm_compressor_low_on_space())
2466 vm_compressor_take_paging_space_action();
2468 if ( (c_seg
= *current_chead
) == NULL
) {
2471 lck_mtx_lock_spin_always(c_list_lock
);
2473 while (c_segments_busy
== TRUE
) {
2474 assert_wait((event_t
) (&c_segments_busy
), THREAD_UNINT
);
2476 lck_mtx_unlock_always(c_list_lock
);
2478 thread_block(THREAD_CONTINUE_NULL
);
2480 lck_mtx_lock_spin_always(c_list_lock
);
2482 if (c_free_segno_head
== (uint32_t)-1) {
2483 uint32_t c_segments_available_new
;
2485 if (c_segments_available
>= c_segments_limit
|| c_segment_pages_compressed
>= c_segment_pages_compressed_limit
) {
2486 lck_mtx_unlock_always(c_list_lock
);
2490 c_segments_busy
= TRUE
;
2491 lck_mtx_unlock_always(c_list_lock
);
2493 kernel_memory_populate(kernel_map
, (vm_offset_t
)c_segments_next_page
,
2494 PAGE_SIZE
, KMA_KOBJECT
, VM_KERN_MEMORY_COMPRESSOR
);
2495 c_segments_next_page
+= PAGE_SIZE
;
2497 c_segments_available_new
= c_segments_available
+ C_SEGMENTS_PER_PAGE
;
2499 if (c_segments_available_new
> c_segments_limit
)
2500 c_segments_available_new
= c_segments_limit
;
2502 for (c_segno
= c_segments_available
+ 1; c_segno
< c_segments_available_new
; c_segno
++)
2503 c_segments
[c_segno
- 1].c_segno
= c_segno
;
2505 lck_mtx_lock_spin_always(c_list_lock
);
2507 c_segments
[c_segno
- 1].c_segno
= c_free_segno_head
;
2508 c_free_segno_head
= c_segments_available
;
2509 c_segments_available
= c_segments_available_new
;
2511 c_segments_busy
= FALSE
;
2512 thread_wakeup((event_t
) (&c_segments_busy
));
2514 c_segno
= c_free_segno_head
;
2515 assert(c_segno
>= 0 && c_segno
< c_segments_limit
);
2517 c_free_segno_head
= c_segments
[c_segno
].c_segno
;
2520 * do the rest of the bookkeeping now while we're still behind
2521 * the list lock and grab our generation id now into a local
2522 * so that we can install it once we have the c_seg allocated
2525 if (c_segment_count
> c_segment_count_max
)
2526 c_segment_count_max
= c_segment_count
;
2528 lck_mtx_unlock_always(c_list_lock
);
2530 c_seg
= (c_segment_t
)zalloc(compressor_segment_zone
);
2531 bzero((char *)c_seg
, sizeof(struct c_segment
));
2533 c_seg
->c_store
.c_buffer
= (int32_t *)C_SEG_BUFFER_ADDRESS(c_segno
);
2535 #if __i386__ || __x86_64__
2536 lck_mtx_init(&c_seg
->c_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
2537 #else /* __i386__ || __x86_64__ */
2538 lck_spin_init(&c_seg
->c_lock
, &vm_compressor_lck_grp
, &vm_compressor_lck_attr
);
2539 #endif /* __i386__ || __x86_64__ */
2541 c_seg
->c_state
= C_IS_EMPTY
;
2542 c_seg
->c_firstemptyslot
= C_SLOT_MAX_INDEX
;
2543 c_seg
->c_mysegno
= c_segno
;
2545 lck_mtx_lock_spin_always(c_list_lock
);
2547 c_seg_switch_state(c_seg
, C_IS_FILLING
, FALSE
);
2548 c_segments
[c_segno
].c_seg
= c_seg
;
2549 lck_mtx_unlock_always(c_list_lock
);
2551 *current_chead
= c_seg
;
2553 c_seg_alloc_nextslot(c_seg
);
2555 size_to_populate
= C_SEG_ALLOCSIZE
- C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
);
2557 if (size_to_populate
) {
2559 min_needed
= PAGE_SIZE
+ (C_SEG_ALLOCSIZE
- C_SEG_BUFSIZE
);
2561 if (C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
) < (unsigned) min_needed
) {
2563 if (size_to_populate
> C_SEG_MAX_POPULATE_SIZE
)
2564 size_to_populate
= C_SEG_MAX_POPULATE_SIZE
;
2566 kernel_memory_populate(kernel_map
,
2567 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[c_seg
->c_populated_offset
],
2570 VM_KERN_MEMORY_COMPRESSOR
);
2572 size_to_populate
= 0;
2574 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2576 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2578 if (size_to_populate
)
2579 c_seg
->c_populated_offset
+= C_SEG_BYTES_TO_OFFSET(size_to_populate
);
2586 c_current_seg_filled(c_segment_t c_seg
, c_segment_t
*current_chead
)
2588 uint32_t unused_bytes
;
2589 uint32_t offset_to_depopulate
;
2590 int new_state
= C_ON_AGE_Q
;
2594 unused_bytes
= trunc_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
- c_seg
->c_nextoffset
));
2598 offset_to_depopulate
= C_SEG_BYTES_TO_OFFSET(round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_nextoffset
)));
2601 * release the extra physical page(s) at the end of the segment
2603 lck_mtx_unlock_always(&c_seg
->c_lock
);
2605 kernel_memory_depopulate(
2607 (vm_offset_t
) &c_seg
->c_store
.c_buffer
[offset_to_depopulate
],
2611 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2613 c_seg
->c_populated_offset
= offset_to_depopulate
;
2615 assert(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
) <= C_SEG_BUFSIZE
);
2618 if (current_chead
== (c_segment_t
*)&freezer_chead
&& DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
&&
2619 c_freezer_swapout_count
< VM_MAX_FREEZER_CSEG_SWAP_COUNT
) {
2620 new_state
= C_ON_SWAPOUT_Q
;
2622 #endif /* CONFIG_FREEZE */
2624 clock_get_system_nanotime(&sec
, &nsec
);
2625 c_seg
->c_creation_ts
= (uint32_t)sec
;
2627 lck_mtx_lock_spin_always(c_list_lock
);
2630 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
2631 c_freezer_swapout_count
++;
2632 #endif /* CONFIG_FREEZE */
2634 c_seg
->c_generation_id
= c_generation_id
++;
2635 c_seg_switch_state(c_seg
, new_state
, FALSE
);
2637 lck_mtx_unlock_always(c_list_lock
);
2640 if (c_seg
->c_state
== C_ON_SWAPOUT_Q
)
2641 thread_wakeup((event_t
)&c_swapout_list_head
);
2642 #endif /* CONFIG_FREEZE */
2644 if (c_seg
->c_state
== C_ON_AGE_Q
&& C_SEG_UNUSED_BYTES(c_seg
) >= PAGE_SIZE
)
2645 c_seg_need_delayed_compaction(c_seg
);
2647 *current_chead
= NULL
;
2651 * returns with c_seg locked
2654 c_seg_swapin_requeue(c_segment_t c_seg
, boolean_t has_data
)
2659 clock_get_system_nanotime(&sec
, &nsec
);
2661 lck_mtx_lock_spin_always(c_list_lock
);
2662 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2664 c_seg
->c_busy_swapping
= 0;
2666 if (c_seg
->c_overage_swap
== TRUE
) {
2667 c_overage_swapped_count
--;
2668 c_seg
->c_overage_swap
= FALSE
;
2670 if (has_data
== TRUE
) {
2671 c_seg_switch_state(c_seg
, C_ON_SWAPPEDIN_Q
, FALSE
);
2673 c_seg
->c_store
.c_buffer
= (int32_t*) NULL
;
2674 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
2676 c_seg_switch_state(c_seg
, C_ON_BAD_Q
, FALSE
);
2678 c_seg
->c_swappedin_ts
= (uint32_t)sec
;
2680 lck_mtx_unlock_always(c_list_lock
);
2686 * c_seg has to be locked and is returned locked.
2687 * PAGE_REPLACMENT_DISALLOWED has to be TRUE on entry and is returned TRUE
2691 c_seg_swapin(c_segment_t c_seg
, boolean_t force_minor_compaction
)
2693 vm_offset_t addr
= 0;
2694 uint32_t io_size
= 0;
2697 assert(C_SEG_IS_ONDISK(c_seg
));
2699 #if !CHECKSUM_THE_SWAP
2700 c_seg_trim_tail(c_seg
);
2702 io_size
= round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
));
2703 f_offset
= c_seg
->c_store
.c_swap_handle
;
2706 c_seg
->c_busy_swapping
= 1;
2707 lck_mtx_unlock_always(&c_seg
->c_lock
);
2709 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2711 addr
= (vm_offset_t
)C_SEG_BUFFER_ADDRESS(c_seg
->c_mysegno
);
2713 kernel_memory_populate(kernel_map
, addr
, io_size
, KMA_COMPRESSOR
, VM_KERN_MEMORY_COMPRESSOR
);
2715 if (vm_swap_get(addr
, f_offset
, io_size
) != KERN_SUCCESS
) {
2716 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2718 kernel_memory_depopulate(kernel_map
, addr
, io_size
, KMA_COMPRESSOR
);
2720 c_seg_swapin_requeue(c_seg
, FALSE
);
2722 c_seg
->c_store
.c_buffer
= (int32_t*) addr
;
2724 vm_swap_decrypt(c_seg
);
2725 #endif /* ENCRYPTED_SWAP */
2727 #if CHECKSUM_THE_SWAP
2728 if (c_seg
->cseg_swap_size
!= io_size
)
2729 panic("swapin size doesn't match swapout size");
2731 if (c_seg
->cseg_hash
!= hash_string((char*) c_seg
->c_store
.c_buffer
, (int)io_size
)) {
2732 panic("c_seg_swapin - Swap hash mismatch\n");
2734 #endif /* CHECKSUM_THE_SWAP */
2736 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2738 if (force_minor_compaction
== TRUE
) {
2739 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
2741 c_seg_minor_compaction_and_unlock(c_seg
, FALSE
);
2743 OSAddAtomic64(c_seg
->c_bytes_used
, &compressor_bytes_used
);
2745 c_seg_swapin_requeue(c_seg
, TRUE
);
2747 C_SEG_WAKEUP_DONE(c_seg
);
2752 c_segment_sv_hash_drop_ref(int hash_indx
)
2754 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
2758 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_indx
].he_record
;
2760 n_sv_he
.he_ref
= o_sv_he
.he_ref
- 1;
2761 n_sv_he
.he_data
= o_sv_he
.he_data
;
2763 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
) {
2764 if (n_sv_he
.he_ref
== 0)
2765 OSAddAtomic(-1, &c_segment_svp_in_hash
);
2773 c_segment_sv_hash_insert(uint32_t data
)
2777 struct c_sv_hash_entry o_sv_he
, n_sv_he
;
2778 boolean_t got_ref
= FALSE
;
2781 OSAddAtomic(1, &c_segment_svp_zero_compressions
);
2783 OSAddAtomic(1, &c_segment_svp_nonzero_compressions
);
2785 hash_sindx
= data
& C_SV_HASH_MASK
;
2787 for (misses
= 0; misses
< C_SV_HASH_MAX_MISS
; misses
++)
2789 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
2791 while (o_sv_he
.he_data
== data
|| o_sv_he
.he_ref
== 0) {
2792 n_sv_he
.he_ref
= o_sv_he
.he_ref
+ 1;
2793 n_sv_he
.he_data
= data
;
2795 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
) {
2796 if (n_sv_he
.he_ref
== 1)
2797 OSAddAtomic(1, &c_segment_svp_in_hash
);
2801 o_sv_he
.he_record
= c_segment_sv_hash_table
[hash_sindx
].he_record
;
2803 if (got_ref
== TRUE
)
2807 if (hash_sindx
== C_SV_HASH_SIZE
)
2810 if (got_ref
== FALSE
)
2813 return (hash_sindx
);
2817 #if RECORD_THE_COMPRESSED_DATA
2820 c_compressed_record_data(char *src
, int c_size
)
2822 if ((c_compressed_record_cptr
+ c_size
+ 4) >= c_compressed_record_ebuf
)
2823 panic("c_compressed_record_cptr >= c_compressed_record_ebuf");
2825 *(int *)((void *)c_compressed_record_cptr
) = c_size
;
2827 c_compressed_record_cptr
+= 4;
2829 memcpy(c_compressed_record_cptr
, src
, c_size
);
2830 c_compressed_record_cptr
+= c_size
;
2836 c_compress_page(char *src
, c_slot_mapping_t slot_ptr
, c_segment_t
*current_chead
, char *scratch_buf
)
2839 int c_rounded_size
= 0;
2844 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_START
, *current_chead
, 0, 0, 0, 0);
2846 if ((c_seg
= c_seg_allocate(current_chead
)) == NULL
)
2849 * returns with c_seg lock held
2850 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
2851 * c_nextslot has been allocated and
2852 * c_store.c_buffer populated
2854 assert(c_seg
->c_state
== C_IS_FILLING
);
2856 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_seg
->c_nextslot
);
2858 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(slot_ptr
);
2859 assert(slot_ptr
== (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(cs
));
2861 cs
->c_offset
= c_seg
->c_nextoffset
;
2863 max_csize
= C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)cs
->c_offset
);
2865 if (max_csize
> PAGE_SIZE
)
2866 max_csize
= PAGE_SIZE
;
2868 #if CHECKSUM_THE_DATA
2869 cs
->c_hash_data
= hash_string(src
, PAGE_SIZE
);
2872 c_size
= WKdm_compress_new((const WK_word
*)(uintptr_t)src
, (WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
2873 (WK_word
*)(uintptr_t)scratch_buf
, max_csize
- 4);
2874 assert(c_size
<= (max_csize
- 4) && c_size
>= -1);
2878 if (max_csize
< PAGE_SIZE
) {
2879 c_current_seg_filled(c_seg
, current_chead
);
2880 assert(*current_chead
== NULL
);
2882 lck_mtx_unlock_always(&c_seg
->c_lock
);
2884 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2889 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
2891 OSAddAtomic(1, &c_segment_noncompressible_pages
);
2893 } else if (c_size
== 0) {
2897 * special case - this is a page completely full of a single 32 bit value
2899 hash_index
= c_segment_sv_hash_insert(*(uint32_t *)(uintptr_t)src
);
2901 if (hash_index
!= -1) {
2902 slot_ptr
->s_cindx
= hash_index
;
2903 slot_ptr
->s_cseg
= C_SV_CSEG_ID
;
2905 OSAddAtomic(1, &c_segment_svp_hash_succeeded
);
2906 #if RECORD_THE_COMPRESSED_DATA
2907 c_compressed_record_data(src
, 4);
2909 goto sv_compression
;
2913 memcpy(&c_seg
->c_store
.c_buffer
[cs
->c_offset
], src
, c_size
);
2915 OSAddAtomic(1, &c_segment_svp_hash_failed
);
2918 #if RECORD_THE_COMPRESSED_DATA
2919 c_compressed_record_data((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
2922 #if CHECKSUM_THE_COMPRESSED_DATA
2923 cs
->c_hash_compressed_data
= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
);
2925 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
2927 PACK_C_SIZE(cs
, c_size
);
2928 c_seg
->c_bytes_used
+= c_rounded_size
;
2929 c_seg
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
2931 slot_ptr
->s_cindx
= c_seg
->c_nextslot
++;
2932 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
2933 slot_ptr
->s_cseg
= c_seg
->c_mysegno
+ 1;
2936 if (c_seg
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
2937 c_current_seg_filled(c_seg
, current_chead
);
2938 assert(*current_chead
== NULL
);
2940 lck_mtx_unlock_always(&c_seg
->c_lock
);
2942 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
2944 #if RECORD_THE_COMPRESSED_DATA
2945 if ((c_compressed_record_cptr
- c_compressed_record_sbuf
) >= C_SEG_ALLOCSIZE
) {
2946 c_compressed_record_write(c_compressed_record_sbuf
, (int)(c_compressed_record_cptr
- c_compressed_record_sbuf
));
2947 c_compressed_record_cptr
= c_compressed_record_sbuf
;
2951 OSAddAtomic64(c_size
, &c_segment_compressed_bytes
);
2952 OSAddAtomic64(c_rounded_size
, &compressor_bytes_used
);
2954 OSAddAtomic64(PAGE_SIZE
, &c_segment_input_bytes
);
2956 OSAddAtomic(1, &c_segment_pages_compressed
);
2957 OSAddAtomic(1, &sample_period_compression_count
);
2959 KERNEL_DEBUG(0xe0400000 | DBG_FUNC_END
, *current_chead
, c_size
, c_segment_input_bytes
, c_segment_compressed_bytes
, 0);
2966 c_decompress_page(char *dst
, volatile c_slot_mapping_t slot_ptr
, int flags
, int *zeroslot
)
2974 boolean_t need_unlock
= TRUE
;
2975 boolean_t consider_defragmenting
= FALSE
;
2976 boolean_t kdp_mode
= FALSE
;
2978 if (flags
& C_KDP
) {
2980 panic("C_KDP passed to decompress page from outside of debugger context");
2983 assert((flags
& C_KEEP
) == C_KEEP
);
2984 assert((flags
& C_DONT_BLOCK
) == C_DONT_BLOCK
);
2986 if ((flags
& (C_DONT_BLOCK
| C_KEEP
)) != (C_DONT_BLOCK
| C_KEEP
)) {
2995 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
2997 if (kdp_lck_rw_lock_is_acquired_exclusive(&c_master_lock
)) {
3004 * if hibernation is enabled, it indicates (via a call
3005 * to 'vm_decompressor_lock' that no further
3006 * decompressions are allowed once it reaches
3007 * the point of flushing all of the currently dirty
3008 * anonymous memory through the compressor and out
3009 * to disk... in this state we allow freeing of compressed
3010 * pages and must honor the C_DONT_BLOCK case
3012 if (dst
&& decompressions_blocked
== TRUE
) {
3013 if (flags
& C_DONT_BLOCK
) {
3016 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3023 * it's safe to atomically assert and block behind the
3024 * lock held in shared mode because "decompressions_blocked" is
3025 * only set and cleared and the thread_wakeup done when the lock
3026 * is held exclusively
3028 assert_wait((event_t
)&decompressions_blocked
, THREAD_UNINT
);
3030 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3032 thread_block(THREAD_CONTINUE_NULL
);
3037 /* s_cseg is actually "segno+1" */
3038 c_seg
= c_segments
[slot_ptr
->s_cseg
- 1].c_seg
;
3041 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3043 if (kdp_lck_mtx_lock_spin_is_acquired(&c_seg
->c_lock
)) {
3048 assert(c_seg
->c_state
!= C_IS_EMPTY
&& c_seg
->c_state
!= C_IS_FREE
);
3050 if (flags
& C_DONT_BLOCK
) {
3051 if (c_seg
->c_busy
|| (C_SEG_IS_ONDISK(c_seg
) && dst
)) {
3058 if (c_seg
->c_busy
) {
3060 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3062 c_seg_wait_on_busy(c_seg
);
3066 c_indx
= slot_ptr
->s_cindx
;
3068 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3070 c_size
= UNPACK_C_SIZE(cs
);
3072 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3075 uint32_t age_of_cseg
;
3076 clock_sec_t cur_ts_sec
;
3077 clock_nsec_t cur_ts_nsec
;
3079 if (C_SEG_IS_ONDISK(c_seg
)) {
3080 assert(kdp_mode
== FALSE
);
3081 c_seg_swapin(c_seg
, FALSE
);
3085 if (c_seg
->c_state
== C_ON_BAD_Q
) {
3086 assert(c_seg
->c_store
.c_buffer
== NULL
);
3089 goto c_seg_invalid_data
;
3091 #if CHECKSUM_THE_COMPRESSED_DATA
3092 if (cs
->c_hash_compressed_data
!= hash_string((char *)&c_seg
->c_store
.c_buffer
[cs
->c_offset
], c_size
))
3093 panic("compressed data doesn't match original");
3095 if (c_rounded_size
== PAGE_SIZE
) {
3097 * page wasn't compressible... just copy it out
3099 memcpy(dst
, &c_seg
->c_store
.c_buffer
[cs
->c_offset
], PAGE_SIZE
);
3100 } else if (c_size
== 4) {
3105 * page was populated with a single value
3106 * that didn't fit into our fast hash
3107 * so we packed it in as a single non-compressed value
3108 * that we need to populate the page with
3110 dptr
= (int32_t *)(uintptr_t)dst
;
3111 data
= *(int32_t *)(&c_seg
->c_store
.c_buffer
[cs
->c_offset
]);
3113 memset_word(dptr
, data
, PAGE_SIZE
/ sizeof(int32_t));
3118 for (i
= 0; i
< (int)(PAGE_SIZE
/ sizeof(int32_t)); i
++)
3128 * we're behind the c_seg lock held in spin mode
3129 * which means pre-emption is disabled... therefore
3130 * the following sequence is atomic and safe
3132 my_cpu_no
= cpu_number();
3134 assert(my_cpu_no
< compressor_cpus
);
3136 scratch_buf
= &compressor_scratch_bufs
[my_cpu_no
* WKdm_SCRATCH_BUF_SIZE
];
3138 scratch_buf
= kdp_compressor_scratch_buf
;
3140 WKdm_decompress_new((WK_word
*)(uintptr_t)&c_seg
->c_store
.c_buffer
[cs
->c_offset
],
3141 (WK_word
*)(uintptr_t)dst
, (WK_word
*)(uintptr_t)scratch_buf
, c_size
);
3144 #if CHECKSUM_THE_DATA
3145 if (cs
->c_hash_data
!= hash_string(dst
, PAGE_SIZE
))
3146 panic("decompressed data doesn't match original");
3148 if (c_seg
->c_swappedin_ts
== 0 && !kdp_mode
) {
3150 clock_get_system_nanotime(&cur_ts_sec
, &cur_ts_nsec
);
3152 age_of_cseg
= (uint32_t)cur_ts_sec
- c_seg
->c_creation_ts
;
3154 if (age_of_cseg
< DECOMPRESSION_SAMPLE_MAX_AGE
)
3155 OSAddAtomic(1, &age_of_decompressions_during_sample_period
[age_of_cseg
]);
3157 OSAddAtomic(1, &overage_decompressions_during_sample_period
);
3159 OSAddAtomic(1, &sample_period_decompression_count
);
3164 if (flags
& C_KEEP
) {
3169 assert(kdp_mode
== FALSE
);
3170 c_seg
->c_bytes_unused
+= c_rounded_size
;
3171 c_seg
->c_bytes_used
-= c_rounded_size
;
3174 if (c_indx
< c_seg
->c_firstemptyslot
)
3175 c_seg
->c_firstemptyslot
= c_indx
;
3177 OSAddAtomic(-1, &c_segment_pages_compressed
);
3179 if (c_seg
->c_state
!= C_ON_BAD_Q
&& !(C_SEG_IS_ONDISK(c_seg
))) {
3181 * C_SEG_IS_ONDISK == TRUE can occur when we're doing a
3182 * free of a compressed page (i.e. dst == NULL)
3184 OSAddAtomic64(-c_rounded_size
, &compressor_bytes_used
);
3186 if (c_seg
->c_state
!= C_IS_FILLING
) {
3187 if (c_seg
->c_bytes_used
== 0) {
3188 if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3189 int pages_populated
;
3191 pages_populated
= (round_page_32(C_SEG_OFFSET_TO_BYTES(c_seg
->c_populated_offset
))) / PAGE_SIZE
;
3192 c_seg
->c_populated_offset
= C_SEG_BYTES_TO_OFFSET(0);
3194 if (pages_populated
) {
3196 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3197 assert(c_seg
->c_store
.c_buffer
!= NULL
);
3200 lck_mtx_unlock_always(&c_seg
->c_lock
);
3202 kernel_memory_depopulate(kernel_map
, (vm_offset_t
) c_seg
->c_store
.c_buffer
, pages_populated
* PAGE_SIZE
, KMA_COMPRESSOR
);
3204 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3205 C_SEG_WAKEUP_DONE(c_seg
);
3207 if (!c_seg
->c_on_minorcompact_q
)
3208 c_seg_need_delayed_compaction(c_seg
);
3210 assert(c_seg
->c_state
== C_ON_SWAPPEDOUTSPARSE_Q
);
3212 } else if (c_seg
->c_on_minorcompact_q
) {
3214 assert(c_seg
->c_state
!= C_ON_BAD_Q
);
3216 if (C_SEG_SHOULD_MINORCOMPACT(c_seg
)) {
3217 c_seg_try_minor_compaction_and_unlock(c_seg
);
3218 need_unlock
= FALSE
;
3220 } else if ( !(C_SEG_IS_ONDISK(c_seg
))) {
3222 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
) {
3223 c_seg_need_delayed_compaction(c_seg
);
3225 } else if (c_seg
->c_state
!= C_ON_SWAPPEDOUTSPARSE_Q
&& C_SEG_ONDISK_IS_SPARSE(c_seg
)) {
3227 c_seg_move_to_sparse_list(c_seg
);
3228 consider_defragmenting
= TRUE
;
3236 if (need_unlock
== TRUE
)
3237 lck_mtx_unlock_always(&c_seg
->c_lock
);
3239 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3241 if (consider_defragmenting
== TRUE
)
3242 vm_swap_consider_defragmenting();
3250 vm_compressor_get(ppnum_t pn
, int *slot
, int flags
)
3252 c_slot_mapping_t slot_ptr
;
3258 dst
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3260 #error "unsupported architecture"
3262 slot_ptr
= (c_slot_mapping_t
)slot
;
3264 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3269 * page was populated with a single value
3270 * that found a home in our hash table
3271 * grab that value from the hash and populate the page
3272 * that we need to populate the page with
3274 dptr
= (int32_t *)(uintptr_t)dst
;
3275 data
= c_segment_sv_hash_table
[slot_ptr
->s_cindx
].he_data
;
3277 memset_word(dptr
, data
, PAGE_SIZE
/ sizeof(int32_t));
3282 for (i
= 0; i
< (int)(PAGE_SIZE
/ sizeof(int32_t)); i
++)
3286 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3288 if ( !(flags
& C_KEEP
)) {
3289 OSAddAtomic(-1, &c_segment_pages_compressed
);
3293 OSAddAtomic(1, &c_segment_svp_nonzero_decompressions
);
3295 OSAddAtomic(1, &c_segment_svp_zero_decompressions
);
3300 retval
= c_decompress_page(dst
, slot_ptr
, flags
, &zeroslot
);
3303 * zeroslot will be set to 0 by c_decompress_page if (flags & C_KEEP)
3304 * or (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be TRUE
3310 * returns 0 if we successfully decompressed a page from a segment already in memory
3311 * returns 1 if we had to first swap in the segment, before successfully decompressing the page
3312 * returns -1 if we encountered an error swapping in the segment - decompression failed
3313 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' or 'C_SEG_IS_ONDISK' to be true
3320 vm_compressor_free(int *slot
, int flags
)
3322 c_slot_mapping_t slot_ptr
;
3326 assert(flags
== 0 || flags
== C_DONT_BLOCK
);
3328 slot_ptr
= (c_slot_mapping_t
)slot
;
3330 if (slot_ptr
->s_cseg
== C_SV_CSEG_ID
) {
3332 c_segment_sv_hash_drop_ref(slot_ptr
->s_cindx
);
3333 OSAddAtomic(-1, &c_segment_pages_compressed
);
3338 retval
= c_decompress_page(NULL
, slot_ptr
, flags
, &zeroslot
);
3340 * returns 0 if we successfully freed the specified compressed page
3341 * returns -2 if (flags & C_DONT_BLOCK) and we found 'c_busy' set
3347 assert(retval
== -2);
3354 vm_compressor_put(ppnum_t pn
, int *slot
, void **current_chead
, char *scratch_buf
)
3360 src
= PHYSMAP_PTOV((uint64_t)pn
<< (uint64_t)PAGE_SHIFT
);
3362 #error "unsupported architecture"
3364 retval
= c_compress_page(src
, (c_slot_mapping_t
)slot
, (c_segment_t
*)current_chead
, scratch_buf
);
3370 vm_compressor_transfer(
3374 c_slot_mapping_t dst_slot
, src_slot
;
3379 src_slot
= (c_slot_mapping_t
) src_slot_p
;
3381 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3382 *dst_slot_p
= *src_slot_p
;
3386 dst_slot
= (c_slot_mapping_t
) dst_slot_p
;
3388 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3389 /* get segment for src_slot */
3390 c_seg
= c_segments
[src_slot
->s_cseg
-1].c_seg
;
3392 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3393 /* wait if it's busy */
3394 if (c_seg
->c_busy
&& !c_seg
->c_busy_swapping
) {
3395 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3396 c_seg_wait_on_busy(c_seg
);
3399 /* find the c_slot */
3400 c_indx
= src_slot
->s_cindx
;
3401 cs
= C_SEG_SLOT_FROM_INDEX(c_seg
, c_indx
);
3402 /* point the c_slot back to dst_slot instead of src_slot */
3403 cs
->c_packed_ptr
= C_SLOT_PACK_PTR(dst_slot
);
3405 *dst_slot_p
= *src_slot_p
;
3407 lck_mtx_unlock_always(&c_seg
->c_lock
);
3408 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3413 int freezer_finished_filling
= 0;
3416 vm_compressor_finished_filling(
3417 void **current_chead
)
3421 if ((c_seg
= *(c_segment_t
*)current_chead
) == NULL
)
3424 assert(c_seg
->c_state
== C_IS_FILLING
);
3426 lck_mtx_lock_spin_always(&c_seg
->c_lock
);
3428 c_current_seg_filled(c_seg
, (c_segment_t
*)current_chead
);
3430 lck_mtx_unlock_always(&c_seg
->c_lock
);
3432 freezer_finished_filling
++;
3437 * This routine is used to transfer the compressed chunks from
3438 * the c_seg/cindx pointed to by slot_p into a new c_seg headed
3439 * by the current_chead and a new cindx within that c_seg.
3441 * Currently, this routine is only used by the "freezer backed by
3442 * compressor with swap" mode to create a series of c_segs that
3443 * only contain compressed data belonging to one task. So, we
3444 * move a task's previously compressed data into a set of new
3445 * c_segs which will also hold the task's yet to be compressed data.
3449 vm_compressor_relocate(
3450 void **current_chead
,
3453 c_slot_mapping_t slot_ptr
;
3454 c_slot_mapping_t src_slot
;
3455 uint32_t c_rounded_size
;
3461 c_segment_t c_seg_dst
= NULL
;
3462 c_segment_t c_seg_src
= NULL
;
3463 kern_return_t kr
= KERN_SUCCESS
;
3466 src_slot
= (c_slot_mapping_t
) slot_p
;
3468 if (src_slot
->s_cseg
== C_SV_CSEG_ID
) {
3470 * no need to relocate... this is a page full of a single
3471 * value which is hashed to a single entry not contained
3478 c_seg_dst
= c_seg_allocate((c_segment_t
*)current_chead
);
3480 * returns with c_seg lock held
3481 * and PAGE_REPLACEMENT_DISALLOWED(TRUE)...
3482 * c_nextslot has been allocated and
3483 * c_store.c_buffer populated
3485 if (c_seg_dst
== NULL
) {
3487 * Out of compression segments?
3489 kr
= KERN_RESOURCE_SHORTAGE
;
3493 assert(c_seg_dst
->c_busy
== 0);
3495 C_SEG_BUSY(c_seg_dst
);
3497 dst_slot
= c_seg_dst
->c_nextslot
;
3499 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3502 c_seg_src
= c_segments
[src_slot
->s_cseg
- 1].c_seg
;
3504 assert(c_seg_dst
!= c_seg_src
);
3506 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3508 if (C_SEG_IS_ONDISK(c_seg_src
)) {
3511 * A "thaw" can mark a process as eligible for
3512 * another freeze cycle without bringing any of
3513 * its swapped out c_segs back from disk (because
3514 * that is done on-demand).
3516 * If the src c_seg we find for our pre-compressed
3517 * data is already on-disk, then we are dealing
3518 * with an app's data that is already packed and
3519 * swapped out. Don't do anything.
3522 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3524 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3531 if (c_seg_src
->c_busy
) {
3533 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3534 c_seg_wait_on_busy(c_seg_src
);
3538 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3543 C_SEG_BUSY(c_seg_src
);
3545 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3547 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3549 /* find the c_slot */
3550 c_indx
= src_slot
->s_cindx
;
3552 c_src
= C_SEG_SLOT_FROM_INDEX(c_seg_src
, c_indx
);
3554 c_size
= UNPACK_C_SIZE(c_src
);
3558 if (c_size
> (uint32_t)(C_SEG_BUFSIZE
- C_SEG_OFFSET_TO_BYTES((int32_t)c_seg_dst
->c_nextoffset
))) {
3560 * This segment is full. We need a new one.
3563 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3565 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3566 C_SEG_WAKEUP_DONE(c_seg_src
);
3567 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3571 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
3573 assert(c_seg_dst
->c_busy
);
3574 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
3575 assert(!c_seg_dst
->c_on_minorcompact_q
);
3577 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
3578 assert(*current_chead
== NULL
);
3580 C_SEG_WAKEUP_DONE(c_seg_dst
);
3582 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3586 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3591 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, c_seg_dst
->c_nextslot
);
3593 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
);
3595 c_rounded_size
= (c_size
+ C_SEG_OFFSET_ALIGNMENT_MASK
) & ~C_SEG_OFFSET_ALIGNMENT_MASK
;
3597 #if CHECKSUM_THE_DATA
3598 c_dst
->c_hash_data
= c_src
->c_hash_data
;
3600 #if CHECKSUM_THE_COMPRESSED_DATA
3601 c_dst
->c_hash_compressed_data
= c_src
->c_hash_compressed_data
;
3604 c_dst
->c_size
= c_src
->c_size
;
3605 c_dst
->c_packed_ptr
= c_src
->c_packed_ptr
;
3606 c_dst
->c_offset
= c_seg_dst
->c_nextoffset
;
3608 if (c_seg_dst
->c_firstemptyslot
== c_seg_dst
->c_nextslot
)
3609 c_seg_dst
->c_firstemptyslot
++;
3611 c_seg_dst
->c_nextslot
++;
3612 c_seg_dst
->c_bytes_used
+= c_rounded_size
;
3613 c_seg_dst
->c_nextoffset
+= C_SEG_BYTES_TO_OFFSET(c_rounded_size
);
3616 PACK_C_SIZE(c_src
, 0);
3618 c_seg_src
->c_bytes_used
-= c_rounded_size
;
3619 c_seg_src
->c_bytes_unused
+= c_rounded_size
;
3621 if (c_indx
< c_seg_src
->c_firstemptyslot
) {
3622 c_seg_src
->c_firstemptyslot
= c_indx
;
3625 c_dst
= C_SEG_SLOT_FROM_INDEX(c_seg_dst
, dst_slot
);
3627 PAGE_REPLACEMENT_ALLOWED(TRUE
);
3628 slot_ptr
= (c_slot_mapping_t
)C_SLOT_UNPACK_PTR(c_dst
);
3629 /* <csegno=0,indx=0> would mean "empty slot", so use csegno+1 */
3630 slot_ptr
->s_cseg
= c_seg_dst
->c_mysegno
+ 1;
3631 slot_ptr
->s_cindx
= dst_slot
;
3633 PAGE_REPLACEMENT_ALLOWED(FALSE
);
3638 lck_mtx_lock_spin_always(&c_seg_src
->c_lock
);
3640 C_SEG_WAKEUP_DONE(c_seg_src
);
3642 if (c_seg_src
->c_bytes_used
== 0 && c_seg_src
->c_state
!= C_IS_FILLING
) {
3643 if (!c_seg_src
->c_on_minorcompact_q
)
3644 c_seg_need_delayed_compaction(c_seg_src
);
3647 lck_mtx_unlock_always(&c_seg_src
->c_lock
);
3652 PAGE_REPLACEMENT_DISALLOWED(TRUE
);
3654 lck_mtx_lock_spin_always(&c_seg_dst
->c_lock
);
3656 if (c_seg_dst
->c_nextoffset
>= C_SEG_OFF_LIMIT
|| c_seg_dst
->c_nextslot
>= C_SLOT_MAX_INDEX
) {
3658 * Nearing or exceeded maximum slot and offset capacity.
3660 assert(c_seg_dst
->c_busy
);
3661 assert(c_seg_dst
->c_state
== C_IS_FILLING
);
3662 assert(!c_seg_dst
->c_on_minorcompact_q
);
3664 c_current_seg_filled(c_seg_dst
, (c_segment_t
*)current_chead
);
3665 assert(*current_chead
== NULL
);
3668 C_SEG_WAKEUP_DONE(c_seg_dst
);
3670 lck_mtx_unlock_always(&c_seg_dst
->c_lock
);
3674 PAGE_REPLACEMENT_DISALLOWED(FALSE
);
3679 #endif /* CONFIG_FREEZE */