2 * Copyright (c) 2006 Apple Computer, 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 <libkern/libkern.h>
39 #include <mach/mach_time.h>
40 #include <mach/task.h>
41 #include <mach/host_priv.h>
42 #include <mach/mach_host.h>
43 #include <pexpert/pexpert.h>
44 #include <sys/kern_event.h>
46 #include <sys/proc_info.h>
47 #include <sys/signal.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/sysproto.h>
54 #include <vm/vm_pageout.h>
55 #include <vm/vm_protos.h>
58 #include <vm/vm_map.h>
59 #endif /* CONFIG_FREEZE */
61 #include <sys/kern_memorystatus.h>
64 /* For logging clarity */
65 static const char *jetsam_kill_cause_name
[] = {
67 "jettisoned" , /* kMemorystatusKilled */
68 "highwater" , /* kMemorystatusKilledHiwat */
69 "vnode-limit" , /* kMemorystatusKilledVnodes */
70 "vm-pageshortage" , /* kMemorystatusKilledVMPageShortage */
71 "vm-thrashing" , /* kMemorystatusKilledVMThrashing */
72 "fc-thrashing" , /* kMemorystatusKilledFCThrashing */
73 "per-process-limit" , /* kMemorystatusKilledPerProcessLimit */
74 "diagnostic" , /* kMemorystatusKilledDiagnostic */
75 "idle-exit" , /* kMemorystatusKilledIdleExit */
78 /* Does cause indicate vm or fc thrashing? */
80 is_thrashing(unsigned cause
)
83 case kMemorystatusKilledVMThrashing
:
84 case kMemorystatusKilledFCThrashing
:
91 /* Callback into vm_compressor.c to signal that thrashing has been mitigated. */
92 extern void vm_thrashing_jetsam_done(void);
95 /* These are very verbose printfs(), enable with
96 * MEMORYSTATUS_DEBUG_LOG
98 #if MEMORYSTATUS_DEBUG_LOG
99 #define MEMORYSTATUS_DEBUG(cond, format, ...) \
101 if (cond) { printf(format, ##__VA_ARGS__); } \
104 #define MEMORYSTATUS_DEBUG(cond, format, ...)
107 /* General tunables */
109 unsigned long delta_percentage
= 5;
110 unsigned long critical_threshold_percentage
= 5;
111 unsigned long idle_offset_percentage
= 5;
112 unsigned long pressure_threshold_percentage
= 15;
113 unsigned long freeze_threshold_percentage
= 50;
115 /* General memorystatus stuff */
117 struct klist memorystatus_klist
;
118 static lck_mtx_t memorystatus_klist_mutex
;
120 static void memorystatus_klist_lock(void);
121 static void memorystatus_klist_unlock(void);
123 static uint64_t memorystatus_idle_delay_time
= 0;
126 * Memorystatus kevents
129 static int filt_memorystatusattach(struct knote
*kn
);
130 static void filt_memorystatusdetach(struct knote
*kn
);
131 static int filt_memorystatus(struct knote
*kn
, long hint
);
133 struct filterops memorystatus_filtops
= {
134 .f_attach
= filt_memorystatusattach
,
135 .f_detach
= filt_memorystatusdetach
,
136 .f_event
= filt_memorystatus
,
140 kMemorystatusNoPressure
= 0x1,
141 kMemorystatusPressure
= 0x2,
142 kMemorystatusLowSwap
= 0x4
145 /* Idle guard handling */
147 static int32_t memorystatus_scheduled_idle_demotions
= 0;
149 static thread_call_t memorystatus_idle_demotion_call
;
151 static void memorystatus_perform_idle_demotion(__unused
void *spare1
, __unused
void *spare2
);
152 static void memorystatus_schedule_idle_demotion_locked(proc_t p
, boolean_t set_state
);
153 static void memorystatus_invalidate_idle_demotion_locked(proc_t p
, boolean_t clean_state
);
154 static void memorystatus_reschedule_idle_demotion_locked(void);
156 static void memorystatus_update_priority_locked(proc_t p
, int priority
, boolean_t head_insert
);
158 boolean_t
is_knote_registered_modify_task_pressure_bits(struct knote
*, int, task_t
, vm_pressure_level_t
, vm_pressure_level_t
);
159 void memorystatus_send_low_swap_note(void);
161 int memorystatus_wakeup
= 0;
163 unsigned int memorystatus_level
= 0;
165 static int memorystatus_list_count
= 0;
167 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1)
169 typedef struct memstat_bucket
{
170 TAILQ_HEAD(, proc
) list
;
174 memstat_bucket_t memstat_bucket
[MEMSTAT_BUCKET_COUNT
];
176 uint64_t memstat_idle_demotion_deadline
= 0;
178 static unsigned int memorystatus_dirty_count
= 0;
182 memorystatus_get_level(__unused
struct proc
*p
, struct memorystatus_get_level_args
*args
, __unused
int *ret
)
184 user_addr_t level
= 0;
188 if (copyout(&memorystatus_level
, level
, sizeof(memorystatus_level
)) != 0) {
195 static proc_t
memorystatus_get_first_proc_locked(unsigned int *bucket_index
, boolean_t search
);
196 static proc_t
memorystatus_get_next_proc_locked(unsigned int *bucket_index
, proc_t p
, boolean_t search
);
198 static void memorystatus_thread(void *param __unused
, wait_result_t wr __unused
);
204 int proc_get_memstat_priority(proc_t
, boolean_t
);
206 /* Kill processes exceeding their limit either under memory pressure (1), or as soon as possible (0) */
207 #define LEGACY_HIWATER 1
209 static boolean_t memorystatus_idle_snapshot
= 0;
211 static int memorystatus_highwater_enabled
= 1;
213 unsigned int memorystatus_delta
= 0;
215 static unsigned int memorystatus_available_pages_critical_base
= 0;
216 //static unsigned int memorystatus_last_foreground_pressure_pages = (unsigned int)-1;
217 static unsigned int memorystatus_available_pages_critical_idle_offset
= 0;
219 #if DEVELOPMENT || DEBUG
220 static unsigned int memorystatus_jetsam_panic_debug
= 0;
222 static unsigned int memorystatus_jetsam_policy
= kPolicyDefault
;
223 static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic
= 0;
226 static unsigned int memorystatus_thread_wasted_wakeup
= 0;
228 static uint32_t kill_under_pressure_cause
= 0;
230 static memorystatus_jetsam_snapshot_t
*memorystatus_jetsam_snapshot
;
231 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries
233 static unsigned int memorystatus_jetsam_snapshot_count
= 0;
234 static unsigned int memorystatus_jetsam_snapshot_max
= 0;
236 static void memorystatus_clear_errors(void);
237 static void memorystatus_get_task_page_counts(task_t task
, uint32_t *footprint
, uint32_t *max_footprint
, uint32_t *max_footprint_lifetime
, uint32_t *purgeable_pages
);
238 static uint32_t memorystatus_build_state(proc_t p
);
239 static void memorystatus_update_levels_locked(boolean_t critical_only
);
240 //static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured);
242 static boolean_t
memorystatus_kill_specific_process(pid_t victim_pid
, uint32_t cause
);
243 static boolean_t
memorystatus_kill_top_process(boolean_t any
, uint32_t cause
, int32_t *priority
, uint32_t *errors
);
245 static boolean_t
memorystatus_kill_hiwat_proc(uint32_t *errors
);
248 static boolean_t
memorystatus_kill_process_async(pid_t victim_pid
, uint32_t cause
);
249 static boolean_t
memorystatus_kill_process_sync(pid_t victim_pid
, uint32_t cause
);
251 #endif /* CONFIG_JETSAM */
255 extern unsigned int vm_page_free_count
;
256 extern unsigned int vm_page_active_count
;
257 extern unsigned int vm_page_inactive_count
;
258 extern unsigned int vm_page_throttled_count
;
259 extern unsigned int vm_page_purgeable_count
;
260 extern unsigned int vm_page_wire_count
;
262 #if VM_PRESSURE_EVENTS
264 #include "vm_pressure.h"
266 extern boolean_t
memorystatus_warn_process(pid_t pid
, boolean_t critical
);
268 vm_pressure_level_t memorystatus_vm_pressure_level
= kVMPressureNormal
;
270 #if CONFIG_MEMORYSTATUS
271 unsigned int memorystatus_available_pages
= (unsigned int)-1;
272 unsigned int memorystatus_available_pages_pressure
= 0;
273 unsigned int memorystatus_available_pages_critical
= 0;
274 unsigned int memorystatus_frozen_count
= 0;
275 unsigned int memorystatus_suspended_count
= 0;
278 * We use this flag to signal if we have any HWM offenders
279 * on the system. This way we can reduce the number of wakeups
280 * of the memorystatus_thread when the system is between the
281 * "pressure" and "critical" threshold.
283 * The (re-)setting of this variable is done without any locks
284 * or synchronization simply because it is not possible (currently)
285 * to keep track of HWM offenders that drop down below their memory
286 * limit and/or exit. So, we choose to burn a couple of wasted wakeups
287 * by allowing the unguarded modification of this variable.
289 boolean_t memorystatus_hwm_candidates
= 0;
291 static int memorystatus_send_note(int event_code
, void *data
, size_t data_length
);
292 #endif /* CONFIG_MEMORYSTATUS */
294 #endif /* VM_PRESSURE_EVENTS */
300 boolean_t memorystatus_freeze_enabled
= FALSE
;
301 int memorystatus_freeze_wakeup
= 0;
303 static inline boolean_t
memorystatus_can_freeze_processes(void);
304 static boolean_t
memorystatus_can_freeze(boolean_t
*memorystatus_freeze_swap_low
);
306 static void memorystatus_freeze_thread(void *param __unused
, wait_result_t wr __unused
);
309 static unsigned int memorystatus_freeze_threshold
= 0;
311 static unsigned int memorystatus_freeze_pages_min
= 0;
312 static unsigned int memorystatus_freeze_pages_max
= 0;
314 static unsigned int memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
;
317 static uint64_t memorystatus_freeze_count
= 0;
318 static uint64_t memorystatus_freeze_pageouts
= 0;
321 static throttle_interval_t throttle_intervals
[] = {
322 { 60, 8, 0, 0, { 0, 0 }, FALSE
}, /* 1 hour intermediate interval, 8x burst */
323 { 24 * 60, 1, 0, 0, { 0, 0 }, FALSE
}, /* 24 hour long interval, no burst */
326 static uint64_t memorystatus_freeze_throttle_count
= 0;
328 static unsigned int memorystatus_suspended_footprint_total
= 0;
330 #endif /* CONFIG_FREEZE */
334 extern struct knote
*vm_find_knote_from_pid(pid_t
, struct klist
*);
336 #if DEVELOPMENT || DEBUG
340 /* Debug aid to aid determination of limit */
343 sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS
345 #pragma unused(oidp, arg2)
348 int error
, enable
= 0;
351 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
352 if (error
|| !req
->newptr
) {
356 error
= SYSCTL_IN(req
, &enable
, sizeof(int));
357 if (error
|| !req
->newptr
) {
361 if (!(enable
== 0 || enable
== 1)) {
367 p
= memorystatus_get_first_proc_locked(&b
, TRUE
);
370 if ((p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
) && (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
)) {
373 memlimit
= p
->p_memstat_memlimit
;
378 task_set_phys_footprint_limit_internal(p
->task
, (memlimit
> 0) ? memlimit
: -1, NULL
, TRUE
);
380 if (memlimit
== -1) {
381 p
->p_memstat_state
|= P_MEMSTAT_FATAL_MEMLIMIT
;
383 if (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
) {
384 p
->p_memstat_state
&= ~P_MEMSTAT_FATAL_MEMLIMIT
;
388 p
= memorystatus_get_next_proc_locked(&b
, p
, TRUE
);
391 memorystatus_highwater_enabled
= enable
;
398 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_idle_snapshot
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_idle_snapshot
, 0, "");
400 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_highwater_enabled
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_highwater_enabled
, 0, sysctl_memorystatus_highwater_enable
, "I", "");
402 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_available_pages
, 0, "");
403 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_critical
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_available_pages_critical
, 0, "");
404 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_critical_base
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_available_pages_critical_base
, 0, "");
405 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_critical_idle_offset
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_available_pages_critical_idle_offset
, 0, "");
407 /* Diagnostic code */
410 kJetsamDiagnosticModeNone
= 0,
411 kJetsamDiagnosticModeAll
= 1,
412 kJetsamDiagnosticModeStopAtFirstActive
= 2,
413 kJetsamDiagnosticModeCount
414 } jetsam_diagnostic_mode
= kJetsamDiagnosticModeNone
;
416 static int jetsam_diagnostic_suspended_one_active_proc
= 0;
419 sysctl_jetsam_diagnostic_mode SYSCTL_HANDLER_ARGS
421 #pragma unused(arg1, arg2)
423 const char *diagnosticStrings
[] = {
424 "jetsam: diagnostic mode: resetting critical level.",
425 "jetsam: diagnostic mode: will examine all processes",
426 "jetsam: diagnostic mode: will stop at first active process"
429 int error
, val
= jetsam_diagnostic_mode
;
430 boolean_t changed
= FALSE
;
432 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
433 if (error
|| !req
->newptr
)
435 if ((val
< 0) || (val
>= kJetsamDiagnosticModeCount
)) {
436 printf("jetsam: diagnostic mode: invalid value - %d\n", val
);
442 if ((unsigned int) val
!= jetsam_diagnostic_mode
) {
443 jetsam_diagnostic_mode
= val
;
445 memorystatus_jetsam_policy
&= ~kPolicyDiagnoseActive
;
447 switch (jetsam_diagnostic_mode
) {
448 case kJetsamDiagnosticModeNone
:
449 /* Already cleared */
451 case kJetsamDiagnosticModeAll
:
452 memorystatus_jetsam_policy
|= kPolicyDiagnoseAll
;
454 case kJetsamDiagnosticModeStopAtFirstActive
:
455 memorystatus_jetsam_policy
|= kPolicyDiagnoseFirst
;
458 /* Already validated */
462 memorystatus_update_levels_locked(FALSE
);
469 printf("%s\n", diagnosticStrings
[val
]);
475 SYSCTL_PROC(_debug
, OID_AUTO
, jetsam_diagnostic_mode
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
|CTLFLAG_ANYBODY
,
476 &jetsam_diagnostic_mode
, 0, sysctl_jetsam_diagnostic_mode
, "I", "Jetsam Diagnostic Mode");
478 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_jetsam_policy_offset_pages_diagnostic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_jetsam_policy_offset_pages_diagnostic
, 0, "");
480 #if VM_PRESSURE_EVENTS
482 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_pressure
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_available_pages_pressure
, 0, "");
486 * This routine is used for targeted notifications
487 * regardless of system memory pressure.
488 * "memnote" is the current user.
492 sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS
494 #pragma unused(arg1, arg2)
496 int error
= 0, pid
= 0;
498 struct knote
*kn
= NULL
;
500 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
501 if (error
|| !req
->newptr
)
505 * We inspect 3 lists here for targeted notifications:
506 * - memorystatus_klist
507 * - vm_pressure_klist
508 * - vm_pressure_dormant_klist
510 * The vm_pressure_* lists are tied to the old VM_PRESSURE
511 * notification mechanism. We intend to stop using that
512 * mechanism and, in turn, get rid of the 2 lists and
513 * vm_dispatch_pressure_note_to_pid() too.
516 memorystatus_klist_lock();
517 kn
= vm_find_knote_from_pid(pid
, &memorystatus_klist
);
520 * Forcibly send this pid a "warning" memory pressure notification.
522 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
523 KNOTE(&memorystatus_klist
, kMemorystatusPressure
);
526 ret
= vm_dispatch_pressure_note_to_pid(pid
, FALSE
);
528 memorystatus_klist_unlock();
533 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_vm_pressure_send
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
534 0, 0, &sysctl_memorystatus_vm_pressure_send
, "I", "");
536 #endif /* VM_PRESSURE_EVENTS */
538 #endif /* CONFIG_JETSAM */
542 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_threshold
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_threshold
, 0, "");
544 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_pages_min
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_pages_min
, 0, "");
545 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_pages_max
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_pages_max
, 0, "");
547 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_count
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_freeze_count
, "");
548 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_pageouts
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_freeze_pageouts
, "");
549 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_throttle_count
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_freeze_throttle_count
, "");
550 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_min_processes
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_suspended_threshold
, 0, "");
552 boolean_t memorystatus_freeze_throttle_enabled
= TRUE
;
553 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_throttle_enabled
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_throttle_enabled
, 0, "");
556 * Manual trigger of freeze and thaw for dev / debug kernels only.
559 sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS
561 #pragma unused(arg1, arg2)
566 if (memorystatus_freeze_enabled
== FALSE
) {
570 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
571 if (error
|| !req
->newptr
)
576 uint32_t purgeable
, wired
, clean
, dirty
;
578 uint32_t max_pages
= 0;
580 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
581 max_pages
= MIN(default_pager_swap_pages_free(), memorystatus_freeze_pages_max
);
583 max_pages
= UINT32_MAX
- 1;
585 error
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, FALSE
);
595 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_freeze
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
596 0, 0, &sysctl_memorystatus_freeze
, "I", "");
599 sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS
601 #pragma unused(arg1, arg2)
606 if (memorystatus_freeze_enabled
== FALSE
) {
610 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
611 if (error
|| !req
->newptr
)
616 error
= task_thaw(p
->task
);
627 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_thaw
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
628 0, 0, &sysctl_memorystatus_available_pages_thaw
, "I", "");
630 #endif /* CONFIG_FREEZE */
632 #endif /* DEVELOPMENT || DEBUG */
634 extern kern_return_t
kernel_thread_start_priority(thread_continue_t continuation
,
637 thread_t
*new_thread
);
641 * Sort processes by size for a single jetsam bucket.
644 static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index
)
646 proc_t p
= NULL
, insert_after_proc
= NULL
, max_proc
= NULL
;
647 uint32_t pages
= 0, max_pages
= 0;
648 memstat_bucket_t
*current_bucket
;
650 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
654 current_bucket
= &memstat_bucket
[bucket_index
];
656 p
= TAILQ_FIRST(¤t_bucket
->list
);
659 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
661 insert_after_proc
= NULL
;
663 p
= TAILQ_NEXT(p
, p_memstat_list
);
668 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
670 if (pages
> max_pages
) {
675 p
= TAILQ_NEXT(p
, p_memstat_list
);
680 TAILQ_REMOVE(¤t_bucket
->list
, max_proc
, p_memstat_list
);
682 if (insert_after_proc
== NULL
) {
683 TAILQ_INSERT_HEAD(¤t_bucket
->list
, max_proc
, p_memstat_list
);
685 TAILQ_INSERT_AFTER(¤t_bucket
->list
, insert_after_proc
, max_proc
, p_memstat_list
);
688 insert_after_proc
= max_proc
;
690 /* Reset parameters for the new search. */
691 p
= TAILQ_NEXT(max_proc
, p_memstat_list
);
693 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
703 #endif /* CONFIG_JETSAM */
705 static proc_t
memorystatus_get_first_proc_locked(unsigned int *bucket_index
, boolean_t search
) {
706 memstat_bucket_t
*current_bucket
;
709 if ((*bucket_index
) >= MEMSTAT_BUCKET_COUNT
) {
713 current_bucket
= &memstat_bucket
[*bucket_index
];
714 next_p
= TAILQ_FIRST(¤t_bucket
->list
);
715 if (!next_p
&& search
) {
716 while (!next_p
&& (++(*bucket_index
) < MEMSTAT_BUCKET_COUNT
)) {
717 current_bucket
= &memstat_bucket
[*bucket_index
];
718 next_p
= TAILQ_FIRST(¤t_bucket
->list
);
725 static proc_t
memorystatus_get_next_proc_locked(unsigned int *bucket_index
, proc_t p
, boolean_t search
) {
726 memstat_bucket_t
*current_bucket
;
729 if (!p
|| ((*bucket_index
) >= MEMSTAT_BUCKET_COUNT
)) {
733 next_p
= TAILQ_NEXT(p
, p_memstat_list
);
734 while (!next_p
&& search
&& (++(*bucket_index
) < MEMSTAT_BUCKET_COUNT
)) {
735 current_bucket
= &memstat_bucket
[*bucket_index
];
736 next_p
= TAILQ_FIRST(¤t_bucket
->list
);
742 __private_extern__
void
743 memorystatus_init(void)
745 thread_t thread
= THREAD_NULL
;
746 kern_return_t result
;
750 memorystatus_freeze_pages_min
= FREEZE_PAGES_MIN
;
751 memorystatus_freeze_pages_max
= FREEZE_PAGES_MAX
;
754 nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS
* NSEC_PER_SEC
, &memorystatus_idle_delay_time
);
757 for (i
= 0; i
< MEMSTAT_BUCKET_COUNT
; i
++) {
758 TAILQ_INIT(&memstat_bucket
[i
].list
);
759 memstat_bucket
[i
].count
= 0;
762 memorystatus_idle_demotion_call
= thread_call_allocate((thread_call_func_t
)memorystatus_perform_idle_demotion
, NULL
);
764 /* Apply overrides */
765 PE_get_default("kern.jetsam_delta", &delta_percentage
, sizeof(delta_percentage
));
766 assert(delta_percentage
< 100);
767 PE_get_default("kern.jetsam_critical_threshold", &critical_threshold_percentage
, sizeof(critical_threshold_percentage
));
768 assert(critical_threshold_percentage
< 100);
769 PE_get_default("kern.jetsam_idle_offset", &idle_offset_percentage
, sizeof(idle_offset_percentage
));
770 assert(idle_offset_percentage
< 100);
771 PE_get_default("kern.jetsam_pressure_threshold", &pressure_threshold_percentage
, sizeof(pressure_threshold_percentage
));
772 assert(pressure_threshold_percentage
< 100);
773 PE_get_default("kern.jetsam_freeze_threshold", &freeze_threshold_percentage
, sizeof(freeze_threshold_percentage
));
774 assert(freeze_threshold_percentage
< 100);
777 memorystatus_delta
= delta_percentage
* atop_64(max_mem
) / 100;
778 memorystatus_available_pages_critical_idle_offset
= idle_offset_percentage
* atop_64(max_mem
) / 100;
779 memorystatus_available_pages_critical_base
= (critical_threshold_percentage
/ delta_percentage
) * memorystatus_delta
;
781 memorystatus_jetsam_snapshot_max
= maxproc
;
782 memorystatus_jetsam_snapshot
=
783 (memorystatus_jetsam_snapshot_t
*)kalloc(sizeof(memorystatus_jetsam_snapshot_t
) +
784 sizeof(memorystatus_jetsam_snapshot_entry_t
) * memorystatus_jetsam_snapshot_max
);
785 if (!memorystatus_jetsam_snapshot
) {
786 panic("Could not allocate memorystatus_jetsam_snapshot");
789 /* No contention at this point */
790 memorystatus_update_levels_locked(FALSE
);
794 memorystatus_freeze_threshold
= (freeze_threshold_percentage
/ delta_percentage
) * memorystatus_delta
;
797 result
= kernel_thread_start_priority(memorystatus_thread
, NULL
, 95 /* MAXPRI_KERNEL */, &thread
);
798 if (result
== KERN_SUCCESS
) {
799 thread_deallocate(thread
);
801 panic("Could not create memorystatus_thread");
805 /* Centralised for the purposes of allowing panic-on-jetsam */
807 vm_wake_compactor_swapper(void);
810 * The jetsam no frills kill call
811 * Return: 0 on success
812 * error code on failure (EINVAL...)
815 jetsam_do_kill(proc_t p
, int jetsam_flags
) {
817 error
= exit1_internal(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
, FALSE
, FALSE
, jetsam_flags
);
822 * Wrapper for processes exiting with memorystatus details
825 memorystatus_do_kill(proc_t p
, uint32_t cause
) {
828 __unused pid_t victim_pid
= p
->p_pid
;
830 KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DO_KILL
)) | DBG_FUNC_START
,
831 victim_pid
, cause
, vm_page_free_count
, 0, 0);
833 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
834 if (memorystatus_jetsam_panic_debug
& (1 << cause
)) {
835 panic("memorystatus_do_kill(): jetsam debug panic (cause: %d)", cause
);
838 #pragma unused(cause)
840 int jetsam_flags
= P_LTERM_JETSAM
;
842 case kMemorystatusKilledHiwat
: jetsam_flags
|= P_JETSAM_HIWAT
; break;
843 case kMemorystatusKilledVnodes
: jetsam_flags
|= P_JETSAM_VNODE
; break;
844 case kMemorystatusKilledVMPageShortage
: jetsam_flags
|= P_JETSAM_VMPAGESHORTAGE
; break;
845 case kMemorystatusKilledVMThrashing
: jetsam_flags
|= P_JETSAM_VMTHRASHING
; break;
846 case kMemorystatusKilledFCThrashing
: jetsam_flags
|= P_JETSAM_FCTHRASHING
; break;
847 case kMemorystatusKilledPerProcessLimit
: jetsam_flags
|= P_JETSAM_PID
; break;
848 case kMemorystatusKilledIdleExit
: jetsam_flags
|= P_JETSAM_IDLEEXIT
; break;
850 error
= jetsam_do_kill(p
, jetsam_flags
);
852 KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DO_KILL
)) | DBG_FUNC_END
,
853 victim_pid
, cause
, vm_page_free_count
, error
, 0);
855 if (COMPRESSED_PAGER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE
) {
856 vm_wake_compactor_swapper();
867 memorystatus_check_levels_locked(void) {
870 memorystatus_update_levels_locked(TRUE
);
875 memorystatus_perform_idle_demotion(__unused
void *spare1
, __unused
void *spare2
)
878 uint64_t current_time
;
879 memstat_bucket_t
*demotion_bucket
;
881 MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion()\n");
883 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_IDLE_DEMOTE
) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
885 current_time
= mach_absolute_time();
889 demotion_bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE_DEFERRED
];
890 p
= TAILQ_FIRST(&demotion_bucket
->list
);
893 MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion() found %d\n", p
->p_pid
);
895 assert(p
->p_memstat_idledeadline
);
896 assert(p
->p_memstat_dirty
& P_DIRTY_DEFER_IN_PROGRESS
);
897 assert((p
->p_memstat_dirty
& (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_IS_DIRTY
)) == P_DIRTY_IDLE_EXIT_ENABLED
);
899 if (current_time
>= p
->p_memstat_idledeadline
) {
900 #if DEBUG || DEVELOPMENT
901 if (!(p
->p_memstat_dirty
& P_DIRTY_MARKED
)) {
902 printf("memorystatus_perform_idle_demotion: moving process %d [%s] to idle band, but never dirtied (0x%x)!\n",
903 p
->p_pid
, (p
->p_comm
? p
->p_comm
: "(unknown)"), p
->p_memstat_dirty
);
906 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
907 memorystatus_update_priority_locked(p
, JETSAM_PRIORITY_IDLE
, false);
909 // The prior process has moved out of the demotion bucket, so grab the new head and continue
910 p
= TAILQ_FIRST(&demotion_bucket
->list
);
914 // No further candidates
918 memorystatus_reschedule_idle_demotion_locked();
922 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_IDLE_DEMOTE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
926 memorystatus_schedule_idle_demotion_locked(proc_t p
, boolean_t set_state
)
928 boolean_t present_in_deferred_bucket
= FALSE
;
930 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
931 present_in_deferred_bucket
= TRUE
;
934 MEMORYSTATUS_DEBUG(1, "memorystatus_schedule_idle_demotion_locked: scheduling demotion to idle band for process %d (dirty:0x%x, set_state %d, demotions %d).\n",
935 p
->p_pid
, p
->p_memstat_dirty
, set_state
, memorystatus_scheduled_idle_demotions
);
937 assert((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
);
940 assert(p
->p_memstat_idledeadline
== 0);
941 p
->p_memstat_dirty
|= P_DIRTY_DEFER_IN_PROGRESS
;
942 p
->p_memstat_idledeadline
= mach_absolute_time() + memorystatus_idle_delay_time
;
945 assert(p
->p_memstat_idledeadline
);
947 if (present_in_deferred_bucket
== FALSE
) {
948 memorystatus_scheduled_idle_demotions
++;
953 memorystatus_invalidate_idle_demotion_locked(proc_t p
, boolean_t clear_state
)
955 boolean_t present_in_deferred_bucket
= FALSE
;
957 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
958 present_in_deferred_bucket
= TRUE
;
959 assert(p
->p_memstat_idledeadline
);
962 MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for process %d (clear_state %d, demotions %d).\n",
963 p
->p_pid
, clear_state
, memorystatus_scheduled_idle_demotions
);
967 p
->p_memstat_idledeadline
= 0;
968 p
->p_memstat_dirty
&= ~P_DIRTY_DEFER_IN_PROGRESS
;
971 if (present_in_deferred_bucket
== TRUE
) {
972 memorystatus_scheduled_idle_demotions
--;
975 assert(memorystatus_scheduled_idle_demotions
>= 0);
979 memorystatus_reschedule_idle_demotion_locked(void) {
980 if (0 == memorystatus_scheduled_idle_demotions
) {
981 if (memstat_idle_demotion_deadline
) {
982 /* Transitioned 1->0, so cancel next call */
983 thread_call_cancel(memorystatus_idle_demotion_call
);
984 memstat_idle_demotion_deadline
= 0;
987 memstat_bucket_t
*demotion_bucket
;
989 demotion_bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE_DEFERRED
];
990 p
= TAILQ_FIRST(&demotion_bucket
->list
);
992 assert(p
&& p
->p_memstat_idledeadline
);
994 if (memstat_idle_demotion_deadline
!= p
->p_memstat_idledeadline
){
995 thread_call_enter_delayed(memorystatus_idle_demotion_call
, p
->p_memstat_idledeadline
);
996 memstat_idle_demotion_deadline
= p
->p_memstat_idledeadline
;
1006 memorystatus_add(proc_t p
, boolean_t locked
)
1008 memstat_bucket_t
*bucket
;
1010 MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding process %d with priority %d.\n", p
->p_pid
, p
->p_memstat_effectivepriority
);
1016 /* Processes marked internal do not have priority tracked */
1017 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
1021 bucket
= &memstat_bucket
[p
->p_memstat_effectivepriority
];
1023 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1024 assert(bucket
->count
== memorystatus_scheduled_idle_demotions
);
1027 TAILQ_INSERT_TAIL(&bucket
->list
, p
, p_memstat_list
);
1030 memorystatus_list_count
++;
1032 memorystatus_check_levels_locked();
1043 memorystatus_update_priority_locked(proc_t p
, int priority
, boolean_t head_insert
)
1045 memstat_bucket_t
*old_bucket
, *new_bucket
;
1047 assert(priority
< MEMSTAT_BUCKET_COUNT
);
1049 /* Ensure that exit isn't underway, leaving the proc retained but removed from its bucket */
1050 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1054 MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting process %d to priority %d, inserting at %s\n",
1055 p
->p_pid
, priority
, head_insert
? "head" : "tail");
1057 old_bucket
= &memstat_bucket
[p
->p_memstat_effectivepriority
];
1058 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1059 assert(old_bucket
->count
== (memorystatus_scheduled_idle_demotions
+ 1));
1062 TAILQ_REMOVE(&old_bucket
->list
, p
, p_memstat_list
);
1063 old_bucket
->count
--;
1065 new_bucket
= &memstat_bucket
[priority
];
1067 TAILQ_INSERT_HEAD(&new_bucket
->list
, p
, p_memstat_list
);
1069 TAILQ_INSERT_TAIL(&new_bucket
->list
, p
, p_memstat_list
);
1070 new_bucket
->count
++;
1073 if (memorystatus_highwater_enabled
&& (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
)) {
1076 * Adjust memory limit based on if the task is going to/from foreground and background.
1079 if (((priority
>= JETSAM_PRIORITY_FOREGROUND
) && (p
->p_memstat_effectivepriority
< JETSAM_PRIORITY_FOREGROUND
)) ||
1080 ((priority
< JETSAM_PRIORITY_FOREGROUND
) && (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
))) {
1081 int32_t memlimit
= (priority
>= JETSAM_PRIORITY_FOREGROUND
) ? -1 : p
->p_memstat_memlimit
;
1082 task_set_phys_footprint_limit_internal(p
->task
, (memlimit
> 0) ? memlimit
: -1, NULL
, TRUE
);
1084 if (memlimit
<= 0) {
1085 p
->p_memstat_state
|= P_MEMSTAT_FATAL_MEMLIMIT
;
1087 p
->p_memstat_state
&= ~P_MEMSTAT_FATAL_MEMLIMIT
;
1093 p
->p_memstat_effectivepriority
= priority
;
1095 memorystatus_check_levels_locked();
1099 memorystatus_update(proc_t p
, int priority
, uint64_t user_data
, boolean_t effective
, boolean_t update_memlimit
, int32_t memlimit
, boolean_t memlimit_background
, boolean_t is_fatal_limit
)
1102 boolean_t head_insert
= false;
1105 #pragma unused(update_memlimit, memlimit, memlimit_background, is_fatal_limit)
1108 MEMORYSTATUS_DEBUG(1, "memorystatus_update: changing process %d: priority %d, user_data 0x%llx\n", p
->p_pid
, priority
, user_data
);
1110 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_UPDATE
) | DBG_FUNC_START
, p
->p_pid
, priority
, user_data
, effective
, 0);
1112 if (priority
== -1) {
1113 /* Use as shorthand for default priority */
1114 priority
= JETSAM_PRIORITY_DEFAULT
;
1115 } else if (priority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1116 /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */
1117 priority
= JETSAM_PRIORITY_IDLE
;
1118 } else if (priority
== JETSAM_PRIORITY_IDLE_HEAD
) {
1119 /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */
1120 priority
= JETSAM_PRIORITY_IDLE
;
1122 } else if ((priority
< 0) || (priority
>= MEMSTAT_BUCKET_COUNT
)) {
1130 assert(!(p
->p_memstat_state
& P_MEMSTAT_INTERNAL
));
1132 if (effective
&& (p
->p_memstat_state
& P_MEMSTAT_PRIORITYUPDATED
)) {
1135 MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", p
->p_pid
);
1139 if ((p
->p_memstat_state
& P_MEMSTAT_TERMINATED
) || ((p
->p_listflag
& P_LIST_EXITED
) != 0)) {
1141 * This could happen when a process calling posix_spawn() is exiting on the jetsam thread.
1148 p
->p_memstat_state
|= P_MEMSTAT_PRIORITYUPDATED
;
1149 p
->p_memstat_userdata
= user_data
;
1150 p
->p_memstat_requestedpriority
= priority
;
1153 if (update_memlimit
) {
1154 p
->p_memstat_memlimit
= memlimit
;
1155 if (memlimit_background
) {
1156 /* Will be set as priority is updated */
1157 p
->p_memstat_state
|= P_MEMSTAT_MEMLIMIT_BACKGROUND
;
1159 /* Cannot have a background memory limit and be fatal. */
1160 is_fatal_limit
= FALSE
;
1163 /* Otherwise, apply now */
1164 if (memorystatus_highwater_enabled
) {
1165 task_set_phys_footprint_limit_internal(p
->task
, (memlimit
> 0) ? memlimit
: -1, NULL
, TRUE
);
1169 if (is_fatal_limit
|| memlimit
<= 0) {
1170 p
->p_memstat_state
|= P_MEMSTAT_FATAL_MEMLIMIT
;
1172 p
->p_memstat_state
&= ~P_MEMSTAT_FATAL_MEMLIMIT
;
1178 * We can't add to the JETSAM_PRIORITY_IDLE_DEFERRED bucket here.
1179 * But, we could be removing it from the bucket.
1180 * Check and take appropriate steps if so.
1183 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1185 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1188 memorystatus_update_priority_locked(p
, priority
, head_insert
);
1194 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_UPDATE
) | DBG_FUNC_END
, ret
, 0, 0, 0, 0);
1200 memorystatus_remove(proc_t p
, boolean_t locked
)
1203 memstat_bucket_t
*bucket
;
1205 MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing process %d\n", p
->p_pid
);
1211 assert(!(p
->p_memstat_state
& P_MEMSTAT_INTERNAL
));
1213 bucket
= &memstat_bucket
[p
->p_memstat_effectivepriority
];
1214 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1215 assert(bucket
->count
== memorystatus_scheduled_idle_demotions
);
1218 TAILQ_REMOVE(&bucket
->list
, p
, p_memstat_list
);
1221 memorystatus_list_count
--;
1223 /* If awaiting demotion to the idle band, clean up */
1224 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1225 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1226 memorystatus_reschedule_idle_demotion_locked();
1229 memorystatus_check_levels_locked();
1232 if (p
->p_memstat_state
& (P_MEMSTAT_FROZEN
)) {
1233 memorystatus_frozen_count
--;
1236 if (p
->p_memstat_state
& P_MEMSTAT_SUSPENDED
) {
1237 memorystatus_suspended_footprint_total
-= p
->p_memstat_suspendedfootprint
;
1238 memorystatus_suspended_count
--;
1256 memorystatus_validate_track_flags(struct proc
*target_p
, uint32_t pcontrol
) {
1257 /* See that the process isn't marked for termination */
1258 if (target_p
->p_memstat_dirty
& P_DIRTY_TERMINATED
) {
1262 /* Idle exit requires that process be tracked */
1263 if ((pcontrol
& PROC_DIRTY_ALLOW_IDLE_EXIT
) &&
1264 !(pcontrol
& PROC_DIRTY_TRACK
)) {
1268 /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */
1269 if ((pcontrol
& PROC_DIRTY_LAUNCH_IN_PROGRESS
) &&
1270 !(pcontrol
& PROC_DIRTY_TRACK
)) {
1274 /* Deferral is only relevant if idle exit is specified */
1275 if ((pcontrol
& PROC_DIRTY_DEFER
) &&
1276 !(pcontrol
& PROC_DIRTY_ALLOWS_IDLE_EXIT
)) {
1284 memorystatus_update_idle_priority_locked(proc_t p
) {
1287 MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p
->p_pid
, p
->p_memstat_dirty
);
1289 if ((p
->p_memstat_dirty
& (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_IS_DIRTY
)) == P_DIRTY_IDLE_EXIT_ENABLED
) {
1290 priority
= (p
->p_memstat_dirty
& P_DIRTY_DEFER_IN_PROGRESS
) ? JETSAM_PRIORITY_IDLE_DEFERRED
: JETSAM_PRIORITY_IDLE
;
1292 priority
= p
->p_memstat_requestedpriority
;
1295 if (priority
!= p
->p_memstat_effectivepriority
) {
1296 memorystatus_update_priority_locked(p
, priority
, false);
1301 * Processes can opt to have their state tracked by the kernel, indicating when they are busy (dirty) or idle
1302 * (clean). They may also indicate that they support termination when idle, with the result that they are promoted
1303 * to their desired, higher, jetsam priority when dirty (and are therefore killed later), and demoted to the low
1304 * priority idle band when clean (and killed earlier, protecting higher priority procesess).
1306 * If the deferral flag is set, then newly tracked processes will be protected for an initial period (as determined by
1307 * memorystatus_idle_delay_time); if they go clean during this time, then they will be moved to a deferred-idle band
1308 * with a slightly higher priority, guarding against immediate termination under memory pressure and being unable to
1309 * make forward progress. Finally, when the guard expires, they will be moved to the standard, lowest-priority, idle
1310 * band. The deferral can be cleared early by clearing the appropriate flag.
1312 * The deferral timer is active only for the duration that the process is marked as guarded and clean; if the process
1313 * is marked dirty, the timer will be cancelled. Upon being subsequently marked clean, the deferment will either be
1314 * re-enabled or the guard state cleared, depending on whether the guard deadline has passed.
1318 memorystatus_dirty_track(proc_t p
, uint32_t pcontrol
) {
1319 unsigned int old_dirty
;
1320 boolean_t reschedule
= FALSE
;
1321 boolean_t already_deferred
= FALSE
;
1322 boolean_t defer_now
= FALSE
;
1325 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DIRTY_TRACK
),
1326 p
->p_pid
, p
->p_memstat_dirty
, pcontrol
, 0, 0);
1330 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1332 * Process is on its way out.
1338 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
1343 if (!memorystatus_validate_track_flags(p
, pcontrol
)) {
1348 old_dirty
= p
->p_memstat_dirty
;
1350 /* These bits are cumulative, as per <rdar://problem/11159924> */
1351 if (pcontrol
& PROC_DIRTY_TRACK
) {
1352 p
->p_memstat_dirty
|= P_DIRTY_TRACK
;
1355 if (pcontrol
& PROC_DIRTY_ALLOW_IDLE_EXIT
) {
1356 p
->p_memstat_dirty
|= P_DIRTY_ALLOW_IDLE_EXIT
;
1359 if (pcontrol
& PROC_DIRTY_LAUNCH_IN_PROGRESS
) {
1360 p
->p_memstat_dirty
|= P_DIRTY_LAUNCH_IN_PROGRESS
;
1363 if (old_dirty
& P_DIRTY_DEFER_IN_PROGRESS
) {
1364 already_deferred
= TRUE
;
1367 /* This can be set and cleared exactly once. */
1368 if (pcontrol
& PROC_DIRTY_DEFER
) {
1370 if ( !(old_dirty
& P_DIRTY_DEFER
)) {
1371 p
->p_memstat_dirty
|= P_DIRTY_DEFER
;
1377 MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for process %d\n",
1378 ((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
) ? "Y" : "N",
1379 defer_now
? "Y" : "N",
1380 p
->p_memstat_dirty
& P_DIRTY
? "Y" : "N",
1383 /* Kick off or invalidate the idle exit deferment if there's a state transition. */
1384 if (!(p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
)) {
1385 if (((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
) &&
1386 defer_now
&& !already_deferred
) {
1389 * Request to defer a clean process that's idle-exit enabled
1390 * and not already in the jetsam deferred band.
1392 memorystatus_schedule_idle_demotion_locked(p
, TRUE
);
1395 } else if (!defer_now
&& already_deferred
) {
1398 * Either the process is no longer idle-exit enabled OR
1399 * there's a request to cancel a currently active deferral.
1401 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1407 * We are trying to operate on a dirty process. Dirty processes have to
1408 * be removed from the deferred band. The question is do we reset the
1409 * deferred state or not?
1411 * This could be a legal request like:
1412 * - this process had opted into the JETSAM_DEFERRED band
1413 * - but it's now dirty and requests to opt out.
1414 * In this case, we remove the process from the band and reset its
1415 * state too. It'll opt back in properly when needed.
1417 * OR, this request could be a user-space bug. E.g.:
1418 * - this process had opted into the JETSAM_DEFERRED band when clean
1419 * - and, then issues another request to again put it into the band except
1420 * this time the process is dirty.
1421 * The process going dirty, as a transition in memorystatus_dirty_set(), will pull the process out of
1422 * the deferred band with its state intact. So our request below is no-op.
1423 * But we do it here anyways for coverage.
1425 * memorystatus_update_idle_priority_locked()
1426 * single-mindedly treats a dirty process as "cannot be in the deferred band".
1429 if (!defer_now
&& already_deferred
) {
1430 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1433 memorystatus_invalidate_idle_demotion_locked(p
, FALSE
);
1438 memorystatus_update_idle_priority_locked(p
);
1441 memorystatus_reschedule_idle_demotion_locked();
1453 memorystatus_dirty_set(proc_t p
, boolean_t self
, uint32_t pcontrol
) {
1455 boolean_t kill
= false;
1456 boolean_t reschedule
= FALSE
;
1457 boolean_t was_dirty
= FALSE
;
1458 boolean_t now_dirty
= FALSE
;
1460 MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self
, p
->p_pid
, pcontrol
, p
->p_memstat_dirty
);
1462 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DIRTY_SET
), p
->p_pid
, self
, pcontrol
, 0, 0);
1466 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1468 * Process is on its way out.
1474 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
1479 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
)
1482 if (!(p
->p_memstat_dirty
& P_DIRTY_TRACK
)) {
1483 /* Dirty tracking not enabled */
1485 } else if (pcontrol
&& (p
->p_memstat_dirty
& P_DIRTY_TERMINATED
)) {
1487 * Process is set to be terminated and we're attempting to mark it dirty.
1488 * Set for termination and marking as clean is OK - see <rdar://problem/10594349>.
1492 int flag
= (self
== TRUE
) ? P_DIRTY
: P_DIRTY_SHUTDOWN
;
1493 if (pcontrol
&& !(p
->p_memstat_dirty
& flag
)) {
1494 /* Mark the process as having been dirtied at some point */
1495 p
->p_memstat_dirty
|= (flag
| P_DIRTY_MARKED
);
1496 memorystatus_dirty_count
++;
1498 } else if ((pcontrol
== 0) && (p
->p_memstat_dirty
& flag
)) {
1499 if ((flag
== P_DIRTY_SHUTDOWN
) && (!p
->p_memstat_dirty
& P_DIRTY
)) {
1500 /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */
1501 p
->p_memstat_dirty
|= P_DIRTY_TERMINATED
;
1503 } else if ((flag
== P_DIRTY
) && (p
->p_memstat_dirty
& P_DIRTY_TERMINATED
)) {
1504 /* Kill previously terminated processes if set clean */
1507 p
->p_memstat_dirty
&= ~flag
;
1508 memorystatus_dirty_count
--;
1520 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
)
1523 if ((was_dirty
== TRUE
&& now_dirty
== FALSE
) ||
1524 (was_dirty
== FALSE
&& now_dirty
== TRUE
)) {
1526 /* Manage idle exit deferral, if applied */
1527 if ((p
->p_memstat_dirty
& (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_DEFER_IN_PROGRESS
)) ==
1528 (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_DEFER_IN_PROGRESS
)) {
1531 * P_DIRTY_DEFER_IN_PROGRESS means the process is in the deferred band OR it might be heading back
1532 * there once it's clean again and has some protection window left.
1535 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
) {
1537 * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE"
1539 * The process will move from the deferred band to its higher requested
1540 * jetsam band. But we don't clear its state i.e. we want to remember that
1541 * this process was part of the "deferred" band and will return to it.
1543 * This way, we don't let it age beyond the protection
1544 * window when it returns to "clean". All the while giving
1545 * it a chance to perform its work while "dirty".
1548 memorystatus_invalidate_idle_demotion_locked(p
, FALSE
);
1553 * Process is back from "dirty" to "clean".
1555 * Is its timer up OR does it still have some protection
1559 if (mach_absolute_time() >= p
->p_memstat_idledeadline
) {
1561 * The process' deadline has expired. It currently
1562 * does not reside in the DEFERRED bucket.
1564 * It's on its way to the JETSAM_PRIORITY_IDLE
1565 * bucket via memorystatus_update_idle_priority_locked()
1568 * So all we need to do is reset all the state on the
1569 * process that's related to the DEFERRED bucket i.e.
1570 * the DIRTY_DEFER_IN_PROGRESS flag and the timer deadline.
1574 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1578 * It still has some protection window left and so
1579 * we just re-arm the timer without modifying any
1580 * state on the process.
1582 memorystatus_schedule_idle_demotion_locked(p
, FALSE
);
1588 memorystatus_update_idle_priority_locked(p
);
1590 /* If the deferral state changed, reschedule the demotion timer */
1592 memorystatus_reschedule_idle_demotion_locked();
1597 psignal(p
, SIGKILL
);
1607 memorystatus_dirty_clear(proc_t p
, uint32_t pcontrol
) {
1611 MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_clear(): %d 0x%x 0x%x\n", p
->p_pid
, pcontrol
, p
->p_memstat_dirty
);
1613 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DIRTY_CLEAR
), p
->p_pid
, pcontrol
, 0, 0, 0);
1617 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1619 * Process is on its way out.
1625 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
1630 if (!(p
->p_memstat_dirty
& P_DIRTY_TRACK
)) {
1631 /* Dirty tracking not enabled */
1636 if (!pcontrol
|| (pcontrol
& (PROC_DIRTY_LAUNCH_IN_PROGRESS
| PROC_DIRTY_DEFER
)) == 0) {
1641 if (pcontrol
& PROC_DIRTY_LAUNCH_IN_PROGRESS
) {
1642 p
->p_memstat_dirty
&= ~P_DIRTY_LAUNCH_IN_PROGRESS
;
1645 /* This can be set and cleared exactly once. */
1646 if (pcontrol
& PROC_DIRTY_DEFER
) {
1648 if (p
->p_memstat_dirty
& P_DIRTY_DEFER
) {
1650 p
->p_memstat_dirty
&= ~P_DIRTY_DEFER
;
1652 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1653 memorystatus_update_idle_priority_locked(p
);
1654 memorystatus_reschedule_idle_demotion_locked();
1666 memorystatus_dirty_get(proc_t p
) {
1671 if (p
->p_memstat_dirty
& P_DIRTY_TRACK
) {
1672 ret
|= PROC_DIRTY_TRACKED
;
1673 if (p
->p_memstat_dirty
& P_DIRTY_ALLOW_IDLE_EXIT
) {
1674 ret
|= PROC_DIRTY_ALLOWS_IDLE_EXIT
;
1676 if (p
->p_memstat_dirty
& P_DIRTY
) {
1677 ret
|= PROC_DIRTY_IS_DIRTY
;
1679 if (p
->p_memstat_dirty
& P_DIRTY_LAUNCH_IN_PROGRESS
) {
1680 ret
|= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS
;
1690 memorystatus_on_terminate(proc_t p
) {
1695 p
->p_memstat_dirty
|= P_DIRTY_TERMINATED
;
1697 if ((p
->p_memstat_dirty
& (P_DIRTY_TRACK
|P_DIRTY_IS_DIRTY
)) == P_DIRTY_TRACK
) {
1698 /* Clean; mark as terminated and issue SIGKILL */
1701 /* Dirty, terminated, or state tracking is unsupported; issue SIGTERM to allow cleanup */
1711 memorystatus_on_suspend(proc_t p
)
1715 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
1719 p
->p_memstat_suspendedfootprint
= pages
;
1720 memorystatus_suspended_footprint_total
+= pages
;
1721 memorystatus_suspended_count
++;
1723 p
->p_memstat_state
|= P_MEMSTAT_SUSPENDED
;
1728 memorystatus_on_resume(proc_t p
)
1738 frozen
= (p
->p_memstat_state
& P_MEMSTAT_FROZEN
);
1740 memorystatus_frozen_count
--;
1741 p
->p_memstat_state
|= P_MEMSTAT_PRIOR_THAW
;
1744 memorystatus_suspended_footprint_total
-= p
->p_memstat_suspendedfootprint
;
1745 memorystatus_suspended_count
--;
1750 p
->p_memstat_state
&= ~(P_MEMSTAT_SUSPENDED
| P_MEMSTAT_FROZEN
);
1756 memorystatus_freeze_entry_t data
= { pid
, FALSE
, 0 };
1757 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
1763 memorystatus_on_inactivity(proc_t p
)
1767 /* Wake the freeze thread */
1768 thread_wakeup((event_t
)&memorystatus_freeze_wakeup
);
1773 memorystatus_build_state(proc_t p
) {
1774 uint32_t snapshot_state
= 0;
1777 if (p
->p_memstat_state
& P_MEMSTAT_SUSPENDED
) {
1778 snapshot_state
|= kMemorystatusSuspended
;
1780 if (p
->p_memstat_state
& P_MEMSTAT_FROZEN
) {
1781 snapshot_state
|= kMemorystatusFrozen
;
1783 if (p
->p_memstat_state
& P_MEMSTAT_PRIOR_THAW
) {
1784 snapshot_state
|= kMemorystatusWasThawed
;
1788 if (p
->p_memstat_dirty
& P_DIRTY_TRACK
) {
1789 snapshot_state
|= kMemorystatusTracked
;
1791 if ((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
) {
1792 snapshot_state
|= kMemorystatusSupportsIdleExit
;
1794 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
) {
1795 snapshot_state
|= kMemorystatusDirty
;
1798 return snapshot_state
;
1804 kill_idle_exit_proc(void)
1806 proc_t p
, victim_p
= PROC_NULL
;
1807 uint64_t current_time
;
1808 boolean_t killed
= FALSE
;
1811 /* Pick next idle exit victim. */
1812 current_time
= mach_absolute_time();
1816 p
= memorystatus_get_first_proc_locked(&i
, FALSE
);
1818 /* No need to look beyond the idle band */
1819 if (p
->p_memstat_effectivepriority
!= JETSAM_PRIORITY_IDLE
) {
1823 if ((p
->p_memstat_dirty
& (P_DIRTY_ALLOW_IDLE_EXIT
|P_DIRTY_IS_DIRTY
|P_DIRTY_TERMINATED
)) == (P_DIRTY_ALLOW_IDLE_EXIT
)) {
1824 if (current_time
>= p
->p_memstat_idledeadline
) {
1825 p
->p_memstat_dirty
|= P_DIRTY_TERMINATED
;
1826 victim_p
= proc_ref_locked(p
);
1831 p
= memorystatus_get_next_proc_locked(&i
, p
, FALSE
);
1837 printf("memorystatus_thread: idle exiting pid %d [%s]\n", victim_p
->p_pid
, (victim_p
->p_comm
? victim_p
->p_comm
: "(unknown)"));
1838 killed
= memorystatus_do_kill(victim_p
, kMemorystatusKilledIdleExit
);
1839 proc_rele(victim_p
);
1848 memorystatus_thread_wake(void) {
1849 thread_wakeup((event_t
)&memorystatus_wakeup
);
1851 #endif /* CONFIG_JETSAM */
1853 extern void vm_pressure_response(void);
1856 memorystatus_thread_block(uint32_t interval_ms
, thread_continue_t continuation
)
1859 assert_wait_timeout(&memorystatus_wakeup
, THREAD_UNINT
, interval_ms
, 1000 * NSEC_PER_USEC
);
1861 assert_wait(&memorystatus_wakeup
, THREAD_UNINT
);
1864 return thread_block(continuation
);
1868 memorystatus_thread(void *param __unused
, wait_result_t wr __unused
)
1870 static boolean_t is_vm_privileged
= FALSE
;
1872 boolean_t post_snapshot
= FALSE
;
1873 uint32_t errors
= 0;
1874 uint32_t hwm_kill
= 0;
1877 if (is_vm_privileged
== FALSE
) {
1879 * It's the first time the thread has run, so just mark the thread as privileged and block.
1880 * This avoids a spurious pass with unset variables, as set out in <rdar://problem/9609402>.
1882 thread_wire(host_priv_self(), current_thread(), TRUE
);
1883 is_vm_privileged
= TRUE
;
1885 memorystatus_thread_block(0, memorystatus_thread
);
1890 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_SCAN
) | DBG_FUNC_START
,
1891 memorystatus_available_pages
, 0, 0, 0, 0);
1894 * Jetsam aware version.
1896 * The VM pressure notification thread is working it's way through clients in parallel.
1898 * So, while the pressure notification thread is targeting processes in order of
1899 * increasing jetsam priority, we can hopefully reduce / stop it's work by killing
1900 * any processes that have exceeded their highwater mark.
1902 * If we run out of HWM processes and our available pages drops below the critical threshold, then,
1903 * we target the least recently used process in order of increasing jetsam priority (exception: the FG band).
1905 while (is_thrashing(kill_under_pressure_cause
) ||
1906 memorystatus_available_pages
<= memorystatus_available_pages_pressure
) {
1911 if (kill_under_pressure_cause
) {
1912 cause
= kill_under_pressure_cause
;
1914 cause
= kMemorystatusKilledVMPageShortage
;
1919 killed
= memorystatus_kill_hiwat_proc(&errors
);
1922 post_snapshot
= TRUE
;
1925 memorystatus_hwm_candidates
= FALSE
;
1928 /* No highwater processes to kill. Continue or stop for now? */
1929 if (!is_thrashing(kill_under_pressure_cause
) &&
1930 (memorystatus_available_pages
> memorystatus_available_pages_critical
)) {
1932 * We are _not_ out of pressure but we are above the critical threshold and there's:
1933 * - no compressor thrashing
1934 * - no more HWM processes left.
1935 * For now, don't kill any other processes.
1938 if (hwm_kill
== 0) {
1939 memorystatus_thread_wasted_wakeup
++;
1947 killed
= memorystatus_kill_top_process(TRUE
, cause
, &priority
, &errors
);
1949 /* Don't generate logs for steady-state idle-exit kills (unless overridden for debug) */
1950 if ((priority
!= JETSAM_PRIORITY_IDLE
) || memorystatus_idle_snapshot
) {
1951 post_snapshot
= TRUE
;
1956 if (memorystatus_available_pages
<= memorystatus_available_pages_critical
) {
1957 /* Under pressure and unable to kill a process - panic */
1958 panic("memorystatus_jetsam_thread: no victim! available pages:%d\n", memorystatus_available_pages
);
1964 * We do not want to over-kill when thrashing has been detected.
1965 * To avoid that, we reset the flag here and notify the
1968 if (is_thrashing(kill_under_pressure_cause
)) {
1969 kill_under_pressure_cause
= 0;
1970 vm_thrashing_jetsam_done();
1974 kill_under_pressure_cause
= 0;
1977 memorystatus_clear_errors();
1980 #if VM_PRESSURE_EVENTS
1982 * LD: We used to target the foreground process first and foremost here.
1983 * Now, we target all processes, starting from the non-suspended, background
1984 * processes first. We will target foreground too.
1986 * memorystatus_update_vm_pressure(TRUE);
1988 //vm_pressure_response();
1991 if (post_snapshot
) {
1992 size_t snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) +
1993 sizeof(memorystatus_jetsam_snapshot_entry_t
) * (memorystatus_jetsam_snapshot_count
);
1994 memorystatus_jetsam_snapshot
->notification_time
= mach_absolute_time();
1995 memorystatus_send_note(kMemorystatusSnapshotNote
, &snapshot_size
, sizeof(snapshot_size
));
1998 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_SCAN
) | DBG_FUNC_END
,
1999 memorystatus_available_pages
, 0, 0, 0, 0);
2001 #else /* CONFIG_JETSAM */
2004 * Jetsam not enabled
2007 #endif /* CONFIG_JETSAM */
2009 memorystatus_thread_block(0, memorystatus_thread
);
2015 * when an idle-exitable proc was killed
2017 * when there are no more idle-exitable procs found
2018 * when the attempt to kill an idle-exitable proc failed
2020 boolean_t
memorystatus_idle_exit_from_VM(void) {
2021 return(kill_idle_exit_proc());
2023 #endif /* !CONFIG_JETSAM */
2028 * Callback invoked when allowable physical memory footprint exceeded
2029 * (dirty pages + IOKit mappings)
2031 * This is invoked for both advisory, non-fatal per-task high watermarks,
2032 * as well as the fatal task memory limits.
2035 memorystatus_on_ledger_footprint_exceeded(boolean_t warning
, const int max_footprint_mb
)
2037 proc_t p
= current_proc();
2039 if (warning
== FALSE
) {
2040 printf("process %d (%s) exceeded physical memory footprint limit of %d MB\n",
2041 p
->p_pid
, p
->p_comm
, max_footprint_mb
);
2044 #if VM_PRESSURE_EVENTS
2045 if (warning
== TRUE
) {
2046 if (memorystatus_warn_process(p
->p_pid
, TRUE
/* critical? */) != TRUE
) {
2047 /* Print warning, since it's possible that task has not registered for pressure notifications */
2048 printf("task_exceeded_footprint: failed to warn the current task (exiting, or no handler registered?).\n");
2052 #endif /* VM_PRESSURE_EVENTS */
2054 if ((p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
) == P_MEMSTAT_FATAL_MEMLIMIT
) {
2056 * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task
2057 * has violated either the system-wide per-task memory limit OR its own task limit.
2059 if (memorystatus_kill_process_sync(p
->p_pid
, kMemorystatusKilledPerProcessLimit
) != TRUE
) {
2060 printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n");
2064 * HWM offender exists. Done without locks or synchronization.
2065 * See comment near its declaration for more details.
2067 memorystatus_hwm_candidates
= TRUE
;
2072 * This is invoked when cpulimits have been exceeded while in fatal mode.
2073 * The jetsam_flags do not apply as those are for memory related kills.
2074 * We call this routine so that the offending process is killed with
2075 * a non-zero exit status.
2078 jetsam_on_ledger_cpulimit_exceeded(void)
2081 int jetsam_flags
= 0; /* make it obvious */
2082 proc_t p
= current_proc();
2084 printf("task_exceeded_cpulimit: killing pid %d [%s]\n",
2085 p
->p_pid
, (p
->p_comm
? p
->p_comm
: "(unknown)"));
2087 retval
= jetsam_do_kill(p
, jetsam_flags
);
2090 printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n");
2095 memorystatus_get_task_page_counts(task_t task
, uint32_t *footprint
, uint32_t *max_footprint
, uint32_t *max_footprint_lifetime
, uint32_t *purgeable_pages
)
2100 *footprint
= (uint32_t)(get_task_phys_footprint(task
) / PAGE_SIZE_64
);
2101 if (max_footprint
) {
2102 *max_footprint
= (uint32_t)(get_task_phys_footprint_max(task
) / PAGE_SIZE_64
);
2104 if (max_footprint_lifetime
) {
2105 *max_footprint_lifetime
= (uint32_t)(get_task_resident_max(task
) / PAGE_SIZE_64
);
2107 if (purgeable_pages
) {
2108 *purgeable_pages
= (uint32_t)(get_task_purgeable_size(task
) / PAGE_SIZE_64
);
2114 memorystatus_update_snapshot_locked(proc_t p
, uint32_t kill_cause
)
2118 for (i
= 0; i
< memorystatus_jetsam_snapshot_count
; i
++) {
2119 if (memorystatus_jetsam_snapshot_list
[i
].pid
== p
->p_pid
) {
2120 /* Update if the priority has changed since the snapshot was taken */
2121 if (memorystatus_jetsam_snapshot_list
[i
].priority
!= p
->p_memstat_effectivepriority
) {
2122 memorystatus_jetsam_snapshot_list
[i
].priority
= p
->p_memstat_effectivepriority
;
2123 strlcpy(memorystatus_jetsam_snapshot_list
[i
].name
, p
->p_comm
, MAXCOMLEN
+1);
2124 memorystatus_jetsam_snapshot_list
[i
].state
= memorystatus_build_state(p
);
2125 memorystatus_jetsam_snapshot_list
[i
].user_data
= p
->p_memstat_userdata
;
2126 memorystatus_jetsam_snapshot_list
[i
].fds
= p
->p_fd
->fd_nfiles
;
2128 memorystatus_jetsam_snapshot_list
[i
].killed
= kill_cause
;
2134 void memorystatus_pages_update(unsigned int pages_avail
)
2136 memorystatus_available_pages
= pages_avail
;
2138 #if VM_PRESSURE_EVENTS
2140 * Since memorystatus_available_pages changes, we should
2141 * re-evaluate the pressure levels on the system and
2142 * check if we need to wake the pressure thread.
2143 * We also update memorystatus_level in that routine.
2145 vm_pressure_response();
2147 if (memorystatus_available_pages
<= memorystatus_available_pages_pressure
) {
2149 if (memorystatus_hwm_candidates
|| (memorystatus_available_pages
<= memorystatus_available_pages_critical
)) {
2150 memorystatus_thread_wake();
2153 #else /* VM_PRESSURE_EVENTS */
2155 boolean_t critical
, delta
;
2157 if (!memorystatus_delta
) {
2161 critical
= (pages_avail
< memorystatus_available_pages_critical
) ? TRUE
: FALSE
;
2162 delta
= ((pages_avail
>= (memorystatus_available_pages
+ memorystatus_delta
))
2163 || (memorystatus_available_pages
>= (pages_avail
+ memorystatus_delta
))) ? TRUE
: FALSE
;
2165 if (critical
|| delta
) {
2166 memorystatus_level
= memorystatus_available_pages
* 100 / atop_64(max_mem
);
2167 memorystatus_thread_wake();
2169 #endif /* VM_PRESSURE_EVENTS */
2173 memorystatus_get_snapshot_properties_for_proc_locked(proc_t p
, memorystatus_jetsam_snapshot_entry_t
*entry
)
2176 clock_usec_t tv_usec
;
2178 memset(entry
, 0, sizeof(memorystatus_jetsam_snapshot_entry_t
));
2180 entry
->pid
= p
->p_pid
;
2181 strlcpy(&entry
->name
[0], p
->p_comm
, MAXCOMLEN
+1);
2182 entry
->priority
= p
->p_memstat_effectivepriority
;
2183 memorystatus_get_task_page_counts(p
->task
, &entry
->pages
, &entry
->max_pages
, &entry
->max_pages_lifetime
, &entry
->purgeable_pages
);
2184 entry
->state
= memorystatus_build_state(p
);
2185 entry
->user_data
= p
->p_memstat_userdata
;
2186 memcpy(&entry
->uuid
[0], &p
->p_uuid
[0], sizeof(p
->p_uuid
));
2187 entry
->fds
= p
->p_fd
->fd_nfiles
;
2189 absolutetime_to_microtime(get_task_cpu_time(p
->task
), &tv_sec
, &tv_usec
);
2190 entry
->cpu_time
.tv_sec
= tv_sec
;
2191 entry
->cpu_time
.tv_usec
= tv_usec
;
2197 memorystatus_jetsam_snapshot_procs_locked(void)
2200 unsigned int b
= 0, i
= 0;
2201 kern_return_t kr
= KERN_SUCCESS
;
2203 mach_msg_type_number_t count
= HOST_VM_INFO64_COUNT
;
2204 vm_statistics64_data_t vm_stat
;
2206 if ((kr
= host_statistics64(host_self(), HOST_VM_INFO64
, (host_info64_t
)&vm_stat
, &count
) != KERN_SUCCESS
)) {
2207 printf("memorystatus_jetsam_snapshot_procs_locked: host_statistics64 failed with %d\n", kr
);
2208 memset(&memorystatus_jetsam_snapshot
->stats
, 0, sizeof(memorystatus_jetsam_snapshot
->stats
));
2210 memorystatus_jetsam_snapshot
->stats
.free_pages
= vm_stat
.free_count
;
2211 memorystatus_jetsam_snapshot
->stats
.active_pages
= vm_stat
.active_count
;
2212 memorystatus_jetsam_snapshot
->stats
.inactive_pages
= vm_stat
.inactive_count
;
2213 memorystatus_jetsam_snapshot
->stats
.throttled_pages
= vm_stat
.throttled_count
;
2214 memorystatus_jetsam_snapshot
->stats
.purgeable_pages
= vm_stat
.purgeable_count
;
2215 memorystatus_jetsam_snapshot
->stats
.wired_pages
= vm_stat
.wire_count
;
2217 memorystatus_jetsam_snapshot
->stats
.speculative_pages
= vm_stat
.speculative_count
;
2218 memorystatus_jetsam_snapshot
->stats
.filebacked_pages
= vm_stat
.external_page_count
;
2219 memorystatus_jetsam_snapshot
->stats
.anonymous_pages
= vm_stat
.internal_page_count
;
2220 memorystatus_jetsam_snapshot
->stats
.compressions
= vm_stat
.compressions
;
2221 memorystatus_jetsam_snapshot
->stats
.decompressions
= vm_stat
.decompressions
;
2222 memorystatus_jetsam_snapshot
->stats
.compressor_pages
= vm_stat
.compressor_page_count
;
2223 memorystatus_jetsam_snapshot
->stats
.total_uncompressed_pages_in_compressor
= vm_stat
.total_uncompressed_pages_in_compressor
;
2226 next_p
= memorystatus_get_first_proc_locked(&b
, TRUE
);
2229 next_p
= memorystatus_get_next_proc_locked(&b
, p
, TRUE
);
2231 if (FALSE
== memorystatus_get_snapshot_properties_for_proc_locked(p
, &memorystatus_jetsam_snapshot_list
[i
])) {
2235 MEMORYSTATUS_DEBUG(0, "jetsam snapshot pid = %d, uuid = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2237 p
->p_uuid
[0], p
->p_uuid
[1], p
->p_uuid
[2], p
->p_uuid
[3], p
->p_uuid
[4], p
->p_uuid
[5], p
->p_uuid
[6], p
->p_uuid
[7],
2238 p
->p_uuid
[8], p
->p_uuid
[9], p
->p_uuid
[10], p
->p_uuid
[11], p
->p_uuid
[12], p
->p_uuid
[13], p
->p_uuid
[14], p
->p_uuid
[15]);
2240 if (++i
== memorystatus_jetsam_snapshot_max
) {
2245 memorystatus_jetsam_snapshot
->snapshot_time
= mach_absolute_time();
2246 memorystatus_jetsam_snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= i
;
2249 #if DEVELOPMENT || DEBUG
2252 memorystatus_cmd_set_panic_bits(user_addr_t buffer
, uint32_t buffer_size
) {
2254 memorystatus_jetsam_panic_options_t debug
;
2256 if (buffer_size
!= sizeof(memorystatus_jetsam_panic_options_t
)) {
2260 ret
= copyin(buffer
, &debug
, buffer_size
);
2265 /* Panic bits match kMemorystatusKilled* enum */
2266 memorystatus_jetsam_panic_debug
= (memorystatus_jetsam_panic_debug
& ~debug
.mask
) | (debug
.data
& debug
.mask
);
2268 /* Copyout new value */
2269 debug
.data
= memorystatus_jetsam_panic_debug
;
2270 ret
= copyout(&debug
, buffer
, sizeof(memorystatus_jetsam_panic_options_t
));
2278 * Jetsam a specific process.
2281 memorystatus_kill_specific_process(pid_t victim_pid
, uint32_t cause
) {
2285 /* TODO - add a victim queue and push this into the main jetsam thread */
2287 p
= proc_find(victim_pid
);
2292 printf("memorystatus: specifically killing pid %d [%s] (%s) - memorystatus_available_pages: %d\n",
2293 victim_pid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
2294 jetsam_kill_cause_name
[cause
], memorystatus_available_pages
);
2298 if (memorystatus_jetsam_snapshot_count
== 0) {
2299 memorystatus_jetsam_snapshot_procs_locked();
2302 memorystatus_update_snapshot_locked(p
, cause
);
2305 killed
= memorystatus_do_kill(p
, cause
);
2312 * Jetsam the first process in the queue.
2315 memorystatus_kill_top_process(boolean_t any
, uint32_t cause
, int32_t *priority
, uint32_t *errors
)
2318 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
2319 boolean_t new_snapshot
= FALSE
, killed
= FALSE
;
2322 #ifndef CONFIG_FREEZE
2326 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM
) | DBG_FUNC_START
,
2327 memorystatus_available_pages
, 0, 0, 0, 0);
2331 memorystatus_sort_by_largest_process_locked(JETSAM_PRIORITY_FOREGROUND
);
2333 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
2335 #if DEVELOPMENT || DEBUG
2337 int procSuspendedForDiagnosis
;
2338 #endif /* DEVELOPMENT || DEBUG */
2341 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
2343 #if DEVELOPMENT || DEBUG
2344 activeProcess
= p
->p_memstat_state
& P_MEMSTAT_FOREGROUND
;
2345 procSuspendedForDiagnosis
= p
->p_memstat_state
& P_MEMSTAT_DIAG_SUSPENDED
;
2346 #endif /* DEVELOPMENT || DEBUG */
2350 if (p
->p_memstat_state
& (P_MEMSTAT_ERROR
| P_MEMSTAT_TERMINATED
)) {
2354 #if DEVELOPMENT || DEBUG
2355 if ((memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) && procSuspendedForDiagnosis
) {
2356 printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid
);
2359 #endif /* DEVELOPMENT || DEBUG */
2361 if (cause
== kMemorystatusKilledVnodes
)
2364 * If the system runs out of vnodes, we systematically jetsam
2365 * processes in hopes of stumbling onto a vnode gain that helps
2366 * the system recover. The process that happens to trigger
2367 * this path has no known relationship to the vnode consumption.
2368 * We attempt to safeguard that process e.g: do not jetsam it.
2371 if (p
== current_proc()) {
2372 /* do not jetsam the current process */
2379 boolean_t reclaim_proc
= !(p
->p_memstat_state
& (P_MEMSTAT_LOCKED
| P_MEMSTAT_NORECLAIM
));
2380 if (any
|| reclaim_proc
) {
2392 *priority
= p
->p_memstat_effectivepriority
;
2396 * Capture a snapshot if none exists and:
2397 * - priority was not requested (this is something other than an ambient kill)
2398 * - the priority was requested *and* the targeted process is not at idle priority
2400 if ((memorystatus_jetsam_snapshot_count
== 0) &&
2401 (memorystatus_idle_snapshot
|| ((!priority
) || (priority
&& (*priority
!= JETSAM_PRIORITY_IDLE
))))) {
2402 memorystatus_jetsam_snapshot_procs_locked();
2403 new_snapshot
= TRUE
;
2407 * Mark as terminated so that if exit1() indicates success, but the process (for example)
2408 * is blocked in task_exception_notify(), it'll be skipped if encountered again - see
2409 * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the
2410 * acquisition of the proc lock.
2412 p
->p_memstat_state
|= P_MEMSTAT_TERMINATED
;
2414 #if DEVELOPMENT || DEBUG
2415 if ((memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) && activeProcess
) {
2416 MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memory_status_level: %d\n",
2417 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"), memorystatus_level
);
2418 memorystatus_update_snapshot_locked(p
, kMemorystatusKilledDiagnostic
);
2419 p
->p_memstat_state
|= P_MEMSTAT_DIAG_SUSPENDED
;
2420 if (memorystatus_jetsam_policy
& kPolicyDiagnoseFirst
) {
2421 jetsam_diagnostic_suspended_one_active_proc
= 1;
2422 printf("jetsam: returning after suspending first active proc - %d\n", aPid
);
2425 p
= proc_ref_locked(p
);
2428 task_suspend(p
->task
);
2435 #endif /* DEVELOPMENT || DEBUG */
2437 /* Shift queue, update stats */
2438 memorystatus_update_snapshot_locked(p
, cause
);
2440 p
= proc_ref_locked(p
);
2443 printf("memorystatus: %s %d [%s] (%s) - memorystatus_available_pages: %d\n",
2444 ((p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE
) ?
2445 "idle exiting pid" : "jetsam killing pid"),
2446 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
2447 jetsam_kill_cause_name
[cause
], memorystatus_available_pages
);
2448 killed
= memorystatus_do_kill(p
, cause
);
2457 /* Failure - unwind and restart. */
2459 proc_rele_locked(p
);
2460 p
->p_memstat_state
&= ~P_MEMSTAT_TERMINATED
;
2461 p
->p_memstat_state
|= P_MEMSTAT_ERROR
;
2464 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
2472 /* Clear snapshot if freshly captured and no target was found */
2473 if (new_snapshot
&& !killed
) {
2474 memorystatus_jetsam_snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
2477 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM
) | DBG_FUNC_END
,
2478 memorystatus_available_pages
, killed
? aPid
: 0, 0, 0, 0);
2486 memorystatus_kill_hiwat_proc(uint32_t *errors
)
2489 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
2490 boolean_t new_snapshot
= FALSE
, killed
= FALSE
;
2493 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM_HIWAT
) | DBG_FUNC_START
,
2494 memorystatus_available_pages
, 0, 0, 0, 0);
2497 memorystatus_sort_by_largest_process_locked(JETSAM_PRIORITY_FOREGROUND
);
2499 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
2505 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
2509 if (p
->p_memstat_state
& (P_MEMSTAT_ERROR
| P_MEMSTAT_TERMINATED
)) {
2513 /* skip if no limit set */
2514 if (p
->p_memstat_memlimit
<= 0) {
2518 /* skip if a currently inapplicable limit is encountered */
2519 if ((p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
) && (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
)) {
2523 footprint
= (uint32_t)(get_task_phys_footprint(p
->task
) / (1024 * 1024));
2524 skip
= (((int32_t)footprint
) <= p
->p_memstat_memlimit
);
2525 #if DEVELOPMENT || DEBUG
2526 if (!skip
&& (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
)) {
2527 if (p
->p_memstat_state
& P_MEMSTAT_DIAG_SUSPENDED
) {
2531 #endif /* DEVELOPMENT || DEBUG */
2535 if (p
->p_memstat_state
& P_MEMSTAT_LOCKED
) {
2546 MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %d Mb > 1 (%d Mb)\n",
2547 (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) ? "suspending": "killing", aPid
, p
->p_comm
, footprint
, p
->p_memstat_memlimit
);
2549 if (memorystatus_jetsam_snapshot_count
== 0) {
2550 memorystatus_jetsam_snapshot_procs_locked();
2551 new_snapshot
= TRUE
;
2554 p
->p_memstat_state
|= P_MEMSTAT_TERMINATED
;
2556 #if DEVELOPMENT || DEBUG
2557 if (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) {
2558 MEMORYSTATUS_DEBUG(1, "jetsam: pid %d suspended for diagnosis - memorystatus_available_pages: %d\n", aPid
, memorystatus_available_pages
);
2559 memorystatus_update_snapshot_locked(p
, kMemorystatusKilledDiagnostic
);
2560 p
->p_memstat_state
|= P_MEMSTAT_DIAG_SUSPENDED
;
2562 p
= proc_ref_locked(p
);
2565 task_suspend(p
->task
);
2572 #endif /* DEVELOPMENT || DEBUG */
2574 memorystatus_update_snapshot_locked(p
, kMemorystatusKilledHiwat
);
2576 p
= proc_ref_locked(p
);
2579 printf("memorystatus: jetsam killing pid %d [%s] (highwater) - memorystatus_available_pages: %d\n",
2580 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"), memorystatus_available_pages
);
2581 killed
= memorystatus_do_kill(p
, kMemorystatusKilledHiwat
);
2590 /* Failure - unwind and restart. */
2592 proc_rele_locked(p
);
2593 p
->p_memstat_state
&= ~P_MEMSTAT_TERMINATED
;
2594 p
->p_memstat_state
|= P_MEMSTAT_ERROR
;
2597 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
2605 /* Clear snapshot if freshly captured and no target was found */
2606 if (new_snapshot
&& !killed
) {
2607 memorystatus_jetsam_snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
2610 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM_HIWAT
) | DBG_FUNC_END
,
2611 memorystatus_available_pages
, killed
? aPid
: 0, 0, 0, 0);
2616 #endif /* LEGACY_HIWATER */
2619 memorystatus_kill_process_async(pid_t victim_pid
, uint32_t cause
) {
2620 /* TODO: allow a general async path */
2621 if ((victim_pid
!= -1) || (cause
!= kMemorystatusKilledVMPageShortage
&& cause
!= kMemorystatusKilledVMThrashing
&&
2622 cause
!= kMemorystatusKilledFCThrashing
)) {
2626 kill_under_pressure_cause
= cause
;
2627 memorystatus_thread_wake();
2632 memorystatus_kill_process_sync(pid_t victim_pid
, uint32_t cause
) {
2634 uint32_t errors
= 0;
2636 if (victim_pid
== -1) {
2637 /* No pid, so kill first process */
2638 res
= memorystatus_kill_top_process(TRUE
, cause
, NULL
, &errors
);
2640 res
= memorystatus_kill_specific_process(victim_pid
, cause
);
2644 memorystatus_clear_errors();
2648 /* Fire off snapshot notification */
2649 size_t snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) +
2650 sizeof(memorystatus_jetsam_snapshot_entry_t
) * memorystatus_jetsam_snapshot_count
;
2651 memorystatus_jetsam_snapshot
->notification_time
= mach_absolute_time();
2652 memorystatus_send_note(kMemorystatusSnapshotNote
, &snapshot_size
, sizeof(snapshot_size
));
2659 memorystatus_kill_on_VM_page_shortage(boolean_t async
) {
2661 return memorystatus_kill_process_async(-1, kMemorystatusKilledVMPageShortage
);
2663 return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMPageShortage
);
2668 memorystatus_kill_on_VM_thrashing(boolean_t async
) {
2670 return memorystatus_kill_process_async(-1, kMemorystatusKilledVMThrashing
);
2672 return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMThrashing
);
2677 memorystatus_kill_on_FC_thrashing(boolean_t async
) {
2679 return memorystatus_kill_process_async(-1, kMemorystatusKilledFCThrashing
);
2681 return memorystatus_kill_process_sync(-1, kMemorystatusKilledFCThrashing
);
2686 memorystatus_kill_on_vnode_limit(void) {
2687 return memorystatus_kill_process_sync(-1, kMemorystatusKilledVnodes
);
2690 #endif /* CONFIG_JETSAM */
2694 __private_extern__
void
2695 memorystatus_freeze_init(void)
2697 kern_return_t result
;
2700 result
= kernel_thread_start(memorystatus_freeze_thread
, NULL
, &thread
);
2701 if (result
== KERN_SUCCESS
) {
2702 thread_deallocate(thread
);
2704 panic("Could not create memorystatus_freeze_thread");
2709 memorystatus_freeze_top_process(boolean_t
*memorystatus_freeze_swap_low
)
2713 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
2716 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_START
,
2717 memorystatus_available_pages
, 0, 0, 0, 0);
2721 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
2724 uint32_t purgeable
, wired
, clean
, dirty
;
2727 uint32_t max_pages
= 0;
2731 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
2734 state
= p
->p_memstat_state
;
2736 /* Ensure the process is eligible for freezing */
2737 if ((state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_FROZEN
)) || !(state
& P_MEMSTAT_SUSPENDED
)) {
2738 continue; // with lock held
2741 /* Only freeze processes meeting our minimum resident page criteria */
2742 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
2743 if (pages
< memorystatus_freeze_pages_min
) {
2744 continue; // with lock held
2747 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
2748 /* Ensure there's enough free space to freeze this process. */
2749 max_pages
= MIN(default_pager_swap_pages_free(), memorystatus_freeze_pages_max
);
2750 if (max_pages
< memorystatus_freeze_pages_min
) {
2751 *memorystatus_freeze_swap_low
= TRUE
;
2756 max_pages
= UINT32_MAX
- 1;
2759 /* Mark as locked temporarily to avoid kill */
2760 p
->p_memstat_state
|= P_MEMSTAT_LOCKED
;
2762 p
= proc_ref_locked(p
);
2768 kr
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, FALSE
);
2770 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_top_process: task_freeze %s for pid %d [%s] - "
2771 "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, shared %d, free swap: %d\n",
2772 (kr
== KERN_SUCCESS
) ? "SUCCEEDED" : "FAILED", aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
2773 memorystatus_available_pages
, purgeable
, wired
, clean
, dirty
, shared
, default_pager_swap_pages_free());
2776 p
->p_memstat_state
&= ~P_MEMSTAT_LOCKED
;
2779 if (KERN_SUCCESS
== kr
) {
2780 memorystatus_freeze_entry_t data
= { aPid
, TRUE
, dirty
};
2782 memorystatus_frozen_count
++;
2784 p
->p_memstat_state
|= (P_MEMSTAT_FROZEN
| (shared
? 0: P_MEMSTAT_NORECLAIM
));
2787 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
2788 throttle_intervals
[i
].pageouts
+= dirty
;
2791 memorystatus_freeze_pageouts
+= dirty
;
2792 memorystatus_freeze_count
++;
2796 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
2798 /* Return the number of reclaimed pages */
2812 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_END
,
2813 memorystatus_available_pages
, aPid
, 0, 0, 0);
2818 static inline boolean_t
2819 memorystatus_can_freeze_processes(void)
2825 if (memorystatus_suspended_count
) {
2826 uint32_t average_resident_pages
, estimated_processes
;
2828 /* Estimate the number of suspended processes we can fit */
2829 average_resident_pages
= memorystatus_suspended_footprint_total
/ memorystatus_suspended_count
;
2830 estimated_processes
= memorystatus_suspended_count
+
2831 ((memorystatus_available_pages
- memorystatus_available_pages_critical
) / average_resident_pages
);
2833 /* If it's predicted that no freeze will occur, lower the threshold temporarily */
2834 if (estimated_processes
<= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
) {
2835 memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_LOW
;
2837 memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
;
2840 MEMORYSTATUS_DEBUG(1, "memorystatus_can_freeze_processes: %d suspended processes, %d average resident pages / process, %d suspended processes estimated\n",
2841 memorystatus_suspended_count
, average_resident_pages
, estimated_processes
);
2843 if ((memorystatus_suspended_count
- memorystatus_frozen_count
) > memorystatus_freeze_suspended_threshold
) {
2858 memorystatus_can_freeze(boolean_t
*memorystatus_freeze_swap_low
)
2860 /* Only freeze if we're sufficiently low on memory; this holds off freeze right
2861 after boot, and is generally is a no-op once we've reached steady state. */
2862 if (memorystatus_available_pages
> memorystatus_freeze_threshold
) {
2866 /* Check minimum suspended process threshold. */
2867 if (!memorystatus_can_freeze_processes()) {
2871 /* Is swap running low? */
2872 if (*memorystatus_freeze_swap_low
) {
2873 /* If there's been no movement in free swap pages since we last attempted freeze, return. */
2874 if (default_pager_swap_pages_free() < memorystatus_freeze_pages_min
) {
2878 /* Pages have been freed - we can retry. */
2879 *memorystatus_freeze_swap_low
= FALSE
;
2887 memorystatus_freeze_update_throttle_interval(mach_timespec_t
*ts
, struct throttle_interval_t
*interval
)
2889 if (CMP_MACH_TIMESPEC(ts
, &interval
->ts
) >= 0) {
2890 if (!interval
->max_pageouts
) {
2891 interval
->max_pageouts
= (interval
->burst_multiple
* (((uint64_t)interval
->mins
* FREEZE_DAILY_PAGEOUTS_MAX
) / (24 * 60)));
2893 printf("memorystatus_freeze_update_throttle_interval: %d minute throttle timeout, resetting\n", interval
->mins
);
2895 interval
->ts
.tv_sec
= interval
->mins
* 60;
2896 interval
->ts
.tv_nsec
= 0;
2897 ADD_MACH_TIMESPEC(&interval
->ts
, ts
);
2898 /* Since we update the throttle stats pre-freeze, adjust for overshoot here */
2899 if (interval
->pageouts
> interval
->max_pageouts
) {
2900 interval
->pageouts
-= interval
->max_pageouts
;
2902 interval
->pageouts
= 0;
2904 interval
->throttle
= FALSE
;
2905 } else if (!interval
->throttle
&& interval
->pageouts
>= interval
->max_pageouts
) {
2906 printf("memorystatus_freeze_update_throttle_interval: %d minute pageout limit exceeded; enabling throttle\n", interval
->mins
);
2907 interval
->throttle
= TRUE
;
2910 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining; throttle %s\n",
2911 interval
->pageouts
, interval
->max_pageouts
, interval
->mins
, (interval
->ts
.tv_sec
- ts
->tv_sec
) / 60,
2912 interval
->throttle
? "on" : "off");
2916 memorystatus_freeze_update_throttle(void)
2922 boolean_t throttled
= FALSE
;
2924 #if DEVELOPMENT || DEBUG
2925 if (!memorystatus_freeze_throttle_enabled
)
2929 clock_get_system_nanotime(&sec
, &nsec
);
2933 /* Check freeze pageouts over multiple intervals and throttle if we've exceeded our budget.
2935 * This ensures that periods of inactivity can't be used as 'credit' towards freeze if the device has
2936 * remained dormant for a long period. We do, however, allow increased thresholds for shorter intervals in
2937 * order to allow for bursts of activity.
2939 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
2940 memorystatus_freeze_update_throttle_interval(&ts
, &throttle_intervals
[i
]);
2941 if (throttle_intervals
[i
].throttle
== TRUE
)
2949 memorystatus_freeze_thread(void *param __unused
, wait_result_t wr __unused
)
2951 static boolean_t memorystatus_freeze_swap_low
= FALSE
;
2953 if (memorystatus_freeze_enabled
) {
2954 if (memorystatus_can_freeze(&memorystatus_freeze_swap_low
)) {
2955 /* Only freeze if we've not exceeded our pageout budgets or we're not backed by swap. */
2956 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS
||
2957 !memorystatus_freeze_update_throttle()) {
2958 memorystatus_freeze_top_process(&memorystatus_freeze_swap_low
);
2960 printf("memorystatus_freeze_thread: in throttle, ignoring freeze\n");
2961 memorystatus_freeze_throttle_count
++; /* Throttled, update stats */
2966 assert_wait((event_t
) &memorystatus_freeze_wakeup
, THREAD_UNINT
);
2967 thread_block((thread_continue_t
) memorystatus_freeze_thread
);
2970 #endif /* CONFIG_FREEZE */
2972 #if VM_PRESSURE_EVENTS
2974 #if CONFIG_MEMORYSTATUS
2977 memorystatus_send_note(int event_code
, void *data
, size_t data_length
) {
2979 struct kev_msg ev_msg
;
2981 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2982 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
2983 ev_msg
.kev_subclass
= KEV_MEMORYSTATUS_SUBCLASS
;
2985 ev_msg
.event_code
= event_code
;
2987 ev_msg
.dv
[0].data_length
= data_length
;
2988 ev_msg
.dv
[0].data_ptr
= data
;
2989 ev_msg
.dv
[1].data_length
= 0;
2991 ret
= kev_post_msg(&ev_msg
);
2993 printf("%s: kev_post_msg() failed, err %d\n", __func__
, ret
);
3000 memorystatus_warn_process(pid_t pid
, boolean_t critical
) {
3002 boolean_t ret
= FALSE
;
3003 struct knote
*kn
= NULL
;
3006 * See comment in sysctl_memorystatus_vm_pressure_send.
3009 memorystatus_klist_lock();
3010 kn
= vm_find_knote_from_pid(pid
, &memorystatus_klist
);
3013 * By setting the "fflags" here, we are forcing
3014 * a process to deal with the case where it's
3015 * bumping up into its memory limits. If we don't
3016 * do this here, we will end up depending on the
3017 * system pressure snapshot evaluation in
3018 * filt_memorystatus().
3022 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
;
3024 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
3026 KNOTE(&memorystatus_klist
, kMemorystatusPressure
);
3029 if (vm_dispatch_pressure_note_to_pid(pid
, FALSE
) == 0) {
3033 memorystatus_klist_unlock();
3039 memorystatus_send_pressure_note(pid_t pid
) {
3040 MEMORYSTATUS_DEBUG(1, "memorystatus_send_pressure_note(): pid %d\n", pid
);
3041 return memorystatus_send_note(kMemorystatusPressureNote
, &pid
, sizeof(pid
));
3045 memorystatus_send_low_swap_note(void) {
3047 struct knote
*kn
= NULL
;
3049 memorystatus_klist_lock();
3050 SLIST_FOREACH(kn
, &memorystatus_klist
, kn_selnext
) {
3051 if (is_knote_registered_modify_task_pressure_bits(kn
, NOTE_MEMORYSTATUS_LOW_SWAP
, NULL
, 0, 0) == TRUE
) {
3052 KNOTE(&memorystatus_klist
, kMemorystatusLowSwap
);
3055 memorystatus_klist_unlock();
3059 memorystatus_bg_pressure_eligible(proc_t p
) {
3060 boolean_t eligible
= FALSE
;
3064 MEMORYSTATUS_DEBUG(1, "memorystatus_bg_pressure_eligible: pid %d, state 0x%x\n", p
->p_pid
, p
->p_memstat_state
);
3066 /* Foreground processes have already been dealt with at this point, so just test for eligibility */
3067 if (!(p
->p_memstat_state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_SUSPENDED
| P_MEMSTAT_FROZEN
))) {
3077 memorystatus_is_foreground_locked(proc_t p
) {
3078 return ((p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_FOREGROUND
) ||
3079 (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_FOREGROUND_SUPPORT
));
3081 #endif /* CONFIG_MEMORYSTATUS */
3084 * Trigger levels to test the mechanism.
3085 * Can be used via a sysctl.
3087 #define TEST_LOW_MEMORY_TRIGGER_ONE 1
3088 #define TEST_LOW_MEMORY_TRIGGER_ALL 2
3089 #define TEST_PURGEABLE_TRIGGER_ONE 3
3090 #define TEST_PURGEABLE_TRIGGER_ALL 4
3091 #define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE 5
3092 #define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL 6
3094 boolean_t memorystatus_manual_testing_on
= FALSE
;
3095 vm_pressure_level_t memorystatus_manual_testing_level
= kVMPressureNormal
;
3097 extern struct knote
*
3098 vm_pressure_select_optimal_candidate_to_notify(struct klist
*, int, boolean_t
);
3101 kern_return_t
vm_pressure_notification_without_levels(boolean_t
);
3103 extern void vm_pressure_klist_lock(void);
3104 extern void vm_pressure_klist_unlock(void);
3106 extern void vm_reset_active_list(void);
3108 extern void delay(int);
3110 #define INTER_NOTIFICATION_DELAY (250000) /* .25 second */
3112 void memorystatus_on_pageout_scan_end(void) {
3119 * knote_pressure_level - to check if the knote is registered for this notification level.
3121 * task - task whose bits we'll be modifying
3123 * pressure_level_to_clear - if the task has been notified of this past level, clear that notification bit so that if/when we revert to that level, the task will be notified again.
3125 * pressure_level_to_set - the task is about to be notified of this new level. Update the task's bit notification information appropriately.
3130 is_knote_registered_modify_task_pressure_bits(struct knote
*kn_max
, int knote_pressure_level
, task_t task
, vm_pressure_level_t pressure_level_to_clear
, vm_pressure_level_t pressure_level_to_set
)
3132 if (kn_max
->kn_sfflags
& knote_pressure_level
) {
3134 if (task_has_been_notified(task
, pressure_level_to_clear
) == TRUE
) {
3136 task_clear_has_been_notified(task
, pressure_level_to_clear
);
3139 task_mark_has_been_notified(task
, pressure_level_to_set
);
3146 extern kern_return_t
vm_pressure_notify_dispatch_vm_clients(boolean_t target_foreground_process
);
3148 #define VM_PRESSURE_DECREASED_SMOOTHING_PERIOD 5000 /* milliseconds */
3151 memorystatus_update_vm_pressure(boolean_t target_foreground_process
)
3153 struct knote
*kn_max
= NULL
;
3154 pid_t target_pid
= -1;
3155 struct klist dispatch_klist
= { NULL
};
3156 proc_t target_proc
= PROC_NULL
;
3157 struct task
*task
= NULL
;
3158 boolean_t found_candidate
= FALSE
;
3160 static vm_pressure_level_t level_snapshot
= kVMPressureNormal
;
3161 static vm_pressure_level_t prev_level_snapshot
= kVMPressureNormal
;
3162 boolean_t smoothing_window_started
= FALSE
;
3163 struct timeval smoothing_window_start_tstamp
= {0, 0};
3164 struct timeval curr_tstamp
= {0, 0};
3165 int elapsed_msecs
= 0;
3168 #define MAX_IDLE_KILLS 100 /* limit the number of idle kills allowed */
3170 int idle_kill_counter
= 0;
3173 * On desktop we take this opportunity to free up memory pressure
3174 * by immediately killing idle exitable processes. We use a delay
3175 * to avoid overkill. And we impose a max counter as a fail safe
3176 * in case daemons re-launch too fast.
3178 while ((memorystatus_vm_pressure_level
!= kVMPressureNormal
) && (idle_kill_counter
< MAX_IDLE_KILLS
)) {
3179 if (memorystatus_idle_exit_from_VM() == FALSE
) {
3180 /* No idle exitable processes left to kill */
3183 idle_kill_counter
++;
3184 delay(1000000); /* 1 second */
3186 #endif /* !CONFIG_JETSAM */
3191 * There is a race window here. But it's not clear
3192 * how much we benefit from having extra synchronization.
3194 level_snapshot
= memorystatus_vm_pressure_level
;
3196 if (prev_level_snapshot
> level_snapshot
) {
3198 * Pressure decreased? Let's take a little breather
3199 * and see if this condition stays.
3201 if (smoothing_window_started
== FALSE
) {
3203 smoothing_window_started
= TRUE
;
3204 microuptime(&smoothing_window_start_tstamp
);
3207 microuptime(&curr_tstamp
);
3208 timevalsub(&curr_tstamp
, &smoothing_window_start_tstamp
);
3209 elapsed_msecs
= curr_tstamp
.tv_sec
* 1000 + curr_tstamp
.tv_usec
/ 1000;
3211 if (elapsed_msecs
< VM_PRESSURE_DECREASED_SMOOTHING_PERIOD
) {
3213 delay(INTER_NOTIFICATION_DELAY
);
3218 prev_level_snapshot
= level_snapshot
;
3219 smoothing_window_started
= FALSE
;
3221 memorystatus_klist_lock();
3222 kn_max
= vm_pressure_select_optimal_candidate_to_notify(&memorystatus_klist
, level_snapshot
, target_foreground_process
);
3224 if (kn_max
== NULL
) {
3225 memorystatus_klist_unlock();
3228 * No more level-based clients to notify.
3229 * Try the non-level based notification clients.
3231 * However, these non-level clients don't understand
3232 * the "return-to-normal" notification.
3234 * So don't consider them for those notifications. Just
3239 if (level_snapshot
!= kVMPressureNormal
) {
3240 goto try_dispatch_vm_clients
;
3242 return KERN_FAILURE
;
3246 target_proc
= kn_max
->kn_kq
->kq_p
;
3249 if (target_proc
!= proc_ref_locked(target_proc
)) {
3250 target_proc
= PROC_NULL
;
3252 memorystatus_klist_unlock();
3256 memorystatus_klist_unlock();
3258 target_pid
= target_proc
->p_pid
;
3260 task
= (struct task
*)(target_proc
->task
);
3262 if (level_snapshot
!= kVMPressureNormal
) {
3264 if (level_snapshot
== kVMPressureWarning
|| level_snapshot
== kVMPressureUrgent
) {
3266 if (is_knote_registered_modify_task_pressure_bits(kn_max
, NOTE_MEMORYSTATUS_PRESSURE_WARN
, task
, kVMPressureCritical
, kVMPressureWarning
) == TRUE
) {
3267 found_candidate
= TRUE
;
3270 if (level_snapshot
== kVMPressureCritical
) {
3272 if (is_knote_registered_modify_task_pressure_bits(kn_max
, NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
, task
, kVMPressureWarning
, kVMPressureCritical
) == TRUE
) {
3273 found_candidate
= TRUE
;
3278 if (kn_max
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
3280 task_clear_has_been_notified(task
, kVMPressureWarning
);
3281 task_clear_has_been_notified(task
, kVMPressureCritical
);
3283 found_candidate
= TRUE
;
3287 if (found_candidate
== FALSE
) {
3291 memorystatus_klist_lock();
3292 KNOTE_DETACH(&memorystatus_klist
, kn_max
);
3293 KNOTE_ATTACH(&dispatch_klist
, kn_max
);
3294 memorystatus_klist_unlock();
3296 KNOTE(&dispatch_klist
, (level_snapshot
!= kVMPressureNormal
) ? kMemorystatusPressure
: kMemorystatusNoPressure
);
3298 memorystatus_klist_lock();
3299 KNOTE_DETACH(&dispatch_klist
, kn_max
);
3300 KNOTE_ATTACH(&memorystatus_klist
, kn_max
);
3301 memorystatus_klist_unlock();
3303 microuptime(&target_proc
->vm_pressure_last_notify_tstamp
);
3304 proc_rele(target_proc
);
3306 if (memorystatus_manual_testing_on
== TRUE
&& target_foreground_process
== TRUE
) {
3310 try_dispatch_vm_clients
:
3311 if (kn_max
== NULL
&& level_snapshot
!= kVMPressureNormal
) {
3313 * We will exit this loop when we are done with
3314 * notification clients (level and non-level based).
3316 if ((vm_pressure_notify_dispatch_vm_clients(target_foreground_process
) == KERN_FAILURE
) && (kn_max
== NULL
)) {
3318 * kn_max == NULL i.e. we didn't find any eligible clients for the level-based notifications
3320 * we have failed to find any eligible clients for the non-level based notifications too.
3324 return KERN_FAILURE
;
3329 * LD: This block of code below used to be invoked in the older memory notification scheme on embedded everytime
3330 * a process was sent a memory pressure notification. The "memorystatus_klist" list was used to hold these
3331 * privileged listeners. But now we have moved to the newer scheme and are trying to move away from the extra
3332 * notifications. So the code is here in case we break compat. and need to send out notifications to the privileged
3338 if (memorystatus_manual_testing_on
== TRUE
) {
3340 * Testing out the pressure notification scheme.
3341 * No need for delays etc.
3345 uint32_t sleep_interval
= INTER_NOTIFICATION_DELAY
;
3347 unsigned int page_delta
= 0;
3348 unsigned int skip_delay_page_threshold
= 0;
3350 assert(memorystatus_available_pages_pressure
>= memorystatus_available_pages_critical_base
);
3352 page_delta
= (memorystatus_available_pages_pressure
- memorystatus_available_pages_critical_base
) / 2;
3353 skip_delay_page_threshold
= memorystatus_available_pages_pressure
- page_delta
;
3355 if (memorystatus_available_pages
<= skip_delay_page_threshold
) {
3357 * We are nearing the critcal mark fast and can't afford to wait between
3362 #endif /* CONFIG_JETSAM */
3364 if (sleep_interval
) {
3365 delay(sleep_interval
);
3370 return KERN_SUCCESS
;
3374 convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t
);
3377 convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t internal_pressure_level
)
3379 vm_pressure_level_t dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_NORMAL
;
3381 switch (internal_pressure_level
) {
3383 case kVMPressureNormal
:
3385 dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_NORMAL
;
3389 case kVMPressureWarning
:
3390 case kVMPressureUrgent
:
3392 dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
3396 case kVMPressureCritical
:
3398 dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
;
3406 return dispatch_level
;
3410 sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS
3412 #pragma unused(arg1, arg2, oidp)
3413 vm_pressure_level_t dispatch_level
= convert_internal_pressure_level_to_dispatch_level(memorystatus_vm_pressure_level
);
3415 return SYSCTL_OUT(req
, &dispatch_level
, sizeof(dispatch_level
));
3418 #if DEBUG || DEVELOPMENT
3420 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_vm_pressure_level
, CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_LOCKED
,
3421 0, 0, &sysctl_memorystatus_vm_pressure_level
, "I", "");
3423 #else /* DEBUG || DEVELOPMENT */
3425 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_vm_pressure_level
, CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
3426 0, 0, &sysctl_memorystatus_vm_pressure_level
, "I", "");
3428 #endif /* DEBUG || DEVELOPMENT */
3430 extern int memorystatus_purge_on_warning
;
3431 extern int memorystatus_purge_on_critical
;
3434 sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS
3436 #pragma unused(arg1, arg2)
3440 int pressure_level
= 0;
3441 int trigger_request
= 0;
3444 error
= sysctl_handle_int(oidp
, &level
, 0, req
);
3445 if (error
|| !req
->newptr
) {
3449 memorystatus_manual_testing_on
= TRUE
;
3451 trigger_request
= (level
>> 16) & 0xFFFF;
3452 pressure_level
= (level
& 0xFFFF);
3454 if (trigger_request
< TEST_LOW_MEMORY_TRIGGER_ONE
||
3455 trigger_request
> TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL
) {
3458 switch (pressure_level
) {
3459 case NOTE_MEMORYSTATUS_PRESSURE_NORMAL
:
3460 case NOTE_MEMORYSTATUS_PRESSURE_WARN
:
3461 case NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
:
3468 * The pressure level is being set from user-space.
3469 * And user-space uses the constants in sys/event.h
3470 * So we translate those events to our internal levels here.
3472 if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
3474 memorystatus_manual_testing_level
= kVMPressureNormal
;
3477 } else if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_WARN
) {
3479 memorystatus_manual_testing_level
= kVMPressureWarning
;
3480 force_purge
= memorystatus_purge_on_warning
;
3482 } else if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
) {
3484 memorystatus_manual_testing_level
= kVMPressureCritical
;
3485 force_purge
= memorystatus_purge_on_critical
;
3488 memorystatus_vm_pressure_level
= memorystatus_manual_testing_level
;
3490 /* purge according to the new pressure level */
3491 switch (trigger_request
) {
3492 case TEST_PURGEABLE_TRIGGER_ONE
:
3493 case TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE
:
3494 if (force_purge
== 0) {
3495 /* no purging requested */
3498 vm_purgeable_object_purge_one_unlocked(force_purge
);
3500 case TEST_PURGEABLE_TRIGGER_ALL
:
3501 case TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL
:
3502 if (force_purge
== 0) {
3503 /* no purging requested */
3506 while (vm_purgeable_object_purge_one_unlocked(force_purge
));
3510 if ((trigger_request
== TEST_LOW_MEMORY_TRIGGER_ONE
) ||
3511 (trigger_request
== TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE
)) {
3513 memorystatus_update_vm_pressure(TRUE
);
3516 if ((trigger_request
== TEST_LOW_MEMORY_TRIGGER_ALL
) ||
3517 (trigger_request
== TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL
)) {
3519 while (memorystatus_update_vm_pressure(FALSE
) == KERN_SUCCESS
) {
3524 if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
3525 memorystatus_manual_testing_on
= FALSE
;
3527 vm_pressure_klist_lock();
3528 vm_reset_active_list();
3529 vm_pressure_klist_unlock();
3532 vm_pressure_klist_lock();
3533 vm_pressure_notification_without_levels(FALSE
);
3534 vm_pressure_klist_unlock();
3540 SYSCTL_PROC(_kern
, OID_AUTO
, memorypressure_manual_trigger
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
3541 0, 0, &sysctl_memorypressure_manual_trigger
, "I", "");
3544 extern int memorystatus_purge_on_warning
;
3545 extern int memorystatus_purge_on_urgent
;
3546 extern int memorystatus_purge_on_critical
;
3548 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_purge_on_warning
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_purge_on_warning
, 0, "");
3549 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_purge_on_urgent
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_purge_on_urgent
, 0, "");
3550 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_purge_on_critical
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_purge_on_critical
, 0, "");
3553 #endif /* VM_PRESSURE_EVENTS */
3555 /* Return both allocated and actual size, since there's a race between allocation and list compilation */
3557 memorystatus_get_priority_list(memorystatus_priority_entry_t
**list_ptr
, size_t *buffer_size
, size_t *list_size
, boolean_t size_only
)
3559 uint32_t list_count
, i
= 0;
3560 memorystatus_priority_entry_t
*list_entry
;
3563 list_count
= memorystatus_list_count
;
3564 *list_size
= sizeof(memorystatus_priority_entry_t
) * list_count
;
3566 /* Just a size check? */
3571 /* Otherwise, validate the size of the buffer */
3572 if (*buffer_size
< *list_size
) {
3576 *list_ptr
= (memorystatus_priority_entry_t
*)kalloc(*list_size
);
3581 memset(*list_ptr
, 0, *list_size
);
3583 *buffer_size
= *list_size
;
3586 list_entry
= *list_ptr
;
3590 p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3591 while (p
&& (*list_size
< *buffer_size
)) {
3592 list_entry
->pid
= p
->p_pid
;
3593 list_entry
->priority
= p
->p_memstat_effectivepriority
;
3594 list_entry
->user_data
= p
->p_memstat_userdata
;
3596 if (((p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
) && (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
)) ||
3597 (p
->p_memstat_memlimit
<= 0)) {
3598 task_get_phys_footprint_limit(p
->task
, &list_entry
->limit
);
3600 list_entry
->limit
= p
->p_memstat_memlimit
;
3603 task_get_phys_footprint_limit(p
->task
, &list_entry
->limit
);
3605 list_entry
->state
= memorystatus_build_state(p
);
3608 *list_size
+= sizeof(memorystatus_priority_entry_t
);
3610 p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
3615 MEMORYSTATUS_DEBUG(1, "memorystatus_get_priority_list: returning %lu for size\n", (unsigned long)*list_size
);
3621 memorystatus_cmd_get_priority_list(user_addr_t buffer
, size_t buffer_size
, int32_t *retval
) {
3623 boolean_t size_only
;
3624 memorystatus_priority_entry_t
*list
= NULL
;
3627 size_only
= ((buffer
== USER_ADDR_NULL
) ? TRUE
: FALSE
);
3629 error
= memorystatus_get_priority_list(&list
, &buffer_size
, &list_size
, size_only
);
3635 error
= copyout(list
, buffer
, list_size
);
3639 *retval
= list_size
;
3644 kfree(list
, buffer_size
);
3653 memorystatus_clear_errors(void)
3658 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_CLEAR_ERRORS
) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3662 p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3664 if (p
->p_memstat_state
& P_MEMSTAT_ERROR
) {
3665 p
->p_memstat_state
&= ~P_MEMSTAT_ERROR
;
3667 p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
3672 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_CLEAR_ERRORS
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3676 memorystatus_update_levels_locked(boolean_t critical_only
) {
3678 memorystatus_available_pages_critical
= memorystatus_available_pages_critical_base
;
3681 * If there's an entry in the first bucket, we have idle processes.
3683 memstat_bucket_t
*first_bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE
];
3684 if (first_bucket
->count
) {
3685 memorystatus_available_pages_critical
+= memorystatus_available_pages_critical_idle_offset
;
3687 if (memorystatus_available_pages_critical
> memorystatus_available_pages_pressure
) {
3689 * The critical threshold must never exceed the pressure threshold
3691 memorystatus_available_pages_critical
= memorystatus_available_pages_pressure
;
3695 #if DEBUG || DEVELOPMENT
3696 if (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) {
3697 memorystatus_available_pages_critical
+= memorystatus_jetsam_policy_offset_pages_diagnostic
;
3699 if (memorystatus_available_pages_critical
> memorystatus_available_pages_pressure
) {
3701 * The critical threshold must never exceed the pressure threshold
3703 memorystatus_available_pages_critical
= memorystatus_available_pages_pressure
;
3708 if (critical_only
) {
3712 #if VM_PRESSURE_EVENTS
3713 memorystatus_available_pages_pressure
= (pressure_threshold_percentage
/ delta_percentage
) * memorystatus_delta
;
3714 #if DEBUG || DEVELOPMENT
3715 if (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) {
3716 memorystatus_available_pages_pressure
+= memorystatus_jetsam_policy_offset_pages_diagnostic
;
3723 memorystatus_get_snapshot(memorystatus_jetsam_snapshot_t
**snapshot
, size_t *snapshot_size
, boolean_t size_only
) {
3724 size_t input_size
= *snapshot_size
;
3726 if (memorystatus_jetsam_snapshot_count
> 0) {
3727 *snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) + (sizeof(memorystatus_jetsam_snapshot_entry_t
) * (memorystatus_jetsam_snapshot_count
));
3736 if (input_size
< *snapshot_size
) {
3740 *snapshot
= memorystatus_jetsam_snapshot
;
3742 MEMORYSTATUS_DEBUG(1, "memorystatus_snapshot: returning %ld for size\n", (long)*snapshot_size
);
3749 memorystatus_cmd_get_jetsam_snapshot(user_addr_t buffer
, size_t buffer_size
, int32_t *retval
) {
3751 boolean_t size_only
;
3752 memorystatus_jetsam_snapshot_t
*snapshot
;
3754 size_only
= ((buffer
== USER_ADDR_NULL
) ? TRUE
: FALSE
);
3756 error
= memorystatus_get_snapshot(&snapshot
, &buffer_size
, size_only
);
3761 /* Copy out and reset */
3763 if ((error
= copyout(snapshot
, buffer
, buffer_size
)) == 0) {
3764 snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
3769 *retval
= buffer_size
;
3776 * Routine: memorystatus_cmd_grp_set_properties
3777 * Purpose: Update properties for a group of processes.
3779 * Supported Properties:
3781 * Move each process out of its effective priority
3782 * band and into a new priority band.
3783 * Maintains relative order from lowest to highest priority.
3784 * In single band, maintains relative order from head to tail.
3786 * eg: before [effectivepriority | pid]
3788 * [17 | p55, p67, p19 ]
3793 * after [ new band | pid]
3794 * [ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101]
3796 * Returns: 0 on success, else non-zero.
3798 * Caveat: We know there is a race window regarding recycled pids.
3799 * A process could be killed before the kernel can act on it here.
3800 * If a pid cannot be found in any of the jetsam priority bands,
3801 * then we simply ignore it. No harm.
3802 * But, if the pid has been recycled then it could be an issue.
3803 * In that scenario, we might move an unsuspecting process to the new
3804 * priority band. It's not clear how the kernel can safeguard
3805 * against this, but it would be an extremely rare case anyway.
3806 * The caller of this api might avoid such race conditions by
3807 * ensuring that the processes passed in the pid list are suspended.
3811 /* This internal structure can expand when we add support for more properties */
3812 typedef struct memorystatus_internal_properties
3815 int32_t priority
; /* see memorytstatus_priority_entry_t : priority */
3816 } memorystatus_internal_properties_t
;
3820 memorystatus_cmd_grp_set_properties(int32_t flags
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
) {
3822 #pragma unused (flags)
3825 * We only handle setting priority
3830 memorystatus_priority_entry_t
*entries
= NULL
;
3831 uint32_t entry_count
= 0;
3833 /* This will be the ordered proc list */
3834 memorystatus_internal_properties_t
*table
= NULL
;
3835 size_t table_size
= 0;
3836 uint32_t table_count
= 0;
3839 uint32_t bucket_index
= 0;
3840 boolean_t head_insert
;
3841 int32_t new_priority
;
3846 if ((buffer
== USER_ADDR_NULL
) || (buffer_size
== 0) || ((buffer_size
% sizeof(memorystatus_priority_entry_t
)) != 0)) {
3851 entry_count
= (buffer_size
/ sizeof(memorystatus_priority_entry_t
));
3852 if ((entries
= (memorystatus_priority_entry_t
*)kalloc(buffer_size
)) == NULL
) {
3857 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_GRP_SET_PROP
) | DBG_FUNC_START
, entry_count
, 0, 0, 0, 0);
3859 if ((error
= copyin(buffer
, entries
, buffer_size
)) != 0) {
3863 /* Verify sanity of input priorities */
3864 for (i
=0; i
< entry_count
; i
++) {
3865 if (entries
[i
].priority
== -1) {
3866 /* Use as shorthand for default priority */
3867 entries
[i
].priority
= JETSAM_PRIORITY_DEFAULT
;
3868 } else if (entries
[i
].priority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
3869 /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use;
3870 * if requested, adjust to JETSAM_PRIORITY_IDLE. */
3871 entries
[i
].priority
= JETSAM_PRIORITY_IDLE
;
3872 } else if (entries
[i
].priority
== JETSAM_PRIORITY_IDLE_HEAD
) {
3873 /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle
3875 /* Deal with this later */
3876 } else if ((entries
[i
].priority
< 0) || (entries
[i
].priority
>= MEMSTAT_BUCKET_COUNT
)) {
3883 table_size
= sizeof(memorystatus_internal_properties_t
) * entry_count
;
3884 if ( (table
= (memorystatus_internal_properties_t
*)kalloc(table_size
)) == NULL
) {
3888 memset(table
, 0, table_size
);
3892 * For each jetsam bucket entry, spin through the input property list.
3893 * When a matching pid is found, populate an adjacent table with the
3894 * appropriate proc pointer and new property values.
3895 * This traversal automatically preserves order from lowest
3896 * to highest priority.
3903 /* Create the ordered table */
3904 p
= memorystatus_get_first_proc_locked(&bucket_index
, TRUE
);
3905 while (p
&& (table_count
< entry_count
)) {
3906 for (i
=0; i
< entry_count
; i
++ ) {
3907 if (p
->p_pid
== entries
[i
].pid
) {
3908 /* Build the table data */
3909 table
[table_count
].proc
= p
;
3910 table
[table_count
].priority
= entries
[i
].priority
;
3915 p
= memorystatus_get_next_proc_locked(&bucket_index
, p
, TRUE
);
3918 /* We now have ordered list of procs ready to move */
3919 for (i
=0; i
< table_count
; i
++) {
3923 /* Allow head inserts -- but relative order is now */
3924 if (table
[i
].priority
== JETSAM_PRIORITY_IDLE_HEAD
) {
3925 new_priority
= JETSAM_PRIORITY_IDLE
;
3928 new_priority
= table
[i
].priority
;
3929 head_insert
= false;
3933 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
3938 * Take appropriate steps if moving proc out of the
3939 * JETSAM_PRIORITY_IDLE_DEFERRED band.
3941 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
3942 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
3945 memorystatus_update_priority_locked(p
, new_priority
, head_insert
);
3951 * if (table_count != entry_count)
3952 * then some pids were not found in a jetsam band.
3953 * harmless but interesting...
3955 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_GRP_SET_PROP
) | DBG_FUNC_END
, entry_count
, table_count
, 0, 0, 0);
3959 kfree(entries
, buffer_size
);
3961 kfree(table
, table_size
);
3968 * This routine is meant solely for the purpose of adjusting jetsam priorities and bands.
3969 * It is _not_ meant to be used for the setting of memory limits, especially, since we can't
3970 * tell if the memory limit being set is fatal or not.
3972 * So the the last 5 args to the memorystatus_update() call below, related to memory limits, are all 0 or FALSE.
3976 memorystatus_cmd_set_priority_properties(pid_t pid
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
) {
3977 const uint32_t MAX_ENTRY_COUNT
= 2; /* Cap the entry count */
3981 uint32_t entry_count
;
3982 memorystatus_priority_properties_t
*entries
;
3984 /* Validate inputs */
3985 if ((pid
== 0) || (buffer
== USER_ADDR_NULL
) || (buffer_size
== 0)) {
3989 /* Make sure the buffer is a multiple of the entry size, and that an excessive size isn't specified */
3990 entry_count
= (buffer_size
/ sizeof(memorystatus_priority_properties_t
));
3991 if (((buffer_size
% sizeof(memorystatus_priority_properties_t
)) != 0) || (entry_count
> MAX_ENTRY_COUNT
)) {
3995 entries
= (memorystatus_priority_properties_t
*)kalloc(buffer_size
);
3997 error
= copyin(buffer
, entries
, buffer_size
);
3999 for (i
= 0; i
< entry_count
; i
++) {
4012 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
4018 error
= memorystatus_update(p
, entries
[i
].priority
, entries
[i
].user_data
, FALSE
, FALSE
, 0, 0, FALSE
);
4022 kfree(entries
, buffer_size
);
4028 memorystatus_cmd_get_pressure_status(int32_t *retval
) {
4031 /* Need privilege for check */
4032 error
= priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE
, 0);
4037 /* Inherently racy, so it's not worth taking a lock here */
4038 *retval
= (kVMPressureNormal
!= memorystatus_vm_pressure_level
) ? 1 : 0;
4044 * Every process, including a P_MEMSTAT_INTERNAL process (currently only pid 1), is allowed to set a HWM.
4048 memorystatus_cmd_set_jetsam_memory_limit(pid_t pid
, int32_t high_water_mark
, __unused
int32_t *retval
, boolean_t is_fatal_limit
) {
4051 proc_t p
= proc_find(pid
);
4056 if (high_water_mark
<= 0) {
4057 high_water_mark
= -1; /* Disable */
4062 p
->p_memstat_memlimit
= high_water_mark
;
4063 if (memorystatus_highwater_enabled
) {
4064 if (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
) {
4066 memorystatus_update_priority_locked(p
, p
->p_memstat_effectivepriority
, false);
4069 * The update priority call above takes care to set/reset the fatal memory limit state
4070 * IF the process is transitioning between foreground <-> background and has a background
4072 * Here, however, the process won't be doing any such transitions and so we explicitly tackle
4073 * the fatal limit state.
4075 is_fatal_limit
= FALSE
;
4078 error
= (task_set_phys_footprint_limit_internal(p
->task
, high_water_mark
, NULL
, TRUE
) == 0) ? 0 : EINVAL
;
4083 if (is_fatal_limit
== TRUE
) {
4084 p
->p_memstat_state
|= P_MEMSTAT_FATAL_MEMLIMIT
;
4086 p
->p_memstat_state
&= ~P_MEMSTAT_FATAL_MEMLIMIT
;
4097 * Returns the jetsam priority (effective or requested) of the process
4098 * associated with this task.
4101 proc_get_memstat_priority(proc_t p
, boolean_t effective_priority
)
4104 if (effective_priority
) {
4105 return p
->p_memstat_effectivepriority
;
4107 return p
->p_memstat_requestedpriority
;
4112 #endif /* CONFIG_JETSAM */
4115 memorystatus_control(struct proc
*p __unused
, struct memorystatus_control_args
*args
, int *ret
) {
4122 /* Root only for now */
4123 if (!kauth_cred_issuser(kauth_cred_get())) {
4129 if (args
->buffersize
> MEMORYSTATUS_BUFFERSIZE_MAX
) {
4134 switch (args
->command
) {
4135 case MEMORYSTATUS_CMD_GET_PRIORITY_LIST
:
4136 error
= memorystatus_cmd_get_priority_list(args
->buffer
, args
->buffersize
, ret
);
4139 case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
:
4140 error
= memorystatus_cmd_set_priority_properties(args
->pid
, args
->buffer
, args
->buffersize
, ret
);
4142 case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES
:
4143 error
= memorystatus_cmd_grp_set_properties((int32_t)args
->flags
, args
->buffer
, args
->buffersize
, ret
);
4145 case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
:
4146 error
= memorystatus_cmd_get_jetsam_snapshot(args
->buffer
, args
->buffersize
, ret
);
4148 case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS
:
4149 error
= memorystatus_cmd_get_pressure_status(ret
);
4151 case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK
:
4152 error
= memorystatus_cmd_set_jetsam_memory_limit(args
->pid
, (int32_t)args
->flags
, ret
, FALSE
);
4154 case MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT
:
4155 error
= memorystatus_cmd_set_jetsam_memory_limit(args
->pid
, (int32_t)args
->flags
, ret
, TRUE
);
4158 #if DEVELOPMENT || DEBUG
4159 case MEMORYSTATUS_CMD_TEST_JETSAM
:
4160 error
= memorystatus_kill_process_sync(args
->pid
, kMemorystatusKilled
) ? 0 : EINVAL
;
4162 case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS
:
4163 error
= memorystatus_cmd_set_panic_bits(args
->buffer
, args
->buffersize
);
4165 #endif /* DEVELOPMENT || DEBUG */
4166 #endif /* CONFIG_JETSAM */
4177 filt_memorystatusattach(struct knote
*kn
)
4179 kn
->kn_flags
|= EV_CLEAR
;
4180 return memorystatus_knote_register(kn
);
4184 filt_memorystatusdetach(struct knote
*kn
)
4186 memorystatus_knote_unregister(kn
);
4190 filt_memorystatus(struct knote
*kn __unused
, long hint
)
4194 case kMemorystatusNoPressure
:
4195 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
4196 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_PRESSURE_NORMAL
;
4199 case kMemorystatusPressure
:
4200 if (memorystatus_vm_pressure_level
== kVMPressureWarning
|| memorystatus_vm_pressure_level
== kVMPressureUrgent
) {
4201 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_WARN
) {
4202 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
4204 } else if (memorystatus_vm_pressure_level
== kVMPressureCritical
) {
4206 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
) {
4207 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
;
4211 case kMemorystatusLowSwap
:
4212 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_LOW_SWAP
) {
4213 kn
->kn_fflags
|= NOTE_MEMORYSTATUS_LOW_SWAP
;
4221 return (kn
->kn_fflags
!= 0);
4225 memorystatus_klist_lock(void) {
4226 lck_mtx_lock(&memorystatus_klist_mutex
);
4230 memorystatus_klist_unlock(void) {
4231 lck_mtx_unlock(&memorystatus_klist_mutex
);
4235 memorystatus_kevent_init(lck_grp_t
*grp
, lck_attr_t
*attr
) {
4236 lck_mtx_init(&memorystatus_klist_mutex
, grp
, attr
);
4237 klist_init(&memorystatus_klist
);
4241 memorystatus_knote_register(struct knote
*kn
) {
4244 memorystatus_klist_lock();
4246 if (kn
->kn_sfflags
& (NOTE_MEMORYSTATUS_PRESSURE_NORMAL
| NOTE_MEMORYSTATUS_PRESSURE_WARN
| NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
| NOTE_MEMORYSTATUS_LOW_SWAP
)) {
4248 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_LOW_SWAP
) {
4249 error
= suser(kauth_cred_get(), 0);
4253 KNOTE_ATTACH(&memorystatus_klist
, kn
);
4259 memorystatus_klist_unlock();
4265 memorystatus_knote_unregister(struct knote
*kn __unused
) {
4266 memorystatus_klist_lock();
4267 KNOTE_DETACH(&memorystatus_klist
, kn
);
4268 memorystatus_klist_unlock();
4273 #if CONFIG_JETSAM && VM_PRESSURE_EVENTS
4275 memorystatus_issue_pressure_kevent(boolean_t pressured
) {
4276 memorystatus_klist_lock();
4277 KNOTE(&memorystatus_klist
, pressured
? kMemorystatusPressure
: kMemorystatusNoPressure
);
4278 memorystatus_klist_unlock();
4281 #endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */