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/coalition.h>
40 #include <mach/mach_time.h>
41 #include <mach/task.h>
42 #include <mach/host_priv.h>
43 #include <mach/mach_host.h>
44 #include <pexpert/pexpert.h>
45 #include <sys/coalition.h>
46 #include <sys/kern_event.h>
48 #include <sys/proc_info.h>
49 #include <sys/signal.h>
50 #include <sys/signalvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysproto.h>
56 #include <vm/vm_pageout.h>
57 #include <vm/vm_protos.h>
60 #include <vm/vm_map.h>
61 #endif /* CONFIG_FREEZE */
63 #include <sys/kern_memorystatus.h>
66 /* For logging clarity */
67 static const char *jetsam_kill_cause_name
[] = {
69 "jettisoned" , /* kMemorystatusKilled */
70 "highwater" , /* kMemorystatusKilledHiwat */
71 "vnode-limit" , /* kMemorystatusKilledVnodes */
72 "vm-pageshortage" , /* kMemorystatusKilledVMPageShortage */
73 "vm-thrashing" , /* kMemorystatusKilledVMThrashing */
74 "fc-thrashing" , /* kMemorystatusKilledFCThrashing */
75 "per-process-limit" , /* kMemorystatusKilledPerProcessLimit */
76 "diagnostic" , /* kMemorystatusKilledDiagnostic */
77 "idle-exit" , /* kMemorystatusKilledIdleExit */
80 /* Does cause indicate vm or fc thrashing? */
82 is_thrashing(unsigned cause
)
85 case kMemorystatusKilledVMThrashing
:
86 case kMemorystatusKilledFCThrashing
:
93 /* Callback into vm_compressor.c to signal that thrashing has been mitigated. */
94 extern void vm_thrashing_jetsam_done(void);
97 /* These are very verbose printfs(), enable with
98 * MEMORYSTATUS_DEBUG_LOG
100 #if MEMORYSTATUS_DEBUG_LOG
101 #define MEMORYSTATUS_DEBUG(cond, format, ...) \
103 if (cond) { printf(format, ##__VA_ARGS__); } \
106 #define MEMORYSTATUS_DEBUG(cond, format, ...)
110 * Active / Inactive limit support
111 * proc list must be locked
113 * The SET_*** macros are used to initialize a limit
114 * for the first time.
116 * The CACHE_*** macros are use to cache the limit that will
117 * soon be in effect down in the ledgers.
120 #define SET_ACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \
122 (p)->p_memstat_memlimit_active = (limit); \
123 (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED; \
125 (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \
127 (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \
131 #define SET_INACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \
133 (p)->p_memstat_memlimit_inactive = (limit); \
134 (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED; \
136 (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \
138 (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \
142 #define CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception) \
144 (p)->p_memstat_memlimit = (p)->p_memstat_memlimit_active; \
145 if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { \
146 (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \
148 (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \
150 if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED) { \
151 trigger_exception = FALSE; \
153 trigger_exception = TRUE; \
157 #define CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception) \
159 (p)->p_memstat_memlimit = (p)->p_memstat_memlimit_inactive; \
160 if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { \
161 (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \
163 (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \
165 if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED) { \
166 trigger_exception = FALSE; \
168 trigger_exception = TRUE; \
173 /* General tunables */
175 unsigned long delta_percentage
= 5;
176 unsigned long critical_threshold_percentage
= 5;
177 unsigned long idle_offset_percentage
= 5;
178 unsigned long pressure_threshold_percentage
= 15;
179 unsigned long freeze_threshold_percentage
= 50;
181 /* General memorystatus stuff */
183 struct klist memorystatus_klist
;
184 static lck_mtx_t memorystatus_klist_mutex
;
186 static void memorystatus_klist_lock(void);
187 static void memorystatus_klist_unlock(void);
189 static uint64_t memorystatus_idle_delay_time
= 0;
192 * Memorystatus kevents
195 static int filt_memorystatusattach(struct knote
*kn
);
196 static void filt_memorystatusdetach(struct knote
*kn
);
197 static int filt_memorystatus(struct knote
*kn
, long hint
);
199 struct filterops memorystatus_filtops
= {
200 .f_attach
= filt_memorystatusattach
,
201 .f_detach
= filt_memorystatusdetach
,
202 .f_event
= filt_memorystatus
,
206 kMemorystatusNoPressure
= 0x1,
207 kMemorystatusPressure
= 0x2,
208 kMemorystatusLowSwap
= 0x4
211 /* Idle guard handling */
213 static int32_t memorystatus_scheduled_idle_demotions
= 0;
215 static thread_call_t memorystatus_idle_demotion_call
;
217 static void memorystatus_perform_idle_demotion(__unused
void *spare1
, __unused
void *spare2
);
218 static void memorystatus_schedule_idle_demotion_locked(proc_t p
, boolean_t set_state
);
219 static void memorystatus_invalidate_idle_demotion_locked(proc_t p
, boolean_t clean_state
);
220 static void memorystatus_reschedule_idle_demotion_locked(void);
222 static void memorystatus_update_priority_locked(proc_t p
, int priority
, boolean_t head_insert
);
224 boolean_t
is_knote_registered_modify_task_pressure_bits(struct knote
*, int, task_t
, vm_pressure_level_t
, vm_pressure_level_t
);
225 void memorystatus_send_low_swap_note(void);
227 int memorystatus_wakeup
= 0;
229 unsigned int memorystatus_level
= 0;
230 unsigned int memorystatus_early_boot_level
= 0;
232 static int memorystatus_list_count
= 0;
234 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1)
236 typedef struct memstat_bucket
{
237 TAILQ_HEAD(, proc
) list
;
241 memstat_bucket_t memstat_bucket
[MEMSTAT_BUCKET_COUNT
];
243 uint64_t memstat_idle_demotion_deadline
= 0;
245 static unsigned int memorystatus_dirty_count
= 0;
248 SYSCTL_INT(_kern
, OID_AUTO
, max_task_pmem
, CTLFLAG_RD
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
, &max_task_footprint_mb
, 0, "");
249 #endif // CONFIG_JETSAM
253 memorystatus_get_level(__unused
struct proc
*p
, struct memorystatus_get_level_args
*args
, __unused
int *ret
)
255 user_addr_t level
= 0;
259 if (copyout(&memorystatus_level
, level
, sizeof(memorystatus_level
)) != 0) {
266 static proc_t
memorystatus_get_first_proc_locked(unsigned int *bucket_index
, boolean_t search
);
267 static proc_t
memorystatus_get_next_proc_locked(unsigned int *bucket_index
, proc_t p
, boolean_t search
);
269 static void memorystatus_thread(void *param __unused
, wait_result_t wr __unused
);
275 static int memorystatus_cmd_set_jetsam_memory_limit(pid_t pid
, int32_t high_water_mark
, __unused
int32_t *retval
, boolean_t is_fatal_limit
);
277 static int memorystatus_cmd_set_memlimit_properties(pid_t pid
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
);
279 static int memorystatus_set_memlimit_properties(pid_t pid
, memorystatus_memlimit_properties_t
*entry
);
281 static int memorystatus_cmd_get_memlimit_properties(pid_t pid
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
);
283 static boolean_t
proc_jetsam_state_is_active_locked(proc_t
);
285 int proc_get_memstat_priority(proc_t
, boolean_t
);
287 /* Kill processes exceeding their limit either under memory pressure (1), or as soon as possible (0) */
288 #define LEGACY_HIWATER 1
290 static boolean_t memorystatus_idle_snapshot
= 0;
292 static int memorystatus_highwater_enabled
= 1; /* Update the cached memlimit data. This should be removed. */
294 unsigned int memorystatus_delta
= 0;
296 static unsigned int memorystatus_available_pages_critical_base
= 0;
297 //static unsigned int memorystatus_last_foreground_pressure_pages = (unsigned int)-1;
298 static unsigned int memorystatus_available_pages_critical_idle_offset
= 0;
300 /* Jetsam Loop Detection */
301 static boolean_t memorystatus_jld_enabled
= TRUE
; /* Enables jetsam loop detection on all devices */
302 static uint32_t memorystatus_jld_eval_period_msecs
= 0; /* Init pass sets this based on device memory size */
303 static int memorystatus_jld_eval_aggressive_count
= 3; /* Raise the priority max after 'n' aggressive loops */
304 static int memorystatus_jld_eval_aggressive_priority_band_max
= 15; /* Kill aggressively up through this band */
307 * A FG app can request that the aggressive jetsam mechanism display some leniency in the FG band. This 'lenient' mode is described as:
308 * --- if aggressive jetsam kills an app in the FG band and gets back >=AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD memory, it will stop the aggressive march further into and up the jetsam bands.
311 * - Such a request is respected/acknowledged only once while that 'requesting' app is in the FG band i.e. if aggressive jetsam was
312 * needed and the 'lenient' mode was deployed then that's it for this special mode while the app is in the FG band.
314 * - If the app is still in the FG band and aggressive jetsam is needed again, there will be no stop-and-check the next time around.
316 * - Also, the transition of the 'requesting' app away from the FG band will void this special behavior.
319 #define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD 25
320 boolean_t memorystatus_aggressive_jetsam_lenient_allowed
= FALSE
;
321 boolean_t memorystatus_aggressive_jetsam_lenient
= FALSE
;
323 #if DEVELOPMENT || DEBUG
325 * Jetsam Loop Detection tunables.
328 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_jld_eval_period_msecs
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_jld_eval_period_msecs
, 0, "");
329 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_jld_eval_aggressive_count
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_jld_eval_aggressive_count
, 0, "");
330 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_jld_eval_aggressive_priority_band_max
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_jld_eval_aggressive_priority_band_max
, 0, "");
331 #endif /* DEVELOPMENT || DEBUG */
333 #if DEVELOPMENT || DEBUG
334 static unsigned int memorystatus_jetsam_panic_debug
= 0;
336 static unsigned int memorystatus_jetsam_policy
= kPolicyDefault
;
337 static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic
= 0;
338 static unsigned int memorystatus_debug_dump_this_bucket
= 0;
341 static unsigned int memorystatus_thread_wasted_wakeup
= 0;
343 static uint32_t kill_under_pressure_cause
= 0;
346 * default jetsam snapshot support
348 static memorystatus_jetsam_snapshot_t
*memorystatus_jetsam_snapshot
;
349 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries
350 static unsigned int memorystatus_jetsam_snapshot_count
= 0;
351 static unsigned int memorystatus_jetsam_snapshot_max
= 0;
352 static uint64_t memorystatus_jetsam_snapshot_last_timestamp
= 0;
353 static uint64_t memorystatus_jetsam_snapshot_timeout
= 0;
354 #define JETSAM_SNAPSHOT_TIMEOUT_SECS 30
357 * snapshot support for memstats collected at boot.
359 static memorystatus_jetsam_snapshot_t memorystatus_at_boot_snapshot
;
361 static void memorystatus_clear_errors(void);
362 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
);
363 static uint32_t memorystatus_build_state(proc_t p
);
364 static void memorystatus_update_levels_locked(boolean_t critical_only
);
365 //static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured);
367 static boolean_t
memorystatus_kill_specific_process(pid_t victim_pid
, uint32_t cause
);
368 static boolean_t
memorystatus_kill_top_process(boolean_t any
, boolean_t sort_flag
, uint32_t cause
, int32_t *priority
, uint32_t *errors
);
369 static boolean_t
memorystatus_kill_top_process_aggressive(boolean_t any
, uint32_t cause
, int aggr_count
, int32_t priority_max
, uint32_t *errors
);
371 static boolean_t
memorystatus_kill_hiwat_proc(uint32_t *errors
);
374 static boolean_t
memorystatus_kill_process_async(pid_t victim_pid
, uint32_t cause
);
375 static boolean_t
memorystatus_kill_process_sync(pid_t victim_pid
, uint32_t cause
);
377 /* Priority Band Sorting Routines */
378 static int memorystatus_sort_bucket(unsigned int bucket_index
, int sort_order
);
379 static int memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index
, int coal_sort_order
);
380 static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index
);
381 static int memorystatus_move_list_locked(unsigned int bucket_index
, pid_t
*pid_list
, int list_sz
);
384 typedef int (*cmpfunc_t
)(const void *a
, const void *b
);
385 extern void qsort(void *a
, size_t n
, size_t es
, cmpfunc_t cmp
);
386 static int memstat_asc_cmp(const void *a
, const void *b
);
388 #endif /* CONFIG_JETSAM */
392 extern unsigned int vm_page_free_count
;
393 extern unsigned int vm_page_active_count
;
394 extern unsigned int vm_page_inactive_count
;
395 extern unsigned int vm_page_throttled_count
;
396 extern unsigned int vm_page_purgeable_count
;
397 extern unsigned int vm_page_wire_count
;
399 #if VM_PRESSURE_EVENTS
401 #include "vm_pressure.h"
403 extern boolean_t
memorystatus_warn_process(pid_t pid
, boolean_t critical
);
405 vm_pressure_level_t memorystatus_vm_pressure_level
= kVMPressureNormal
;
407 #if CONFIG_MEMORYSTATUS
408 unsigned int memorystatus_available_pages
= (unsigned int)-1;
409 unsigned int memorystatus_available_pages_pressure
= 0;
410 unsigned int memorystatus_available_pages_critical
= 0;
411 unsigned int memorystatus_frozen_count
= 0;
412 unsigned int memorystatus_suspended_count
= 0;
415 * We use this flag to signal if we have any HWM offenders
416 * on the system. This way we can reduce the number of wakeups
417 * of the memorystatus_thread when the system is between the
418 * "pressure" and "critical" threshold.
420 * The (re-)setting of this variable is done without any locks
421 * or synchronization simply because it is not possible (currently)
422 * to keep track of HWM offenders that drop down below their memory
423 * limit and/or exit. So, we choose to burn a couple of wasted wakeups
424 * by allowing the unguarded modification of this variable.
426 boolean_t memorystatus_hwm_candidates
= 0;
428 static int memorystatus_send_note(int event_code
, void *data
, size_t data_length
);
429 #endif /* CONFIG_MEMORYSTATUS */
431 #endif /* VM_PRESSURE_EVENTS */
437 boolean_t memorystatus_freeze_enabled
= FALSE
;
438 int memorystatus_freeze_wakeup
= 0;
440 lck_grp_attr_t
*freezer_lck_grp_attr
;
441 lck_grp_t
*freezer_lck_grp
;
442 static lck_mtx_t freezer_mutex
;
444 static inline boolean_t
memorystatus_can_freeze_processes(void);
445 static boolean_t
memorystatus_can_freeze(boolean_t
*memorystatus_freeze_swap_low
);
447 static void memorystatus_freeze_thread(void *param __unused
, wait_result_t wr __unused
);
450 static unsigned int memorystatus_freeze_threshold
= 0;
452 static unsigned int memorystatus_freeze_pages_min
= 0;
453 static unsigned int memorystatus_freeze_pages_max
= 0;
455 static unsigned int memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
;
457 static unsigned int memorystatus_freeze_daily_mb_max
= FREEZE_DAILY_MB_MAX_DEFAULT
;
460 static uint64_t memorystatus_freeze_count
= 0;
461 static uint64_t memorystatus_freeze_pageouts
= 0;
464 static throttle_interval_t throttle_intervals
[] = {
465 { 60, 8, 0, 0, { 0, 0 }, FALSE
}, /* 1 hour intermediate interval, 8x burst */
466 { 24 * 60, 1, 0, 0, { 0, 0 }, FALSE
}, /* 24 hour long interval, no burst */
469 static uint64_t memorystatus_freeze_throttle_count
= 0;
471 static unsigned int memorystatus_suspended_footprint_total
= 0;
473 extern uint64_t vm_swap_get_free_space(void);
475 static boolean_t
memorystatus_freeze_update_throttle();
477 #endif /* CONFIG_FREEZE */
481 extern struct knote
*vm_find_knote_from_pid(pid_t
, struct klist
*);
483 #if DEVELOPMENT || DEBUG
488 memorystatus_debug_dump_bucket_locked (unsigned int bucket_index
)
492 uint32_t pages_in_mb
= 0;
493 unsigned int b
= bucket_index
;
494 boolean_t traverse_all_buckets
= FALSE
;
496 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
497 traverse_all_buckets
= TRUE
;
500 traverse_all_buckets
= FALSE
;
505 * Missing from this dump is the value actually
506 * stored in the ledger... also, format could be better.
508 printf("memorystatus_debug_dump ***START***\n");
509 printf("bucket [pid] [pages/pages-mb] state [EP / RP] dirty deadline [C-limit / A-limit / IA-limit] name\n");
510 p
= memorystatus_get_first_proc_locked(&b
, traverse_all_buckets
);
512 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
513 pages_in_mb
= (pages
* 4096) /1024 / 1024;
514 printf("%d [%d] [%d/%dMB] 0x%x [%d / %d] 0x%x %lld [%d%s / %d%s / %d%s] %s\n",
515 b
, p
->p_pid
, pages
, pages_in_mb
,
516 p
->p_memstat_state
, p
->p_memstat_effectivepriority
, p
->p_memstat_requestedpriority
, p
->p_memstat_dirty
, p
->p_memstat_idledeadline
,
517 p
->p_memstat_memlimit
,
518 (p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
? "F " : "NF"),
519 p
->p_memstat_memlimit_active
,
520 (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL
? "F " : "NF"),
521 p
->p_memstat_memlimit_inactive
,
522 (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL
? "F " : "NF"),
523 (p
->p_comm
? p
->p_comm
: "unknown"));
524 p
= memorystatus_get_next_proc_locked(&b
, p
, traverse_all_buckets
);
526 printf("memorystatus_debug_dump ***END***\n");
530 sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS
532 #pragma unused(oidp, arg2)
533 int bucket_index
= 0;
535 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
536 if (error
|| !req
->newptr
) {
539 error
= SYSCTL_IN(req
, &bucket_index
, sizeof(int));
540 if (error
|| !req
->newptr
) {
543 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
545 * All jetsam buckets will be dumped.
549 * Only a single bucket will be dumped.
554 memorystatus_debug_dump_bucket_locked(bucket_index
);
556 memorystatus_debug_dump_this_bucket
= bucket_index
;
561 * Debug aid to look at jetsam buckets and proc jetsam fields.
562 * Use this sysctl to act on a particular jetsam bucket.
563 * Writing the sysctl triggers the dump.
564 * Usage: sysctl kern.memorystatus_debug_dump_this_bucket=<bucket_index>
567 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_debug_dump_this_bucket
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_debug_dump_this_bucket
, 0, sysctl_memorystatus_debug_dump_bucket
, "I", "");
570 /* Debug aid to aid determination of limit */
573 sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS
575 #pragma unused(oidp, arg2)
578 int error
, enable
= 0;
580 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
581 if (error
|| !req
->newptr
) {
585 error
= SYSCTL_IN(req
, &enable
, sizeof(int));
586 if (error
|| !req
->newptr
) {
590 if (!(enable
== 0 || enable
== 1)) {
596 p
= memorystatus_get_first_proc_locked(&b
, TRUE
);
598 boolean_t trigger_exception
;
602 * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
603 * Background limits are described via the inactive limit slots.
606 if (proc_jetsam_state_is_active_locked(p
) == TRUE
) {
607 CACHE_ACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
609 CACHE_INACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
614 * Disabling limits does not touch the stored variants.
615 * Set the cached limit fields to system_wide defaults.
617 p
->p_memstat_memlimit
= -1;
618 p
->p_memstat_state
|= P_MEMSTAT_FATAL_MEMLIMIT
;
619 trigger_exception
= TRUE
;
623 * Enforce the cached limit by writing to the ledger.
625 task_set_phys_footprint_limit_internal(p
->task
, (p
->p_memstat_memlimit
> 0) ? p
->p_memstat_memlimit
: -1, NULL
, trigger_exception
);
627 p
= memorystatus_get_next_proc_locked(&b
, p
, TRUE
);
630 memorystatus_highwater_enabled
= enable
;
638 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_idle_snapshot
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_idle_snapshot
, 0, "");
640 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_highwater_enabled
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_highwater_enabled
, 0, sysctl_memorystatus_highwater_enable
, "I", "");
642 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_available_pages
, 0, "");
643 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_critical
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_available_pages_critical
, 0, "");
644 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_critical_base
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_available_pages_critical_base
, 0, "");
645 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_critical_idle_offset
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_available_pages_critical_idle_offset
, 0, "");
647 /* Diagnostic code */
650 kJetsamDiagnosticModeNone
= 0,
651 kJetsamDiagnosticModeAll
= 1,
652 kJetsamDiagnosticModeStopAtFirstActive
= 2,
653 kJetsamDiagnosticModeCount
654 } jetsam_diagnostic_mode
= kJetsamDiagnosticModeNone
;
656 static int jetsam_diagnostic_suspended_one_active_proc
= 0;
659 sysctl_jetsam_diagnostic_mode SYSCTL_HANDLER_ARGS
661 #pragma unused(arg1, arg2)
663 const char *diagnosticStrings
[] = {
664 "jetsam: diagnostic mode: resetting critical level.",
665 "jetsam: diagnostic mode: will examine all processes",
666 "jetsam: diagnostic mode: will stop at first active process"
669 int error
, val
= jetsam_diagnostic_mode
;
670 boolean_t changed
= FALSE
;
672 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
673 if (error
|| !req
->newptr
)
675 if ((val
< 0) || (val
>= kJetsamDiagnosticModeCount
)) {
676 printf("jetsam: diagnostic mode: invalid value - %d\n", val
);
682 if ((unsigned int) val
!= jetsam_diagnostic_mode
) {
683 jetsam_diagnostic_mode
= val
;
685 memorystatus_jetsam_policy
&= ~kPolicyDiagnoseActive
;
687 switch (jetsam_diagnostic_mode
) {
688 case kJetsamDiagnosticModeNone
:
689 /* Already cleared */
691 case kJetsamDiagnosticModeAll
:
692 memorystatus_jetsam_policy
|= kPolicyDiagnoseAll
;
694 case kJetsamDiagnosticModeStopAtFirstActive
:
695 memorystatus_jetsam_policy
|= kPolicyDiagnoseFirst
;
698 /* Already validated */
702 memorystatus_update_levels_locked(FALSE
);
709 printf("%s\n", diagnosticStrings
[val
]);
715 SYSCTL_PROC(_debug
, OID_AUTO
, jetsam_diagnostic_mode
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
|CTLFLAG_ANYBODY
,
716 &jetsam_diagnostic_mode
, 0, sysctl_jetsam_diagnostic_mode
, "I", "Jetsam Diagnostic Mode");
718 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_jetsam_policy_offset_pages_diagnostic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_jetsam_policy_offset_pages_diagnostic
, 0, "");
720 #if VM_PRESSURE_EVENTS
722 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_available_pages_pressure
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_available_pages_pressure
, 0, "");
726 * This routine is used for targeted notifications
727 * regardless of system memory pressure.
728 * "memnote" is the current user.
732 sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS
734 #pragma unused(arg1, arg2)
736 int error
= 0, pid
= 0;
738 struct knote
*kn
= NULL
;
739 boolean_t found_knote
= FALSE
;
741 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
742 if (error
|| !req
->newptr
)
746 * We inspect 3 lists here for targeted notifications:
747 * - memorystatus_klist
748 * - vm_pressure_klist
749 * - vm_pressure_dormant_klist
751 * The vm_pressure_* lists are tied to the old VM_PRESSURE
752 * notification mechanism. We intend to stop using that
753 * mechanism and, in turn, get rid of the 2 lists and
754 * vm_dispatch_pressure_note_to_pid() too.
757 memorystatus_klist_lock();
759 SLIST_FOREACH(kn
, &memorystatus_klist
, kn_selnext
) {
760 proc_t knote_proc
= kn
->kn_kq
->kq_p
;
761 pid_t knote_pid
= knote_proc
->p_pid
;
763 if (knote_pid
== pid
) {
765 * Forcibly send this pid a "warning" memory pressure notification.
767 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
773 KNOTE(&memorystatus_klist
, 0);
776 ret
= vm_dispatch_pressure_note_to_pid(pid
, FALSE
);
779 memorystatus_klist_unlock();
784 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_vm_pressure_send
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
785 0, 0, &sysctl_memorystatus_vm_pressure_send
, "I", "");
787 #endif /* VM_PRESSURE_EVENTS */
789 #endif /* CONFIG_JETSAM */
793 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_daily_mb_max
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_daily_mb_max
, 0, "");
795 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_threshold
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_threshold
, 0, "");
797 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_pages_min
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_pages_min
, 0, "");
798 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_pages_max
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_pages_max
, 0, "");
800 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_count
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_freeze_count
, "");
801 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_pageouts
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_freeze_pageouts
, "");
802 SYSCTL_QUAD(_kern
, OID_AUTO
, memorystatus_freeze_throttle_count
, CTLFLAG_RD
|CTLFLAG_LOCKED
, &memorystatus_freeze_throttle_count
, "");
803 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_min_processes
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_suspended_threshold
, 0, "");
805 boolean_t memorystatus_freeze_throttle_enabled
= TRUE
;
806 SYSCTL_UINT(_kern
, OID_AUTO
, memorystatus_freeze_throttle_enabled
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_freeze_throttle_enabled
, 0, "");
809 * Manual trigger of freeze and thaw for dev / debug kernels only.
812 sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS
814 #pragma unused(arg1, arg2)
818 if (memorystatus_freeze_enabled
== FALSE
) {
822 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
823 if (error
|| !req
->newptr
)
827 vm_pageout_anonymous_pages();
832 lck_mtx_lock(&freezer_mutex
);
836 uint32_t purgeable
, wired
, clean
, dirty
;
838 uint32_t max_pages
= 0;
840 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
842 unsigned int avail_swap_space
= 0; /* in pages. */
844 if (DEFAULT_FREEZER_IS_ACTIVE
) {
846 * Freezer backed by default pager and swap file(s).
848 avail_swap_space
= default_pager_swap_pages_free();
851 * Freezer backed by the compressor and swap file(s)
852 * while will hold compressed data.
854 avail_swap_space
= vm_swap_get_free_space() / PAGE_SIZE_64
;
857 max_pages
= MIN(avail_swap_space
, memorystatus_freeze_pages_max
);
861 * We only have the compressor without any swap.
863 max_pages
= UINT32_MAX
- 1;
866 error
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, FALSE
);
872 lck_mtx_unlock(&freezer_mutex
);
876 lck_mtx_unlock(&freezer_mutex
);
880 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_freeze
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
881 0, 0, &sysctl_memorystatus_freeze
, "I", "");
884 sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS
886 #pragma unused(arg1, arg2)
891 if (memorystatus_freeze_enabled
== FALSE
) {
895 error
= sysctl_handle_int(oidp
, &pid
, 0, req
);
896 if (error
|| !req
->newptr
)
901 error
= task_thaw(p
->task
);
912 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_thaw
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
913 0, 0, &sysctl_memorystatus_available_pages_thaw
, "I", "");
915 #endif /* CONFIG_FREEZE */
917 #endif /* DEVELOPMENT || DEBUG */
919 extern kern_return_t
kernel_thread_start_priority(thread_continue_t continuation
,
922 thread_t
*new_thread
);
926 * Picks the sorting routine for a given jetsam priority band.
929 * bucket_index - jetsam priority band to be sorted.
930 * sort_order - JETSAM_SORT_xxx from kern_memorystatus.h
931 * Currently sort_order is only meaningful when handling
938 static int memorystatus_sort_bucket(unsigned int bucket_index
, int sort_order
)
943 * Verify the jetsam priority
945 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
949 #if DEVELOPMENT || DEBUG
950 if (sort_order
== JETSAM_SORT_DEFAULT
) {
951 coal_sort_order
= COALITION_SORT_DEFAULT
;
953 coal_sort_order
= sort_order
; /* only used for testing scenarios */
957 if (sort_order
== JETSAM_SORT_DEFAULT
) {
958 coal_sort_order
= COALITION_SORT_DEFAULT
;
965 switch (bucket_index
) {
966 case JETSAM_PRIORITY_FOREGROUND
:
967 if (memorystatus_sort_by_largest_coalition_locked(bucket_index
, coal_sort_order
) == 0) {
969 * Fall back to per process sorting when zero coalitions are found.
971 memorystatus_sort_by_largest_process_locked(bucket_index
);
975 memorystatus_sort_by_largest_process_locked(bucket_index
);
984 * Sort processes by size for a single jetsam bucket.
987 static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index
)
989 proc_t p
= NULL
, insert_after_proc
= NULL
, max_proc
= NULL
;
990 proc_t next_p
= NULL
, prev_max_proc
= NULL
;
991 uint32_t pages
= 0, max_pages
= 0;
992 memstat_bucket_t
*current_bucket
;
994 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
998 current_bucket
= &memstat_bucket
[bucket_index
];
1000 p
= TAILQ_FIRST(¤t_bucket
->list
);
1003 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
1008 while ((next_p
= TAILQ_NEXT(p
, p_memstat_list
)) != NULL
) {
1009 /* traversing list until we find next largest process */
1011 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
1012 if (pages
> max_pages
) {
1018 if (prev_max_proc
!= max_proc
) {
1019 /* found a larger process, place it in the list */
1020 TAILQ_REMOVE(¤t_bucket
->list
, max_proc
, p_memstat_list
);
1021 if (insert_after_proc
== NULL
) {
1022 TAILQ_INSERT_HEAD(¤t_bucket
->list
, max_proc
, p_memstat_list
);
1024 TAILQ_INSERT_AFTER(¤t_bucket
->list
, insert_after_proc
, max_proc
, p_memstat_list
);
1026 prev_max_proc
= max_proc
;
1029 insert_after_proc
= max_proc
;
1031 p
= TAILQ_NEXT(max_proc
, p_memstat_list
);
1035 #endif /* CONFIG_JETSAM */
1037 static proc_t
memorystatus_get_first_proc_locked(unsigned int *bucket_index
, boolean_t search
) {
1038 memstat_bucket_t
*current_bucket
;
1041 if ((*bucket_index
) >= MEMSTAT_BUCKET_COUNT
) {
1045 current_bucket
= &memstat_bucket
[*bucket_index
];
1046 next_p
= TAILQ_FIRST(¤t_bucket
->list
);
1047 if (!next_p
&& search
) {
1048 while (!next_p
&& (++(*bucket_index
) < MEMSTAT_BUCKET_COUNT
)) {
1049 current_bucket
= &memstat_bucket
[*bucket_index
];
1050 next_p
= TAILQ_FIRST(¤t_bucket
->list
);
1057 static proc_t
memorystatus_get_next_proc_locked(unsigned int *bucket_index
, proc_t p
, boolean_t search
) {
1058 memstat_bucket_t
*current_bucket
;
1061 if (!p
|| ((*bucket_index
) >= MEMSTAT_BUCKET_COUNT
)) {
1065 next_p
= TAILQ_NEXT(p
, p_memstat_list
);
1066 while (!next_p
&& search
&& (++(*bucket_index
) < MEMSTAT_BUCKET_COUNT
)) {
1067 current_bucket
= &memstat_bucket
[*bucket_index
];
1068 next_p
= TAILQ_FIRST(¤t_bucket
->list
);
1074 __private_extern__
void
1075 memorystatus_init(void)
1077 thread_t thread
= THREAD_NULL
;
1078 kern_return_t result
;
1082 memorystatus_freeze_pages_min
= FREEZE_PAGES_MIN
;
1083 memorystatus_freeze_pages_max
= FREEZE_PAGES_MAX
;
1086 nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS
* NSEC_PER_SEC
, &memorystatus_idle_delay_time
);
1089 for (i
= 0; i
< MEMSTAT_BUCKET_COUNT
; i
++) {
1090 TAILQ_INIT(&memstat_bucket
[i
].list
);
1091 memstat_bucket
[i
].count
= 0;
1094 memorystatus_idle_demotion_call
= thread_call_allocate((thread_call_func_t
)memorystatus_perform_idle_demotion
, NULL
);
1096 /* Apply overrides */
1097 PE_get_default("kern.jetsam_delta", &delta_percentage
, sizeof(delta_percentage
));
1098 assert(delta_percentage
< 100);
1099 PE_get_default("kern.jetsam_critical_threshold", &critical_threshold_percentage
, sizeof(critical_threshold_percentage
));
1100 assert(critical_threshold_percentage
< 100);
1101 PE_get_default("kern.jetsam_idle_offset", &idle_offset_percentage
, sizeof(idle_offset_percentage
));
1102 assert(idle_offset_percentage
< 100);
1103 PE_get_default("kern.jetsam_pressure_threshold", &pressure_threshold_percentage
, sizeof(pressure_threshold_percentage
));
1104 assert(pressure_threshold_percentage
< 100);
1105 PE_get_default("kern.jetsam_freeze_threshold", &freeze_threshold_percentage
, sizeof(freeze_threshold_percentage
));
1106 assert(freeze_threshold_percentage
< 100);
1109 /* device tree can request to take snapshots for idle-exit kills by default */
1110 PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot
, sizeof(memorystatus_idle_snapshot
));
1112 memorystatus_delta
= delta_percentage
* atop_64(max_mem
) / 100;
1113 memorystatus_available_pages_critical_idle_offset
= idle_offset_percentage
* atop_64(max_mem
) / 100;
1114 memorystatus_available_pages_critical_base
= (critical_threshold_percentage
/ delta_percentage
) * memorystatus_delta
;
1116 memorystatus_jetsam_snapshot_max
= maxproc
;
1117 memorystatus_jetsam_snapshot
=
1118 (memorystatus_jetsam_snapshot_t
*)kalloc(sizeof(memorystatus_jetsam_snapshot_t
) +
1119 sizeof(memorystatus_jetsam_snapshot_entry_t
) * memorystatus_jetsam_snapshot_max
);
1120 if (!memorystatus_jetsam_snapshot
) {
1121 panic("Could not allocate memorystatus_jetsam_snapshot");
1124 nanoseconds_to_absolutetime((uint64_t)JETSAM_SNAPSHOT_TIMEOUT_SECS
* NSEC_PER_SEC
, &memorystatus_jetsam_snapshot_timeout
);
1126 memset(&memorystatus_at_boot_snapshot
, 0, sizeof(memorystatus_jetsam_snapshot_t
));
1128 /* No contention at this point */
1129 memorystatus_update_levels_locked(FALSE
);
1131 /* Jetsam Loop Detection */
1132 if (max_mem
<= (512 * 1024 * 1024)) {
1133 /* 512 MB devices */
1134 memorystatus_jld_eval_period_msecs
= 8000; /* 8000 msecs == 8 second window */
1136 /* 1GB and larger devices */
1137 memorystatus_jld_eval_period_msecs
= 6000; /* 6000 msecs == 6 second window */
1142 memorystatus_freeze_threshold
= (freeze_threshold_percentage
/ delta_percentage
) * memorystatus_delta
;
1145 result
= kernel_thread_start_priority(memorystatus_thread
, NULL
, 95 /* MAXPRI_KERNEL */, &thread
);
1146 if (result
== KERN_SUCCESS
) {
1147 thread_deallocate(thread
);
1149 panic("Could not create memorystatus_thread");
1153 /* Centralised for the purposes of allowing panic-on-jetsam */
1155 vm_wake_compactor_swapper(void);
1158 * The jetsam no frills kill call
1159 * Return: 0 on success
1160 * error code on failure (EINVAL...)
1163 jetsam_do_kill(proc_t p
, int jetsam_flags
) {
1165 error
= exit1_internal(p
, W_EXITCODE(0, SIGKILL
), (int *)NULL
, FALSE
, FALSE
, jetsam_flags
);
1170 * Wrapper for processes exiting with memorystatus details
1173 memorystatus_do_kill(proc_t p
, uint32_t cause
) {
1176 __unused pid_t victim_pid
= p
->p_pid
;
1178 KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DO_KILL
)) | DBG_FUNC_START
,
1179 victim_pid
, cause
, vm_page_free_count
, 0, 0);
1181 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
1182 if (memorystatus_jetsam_panic_debug
& (1 << cause
)) {
1183 panic("memorystatus_do_kill(): jetsam debug panic (cause: %d)", cause
);
1186 #pragma unused(cause)
1188 int jetsam_flags
= P_LTERM_JETSAM
;
1190 case kMemorystatusKilledHiwat
: jetsam_flags
|= P_JETSAM_HIWAT
; break;
1191 case kMemorystatusKilledVnodes
: jetsam_flags
|= P_JETSAM_VNODE
; break;
1192 case kMemorystatusKilledVMPageShortage
: jetsam_flags
|= P_JETSAM_VMPAGESHORTAGE
; break;
1193 case kMemorystatusKilledVMThrashing
: jetsam_flags
|= P_JETSAM_VMTHRASHING
; break;
1194 case kMemorystatusKilledFCThrashing
: jetsam_flags
|= P_JETSAM_FCTHRASHING
; break;
1195 case kMemorystatusKilledPerProcessLimit
: jetsam_flags
|= P_JETSAM_PID
; break;
1196 case kMemorystatusKilledIdleExit
: jetsam_flags
|= P_JETSAM_IDLEEXIT
; break;
1198 error
= jetsam_do_kill(p
, jetsam_flags
);
1200 KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DO_KILL
)) | DBG_FUNC_END
,
1201 victim_pid
, cause
, vm_page_free_count
, error
, 0);
1203 vm_wake_compactor_swapper();
1205 return (error
== 0);
1213 memorystatus_check_levels_locked(void) {
1216 memorystatus_update_levels_locked(TRUE
);
1221 memorystatus_perform_idle_demotion(__unused
void *spare1
, __unused
void *spare2
)
1224 uint64_t current_time
;
1225 memstat_bucket_t
*demotion_bucket
;
1227 MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion()\n");
1229 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_IDLE_DEMOTE
) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
1231 current_time
= mach_absolute_time();
1235 demotion_bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE_DEFERRED
];
1236 p
= TAILQ_FIRST(&demotion_bucket
->list
);
1239 MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion() found %d\n", p
->p_pid
);
1241 assert(p
->p_memstat_idledeadline
);
1242 assert(p
->p_memstat_dirty
& P_DIRTY_DEFER_IN_PROGRESS
);
1243 assert((p
->p_memstat_dirty
& (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_IS_DIRTY
)) == P_DIRTY_IDLE_EXIT_ENABLED
);
1245 if (current_time
>= p
->p_memstat_idledeadline
) {
1246 #if DEBUG || DEVELOPMENT
1247 if (!(p
->p_memstat_dirty
& P_DIRTY_MARKED
)) {
1248 printf("memorystatus_perform_idle_demotion: moving process %d [%s] to idle band, but never dirtied (0x%x)!\n",
1249 p
->p_pid
, (p
->p_comm
? p
->p_comm
: "(unknown)"), p
->p_memstat_dirty
);
1252 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1253 memorystatus_update_priority_locked(p
, JETSAM_PRIORITY_IDLE
, false);
1255 // The prior process has moved out of the demotion bucket, so grab the new head and continue
1256 p
= TAILQ_FIRST(&demotion_bucket
->list
);
1260 // No further candidates
1264 memorystatus_reschedule_idle_demotion_locked();
1268 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_IDLE_DEMOTE
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
1272 memorystatus_schedule_idle_demotion_locked(proc_t p
, boolean_t set_state
)
1274 boolean_t present_in_deferred_bucket
= FALSE
;
1276 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1277 present_in_deferred_bucket
= TRUE
;
1280 MEMORYSTATUS_DEBUG(1, "memorystatus_schedule_idle_demotion_locked: scheduling demotion to idle band for pid %d (dirty:0x%x, set_state %d, demotions %d).\n",
1281 p
->p_pid
, p
->p_memstat_dirty
, set_state
, memorystatus_scheduled_idle_demotions
);
1283 assert((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
);
1286 assert(p
->p_memstat_idledeadline
== 0);
1287 p
->p_memstat_dirty
|= P_DIRTY_DEFER_IN_PROGRESS
;
1288 p
->p_memstat_idledeadline
= mach_absolute_time() + memorystatus_idle_delay_time
;
1291 assert(p
->p_memstat_idledeadline
);
1293 if (present_in_deferred_bucket
== FALSE
) {
1294 memorystatus_scheduled_idle_demotions
++;
1299 memorystatus_invalidate_idle_demotion_locked(proc_t p
, boolean_t clear_state
)
1301 boolean_t present_in_deferred_bucket
= FALSE
;
1303 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1304 present_in_deferred_bucket
= TRUE
;
1305 assert(p
->p_memstat_idledeadline
);
1308 MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for pid %d (clear_state %d, demotions %d).\n",
1309 p
->p_pid
, clear_state
, memorystatus_scheduled_idle_demotions
);
1313 p
->p_memstat_idledeadline
= 0;
1314 p
->p_memstat_dirty
&= ~P_DIRTY_DEFER_IN_PROGRESS
;
1317 if (present_in_deferred_bucket
== TRUE
) {
1318 memorystatus_scheduled_idle_demotions
--;
1321 assert(memorystatus_scheduled_idle_demotions
>= 0);
1325 memorystatus_reschedule_idle_demotion_locked(void) {
1326 if (0 == memorystatus_scheduled_idle_demotions
) {
1327 if (memstat_idle_demotion_deadline
) {
1328 /* Transitioned 1->0, so cancel next call */
1329 thread_call_cancel(memorystatus_idle_demotion_call
);
1330 memstat_idle_demotion_deadline
= 0;
1333 memstat_bucket_t
*demotion_bucket
;
1335 demotion_bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE_DEFERRED
];
1336 p
= TAILQ_FIRST(&demotion_bucket
->list
);
1338 assert(p
&& p
->p_memstat_idledeadline
);
1340 if (memstat_idle_demotion_deadline
!= p
->p_memstat_idledeadline
){
1341 thread_call_enter_delayed(memorystatus_idle_demotion_call
, p
->p_memstat_idledeadline
);
1342 memstat_idle_demotion_deadline
= p
->p_memstat_idledeadline
;
1352 memorystatus_add(proc_t p
, boolean_t locked
)
1354 memstat_bucket_t
*bucket
;
1356 MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding pid %d with priority %d.\n", p
->p_pid
, p
->p_memstat_effectivepriority
);
1362 /* Processes marked internal do not have priority tracked */
1363 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
1367 bucket
= &memstat_bucket
[p
->p_memstat_effectivepriority
];
1369 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1370 assert(bucket
->count
== memorystatus_scheduled_idle_demotions
);
1373 TAILQ_INSERT_TAIL(&bucket
->list
, p
, p_memstat_list
);
1376 memorystatus_list_count
++;
1378 memorystatus_check_levels_locked();
1390 * Moves a process from one jetsam bucket to another.
1391 * which changes the LRU position of the process.
1393 * Monitors transition between buckets and if necessary
1394 * will update cached memory limits accordingly.
1397 memorystatus_update_priority_locked(proc_t p
, int priority
, boolean_t head_insert
)
1399 memstat_bucket_t
*old_bucket
, *new_bucket
;
1401 assert(priority
< MEMSTAT_BUCKET_COUNT
);
1403 /* Ensure that exit isn't underway, leaving the proc retained but removed from its bucket */
1404 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1408 MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting pid %d to priority %d, inserting at %s\n",
1409 p
->p_pid
, priority
, head_insert
? "head" : "tail");
1411 old_bucket
= &memstat_bucket
[p
->p_memstat_effectivepriority
];
1412 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1413 assert(old_bucket
->count
== (memorystatus_scheduled_idle_demotions
+ 1));
1416 TAILQ_REMOVE(&old_bucket
->list
, p
, p_memstat_list
);
1417 old_bucket
->count
--;
1419 new_bucket
= &memstat_bucket
[priority
];
1421 TAILQ_INSERT_HEAD(&new_bucket
->list
, p
, p_memstat_list
);
1423 TAILQ_INSERT_TAIL(&new_bucket
->list
, p
, p_memstat_list
);
1424 new_bucket
->count
++;
1427 if (memorystatus_highwater_enabled
) {
1428 boolean_t trigger_exception
;
1431 * If cached limit data is updated, then the limits
1432 * will be enforced by writing to the ledgers.
1434 boolean_t ledger_update_needed
= TRUE
;
1437 * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
1438 * Background limits are described via the inactive limit slots.
1440 * Here, we must update the cached memory limit if the task
1441 * is transitioning between:
1442 * active <--> inactive
1445 * dirty <--> clean is ignored
1447 * We bypass processes that have opted into dirty tracking because
1448 * a move between buckets does not imply a transition between the
1449 * dirty <--> clean state.
1450 * Setting limits on processes opted into dirty tracking is handled
1451 * in memorystatus_dirty_set() where the transition is very clear.
1454 if (p
->p_memstat_dirty
& P_DIRTY_TRACK
) {
1456 ledger_update_needed
= FALSE
;
1458 } else if ((priority
>= JETSAM_PRIORITY_FOREGROUND
) && (p
->p_memstat_effectivepriority
< JETSAM_PRIORITY_FOREGROUND
)) {
1460 * inactive --> active
1462 * assign active state
1464 CACHE_ACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
1466 } else if ((priority
< JETSAM_PRIORITY_FOREGROUND
) && (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
)) {
1468 * active --> inactive
1470 * assign inactive state
1472 CACHE_INACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
1475 * The transition between jetsam priority buckets apparently did
1476 * not affect active/inactive state.
1477 * This is not unusual... especially during startup when
1478 * processes are getting established in their respective bands.
1480 ledger_update_needed
= FALSE
;
1484 * Enforce the new limits by writing to the ledger
1486 if (ledger_update_needed
) {
1487 task_set_phys_footprint_limit_internal(p
->task
, (p
->p_memstat_memlimit
> 0) ? p
->p_memstat_memlimit
: -1, NULL
, trigger_exception
);
1489 MEMORYSTATUS_DEBUG(3, "memorystatus_update_priority_locked: new limit on pid %d (%dMB %s) priority old --> new (%d --> %d) dirty?=0x%x %s\n",
1490 p
->p_pid
, (p
->p_memstat_memlimit
> 0 ? p
->p_memstat_memlimit
: -1),
1491 (p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
? "F " : "NF"), p
->p_memstat_effectivepriority
, priority
, p
->p_memstat_dirty
,
1492 (p
->p_memstat_dirty
? ((p
->p_memstat_dirty
& P_DIRTY
) ? "isdirty" : "isclean") : ""));
1496 #endif /* CONFIG_JETSAM */
1498 p
->p_memstat_effectivepriority
= priority
;
1500 memorystatus_check_levels_locked();
1505 * Description: Update the jetsam priority and memory limit attributes for a given process.
1508 * p init this process's jetsam information.
1509 * priority The jetsam priority band
1510 * user_data user specific data, unused by the kernel
1511 * effective guards against race if process's update already occurred
1512 * update_memlimit When true we know this is the init step via the posix_spawn path.
1514 * memlimit_active Value in megabytes; The monitored footprint level while the
1515 * process is active. Exceeding it may result in termination
1516 * based on it's associated fatal flag.
1518 * memlimit_active_is_fatal When a process is active and exceeds its memory footprint,
1519 * this describes whether or not it should be immediately fatal.
1521 * memlimit_inactive Value in megabytes; The monitored footprint level while the
1522 * process is inactive. Exceeding it may result in termination
1523 * based on it's associated fatal flag.
1525 * memlimit_inactive_is_fatal When a process is inactive and exceeds its memory footprint,
1526 * this describes whether or not it should be immediatly fatal.
1528 * memlimit_background This process has a high-water-mark while in the background.
1529 * No longer meaningful. Background limits are described via
1530 * the inactive slots. Flag is ignored.
1533 * Returns: 0 Success
1538 memorystatus_update(proc_t p
, int priority
, uint64_t user_data
, boolean_t effective
, boolean_t update_memlimit
,
1539 int32_t memlimit_active
, boolean_t memlimit_active_is_fatal
,
1540 int32_t memlimit_inactive
, boolean_t memlimit_inactive_is_fatal
,
1541 __unused boolean_t memlimit_background
)
1544 boolean_t head_insert
= false;
1547 #pragma unused(update_memlimit, memlimit_active, memlimit_inactive)
1548 #pragma unused(memlimit_active_is_fatal, memlimit_inactive_is_fatal)
1549 #endif /* !CONFIG_JETSAM */
1551 MEMORYSTATUS_DEBUG(1, "memorystatus_update: changing pid %d: priority %d, user_data 0x%llx\n", p
->p_pid
, priority
, user_data
);
1553 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_UPDATE
) | DBG_FUNC_START
, p
->p_pid
, priority
, user_data
, effective
, 0);
1555 if (priority
== -1) {
1556 /* Use as shorthand for default priority */
1557 priority
= JETSAM_PRIORITY_DEFAULT
;
1558 } else if (priority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1559 /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */
1560 priority
= JETSAM_PRIORITY_IDLE
;
1561 } else if (priority
== JETSAM_PRIORITY_IDLE_HEAD
) {
1562 /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */
1563 priority
= JETSAM_PRIORITY_IDLE
;
1565 } else if ((priority
< 0) || (priority
>= MEMSTAT_BUCKET_COUNT
)) {
1573 assert(!(p
->p_memstat_state
& P_MEMSTAT_INTERNAL
));
1575 if (effective
&& (p
->p_memstat_state
& P_MEMSTAT_PRIORITYUPDATED
)) {
1578 MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", p
->p_pid
);
1582 if ((p
->p_memstat_state
& P_MEMSTAT_TERMINATED
) || ((p
->p_listflag
& P_LIST_EXITED
) != 0)) {
1584 * This could happen when a process calling posix_spawn() is exiting on the jetsam thread.
1591 p
->p_memstat_state
|= P_MEMSTAT_PRIORITYUPDATED
;
1592 p
->p_memstat_userdata
= user_data
;
1593 p
->p_memstat_requestedpriority
= priority
;
1596 if (update_memlimit
) {
1597 boolean_t trigger_exception
;
1600 * Posix_spawn'd processes come through this path to instantiate ledger limits.
1601 * Forked processes do not come through this path, so no ledger limits exist.
1602 * (That's why forked processes can consume unlimited memory.)
1605 MEMORYSTATUS_DEBUG(3, "memorystatus_update(enter): pid %d, priority %d, dirty=0x%x, Active(%dMB %s), Inactive(%dMB, %s)\n",
1606 p
->p_pid
, priority
, p
->p_memstat_dirty
,
1607 memlimit_active
, (memlimit_active_is_fatal
? "F " : "NF"),
1608 memlimit_inactive
, (memlimit_inactive_is_fatal
? "F " : "NF"));
1610 if (memlimit_background
) {
1613 * With 2-level HWM support, we no longer honor P_MEMSTAT_MEMLIMIT_BACKGROUND.
1614 * Background limits are described via the inactive limit slots.
1617 // p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_BACKGROUND;
1619 #if DEVELOPMENT || DEBUG
1620 printf("memorystatus_update: WARNING %s[%d] set unused flag P_MEMSTAT_MEMLIMIT_BACKGROUND [A==%dMB %s] [IA==%dMB %s]\n",
1621 (p
->p_comm
? p
->p_comm
: "unknown"), p
->p_pid
,
1622 memlimit_active
, (memlimit_active_is_fatal
? "F " : "NF"),
1623 memlimit_inactive
, (memlimit_inactive_is_fatal
? "F " : "NF"));
1624 #endif /* DEVELOPMENT || DEBUG */
1627 if (memlimit_active
<= 0) {
1629 * This process will have a system_wide task limit when active.
1630 * System_wide task limit is always fatal.
1631 * It's quite common to see non-fatal flag passed in here.
1632 * It's not an error, we just ignore it.
1636 * For backward compatibility with some unexplained launchd behavior,
1637 * we allow a zero sized limit. But we still enforce system_wide limit
1638 * when written to the ledgers.
1641 if (memlimit_active
< 0) {
1642 memlimit_active
= -1; /* enforces system_wide task limit */
1644 memlimit_active_is_fatal
= TRUE
;
1647 if (memlimit_inactive
<= 0) {
1649 * This process will have a system_wide task limit when inactive.
1650 * System_wide task limit is always fatal.
1653 memlimit_inactive
= -1;
1654 memlimit_inactive_is_fatal
= TRUE
;
1658 * Initialize the active limit variants for this process.
1660 SET_ACTIVE_LIMITS_LOCKED(p
, memlimit_active
, memlimit_active_is_fatal
);
1663 * Initialize the inactive limit variants for this process.
1665 SET_INACTIVE_LIMITS_LOCKED(p
, memlimit_inactive
, memlimit_inactive_is_fatal
);
1668 * Initialize the cached limits for target process.
1669 * When the target process is dirty tracked, it's typically
1670 * in a clean state. Non dirty tracked processes are
1671 * typically active (Foreground or above).
1672 * But just in case, we don't make assumptions...
1675 if (proc_jetsam_state_is_active_locked(p
) == TRUE
) {
1676 CACHE_ACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
1678 CACHE_INACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
1682 * Enforce the cached limit by writing to the ledger.
1684 if (memorystatus_highwater_enabled
) {
1686 assert(trigger_exception
== TRUE
);
1687 task_set_phys_footprint_limit_internal(p
->task
, ((p
->p_memstat_memlimit
> 0) ? p
->p_memstat_memlimit
: -1), NULL
, trigger_exception
);
1689 MEMORYSTATUS_DEBUG(3, "memorystatus_update: init: limit on pid %d (%dMB %s) targeting priority(%d) dirty?=0x%x %s\n",
1690 p
->p_pid
, (p
->p_memstat_memlimit
> 0 ? p
->p_memstat_memlimit
: -1),
1691 (p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
? "F " : "NF"), priority
, p
->p_memstat_dirty
,
1692 (p
->p_memstat_dirty
? ((p
->p_memstat_dirty
& P_DIRTY
) ? "isdirty" : "isclean") : ""));
1695 #endif /* CONFIG_JETSAM */
1698 * We can't add to the JETSAM_PRIORITY_IDLE_DEFERRED bucket here.
1699 * But, we could be removing it from the bucket.
1700 * Check and take appropriate steps if so.
1703 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1705 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1708 memorystatus_update_priority_locked(p
, priority
, head_insert
);
1714 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_UPDATE
) | DBG_FUNC_END
, ret
, 0, 0, 0, 0);
1720 memorystatus_remove(proc_t p
, boolean_t locked
)
1723 memstat_bucket_t
*bucket
;
1725 MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing pid %d\n", p
->p_pid
);
1731 assert(!(p
->p_memstat_state
& P_MEMSTAT_INTERNAL
));
1733 bucket
= &memstat_bucket
[p
->p_memstat_effectivepriority
];
1734 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1735 assert(bucket
->count
== memorystatus_scheduled_idle_demotions
);
1738 TAILQ_REMOVE(&bucket
->list
, p
, p_memstat_list
);
1741 memorystatus_list_count
--;
1743 /* If awaiting demotion to the idle band, clean up */
1744 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
1745 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1746 memorystatus_reschedule_idle_demotion_locked();
1749 memorystatus_check_levels_locked();
1752 if (p
->p_memstat_state
& (P_MEMSTAT_FROZEN
)) {
1753 memorystatus_frozen_count
--;
1756 if (p
->p_memstat_state
& P_MEMSTAT_SUSPENDED
) {
1757 memorystatus_suspended_footprint_total
-= p
->p_memstat_suspendedfootprint
;
1758 memorystatus_suspended_count
--;
1776 * Validate dirty tracking flags with process state.
1784 memorystatus_validate_track_flags(struct proc
*target_p
, uint32_t pcontrol
) {
1785 /* See that the process isn't marked for termination */
1786 if (target_p
->p_memstat_dirty
& P_DIRTY_TERMINATED
) {
1790 /* Idle exit requires that process be tracked */
1791 if ((pcontrol
& PROC_DIRTY_ALLOW_IDLE_EXIT
) &&
1792 !(pcontrol
& PROC_DIRTY_TRACK
)) {
1796 /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */
1797 if ((pcontrol
& PROC_DIRTY_LAUNCH_IN_PROGRESS
) &&
1798 !(pcontrol
& PROC_DIRTY_TRACK
)) {
1802 /* Deferral is only relevant if idle exit is specified */
1803 if ((pcontrol
& PROC_DIRTY_DEFER
) &&
1804 !(pcontrol
& PROC_DIRTY_ALLOWS_IDLE_EXIT
)) {
1812 memorystatus_update_idle_priority_locked(proc_t p
) {
1815 MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p
->p_pid
, p
->p_memstat_dirty
);
1817 if ((p
->p_memstat_dirty
& (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_IS_DIRTY
)) == P_DIRTY_IDLE_EXIT_ENABLED
) {
1818 priority
= (p
->p_memstat_dirty
& P_DIRTY_DEFER_IN_PROGRESS
) ? JETSAM_PRIORITY_IDLE_DEFERRED
: JETSAM_PRIORITY_IDLE
;
1820 priority
= p
->p_memstat_requestedpriority
;
1823 if (priority
!= p
->p_memstat_effectivepriority
) {
1824 memorystatus_update_priority_locked(p
, priority
, false);
1829 * Processes can opt to have their state tracked by the kernel, indicating when they are busy (dirty) or idle
1830 * (clean). They may also indicate that they support termination when idle, with the result that they are promoted
1831 * to their desired, higher, jetsam priority when dirty (and are therefore killed later), and demoted to the low
1832 * priority idle band when clean (and killed earlier, protecting higher priority procesess).
1834 * If the deferral flag is set, then newly tracked processes will be protected for an initial period (as determined by
1835 * memorystatus_idle_delay_time); if they go clean during this time, then they will be moved to a deferred-idle band
1836 * with a slightly higher priority, guarding against immediate termination under memory pressure and being unable to
1837 * make forward progress. Finally, when the guard expires, they will be moved to the standard, lowest-priority, idle
1838 * band. The deferral can be cleared early by clearing the appropriate flag.
1840 * The deferral timer is active only for the duration that the process is marked as guarded and clean; if the process
1841 * is marked dirty, the timer will be cancelled. Upon being subsequently marked clean, the deferment will either be
1842 * re-enabled or the guard state cleared, depending on whether the guard deadline has passed.
1846 memorystatus_dirty_track(proc_t p
, uint32_t pcontrol
) {
1847 unsigned int old_dirty
;
1848 boolean_t reschedule
= FALSE
;
1849 boolean_t already_deferred
= FALSE
;
1850 boolean_t defer_now
= FALSE
;
1853 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DIRTY_TRACK
),
1854 p
->p_pid
, p
->p_memstat_dirty
, pcontrol
, 0, 0);
1858 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1860 * Process is on its way out.
1866 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
1871 if ((ret
= memorystatus_validate_track_flags(p
, pcontrol
)) != 0) {
1876 old_dirty
= p
->p_memstat_dirty
;
1878 /* These bits are cumulative, as per <rdar://problem/11159924> */
1879 if (pcontrol
& PROC_DIRTY_TRACK
) {
1880 p
->p_memstat_dirty
|= P_DIRTY_TRACK
;
1883 if (pcontrol
& PROC_DIRTY_ALLOW_IDLE_EXIT
) {
1884 p
->p_memstat_dirty
|= P_DIRTY_ALLOW_IDLE_EXIT
;
1887 if (pcontrol
& PROC_DIRTY_LAUNCH_IN_PROGRESS
) {
1888 p
->p_memstat_dirty
|= P_DIRTY_LAUNCH_IN_PROGRESS
;
1891 if (old_dirty
& P_DIRTY_DEFER_IN_PROGRESS
) {
1892 already_deferred
= TRUE
;
1895 /* This can be set and cleared exactly once. */
1896 if (pcontrol
& PROC_DIRTY_DEFER
) {
1898 if ( !(old_dirty
& P_DIRTY_DEFER
)) {
1899 p
->p_memstat_dirty
|= P_DIRTY_DEFER
;
1905 MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for pid %d\n",
1906 ((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
) ? "Y" : "N",
1907 defer_now
? "Y" : "N",
1908 p
->p_memstat_dirty
& P_DIRTY
? "Y" : "N",
1911 /* Kick off or invalidate the idle exit deferment if there's a state transition. */
1912 if (!(p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
)) {
1913 if (((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
) &&
1914 defer_now
&& !already_deferred
) {
1917 * Request to defer a clean process that's idle-exit enabled
1918 * and not already in the jetsam deferred band.
1920 memorystatus_schedule_idle_demotion_locked(p
, TRUE
);
1923 } else if (!defer_now
&& already_deferred
) {
1926 * Either the process is no longer idle-exit enabled OR
1927 * there's a request to cancel a currently active deferral.
1929 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1935 * We are trying to operate on a dirty process. Dirty processes have to
1936 * be removed from the deferred band. The question is do we reset the
1937 * deferred state or not?
1939 * This could be a legal request like:
1940 * - this process had opted into the JETSAM_DEFERRED band
1941 * - but it's now dirty and requests to opt out.
1942 * In this case, we remove the process from the band and reset its
1943 * state too. It'll opt back in properly when needed.
1945 * OR, this request could be a user-space bug. E.g.:
1946 * - this process had opted into the JETSAM_DEFERRED band when clean
1947 * - and, then issues another request to again put it into the band except
1948 * this time the process is dirty.
1949 * The process going dirty, as a transition in memorystatus_dirty_set(), will pull the process out of
1950 * the deferred band with its state intact. So our request below is no-op.
1951 * But we do it here anyways for coverage.
1953 * memorystatus_update_idle_priority_locked()
1954 * single-mindedly treats a dirty process as "cannot be in the deferred band".
1957 if (!defer_now
&& already_deferred
) {
1958 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
1961 memorystatus_invalidate_idle_demotion_locked(p
, FALSE
);
1966 memorystatus_update_idle_priority_locked(p
);
1969 memorystatus_reschedule_idle_demotion_locked();
1981 memorystatus_dirty_set(proc_t p
, boolean_t self
, uint32_t pcontrol
) {
1983 boolean_t kill
= false;
1984 boolean_t reschedule
= FALSE
;
1985 boolean_t was_dirty
= FALSE
;
1986 boolean_t now_dirty
= FALSE
;
1988 MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self
, p
->p_pid
, pcontrol
, p
->p_memstat_dirty
);
1990 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DIRTY_SET
), p
->p_pid
, self
, pcontrol
, 0, 0);
1994 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
1996 * Process is on its way out.
2002 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
2007 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
)
2010 if (!(p
->p_memstat_dirty
& P_DIRTY_TRACK
)) {
2011 /* Dirty tracking not enabled */
2013 } else if (pcontrol
&& (p
->p_memstat_dirty
& P_DIRTY_TERMINATED
)) {
2015 * Process is set to be terminated and we're attempting to mark it dirty.
2016 * Set for termination and marking as clean is OK - see <rdar://problem/10594349>.
2020 int flag
= (self
== TRUE
) ? P_DIRTY
: P_DIRTY_SHUTDOWN
;
2021 if (pcontrol
&& !(p
->p_memstat_dirty
& flag
)) {
2022 /* Mark the process as having been dirtied at some point */
2023 p
->p_memstat_dirty
|= (flag
| P_DIRTY_MARKED
);
2024 memorystatus_dirty_count
++;
2026 } else if ((pcontrol
== 0) && (p
->p_memstat_dirty
& flag
)) {
2027 if ((flag
== P_DIRTY_SHUTDOWN
) && (!(p
->p_memstat_dirty
& P_DIRTY
))) {
2028 /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */
2029 p
->p_memstat_dirty
|= P_DIRTY_TERMINATED
;
2031 } else if ((flag
== P_DIRTY
) && (p
->p_memstat_dirty
& P_DIRTY_TERMINATED
)) {
2032 /* Kill previously terminated processes if set clean */
2035 p
->p_memstat_dirty
&= ~flag
;
2036 memorystatus_dirty_count
--;
2048 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
)
2051 if ((was_dirty
== TRUE
&& now_dirty
== FALSE
) ||
2052 (was_dirty
== FALSE
&& now_dirty
== TRUE
)) {
2054 /* Manage idle exit deferral, if applied */
2055 if ((p
->p_memstat_dirty
& (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_DEFER_IN_PROGRESS
)) ==
2056 (P_DIRTY_IDLE_EXIT_ENABLED
|P_DIRTY_DEFER_IN_PROGRESS
)) {
2059 * P_DIRTY_DEFER_IN_PROGRESS means the process is in the deferred band OR it might be heading back
2060 * there once it's clean again and has some protection window left.
2063 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
) {
2065 * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE"
2067 * The process will move from the deferred band to its higher requested
2068 * jetsam band. But we don't clear its state i.e. we want to remember that
2069 * this process was part of the "deferred" band and will return to it.
2071 * This way, we don't let it age beyond the protection
2072 * window when it returns to "clean". All the while giving
2073 * it a chance to perform its work while "dirty".
2076 memorystatus_invalidate_idle_demotion_locked(p
, FALSE
);
2081 * Process is back from "dirty" to "clean".
2083 * Is its timer up OR does it still have some protection
2087 if (mach_absolute_time() >= p
->p_memstat_idledeadline
) {
2089 * The process' deadline has expired. It currently
2090 * does not reside in the DEFERRED bucket.
2092 * It's on its way to the JETSAM_PRIORITY_IDLE
2093 * bucket via memorystatus_update_idle_priority_locked()
2096 * So all we need to do is reset all the state on the
2097 * process that's related to the DEFERRED bucket i.e.
2098 * the DIRTY_DEFER_IN_PROGRESS flag and the timer deadline.
2102 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
2106 * It still has some protection window left and so
2107 * we just re-arm the timer without modifying any
2108 * state on the process.
2110 memorystatus_schedule_idle_demotion_locked(p
, FALSE
);
2116 memorystatus_update_idle_priority_locked(p
);
2119 if (memorystatus_highwater_enabled
) {
2120 boolean_t trigger_exception
;
2122 * We are in this path because this process transitioned between
2123 * dirty <--> clean state. Update the cached memory limits.
2126 if (proc_jetsam_state_is_active_locked(p
) == TRUE
) {
2130 CACHE_ACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
2135 CACHE_INACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
2139 * Enforce the new limits by writing to the ledger.
2141 * This is a hot path and holding the proc_list_lock while writing to the ledgers,
2142 * (where the task lock is taken) is bad. So, we temporarily drop the proc_list_lock.
2143 * We aren't traversing the jetsam bucket list here, so we should be safe.
2144 * See rdar://21394491.
2147 if (proc_ref_locked(p
) == p
) {
2149 if (p
->p_memstat_memlimit
> 0) {
2150 ledger_limit
= p
->p_memstat_memlimit
;
2155 task_set_phys_footprint_limit_internal(p
->task
, ledger_limit
, NULL
, trigger_exception
);
2157 proc_rele_locked(p
);
2159 MEMORYSTATUS_DEBUG(3, "memorystatus_dirty_set: new limit on pid %d (%dMB %s) priority(%d) dirty?=0x%x %s\n",
2160 p
->p_pid
, (p
->p_memstat_memlimit
> 0 ? p
->p_memstat_memlimit
: -1),
2161 (p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
? "F " : "NF"), p
->p_memstat_effectivepriority
, p
->p_memstat_dirty
,
2162 (p
->p_memstat_dirty
? ((p
->p_memstat_dirty
& P_DIRTY
) ? "isdirty" : "isclean") : ""));
2166 #endif /* CONFIG_JETSAM */
2168 /* If the deferral state changed, reschedule the demotion timer */
2170 memorystatus_reschedule_idle_demotion_locked();
2175 if (proc_ref_locked(p
) == p
) {
2177 psignal(p
, SIGKILL
);
2179 proc_rele_locked(p
);
2190 memorystatus_dirty_clear(proc_t p
, uint32_t pcontrol
) {
2194 MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_clear(): %d 0x%x 0x%x\n", p
->p_pid
, pcontrol
, p
->p_memstat_dirty
);
2196 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_DIRTY_CLEAR
), p
->p_pid
, pcontrol
, 0, 0, 0);
2200 if ((p
->p_listflag
& P_LIST_EXITED
) != 0) {
2202 * Process is on its way out.
2208 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
2213 if (!(p
->p_memstat_dirty
& P_DIRTY_TRACK
)) {
2214 /* Dirty tracking not enabled */
2219 if (!pcontrol
|| (pcontrol
& (PROC_DIRTY_LAUNCH_IN_PROGRESS
| PROC_DIRTY_DEFER
)) == 0) {
2224 if (pcontrol
& PROC_DIRTY_LAUNCH_IN_PROGRESS
) {
2225 p
->p_memstat_dirty
&= ~P_DIRTY_LAUNCH_IN_PROGRESS
;
2228 /* This can be set and cleared exactly once. */
2229 if (pcontrol
& PROC_DIRTY_DEFER
) {
2231 if (p
->p_memstat_dirty
& P_DIRTY_DEFER
) {
2233 p
->p_memstat_dirty
&= ~P_DIRTY_DEFER
;
2235 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
2236 memorystatus_update_idle_priority_locked(p
);
2237 memorystatus_reschedule_idle_demotion_locked();
2249 memorystatus_dirty_get(proc_t p
) {
2254 if (p
->p_memstat_dirty
& P_DIRTY_TRACK
) {
2255 ret
|= PROC_DIRTY_TRACKED
;
2256 if (p
->p_memstat_dirty
& P_DIRTY_ALLOW_IDLE_EXIT
) {
2257 ret
|= PROC_DIRTY_ALLOWS_IDLE_EXIT
;
2259 if (p
->p_memstat_dirty
& P_DIRTY
) {
2260 ret
|= PROC_DIRTY_IS_DIRTY
;
2262 if (p
->p_memstat_dirty
& P_DIRTY_LAUNCH_IN_PROGRESS
) {
2263 ret
|= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS
;
2273 memorystatus_on_terminate(proc_t p
) {
2278 p
->p_memstat_dirty
|= P_DIRTY_TERMINATED
;
2280 if ((p
->p_memstat_dirty
& (P_DIRTY_TRACK
|P_DIRTY_IS_DIRTY
)) == P_DIRTY_TRACK
) {
2281 /* Clean; mark as terminated and issue SIGKILL */
2284 /* Dirty, terminated, or state tracking is unsupported; issue SIGTERM to allow cleanup */
2294 memorystatus_on_suspend(proc_t p
)
2298 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
2302 p
->p_memstat_suspendedfootprint
= pages
;
2303 memorystatus_suspended_footprint_total
+= pages
;
2304 memorystatus_suspended_count
++;
2306 p
->p_memstat_state
|= P_MEMSTAT_SUSPENDED
;
2311 memorystatus_on_resume(proc_t p
)
2321 frozen
= (p
->p_memstat_state
& P_MEMSTAT_FROZEN
);
2323 memorystatus_frozen_count
--;
2324 p
->p_memstat_state
|= P_MEMSTAT_PRIOR_THAW
;
2327 memorystatus_suspended_footprint_total
-= p
->p_memstat_suspendedfootprint
;
2328 memorystatus_suspended_count
--;
2333 p
->p_memstat_state
&= ~(P_MEMSTAT_SUSPENDED
| P_MEMSTAT_FROZEN
);
2339 memorystatus_freeze_entry_t data
= { pid
, FALSE
, 0 };
2340 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
2346 memorystatus_on_inactivity(proc_t p
)
2350 /* Wake the freeze thread */
2351 thread_wakeup((event_t
)&memorystatus_freeze_wakeup
);
2356 memorystatus_build_state(proc_t p
) {
2357 uint32_t snapshot_state
= 0;
2360 if (p
->p_memstat_state
& P_MEMSTAT_SUSPENDED
) {
2361 snapshot_state
|= kMemorystatusSuspended
;
2363 if (p
->p_memstat_state
& P_MEMSTAT_FROZEN
) {
2364 snapshot_state
|= kMemorystatusFrozen
;
2366 if (p
->p_memstat_state
& P_MEMSTAT_PRIOR_THAW
) {
2367 snapshot_state
|= kMemorystatusWasThawed
;
2371 if (p
->p_memstat_dirty
& P_DIRTY_TRACK
) {
2372 snapshot_state
|= kMemorystatusTracked
;
2374 if ((p
->p_memstat_dirty
& P_DIRTY_IDLE_EXIT_ENABLED
) == P_DIRTY_IDLE_EXIT_ENABLED
) {
2375 snapshot_state
|= kMemorystatusSupportsIdleExit
;
2377 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
) {
2378 snapshot_state
|= kMemorystatusDirty
;
2381 return snapshot_state
;
2387 kill_idle_exit_proc(void)
2389 proc_t p
, victim_p
= PROC_NULL
;
2390 uint64_t current_time
;
2391 boolean_t killed
= FALSE
;
2394 /* Pick next idle exit victim. */
2395 current_time
= mach_absolute_time();
2399 p
= memorystatus_get_first_proc_locked(&i
, FALSE
);
2401 /* No need to look beyond the idle band */
2402 if (p
->p_memstat_effectivepriority
!= JETSAM_PRIORITY_IDLE
) {
2406 if ((p
->p_memstat_dirty
& (P_DIRTY_ALLOW_IDLE_EXIT
|P_DIRTY_IS_DIRTY
|P_DIRTY_TERMINATED
)) == (P_DIRTY_ALLOW_IDLE_EXIT
)) {
2407 if (current_time
>= p
->p_memstat_idledeadline
) {
2408 p
->p_memstat_dirty
|= P_DIRTY_TERMINATED
;
2409 victim_p
= proc_ref_locked(p
);
2414 p
= memorystatus_get_next_proc_locked(&i
, p
, FALSE
);
2420 printf("memorystatus_thread: idle exiting pid %d [%s]\n", victim_p
->p_pid
, (victim_p
->p_comm
? victim_p
->p_comm
: "(unknown)"));
2421 killed
= memorystatus_do_kill(victim_p
, kMemorystatusKilledIdleExit
);
2422 proc_rele(victim_p
);
2431 memorystatus_thread_wake(void) {
2432 thread_wakeup((event_t
)&memorystatus_wakeup
);
2434 #endif /* CONFIG_JETSAM */
2436 extern void vm_pressure_response(void);
2439 memorystatus_thread_block(uint32_t interval_ms
, thread_continue_t continuation
)
2442 assert_wait_timeout(&memorystatus_wakeup
, THREAD_UNINT
, interval_ms
, 1000 * NSEC_PER_USEC
);
2444 assert_wait(&memorystatus_wakeup
, THREAD_UNINT
);
2447 return thread_block(continuation
);
2451 memorystatus_thread(void *param __unused
, wait_result_t wr __unused
)
2453 static boolean_t is_vm_privileged
= FALSE
;
2456 boolean_t post_snapshot
= FALSE
;
2457 uint32_t errors
= 0;
2458 uint32_t hwm_kill
= 0;
2459 boolean_t sort_flag
= TRUE
;
2461 /* Jetsam Loop Detection - locals */
2462 memstat_bucket_t
*bucket
;
2463 int jld_bucket_count
= 0;
2464 struct timeval jld_now_tstamp
= {0,0};
2465 uint64_t jld_now_msecs
= 0;
2467 /* Jetsam Loop Detection - statics */
2468 static uint64_t jld_timestamp_msecs
= 0;
2469 static int jld_idle_kill_candidates
= 0; /* Number of available processes in band 0,1 at start */
2470 static int jld_idle_kills
= 0; /* Number of procs killed during eval period */
2471 static int jld_eval_aggressive_count
= 0; /* Bumps the max priority in aggressive loop */
2472 static int32_t jld_priority_band_max
= JETSAM_PRIORITY_UI_SUPPORT
;
2475 if (is_vm_privileged
== FALSE
) {
2477 * It's the first time the thread has run, so just mark the thread as privileged and block.
2478 * This avoids a spurious pass with unset variables, as set out in <rdar://problem/9609402>.
2480 thread_wire(host_priv_self(), current_thread(), TRUE
);
2481 is_vm_privileged
= TRUE
;
2483 if (vm_restricted_to_single_processor
== TRUE
)
2484 thread_vm_bind_group_add();
2486 memorystatus_thread_block(0, memorystatus_thread
);
2491 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_SCAN
) | DBG_FUNC_START
,
2492 memorystatus_available_pages
, memorystatus_jld_enabled
, memorystatus_jld_eval_period_msecs
, memorystatus_jld_eval_aggressive_count
,0);
2495 * Jetsam aware version.
2497 * The VM pressure notification thread is working it's way through clients in parallel.
2499 * So, while the pressure notification thread is targeting processes in order of
2500 * increasing jetsam priority, we can hopefully reduce / stop it's work by killing
2501 * any processes that have exceeded their highwater mark.
2503 * If we run out of HWM processes and our available pages drops below the critical threshold, then,
2504 * we target the least recently used process in order of increasing jetsam priority (exception: the FG band).
2506 while (is_thrashing(kill_under_pressure_cause
) ||
2507 memorystatus_available_pages
<= memorystatus_available_pages_pressure
) {
2512 if (kill_under_pressure_cause
) {
2513 cause
= kill_under_pressure_cause
;
2515 cause
= kMemorystatusKilledVMPageShortage
;
2520 killed
= memorystatus_kill_hiwat_proc(&errors
);
2523 post_snapshot
= TRUE
;
2526 memorystatus_hwm_candidates
= FALSE
;
2529 /* No highwater processes to kill. Continue or stop for now? */
2530 if (!is_thrashing(kill_under_pressure_cause
) &&
2531 (memorystatus_available_pages
> memorystatus_available_pages_critical
)) {
2533 * We are _not_ out of pressure but we are above the critical threshold and there's:
2534 * - no compressor thrashing
2535 * - no more HWM processes left.
2536 * For now, don't kill any other processes.
2539 if (hwm_kill
== 0) {
2540 memorystatus_thread_wasted_wakeup
++;
2546 if (memorystatus_jld_enabled
== TRUE
) {
2549 * Jetsam Loop Detection: attempt to detect
2550 * rapid daemon relaunches in the lower bands.
2553 microuptime(&jld_now_tstamp
);
2556 * Ignore usecs in this calculation.
2557 * msecs granularity is close enough.
2559 jld_now_msecs
= (jld_now_tstamp
.tv_sec
* 1000);
2562 bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE
];
2563 jld_bucket_count
= bucket
->count
;
2564 bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE_DEFERRED
];
2565 jld_bucket_count
+= bucket
->count
;
2569 * memorystatus_jld_eval_period_msecs is a tunable
2570 * memorystatus_jld_eval_aggressive_count is a tunable
2571 * memorystatus_jld_eval_aggressive_priority_band_max is a tunable
2573 if ( (jld_bucket_count
== 0) ||
2574 (jld_now_msecs
> (jld_timestamp_msecs
+ memorystatus_jld_eval_period_msecs
))) {
2577 * Refresh evaluation parameters
2579 jld_timestamp_msecs
= jld_now_msecs
;
2580 jld_idle_kill_candidates
= jld_bucket_count
;
2582 jld_eval_aggressive_count
= 0;
2583 jld_priority_band_max
= JETSAM_PRIORITY_UI_SUPPORT
;
2586 if (jld_idle_kills
> jld_idle_kill_candidates
) {
2587 jld_eval_aggressive_count
++;
2588 if (jld_eval_aggressive_count
> memorystatus_jld_eval_aggressive_count
) {
2590 * Bump up the jetsam priority limit (eg: the bucket index)
2591 * Enforce bucket index sanity.
2593 if ((memorystatus_jld_eval_aggressive_priority_band_max
< 0) ||
2594 (memorystatus_jld_eval_aggressive_priority_band_max
>= MEMSTAT_BUCKET_COUNT
)) {
2596 * Do nothing. Stick with the default level.
2599 jld_priority_band_max
= memorystatus_jld_eval_aggressive_priority_band_max
;
2603 killed
= memorystatus_kill_top_process_aggressive(
2605 kMemorystatusKilledVMThrashing
,
2606 jld_eval_aggressive_count
,
2607 jld_priority_band_max
,
2612 /* Always generate logs after aggressive kill */
2613 post_snapshot
= TRUE
;
2620 killed
= memorystatus_kill_top_process(TRUE
, sort_flag
, cause
, &priority
, &errors
);
2625 * Don't generate logs for steady-state idle-exit kills,
2626 * unless it is overridden for debug or by the device
2629 if ((priority
!= JETSAM_PRIORITY_IDLE
) || memorystatus_idle_snapshot
) {
2630 post_snapshot
= TRUE
;
2633 /* Jetsam Loop Detection */
2634 if (memorystatus_jld_enabled
== TRUE
) {
2635 if ((priority
== JETSAM_PRIORITY_IDLE
) || (priority
== JETSAM_PRIORITY_IDLE_DEFERRED
)) {
2639 * We've reached into bands beyond idle deferred.
2640 * We make no attempt to monitor them
2647 if (memorystatus_available_pages
<= memorystatus_available_pages_critical
) {
2648 /* Under pressure and unable to kill a process - panic */
2649 panic("memorystatus_jetsam_thread: no victim! available pages:%d\n", memorystatus_available_pages
);
2655 * We do not want to over-kill when thrashing has been detected.
2656 * To avoid that, we reset the flag here and notify the
2659 if (is_thrashing(kill_under_pressure_cause
)) {
2660 kill_under_pressure_cause
= 0;
2661 vm_thrashing_jetsam_done();
2665 kill_under_pressure_cause
= 0;
2668 memorystatus_clear_errors();
2671 #if VM_PRESSURE_EVENTS
2673 * LD: We used to target the foreground process first and foremost here.
2674 * Now, we target all processes, starting from the non-suspended, background
2675 * processes first. We will target foreground too.
2677 * memorystatus_update_vm_pressure(TRUE);
2679 //vm_pressure_response();
2682 if (post_snapshot
) {
2683 size_t snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) +
2684 sizeof(memorystatus_jetsam_snapshot_entry_t
) * (memorystatus_jetsam_snapshot_count
);
2685 uint64_t timestamp_now
= mach_absolute_time();
2686 memorystatus_jetsam_snapshot
->notification_time
= timestamp_now
;
2687 if (memorystatus_jetsam_snapshot_last_timestamp
== 0 ||
2688 timestamp_now
> memorystatus_jetsam_snapshot_last_timestamp
+ memorystatus_jetsam_snapshot_timeout
) {
2689 int ret
= memorystatus_send_note(kMemorystatusSnapshotNote
, &snapshot_size
, sizeof(snapshot_size
));
2692 memorystatus_jetsam_snapshot_last_timestamp
= timestamp_now
;
2698 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_SCAN
) | DBG_FUNC_END
,
2699 memorystatus_available_pages
, 0, 0, 0, 0);
2701 #else /* CONFIG_JETSAM */
2704 * Jetsam not enabled
2707 #endif /* CONFIG_JETSAM */
2709 memorystatus_thread_block(0, memorystatus_thread
);
2715 * when an idle-exitable proc was killed
2717 * when there are no more idle-exitable procs found
2718 * when the attempt to kill an idle-exitable proc failed
2720 boolean_t
memorystatus_idle_exit_from_VM(void) {
2721 return(kill_idle_exit_proc());
2723 #endif /* !CONFIG_JETSAM */
2728 * Callback invoked when allowable physical memory footprint exceeded
2729 * (dirty pages + IOKit mappings)
2731 * This is invoked for both advisory, non-fatal per-task high watermarks,
2732 * as well as the fatal task memory limits.
2735 memorystatus_on_ledger_footprint_exceeded(boolean_t warning
, const int max_footprint_mb
)
2737 boolean_t is_active
;
2740 proc_t p
= current_proc();
2744 is_active
= proc_jetsam_state_is_active_locked(p
);
2745 is_fatal
= (p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
);
2747 if (warning
== FALSE
) {
2749 * We only want the EXC_RESOURCE to trigger once per lifetime
2750 * of the active/inactive limit state. So, here, we detect the
2751 * active/inactive state of the process and mark the
2752 * state as exception has been triggered.
2754 if (is_active
== TRUE
) {
2756 * turn off exceptions for active state
2758 p
->p_memstat_state
|= P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED
;
2761 * turn off exceptions for inactive state
2763 p
->p_memstat_state
|= P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED
;
2767 * Soft memory limit is a non-fatal high-water-mark
2768 * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit.
2770 printf("process %d (%s) exceeded physical memory footprint, the %s%sMemoryLimit of %d MB\n",
2771 p
->p_pid
, p
->p_comm
, (is_active
? "Active" : "Inactive"),
2772 (is_fatal
? "Hard" : "Soft"), max_footprint_mb
);
2778 #if VM_PRESSURE_EVENTS
2779 if (warning
== TRUE
) {
2780 if (memorystatus_warn_process(p
->p_pid
, TRUE
/* critical? */) != TRUE
) {
2781 /* Print warning, since it's possible that task has not registered for pressure notifications */
2782 printf("task_exceeded_footprint: failed to warn the current task (exiting, or no handler registered?).\n");
2786 #endif /* VM_PRESSURE_EVENTS */
2790 * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task
2791 * has violated either the system-wide per-task memory limit OR its own task limit.
2793 if (memorystatus_kill_process_sync(p
->p_pid
, kMemorystatusKilledPerProcessLimit
) != TRUE
) {
2794 printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n");
2798 * HWM offender exists. Done without locks or synchronization.
2799 * See comment near its declaration for more details.
2801 memorystatus_hwm_candidates
= TRUE
;
2806 * Toggle the P_MEMSTAT_TERMINATED state.
2807 * Takes the proc_list_lock.
2810 proc_memstat_terminated(proc_t p
, boolean_t set
)
2812 #if DEVELOPMENT || DEBUG
2816 p
->p_memstat_state
|= P_MEMSTAT_TERMINATED
;
2818 p
->p_memstat_state
&= ~P_MEMSTAT_TERMINATED
;
2823 #pragma unused(p, set)
2827 #endif /* DEVELOPMENT || DEBUG */
2832 * This is invoked when cpulimits have been exceeded while in fatal mode.
2833 * The jetsam_flags do not apply as those are for memory related kills.
2834 * We call this routine so that the offending process is killed with
2835 * a non-zero exit status.
2838 jetsam_on_ledger_cpulimit_exceeded(void)
2841 int jetsam_flags
= 0; /* make it obvious */
2842 proc_t p
= current_proc();
2844 printf("task_exceeded_cpulimit: killing pid %d [%s]\n",
2845 p
->p_pid
, (p
->p_comm
? p
->p_comm
: "(unknown)"));
2847 retval
= jetsam_do_kill(p
, jetsam_flags
);
2850 printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n");
2855 memorystatus_get_task_page_counts(task_t task
, uint32_t *footprint
, uint32_t *max_footprint
, uint32_t *max_footprint_lifetime
, uint32_t *purgeable_pages
)
2860 *footprint
= (uint32_t)(get_task_phys_footprint(task
) / PAGE_SIZE_64
);
2861 if (max_footprint
) {
2862 *max_footprint
= (uint32_t)(get_task_phys_footprint_max(task
) / PAGE_SIZE_64
);
2864 if (max_footprint_lifetime
) {
2865 *max_footprint_lifetime
= (uint32_t)(get_task_resident_max(task
) / PAGE_SIZE_64
);
2867 if (purgeable_pages
) {
2868 *purgeable_pages
= (uint32_t)(get_task_purgeable_size(task
) / PAGE_SIZE_64
);
2873 memorystatus_update_jetsam_snapshot_entry_locked(proc_t p
, uint32_t kill_cause
)
2877 for (i
= 0; i
< memorystatus_jetsam_snapshot_count
; i
++) {
2878 if (memorystatus_jetsam_snapshot_list
[i
].pid
== p
->p_pid
) {
2879 /* Update if the priority has changed since the snapshot was taken */
2880 if (memorystatus_jetsam_snapshot_list
[i
].priority
!= p
->p_memstat_effectivepriority
) {
2881 memorystatus_jetsam_snapshot_list
[i
].priority
= p
->p_memstat_effectivepriority
;
2882 strlcpy(memorystatus_jetsam_snapshot_list
[i
].name
, p
->p_comm
, MAXCOMLEN
+1);
2883 memorystatus_jetsam_snapshot_list
[i
].state
= memorystatus_build_state(p
);
2884 memorystatus_jetsam_snapshot_list
[i
].user_data
= p
->p_memstat_userdata
;
2885 memorystatus_jetsam_snapshot_list
[i
].fds
= p
->p_fd
->fd_nfiles
;
2887 memorystatus_jetsam_snapshot_list
[i
].killed
= kill_cause
;
2893 void memorystatus_pages_update(unsigned int pages_avail
)
2895 memorystatus_available_pages
= pages_avail
;
2897 #if VM_PRESSURE_EVENTS
2899 * Since memorystatus_available_pages changes, we should
2900 * re-evaluate the pressure levels on the system and
2901 * check if we need to wake the pressure thread.
2902 * We also update memorystatus_level in that routine.
2904 vm_pressure_response();
2906 if (memorystatus_available_pages
<= memorystatus_available_pages_pressure
) {
2908 if (memorystatus_hwm_candidates
|| (memorystatus_available_pages
<= memorystatus_available_pages_critical
)) {
2909 memorystatus_thread_wake();
2912 #else /* VM_PRESSURE_EVENTS */
2914 boolean_t critical
, delta
;
2916 if (!memorystatus_delta
) {
2920 critical
= (pages_avail
< memorystatus_available_pages_critical
) ? TRUE
: FALSE
;
2921 delta
= ((pages_avail
>= (memorystatus_available_pages
+ memorystatus_delta
))
2922 || (memorystatus_available_pages
>= (pages_avail
+ memorystatus_delta
))) ? TRUE
: FALSE
;
2924 if (critical
|| delta
) {
2925 memorystatus_level
= memorystatus_available_pages
* 100 / atop_64(max_mem
);
2926 memorystatus_thread_wake();
2928 #endif /* VM_PRESSURE_EVENTS */
2932 memorystatus_init_jetsam_snapshot_entry_locked(proc_t p
, memorystatus_jetsam_snapshot_entry_t
*entry
)
2935 clock_usec_t tv_usec
;
2937 memset(entry
, 0, sizeof(memorystatus_jetsam_snapshot_entry_t
));
2939 entry
->pid
= p
->p_pid
;
2940 strlcpy(&entry
->name
[0], p
->p_comm
, MAXCOMLEN
+1);
2941 entry
->priority
= p
->p_memstat_effectivepriority
;
2942 memorystatus_get_task_page_counts(p
->task
, &entry
->pages
, &entry
->max_pages
, &entry
->max_pages_lifetime
, &entry
->purgeable_pages
);
2943 entry
->state
= memorystatus_build_state(p
);
2944 entry
->user_data
= p
->p_memstat_userdata
;
2945 memcpy(&entry
->uuid
[0], &p
->p_uuid
[0], sizeof(p
->p_uuid
));
2946 entry
->fds
= p
->p_fd
->fd_nfiles
;
2948 absolutetime_to_microtime(get_task_cpu_time(p
->task
), &tv_sec
, &tv_usec
);
2949 entry
->cpu_time
.tv_sec
= tv_sec
;
2950 entry
->cpu_time
.tv_usec
= tv_usec
;
2956 memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t
*snapshot
)
2958 kern_return_t kr
= KERN_SUCCESS
;
2959 mach_msg_type_number_t count
= HOST_VM_INFO64_COUNT
;
2960 vm_statistics64_data_t vm_stat
;
2962 if ((kr
= host_statistics64(host_self(), HOST_VM_INFO64
, (host_info64_t
)&vm_stat
, &count
) != KERN_SUCCESS
)) {
2963 printf("memorystatus_init_jetsam_snapshot_stats: host_statistics64 failed with %d\n", kr
);
2964 memset(&snapshot
->stats
, 0, sizeof(snapshot
->stats
));
2966 snapshot
->stats
.free_pages
= vm_stat
.free_count
;
2967 snapshot
->stats
.active_pages
= vm_stat
.active_count
;
2968 snapshot
->stats
.inactive_pages
= vm_stat
.inactive_count
;
2969 snapshot
->stats
.throttled_pages
= vm_stat
.throttled_count
;
2970 snapshot
->stats
.purgeable_pages
= vm_stat
.purgeable_count
;
2971 snapshot
->stats
.wired_pages
= vm_stat
.wire_count
;
2973 snapshot
->stats
.speculative_pages
= vm_stat
.speculative_count
;
2974 snapshot
->stats
.filebacked_pages
= vm_stat
.external_page_count
;
2975 snapshot
->stats
.anonymous_pages
= vm_stat
.internal_page_count
;
2976 snapshot
->stats
.compressions
= vm_stat
.compressions
;
2977 snapshot
->stats
.decompressions
= vm_stat
.decompressions
;
2978 snapshot
->stats
.compressor_pages
= vm_stat
.compressor_page_count
;
2979 snapshot
->stats
.total_uncompressed_pages_in_compressor
= vm_stat
.total_uncompressed_pages_in_compressor
;
2984 * Collect vm statistics at boot.
2985 * Called only once (see kern_exec.c)
2986 * Data can be consumed at any time.
2989 memorystatus_init_at_boot_snapshot() {
2990 memorystatus_init_snapshot_vmstats(&memorystatus_at_boot_snapshot
);
2991 memorystatus_at_boot_snapshot
.entry_count
= 0;
2992 memorystatus_at_boot_snapshot
.notification_time
= 0; /* updated when consumed */
2993 memorystatus_at_boot_snapshot
.snapshot_time
= mach_absolute_time();
2997 memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t
*od_snapshot
, uint32_t ods_list_count
)
3000 unsigned int b
= 0, i
= 0;
3002 memorystatus_jetsam_snapshot_t
*snapshot
= NULL
;
3003 memorystatus_jetsam_snapshot_entry_t
*snapshot_list
= NULL
;
3004 unsigned int snapshot_max
= 0;
3008 * This is an on_demand snapshot
3010 snapshot
= od_snapshot
;
3011 snapshot_list
= od_snapshot
->entries
;
3012 snapshot_max
= ods_list_count
;
3015 * This is a jetsam event snapshot
3017 snapshot
= memorystatus_jetsam_snapshot
;
3018 snapshot_list
= memorystatus_jetsam_snapshot
->entries
;
3019 snapshot_max
= memorystatus_jetsam_snapshot_max
;
3022 memorystatus_init_snapshot_vmstats(snapshot
);
3024 next_p
= memorystatus_get_first_proc_locked(&b
, TRUE
);
3027 next_p
= memorystatus_get_next_proc_locked(&b
, p
, TRUE
);
3029 if (FALSE
== memorystatus_init_jetsam_snapshot_entry_locked(p
, &snapshot_list
[i
])) {
3033 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",
3035 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],
3036 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]);
3038 if (++i
== snapshot_max
) {
3043 snapshot
->snapshot_time
= mach_absolute_time();
3044 snapshot
->entry_count
= i
;
3047 /* update the system buffer count */
3048 memorystatus_jetsam_snapshot_count
= i
;
3052 #if DEVELOPMENT || DEBUG
3055 memorystatus_cmd_set_panic_bits(user_addr_t buffer
, uint32_t buffer_size
) {
3057 memorystatus_jetsam_panic_options_t debug
;
3059 if (buffer_size
!= sizeof(memorystatus_jetsam_panic_options_t
)) {
3063 ret
= copyin(buffer
, &debug
, buffer_size
);
3068 /* Panic bits match kMemorystatusKilled* enum */
3069 memorystatus_jetsam_panic_debug
= (memorystatus_jetsam_panic_debug
& ~debug
.mask
) | (debug
.data
& debug
.mask
);
3071 /* Copyout new value */
3072 debug
.data
= memorystatus_jetsam_panic_debug
;
3073 ret
= copyout(&debug
, buffer
, sizeof(memorystatus_jetsam_panic_options_t
));
3079 * Triggers a sort_order on a specified jetsam priority band.
3080 * This is for testing only, used to force a path through the sort
3084 memorystatus_cmd_test_jetsam_sort(int priority
, int sort_order
) {
3088 unsigned int bucket_index
= 0;
3090 if (priority
== -1) {
3091 /* Use as shorthand for default priority */
3092 bucket_index
= JETSAM_PRIORITY_DEFAULT
;
3094 bucket_index
= (unsigned int)priority
;
3097 error
= memorystatus_sort_bucket(bucket_index
, sort_order
);
3105 * Jetsam a specific process.
3108 memorystatus_kill_specific_process(pid_t victim_pid
, uint32_t cause
) {
3112 /* TODO - add a victim queue and push this into the main jetsam thread */
3113 p
= proc_find(victim_pid
);
3120 if (memorystatus_jetsam_snapshot_count
== 0) {
3121 memorystatus_init_jetsam_snapshot_locked(NULL
,0);
3124 memorystatus_update_jetsam_snapshot_entry_locked(p
, cause
);
3127 printf("memorystatus: specifically killing pid %d [%s] (%s %d) - memorystatus_available_pages: %d\n",
3128 victim_pid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
3129 jetsam_kill_cause_name
[cause
], p
->p_memstat_effectivepriority
, memorystatus_available_pages
);
3132 killed
= memorystatus_do_kill(p
, cause
);
3139 * Jetsam the first process in the queue.
3142 memorystatus_kill_top_process(boolean_t any
, boolean_t sort_flag
, uint32_t cause
, int32_t *priority
, uint32_t *errors
)
3145 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
3146 boolean_t new_snapshot
= FALSE
, killed
= FALSE
;
3151 #ifndef CONFIG_FREEZE
3155 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM
) | DBG_FUNC_START
,
3156 memorystatus_available_pages
, 0, 0, 0, 0);
3159 if (sort_flag
== TRUE
) {
3160 (void)memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND
, JETSAM_SORT_DEFAULT
);
3165 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3167 #if DEVELOPMENT || DEBUG
3169 int procSuspendedForDiagnosis
;
3170 #endif /* DEVELOPMENT || DEBUG */
3173 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
3175 #if DEVELOPMENT || DEBUG
3176 activeProcess
= p
->p_memstat_state
& P_MEMSTAT_FOREGROUND
;
3177 procSuspendedForDiagnosis
= p
->p_memstat_state
& P_MEMSTAT_DIAG_SUSPENDED
;
3178 #endif /* DEVELOPMENT || DEBUG */
3181 aPid_ep
= p
->p_memstat_effectivepriority
;
3183 if (p
->p_memstat_state
& (P_MEMSTAT_ERROR
| P_MEMSTAT_TERMINATED
)) {
3187 #if DEVELOPMENT || DEBUG
3188 if ((memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) && procSuspendedForDiagnosis
) {
3189 printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid
);
3192 #endif /* DEVELOPMENT || DEBUG */
3194 if (cause
== kMemorystatusKilledVnodes
)
3197 * If the system runs out of vnodes, we systematically jetsam
3198 * processes in hopes of stumbling onto a vnode gain that helps
3199 * the system recover. The process that happens to trigger
3200 * this path has no known relationship to the vnode consumption.
3201 * We attempt to safeguard that process e.g: do not jetsam it.
3204 if (p
== current_proc()) {
3205 /* do not jetsam the current process */
3212 boolean_t reclaim_proc
= !(p
->p_memstat_state
& (P_MEMSTAT_LOCKED
| P_MEMSTAT_NORECLAIM
));
3213 if (any
|| reclaim_proc
) {
3225 * Capture a snapshot if none exists and:
3226 * - priority was not requested (this is something other than an ambient kill)
3227 * - the priority was requested *and* the targeted process is not at idle priority
3229 if ((memorystatus_jetsam_snapshot_count
== 0) &&
3230 (memorystatus_idle_snapshot
|| ((!priority
) || (priority
&& (*priority
!= JETSAM_PRIORITY_IDLE
))))) {
3231 memorystatus_init_jetsam_snapshot_locked(NULL
,0);
3232 new_snapshot
= TRUE
;
3236 * Mark as terminated so that if exit1() indicates success, but the process (for example)
3237 * is blocked in task_exception_notify(), it'll be skipped if encountered again - see
3238 * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the
3239 * acquisition of the proc lock.
3241 p
->p_memstat_state
|= P_MEMSTAT_TERMINATED
;
3243 #if DEVELOPMENT || DEBUG
3244 if ((memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) && activeProcess
) {
3245 MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memory_status_level: %d\n",
3246 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"), memorystatus_level
);
3247 memorystatus_update_jetsam_snapshot_entry_locked(p
, kMemorystatusKilledDiagnostic
);
3248 p
->p_memstat_state
|= P_MEMSTAT_DIAG_SUSPENDED
;
3249 if (memorystatus_jetsam_policy
& kPolicyDiagnoseFirst
) {
3250 jetsam_diagnostic_suspended_one_active_proc
= 1;
3251 printf("jetsam: returning after suspending first active proc - %d\n", aPid
);
3254 p
= proc_ref_locked(p
);
3257 task_suspend(p
->task
);
3259 *priority
= aPid_ep
;
3267 #endif /* DEVELOPMENT || DEBUG */
3269 /* Shift queue, update stats */
3270 memorystatus_update_jetsam_snapshot_entry_locked(p
, cause
);
3272 if (proc_ref_locked(p
) == p
) {
3274 printf("memorystatus: %s %d [%s] (%s %d) - memorystatus_available_pages: %d\n",
3275 ((aPid_ep
== JETSAM_PRIORITY_IDLE
) ?
3276 "idle exiting pid" : "jetsam killing pid"),
3277 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
3278 jetsam_kill_cause_name
[cause
], aPid_ep
, memorystatus_available_pages
);
3280 killed
= memorystatus_do_kill(p
, cause
);
3285 *priority
= aPid_ep
;
3293 * Failure - first unwind the state,
3294 * then fall through to restart the search.
3297 proc_rele_locked(p
);
3298 p
->p_memstat_state
&= ~P_MEMSTAT_TERMINATED
;
3299 p
->p_memstat_state
|= P_MEMSTAT_ERROR
;
3304 * Failure - restart the search.
3306 * We might have raced with "p" exiting on another core, resulting in no
3307 * ref on "p". Or, we may have failed to kill "p".
3309 * Either way, we fall thru to here, leaving the proc in the
3310 * P_MEMSTAT_TERMINATED state.
3312 * And, we hold the the proc_list_lock at this point.
3316 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3324 /* Clear snapshot if freshly captured and no target was found */
3325 if (new_snapshot
&& !killed
) {
3326 memorystatus_jetsam_snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
3329 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM
) | DBG_FUNC_END
,
3330 memorystatus_available_pages
, killed
? aPid
: 0, kill_count
, 0, 0);
3336 * Jetsam aggressively
3339 memorystatus_kill_top_process_aggressive(boolean_t any
, uint32_t cause
, int aggr_count
, int32_t priority_max
,
3343 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
3344 boolean_t new_snapshot
= FALSE
, killed
= FALSE
;
3347 int32_t aPid_ep
= 0;
3348 unsigned int memorystatus_level_snapshot
= 0;
3352 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM
) | DBG_FUNC_START
,
3353 memorystatus_available_pages
, priority_max
, 0, 0, 0);
3355 memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND
, JETSAM_SORT_DEFAULT
);
3359 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3361 #if DEVELOPMENT || DEBUG
3363 int procSuspendedForDiagnosis
;
3364 #endif /* DEVELOPMENT || DEBUG */
3366 if ((unsigned int)(next_p
->p_memstat_effectivepriority
) != i
) {
3369 * We have raced with next_p running on another core, as it has
3370 * moved to a different jetsam priority band. This means we have
3371 * lost our place in line while traversing the jetsam list. We
3372 * attempt to recover by rewinding to the beginning of the band
3373 * we were already traversing. By doing this, we do not guarantee
3374 * that no process escapes this aggressive march, but we can make
3375 * skipping an entire range of processes less likely. (PR-21069019)
3378 MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: rewinding %s moved from band %d --> %d\n",
3379 aggr_count
, next_p
->p_comm
, i
, next_p
->p_memstat_effectivepriority
);
3381 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3386 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
3388 if (p
->p_memstat_effectivepriority
> priority_max
) {
3390 * Bail out of this killing spree if we have
3391 * reached beyond the priority_max jetsam band.
3392 * That is, we kill up to and through the
3393 * priority_max jetsam band.
3399 #if DEVELOPMENT || DEBUG
3400 activeProcess
= p
->p_memstat_state
& P_MEMSTAT_FOREGROUND
;
3401 procSuspendedForDiagnosis
= p
->p_memstat_state
& P_MEMSTAT_DIAG_SUSPENDED
;
3402 #endif /* DEVELOPMENT || DEBUG */
3405 aPid_ep
= p
->p_memstat_effectivepriority
;
3407 if (p
->p_memstat_state
& (P_MEMSTAT_ERROR
| P_MEMSTAT_TERMINATED
)) {
3411 #if DEVELOPMENT || DEBUG
3412 if ((memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) && procSuspendedForDiagnosis
) {
3413 printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid
);
3416 #endif /* DEVELOPMENT || DEBUG */
3419 * Capture a snapshot if none exists.
3421 if (memorystatus_jetsam_snapshot_count
== 0) {
3422 memorystatus_init_jetsam_snapshot_locked(NULL
,0);
3423 new_snapshot
= TRUE
;
3427 * Mark as terminated so that if exit1() indicates success, but the process (for example)
3428 * is blocked in task_exception_notify(), it'll be skipped if encountered again - see
3429 * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the
3430 * acquisition of the proc lock.
3432 p
->p_memstat_state
|= P_MEMSTAT_TERMINATED
;
3434 /* Shift queue, update stats */
3435 memorystatus_update_jetsam_snapshot_entry_locked(p
, cause
);
3438 * In order to kill the target process, we will drop the proc_list_lock.
3439 * To guaranteee that p and next_p don't disappear out from under the lock,
3440 * we must take a ref on both.
3441 * If we cannot get a reference, then it's likely we've raced with
3442 * that process exiting on another core.
3444 if (proc_ref_locked(p
) == p
) {
3446 while (next_p
&& (proc_ref_locked(next_p
) != next_p
)) {
3450 * We must have raced with next_p exiting on another core.
3451 * Recover by getting the next eligible process in the band.
3454 MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: skipping %d [%s] (exiting?)\n",
3455 aggr_count
, next_p
->p_pid
, (next_p
->p_comm
? next_p
->p_comm
: "(unknown)"));
3458 next_p
= memorystatus_get_next_proc_locked(&i
, temp_p
, TRUE
);
3463 printf("memorystatus: aggressive%d: %s %d [%s] (%s %d) - memorystatus_available_pages: %d\n",
3465 ((aPid_ep
== JETSAM_PRIORITY_IDLE
) ? "idle exiting pid" : "jetsam killing pid"),
3466 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
3467 jetsam_kill_cause_name
[cause
], aPid_ep
, memorystatus_available_pages
);
3469 memorystatus_level_snapshot
= memorystatus_level
;
3471 killed
= memorystatus_do_kill(p
, cause
);
3481 * Continue the killing spree.
3485 proc_rele_locked(next_p
);
3488 if (aPid_ep
== JETSAM_PRIORITY_FOREGROUND
&& memorystatus_aggressive_jetsam_lenient
== TRUE
) {
3489 if (memorystatus_level
> memorystatus_level_snapshot
&& ((memorystatus_level
- memorystatus_level_snapshot
) >= AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD
)) {
3490 #if DEVELOPMENT || DEBUG
3491 printf("Disabling Lenient mode after one-time deployment.\n");
3492 #endif /* DEVELOPMENT || DEBUG */
3493 memorystatus_aggressive_jetsam_lenient
= FALSE
;
3502 * Failure - first unwind the state,
3503 * then fall through to restart the search.
3506 proc_rele_locked(p
);
3508 proc_rele_locked(next_p
);
3510 p
->p_memstat_state
&= ~P_MEMSTAT_TERMINATED
;
3511 p
->p_memstat_state
|= P_MEMSTAT_ERROR
;
3517 * Failure - restart the search at the beginning of
3518 * the band we were already traversing.
3520 * We might have raced with "p" exiting on another core, resulting in no
3521 * ref on "p". Or, we may have failed to kill "p".
3523 * Either way, we fall thru to here, leaving the proc in the
3524 * P_MEMSTAT_TERMINATED or P_MEMSTAT_ERROR state.
3526 * And, we hold the the proc_list_lock at this point.
3529 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3535 /* Clear snapshot if freshly captured and no target was found */
3536 if (new_snapshot
&& (kill_count
== 0)) {
3537 memorystatus_jetsam_snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
3540 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM
) | DBG_FUNC_END
,
3541 memorystatus_available_pages
, killed
? aPid
: 0, kill_count
, 0, 0);
3543 if (kill_count
> 0) {
3554 memorystatus_kill_hiwat_proc(uint32_t *errors
)
3557 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
3558 boolean_t new_snapshot
= FALSE
, killed
= FALSE
;
3563 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM_HIWAT
) | DBG_FUNC_START
,
3564 memorystatus_available_pages
, 0, 0, 0, 0);
3568 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3574 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
3577 aPid_ep
= p
->p_memstat_effectivepriority
;
3579 if (p
->p_memstat_state
& (P_MEMSTAT_ERROR
| P_MEMSTAT_TERMINATED
)) {
3583 /* skip if no limit set */
3584 if (p
->p_memstat_memlimit
<= 0) {
3590 * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
3591 * Background limits are described via the inactive limit slots.
3592 * Their fatal/non-fatal setting will drive whether or not to be
3593 * considered in this kill path.
3596 /* skip if a currently inapplicable limit is encountered */
3597 if ((p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_BACKGROUND
) && (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
)) {
3602 footprint
= (uint32_t)(get_task_phys_footprint(p
->task
) / (1024 * 1024));
3603 skip
= (((int32_t)footprint
) <= p
->p_memstat_memlimit
);
3605 #if DEVELOPMENT || DEBUG
3606 if (!skip
&& (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
)) {
3607 if (p
->p_memstat_state
& P_MEMSTAT_DIAG_SUSPENDED
) {
3611 #endif /* DEVELOPMENT || DEBUG */
3615 if (p
->p_memstat_state
& P_MEMSTAT_LOCKED
) {
3626 MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %d Mb > 1 (%d Mb)\n",
3627 (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) ? "suspending": "killing", aPid
, p
->p_comm
, footprint
, p
->p_memstat_memlimit
);
3629 if (memorystatus_jetsam_snapshot_count
== 0) {
3630 memorystatus_init_jetsam_snapshot_locked(NULL
,0);
3631 new_snapshot
= TRUE
;
3634 p
->p_memstat_state
|= P_MEMSTAT_TERMINATED
;
3636 #if DEVELOPMENT || DEBUG
3637 if (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) {
3638 MEMORYSTATUS_DEBUG(1, "jetsam: pid %d suspended for diagnosis - memorystatus_available_pages: %d\n", aPid
, memorystatus_available_pages
);
3639 memorystatus_update_jetsam_snapshot_entry_locked(p
, kMemorystatusKilledDiagnostic
);
3640 p
->p_memstat_state
|= P_MEMSTAT_DIAG_SUSPENDED
;
3642 p
= proc_ref_locked(p
);
3645 task_suspend(p
->task
);
3652 #endif /* DEVELOPMENT || DEBUG */
3654 memorystatus_update_jetsam_snapshot_entry_locked(p
, kMemorystatusKilledHiwat
);
3656 if (proc_ref_locked(p
) == p
) {
3659 printf("memorystatus: jetsam killing pid %d [%s] (highwater %d) - memorystatus_available_pages: %d\n",
3660 aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"), aPid_ep
, memorystatus_available_pages
);
3662 killed
= memorystatus_do_kill(p
, kMemorystatusKilledHiwat
);
3672 * Failure - first unwind the state,
3673 * then fall through to restart the search.
3676 proc_rele_locked(p
);
3677 p
->p_memstat_state
&= ~P_MEMSTAT_TERMINATED
;
3678 p
->p_memstat_state
|= P_MEMSTAT_ERROR
;
3683 * Failure - restart the search.
3685 * We might have raced with "p" exiting on another core, resulting in no
3686 * ref on "p". Or, we may have failed to kill "p".
3688 * Either way, we fall thru to here, leaving the proc in the
3689 * P_MEMSTAT_TERMINATED state.
3691 * And, we hold the the proc_list_lock at this point.
3695 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3703 /* Clear snapshot if freshly captured and no target was found */
3704 if (new_snapshot
&& !killed
) {
3705 memorystatus_jetsam_snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
3708 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_JETSAM_HIWAT
) | DBG_FUNC_END
,
3709 memorystatus_available_pages
, killed
? aPid
: 0, kill_count
, 0, 0);
3714 #endif /* LEGACY_HIWATER */
3717 memorystatus_kill_process_async(pid_t victim_pid
, uint32_t cause
) {
3718 /* TODO: allow a general async path */
3719 if ((victim_pid
!= -1) || (cause
!= kMemorystatusKilledVMPageShortage
&& cause
!= kMemorystatusKilledVMThrashing
&&
3720 cause
!= kMemorystatusKilledFCThrashing
)) {
3724 kill_under_pressure_cause
= cause
;
3725 memorystatus_thread_wake();
3730 memorystatus_kill_process_sync(pid_t victim_pid
, uint32_t cause
) {
3732 uint32_t errors
= 0;
3734 if (victim_pid
== -1) {
3735 /* No pid, so kill first process */
3736 res
= memorystatus_kill_top_process(TRUE
, TRUE
, cause
, NULL
, &errors
);
3738 res
= memorystatus_kill_specific_process(victim_pid
, cause
);
3742 memorystatus_clear_errors();
3746 /* Fire off snapshot notification */
3747 size_t snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) +
3748 sizeof(memorystatus_jetsam_snapshot_entry_t
) * memorystatus_jetsam_snapshot_count
;
3749 uint64_t timestamp_now
= mach_absolute_time();
3750 memorystatus_jetsam_snapshot
->notification_time
= timestamp_now
;
3751 if (memorystatus_jetsam_snapshot_last_timestamp
== 0 ||
3752 timestamp_now
> memorystatus_jetsam_snapshot_last_timestamp
+ memorystatus_jetsam_snapshot_timeout
) {
3753 int ret
= memorystatus_send_note(kMemorystatusSnapshotNote
, &snapshot_size
, sizeof(snapshot_size
));
3756 memorystatus_jetsam_snapshot_last_timestamp
= timestamp_now
;
3766 memorystatus_kill_on_VM_page_shortage(boolean_t async
) {
3768 return memorystatus_kill_process_async(-1, kMemorystatusKilledVMPageShortage
);
3770 return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMPageShortage
);
3775 memorystatus_kill_on_VM_thrashing(boolean_t async
) {
3777 return memorystatus_kill_process_async(-1, kMemorystatusKilledVMThrashing
);
3779 return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMThrashing
);
3784 memorystatus_kill_on_FC_thrashing(boolean_t async
) {
3786 return memorystatus_kill_process_async(-1, kMemorystatusKilledFCThrashing
);
3788 return memorystatus_kill_process_sync(-1, kMemorystatusKilledFCThrashing
);
3793 memorystatus_kill_on_vnode_limit(void) {
3794 return memorystatus_kill_process_sync(-1, kMemorystatusKilledVnodes
);
3797 #endif /* CONFIG_JETSAM */
3801 __private_extern__
void
3802 memorystatus_freeze_init(void)
3804 kern_return_t result
;
3807 freezer_lck_grp_attr
= lck_grp_attr_alloc_init();
3808 freezer_lck_grp
= lck_grp_alloc_init("freezer", freezer_lck_grp_attr
);
3810 lck_mtx_init(&freezer_mutex
, freezer_lck_grp
, NULL
);
3812 result
= kernel_thread_start(memorystatus_freeze_thread
, NULL
, &thread
);
3813 if (result
== KERN_SUCCESS
) {
3814 thread_deallocate(thread
);
3816 panic("Could not create memorystatus_freeze_thread");
3821 * Synchronously freeze the passed proc. Called with a reference to the proc held.
3823 * Returns EINVAL or the value returned by task_freeze().
3826 memorystatus_freeze_process_sync(proc_t p
)
3830 boolean_t memorystatus_freeze_swap_low
= FALSE
;
3832 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_START
,
3833 memorystatus_available_pages
, 0, 0, 0, 0);
3835 lck_mtx_lock(&freezer_mutex
);
3841 if (memorystatus_freeze_enabled
== FALSE
) {
3845 if (!memorystatus_can_freeze(&memorystatus_freeze_swap_low
)) {
3849 if (memorystatus_freeze_update_throttle()) {
3850 printf("memorystatus_freeze_process_sync: in throttle, ignorning freeze\n");
3851 memorystatus_freeze_throttle_count
++;
3858 uint32_t purgeable
, wired
, clean
, dirty
, state
;
3859 uint32_t max_pages
, pages
, i
;
3863 state
= p
->p_memstat_state
;
3865 /* Ensure the process is eligible for freezing */
3866 if ((state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_FROZEN
)) || !(state
& P_MEMSTAT_SUSPENDED
)) {
3871 /* Only freeze processes meeting our minimum resident page criteria */
3872 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
3873 if (pages
< memorystatus_freeze_pages_min
) {
3878 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
3880 unsigned int avail_swap_space
= 0; /* in pages. */
3882 if (DEFAULT_FREEZER_IS_ACTIVE
) {
3884 * Freezer backed by default pager and swap file(s).
3886 avail_swap_space
= default_pager_swap_pages_free();
3889 * Freezer backed by the compressor and swap file(s)
3890 * while will hold compressed data.
3892 avail_swap_space
= vm_swap_get_free_space() / PAGE_SIZE_64
;
3895 max_pages
= MIN(avail_swap_space
, memorystatus_freeze_pages_max
);
3897 if (max_pages
< memorystatus_freeze_pages_min
) {
3903 * We only have the compressor without any swap.
3905 max_pages
= UINT32_MAX
- 1;
3908 /* Mark as locked temporarily to avoid kill */
3909 p
->p_memstat_state
|= P_MEMSTAT_LOCKED
;
3912 ret
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, FALSE
);
3914 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_process_sync: task_freeze %s for pid %d [%s] - "
3915 "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, shared %d, free swap: %d\n",
3916 (ret
== KERN_SUCCESS
) ? "SUCCEEDED" : "FAILED", aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
3917 memorystatus_available_pages
, purgeable
, wired
, clean
, dirty
, shared
, default_pager_swap_pages_free());
3920 p
->p_memstat_state
&= ~P_MEMSTAT_LOCKED
;
3922 if (ret
== KERN_SUCCESS
) {
3923 memorystatus_freeze_entry_t data
= { aPid
, TRUE
, dirty
};
3925 memorystatus_frozen_count
++;
3927 p
->p_memstat_state
|= (P_MEMSTAT_FROZEN
| (shared
? 0: P_MEMSTAT_NORECLAIM
));
3929 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
3931 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
3932 throttle_intervals
[i
].pageouts
+= dirty
;
3936 memorystatus_freeze_pageouts
+= dirty
;
3937 memorystatus_freeze_count
++;
3941 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
3948 lck_mtx_unlock(&freezer_mutex
);
3949 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_END
,
3950 memorystatus_available_pages
, aPid
, 0, 0, 0);
3956 memorystatus_freeze_top_process(boolean_t
*memorystatus_freeze_swap_low
)
3960 proc_t p
= PROC_NULL
, next_p
= PROC_NULL
;
3963 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_START
,
3964 memorystatus_available_pages
, 0, 0, 0, 0);
3968 next_p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
3971 uint32_t purgeable
, wired
, clean
, dirty
;
3974 uint32_t max_pages
= 0;
3978 next_p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
3981 state
= p
->p_memstat_state
;
3983 /* Ensure the process is eligible for freezing */
3984 if ((state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_FROZEN
)) || !(state
& P_MEMSTAT_SUSPENDED
)) {
3985 continue; // with lock held
3988 /* Only freeze processes meeting our minimum resident page criteria */
3989 memorystatus_get_task_page_counts(p
->task
, &pages
, NULL
, NULL
, NULL
);
3990 if (pages
< memorystatus_freeze_pages_min
) {
3991 continue; // with lock held
3994 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
3996 /* Ensure there's enough free space to freeze this process. */
3998 unsigned int avail_swap_space
= 0; /* in pages. */
4000 if (DEFAULT_FREEZER_IS_ACTIVE
) {
4002 * Freezer backed by default pager and swap file(s).
4004 avail_swap_space
= default_pager_swap_pages_free();
4007 * Freezer backed by the compressor and swap file(s)
4008 * while will hold compressed data.
4010 avail_swap_space
= vm_swap_get_free_space() / PAGE_SIZE_64
;
4013 max_pages
= MIN(avail_swap_space
, memorystatus_freeze_pages_max
);
4015 if (max_pages
< memorystatus_freeze_pages_min
) {
4016 *memorystatus_freeze_swap_low
= TRUE
;
4022 * We only have the compressor pool.
4024 max_pages
= UINT32_MAX
- 1;
4027 /* Mark as locked temporarily to avoid kill */
4028 p
->p_memstat_state
|= P_MEMSTAT_LOCKED
;
4030 p
= proc_ref_locked(p
);
4036 kr
= task_freeze(p
->task
, &purgeable
, &wired
, &clean
, &dirty
, max_pages
, &shared
, FALSE
);
4038 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_top_process: task_freeze %s for pid %d [%s] - "
4039 "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, shared %d, free swap: %d\n",
4040 (kr
== KERN_SUCCESS
) ? "SUCCEEDED" : "FAILED", aPid
, (p
->p_comm
? p
->p_comm
: "(unknown)"),
4041 memorystatus_available_pages
, purgeable
, wired
, clean
, dirty
, shared
, default_pager_swap_pages_free());
4044 p
->p_memstat_state
&= ~P_MEMSTAT_LOCKED
;
4047 if (KERN_SUCCESS
== kr
) {
4048 memorystatus_freeze_entry_t data
= { aPid
, TRUE
, dirty
};
4050 memorystatus_frozen_count
++;
4052 p
->p_memstat_state
|= (P_MEMSTAT_FROZEN
| (shared
? 0: P_MEMSTAT_NORECLAIM
));
4054 if (DEFAULT_FREEZER_IS_ACTIVE
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
4056 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
4057 throttle_intervals
[i
].pageouts
+= dirty
;
4061 memorystatus_freeze_pageouts
+= dirty
;
4062 memorystatus_freeze_count
++;
4066 memorystatus_send_note(kMemorystatusFreezeNote
, &data
, sizeof(data
));
4068 /* Return KERN_SUCESS */
4082 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_FREEZE
) | DBG_FUNC_END
,
4083 memorystatus_available_pages
, aPid
, 0, 0, 0);
4088 static inline boolean_t
4089 memorystatus_can_freeze_processes(void)
4095 if (memorystatus_suspended_count
) {
4096 uint32_t average_resident_pages
, estimated_processes
;
4098 /* Estimate the number of suspended processes we can fit */
4099 average_resident_pages
= memorystatus_suspended_footprint_total
/ memorystatus_suspended_count
;
4100 estimated_processes
= memorystatus_suspended_count
+
4101 ((memorystatus_available_pages
- memorystatus_available_pages_critical
) / average_resident_pages
);
4103 /* If it's predicted that no freeze will occur, lower the threshold temporarily */
4104 if (estimated_processes
<= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
) {
4105 memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_LOW
;
4107 memorystatus_freeze_suspended_threshold
= FREEZE_SUSPENDED_THRESHOLD_DEFAULT
;
4110 MEMORYSTATUS_DEBUG(1, "memorystatus_can_freeze_processes: %d suspended processes, %d average resident pages / process, %d suspended processes estimated\n",
4111 memorystatus_suspended_count
, average_resident_pages
, estimated_processes
);
4113 if ((memorystatus_suspended_count
- memorystatus_frozen_count
) > memorystatus_freeze_suspended_threshold
) {
4128 memorystatus_can_freeze(boolean_t
*memorystatus_freeze_swap_low
)
4130 boolean_t can_freeze
= TRUE
;
4132 /* Only freeze if we're sufficiently low on memory; this holds off freeze right
4133 after boot, and is generally is a no-op once we've reached steady state. */
4134 if (memorystatus_available_pages
> memorystatus_freeze_threshold
) {
4138 /* Check minimum suspended process threshold. */
4139 if (!memorystatus_can_freeze_processes()) {
4143 if (COMPRESSED_PAGER_IS_SWAPLESS
|| DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS
) {
4145 * In-core compressor used for freezing WITHOUT on-disk swap support.
4148 if (vm_compressor_low_on_space()) {
4149 if (*memorystatus_freeze_swap_low
) {
4150 *memorystatus_freeze_swap_low
= TRUE
;
4156 if (*memorystatus_freeze_swap_low
) {
4157 *memorystatus_freeze_swap_low
= FALSE
;
4164 * Freezing WITH on-disk swap support.
4167 if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED
) {
4169 * In-core compressor fronts the swap.
4171 if (vm_swap_low_on_space()) {
4172 if (*memorystatus_freeze_swap_low
) {
4173 *memorystatus_freeze_swap_low
= TRUE
;
4179 } else if (DEFAULT_FREEZER_IS_ACTIVE
) {
4181 * Legacy freeze mode with no compressor support.
4183 if (default_pager_swap_pages_free() < memorystatus_freeze_pages_min
) {
4184 if (*memorystatus_freeze_swap_low
) {
4185 *memorystatus_freeze_swap_low
= TRUE
;
4191 panic("Not a valid freeze configuration.\n");
4199 memorystatus_freeze_update_throttle_interval(mach_timespec_t
*ts
, struct throttle_interval_t
*interval
)
4201 unsigned int freeze_daily_pageouts_max
= memorystatus_freeze_daily_mb_max
* (1024 * 1024 / PAGE_SIZE
);
4202 if (CMP_MACH_TIMESPEC(ts
, &interval
->ts
) >= 0) {
4203 if (!interval
->max_pageouts
) {
4204 interval
->max_pageouts
= (interval
->burst_multiple
* (((uint64_t)interval
->mins
* freeze_daily_pageouts_max
) / (24 * 60)));
4206 printf("memorystatus_freeze_update_throttle_interval: %d minute throttle timeout, resetting\n", interval
->mins
);
4208 interval
->ts
.tv_sec
= interval
->mins
* 60;
4209 interval
->ts
.tv_nsec
= 0;
4210 ADD_MACH_TIMESPEC(&interval
->ts
, ts
);
4211 /* Since we update the throttle stats pre-freeze, adjust for overshoot here */
4212 if (interval
->pageouts
> interval
->max_pageouts
) {
4213 interval
->pageouts
-= interval
->max_pageouts
;
4215 interval
->pageouts
= 0;
4217 interval
->throttle
= FALSE
;
4218 } else if (!interval
->throttle
&& interval
->pageouts
>= interval
->max_pageouts
) {
4219 printf("memorystatus_freeze_update_throttle_interval: %d minute pageout limit exceeded; enabling throttle\n", interval
->mins
);
4220 interval
->throttle
= TRUE
;
4223 MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining; throttle %s\n",
4224 interval
->pageouts
, interval
->max_pageouts
, interval
->mins
, (interval
->ts
.tv_sec
- ts
->tv_sec
) / 60,
4225 interval
->throttle
? "on" : "off");
4229 memorystatus_freeze_update_throttle(void)
4235 boolean_t throttled
= FALSE
;
4237 #if DEVELOPMENT || DEBUG
4238 if (!memorystatus_freeze_throttle_enabled
)
4242 clock_get_system_nanotime(&sec
, &nsec
);
4246 /* Check freeze pageouts over multiple intervals and throttle if we've exceeded our budget.
4248 * This ensures that periods of inactivity can't be used as 'credit' towards freeze if the device has
4249 * remained dormant for a long period. We do, however, allow increased thresholds for shorter intervals in
4250 * order to allow for bursts of activity.
4252 for (i
= 0; i
< sizeof(throttle_intervals
) / sizeof(struct throttle_interval_t
); i
++) {
4253 memorystatus_freeze_update_throttle_interval(&ts
, &throttle_intervals
[i
]);
4254 if (throttle_intervals
[i
].throttle
== TRUE
)
4262 memorystatus_freeze_thread(void *param __unused
, wait_result_t wr __unused
)
4264 static boolean_t memorystatus_freeze_swap_low
= FALSE
;
4266 lck_mtx_lock(&freezer_mutex
);
4267 if (memorystatus_freeze_enabled
) {
4268 if (memorystatus_can_freeze(&memorystatus_freeze_swap_low
)) {
4269 /* Only freeze if we've not exceeded our pageout budgets.*/
4270 if (!memorystatus_freeze_update_throttle()) {
4271 memorystatus_freeze_top_process(&memorystatus_freeze_swap_low
);
4273 printf("memorystatus_freeze_thread: in throttle, ignoring freeze\n");
4274 memorystatus_freeze_throttle_count
++; /* Throttled, update stats */
4278 lck_mtx_unlock(&freezer_mutex
);
4280 assert_wait((event_t
) &memorystatus_freeze_wakeup
, THREAD_UNINT
);
4281 thread_block((thread_continue_t
) memorystatus_freeze_thread
);
4284 #endif /* CONFIG_FREEZE */
4286 #if VM_PRESSURE_EVENTS
4288 #if CONFIG_MEMORYSTATUS
4291 memorystatus_send_note(int event_code
, void *data
, size_t data_length
) {
4293 struct kev_msg ev_msg
;
4295 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
4296 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
4297 ev_msg
.kev_subclass
= KEV_MEMORYSTATUS_SUBCLASS
;
4299 ev_msg
.event_code
= event_code
;
4301 ev_msg
.dv
[0].data_length
= data_length
;
4302 ev_msg
.dv
[0].data_ptr
= data
;
4303 ev_msg
.dv
[1].data_length
= 0;
4305 ret
= kev_post_msg(&ev_msg
);
4307 printf("%s: kev_post_msg() failed, err %d\n", __func__
, ret
);
4314 memorystatus_warn_process(pid_t pid
, boolean_t critical
) {
4316 boolean_t ret
= FALSE
;
4317 boolean_t found_knote
= FALSE
;
4318 struct knote
*kn
= NULL
;
4321 * See comment in sysctl_memorystatus_vm_pressure_send.
4324 memorystatus_klist_lock();
4326 SLIST_FOREACH(kn
, &memorystatus_klist
, kn_selnext
) {
4327 proc_t knote_proc
= kn
->kn_kq
->kq_p
;
4328 pid_t knote_pid
= knote_proc
->p_pid
;
4330 if (knote_pid
== pid
) {
4332 * By setting the "fflags" here, we are forcing
4333 * a process to deal with the case where it's
4334 * bumping up into its memory limits. If we don't
4335 * do this here, we will end up depending on the
4336 * system pressure snapshot evaluation in
4337 * filt_memorystatus().
4341 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
) {
4342 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
;
4343 } else if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_WARN
) {
4344 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
4347 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_WARN
) {
4348 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
4357 KNOTE(&memorystatus_klist
, 0);
4360 if (vm_dispatch_pressure_note_to_pid(pid
, FALSE
) == 0) {
4365 memorystatus_klist_unlock();
4371 * Can only be set by the current task on itself.
4374 memorystatus_low_mem_privileged_listener(uint32_t op_flags
)
4376 boolean_t set_privilege
= FALSE
;
4378 * Need an entitlement check here?
4380 if (op_flags
== MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE
) {
4381 set_privilege
= TRUE
;
4382 } else if (op_flags
== MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE
) {
4383 set_privilege
= FALSE
;
4388 return (task_low_mem_privileged_listener(current_task(), set_privilege
, NULL
));
4392 memorystatus_send_pressure_note(pid_t pid
) {
4393 MEMORYSTATUS_DEBUG(1, "memorystatus_send_pressure_note(): pid %d\n", pid
);
4394 return memorystatus_send_note(kMemorystatusPressureNote
, &pid
, sizeof(pid
));
4398 memorystatus_send_low_swap_note(void) {
4400 struct knote
*kn
= NULL
;
4402 memorystatus_klist_lock();
4403 SLIST_FOREACH(kn
, &memorystatus_klist
, kn_selnext
) {
4404 /* We call is_knote_registered_modify_task_pressure_bits to check if the sfflags for the
4405 * current note contain NOTE_MEMORYSTATUS_LOW_SWAP. Once we find one note in the memorystatus_klist
4406 * that has the NOTE_MEMORYSTATUS_LOW_SWAP flags in its sfflags set, we call KNOTE with
4407 * kMemoryStatusLowSwap as the hint to process and update all knotes on the memorystatus_klist accordingly. */
4408 if (is_knote_registered_modify_task_pressure_bits(kn
, NOTE_MEMORYSTATUS_LOW_SWAP
, NULL
, 0, 0) == TRUE
) {
4409 KNOTE(&memorystatus_klist
, kMemorystatusLowSwap
);
4414 memorystatus_klist_unlock();
4418 memorystatus_bg_pressure_eligible(proc_t p
) {
4419 boolean_t eligible
= FALSE
;
4423 MEMORYSTATUS_DEBUG(1, "memorystatus_bg_pressure_eligible: pid %d, state 0x%x\n", p
->p_pid
, p
->p_memstat_state
);
4425 /* Foreground processes have already been dealt with at this point, so just test for eligibility */
4426 if (!(p
->p_memstat_state
& (P_MEMSTAT_TERMINATED
| P_MEMSTAT_LOCKED
| P_MEMSTAT_SUSPENDED
| P_MEMSTAT_FROZEN
))) {
4436 memorystatus_is_foreground_locked(proc_t p
) {
4437 return ((p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_FOREGROUND
) ||
4438 (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_FOREGROUND_SUPPORT
));
4440 #endif /* CONFIG_MEMORYSTATUS */
4443 * Trigger levels to test the mechanism.
4444 * Can be used via a sysctl.
4446 #define TEST_LOW_MEMORY_TRIGGER_ONE 1
4447 #define TEST_LOW_MEMORY_TRIGGER_ALL 2
4448 #define TEST_PURGEABLE_TRIGGER_ONE 3
4449 #define TEST_PURGEABLE_TRIGGER_ALL 4
4450 #define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE 5
4451 #define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL 6
4453 boolean_t memorystatus_manual_testing_on
= FALSE
;
4454 vm_pressure_level_t memorystatus_manual_testing_level
= kVMPressureNormal
;
4456 extern struct knote
*
4457 vm_pressure_select_optimal_candidate_to_notify(struct klist
*, int, boolean_t
);
4460 kern_return_t
vm_pressure_notification_without_levels(boolean_t
);
4462 extern void vm_pressure_klist_lock(void);
4463 extern void vm_pressure_klist_unlock(void);
4465 extern void vm_reset_active_list(void);
4467 extern void delay(int);
4469 #define INTER_NOTIFICATION_DELAY (250000) /* .25 second */
4471 void memorystatus_on_pageout_scan_end(void) {
4478 * knote_pressure_level - to check if the knote is registered for this notification level.
4480 * task - task whose bits we'll be modifying
4482 * 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.
4484 * pressure_level_to_set - the task is about to be notified of this new level. Update the task's bit notification information appropriately.
4489 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
)
4491 if (kn_max
->kn_sfflags
& knote_pressure_level
) {
4493 if (task_has_been_notified(task
, pressure_level_to_clear
) == TRUE
) {
4495 task_clear_has_been_notified(task
, pressure_level_to_clear
);
4498 task_mark_has_been_notified(task
, pressure_level_to_set
);
4505 extern kern_return_t
vm_pressure_notify_dispatch_vm_clients(boolean_t target_foreground_process
);
4507 #define VM_PRESSURE_DECREASED_SMOOTHING_PERIOD 5000 /* milliseconds */
4510 memorystatus_update_vm_pressure(boolean_t target_foreground_process
)
4512 struct knote
*kn_max
= NULL
;
4513 struct knote
*kn_cur
= NULL
, *kn_temp
= NULL
; /* for safe list traversal */
4514 pid_t target_pid
= -1;
4515 struct klist dispatch_klist
= { NULL
};
4516 proc_t target_proc
= PROC_NULL
;
4517 struct task
*task
= NULL
;
4518 boolean_t found_candidate
= FALSE
;
4520 static vm_pressure_level_t level_snapshot
= kVMPressureNormal
;
4521 static vm_pressure_level_t prev_level_snapshot
= kVMPressureNormal
;
4522 boolean_t smoothing_window_started
= FALSE
;
4523 struct timeval smoothing_window_start_tstamp
= {0, 0};
4524 struct timeval curr_tstamp
= {0, 0};
4525 int elapsed_msecs
= 0;
4528 #define MAX_IDLE_KILLS 100 /* limit the number of idle kills allowed */
4530 int idle_kill_counter
= 0;
4533 * On desktop we take this opportunity to free up memory pressure
4534 * by immediately killing idle exitable processes. We use a delay
4535 * to avoid overkill. And we impose a max counter as a fail safe
4536 * in case daemons re-launch too fast.
4538 while ((memorystatus_vm_pressure_level
!= kVMPressureNormal
) && (idle_kill_counter
< MAX_IDLE_KILLS
)) {
4539 if (memorystatus_idle_exit_from_VM() == FALSE
) {
4540 /* No idle exitable processes left to kill */
4543 idle_kill_counter
++;
4545 if (memorystatus_manual_testing_on
== TRUE
) {
4547 * Skip the delay when testing
4548 * the pressure notification scheme.
4551 delay(1000000); /* 1 second */
4554 #endif /* !CONFIG_JETSAM */
4559 * There is a race window here. But it's not clear
4560 * how much we benefit from having extra synchronization.
4562 level_snapshot
= memorystatus_vm_pressure_level
;
4564 if (prev_level_snapshot
> level_snapshot
) {
4566 * Pressure decreased? Let's take a little breather
4567 * and see if this condition stays.
4569 if (smoothing_window_started
== FALSE
) {
4571 smoothing_window_started
= TRUE
;
4572 microuptime(&smoothing_window_start_tstamp
);
4575 microuptime(&curr_tstamp
);
4576 timevalsub(&curr_tstamp
, &smoothing_window_start_tstamp
);
4577 elapsed_msecs
= curr_tstamp
.tv_sec
* 1000 + curr_tstamp
.tv_usec
/ 1000;
4579 if (elapsed_msecs
< VM_PRESSURE_DECREASED_SMOOTHING_PERIOD
) {
4581 delay(INTER_NOTIFICATION_DELAY
);
4586 prev_level_snapshot
= level_snapshot
;
4587 smoothing_window_started
= FALSE
;
4589 memorystatus_klist_lock();
4590 kn_max
= vm_pressure_select_optimal_candidate_to_notify(&memorystatus_klist
, level_snapshot
, target_foreground_process
);
4592 if (kn_max
== NULL
) {
4593 memorystatus_klist_unlock();
4596 * No more level-based clients to notify.
4597 * Try the non-level based notification clients.
4599 * However, these non-level clients don't understand
4600 * the "return-to-normal" notification.
4602 * So don't consider them for those notifications. Just
4607 if (level_snapshot
!= kVMPressureNormal
) {
4608 goto try_dispatch_vm_clients
;
4610 return KERN_FAILURE
;
4614 target_proc
= kn_max
->kn_kq
->kq_p
;
4617 if (target_proc
!= proc_ref_locked(target_proc
)) {
4618 target_proc
= PROC_NULL
;
4620 memorystatus_klist_unlock();
4625 target_pid
= target_proc
->p_pid
;
4627 task
= (struct task
*)(target_proc
->task
);
4629 if (level_snapshot
!= kVMPressureNormal
) {
4631 if (level_snapshot
== kVMPressureWarning
|| level_snapshot
== kVMPressureUrgent
) {
4633 if (is_knote_registered_modify_task_pressure_bits(kn_max
, NOTE_MEMORYSTATUS_PRESSURE_WARN
, task
, kVMPressureCritical
, kVMPressureWarning
) == TRUE
) {
4634 found_candidate
= TRUE
;
4637 if (level_snapshot
== kVMPressureCritical
) {
4639 if (is_knote_registered_modify_task_pressure_bits(kn_max
, NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
, task
, kVMPressureWarning
, kVMPressureCritical
) == TRUE
) {
4640 found_candidate
= TRUE
;
4645 if (kn_max
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
4647 task_clear_has_been_notified(task
, kVMPressureWarning
);
4648 task_clear_has_been_notified(task
, kVMPressureCritical
);
4650 found_candidate
= TRUE
;
4654 if (found_candidate
== FALSE
) {
4655 proc_rele(target_proc
);
4656 memorystatus_klist_unlock();
4660 SLIST_FOREACH_SAFE(kn_cur
, &memorystatus_klist
, kn_selnext
, kn_temp
) {
4661 proc_t knote_proc
= kn_cur
->kn_kq
->kq_p
;
4662 pid_t knote_pid
= knote_proc
->p_pid
;
4663 if (knote_pid
== target_pid
) {
4664 KNOTE_DETACH(&memorystatus_klist
, kn_cur
);
4665 KNOTE_ATTACH(&dispatch_klist
, kn_cur
);
4669 KNOTE(&dispatch_klist
, (level_snapshot
!= kVMPressureNormal
) ? kMemorystatusPressure
: kMemorystatusNoPressure
);
4671 SLIST_FOREACH_SAFE(kn_cur
, &dispatch_klist
, kn_selnext
, kn_temp
) {
4672 KNOTE_DETACH(&dispatch_klist
, kn_cur
);
4673 KNOTE_ATTACH(&memorystatus_klist
, kn_cur
);
4676 memorystatus_klist_unlock();
4678 microuptime(&target_proc
->vm_pressure_last_notify_tstamp
);
4679 proc_rele(target_proc
);
4681 if (memorystatus_manual_testing_on
== TRUE
&& target_foreground_process
== TRUE
) {
4685 try_dispatch_vm_clients
:
4686 if (kn_max
== NULL
&& level_snapshot
!= kVMPressureNormal
) {
4688 * We will exit this loop when we are done with
4689 * notification clients (level and non-level based).
4691 if ((vm_pressure_notify_dispatch_vm_clients(target_foreground_process
) == KERN_FAILURE
) && (kn_max
== NULL
)) {
4693 * kn_max == NULL i.e. we didn't find any eligible clients for the level-based notifications
4695 * we have failed to find any eligible clients for the non-level based notifications too.
4699 return KERN_FAILURE
;
4704 * LD: This block of code below used to be invoked in the older memory notification scheme on embedded everytime
4705 * a process was sent a memory pressure notification. The "memorystatus_klist" list was used to hold these
4706 * privileged listeners. But now we have moved to the newer scheme and are trying to move away from the extra
4707 * notifications. So the code is here in case we break compat. and need to send out notifications to the privileged
4713 if (memorystatus_manual_testing_on
== TRUE
) {
4715 * Testing out the pressure notification scheme.
4716 * No need for delays etc.
4720 uint32_t sleep_interval
= INTER_NOTIFICATION_DELAY
;
4722 unsigned int page_delta
= 0;
4723 unsigned int skip_delay_page_threshold
= 0;
4725 assert(memorystatus_available_pages_pressure
>= memorystatus_available_pages_critical_base
);
4727 page_delta
= (memorystatus_available_pages_pressure
- memorystatus_available_pages_critical_base
) / 2;
4728 skip_delay_page_threshold
= memorystatus_available_pages_pressure
- page_delta
;
4730 if (memorystatus_available_pages
<= skip_delay_page_threshold
) {
4732 * We are nearing the critcal mark fast and can't afford to wait between
4737 #endif /* CONFIG_JETSAM */
4739 if (sleep_interval
) {
4740 delay(sleep_interval
);
4745 return KERN_SUCCESS
;
4749 convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t
);
4752 convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t internal_pressure_level
)
4754 vm_pressure_level_t dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_NORMAL
;
4756 switch (internal_pressure_level
) {
4758 case kVMPressureNormal
:
4760 dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_NORMAL
;
4764 case kVMPressureWarning
:
4765 case kVMPressureUrgent
:
4767 dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
4771 case kVMPressureCritical
:
4773 dispatch_level
= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
;
4781 return dispatch_level
;
4785 sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS
4787 #pragma unused(arg1, arg2, oidp)
4788 vm_pressure_level_t dispatch_level
= convert_internal_pressure_level_to_dispatch_level(memorystatus_vm_pressure_level
);
4790 return SYSCTL_OUT(req
, &dispatch_level
, sizeof(dispatch_level
));
4793 #if DEBUG || DEVELOPMENT
4795 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_vm_pressure_level
, CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_LOCKED
,
4796 0, 0, &sysctl_memorystatus_vm_pressure_level
, "I", "");
4798 #else /* DEBUG || DEVELOPMENT */
4800 SYSCTL_PROC(_kern
, OID_AUTO
, memorystatus_vm_pressure_level
, CTLTYPE_INT
|CTLFLAG_RD
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
4801 0, 0, &sysctl_memorystatus_vm_pressure_level
, "I", "");
4803 #endif /* DEBUG || DEVELOPMENT */
4805 extern int memorystatus_purge_on_warning
;
4806 extern int memorystatus_purge_on_critical
;
4809 sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS
4811 #pragma unused(arg1, arg2)
4815 int pressure_level
= 0;
4816 int trigger_request
= 0;
4819 error
= sysctl_handle_int(oidp
, &level
, 0, req
);
4820 if (error
|| !req
->newptr
) {
4824 memorystatus_manual_testing_on
= TRUE
;
4826 trigger_request
= (level
>> 16) & 0xFFFF;
4827 pressure_level
= (level
& 0xFFFF);
4829 if (trigger_request
< TEST_LOW_MEMORY_TRIGGER_ONE
||
4830 trigger_request
> TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL
) {
4833 switch (pressure_level
) {
4834 case NOTE_MEMORYSTATUS_PRESSURE_NORMAL
:
4835 case NOTE_MEMORYSTATUS_PRESSURE_WARN
:
4836 case NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
:
4843 * The pressure level is being set from user-space.
4844 * And user-space uses the constants in sys/event.h
4845 * So we translate those events to our internal levels here.
4847 if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
4849 memorystatus_manual_testing_level
= kVMPressureNormal
;
4852 } else if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_WARN
) {
4854 memorystatus_manual_testing_level
= kVMPressureWarning
;
4855 force_purge
= memorystatus_purge_on_warning
;
4857 } else if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
) {
4859 memorystatus_manual_testing_level
= kVMPressureCritical
;
4860 force_purge
= memorystatus_purge_on_critical
;
4863 memorystatus_vm_pressure_level
= memorystatus_manual_testing_level
;
4865 /* purge according to the new pressure level */
4866 switch (trigger_request
) {
4867 case TEST_PURGEABLE_TRIGGER_ONE
:
4868 case TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE
:
4869 if (force_purge
== 0) {
4870 /* no purging requested */
4873 vm_purgeable_object_purge_one_unlocked(force_purge
);
4875 case TEST_PURGEABLE_TRIGGER_ALL
:
4876 case TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL
:
4877 if (force_purge
== 0) {
4878 /* no purging requested */
4881 while (vm_purgeable_object_purge_one_unlocked(force_purge
));
4885 if ((trigger_request
== TEST_LOW_MEMORY_TRIGGER_ONE
) ||
4886 (trigger_request
== TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE
)) {
4888 memorystatus_update_vm_pressure(TRUE
);
4891 if ((trigger_request
== TEST_LOW_MEMORY_TRIGGER_ALL
) ||
4892 (trigger_request
== TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL
)) {
4894 while (memorystatus_update_vm_pressure(FALSE
) == KERN_SUCCESS
) {
4899 if (pressure_level
== NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
4900 memorystatus_manual_testing_on
= FALSE
;
4902 vm_pressure_klist_lock();
4903 vm_reset_active_list();
4904 vm_pressure_klist_unlock();
4907 vm_pressure_klist_lock();
4908 vm_pressure_notification_without_levels(FALSE
);
4909 vm_pressure_klist_unlock();
4915 SYSCTL_PROC(_kern
, OID_AUTO
, memorypressure_manual_trigger
, CTLTYPE_INT
|CTLFLAG_WR
|CTLFLAG_LOCKED
|CTLFLAG_MASKED
,
4916 0, 0, &sysctl_memorypressure_manual_trigger
, "I", "");
4919 extern int memorystatus_purge_on_warning
;
4920 extern int memorystatus_purge_on_urgent
;
4921 extern int memorystatus_purge_on_critical
;
4923 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_purge_on_warning
, CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_purge_on_warning
, 0, "");
4924 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_purge_on_urgent
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_purge_on_urgent
, 0, "");
4925 SYSCTL_INT(_kern
, OID_AUTO
, memorystatus_purge_on_critical
, CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
, &memorystatus_purge_on_critical
, 0, "");
4928 #endif /* VM_PRESSURE_EVENTS */
4930 /* Return both allocated and actual size, since there's a race between allocation and list compilation */
4932 memorystatus_get_priority_list(memorystatus_priority_entry_t
**list_ptr
, size_t *buffer_size
, size_t *list_size
, boolean_t size_only
)
4934 uint32_t list_count
, i
= 0;
4935 memorystatus_priority_entry_t
*list_entry
;
4938 list_count
= memorystatus_list_count
;
4939 *list_size
= sizeof(memorystatus_priority_entry_t
) * list_count
;
4941 /* Just a size check? */
4946 /* Otherwise, validate the size of the buffer */
4947 if (*buffer_size
< *list_size
) {
4951 *list_ptr
= (memorystatus_priority_entry_t
*)kalloc(*list_size
);
4956 memset(*list_ptr
, 0, *list_size
);
4958 *buffer_size
= *list_size
;
4961 list_entry
= *list_ptr
;
4965 p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
4966 while (p
&& (*list_size
< *buffer_size
)) {
4967 list_entry
->pid
= p
->p_pid
;
4968 list_entry
->priority
= p
->p_memstat_effectivepriority
;
4969 list_entry
->user_data
= p
->p_memstat_userdata
;
4973 * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
4974 * Background limits are described via the inactive limit slots.
4975 * So, here, the cached limit should always be valid.
4978 if (p
->p_memstat_memlimit
<= 0) {
4979 task_get_phys_footprint_limit(p
->task
, &list_entry
->limit
);
4981 list_entry
->limit
= p
->p_memstat_memlimit
;
4984 task_get_phys_footprint_limit(p
->task
, &list_entry
->limit
);
4986 list_entry
->state
= memorystatus_build_state(p
);
4989 *list_size
+= sizeof(memorystatus_priority_entry_t
);
4991 p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
4996 MEMORYSTATUS_DEBUG(1, "memorystatus_get_priority_list: returning %lu for size\n", (unsigned long)*list_size
);
5002 memorystatus_cmd_get_priority_list(user_addr_t buffer
, size_t buffer_size
, int32_t *retval
) {
5004 boolean_t size_only
;
5005 memorystatus_priority_entry_t
*list
= NULL
;
5008 size_only
= ((buffer
== USER_ADDR_NULL
) ? TRUE
: FALSE
);
5010 error
= memorystatus_get_priority_list(&list
, &buffer_size
, &list_size
, size_only
);
5016 error
= copyout(list
, buffer
, list_size
);
5020 *retval
= list_size
;
5025 kfree(list
, buffer_size
);
5034 memorystatus_clear_errors(void)
5039 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_CLEAR_ERRORS
) | DBG_FUNC_START
, 0, 0, 0, 0, 0);
5043 p
= memorystatus_get_first_proc_locked(&i
, TRUE
);
5045 if (p
->p_memstat_state
& P_MEMSTAT_ERROR
) {
5046 p
->p_memstat_state
&= ~P_MEMSTAT_ERROR
;
5048 p
= memorystatus_get_next_proc_locked(&i
, p
, TRUE
);
5053 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_CLEAR_ERRORS
) | DBG_FUNC_END
, 0, 0, 0, 0, 0);
5057 memorystatus_update_levels_locked(boolean_t critical_only
) {
5059 memorystatus_available_pages_critical
= memorystatus_available_pages_critical_base
;
5062 * If there's an entry in the first bucket, we have idle processes.
5064 memstat_bucket_t
*first_bucket
= &memstat_bucket
[JETSAM_PRIORITY_IDLE
];
5065 if (first_bucket
->count
) {
5066 memorystatus_available_pages_critical
+= memorystatus_available_pages_critical_idle_offset
;
5068 if (memorystatus_available_pages_critical
> memorystatus_available_pages_pressure
) {
5070 * The critical threshold must never exceed the pressure threshold
5072 memorystatus_available_pages_critical
= memorystatus_available_pages_pressure
;
5076 #if DEBUG || DEVELOPMENT
5077 if (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) {
5078 memorystatus_available_pages_critical
+= memorystatus_jetsam_policy_offset_pages_diagnostic
;
5080 if (memorystatus_available_pages_critical
> memorystatus_available_pages_pressure
) {
5082 * The critical threshold must never exceed the pressure threshold
5084 memorystatus_available_pages_critical
= memorystatus_available_pages_pressure
;
5089 if (critical_only
) {
5093 #if VM_PRESSURE_EVENTS
5094 memorystatus_available_pages_pressure
= (pressure_threshold_percentage
/ delta_percentage
) * memorystatus_delta
;
5095 #if DEBUG || DEVELOPMENT
5096 if (memorystatus_jetsam_policy
& kPolicyDiagnoseActive
) {
5097 memorystatus_available_pages_pressure
+= memorystatus_jetsam_policy_offset_pages_diagnostic
;
5104 * Get the at_boot snapshot
5107 memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t
**snapshot
, size_t *snapshot_size
, boolean_t size_only
) {
5108 size_t input_size
= *snapshot_size
;
5111 * The at_boot snapshot has no entry list.
5113 *snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
);
5120 * Validate the size of the snapshot buffer
5122 if (input_size
< *snapshot_size
) {
5127 * Update the notification_time only
5129 memorystatus_at_boot_snapshot
.notification_time
= mach_absolute_time();
5130 *snapshot
= &memorystatus_at_boot_snapshot
;
5132 MEMORYSTATUS_DEBUG(7, "memorystatus_get_at_boot_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%d)\n",
5133 (long)input_size
, (long)*snapshot_size
, 0);
5138 memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t
**snapshot
, size_t *snapshot_size
, boolean_t size_only
) {
5139 size_t input_size
= *snapshot_size
;
5140 uint32_t ods_list_count
= memorystatus_list_count
;
5141 memorystatus_jetsam_snapshot_t
*ods
= NULL
; /* The on_demand snapshot buffer */
5143 *snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) + (sizeof(memorystatus_jetsam_snapshot_entry_t
) * (ods_list_count
));
5150 * Validate the size of the snapshot buffer.
5151 * This is inherently racey. May want to revisit
5152 * this error condition and trim the output when
5155 if (input_size
< *snapshot_size
) {
5160 * Allocate and initialize a snapshot buffer.
5162 ods
= (memorystatus_jetsam_snapshot_t
*)kalloc(*snapshot_size
);
5167 memset(ods
, 0, *snapshot_size
);
5170 memorystatus_init_jetsam_snapshot_locked(ods
, ods_list_count
);
5174 * Return the kernel allocated, on_demand buffer.
5175 * The caller of this routine will copy the data out
5176 * to user space and then free the kernel allocated
5181 MEMORYSTATUS_DEBUG(7, "memorystatus_get_on_demand_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
5182 (long)input_size
, (long)*snapshot_size
, (long)ods_list_count
);
5188 memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t
**snapshot
, size_t *snapshot_size
, boolean_t size_only
) {
5189 size_t input_size
= *snapshot_size
;
5191 if (memorystatus_jetsam_snapshot_count
> 0) {
5192 *snapshot_size
= sizeof(memorystatus_jetsam_snapshot_t
) + (sizeof(memorystatus_jetsam_snapshot_entry_t
) * (memorystatus_jetsam_snapshot_count
));
5201 if (input_size
< *snapshot_size
) {
5205 *snapshot
= memorystatus_jetsam_snapshot
;
5207 MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
5208 (long)input_size
, (long)*snapshot_size
, (long)memorystatus_jetsam_snapshot_count
);
5215 memorystatus_cmd_get_jetsam_snapshot(int32_t flags
, user_addr_t buffer
, size_t buffer_size
, int32_t *retval
) {
5217 boolean_t size_only
;
5218 boolean_t is_default_snapshot
= FALSE
;
5219 boolean_t is_on_demand_snapshot
= FALSE
;
5220 boolean_t is_at_boot_snapshot
= FALSE
;
5221 memorystatus_jetsam_snapshot_t
*snapshot
;
5223 size_only
= ((buffer
== USER_ADDR_NULL
) ? TRUE
: FALSE
);
5227 is_default_snapshot
= TRUE
;
5228 error
= memorystatus_get_jetsam_snapshot(&snapshot
, &buffer_size
, size_only
);
5230 if (flags
& ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND
| MEMORYSTATUS_SNAPSHOT_AT_BOOT
)) {
5232 * Unsupported bit set in flag.
5237 if ((flags
& (MEMORYSTATUS_SNAPSHOT_ON_DEMAND
| MEMORYSTATUS_SNAPSHOT_AT_BOOT
)) ==
5238 (MEMORYSTATUS_SNAPSHOT_ON_DEMAND
| MEMORYSTATUS_SNAPSHOT_AT_BOOT
)) {
5240 * Can't have both set at the same time.
5245 if (flags
& MEMORYSTATUS_SNAPSHOT_ON_DEMAND
) {
5246 is_on_demand_snapshot
= TRUE
;
5248 * When not requesting the size only, the following call will allocate
5249 * an on_demand snapshot buffer, which is freed below.
5251 error
= memorystatus_get_on_demand_snapshot(&snapshot
, &buffer_size
, size_only
);
5253 } else if (flags
& MEMORYSTATUS_SNAPSHOT_AT_BOOT
) {
5254 is_at_boot_snapshot
= TRUE
;
5255 error
= memorystatus_get_at_boot_snapshot(&snapshot
, &buffer_size
, size_only
);
5258 * Invalid flag setting.
5269 * Copy the data out to user space and clear the snapshot buffer.
5270 * If working with the jetsam snapshot,
5271 * clearing the buffer means, reset the count.
5272 * If working with an on_demand snapshot
5273 * clearing the buffer means, free it.
5274 * If working with the at_boot snapshot
5275 * there is nothing to clear or update.
5278 if ((error
= copyout(snapshot
, buffer
, buffer_size
)) == 0) {
5279 if (is_default_snapshot
) {
5281 * The jetsam snapshot is never freed, its count is simply reset.
5283 snapshot
->entry_count
= memorystatus_jetsam_snapshot_count
= 0;
5286 memorystatus_jetsam_snapshot_last_timestamp
= 0;
5291 if (is_on_demand_snapshot
) {
5293 * The on_demand snapshot is always freed,
5294 * even if the copyout failed.
5297 kfree(snapshot
, buffer_size
);
5303 *retval
= buffer_size
;
5310 * Routine: memorystatus_cmd_grp_set_properties
5311 * Purpose: Update properties for a group of processes.
5313 * Supported Properties:
5315 * Move each process out of its effective priority
5316 * band and into a new priority band.
5317 * Maintains relative order from lowest to highest priority.
5318 * In single band, maintains relative order from head to tail.
5320 * eg: before [effectivepriority | pid]
5322 * [17 | p55, p67, p19 ]
5327 * after [ new band | pid]
5328 * [ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101]
5330 * Returns: 0 on success, else non-zero.
5332 * Caveat: We know there is a race window regarding recycled pids.
5333 * A process could be killed before the kernel can act on it here.
5334 * If a pid cannot be found in any of the jetsam priority bands,
5335 * then we simply ignore it. No harm.
5336 * But, if the pid has been recycled then it could be an issue.
5337 * In that scenario, we might move an unsuspecting process to the new
5338 * priority band. It's not clear how the kernel can safeguard
5339 * against this, but it would be an extremely rare case anyway.
5340 * The caller of this api might avoid such race conditions by
5341 * ensuring that the processes passed in the pid list are suspended.
5345 /* This internal structure can expand when we add support for more properties */
5346 typedef struct memorystatus_internal_properties
5349 int32_t priority
; /* see memorytstatus_priority_entry_t : priority */
5350 } memorystatus_internal_properties_t
;
5354 memorystatus_cmd_grp_set_properties(int32_t flags
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
) {
5356 #pragma unused (flags)
5359 * We only handle setting priority
5364 memorystatus_priority_entry_t
*entries
= NULL
;
5365 uint32_t entry_count
= 0;
5367 /* This will be the ordered proc list */
5368 memorystatus_internal_properties_t
*table
= NULL
;
5369 size_t table_size
= 0;
5370 uint32_t table_count
= 0;
5373 uint32_t bucket_index
= 0;
5374 boolean_t head_insert
;
5375 int32_t new_priority
;
5380 if ((buffer
== USER_ADDR_NULL
) || (buffer_size
== 0) || ((buffer_size
% sizeof(memorystatus_priority_entry_t
)) != 0)) {
5385 entry_count
= (buffer_size
/ sizeof(memorystatus_priority_entry_t
));
5386 if ((entries
= (memorystatus_priority_entry_t
*)kalloc(buffer_size
)) == NULL
) {
5391 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_GRP_SET_PROP
) | DBG_FUNC_START
, entry_count
, 0, 0, 0, 0);
5393 if ((error
= copyin(buffer
, entries
, buffer_size
)) != 0) {
5397 /* Verify sanity of input priorities */
5398 for (i
=0; i
< entry_count
; i
++) {
5399 if (entries
[i
].priority
== -1) {
5400 /* Use as shorthand for default priority */
5401 entries
[i
].priority
= JETSAM_PRIORITY_DEFAULT
;
5402 } else if (entries
[i
].priority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
5403 /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use;
5404 * if requested, adjust to JETSAM_PRIORITY_IDLE. */
5405 entries
[i
].priority
= JETSAM_PRIORITY_IDLE
;
5406 } else if (entries
[i
].priority
== JETSAM_PRIORITY_IDLE_HEAD
) {
5407 /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle
5409 /* Deal with this later */
5410 } else if ((entries
[i
].priority
< 0) || (entries
[i
].priority
>= MEMSTAT_BUCKET_COUNT
)) {
5417 table_size
= sizeof(memorystatus_internal_properties_t
) * entry_count
;
5418 if ( (table
= (memorystatus_internal_properties_t
*)kalloc(table_size
)) == NULL
) {
5422 memset(table
, 0, table_size
);
5426 * For each jetsam bucket entry, spin through the input property list.
5427 * When a matching pid is found, populate an adjacent table with the
5428 * appropriate proc pointer and new property values.
5429 * This traversal automatically preserves order from lowest
5430 * to highest priority.
5437 /* Create the ordered table */
5438 p
= memorystatus_get_first_proc_locked(&bucket_index
, TRUE
);
5439 while (p
&& (table_count
< entry_count
)) {
5440 for (i
=0; i
< entry_count
; i
++ ) {
5441 if (p
->p_pid
== entries
[i
].pid
) {
5442 /* Build the table data */
5443 table
[table_count
].proc
= p
;
5444 table
[table_count
].priority
= entries
[i
].priority
;
5449 p
= memorystatus_get_next_proc_locked(&bucket_index
, p
, TRUE
);
5452 /* We now have ordered list of procs ready to move */
5453 for (i
=0; i
< table_count
; i
++) {
5457 /* Allow head inserts -- but relative order is now */
5458 if (table
[i
].priority
== JETSAM_PRIORITY_IDLE_HEAD
) {
5459 new_priority
= JETSAM_PRIORITY_IDLE
;
5462 new_priority
= table
[i
].priority
;
5463 head_insert
= false;
5467 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
5472 * Take appropriate steps if moving proc out of the
5473 * JETSAM_PRIORITY_IDLE_DEFERRED band.
5475 if (p
->p_memstat_effectivepriority
== JETSAM_PRIORITY_IDLE_DEFERRED
) {
5476 memorystatus_invalidate_idle_demotion_locked(p
, TRUE
);
5479 memorystatus_update_priority_locked(p
, new_priority
, head_insert
);
5485 * if (table_count != entry_count)
5486 * then some pids were not found in a jetsam band.
5487 * harmless but interesting...
5489 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT
, BSD_MEMSTAT_GRP_SET_PROP
) | DBG_FUNC_END
, entry_count
, table_count
, 0, 0, 0);
5493 kfree(entries
, buffer_size
);
5495 kfree(table
, table_size
);
5502 * This routine is used to update a process's jetsam priority position and stored user_data.
5503 * It is not used for the setting of memory limits, which is why the last 6 args to the
5504 * memorystatus_update() call are 0 or FALSE.
5508 memorystatus_cmd_set_priority_properties(pid_t pid
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
) {
5510 memorystatus_priority_properties_t mpp_entry
;
5512 /* Validate inputs */
5513 if ((pid
== 0) || (buffer
== USER_ADDR_NULL
) || (buffer_size
!= sizeof(memorystatus_priority_properties_t
))) {
5517 error
= copyin(buffer
, &mpp_entry
, buffer_size
);
5527 if (p
->p_memstat_state
& P_MEMSTAT_INTERNAL
) {
5532 error
= memorystatus_update(p
, mpp_entry
.priority
, mpp_entry
.user_data
, FALSE
, FALSE
, 0, 0, FALSE
, FALSE
, FALSE
);
5540 memorystatus_cmd_set_memlimit_properties(pid_t pid
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
) {
5542 memorystatus_memlimit_properties_t mmp_entry
;
5544 /* Validate inputs */
5545 if ((pid
== 0) || (buffer
== USER_ADDR_NULL
) || (buffer_size
!= sizeof(memorystatus_memlimit_properties_t
))) {
5549 error
= copyin(buffer
, &mmp_entry
, buffer_size
);
5552 error
= memorystatus_set_memlimit_properties(pid
, &mmp_entry
);
5559 * When getting the memlimit settings, we can't simply call task_get_phys_footprint_limit().
5560 * That gets the proc's cached memlimit and there is no guarantee that the active/inactive
5561 * limits will be the same in the no-limit case. Instead we convert limits <= 0 using
5562 * task_convert_phys_footprint_limit(). It computes the same limit value that would be written
5563 * to the task's ledgers via task_set_phys_footprint_limit().
5566 memorystatus_cmd_get_memlimit_properties(pid_t pid
, user_addr_t buffer
, size_t buffer_size
, __unused
int32_t *retval
) {
5568 memorystatus_memlimit_properties_t mmp_entry
;
5570 /* Validate inputs */
5571 if ((pid
== 0) || (buffer
== USER_ADDR_NULL
) || (buffer_size
!= sizeof(memorystatus_memlimit_properties_t
))) {
5575 memset (&mmp_entry
, 0, sizeof(memorystatus_memlimit_properties_t
));
5577 proc_t p
= proc_find(pid
);
5583 * Get the active limit and attributes.
5584 * No locks taken since we hold a reference to the proc.
5587 if (p
->p_memstat_memlimit_active
> 0 ) {
5588 mmp_entry
.memlimit_active
= p
->p_memstat_memlimit_active
;
5590 task_convert_phys_footprint_limit(-1, &mmp_entry
.memlimit_active
);
5593 if (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL
) {
5594 mmp_entry
.memlimit_active_attr
|= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
;
5598 * Get the inactive limit and attributes
5600 if (p
->p_memstat_memlimit_inactive
<= 0) {
5601 task_convert_phys_footprint_limit(-1, &mmp_entry
.memlimit_inactive
);
5603 mmp_entry
.memlimit_inactive
= p
->p_memstat_memlimit_inactive
;
5605 if (p
->p_memstat_state
& P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL
) {
5606 mmp_entry
.memlimit_inactive_attr
|= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
;
5610 error
= copyout(&mmp_entry
, buffer
, buffer_size
);
5617 memorystatus_cmd_get_pressure_status(int32_t *retval
) {
5620 /* Need privilege for check */
5621 error
= priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE
, 0);
5626 /* Inherently racy, so it's not worth taking a lock here */
5627 *retval
= (kVMPressureNormal
!= memorystatus_vm_pressure_level
) ? 1 : 0;
5633 memorystatus_get_pressure_status_kdp() {
5634 return (kVMPressureNormal
!= memorystatus_vm_pressure_level
) ? 1 : 0;
5638 * Every process, including a P_MEMSTAT_INTERNAL process (currently only pid 1), is allowed to set a HWM.
5640 * This call is inflexible -- it does not distinguish between active/inactive, fatal/non-fatal
5641 * So, with 2-level HWM preserving previous behavior will map as follows.
5642 * - treat the limit passed in as both an active and inactive limit.
5643 * - treat the is_fatal_limit flag as though it applies to both active and inactive limits.
5645 * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK
5646 * - the is_fatal_limit is FALSE, meaning the active and inactive limits are non-fatal/soft
5647 * - so mapping is (active/non-fatal, inactive/non-fatal)
5649 * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT
5650 * - the is_fatal_limit is TRUE, meaning the process's active and inactive limits are fatal/hard
5651 * - so mapping is (active/fatal, inactive/fatal)
5655 memorystatus_cmd_set_jetsam_memory_limit(pid_t pid
, int32_t high_water_mark
, __unused
int32_t *retval
, boolean_t is_fatal_limit
) {
5657 memorystatus_memlimit_properties_t entry
;
5659 entry
.memlimit_active
= high_water_mark
;
5660 entry
.memlimit_active_attr
= 0;
5661 entry
.memlimit_inactive
= high_water_mark
;
5662 entry
.memlimit_inactive_attr
= 0;
5664 if (is_fatal_limit
== TRUE
) {
5665 entry
.memlimit_active_attr
|= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
;
5666 entry
.memlimit_inactive_attr
|= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
;
5669 error
= memorystatus_set_memlimit_properties(pid
, &entry
);
5674 memorystatus_set_memlimit_properties(pid_t pid
, memorystatus_memlimit_properties_t
*entry
) {
5676 int32_t memlimit_active
;
5677 boolean_t memlimit_active_is_fatal
;
5678 int32_t memlimit_inactive
;
5679 boolean_t memlimit_inactive_is_fatal
;
5680 uint32_t valid_attrs
= 0;
5683 proc_t p
= proc_find(pid
);
5689 * Check for valid attribute flags.
5691 valid_attrs
|= (MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
);
5692 if ((entry
->memlimit_active_attr
& (~valid_attrs
)) != 0) {
5696 if ((entry
->memlimit_inactive_attr
& (~valid_attrs
)) != 0) {
5702 * Setup the active memlimit properties
5704 memlimit_active
= entry
->memlimit_active
;
5705 if (entry
->memlimit_active_attr
& MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
) {
5706 memlimit_active_is_fatal
= TRUE
;
5708 memlimit_active_is_fatal
= FALSE
;
5712 * Setup the inactive memlimit properties
5714 memlimit_inactive
= entry
->memlimit_inactive
;
5715 if (entry
->memlimit_inactive_attr
& MEMORYSTATUS_MEMLIMIT_ATTR_FATAL
) {
5716 memlimit_inactive_is_fatal
= TRUE
;
5718 memlimit_inactive_is_fatal
= FALSE
;
5722 * Setting a limit of <= 0 implies that the process has no
5723 * high-water-mark and has no per-task-limit. That means
5724 * the system_wide task limit is in place, which by the way,
5728 if (memlimit_active
<= 0) {
5730 * Enforce the fatal system_wide task limit while process is active.
5732 memlimit_active
= -1;
5733 memlimit_active_is_fatal
= TRUE
;
5736 if (memlimit_inactive
<= 0) {
5738 * Enforce the fatal system_wide task limit while process is inactive.
5740 memlimit_inactive
= -1;
5741 memlimit_inactive_is_fatal
= TRUE
;
5747 * Store the active limit variants in the proc.
5749 SET_ACTIVE_LIMITS_LOCKED(p
, memlimit_active
, memlimit_active_is_fatal
);
5752 * Store the inactive limit variants in the proc.
5754 SET_INACTIVE_LIMITS_LOCKED(p
, memlimit_inactive
, memlimit_inactive_is_fatal
);
5757 * Enforce appropriate limit variant by updating the cached values
5758 * and writing the ledger.
5759 * Limit choice is based on process active/inactive state.
5762 if (memorystatus_highwater_enabled
) {
5763 boolean_t trigger_exception
;
5765 * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
5766 * Background limits are described via the inactive limit slots.
5769 if (proc_jetsam_state_is_active_locked(p
) == TRUE
) {
5770 CACHE_ACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
5772 CACHE_INACTIVE_LIMITS_LOCKED(p
, trigger_exception
);
5775 /* Enforce the limit by writing to the ledgers */
5776 assert(trigger_exception
== TRUE
);
5777 error
= (task_set_phys_footprint_limit_internal(p
->task
, ((p
->p_memstat_memlimit
> 0) ? p
->p_memstat_memlimit
: -1), NULL
, trigger_exception
) == 0) ? 0 : EINVAL
;
5779 MEMORYSTATUS_DEBUG(3, "memorystatus_set_memlimit_properties: new limit on pid %d (%dMB %s) current priority (%d) dirty_state?=0x%x %s\n",
5780 p
->p_pid
, (p
->p_memstat_memlimit
> 0 ? p
->p_memstat_memlimit
: -1),
5781 (p
->p_memstat_state
& P_MEMSTAT_FATAL_MEMLIMIT
? "F " : "NF"), p
->p_memstat_effectivepriority
, p
->p_memstat_dirty
,
5782 (p
->p_memstat_dirty
? ((p
->p_memstat_dirty
& P_DIRTY
) ? "isdirty" : "isclean") : ""));
5792 * Returns the jetsam priority (effective or requested) of the process
5793 * associated with this task.
5796 proc_get_memstat_priority(proc_t p
, boolean_t effective_priority
)
5799 if (effective_priority
) {
5800 return p
->p_memstat_effectivepriority
;
5802 return p
->p_memstat_requestedpriority
;
5810 * Evaluates active vs. inactive process state.
5811 * Processes that opt into dirty tracking are evaluated
5812 * based on clean vs dirty state.
5814 * clean ==> inactive
5816 * Process that do not opt into dirty tracking are
5817 * evalulated based on priority level.
5818 * Foreground or above ==> active
5819 * Below Foreground ==> inactive
5821 * Return: TRUE if active
5826 proc_jetsam_state_is_active_locked(proc_t p
) {
5828 if (p
->p_memstat_dirty
& P_DIRTY_TRACK
) {
5830 * process has opted into dirty tracking
5831 * active state is based on dirty vs. clean
5833 if (p
->p_memstat_dirty
& P_DIRTY_IS_DIRTY
) {
5836 * implies active state
5842 * implies inactive state
5846 } else if (p
->p_memstat_effectivepriority
>= JETSAM_PRIORITY_FOREGROUND
) {
5848 * process is Foreground or higher
5849 * implies active state
5854 * process found below Foreground
5855 * implies inactive state
5861 #endif /* CONFIG_JETSAM */
5864 memorystatus_control(struct proc
*p __unused
, struct memorystatus_control_args
*args
, int *ret
) {
5871 /* Root only for now */
5872 if (!kauth_cred_issuser(kauth_cred_get())) {
5878 if (args
->buffersize
> MEMORYSTATUS_BUFFERSIZE_MAX
) {
5883 switch (args
->command
) {
5884 case MEMORYSTATUS_CMD_GET_PRIORITY_LIST
:
5885 error
= memorystatus_cmd_get_priority_list(args
->buffer
, args
->buffersize
, ret
);
5888 case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
:
5889 error
= memorystatus_cmd_set_priority_properties(args
->pid
, args
->buffer
, args
->buffersize
, ret
);
5891 case MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
:
5892 error
= memorystatus_cmd_set_memlimit_properties(args
->pid
, args
->buffer
, args
->buffersize
, ret
);
5894 case MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
:
5895 error
= memorystatus_cmd_get_memlimit_properties(args
->pid
, args
->buffer
, args
->buffersize
, ret
);
5897 case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES
:
5898 error
= memorystatus_cmd_grp_set_properties((int32_t)args
->flags
, args
->buffer
, args
->buffersize
, ret
);
5900 case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
:
5901 error
= memorystatus_cmd_get_jetsam_snapshot((int32_t)args
->flags
, args
->buffer
, args
->buffersize
, ret
);
5903 case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS
:
5904 error
= memorystatus_cmd_get_pressure_status(ret
);
5906 case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK
:
5908 * This call does not distinguish between active and inactive limits.
5909 * Default behavior in 2-level HWM world is to set both.
5910 * Non-fatal limit is also assumed for both.
5912 error
= memorystatus_cmd_set_jetsam_memory_limit(args
->pid
, (int32_t)args
->flags
, ret
, FALSE
);
5914 case MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT
:
5916 * This call does not distinguish between active and inactive limits.
5917 * Default behavior in 2-level HWM world is to set both.
5918 * Fatal limit is also assumed for both.
5920 error
= memorystatus_cmd_set_jetsam_memory_limit(args
->pid
, (int32_t)args
->flags
, ret
, TRUE
);
5923 #if DEVELOPMENT || DEBUG
5924 case MEMORYSTATUS_CMD_TEST_JETSAM
:
5925 error
= memorystatus_kill_process_sync(args
->pid
, kMemorystatusKilled
) ? 0 : EINVAL
;
5927 case MEMORYSTATUS_CMD_TEST_JETSAM_SORT
:
5928 error
= memorystatus_cmd_test_jetsam_sort(args
->pid
, (int32_t)args
->flags
);
5930 case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS
:
5931 error
= memorystatus_cmd_set_panic_bits(args
->buffer
, args
->buffersize
);
5933 #endif /* DEVELOPMENT || DEBUG */
5934 case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_ENABLE
:
5935 if (memorystatus_aggressive_jetsam_lenient_allowed
== FALSE
) {
5936 #if DEVELOPMENT || DEBUG
5937 printf("Enabling Lenient Mode\n");
5938 #endif /* DEVELOPMENT || DEBUG */
5940 memorystatus_aggressive_jetsam_lenient_allowed
= TRUE
;
5941 memorystatus_aggressive_jetsam_lenient
= TRUE
;
5944 case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE
:
5945 #if DEVELOPMENT || DEBUG
5946 printf("Disabling Lenient mode\n");
5947 #endif /* DEVELOPMENT || DEBUG */
5948 memorystatus_aggressive_jetsam_lenient_allowed
= FALSE
;
5949 memorystatus_aggressive_jetsam_lenient
= FALSE
;
5951 #endif /* CONFIG_JETSAM */
5952 case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE
:
5953 case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE
:
5954 error
= memorystatus_low_mem_privileged_listener(args
->command
);
5966 filt_memorystatusattach(struct knote
*kn
)
5968 kn
->kn_flags
|= EV_CLEAR
;
5969 return memorystatus_knote_register(kn
);
5973 filt_memorystatusdetach(struct knote
*kn
)
5975 memorystatus_knote_unregister(kn
);
5979 filt_memorystatus(struct knote
*kn __unused
, long hint
)
5983 case kMemorystatusNoPressure
:
5984 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_NORMAL
) {
5985 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_NORMAL
;
5988 case kMemorystatusPressure
:
5989 if (memorystatus_vm_pressure_level
== kVMPressureWarning
|| memorystatus_vm_pressure_level
== kVMPressureUrgent
) {
5990 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_WARN
) {
5991 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_WARN
;
5993 } else if (memorystatus_vm_pressure_level
== kVMPressureCritical
) {
5995 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
) {
5996 kn
->kn_fflags
= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
;
6000 case kMemorystatusLowSwap
:
6001 if (kn
->kn_sfflags
& NOTE_MEMORYSTATUS_LOW_SWAP
) {
6002 kn
->kn_fflags
= NOTE_MEMORYSTATUS_LOW_SWAP
;
6010 return (kn
->kn_fflags
!= 0);
6014 memorystatus_klist_lock(void) {
6015 lck_mtx_lock(&memorystatus_klist_mutex
);
6019 memorystatus_klist_unlock(void) {
6020 lck_mtx_unlock(&memorystatus_klist_mutex
);
6024 memorystatus_kevent_init(lck_grp_t
*grp
, lck_attr_t
*attr
) {
6025 lck_mtx_init(&memorystatus_klist_mutex
, grp
, attr
);
6026 klist_init(&memorystatus_klist
);
6030 memorystatus_knote_register(struct knote
*kn
) {
6033 memorystatus_klist_lock();
6035 if (kn
->kn_sfflags
& (NOTE_MEMORYSTATUS_PRESSURE_NORMAL
| NOTE_MEMORYSTATUS_PRESSURE_WARN
| NOTE_MEMORYSTATUS_PRESSURE_CRITICAL
| NOTE_MEMORYSTATUS_LOW_SWAP
)) {
6037 KNOTE_ATTACH(&memorystatus_klist
, kn
);
6043 memorystatus_klist_unlock();
6049 memorystatus_knote_unregister(struct knote
*kn __unused
) {
6050 memorystatus_klist_lock();
6051 KNOTE_DETACH(&memorystatus_klist
, kn
);
6052 memorystatus_klist_unlock();
6057 #if CONFIG_JETSAM && VM_PRESSURE_EVENTS
6059 memorystatus_issue_pressure_kevent(boolean_t pressured
) {
6060 memorystatus_klist_lock();
6061 KNOTE(&memorystatus_klist
, pressured
? kMemorystatusPressure
: kMemorystatusNoPressure
);
6062 memorystatus_klist_unlock();
6065 #endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */
6069 /* Coalition support */
6071 /* sorting info for a particular priority bucket */
6072 typedef struct memstat_sort_info
{
6073 coalition_t msi_coal
;
6074 uint64_t msi_page_count
;
6077 } memstat_sort_info_t
;
6080 * qsort from smallest page count to largest page count
6082 * return < 0 for a < b
6086 static int memstat_asc_cmp(const void *a
, const void *b
)
6088 const memstat_sort_info_t
*msA
= (const memstat_sort_info_t
*)a
;
6089 const memstat_sort_info_t
*msB
= (const memstat_sort_info_t
*)b
;
6091 return (int)((uint64_t)msA
->msi_page_count
- (uint64_t)msB
->msi_page_count
);
6095 * Return the number of pids rearranged during this sort.
6098 memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index
, int coal_sort_order
)
6100 #define MAX_SORT_PIDS 80
6101 #define MAX_COAL_LEADERS 10
6103 unsigned int b
= bucket_index
;
6107 coalition_t coal
= COALITION_NULL
;
6109 int total_pids_moved
= 0;
6113 * The system is typically under memory pressure when in this
6114 * path, hence, we want to avoid dynamic memory allocation.
6116 memstat_sort_info_t leaders
[MAX_COAL_LEADERS
];
6117 pid_t pid_list
[MAX_SORT_PIDS
];
6119 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
6124 * Clear the array that holds coalition leader information
6126 for (i
=0; i
< MAX_COAL_LEADERS
; i
++) {
6127 leaders
[i
].msi_coal
= COALITION_NULL
;
6128 leaders
[i
].msi_page_count
= 0; /* will hold total coalition page count */
6129 leaders
[i
].msi_pid
= 0; /* will hold coalition leader pid */
6130 leaders
[i
].msi_ntasks
= 0; /* will hold the number of tasks in a coalition */
6133 p
= memorystatus_get_first_proc_locked(&b
, FALSE
);
6135 if (coalition_is_leader(p
->task
, COALITION_TYPE_JETSAM
, &coal
)) {
6136 if (nleaders
< MAX_COAL_LEADERS
) {
6137 int coal_ntasks
= 0;
6138 uint64_t coal_page_count
= coalition_get_page_count(coal
, &coal_ntasks
);
6139 leaders
[nleaders
].msi_coal
= coal
;
6140 leaders
[nleaders
].msi_page_count
= coal_page_count
;
6141 leaders
[nleaders
].msi_pid
= p
->p_pid
; /* the coalition leader */
6142 leaders
[nleaders
].msi_ntasks
= coal_ntasks
;
6146 * We've hit MAX_COAL_LEADERS meaning we can handle no more coalitions.
6147 * Abandoned coalitions will linger at the tail of the priority band
6148 * when this sort session ends.
6149 * TODO: should this be an assert?
6151 printf("%s: WARNING: more than %d leaders in priority band [%d]\n",
6152 __FUNCTION__
, MAX_COAL_LEADERS
, bucket_index
);
6156 p
=memorystatus_get_next_proc_locked(&b
, p
, FALSE
);
6159 if (nleaders
== 0) {
6160 /* Nothing to sort */
6165 * Sort the coalition leader array, from smallest coalition page count
6166 * to largest coalition page count. When inserted in the priority bucket,
6167 * smallest coalition is handled first, resulting in the last to be jetsammed.
6170 qsort(leaders
, nleaders
, sizeof(memstat_sort_info_t
), memstat_asc_cmp
);
6174 for (i
= 0; i
< nleaders
; i
++) {
6175 printf("%s: coal_leader[%d of %d] pid[%d] pages[%llu] ntasks[%d]\n",
6176 __FUNCTION__
, i
, nleaders
, leaders
[i
].msi_pid
, leaders
[i
].msi_page_count
,
6177 leaders
[i
].msi_ntasks
);
6182 * During coalition sorting, processes in a priority band are rearranged
6183 * by being re-inserted at the head of the queue. So, when handling a
6184 * list, the first process that gets moved to the head of the queue,
6185 * ultimately gets pushed toward the queue tail, and hence, jetsams last.
6187 * So, for example, the coalition leader is expected to jetsam last,
6188 * after its coalition members. Therefore, the coalition leader is
6189 * inserted at the head of the queue first.
6191 * After processing a coalition, the jetsam order is as follows:
6192 * undefs(jetsam first), extensions, xpc services, leader(jetsam last)
6196 * Coalition members are rearranged in the priority bucket here,
6197 * based on their coalition role.
6199 total_pids_moved
= 0;
6200 for (i
=0; i
< nleaders
; i
++) {
6202 /* a bit of bookkeeping */
6205 /* Coalition leaders are jetsammed last, so move into place first */
6206 pid_list
[0] = leaders
[i
].msi_pid
;
6207 pids_moved
+= memorystatus_move_list_locked(bucket_index
, pid_list
, 1);
6209 /* xpc services should jetsam after extensions */
6210 ntasks
= coalition_get_pid_list (leaders
[i
].msi_coal
, COALITION_ROLEMASK_XPC
,
6211 coal_sort_order
, pid_list
, MAX_SORT_PIDS
);
6214 pids_moved
+= memorystatus_move_list_locked(bucket_index
, pid_list
,
6215 (ntasks
<= MAX_SORT_PIDS
? ntasks
: MAX_SORT_PIDS
));
6218 /* extensions should jetsam after unmarked processes */
6219 ntasks
= coalition_get_pid_list (leaders
[i
].msi_coal
, COALITION_ROLEMASK_EXT
,
6220 coal_sort_order
, pid_list
, MAX_SORT_PIDS
);
6223 pids_moved
+= memorystatus_move_list_locked(bucket_index
, pid_list
,
6224 (ntasks
<= MAX_SORT_PIDS
? ntasks
: MAX_SORT_PIDS
));
6227 /* undefined coalition members should be the first to jetsam */
6228 ntasks
= coalition_get_pid_list (leaders
[i
].msi_coal
, COALITION_ROLEMASK_UNDEF
,
6229 coal_sort_order
, pid_list
, MAX_SORT_PIDS
);
6232 pids_moved
+= memorystatus_move_list_locked(bucket_index
, pid_list
,
6233 (ntasks
<= MAX_SORT_PIDS
? ntasks
: MAX_SORT_PIDS
));
6237 if (pids_moved
== leaders
[i
].msi_ntasks
) {
6239 * All the pids in the coalition were found in this band.
6241 printf("%s: pids_moved[%d] equal total coalition ntasks[%d] \n", __FUNCTION__
,
6242 pids_moved
, leaders
[i
].msi_ntasks
);
6243 } else if (pids_moved
> leaders
[i
].msi_ntasks
) {
6245 * Apparently new coalition members showed up during the sort?
6247 printf("%s: pids_moved[%d] were greater than expected coalition ntasks[%d] \n", __FUNCTION__
,
6248 pids_moved
, leaders
[i
].msi_ntasks
);
6251 * Apparently not all the pids in the coalition were found in this band?
6253 printf("%s: pids_moved[%d] were less than expected coalition ntasks[%d] \n", __FUNCTION__
,
6254 pids_moved
, leaders
[i
].msi_ntasks
);
6258 total_pids_moved
+= pids_moved
;
6262 return(total_pids_moved
);
6267 * Traverse a list of pids, searching for each within the priority band provided.
6268 * If pid is found, move it to the front of the priority band.
6269 * Never searches outside the priority band provided.
6272 * bucket_index - jetsam priority band.
6273 * pid_list - pointer to a list of pids.
6274 * list_sz - number of pids in the list.
6276 * Pid list ordering is important in that,
6277 * pid_list[n] is expected to jetsam ahead of pid_list[n+1].
6278 * The sort_order is set by the coalition default.
6281 * the number of pids found and hence moved within the priority band.
6284 memorystatus_move_list_locked(unsigned int bucket_index
, pid_t
*pid_list
, int list_sz
)
6286 memstat_bucket_t
*current_bucket
;
6290 if ((pid_list
== NULL
) || (list_sz
<= 0)) {
6294 if (bucket_index
>= MEMSTAT_BUCKET_COUNT
) {
6298 current_bucket
= &memstat_bucket
[bucket_index
];
6299 for (i
=0; i
< list_sz
; i
++) {
6300 unsigned int b
= bucket_index
;
6302 proc_t aProc
= NULL
;
6306 list_index
= ((list_sz
- 1) - i
);
6307 aPid
= pid_list
[list_index
];
6309 /* never search beyond bucket_index provided */
6310 p
= memorystatus_get_first_proc_locked(&b
, FALSE
);
6312 if (p
->p_pid
== aPid
) {
6316 p
= memorystatus_get_next_proc_locked(&b
, p
, FALSE
);
6319 if (aProc
== NULL
) {
6320 /* pid not found in this band, just skip it */
6323 TAILQ_REMOVE(¤t_bucket
->list
, aProc
, p_memstat_list
);
6324 TAILQ_INSERT_HEAD(¤t_bucket
->list
, aProc
, p_memstat_list
);
6330 #endif /* CONFIG_JETSAM */