2 * Copyright (c) 2006-2018 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@
30 #include <kern/sched_prim.h>
31 #include <kern/kalloc.h>
32 #include <kern/assert.h>
33 #include <kern/debug.h>
34 #include <kern/locks.h>
35 #include <kern/task.h>
36 #include <kern/thread.h>
37 #include <kern/host.h>
38 #include <kern/policy_internal.h>
39 #include <kern/thread_group.h>
41 #include <libkern/libkern.h>
42 #include <mach/coalition.h>
43 #include <mach/mach_time.h>
44 #include <mach/task.h>
45 #include <mach/host_priv.h>
46 #include <mach/mach_host.h>
48 #include <pexpert/pexpert.h>
49 #include <sys/coalition.h>
50 #include <sys/kern_event.h>
52 #include <sys/proc_info.h>
53 #include <sys/reason.h>
54 #include <sys/signal.h>
55 #include <sys/signalvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
61 #include <vm/vm_pageout.h>
62 #include <vm/vm_protos.h>
63 #include <mach/machine/sdt.h>
64 #include <libkern/section_keywords.h>
65 #include <stdatomic.h>
67 #include <IOKit/IOBSD.h>
70 #include <vm/vm_map.h>
71 #endif /* CONFIG_FREEZE */
73 #include <sys/kern_memorystatus.h>
74 #include <sys/kern_memorystatus_freeze.h>
75 #include <sys/kern_memorystatus_notify.h>
79 extern unsigned int memorystatus_available_pages
;
80 extern unsigned int memorystatus_available_pages_pressure
;
81 extern unsigned int memorystatus_available_pages_critical
;
82 extern unsigned int memorystatus_available_pages_critical_base
;
83 extern unsigned int memorystatus_available_pages_critical_idle_offset
;
85 #else /* CONFIG_JETSAM */
87 extern uint64_t memorystatus_available_pages
;
88 extern uint64_t memorystatus_available_pages_pressure
;
89 extern uint64_t memorystatus_available_pages_critical
;
91 #endif /* CONFIG_JETSAM */
93 unsigned int memorystatus_frozen_count
= 0;
94 unsigned int memorystatus_suspended_count
= 0;
95 unsigned long freeze_threshold_percentage
= 50;
99 lck_grp_attr_t
*freezer_lck_grp_attr
;
100 lck_grp_t
*freezer_lck_grp
;
101 static lck_mtx_t freezer_mutex
;
104 unsigned int memorystatus_freeze_threshold
= 0;
105 unsigned int memorystatus_freeze_pages_min
= 0;
106 unsigned int memorystatus_freeze_pages_max
= 0;
107 unsigned int memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
;
108 unsigned int memorystatus_freeze_daily_mb_max
= FREEZE_DAILY_MB_MAX_DEFAULT
;
109 uint64_t memorystatus_freeze_budget_pages_remaining
= 0; //remaining # of pages that can be frozen to disk
110 boolean_t memorystatus_freeze_degradation
= FALSE
; //protected by the freezer mutex. Signals we are in a degraded freeze mode.
112 unsigned int memorystatus_max_frozen_demotions_daily
= 0;
113 unsigned int memorystatus_thaw_count_demotion_threshold
= 0;
115 boolean_t memorystatus_freeze_enabled
= FALSE
;
116 int memorystatus_freeze_wakeup
= 0;
117 int memorystatus_freeze_jetsam_band
= 0; /* the jetsam band which will contain P_MEMSTAT_FROZEN processes */
119 #define MAX_XPC_SERVICE_PIDS 10 /* Max. # of XPC services per coalition we'll consider freezing. */
121 #ifdef XNU_KERNEL_PRIVATE
123 unsigned int memorystatus_frozen_processes_max
= 0;
124 unsigned int memorystatus_frozen_shared_mb
= 0;
125 unsigned int memorystatus_frozen_shared_mb_max
= 0;
126 unsigned int memorystatus_freeze_shared_mb_per_process_max
= 0; /* Max. MB allowed per process to be freezer-eligible. */
127 unsigned int memorystatus_freeze_private_shared_pages_ratio
= 2; /* Ratio of private:shared pages for a process to be freezer-eligible. */
128 unsigned int memorystatus_thaw_count
= 0; /* # of thaws in the current freezer interval */
129 uint64_t memorystatus_thaw_count_since_boot
= 0; /* The number of thaws since boot */
130 unsigned int memorystatus_refreeze_eligible_count
= 0; /* # of processes currently thawed i.e. have state on disk & in-memory */
132 /* Freezer counters collected for telemtry */
133 static struct memorystatus_freezer_stats_t
{
135 * # of processes that we've considered freezing.
136 * Used to normalize the error reasons below.
138 uint64_t mfs_process_considered_count
;
141 * The following counters track how many times we've failed to freeze
142 * a process because of a specific FREEZER_ERROR.
144 /* EXCESS_SHARED_MEMORY */
145 uint64_t mfs_error_excess_shared_memory_count
;
146 /* LOW_PRIVATE_SHARED_RATIO */
147 uint64_t mfs_error_low_private_shared_ratio_count
;
148 /* NO_COMPRESSOR_SPACE */
149 uint64_t mfs_error_no_compressor_space_count
;
151 uint64_t mfs_error_no_swap_space_count
;
152 /* pages < memorystatus_freeze_pages_min */
153 uint64_t mfs_error_below_min_pages_count
;
154 /* dasd determined it was unlikely to be relaunched. */
155 uint64_t mfs_error_low_probability_of_use_count
;
156 /* transient reasons (like inability to acquire a lock). */
157 uint64_t mfs_error_other_count
;
160 * # of times that we saw memorystatus_available_pages <= memorystatus_freeze_threshold.
161 * Used to normalize skipped_full_count and shared_mb_high_count.
163 uint64_t mfs_below_threshold_count
;
165 /* Skipped running the freezer because we were out of slots */
166 uint64_t mfs_skipped_full_count
;
168 /* Skipped running the freezer because we were over the shared mb limit*/
169 uint64_t mfs_skipped_shared_mb_high_count
;
172 * How many pages have not been sent to swap because they were in a shared object?
173 * This is being used to gather telemtry so we can understand the impact we'd have
174 * on our NAND budget if we did swap out these pages.
176 uint64_t mfs_shared_pages_skipped
;
179 * A running sum of the total number of bytes sent to NAND during
180 * refreeze operations since boot.
182 uint64_t mfs_bytes_refrozen
;
183 /* The number of refreeze operations since boot */
184 uint64_t mfs_refreeze_count
;
185 } memorystatus_freezer_stats
= {0};
187 #endif /* XNU_KERNEL_PRIVATE */
189 static inline boolean_t
memorystatus_can_freeze_processes(void);
190 static boolean_t
memorystatus_can_freeze(boolean_t
*memorystatus_freeze_swap_low
);
191 static boolean_t
memorystatus_is_process_eligible_for_freeze(proc_t p
);
192 static void memorystatus_freeze_thread(void *param __unused
, wait_result_t wr __unused
);
193 static void memorystatus_freeze_start_normal_throttle_interval(uint32_t new_budget
, mach_timespec_t start_ts
);
195 void memorystatus_disable_freeze(void);
198 static uint64_t memorystatus_freeze_pageouts
= 0;
201 #define DEGRADED_WINDOW_MINS (30)
202 #define NORMAL_WINDOW_MINS (24 * 60)
204 /* Protected by the freezer_mutex */
205 static throttle_interval_t throttle_intervals
[] = {
206 { DEGRADED_WINDOW_MINS
, 1, 0, 0, { 0, 0 }},
207 { NORMAL_WINDOW_MINS
, 1, 0, 0, { 0, 0 }},
209 throttle_interval_t
*degraded_throttle_window
= &throttle_intervals
[0];
210 throttle_interval_t
*normal_throttle_window
= &throttle_intervals
[1];
212 extern uint64_t vm_swap_get_free_space(void);
213 extern boolean_t
vm_swap_max_budget(uint64_t *);
214 extern int i_coal_jetsam_get_taskrole(coalition_t coal
, task_t task
);
216 static void memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed
);
217 static void memorystatus_demote_frozen_processes(boolean_t force_one
);
219 * Converts the freezer_error_code into a string and updates freezer error counts.
221 static void memorystatus_freezer_stringify_error(const int freezer_error_code
, char* buffer
, size_t len
);
223 static uint64_t memorystatus_freezer_thread_next_run_ts
= 0;
225 /* Sysctls needed for aggd stats */
227 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_frozen_count
, 0, "");
228 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_thaw_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_thaw_count
, 0, "");
229 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_thaw_count_since_boot
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_thaw_count_since_boot
, "");
230 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_pageouts
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freeze_pageouts
, "");
231 #if DEVELOPMENT || DEBUG
232 static int sysctl_memorystatus_freeze_budget_pages_remaining SYSCTL_HANDLER_ARGS
234 #pragma unused(arg1, arg2, oidp)
236 uint64_t new_budget
= memorystatus_freeze_budget_pages_remaining
;
237 mach_timespec_t now_ts
;
241 lck_mtx_lock(&freezer_mutex
);
243 error
= sysctl_io_number(req
, memorystatus_freeze_budget_pages_remaining
, sizeof(uint64_t), &new_budget
, &changed
);
245 /* Start a new interval with this budget. */
246 clock_get_system_nanotime(&sec
, &nsec
);
247 now_ts
.tv_sec
= (unsigned int)(MIN(sec
, UINT32_MAX
));
248 now_ts
.tv_nsec
= nsec
;
249 memorystatus_freeze_start_normal_throttle_interval((uint32_t) MIN(new_budget
, UINT32_MAX
), now_ts
);
250 /* Don't carry over any excess pageouts since we're forcing a new budget */
251 normal_throttle_window
->pageouts
= 0;
252 memorystatus_freeze_budget_pages_remaining
= normal_throttle_window
->max_pageouts
;
255 lck_mtx_unlock(&freezer_mutex
);
259 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_freeze_budget_pages_remaining
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0, &sysctl_memorystatus_freeze_budget_pages_remaining
, "Q", "");
260 #else /* DEVELOPMENT || DEBUG */
261 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_budget_pages_remaining
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freeze_budget_pages_remaining
, "");
262 #endif /* DEVELOPMENT || DEBUG */
263 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_excess_shared_memory_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_excess_shared_memory_count
, "");
264 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_low_private_shared_ratio_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_low_private_shared_ratio_count
, "");
265 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_no_compressor_space_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_no_compressor_space_count
, "");
266 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_no_swap_space_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_no_swap_space_count
, "");
267 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_below_min_pages_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_below_min_pages_count
, "");
268 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_low_probability_of_use_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_low_probability_of_use_count
, "");
269 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_error_other_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_error_other_count
, "");
270 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_process_considered_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_process_considered_count
, "");
271 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_below_threshold_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_below_threshold_count
, "");
272 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_skipped_full_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_skipped_full_count
, "");
273 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_skipped_shared_mb_high_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_skipped_shared_mb_high_count
, "");
274 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_shared_pages_skipped
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_shared_pages_skipped
, "");
275 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_bytes_refrozen
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_bytes_refrozen
, "");
276 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freezer_refreeze_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freezer_stats
.mfs_refreeze_count
, "");
280 * Calculates the hit rate for the freezer.
281 * The hit rate is defined as the percentage of procs that are currently in the
282 * freezer which we have thawed.
283 * A low hit rate means we're freezing bad candidates since they're not re-used.
285 static int sysctl_memorystatus_freezer_thaw_percentage SYSCTL_HANDLER_ARGS
287 #pragma unused(arg1, arg2)
288 size_t thaw_count
= 0, frozen_count
= 0;
289 int thaw_percentage
= 100;
290 unsigned int band
= (unsigned int) memorystatus_freeze_jetsam_band
;
291 proc_t p
= PROC_NULL
;
294 p
= memorystatus_get_first_proc_locked(&band
, FALSE
);
297 if (p
->p_memstat_state
& P_MEMSTAT_FROZEN
) {
298 if (p
->p_memstat_thaw_count
> 0) {
303 p
= memorystatus_get_next_proc_locked(&band
, p
, FALSE
);
306 if (frozen_count
> 0) {
307 assert(thaw_count
<= frozen_count
);
308 thaw_percentage
= (int)(100 * thaw_count
/ frozen_count
);
310 return sysctl_handle_int(oidp
, &thaw_percentage
, 0, req
);
312 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_freezer_thaw_percentage
, CTLTYPE_INT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, &sysctl_memorystatus_freezer_thaw_percentage
, "I", "");
314 #define FREEZER_ERROR_STRING_LENGTH 128
316 #if DEVELOPMENT || DEBUG
318 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_jetsam_band
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_jetsam_band
, 0, "");
319 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_daily_mb_max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_daily_mb_max
, 0, "");
320 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_degraded_mode
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_freeze_degradation
, 0, "");
321 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_threshold
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_threshold
, 0, "");
322 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_pages_min
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_pages_min
, 0, "");
323 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_pages_max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_pages_max
, 0, "");
324 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_refreeze_eligible_count
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_refreeze_eligible_count
, 0, "");
325 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_processes_max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_frozen_processes_max
, 0, "");
328 * Max. shared-anonymous memory in MB that can be held by frozen processes in the high jetsam band.
329 * "0" means no limit.
330 * Default is 10% of system-wide task limit.
333 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_shared_mb_max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_frozen_shared_mb_max
, 0, "");
334 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_shared_mb
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &memorystatus_frozen_shared_mb
, 0, "");
336 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_shared_mb_per_process_max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_shared_mb_per_process_max
, 0, "");
337 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_private_shared_pages_ratio
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_private_shared_pages_ratio
, 0, "");
339 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_min_processes
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_suspended_threshold
, 0, "");
342 * max. # of frozen process demotions we will allow in our daily cycle.
344 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_max_freeze_demotions_daily
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_max_frozen_demotions_daily
, 0, "");
346 * min # of thaws needed by a process to protect it from getting demoted into the IDLE band.
348 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_thaw_count_demotion_threshold
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_thaw_count_demotion_threshold
, 0, "");
350 boolean_t memorystatus_freeze_throttle_enabled
= TRUE
;
351 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_throttle_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_throttle_enabled
, 0, "");
354 * When set to true, this keeps frozen processes in the compressor pool in memory, instead of swapping them out to disk.
355 * Exposed via the sysctl kern.memorystatus_freeze_to_memory.
357 boolean_t memorystatus_freeze_to_memory
= FALSE
;
358 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_to_memory
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &memorystatus_freeze_to_memory
, 0, "");
360 #define VM_PAGES_FOR_ALL_PROCS (2)
363 * Manual trigger of freeze and thaw for dev / debug kernels only.
366 sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS
368 #pragma unused(arg1, arg2)
371 int freezer_error_code
= 0;
372 pid_t pid_list
[MAX_XPC_SERVICE_PIDS
];
374 coalition_t coal
= COALITION_NULL
;
376 if (memorystatus_freeze_enabled
== FALSE
) {
377 printf("sysctl_freeze: Freeze is DISABLED\n");
381 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
382 if (error
|| !req
->newptr
) {
386 if (pid
== VM_PAGES_FOR_ALL_PROCS
) {
387 vm_pageout_anonymous_pages();
392 lck_mtx_lock(&freezer_mutex
);
397 memorystatus_freezer_stats
.mfs_process_considered_count
++;
398 uint32_t purgeable
, wired
, clean
, dirty
, shared
;
399 uint32_t max_pages
= 0, state
= 0;
401 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
403 * Freezer backed by the compressor and swap file(s)
404 * will hold compressed data.
406 * Set the sysctl kern.memorystatus_freeze_to_memory to true to keep compressed data from
407 * being swapped out to disk. Note that this disables freezer swap support globally,
408 * not just for the process being frozen.
411 * We don't care about the global freezer budget or the process's (min/max) budget here.
412 * The freeze sysctl is meant to force-freeze a process.
414 * We also don't update any global or process stats on this path, so that the jetsam/ freeze
415 * logic remains unaffected. The tasks we're performing here are: freeze the process, set the
416 * P_MEMSTAT_FROZEN bit, and elevate the process to a higher band (if the freezer is active).
418 max_pages
= memorystatus_freeze_pages_max
;
421 * We only have the compressor without any swap.
423 max_pages
= UINT32_MAX
- 1;
427 state
= p
->p_memstat_state
;
431 * The jetsam path also verifies that the process is a suspended App. We don't care about that here.
432 * We simply ensure that jetsam is not already working on the process and that the process has not
433 * explicitly disabled freezing.
435 if (state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_FREEZE_DISABLED
)) {
436 printf("sysctl_freeze: p_memstat_state check failed, process is%s%s%s\n",
437 (state
& P_MEMSTAT_TERMINATED
) ? " terminated" : "",
438 (state
& P_MEMSTAT_LOCKED
) ? " locked" : "",
439 (state
& P_MEMSTAT_FREEZE_DISABLED
) ? " unfreezable" : "");
442 lck_mtx_unlock(&freezer_mutex
);
446 error
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, &freezer_error_code
, FALSE
/* eval only */);
447 if (!error
|| freezer_error_code
== FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO
) {
448 memorystatus_freezer_stats
.mfs_shared_pages_skipped
+= shared
;
452 char reason
[FREEZER_ERROR_STRING_LENGTH
];
453 memorystatus_freezer_stringify_error(freezer_error_code
, reason
, sizeof(reason
));
455 printf("sysctl_freeze: task_freeze failed: %s\n", reason
);
457 if (error
== KERN_NO_SPACE
) {
458 /* Make it easy to distinguish between failures due to low compressor/ swap space and other failures. */
465 if ((p
->p_memstat_state
& P_MEMSTAT_FROZEN
) == 0) {
466 p
->p_memstat_state
|= P_MEMSTAT_FROZEN
;
467 memorystatus_frozen_count
++;
469 // This was a re-freeze
470 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
471 memorystatus_freezer_stats
.mfs_bytes_refrozen
+= dirty
* PAGE_SIZE
;
472 memorystatus_freezer_stats
.mfs_refreeze_count
++;
475 p
->p_memstat_frozen_count
++;
480 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
482 * We elevate only if we are going to swap out the data.
484 error
= memorystatus_update_inactive_jetsam_priority_band(pid
, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE
,
485 memorystatus_freeze_jetsam_band
, TRUE
);
488 printf("sysctl_freeze: Elevating frozen process to higher jetsam band failed with %d\n", error
);
493 if ((error
== 0) && (coal
== NULL
)) {
495 * We froze a process and so we check to see if it was
496 * a coalition leader and if it has XPC services that
497 * might need freezing.
498 * Only one leader can be frozen at a time and so we shouldn't
499 * enter this block more than once per call. Hence the
500 * check that 'coal' has to be NULL. We should make this an
501 * assert() or panic() once we have a much more concrete way
502 * to detect an app vs a daemon.
505 task_t curr_task
= NULL
;
507 curr_task
= proc_task(p
);
508 coal
= task_get_coalition(curr_task
, COALITION_TYPE_JETSAM
);
509 if (coalition_is_leader(curr_task
, coal
)) {
510 ntasks
= coalition_get_pid_list(coal
, COALITION_ROLEMASK_XPC
,
511 COALITION_SORT_DEFAULT
, pid_list
, MAX_XPC_SERVICE_PIDS
);
513 if (ntasks
> MAX_XPC_SERVICE_PIDS
) {
514 ntasks
= MAX_XPC_SERVICE_PIDS
;
522 pid
= pid_list
[--ntasks
];
526 lck_mtx_unlock(&freezer_mutex
);
529 printf("sysctl_freeze: Invalid process\n");
533 lck_mtx_unlock(&freezer_mutex
);
537 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_freeze
, CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
,
538 0, 0, &sysctl_memorystatus_freeze
, "I", "");
541 * Manual trigger of agressive frozen demotion for dev / debug kernels only.
544 sysctl_memorystatus_demote_frozen_process SYSCTL_HANDLER_ARGS
546 #pragma unused(arg1, arg2, oidp, req)
549 * Only demote on write to prevent demoting during `sysctl -a`.
550 * The actual value written doesn't matter.
552 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
553 if (error
|| !req
->newptr
) {
556 memorystatus_demote_frozen_processes(false);
560 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_demote_frozen_processes
, CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
, 0, 0, &sysctl_memorystatus_demote_frozen_process
, "I", "");
563 sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS
565 #pragma unused(arg1, arg2)
570 if (memorystatus_freeze_enabled
== FALSE
) {
574 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
575 if (error
|| !req
->newptr
) {
579 if (pid
== VM_PAGES_FOR_ALL_PROCS
) {
580 do_fastwake_warmup_all();
585 error
= task_thaw(p
->task
);
591 * task_thaw() succeeded.
593 * We increment memorystatus_frozen_count on the sysctl freeze path.
594 * And so we need the P_MEMSTAT_FROZEN to decrement the frozen count
595 * when this process exits.
598 * p->p_memstat_state &= ~P_MEMSTAT_FROZEN;
599 * proc_list_unlock();
610 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_thaw
, CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
,
611 0, 0, &sysctl_memorystatus_available_pages_thaw
, "I", "");
614 typedef struct _global_freezable_status
{
615 boolean_t freeze_pages_threshold_crossed
;
616 boolean_t freeze_eligible_procs_available
;
617 boolean_t freeze_scheduled_in_future
;
618 }global_freezable_status_t
;
620 typedef struct _proc_freezable_status
{
621 boolean_t freeze_has_memstat_state
;
622 boolean_t freeze_has_pages_min
;
623 int freeze_has_probability
;
624 int freeze_leader_eligible
;
625 boolean_t freeze_attempted
;
626 uint32_t p_memstat_state
;
628 int p_freeze_error_code
;
631 char p_name
[MAXCOMLEN
+ 1];
632 }proc_freezable_status_t
;
634 #define MAX_FREEZABLE_PROCESSES 200 /* Total # of processes in band 0 that we evaluate for freezability */
637 * For coalition based freezing evaluations, we proceed as follows:
638 * - detect that the process is a coalition member and a XPC service
639 * - mark its 'freeze_leader_eligible' field with FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN
640 * - continue its freezability evaluation assuming its leader will be freezable too
642 * Once we are done evaluating all processes, we do a quick run thru all
643 * processes and for a coalition member XPC service we look up the 'freezable'
644 * status of its leader and iff:
645 * - the xpc service is freezable i.e. its individual freeze evaluation worked
646 * - and, its leader is also marked freezable
647 * we update its 'freeze_leader_eligible' to FREEZE_PROC_LEADER_FREEZABLE_SUCCESS.
650 #define FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN (-1)
651 #define FREEZE_PROC_LEADER_FREEZABLE_SUCCESS (1)
652 #define FREEZE_PROC_LEADER_FREEZABLE_FAILURE (2)
655 memorystatus_freezer_get_status(user_addr_t buffer
, size_t buffer_size
, int32_t *retval
)
657 uint32_t proc_count
= 0, freeze_eligible_proc_considered
= 0, band
= 0, xpc_index
= 0, leader_index
= 0;
658 global_freezable_status_t
*list_head
;
659 proc_freezable_status_t
*list_entry
, *list_entry_start
;
660 size_t list_size
= 0, entry_count
= 0;
661 proc_t p
, leader_proc
;
662 memstat_bucket_t
*bucket
;
663 uint32_t state
= 0, pages
= 0;
664 boolean_t try_freeze
= TRUE
, xpc_skip_size_probability_check
= FALSE
;
665 int error
= 0, probability_of_use
= 0;
666 pid_t leader_pid
= 0;
669 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
== FALSE
) {
673 list_size
= sizeof(global_freezable_status_t
) + (sizeof(proc_freezable_status_t
) * MAX_FREEZABLE_PROCESSES
);
675 if (buffer_size
< list_size
) {
679 list_head
= kheap_alloc(KHEAP_TEMP
, list_size
, Z_WAITOK
| Z_ZERO
);
680 if (list_head
== NULL
) {
684 list_size
= sizeof(global_freezable_status_t
);
688 uint64_t curr_time
= mach_absolute_time();
690 list_head
->freeze_pages_threshold_crossed
= (memorystatus_available_pages
< memorystatus_freeze_threshold
);
691 list_head
->freeze_eligible_procs_available
= ((memorystatus_suspended_count
- memorystatus_frozen_count
) > memorystatus_freeze_suspended_threshold
);
692 list_head
->freeze_scheduled_in_future
= (curr_time
< memorystatus_freezer_thread_next_run_ts
);
694 list_entry_start
= (proc_freezable_status_t
*) ((uintptr_t)list_head
+ sizeof(global_freezable_status_t
));
695 list_entry
= list_entry_start
;
697 bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE
];
699 entry_count
= (memorystatus_global_probabilities_size
/ sizeof(memorystatus_internal_probabilities_t
));
701 p
= memorystatus_get_first_proc_locked(&band
, FALSE
);
704 while ((proc_count
<= MAX_FREEZABLE_PROCESSES
) &&
706 (list_size
< buffer_size
)) {
709 * Daemon:- We will consider freezing it iff:
710 * - it belongs to a coalition and the leader is freeze-eligible (delayed evaluation)
711 * - its role in the coalition is XPC service.
713 * We skip memory size requirements in this case.
716 coalition_t coal
= COALITION_NULL
;
717 task_t leader_task
= NULL
, curr_task
= NULL
;
718 int task_role_in_coalition
= 0;
720 curr_task
= proc_task(p
);
721 coal
= task_get_coalition(curr_task
, COALITION_TYPE_JETSAM
);
723 if (coal
== COALITION_NULL
|| coalition_is_leader(curr_task
, coal
)) {
725 * By default, XPC services without an app
726 * will be the leader of their own single-member
729 goto skip_ineligible_xpc
;
732 leader_task
= coalition_get_leader(coal
);
733 if (leader_task
== TASK_NULL
) {
735 * This jetsam coalition is currently leader-less.
736 * This could happen if the app died, but XPC services
737 * have not yet exited.
739 goto skip_ineligible_xpc
;
742 leader_proc
= (proc_t
)get_bsdtask_info(leader_task
);
743 task_deallocate(leader_task
);
745 if (leader_proc
== PROC_NULL
) {
746 /* leader task is exiting */
747 goto skip_ineligible_xpc
;
750 task_role_in_coalition
= i_coal_jetsam_get_taskrole(coal
, curr_task
);
752 if (task_role_in_coalition
== COALITION_TASKROLE_XPC
) {
753 xpc_skip_size_probability_check
= TRUE
;
754 leader_pid
= leader_proc
->p_pid
;
759 p
= memorystatus_get_next_proc_locked(&band
, p
, FALSE
);
765 strlcpy(list_entry
->p_name
, p
->p_name
, MAXCOMLEN
+ 1);
767 list_entry
->p_pid
= p
->p_pid
;
769 state
= p
->p_memstat_state
;
771 if ((state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_FREEZE_DISABLED
| P_MEMSTAT_FREEZE_IGNORE
)) ||
772 !(state
& P_MEMSTAT_SUSPENDED
)) {
773 try_freeze
= list_entry
->freeze_has_memstat_state
= FALSE
;
775 try_freeze
= list_entry
->freeze_has_memstat_state
= TRUE
;
778 list_entry
->p_memstat_state
= state
;
780 if (xpc_skip_size_probability_check
== TRUE
) {
782 * Assuming the coalition leader is freezable
783 * we don't care re. minimum pages and probability
784 * as long as the process isn't marked P_MEMSTAT_FREEZE_DISABLED.
785 * XPC services have to be explicity opted-out of the disabled
786 * state. And we checked that state above.
788 list_entry
->freeze_has_pages_min
= TRUE
;
789 list_entry
->p_pages
= -1;
790 list_entry
->freeze_has_probability
= -1;
792 list_entry
->freeze_leader_eligible
= FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN
;
793 list_entry
->p_leader_pid
= leader_pid
;
795 xpc_skip_size_probability_check
= FALSE
;
797 list_entry
->freeze_leader_eligible
= FREEZE_PROC_LEADER_FREEZABLE_SUCCESS
; /* Apps are freeze eligible and their own leaders. */
798 list_entry
->p_leader_pid
= 0; /* Setting this to 0 signifies this isn't a coalition driven freeze. */
800 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
);
801 if (pages
< memorystatus_freeze_pages_min
) {
802 try_freeze
= list_entry
->freeze_has_pages_min
= FALSE
;
804 list_entry
->freeze_has_pages_min
= TRUE
;
807 list_entry
->p_pages
= pages
;
811 for (j
= 0; j
< entry_count
; j
++) {
812 if (strncmp(memorystatus_global_probabilities_table
[j
].proc_name
,
814 MAXCOMLEN
+ 1) == 0) {
815 probability_of_use
= memorystatus_global_probabilities_table
[j
].use_probability
;
820 list_entry
->freeze_has_probability
= probability_of_use
;
822 try_freeze
= ((probability_of_use
> 0) && try_freeze
);
824 list_entry
->freeze_has_probability
= -1;
829 uint32_t purgeable
, wired
, clean
, dirty
, shared
;
830 uint32_t max_pages
= 0;
831 int freezer_error_code
= 0;
833 error
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, &freezer_error_code
, TRUE
/* eval only */);
836 list_entry
->p_freeze_error_code
= freezer_error_code
;
839 list_entry
->freeze_attempted
= TRUE
;
843 freeze_eligible_proc_considered
++;
845 list_size
+= sizeof(proc_freezable_status_t
);
847 p
= memorystatus_get_next_proc_locked(&band
, p
, FALSE
);
853 list_entry
= list_entry_start
;
855 for (xpc_index
= 0; xpc_index
< freeze_eligible_proc_considered
; xpc_index
++) {
856 if (list_entry
[xpc_index
].freeze_leader_eligible
== FREEZE_PROC_LEADER_FREEZABLE_UNKNOWN
) {
857 leader_pid
= list_entry
[xpc_index
].p_leader_pid
;
859 leader_proc
= proc_find(leader_pid
);
862 if (leader_proc
->p_memstat_state
& P_MEMSTAT_FROZEN
) {
864 * Leader has already been frozen.
866 list_entry
[xpc_index
].freeze_leader_eligible
= FREEZE_PROC_LEADER_FREEZABLE_SUCCESS
;
867 proc_rele(leader_proc
);
870 proc_rele(leader_proc
);
873 for (leader_index
= 0; leader_index
< freeze_eligible_proc_considered
; leader_index
++) {
874 if (list_entry
[leader_index
].p_pid
== leader_pid
) {
875 if (list_entry
[leader_index
].freeze_attempted
&& list_entry
[leader_index
].p_freeze_error_code
== 0) {
876 list_entry
[xpc_index
].freeze_leader_eligible
= FREEZE_PROC_LEADER_FREEZABLE_SUCCESS
;
878 list_entry
[xpc_index
].freeze_leader_eligible
= FREEZE_PROC_LEADER_FREEZABLE_FAILURE
;
879 list_entry
[xpc_index
].p_freeze_error_code
= FREEZER_ERROR_GENERIC
;
886 * Didn't find the leader entry. This might be likely because
887 * the leader never made it down to band 0.
889 if (leader_index
== freeze_eligible_proc_considered
) {
890 list_entry
[xpc_index
].freeze_leader_eligible
= FREEZE_PROC_LEADER_FREEZABLE_FAILURE
;
891 list_entry
[xpc_index
].p_freeze_error_code
= FREEZER_ERROR_GENERIC
;
896 buffer_size
= MIN(list_size
, INT32_MAX
);
898 error
= copyout(list_head
, buffer
, buffer_size
);
900 *retval
= (int32_t) buffer_size
;
905 list_size
= sizeof(global_freezable_status_t
) + (sizeof(proc_freezable_status_t
) * MAX_FREEZABLE_PROCESSES
);
906 kheap_free(KHEAP_TEMP
, list_head
, list_size
);
908 MEMORYSTATUS_DEBUG(1, "memorystatus_freezer_get_status: returning %d (%lu - size)\n", error
, (unsigned long)*list_size
);
913 #endif /* DEVELOPMENT || DEBUG */
916 * Get a list of all processes in the freezer band which are currently frozen.
917 * Used by powerlog to collect analytics on frozen process.
920 memorystatus_freezer_get_procs(user_addr_t buffer
, size_t buffer_size
, int32_t *retval
)
922 global_frozen_procs_t
*frozen_procs
= NULL
;
923 uint32_t band
= memorystatus_freeze_jetsam_band
;
927 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
== FALSE
) {
930 if (buffer_size
< sizeof(global_frozen_procs_t
)) {
933 frozen_procs
= kheap_alloc(KHEAP_TEMP
, sizeof(global_frozen_procs_t
),
935 if (frozen_procs
== NULL
) {
940 p
= memorystatus_get_first_proc_locked(&band
, FALSE
);
941 while (p
&& frozen_procs
->gfp_num_frozen
< FREEZER_CONTROL_GET_PROCS_MAX_COUNT
) {
942 state
= p
->p_memstat_state
;
943 if (state
& P_MEMSTAT_FROZEN
) {
944 frozen_procs
->gfp_procs
[frozen_procs
->gfp_num_frozen
].fp_pid
= p
->p_pid
;
945 strlcpy(frozen_procs
->gfp_procs
[frozen_procs
->gfp_num_frozen
].fp_name
,
946 p
->p_name
, sizeof(proc_name_t
));
947 frozen_procs
->gfp_num_frozen
++;
949 p
= memorystatus_get_next_proc_locked(&band
, p
, FALSE
);
953 buffer_size
= MIN(buffer_size
, sizeof(global_frozen_procs_t
));
954 error
= copyout(frozen_procs
, buffer
, buffer_size
);
956 *retval
= (int32_t) buffer_size
;
960 kheap_free(KHEAP_TEMP
, frozen_procs
, sizeof(global_frozen_procs_t
));
966 memorystatus_freezer_control(int32_t flags
, user_addr_t buffer
, size_t buffer_size
, int32_t *retval
)
970 #if DEVELOPMENT || DEBUG
971 if (flags
== FREEZER_CONTROL_GET_STATUS
) {
972 err
= memorystatus_freezer_get_status(buffer
, buffer_size
, retval
);
974 #endif /* DEVELOPMENT || DEBUG */
975 if (flags
== FREEZER_CONTROL_GET_PROCS
) {
976 err
= memorystatus_freezer_get_procs(buffer
, buffer_size
, retval
);
982 extern void vm_swap_consider_defragmenting(int);
983 extern boolean_t
memorystatus_kill_elevated_process(uint32_t, os_reason_t
, unsigned int, int, uint32_t *, uint64_t *);
986 * This routine will _jetsam_ all frozen processes
987 * and reclaim the swap space immediately.
989 * So freeze has to be DISABLED when we call this routine.
993 memorystatus_disable_freeze(void)
995 memstat_bucket_t
*bucket
;
996 int bucket_count
= 0, retries
= 0;
997 boolean_t retval
= FALSE
, killed
= FALSE
;
998 uint32_t errors
= 0, errors_over_prev_iteration
= 0;
999 os_reason_t jetsam_reason
= 0;
1000 unsigned int band
= 0;
1001 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
1002 uint64_t memory_reclaimed
= 0, footprint
= 0;
1004 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE_DISABLE
) | DBG_FUNC_START
,
1005 memorystatus_available_pages
, 0, 0, 0, 0);
1007 assert(memorystatus_freeze_enabled
== FALSE
);
1009 jetsam_reason
= os_reason_create(OS_REASON_JETSAM
, JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE
);
1010 if (jetsam_reason
== OS_REASON_NULL
) {
1011 printf("memorystatus_disable_freeze: failed to allocate jetsam reason\n");
1015 * Let's relocate all frozen processes into band 8. Demoted frozen processes
1016 * are sitting in band 0 currently and it's possible to have a frozen process
1017 * in the FG band being actively used. We don't reset its frozen state when
1018 * it is resumed because it has state on disk.
1020 * We choose to do this relocation rather than implement a new 'kill frozen'
1021 * process function for these reasons:
1022 * - duplication of code: too many kill functions exist and we need to rework them better.
1023 * - disk-space-shortage kills are rare
1024 * - not having the 'real' jetsam band at time of the this frozen kill won't preclude us
1025 * from answering any imp. questions re. jetsam policy/effectiveness.
1027 * This is essentially what memorystatus_update_inactive_jetsam_priority_band() does while
1028 * avoiding the application of memory limits.
1034 band
= JETSAM_PRIORITY_IDLE
;
1038 next_p
= memorystatus_get_first_proc_locked(&band
, TRUE
);
1041 next_p
= memorystatus_get_next_proc_locked(&band
, p
, TRUE
);
1043 if (p
->p_memstat_effectivepriority
> JETSAM_PRIORITY_FOREGROUND
) {
1047 if ((p
->p_memstat_state
& P_MEMSTAT_FROZEN
) == FALSE
) {
1051 if (p
->p_memstat_state
& P_MEMSTAT_ERROR
) {
1052 p
->p_memstat_state
&= ~P_MEMSTAT_ERROR
;
1055 if (p
->p_memstat_effectivepriority
== memorystatus_freeze_jetsam_band
) {
1060 * We explicitly add this flag here so the process looks like a normal
1061 * frozen process i.e. P_MEMSTAT_FROZEN and P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND.
1062 * We don't bother with assigning the 'active' memory
1063 * limits at this point because we are going to be killing it soon below.
1065 p
->p_memstat_state
|= P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND
;
1066 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1068 memorystatus_update_priority_locked(p
, memorystatus_freeze_jetsam_band
, FALSE
, TRUE
);
1071 bucket
= &memstat_bucket
[memorystatus_freeze_jetsam_band
];
1072 bucket_count
= bucket
->count
;
1076 * Bucket count is already stale at this point. But, we don't expect
1077 * freezing to continue since we have already disabled the freeze functionality.
1078 * However, an existing freeze might be in progress. So we might miss that process
1079 * in the first go-around. We hope to catch it in the next.
1082 errors_over_prev_iteration
= 0;
1083 while (bucket_count
) {
1087 * memorystatus_kill_elevated_process() drops a reference,
1088 * so take another one so we can continue to use this exit reason
1089 * even after it returns.
1092 os_reason_ref(jetsam_reason
);
1093 retval
= memorystatus_kill_elevated_process(
1094 kMemorystatusKilledDiskSpaceShortage
,
1096 memorystatus_freeze_jetsam_band
,
1097 0, /* the iteration of aggressive jetsam..ignored here */
1102 printf("memorystatus_disable_freeze: memorystatus_kill_elevated_process returned %d error(s)\n", errors
);
1103 errors_over_prev_iteration
+= errors
;
1109 * No frozen processes left to kill.
1115 memory_reclaimed
+= footprint
;
1120 if (memorystatus_frozen_count
) {
1122 * A frozen process snuck in and so
1123 * go back around to kill it. That
1124 * process may have been resumed and
1125 * put into the FG band too. So we
1126 * have to do the relocation again.
1128 assert(memorystatus_freeze_enabled
== FALSE
);
1135 #if DEVELOPMENT || DEBUG
1136 panic("memorystatus_disable_freeze: Failed to kill all frozen processes, memorystatus_frozen_count = %d, errors = %d",
1137 memorystatus_frozen_count
, errors_over_prev_iteration
);
1138 #endif /* DEVELOPMENT || DEBUG */
1142 os_reason_free(jetsam_reason
);
1145 vm_swap_consider_defragmenting(VM_SWAP_FLAGS_FORCE_DEFRAG
| VM_SWAP_FLAGS_FORCE_RECLAIM
);
1148 size_t snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) +
1149 sizeof(memorystatus_jetsam_snapshot_entry_t
) * (memorystatus_jetsam_snapshot_count
);
1150 uint64_t timestamp_now
= mach_absolute_time();
1151 memorystatus_jetsam_snapshot
->notification_time
= timestamp_now
;
1152 memorystatus_jetsam_snapshot
->js_gencount
++;
1153 if (memorystatus_jetsam_snapshot_count
> 0 && (memorystatus_jetsam_snapshot_last_timestamp
== 0 ||
1154 timestamp_now
> memorystatus_jetsam_snapshot_last_timestamp
+ memorystatus_jetsam_snapshot_timeout
)) {
1156 int ret
= memorystatus_send_note(kMemorystatusSnapshotNote
, &snapshot_size
, sizeof(snapshot_size
));
1159 memorystatus_jetsam_snapshot_last_timestamp
= timestamp_now
;
1167 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE_DISABLE
) | DBG_FUNC_END
,
1168 memorystatus_available_pages
, memory_reclaimed
, 0, 0, 0);
1173 __private_extern__
void
1174 memorystatus_freeze_init(void)
1176 kern_return_t result
;
1179 freezer_lck_grp_attr
= lck_grp_attr_alloc_init();
1180 freezer_lck_grp
= lck_grp_alloc_init("freezer", freezer_lck_grp_attr
);
1182 lck_mtx_init(&freezer_mutex
, freezer_lck_grp
, NULL
);
1185 * This is just the default value if the underlying
1186 * storage device doesn't have any specific budget.
1187 * We check with the storage layer in memorystatus_freeze_update_throttle()
1188 * before we start our freezing the first time.
1190 memorystatus_freeze_budget_pages_remaining
= (memorystatus_freeze_daily_mb_max
* 1024 * 1024) / PAGE_SIZE
;
1192 result
= kernel_thread_start(memorystatus_freeze_thread
, NULL
, &thread
);
1193 if (result
== KERN_SUCCESS
) {
1194 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, TASK_POLICY_IO
, THROTTLE_LEVEL_COMPRESSOR_TIER2
);
1195 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, TASK_POLICY_PASSIVE_IO
, TASK_POLICY_ENABLE
);
1196 thread_set_thread_name(thread
, "VM_freezer");
1198 thread_deallocate(thread
);
1200 panic("Could not create memorystatus_freeze_thread");
1205 memorystatus_is_process_eligible_for_freeze(proc_t p
)
1208 * Called with proc_list_lock held.
1211 LCK_MTX_ASSERT(proc_list_mlock
, LCK_MTX_ASSERT_OWNED
);
1213 boolean_t should_freeze
= FALSE
;
1214 uint32_t state
= 0, pages
= 0;
1215 int probability_of_use
= 0;
1216 size_t entry_count
= 0, i
= 0;
1217 bool first_consideration
= true;
1219 state
= p
->p_memstat_state
;
1221 if (state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_FREEZE_DISABLED
| P_MEMSTAT_FREEZE_IGNORE
)) {
1227 * Daemon:- We consider freezing it if:
1228 * - it belongs to a coalition and the leader is frozen, and,
1229 * - its role in the coalition is XPC service.
1231 * We skip memory size requirements in this case.
1234 coalition_t coal
= COALITION_NULL
;
1235 task_t leader_task
= NULL
, curr_task
= NULL
;
1236 proc_t leader_proc
= NULL
;
1237 int task_role_in_coalition
= 0;
1239 curr_task
= proc_task(p
);
1240 coal
= task_get_coalition(curr_task
, COALITION_TYPE_JETSAM
);
1242 if (coal
== NULL
|| coalition_is_leader(curr_task
, coal
)) {
1244 * By default, XPC services without an app
1245 * will be the leader of their own single-member
1251 leader_task
= coalition_get_leader(coal
);
1252 if (leader_task
== TASK_NULL
) {
1254 * This jetsam coalition is currently leader-less.
1255 * This could happen if the app died, but XPC services
1256 * have not yet exited.
1261 leader_proc
= (proc_t
)get_bsdtask_info(leader_task
);
1262 task_deallocate(leader_task
);
1264 if (leader_proc
== PROC_NULL
) {
1265 /* leader task is exiting */
1269 if (!(leader_proc
->p_memstat_state
& P_MEMSTAT_FROZEN
)) {
1273 task_role_in_coalition
= i_coal_jetsam_get_taskrole(coal
, curr_task
);
1275 if (task_role_in_coalition
== COALITION_TASKROLE_XPC
) {
1276 should_freeze
= TRUE
;
1282 * Application. In addition to the above states we need to make
1283 * sure we only consider suspended applications for freezing.
1285 if (!(state
& P_MEMSTAT_SUSPENDED
)) {
1291 * This proc is a suspended application.
1292 * We're interested in tracking what percentage of these
1293 * actually get frozen.
1294 * To avoid skewing the metrics towards processes which
1295 * are considered more frequently, we only track failures once
1298 first_consideration
= !(state
& P_MEMSTAT_FREEZE_CONSIDERED
);
1300 if (first_consideration
) {
1301 memorystatus_freezer_stats
.mfs_process_considered_count
++;
1302 p
->p_memstat_state
|= P_MEMSTAT_FREEZE_CONSIDERED
;
1305 /* Only freeze applications meeting our minimum resident page criteria */
1306 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
);
1307 if (pages
< memorystatus_freeze_pages_min
) {
1308 if (first_consideration
) {
1309 memorystatus_freezer_stats
.mfs_error_below_min_pages_count
++;
1314 /* Don't freeze processes that are already exiting on core. It may have started exiting
1315 * after we chose it for freeze, but before we obtained the proc_list_lock.
1316 * NB: This is only possible if we're coming in from memorystatus_freeze_process_sync.
1317 * memorystatus_freeze_top_process holds the proc_list_lock while it traverses the bands.
1319 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1320 if (first_consideration
) {
1321 memorystatus_freezer_stats
.mfs_error_other_count
++;
1326 entry_count
= (memorystatus_global_probabilities_size
/ sizeof(memorystatus_internal_probabilities_t
));
1329 for (i
= 0; i
< entry_count
; i
++) {
1330 if (strncmp(memorystatus_global_probabilities_table
[i
].proc_name
,
1332 MAXCOMLEN
+ 1) == 0) {
1333 probability_of_use
= memorystatus_global_probabilities_table
[i
].use_probability
;
1338 if (probability_of_use
== 0) {
1339 if (first_consideration
) {
1340 memorystatus_freezer_stats
.mfs_error_low_probability_of_use_count
++;
1346 should_freeze
= TRUE
;
1348 if (should_freeze
&& !first_consideration
&& !(state
& P_MEMSTAT_FROZEN
)) {
1350 * We're freezing this for the first time and we previously considered it ineligible.
1351 * Bump the considered count so that we track this as 1 failure
1354 memorystatus_freezer_stats
.mfs_process_considered_count
++;
1356 return should_freeze
;
1360 * Synchronously freeze the passed proc. Called with a reference to the proc held.
1362 * Doesn't deal with:
1363 * - re-freezing because this is called on a specific process and
1364 * not by the freezer thread. If that changes, we'll have to teach it about
1365 * refreezing a frozen process.
1367 * - grouped/coalition freezing because we are hoping to deprecate this
1368 * interface as it was used by user-space to freeze particular processes. But
1369 * we have moved away from that approach to having the kernel choose the optimal
1370 * candidates to be frozen.
1372 * Returns EINVAL or the value returned by task_freeze().
1375 memorystatus_freeze_process_sync(proc_t p
)
1379 boolean_t memorystatus_freeze_swap_low
= FALSE
;
1380 int freezer_error_code
= 0;
1382 lck_mtx_lock(&freezer_mutex
);
1385 printf("memorystatus_freeze_process_sync: Invalid process\n");
1389 if (memorystatus_freeze_enabled
== FALSE
) {
1390 printf("memorystatus_freeze_process_sync: Freezing is DISABLED\n");
1394 if (!memorystatus_can_freeze(&memorystatus_freeze_swap_low
)) {
1395 printf("memorystatus_freeze_process_sync: Low compressor and/or low swap space...skipping freeze\n");
1399 memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining
);
1400 if (!memorystatus_freeze_budget_pages_remaining
) {
1401 printf("memorystatus_freeze_process_sync: exit with NO available budget\n");
1408 uint32_t purgeable
, wired
, clean
, dirty
, shared
;
1414 /* Ensure the process is eligible for freezing */
1415 if (memorystatus_is_process_eligible_for_freeze(p
) == FALSE
) {
1420 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1421 max_pages
= MIN(memorystatus_freeze_pages_max
, memorystatus_freeze_budget_pages_remaining
);
1424 * We only have the compressor without any swap.
1426 max_pages
= UINT32_MAX
- 1;
1429 /* Mark as locked temporarily to avoid kill */
1430 p
->p_memstat_state
|= P_MEMSTAT_LOCKED
;
1433 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_START
,
1434 memorystatus_available_pages
, 0, 0, 0, 0);
1436 max_pages
= MIN(max_pages
, UINT32_MAX
);
1437 ret
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, (uint32_t) max_pages
, &shared
, &freezer_error_code
, FALSE
/* eval only */);
1438 if (ret
== KERN_SUCCESS
|| freezer_error_code
== FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO
) {
1439 memorystatus_freezer_stats
.mfs_shared_pages_skipped
+= shared
;
1442 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_END
,
1443 memorystatus_available_pages
, aPid
, 0, 0, 0);
1445 DTRACE_MEMORYSTATUS6(memorystatus_freeze
, proc_t
, p
, unsigned int, memorystatus_available_pages
, boolean_t
, purgeable
, unsigned int, wired
, uint32_t, clean
, uint32_t, dirty
);
1447 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_process_sync: task_freeze %s for pid %d [%s] - "
1448 "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n",
1449 (ret
== KERN_SUCCESS
) ? "SUCCEEDED" : "FAILED", aPid
, (*p
->p_name
? p
->p_name
: "(unknown)"),
1450 memorystatus_available_pages
, purgeable
, wired
, clean
, dirty
, max_pages
, shared
);
1454 if (ret
== KERN_SUCCESS
) {
1455 memorystatus_freeze_entry_t data
= { aPid
, TRUE
, dirty
};
1457 p
->p_memstat_freeze_sharedanon_pages
+= shared
;
1459 memorystatus_frozen_shared_mb
+= shared
;
1461 if ((p
->p_memstat_state
& P_MEMSTAT_FROZEN
) == 0) {
1462 p
->p_memstat_state
|= P_MEMSTAT_FROZEN
;
1463 memorystatus_frozen_count
++;
1465 // This was a re-freeze
1466 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1467 memorystatus_freezer_stats
.mfs_bytes_refrozen
+= dirty
* PAGE_SIZE
;
1468 memorystatus_freezer_stats
.mfs_refreeze_count
++;
1472 p
->p_memstat_frozen_count
++;
1475 * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process
1476 * to its higher jetsam band.
1480 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
1482 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1483 ret
= memorystatus_update_inactive_jetsam_priority_band(p
->p_pid
, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE
,
1484 memorystatus_freeze_jetsam_band
, TRUE
);
1487 printf("Elevating the frozen process failed with %d\n", ret
);
1494 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
1495 throttle_intervals
[i
].pageouts
+= dirty
;
1498 memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining
);
1499 os_log_with_startup_serial(OS_LOG_DEFAULT
, "memorystatus: freezing (specific) pid %d [%s] done memorystatus_freeze_budget_pages_remaining %llu froze %u pages",
1500 aPid
, ((p
&& *p
->p_name
) ? p
->p_name
: "unknown"), memorystatus_freeze_budget_pages_remaining
, dirty
);
1504 memorystatus_freeze_pageouts
+= dirty
;
1506 if (memorystatus_frozen_count
== (memorystatus_frozen_processes_max
- 1)) {
1508 * Add some eviction logic here? At some point should we
1509 * jetsam a process to get back its swap space so that we
1510 * can freeze a more eligible process at this moment in time?
1514 char reason
[FREEZER_ERROR_STRING_LENGTH
];
1515 memorystatus_freezer_stringify_error(freezer_error_code
, reason
, sizeof(reason
));
1517 os_log_with_startup_serial(OS_LOG_DEFAULT
, "memorystatus: freezing (specific) pid %d [%s]...skipped (%s)",
1518 aPid
, ((p
&& *p
->p_name
) ? p
->p_name
: "unknown"), reason
);
1519 p
->p_memstat_state
|= P_MEMSTAT_FREEZE_IGNORE
;
1522 p
->p_memstat_state
&= ~P_MEMSTAT_LOCKED
;
1523 wakeup(&p
->p_memstat_state
);
1528 lck_mtx_unlock(&freezer_mutex
);
1534 * Caller must hold the freezer_mutex and it will be locked on return.
1537 memorystatus_freeze_top_process(void)
1539 pid_t aPid
= 0, coal_xpc_pid
= 0;
1541 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
1543 unsigned int band
= JETSAM_PRIORITY_IDLE
;
1544 boolean_t refreeze_processes
= FALSE
;
1545 task_t curr_task
= NULL
;
1546 coalition_t coal
= COALITION_NULL
;
1547 pid_t pid_list
[MAX_XPC_SERVICE_PIDS
];
1548 unsigned int ntasks
= 0;
1549 LCK_MTX_ASSERT(&freezer_mutex
, LCK_MTX_ASSERT_OWNED
);
1551 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE_SCAN
) | DBG_FUNC_START
, memorystatus_available_pages
, 0, 0, 0, 0);
1555 if (memorystatus_frozen_count
>= memorystatus_frozen_processes_max
) {
1557 * Freezer is already full but we are here and so let's
1558 * try to refreeze any processes we might have thawed
1559 * in the past and push out their compressed state out.
1561 refreeze_processes
= TRUE
;
1562 band
= (unsigned int) memorystatus_freeze_jetsam_band
;
1567 next_p
= memorystatus_get_first_proc_locked(&band
, FALSE
);
1570 uint32_t purgeable
, wired
, clean
, dirty
, shared
;
1571 uint64_t max_pages
= 0;
1572 int freezer_error_code
= 0;
1577 next_p
= memorystatus_get_next_proc_locked(&band
, p
, FALSE
);
1580 * We have frozen a coalition leader and now are
1581 * dealing with its XPC services. We get our
1582 * next_p for each XPC service from the pid_list
1583 * acquired after a successful task_freeze call
1584 * on the coalition leader.
1588 coal_xpc_pid
= pid_list
[--ntasks
];
1589 next_p
= proc_findinternal(coal_xpc_pid
, 1 /* proc_list_lock held */);
1591 * We grab a reference when we are about to freeze the process. So, drop
1592 * the reference that proc_findinternal() grabbed for us.
1593 * We also have the proc_list_lock and so this process is stable.
1596 proc_rele_locked(next_p
);
1605 if (p
->p_memstat_effectivepriority
!= (int32_t) band
) {
1607 * We shouldn't be freezing processes outside the
1613 /* Ensure the process is eligible for (re-)freezing */
1614 if (refreeze_processes
) {
1616 * Has to have been frozen once before.
1618 if ((p
->p_memstat_state
& P_MEMSTAT_FROZEN
) == FALSE
) {
1623 * Has to have been resumed once before.
1625 if ((p
->p_memstat_state
& P_MEMSTAT_REFREEZE_ELIGIBLE
) == FALSE
) {
1630 * Not currently being looked at for something.
1632 if (p
->p_memstat_state
& P_MEMSTAT_LOCKED
) {
1637 * We are going to try and refreeze and so re-evaluate
1638 * the process. We don't want to double count the shared
1639 * memory. So deduct the old snapshot here.
1641 memorystatus_frozen_shared_mb
-= p
->p_memstat_freeze_sharedanon_pages
;
1642 p
->p_memstat_freeze_sharedanon_pages
= 0;
1644 p
->p_memstat_state
&= ~P_MEMSTAT_REFREEZE_ELIGIBLE
;
1645 memorystatus_refreeze_eligible_count
--;
1647 if (memorystatus_is_process_eligible_for_freeze(p
) == FALSE
) {
1648 continue; // with lock held
1652 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1654 * Freezer backed by the compressor and swap file(s)
1655 * will hold compressed data.
1658 max_pages
= MIN(memorystatus_freeze_pages_max
, memorystatus_freeze_budget_pages_remaining
);
1661 * We only have the compressor pool.
1663 max_pages
= UINT32_MAX
- 1;
1666 /* Mark as locked temporarily to avoid kill */
1667 p
->p_memstat_state
|= P_MEMSTAT_LOCKED
;
1669 p
= proc_ref_locked(p
);
1671 memorystatus_freezer_stats
.mfs_error_other_count
++;
1677 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_START
,
1678 memorystatus_available_pages
, 0, 0, 0, 0);
1680 max_pages
= MIN(max_pages
, UINT32_MAX
);
1681 kr
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, (uint32_t) max_pages
, &shared
, &freezer_error_code
, FALSE
/* eval only */);
1682 if (kr
== KERN_SUCCESS
|| freezer_error_code
== FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO
) {
1683 memorystatus_freezer_stats
.mfs_shared_pages_skipped
+= shared
;
1686 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_END
,
1687 memorystatus_available_pages
, aPid
, 0, 0, 0);
1689 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_top_process: task_freeze %s for pid %d [%s] - "
1690 "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n",
1691 (kr
== KERN_SUCCESS
) ? "SUCCEEDED" : "FAILED", aPid
, (*p
->p_name
? p
->p_name
: "(unknown)"),
1692 memorystatus_available_pages
, purgeable
, wired
, clean
, dirty
, max_pages
, shared
);
1697 if (KERN_SUCCESS
== kr
) {
1698 memorystatus_freeze_entry_t data
= { aPid
, TRUE
, dirty
};
1700 p
->p_memstat_freeze_sharedanon_pages
+= shared
;
1702 memorystatus_frozen_shared_mb
+= shared
;
1704 if ((p
->p_memstat_state
& P_MEMSTAT_FROZEN
) == 0) {
1705 p
->p_memstat_state
|= P_MEMSTAT_FROZEN
;
1706 memorystatus_frozen_count
++;
1708 // This was a re-freeze
1709 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1710 memorystatus_freezer_stats
.mfs_bytes_refrozen
+= dirty
* PAGE_SIZE
;
1711 memorystatus_freezer_stats
.mfs_refreeze_count
++;
1715 p
->p_memstat_frozen_count
++;
1718 * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process
1719 * to its higher jetsam band.
1723 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
1725 if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1726 ret
= memorystatus_update_inactive_jetsam_priority_band(p
->p_pid
, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE
, memorystatus_freeze_jetsam_band
, TRUE
);
1729 printf("Elevating the frozen process failed with %d\n", ret
);
1735 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
1736 throttle_intervals
[i
].pageouts
+= dirty
;
1739 memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining
);
1740 os_log_with_startup_serial(OS_LOG_DEFAULT
, "memorystatus: %sfreezing (%s) pid %d [%s] done, memorystatus_freeze_budget_pages_remaining %llu %sfroze %u pages\n",
1741 refreeze_processes
? "re" : "", (coal
== NULL
? "general" : "coalition-driven"), aPid
, ((p
&& *p
->p_name
) ? p
->p_name
: "unknown"), memorystatus_freeze_budget_pages_remaining
, refreeze_processes
? "Re" : "", dirty
);
1745 memorystatus_freeze_pageouts
+= dirty
;
1747 if (memorystatus_frozen_count
== (memorystatus_frozen_processes_max
- 1)) {
1749 * Add some eviction logic here? At some point should we
1750 * jetsam a process to get back its swap space so that we
1751 * can freeze a more eligible process at this moment in time?
1755 /* Return KERN_SUCCESS */
1759 * We froze a process successfully. We can stop now
1760 * and see if that helped if this process isn't part
1764 * - if it is a leader, get the list of XPC services
1765 * that need to be frozen.
1766 * - if it is a XPC service whose leader was frozen
1767 * here, continue on to the next XPC service in the list.
1771 curr_task
= proc_task(p
);
1772 coal
= task_get_coalition(curr_task
, COALITION_TYPE_JETSAM
);
1773 if (coalition_is_leader(curr_task
, coal
)) {
1774 ntasks
= coalition_get_pid_list(coal
, COALITION_ROLEMASK_XPC
,
1775 COALITION_SORT_DEFAULT
, pid_list
, MAX_XPC_SERVICE_PIDS
);
1777 if (ntasks
> MAX_XPC_SERVICE_PIDS
) {
1778 ntasks
= MAX_XPC_SERVICE_PIDS
;
1786 * Start off with our first next_p in this list.
1788 coal_xpc_pid
= pid_list
[--ntasks
];
1789 next_p
= proc_findinternal(coal_xpc_pid
, 1 /* proc_list_lock held */);
1792 * We grab a reference when we are about to freeze the process. So drop
1793 * the reference that proc_findinternal() grabbed for us.
1794 * We also have the proc_list_lock and so this process is stable.
1797 proc_rele_locked(next_p
);
1802 p
->p_memstat_state
&= ~P_MEMSTAT_LOCKED
;
1803 wakeup(&p
->p_memstat_state
);
1804 proc_rele_locked(p
);
1806 if (coal
&& next_p
) {
1811 * No coalition leader was frozen. So we don't
1812 * need to evaluate any XPC services.
1816 * We have frozen all eligible XPC services for
1817 * the current coalition leader.
1819 * Either way, we can break here and see if freezing
1825 p
->p_memstat_state
&= ~P_MEMSTAT_LOCKED
;
1826 wakeup(&p
->p_memstat_state
);
1828 if (refreeze_processes
== TRUE
) {
1829 if ((freezer_error_code
== FREEZER_ERROR_EXCESS_SHARED_MEMORY
) ||
1830 (freezer_error_code
== FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO
)) {
1832 * Keeping this prior-frozen process in this high band when
1833 * we failed to re-freeze it due to bad shared memory usage
1834 * could cause excessive pressure on the lower bands.
1835 * We need to demote it for now. It'll get re-evaluated next
1836 * time because we don't set the P_MEMSTAT_FREEZE_IGNORE
1840 p
->p_memstat_state
&= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND
;
1841 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1842 memorystatus_update_priority_locked(p
, JETSAM_PRIORITY_IDLE
, TRUE
, TRUE
);
1845 p
->p_memstat_state
|= P_MEMSTAT_FREEZE_IGNORE
;
1848 char reason
[FREEZER_ERROR_STRING_LENGTH
];
1849 memorystatus_freezer_stringify_error(freezer_error_code
, reason
, sizeof(reason
));
1851 os_log_with_startup_serial(OS_LOG_DEFAULT
, "memorystatus: %sfreezing (%s) pid %d [%s]...skipped (%s)\n",
1852 refreeze_processes
? "re" : "", (coal
== NULL
? "general" : "coalition-driven"), aPid
, ((p
&& *p
->p_name
) ? p
->p_name
: "unknown"), reason
);
1854 proc_rele_locked(p
);
1856 if (vm_compressor_low_on_space() || vm_swap_low_on_space()) {
1863 (memorystatus_refreeze_eligible_count
>= MIN_THAW_REFREEZE_THRESHOLD
) &&
1864 (refreeze_processes
== FALSE
)) {
1866 * We failed to freeze a process from the IDLE
1867 * band AND we have some thawed processes
1868 * AND haven't tried refreezing as yet.
1869 * Let's try and re-freeze processes in the
1870 * frozen band that have been resumed in the past
1871 * and so have brought in state from disk.
1874 band
= (unsigned int) memorystatus_freeze_jetsam_band
;
1876 refreeze_processes
= TRUE
;
1878 goto freeze_process
;
1883 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE_SCAN
) | DBG_FUNC_END
, memorystatus_available_pages
, aPid
, 0, 0, 0);
1888 static inline boolean_t
1889 memorystatus_can_freeze_processes(void)
1895 if (memorystatus_suspended_count
) {
1896 memorystatus_freeze_suspended_threshold
= MIN(memorystatus_freeze_suspended_threshold
, FREEZE_SUSPENDED_THRESHOLD_DEFAULT
);
1898 if ((memorystatus_suspended_count
- memorystatus_frozen_count
) > memorystatus_freeze_suspended_threshold
) {
1913 memorystatus_can_freeze(boolean_t
*memorystatus_freeze_swap_low
)
1915 boolean_t can_freeze
= TRUE
;
1917 /* Only freeze if we're sufficiently low on memory; this holds off freeze right
1918 * after boot, and is generally is a no-op once we've reached steady state. */
1919 if (memorystatus_available_pages
> memorystatus_freeze_threshold
) {
1923 /* Check minimum suspended process threshold. */
1924 if (!memorystatus_can_freeze_processes()) {
1927 assert(VM_CONFIG_COMPRESSOR_IS_PRESENT
);
1929 if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE
) {
1931 * In-core compressor used for freezing WITHOUT on-disk swap support.
1933 if (vm_compressor_low_on_space()) {
1934 if (*memorystatus_freeze_swap_low
) {
1935 *memorystatus_freeze_swap_low
= TRUE
;
1940 if (*memorystatus_freeze_swap_low
) {
1941 *memorystatus_freeze_swap_low
= FALSE
;
1948 * Freezing WITH on-disk swap support.
1950 * In-core compressor fronts the swap.
1952 if (vm_swap_low_on_space()) {
1953 if (*memorystatus_freeze_swap_low
) {
1954 *memorystatus_freeze_swap_low
= TRUE
;
1965 * This function evaluates if the currently frozen processes deserve
1966 * to stay in the higher jetsam band. There are 2 modes:
1967 * - 'force one == TRUE': (urgent mode)
1968 * We are out of budget and can't refreeze a process. The process's
1969 * state, if it was resumed, will stay in compressed memory. If we let it
1970 * remain up in the higher frozen jetsam band, it'll put a lot of pressure on
1971 * the lower bands. So we force-demote the least-recently-used-and-thawed
1974 * - 'force_one == FALSE': (normal mode)
1975 * If the # of thaws of a process is below our threshold, then we
1976 * will demote that process into the IDLE band.
1977 * We don't immediately kill the process here because it already has
1978 * state on disk and so it might be worth giving it another shot at
1979 * getting thawed/resumed and used.
1982 memorystatus_demote_frozen_processes(boolean_t force_one
)
1984 unsigned int band
= (unsigned int) memorystatus_freeze_jetsam_band
;
1985 unsigned int demoted_proc_count
= 0;
1986 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
1987 /* We demote to IDLE unless someone has asserted a higher priority on this process. */
1988 int maxpriority
= JETSAM_PRIORITY_IDLE
;
1992 if (memorystatus_freeze_enabled
== FALSE
) {
1994 * Freeze has been disabled likely to
1995 * reclaim swap space. So don't change
1996 * any state on the frozen processes.
2002 next_p
= memorystatus_get_first_proc_locked(&band
, FALSE
);
2005 next_p
= memorystatus_get_next_proc_locked(&band
, p
, FALSE
);
2007 if ((p
->p_memstat_state
& P_MEMSTAT_FROZEN
) == FALSE
) {
2011 if (p
->p_memstat_state
& P_MEMSTAT_LOCKED
) {
2015 if (force_one
== TRUE
) {
2016 if ((p
->p_memstat_state
& P_MEMSTAT_REFREEZE_ELIGIBLE
) == 0) {
2018 * This process hasn't been thawed recently and so most of
2019 * its state sits on NAND and so we skip it -- jetsamming it
2020 * won't help with memory pressure.
2025 if (p
->p_memstat_thaw_count
>= memorystatus_thaw_count_demotion_threshold
) {
2027 * This process has met / exceeded our thaw count demotion threshold
2028 * and so we let it live in the higher bands.
2034 p
->p_memstat_state
&= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND
;
2035 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
2037 maxpriority
= MAX(p
->p_memstat_assertionpriority
, maxpriority
);
2038 memorystatus_update_priority_locked(p
, maxpriority
, FALSE
, FALSE
);
2039 #if DEVELOPMENT || DEBUG
2040 os_log_with_startup_serial(OS_LOG_DEFAULT
, "memorystatus_demote_frozen_process(%s) pid %d [%s]",
2041 (force_one
? "urgent" : "normal"), (p
? p
->p_pid
: -1), ((p
&& *p
->p_name
) ? p
->p_name
: "unknown"));
2042 #endif /* DEVELOPMENT || DEBUG */
2045 * The freezer thread will consider this a normal app to be frozen
2046 * because it is in the IDLE band. So we don't need the
2047 * P_MEMSTAT_REFREEZE_ELIGIBLE state here. Also, if it gets resumed
2048 * we'll correctly count it as eligible for re-freeze again.
2050 * We don't drop the frozen count because this process still has
2051 * state on disk. So there's a chance it gets resumed and then it
2052 * should land in the higher jetsam band. For that it needs to
2053 * remain marked frozen.
2055 if (p
->p_memstat_state
& P_MEMSTAT_REFREEZE_ELIGIBLE
) {
2056 p
->p_memstat_state
&= ~P_MEMSTAT_REFREEZE_ELIGIBLE
;
2057 memorystatus_refreeze_eligible_count
--;
2060 demoted_proc_count
++;
2062 if ((force_one
== TRUE
) || (demoted_proc_count
== memorystatus_max_frozen_demotions_daily
)) {
2067 if (force_one
== FALSE
) {
2069 * We use these counters to track daily hit rates.
2070 * So we only reset them to 0 under the normal
2073 memorystatus_thaw_count
= 0;
2080 * Calculate a new freezer budget.
2081 * @param time_since_last_interval_expired_sec How long has it been (in seconds) since the previous interval expired.
2082 * @param burst_multiple The burst_multiple for the new period
2083 * @param interval_duration_min How many minutes will the new interval be?
2084 * @param rollover The amount to rollover from the previous budget.
2086 * @return A budget for the new interval.
2089 memorystatus_freeze_calculate_new_budget(
2090 unsigned int time_since_last_interval_expired_sec
,
2091 unsigned int burst_multiple
,
2092 unsigned int interval_duration_min
,
2095 uint64_t freeze_daily_budget
= 0, freeze_daily_budget_mb
= 0, daily_budget_pageouts
= 0, budget_missed
= 0, freeze_daily_pageouts_max
= 0, new_budget
= 0;
2096 const static unsigned int kNumSecondsInDay
= 60 * 60 * 24;
2097 /* Precision factor for days_missed. 2 decimal points. */
2098 const static unsigned int kFixedPointFactor
= 100;
2099 unsigned int days_missed
;
2101 /* Get the daily budget from the storage layer */
2102 if (vm_swap_max_budget(&freeze_daily_budget
)) {
2103 freeze_daily_budget_mb
= freeze_daily_budget
/ (1024 * 1024);
2104 assert(freeze_daily_budget_mb
<= UINT32_MAX
);
2105 memorystatus_freeze_daily_mb_max
= (unsigned int) freeze_daily_budget_mb
;
2106 os_log_with_startup_serial(OS_LOG_DEFAULT
, "memorystatus: memorystatus_freeze_daily_mb_max set to %dMB\n", memorystatus_freeze_daily_mb_max
);
2108 /* Calculate the daily pageout budget */
2109 freeze_daily_pageouts_max
= memorystatus_freeze_daily_mb_max
* (1024 * 1024 / PAGE_SIZE
);
2111 daily_budget_pageouts
= (burst_multiple
* (((uint64_t) interval_duration_min
* freeze_daily_pageouts_max
) / (kNumSecondsInDay
/ 60)));
2114 * Add additional budget for time since the interval expired.
2115 * For example, if the interval expired n days ago, we should get an additional n days
2116 * of budget since we didn't use any budget during those n days.
2118 days_missed
= time_since_last_interval_expired_sec
* kFixedPointFactor
/ kNumSecondsInDay
;
2119 budget_missed
= days_missed
* freeze_daily_pageouts_max
/ kFixedPointFactor
;
2120 new_budget
= rollover
+ daily_budget_pageouts
+ budget_missed
;
2121 return (uint32_t) MIN(new_budget
, UINT32_MAX
);
2125 memorystatus_freezer_stringify_error(
2126 const int freezer_error_code
,
2130 if (freezer_error_code
== FREEZER_ERROR_EXCESS_SHARED_MEMORY
) {
2131 memorystatus_freezer_stats
.mfs_error_excess_shared_memory_count
++;
2132 strlcpy(buffer
, "too much shared memory", len
);
2133 } else if (freezer_error_code
== FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO
) {
2134 memorystatus_freezer_stats
.mfs_error_low_private_shared_ratio_count
++;
2135 strlcpy(buffer
, "low private-shared pages ratio", len
);
2136 } else if (freezer_error_code
== FREEZER_ERROR_NO_COMPRESSOR_SPACE
) {
2137 memorystatus_freezer_stats
.mfs_error_no_compressor_space_count
++;
2138 strlcpy(buffer
, "no compressor space", len
);
2139 } else if (freezer_error_code
== FREEZER_ERROR_NO_SWAP_SPACE
) {
2140 memorystatus_freezer_stats
.mfs_error_no_swap_space_count
++;
2141 strlcpy(buffer
, "no swap space", len
);
2143 strlcpy(buffer
, "unknown error", len
);
2148 * Start a new normal throttle interval with the given budget.
2149 * Caller must hold the freezer mutex
2152 memorystatus_freeze_start_normal_throttle_interval(uint32_t new_budget
, mach_timespec_t start_ts
)
2154 LCK_MTX_ASSERT(&freezer_mutex
, LCK_MTX_ASSERT_OWNED
);
2156 normal_throttle_window
->max_pageouts
= new_budget
;
2157 normal_throttle_window
->ts
.tv_sec
= normal_throttle_window
->mins
* 60;
2158 normal_throttle_window
->ts
.tv_nsec
= 0;
2159 ADD_MACH_TIMESPEC(&normal_throttle_window
->ts
, &start_ts
);
2160 /* Since we update the throttle stats pre-freeze, adjust for overshoot here */
2161 if (normal_throttle_window
->pageouts
> normal_throttle_window
->max_pageouts
) {
2162 normal_throttle_window
->pageouts
-= normal_throttle_window
->max_pageouts
;
2164 normal_throttle_window
->pageouts
= 0;
2166 /* Ensure the normal window is now active. */
2167 memorystatus_freeze_degradation
= FALSE
;
2170 #if DEVELOPMENT || DEBUG
2173 sysctl_memorystatus_freeze_calculate_new_budget SYSCTL_HANDLER_ARGS
2175 #pragma unused(arg1, arg2)
2177 unsigned int time_since_last_interval_expired_sec
= 0;
2178 unsigned int new_budget
;
2180 error
= sysctl_handle_int(oidp
, &time_since_last_interval_expired_sec
, 0, req
);
2181 if (error
|| !req
->newptr
) {
2184 new_budget
= memorystatus_freeze_calculate_new_budget(time_since_last_interval_expired_sec
, 1, NORMAL_WINDOW_MINS
, 0);
2185 return copyout(&new_budget
, req
->oldptr
, MIN(sizeof(req
->oldlen
), sizeof(new_budget
)));
2188 SYSCTL_PROC(_vm
, OID_AUTO
, memorystatus_freeze_calculate_new_budget
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_MASKED
,
2189 0, 0, &sysctl_memorystatus_freeze_calculate_new_budget
, "I", "");
2191 #endif /* DEVELOPMENT || DEBUG */
2194 * This function will do 4 things:
2196 * 1) check to see if we are currently in a degraded freezer mode, and if so:
2197 * - check to see if our window has expired and we should exit this mode, OR,
2198 * - return a budget based on the degraded throttle window's max. pageouts vs current pageouts.
2200 * 2) check to see if we are in a NEW normal window and update the normal throttle window's params.
2202 * 3) check what the current normal window allows for a budget.
2204 * 4) calculate the current rate of pageouts for DEGRADED_WINDOW_MINS duration. If that rate is below
2205 * what we would normally expect, then we are running low on our daily budget and need to enter
2206 * degraded perf. mode.
2208 * Caller must hold the freezer mutex
2209 * Caller must not hold the proc_list lock
2213 memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed
)
2217 mach_timespec_t now_ts
;
2218 LCK_MTX_ASSERT(&freezer_mutex
, LCK_MTX_ASSERT_OWNED
);
2219 LCK_MTX_ASSERT(proc_list_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
2221 unsigned int freeze_daily_pageouts_max
= 0;
2222 uint32_t budget_rollover
= 0;
2224 #if DEVELOPMENT || DEBUG
2225 if (!memorystatus_freeze_throttle_enabled
) {
2227 * No throttling...we can use the full budget everytime.
2229 *budget_pages_allowed
= UINT64_MAX
;
2234 clock_get_system_nanotime(&sec
, &nsec
);
2235 now_ts
.tv_sec
= (unsigned int)(MIN(sec
, UINT32_MAX
));
2236 now_ts
.tv_nsec
= nsec
;
2238 struct throttle_interval_t
*interval
= NULL
;
2240 if (memorystatus_freeze_degradation
== TRUE
) {
2241 interval
= degraded_throttle_window
;
2243 if (CMP_MACH_TIMESPEC(&now_ts
, &interval
->ts
) >= 0) {
2244 interval
->pageouts
= 0;
2245 interval
->max_pageouts
= 0;
2247 *budget_pages_allowed
= interval
->max_pageouts
- interval
->pageouts
;
2251 interval
= normal_throttle_window
;
2253 if (CMP_MACH_TIMESPEC(&now_ts
, &interval
->ts
) >= 0) {
2254 /* How long has it been since the previous interval expired? */
2255 mach_timespec_t expiration_period_ts
= now_ts
;
2256 SUB_MACH_TIMESPEC(&expiration_period_ts
, &interval
->ts
);
2257 /* Get unused budget. Clamp to 0. We'll adjust for overused budget in the next interval. */
2258 budget_rollover
= interval
->pageouts
> interval
->max_pageouts
?
2259 0 : interval
->max_pageouts
- interval
->pageouts
;
2261 memorystatus_freeze_start_normal_throttle_interval(memorystatus_freeze_calculate_new_budget(
2262 expiration_period_ts
.tv_sec
, interval
->burst_multiple
,
2263 interval
->mins
, budget_rollover
),
2265 *budget_pages_allowed
= interval
->max_pageouts
;
2266 memorystatus_freezer_stats
.mfs_shared_pages_skipped
= 0;
2268 memorystatus_demote_frozen_processes(FALSE
); /* normal mode...don't force a demotion */
2271 * Current throttle window.
2272 * Deny freezing if we have no budget left.
2273 * Try graceful degradation if we are within 25% of:
2274 * - the daily budget, and
2275 * - the current budget left is below our normal budget expectations.
2278 if (memorystatus_freeze_degradation
== FALSE
) {
2279 if (interval
->pageouts
>= interval
->max_pageouts
) {
2280 *budget_pages_allowed
= 0;
2282 int budget_left
= interval
->max_pageouts
- interval
->pageouts
;
2283 int budget_threshold
= (freeze_daily_pageouts_max
* FREEZE_DEGRADATION_BUDGET_THRESHOLD
) / 100;
2285 mach_timespec_t time_left
= {0, 0};
2287 time_left
.tv_sec
= interval
->ts
.tv_sec
;
2288 time_left
.tv_nsec
= 0;
2290 SUB_MACH_TIMESPEC(&time_left
, &now_ts
);
2292 if (budget_left
<= budget_threshold
) {
2294 * For the current normal window, calculate how much we would pageout in a DEGRADED_WINDOW_MINS duration.
2295 * And also calculate what we would pageout for the same DEGRADED_WINDOW_MINS duration if we had the full
2296 * daily pageout budget.
2299 unsigned int current_budget_rate_allowed
= ((budget_left
/ time_left
.tv_sec
) / 60) * DEGRADED_WINDOW_MINS
;
2300 unsigned int normal_budget_rate_allowed
= (freeze_daily_pageouts_max
/ NORMAL_WINDOW_MINS
) * DEGRADED_WINDOW_MINS
;
2303 * The current rate of pageouts is below what we would expect for
2304 * the normal rate i.e. we have below normal budget left and so...
2307 if (current_budget_rate_allowed
< normal_budget_rate_allowed
) {
2308 memorystatus_freeze_degradation
= TRUE
;
2309 degraded_throttle_window
->max_pageouts
= current_budget_rate_allowed
;
2310 degraded_throttle_window
->pageouts
= 0;
2313 * Switch over to the degraded throttle window so the budget
2314 * doled out is based on that window.
2316 interval
= degraded_throttle_window
;
2320 *budget_pages_allowed
= interval
->max_pageouts
- interval
->pageouts
;
2325 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining; throttle %s\n",
2326 interval
->pageouts
, interval
->max_pageouts
, interval
->mins
, (interval
->ts
.tv_sec
- now_ts
->tv_sec
) / 60,
2327 interval
->throttle
? "on" : "off");
2331 memorystatus_freeze_thread(void *param __unused
, wait_result_t wr __unused
)
2333 static boolean_t memorystatus_freeze_swap_low
= FALSE
;
2335 lck_mtx_lock(&freezer_mutex
);
2337 if (memorystatus_freeze_enabled
) {
2338 if ((memorystatus_frozen_count
< memorystatus_frozen_processes_max
) ||
2339 (memorystatus_refreeze_eligible_count
>= MIN_THAW_REFREEZE_THRESHOLD
)) {
2340 if (memorystatus_can_freeze(&memorystatus_freeze_swap_low
)) {
2341 /* Only freeze if we've not exceeded our pageout budgets.*/
2342 memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining
);
2344 if (memorystatus_freeze_budget_pages_remaining
) {
2345 memorystatus_freeze_top_process();
2347 memorystatus_demote_frozen_processes(TRUE
); /* urgent mode..force one demotion */
2354 * We use memorystatus_apps_idle_delay_time because if/when we adopt aging for applications,
2355 * it'll tie neatly into running the freezer once we age an application.
2357 * Till then, it serves as a good interval that can be tuned via a sysctl too.
2359 memorystatus_freezer_thread_next_run_ts
= mach_absolute_time() + memorystatus_apps_idle_delay_time
;
2361 assert_wait((event_t
) &memorystatus_freeze_wakeup
, THREAD_UNINT
);
2362 lck_mtx_unlock(&freezer_mutex
);
2364 thread_block((thread_continue_t
) memorystatus_freeze_thread
);
2368 memorystatus_freeze_thread_should_run(void)
2371 * No freezer_mutex held here...see why near call-site
2372 * within memorystatus_pages_update().
2375 boolean_t should_run
= FALSE
;
2377 if (memorystatus_freeze_enabled
== FALSE
) {
2381 if (memorystatus_available_pages
> memorystatus_freeze_threshold
) {
2385 memorystatus_freezer_stats
.mfs_below_threshold_count
++;
2387 if ((memorystatus_frozen_count
>= memorystatus_frozen_processes_max
)) {
2389 * Consider this as a skip even if we wake up to refreeze because
2390 * we won't freeze any new procs.
2392 memorystatus_freezer_stats
.mfs_skipped_full_count
++;
2393 if (memorystatus_refreeze_eligible_count
< MIN_THAW_REFREEZE_THRESHOLD
) {
2398 if (memorystatus_frozen_shared_mb_max
&& (memorystatus_frozen_shared_mb
>= memorystatus_frozen_shared_mb_max
)) {
2399 memorystatus_freezer_stats
.mfs_skipped_shared_mb_high_count
++;
2403 uint64_t curr_time
= mach_absolute_time();
2405 if (curr_time
< memorystatus_freezer_thread_next_run_ts
) {
2416 memorystatus_get_process_is_freezable(pid_t pid
, int *is_freezable
)
2418 proc_t p
= PROC_NULL
;
2430 * Only allow this on the current proc for now.
2431 * We can check for privileges and allow targeting another process in the future.
2433 if (p
!= current_proc()) {
2439 *is_freezable
= ((p
->p_memstat_state
& P_MEMSTAT_FREEZE_DISABLED
) ? 0 : 1);
2440 proc_rele_locked(p
);
2447 memorystatus_set_process_is_freezable(pid_t pid
, boolean_t is_freezable
)
2449 proc_t p
= PROC_NULL
;
2456 * To enable freezable status, you need to be root or an entitlement.
2459 !kauth_cred_issuser(kauth_cred_get()) &&
2460 !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT
)) {
2470 * A process can change its own status. A coalition leader can
2471 * change the status of coalition members.
2473 if (p
!= current_proc()) {
2474 coalition_t coal
= task_get_coalition(proc_task(p
), COALITION_TYPE_JETSAM
);
2475 if (!coalition_is_leader(proc_task(current_proc()), coal
)) {
2482 if (is_freezable
== FALSE
) {
2483 /* Freeze preference set to FALSE. Set the P_MEMSTAT_FREEZE_DISABLED bit. */
2484 p
->p_memstat_state
|= P_MEMSTAT_FREEZE_DISABLED
;
2485 printf("memorystatus_set_process_is_freezable: disabling freeze for pid %d [%s]\n",
2486 p
->p_pid
, (*p
->p_name
? p
->p_name
: "unknown"));
2488 p
->p_memstat_state
&= ~P_MEMSTAT_FREEZE_DISABLED
;
2489 printf("memorystatus_set_process_is_freezable: enabling freeze for pid %d [%s]\n",
2490 p
->p_pid
, (*p
->p_name
? p
->p_name
: "unknown"));
2492 proc_rele_locked(p
);
2499 sysctl_memorystatus_do_fastwake_warmup_all SYSCTL_HANDLER_ARGS
2501 #pragma unused(oidp, arg1, arg2)
2507 /* Need to be root or have entitlement */
2508 if (!kauth_cred_issuser(kauth_cred_get()) && !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT
)) {
2512 if (memorystatus_freeze_enabled
== FALSE
) {
2516 do_fastwake_warmup_all();
2521 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_do_fastwake_warmup_all
, CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
| CTLFLAG_MASKED
,
2522 0, 0, &sysctl_memorystatus_do_fastwake_warmup_all
, "I", "");
2524 #endif /* CONFIG_FREEZE */