X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/008676633c2ad2c325837c2b64915f7ded690a8f..0a7de7458d150b5d4dffc935ba399be265ef0a1a:/bsd/kern/kern_memorystatus.c?ds=sidebyside diff --git a/bsd/kern/kern_memorystatus.c b/bsd/kern/kern_memorystatus.c index 46b037aba..5c3410624 100644 --- a/bsd/kern/kern_memorystatus.c +++ b/bsd/kern/kern_memorystatus.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2006 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2006-2018 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ - * + * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in @@ -11,10 +11,10 @@ * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. - * + * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. - * + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -22,7 +22,7 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * + * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ * */ @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -45,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -65,41 +67,87 @@ #include #endif /* CONFIG_FREEZE */ -#include +#include #include +#include +#include /* For logging clarity */ -static const char *jetsam_kill_cause_name[] = { - "" , - "jettisoned" , /* kMemorystatusKilled */ - "highwater" , /* kMemorystatusKilledHiwat */ - "vnode-limit" , /* kMemorystatusKilledVnodes */ - "vm-pageshortage" , /* kMemorystatusKilledVMPageShortage */ - "vm-thrashing" , /* kMemorystatusKilledVMThrashing */ - "fc-thrashing" , /* kMemorystatusKilledFCThrashing */ - "per-process-limit" , /* kMemorystatusKilledPerProcessLimit */ - "diagnostic" , /* kMemorystatusKilledDiagnostic */ - "idle-exit" , /* kMemorystatusKilledIdleExit */ +static const char *memorystatus_kill_cause_name[] = { + "", /* kMemorystatusInvalid */ + "jettisoned", /* kMemorystatusKilled */ + "highwater", /* kMemorystatusKilledHiwat */ + "vnode-limit", /* kMemorystatusKilledVnodes */ + "vm-pageshortage", /* kMemorystatusKilledVMPageShortage */ + "proc-thrashing", /* kMemorystatusKilledProcThrashing */ + "fc-thrashing", /* kMemorystatusKilledFCThrashing */ + "per-process-limit", /* kMemorystatusKilledPerProcessLimit */ + "disk-space-shortage", /* kMemorystatusKilledDiskSpaceShortage */ + "idle-exit", /* kMemorystatusKilledIdleExit */ + "zone-map-exhaustion", /* kMemorystatusKilledZoneMapExhaustion */ + "vm-compressor-thrashing", /* kMemorystatusKilledVMCompressorThrashing */ + "vm-compressor-space-shortage", /* kMemorystatusKilledVMCompressorSpaceShortage */ }; -#if CONFIG_JETSAM +static const char * +memorystatus_priority_band_name(int32_t priority) +{ + switch (priority) { + case JETSAM_PRIORITY_FOREGROUND: + return "FOREGROUND"; + case JETSAM_PRIORITY_AUDIO_AND_ACCESSORY: + return "AUDIO_AND_ACCESSORY"; + case JETSAM_PRIORITY_CONDUCTOR: + return "CONDUCTOR"; + case JETSAM_PRIORITY_HOME: + return "HOME"; + case JETSAM_PRIORITY_EXECUTIVE: + return "EXECUTIVE"; + case JETSAM_PRIORITY_IMPORTANT: + return "IMPORTANT"; + case JETSAM_PRIORITY_CRITICAL: + return "CRITICAL"; + } + + return "?"; +} + /* Does cause indicate vm or fc thrashing? */ static boolean_t -is_thrashing(unsigned cause) +is_reason_thrashing(unsigned cause) { switch (cause) { - case kMemorystatusKilledVMThrashing: case kMemorystatusKilledFCThrashing: + case kMemorystatusKilledVMCompressorThrashing: + case kMemorystatusKilledVMCompressorSpaceShortage: return TRUE; default: return FALSE; } } -/* Callback into vm_compressor.c to signal that thrashing has been mitigated. */ -extern void vm_thrashing_jetsam_done(void); -#endif /* CONFIG_JETSAM */ +/* Is the zone map almost full? */ +static boolean_t +is_reason_zone_map_exhaustion(unsigned cause) +{ + if (cause == kMemorystatusKilledZoneMapExhaustion) { + return TRUE; + } + return FALSE; +} + +/* + * Returns the current zone map size and capacity to include in the jetsam snapshot. + * Defined in zalloc.c + */ +extern void get_zone_map_size(uint64_t *current_size, uint64_t *capacity); + +/* + * Returns the name of the largest zone and its size to include in the jetsam snapshot. + * Defined in zalloc.c + */ +extern void get_largest_zone_info(char *zone_name, size_t zone_name_len, uint64_t *zone_size); /* These are very verbose printfs(), enable with * MEMORYSTATUS_DEBUG_LOG @@ -124,48 +172,48 @@ do { \ * soon be in effect down in the ledgers. */ -#define SET_ACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ -MACRO_BEGIN \ -(p)->p_memstat_memlimit_active = (limit); \ - if (is_fatal) { \ - (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ - } else { \ - (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ - } \ +#define SET_ACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ +MACRO_BEGIN \ +(p)->p_memstat_memlimit_active = (limit); \ + if (is_fatal) { \ + (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ + } else { \ + (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ + } \ MACRO_END -#define SET_INACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ -MACRO_BEGIN \ -(p)->p_memstat_memlimit_inactive = (limit); \ - if (is_fatal) { \ - (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ - } else { \ - (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ - } \ +#define SET_INACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ +MACRO_BEGIN \ +(p)->p_memstat_memlimit_inactive = (limit); \ + if (is_fatal) { \ + (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ + } else { \ + (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ + } \ MACRO_END -#define CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal) \ -MACRO_BEGIN \ -(p)->p_memstat_memlimit = (p)->p_memstat_memlimit_active; \ - if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { \ - (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ - is_fatal = TRUE; \ - } else { \ - (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ - is_fatal = FALSE; \ - } \ +#define CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal) \ +MACRO_BEGIN \ +(p)->p_memstat_memlimit = (p)->p_memstat_memlimit_active; \ + if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { \ + (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ + is_fatal = TRUE; \ + } else { \ + (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ + is_fatal = FALSE; \ + } \ MACRO_END -#define CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal) \ -MACRO_BEGIN \ -(p)->p_memstat_memlimit = (p)->p_memstat_memlimit_inactive; \ - if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { \ - (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ - is_fatal = TRUE; \ - } else { \ - (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ - is_fatal = FALSE; \ - } \ +#define CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal) \ +MACRO_BEGIN \ +(p)->p_memstat_memlimit = (p)->p_memstat_memlimit_inactive; \ + if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { \ + (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ + is_fatal = TRUE; \ + } else { \ + (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ + is_fatal = FALSE; \ + } \ MACRO_END @@ -193,13 +241,13 @@ static uint64_t memorystatus_apps_idle_delay_time = 0; * Memorystatus kevents */ -static int filt_memorystatusattach(struct knote *kn); +static int filt_memorystatusattach(struct knote *kn, struct kevent_internal_s *kev); static void filt_memorystatusdetach(struct knote *kn); static int filt_memorystatus(struct knote *kn, long hint); static int filt_memorystatustouch(struct knote *kn, struct kevent_internal_s *kev); static int filt_memorystatusprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev); -struct filterops memorystatus_filtops = { +SECURITY_READ_ONLY_EARLY(struct filterops) memorystatus_filtops = { .f_attach = filt_memorystatusattach, .f_detach = filt_memorystatusdetach, .f_event = filt_memorystatus, @@ -229,47 +277,63 @@ static void memorystatus_reschedule_idle_demotion_locked(void); static void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check); +int memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap); + vm_pressure_level_t convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t); boolean_t is_knote_registered_modify_task_pressure_bits(struct knote*, int, task_t, vm_pressure_level_t, vm_pressure_level_t); void memorystatus_klist_reset_all_for_level(vm_pressure_level_t pressure_level_to_clear); void memorystatus_send_low_swap_note(void); -int memorystatus_wakeup = 0; - unsigned int memorystatus_level = 0; static int memorystatus_list_count = 0; + #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1) typedef struct memstat_bucket { - TAILQ_HEAD(, proc) list; - int count; + TAILQ_HEAD(, proc) list; + int count; } memstat_bucket_t; memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT]; +int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index); + uint64_t memstat_idle_demotion_deadline = 0; int system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; int applications_aging_band = JETSAM_PRIORITY_IDLE; -#define isProcessInAgingBands(p) ((isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) || (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band))) -#define isApp(p) (! (p->p_memstat_dirty & P_DIRTY_TRACK)) -#define isSysProc(p) ((p->p_memstat_dirty & P_DIRTY_TRACK)) +#define isProcessInAgingBands(p) ((isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) || (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band))) + +/* + * Checking the p_memstat_state almost always requires the proc_list_lock + * because the jetsam thread could be on the other core changing the state. + * + * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too. + * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too. + */ +#define isApp(p) ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK)) +#define isSysProc(p) ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK)) -#define kJetsamAgingPolicyNone (0) -#define kJetsamAgingPolicyLegacy (1) -#define kJetsamAgingPolicySysProcsReclaimedFirst (2) -#define kJetsamAgingPolicyAppsReclaimedFirst (3) -#define kJetsamAgingPolicyMax kJetsamAgingPolicyAppsReclaimedFirst +#define kJetsamAgingPolicyNone (0) +#define kJetsamAgingPolicyLegacy (1) +#define kJetsamAgingPolicySysProcsReclaimedFirst (2) +#define kJetsamAgingPolicyAppsReclaimedFirst (3) +#define kJetsamAgingPolicyMax kJetsamAgingPolicyAppsReclaimedFirst unsigned int jetsam_aging_policy = kJetsamAgingPolicyLegacy; extern int corpse_for_fatal_memkill; -extern unsigned long total_corpses_count; +extern unsigned long total_corpses_count(void) __attribute__((pure)); extern void task_purge_all_corpses(void); +extern uint64_t vm_purgeable_purge_task_owned(task_t task); +boolean_t memorystatus_allowed_vm_map_fork(task_t); +#if DEVELOPMENT || DEBUG +void memorystatus_abort_vm_map_fork(task_t); +#endif #if 0 @@ -289,7 +353,7 @@ sysctl_set_jetsam_aging_policy SYSCTL_HANDLER_ARGS error = sysctl_io_number(req, jetsam_aging_policy, sizeof(int), &val, NULL); if (error || !req->newptr) { - return (error); + return error; } if ((val < 0) || (val > kJetsamAgingPolicyMax)) { @@ -302,48 +366,45 @@ sysctl_set_jetsam_aging_policy SYSCTL_HANDLER_ARGS * that might be in progress currently. We use the proc_list_lock() just for * consistency with all the routines dealing with 'aging' processes. We need * a lighterweight lock. - */ + */ proc_list_lock(); old_system_procs_aging_band = system_procs_aging_band; old_applications_aging_band = applications_aging_band; - - switch (val) { - case kJetsamAgingPolicyNone: - new_system_procs_aging_band = JETSAM_PRIORITY_IDLE; - new_applications_aging_band = JETSAM_PRIORITY_IDLE; - break; + switch (val) { + case kJetsamAgingPolicyNone: + new_system_procs_aging_band = JETSAM_PRIORITY_IDLE; + new_applications_aging_band = JETSAM_PRIORITY_IDLE; + break; - case kJetsamAgingPolicyLegacy: - /* - * Legacy behavior where some daemons get a 10s protection once and only before the first clean->dirty->clean transition before going into IDLE band. - */ - new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; - new_applications_aging_band = JETSAM_PRIORITY_IDLE; - break; + case kJetsamAgingPolicyLegacy: + /* + * Legacy behavior where some daemons get a 10s protection once and only before the first clean->dirty->clean transition before going into IDLE band. + */ + new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; + new_applications_aging_band = JETSAM_PRIORITY_IDLE; + break; - case kJetsamAgingPolicySysProcsReclaimedFirst: - new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; - new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; - break; + case kJetsamAgingPolicySysProcsReclaimedFirst: + new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; + new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; + break; - case kJetsamAgingPolicyAppsReclaimedFirst: - new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; - new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; - break; + case kJetsamAgingPolicyAppsReclaimedFirst: + new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; + new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; + break; - default: - break; + default: + break; } if (old_system_procs_aging_band && (old_system_procs_aging_band != new_system_procs_aging_band)) { - old_bucket = &memstat_bucket[old_system_procs_aging_band]; p = TAILQ_FIRST(&old_bucket->list); - + while (p) { - next_proc = TAILQ_NEXT(p, p_memstat_list); if (isSysProc(p)) { @@ -360,12 +421,10 @@ sysctl_set_jetsam_aging_policy SYSCTL_HANDLER_ARGS } if (old_applications_aging_band && (old_applications_aging_band != new_applications_aging_band)) { - old_bucket = &memstat_bucket[old_applications_aging_band]; p = TAILQ_FIRST(&old_bucket->list); - - while (p) { + while (p) { next_proc = TAILQ_NEXT(p, p_memstat_list); if (isApp(p)) { @@ -387,11 +446,11 @@ sysctl_set_jetsam_aging_policy SYSCTL_HANDLER_ARGS proc_list_unlock(); - return (0); + return 0; } -SYSCTL_PROC(_kern, OID_AUTO, set_jetsam_aging_policy, CTLTYPE_INT|CTLFLAG_RW, - 0, 0, sysctl_set_jetsam_aging_policy, "I", "Jetsam Aging Policy"); +SYSCTL_PROC(_kern, OID_AUTO, set_jetsam_aging_policy, CTLTYPE_INT | CTLFLAG_RW, + 0, 0, sysctl_set_jetsam_aging_policy, "I", "Jetsam Aging Policy"); #endif /*0*/ static int @@ -407,7 +466,7 @@ sysctl_jetsam_set_sysprocs_idle_delay_time SYSCTL_HANDLER_ARGS error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL); if (error || !req->newptr) { - return (error); + return error; } if ((val < 0) || (val > INT32_MAX)) { @@ -416,12 +475,12 @@ sysctl_jetsam_set_sysprocs_idle_delay_time SYSCTL_HANDLER_ARGS } nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); - - return(0); + + return 0; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_sysprocs_idle_delay_time, CTLTYPE_INT|CTLFLAG_RW, - 0, 0, sysctl_jetsam_set_sysprocs_idle_delay_time, "I", "Aging window for system processes"); +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_sysprocs_idle_delay_time, CTLTYPE_INT | CTLFLAG_RW, + 0, 0, sysctl_jetsam_set_sysprocs_idle_delay_time, "I", "Aging window for system processes"); static int @@ -437,7 +496,7 @@ sysctl_jetsam_set_apps_idle_delay_time SYSCTL_HANDLER_ARGS error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL); if (error || !req->newptr) { - return (error); + return error; } if ((val < 0) || (val > INT32_MAX)) { @@ -446,31 +505,36 @@ sysctl_jetsam_set_apps_idle_delay_time SYSCTL_HANDLER_ARGS } nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); - - return(0); + + return 0; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_apps_idle_delay_time, CTLTYPE_INT|CTLFLAG_RW, - 0, 0, sysctl_jetsam_set_apps_idle_delay_time, "I", "Aging window for applications"); +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_apps_idle_delay_time, CTLTYPE_INT | CTLFLAG_RW, + 0, 0, sysctl_jetsam_set_apps_idle_delay_time, "I", "Aging window for applications"); -SYSCTL_INT(_kern, OID_AUTO, jetsam_aging_policy, CTLTYPE_INT|CTLFLAG_RD, &jetsam_aging_policy, 0, ""); +SYSCTL_INT(_kern, OID_AUTO, jetsam_aging_policy, CTLTYPE_INT | CTLFLAG_RD, &jetsam_aging_policy, 0, ""); static unsigned int memorystatus_dirty_count = 0; -SYSCTL_INT(_kern, OID_AUTO, max_task_pmem, CTLFLAG_RD|CTLFLAG_LOCKED|CTLFLAG_MASKED, &max_task_footprint_mb, 0, ""); +SYSCTL_INT(_kern, OID_AUTO, max_task_pmem, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED, &max_task_footprint_mb, 0, ""); +#if CONFIG_EMBEDDED + +SYSCTL_INT(_kern, OID_AUTO, memorystatus_level, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_level, 0, ""); + +#endif /* CONFIG_EMBEDDED */ int memorystatus_get_level(__unused struct proc *p, struct memorystatus_get_level_args *args, __unused int *ret) { - user_addr_t level = 0; - + user_addr_t level = 0; + level = args->level; - + if (copyout(&memorystatus_level, level, sizeof(memorystatus_level)) != 0) { return EFAULT; } - + return 0; } @@ -488,12 +552,6 @@ static boolean_t memorystatus_kill_specific_process(pid_t victim_pid, uint32_t c static boolean_t memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason); -/* Jetsam */ - -#if CONFIG_JETSAM - -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); - static int memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval); static int memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry); @@ -508,14 +566,10 @@ static boolean_t memorystatus_idle_snapshot = 0; unsigned int memorystatus_delta = 0; -static unsigned int memorystatus_available_pages_critical_base = 0; -//static unsigned int memorystatus_last_foreground_pressure_pages = (unsigned int)-1; -static unsigned int memorystatus_available_pages_critical_idle_offset = 0; - /* Jetsam Loop Detection */ -static boolean_t memorystatus_jld_enabled = TRUE; /* Enables jetsam loop detection on all devices */ -static uint32_t memorystatus_jld_eval_period_msecs = 0; /* Init pass sets this based on device memory size */ -static int memorystatus_jld_eval_aggressive_count = 3; /* Raise the priority max after 'n' aggressive loops */ +static boolean_t memorystatus_jld_enabled = FALSE; /* Enable jetsam loop detection */ +static uint32_t memorystatus_jld_eval_period_msecs = 0; /* Init pass sets this based on device memory size */ +static int memorystatus_jld_eval_aggressive_count = 3; /* Raise the priority max after 'n' aggressive loops */ static int memorystatus_jld_eval_aggressive_priority_band_max = 15; /* Kill aggressively up through this band */ /* @@ -524,44 +578,39 @@ static int memorystatus_jld_eval_aggressive_priority_band_max = 15; /* Kil * * RESTRICTIONS: * - Such a request is respected/acknowledged only once while that 'requesting' app is in the FG band i.e. if aggressive jetsam was - * needed and the 'lenient' mode was deployed then that's it for this special mode while the app is in the FG band. + * needed and the 'lenient' mode was deployed then that's it for this special mode while the app is in the FG band. * * - 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. * * - Also, the transition of the 'requesting' app away from the FG band will void this special behavior. */ -#define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD 25 -boolean_t memorystatus_aggressive_jetsam_lenient_allowed = FALSE; -boolean_t memorystatus_aggressive_jetsam_lenient = FALSE; +#define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD 25 +boolean_t memorystatus_aggressive_jetsam_lenient_allowed = FALSE; +boolean_t memorystatus_aggressive_jetsam_lenient = FALSE; #if DEVELOPMENT || DEBUG -/* +/* * Jetsam Loop Detection tunables. */ -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_period_msecs, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jld_eval_period_msecs, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_count, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_count, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_priority_band_max, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_priority_band_max, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_period_msecs, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_count, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_count, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_priority_band_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_priority_band_max, 0, ""); #endif /* DEVELOPMENT || DEBUG */ -#if DEVELOPMENT || DEBUG -static unsigned int memorystatus_jetsam_panic_debug = 0; -static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic = 0; -#endif - -static unsigned int memorystatus_jetsam_policy = kPolicyDefault; -static unsigned int memorystatus_thread_wasted_wakeup = 0; - static uint32_t kill_under_pressure_cause = 0; /* * default jetsam snapshot support */ static memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot; +static memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy; #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries static unsigned int memorystatus_jetsam_snapshot_count = 0; +static unsigned int memorystatus_jetsam_snapshot_copy_count = 0; static unsigned int memorystatus_jetsam_snapshot_max = 0; +static unsigned int memorystatus_jetsam_snapshot_size = 0; static uint64_t memorystatus_jetsam_snapshot_last_timestamp = 0; static uint64_t memorystatus_jetsam_snapshot_timeout = 0; #define JETSAM_SNAPSHOT_TIMEOUT_SECS 30 @@ -576,23 +625,22 @@ static boolean_t memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memory static void memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime); static void memorystatus_clear_errors(void); -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); +static void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages); static void memorystatus_get_task_phys_footprint_page_counts(task_t task, - uint64_t *internal_pages, uint64_t *internal_compressed_pages, - uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, - uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, - uint64_t *iokit_mapped_pages, uint64_t *page_table_pages); + uint64_t *internal_pages, uint64_t *internal_compressed_pages, + uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, + uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, + uint64_t *iokit_mapped_pages, uint64_t *page_table_pages); static void memorystatus_get_task_memory_region_count(task_t task, uint64_t *count); static uint32_t memorystatus_build_state(proc_t p); -static void memorystatus_update_levels_locked(boolean_t critical_only); //static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured); static boolean_t memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, os_reason_t jetsam_reason, int32_t *priority, uint32_t *errors); -static boolean_t memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reason_t jetsam_reason, int aggr_count, int32_t priority_max, uint32_t *errors); -static boolean_t memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, int aggr_count, uint32_t *errors); -static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors); +static boolean_t memorystatus_kill_top_process_aggressive(uint32_t cause, int aggr_count, int32_t priority_max, uint32_t *errors); +static boolean_t memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, uint32_t *errors); +static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged); static boolean_t memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause); @@ -607,8 +655,6 @@ typedef int (*cmpfunc_t)(const void *a, const void *b); extern void qsort(void *a, size_t n, size_t es, cmpfunc_t cmp); static int memstat_asc_cmp(const void *a, const void *b); -#endif /* CONFIG_JETSAM */ - /* VM pressure */ extern unsigned int vm_page_free_count; @@ -618,31 +664,58 @@ extern unsigned int vm_page_throttled_count; extern unsigned int vm_page_purgeable_count; extern unsigned int vm_page_wire_count; #if CONFIG_SECLUDED_MEMORY -extern unsigned int vm_page_secluded_count; +extern unsigned int vm_page_secluded_count; #endif /* CONFIG_SECLUDED_MEMORY */ -#if VM_PRESSURE_EVENTS - -boolean_t memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused boolean_t is_fatal, boolean_t exceeded); - -vm_pressure_level_t memorystatus_vm_pressure_level = kVMPressureNormal; - -#if CONFIG_MEMORYSTATUS +#if CONFIG_JETSAM unsigned int memorystatus_available_pages = (unsigned int)-1; unsigned int memorystatus_available_pages_pressure = 0; unsigned int memorystatus_available_pages_critical = 0; -unsigned int memorystatus_frozen_count = 0; -unsigned int memorystatus_suspended_count = 0; -unsigned int memorystatus_policy_more_free_offset_pages = 0; +static unsigned int memorystatus_available_pages_critical_base = 0; +static unsigned int memorystatus_available_pages_critical_idle_offset = 0; -#if CONFIG_JETSAM #if DEVELOPMENT || DEBUG SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); #else -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD| CTLFLAG_MASKED | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); #endif /* DEVELOPMENT || DEBUG */ + +static unsigned int memorystatus_jetsam_policy = kPolicyDefault; +unsigned int memorystatus_policy_more_free_offset_pages = 0; +static void memorystatus_update_levels_locked(boolean_t critical_only); +static unsigned int memorystatus_thread_wasted_wakeup = 0; + +/* Callback into vm_compressor.c to signal that thrashing has been mitigated. */ +extern void vm_thrashing_jetsam_done(void); +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); + +int32_t max_kill_priority = JETSAM_PRIORITY_MAX; + +#else /* CONFIG_JETSAM */ + +uint64_t memorystatus_available_pages = (uint64_t)-1; +uint64_t memorystatus_available_pages_pressure = (uint64_t)-1; +uint64_t memorystatus_available_pages_critical = (uint64_t)-1; + +int32_t max_kill_priority = JETSAM_PRIORITY_IDLE; #endif /* CONFIG_JETSAM */ +unsigned int memorystatus_frozen_count = 0; +unsigned int memorystatus_frozen_processes_max = 0; +unsigned int memorystatus_frozen_shared_mb = 0; +unsigned int memorystatus_frozen_shared_mb_max = 0; +unsigned int memorystatus_freeze_shared_mb_per_process_max = 0; /* Max. MB allowed per process to be freezer-eligible. */ +unsigned int memorystatus_freeze_private_shared_pages_ratio = 2; /* Ratio of private:shared pages for a process to be freezer-eligible. */ +unsigned int memorystatus_suspended_count = 0; +unsigned int memorystatus_thaw_count = 0; +unsigned int memorystatus_refreeze_eligible_count = 0; /* # of processes currently thawed i.e. have state on disk & in-memory */ + +#if VM_PRESSURE_EVENTS + +boolean_t memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused boolean_t is_fatal, boolean_t exceeded); + +vm_pressure_level_t memorystatus_vm_pressure_level = kVMPressureNormal; + /* * We use this flag to signal if we have any HWM offenders * on the system. This way we can reduce the number of wakeups @@ -658,7 +731,21 @@ SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD| CTLFLAG_M boolean_t memorystatus_hwm_candidates = 0; static int memorystatus_send_note(int event_code, void *data, size_t data_length); -#endif /* CONFIG_MEMORYSTATUS */ + +/* + * This value is the threshold that a process must meet to be considered for scavenging. + */ +#if CONFIG_EMBEDDED +#define VM_PRESSURE_MINIMUM_RSIZE 6 /* MB */ +#else /* CONFIG_EMBEDDED */ +#define VM_PRESSURE_MINIMUM_RSIZE 10 /* MB */ +#endif /* CONFIG_EMBEDDED */ + +uint32_t vm_pressure_task_footprint_min = VM_PRESSURE_MINIMUM_RSIZE; + +#if DEVELOPMENT || DEBUG +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_vm_pressure_task_footprint_min, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pressure_task_footprint_min, 0, ""); +#endif /* DEVELOPMENT || DEBUG */ #endif /* VM_PRESSURE_EVENTS */ @@ -669,15 +756,28 @@ lck_grp_attr_t *disconnect_page_mappings_lck_grp_attr; lck_grp_t *disconnect_page_mappings_lck_grp; static lck_mtx_t disconnect_page_mappings_mutex; -#endif +extern boolean_t kill_on_no_paging_space; +#endif /* DEVELOPMENT || DEBUG */ + +/* + * Table that expresses the probability of a process + * being used in the next hour. + */ +typedef struct memorystatus_internal_probabilities { + char proc_name[MAXCOMLEN + 1]; + int use_probability; +} memorystatus_internal_probabilities_t; + +static memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table = NULL; +static size_t memorystatus_global_probabilities_size = 0; /* Freeze */ #if CONFIG_FREEZE - boolean_t memorystatus_freeze_enabled = FALSE; int memorystatus_freeze_wakeup = 0; +int memorystatus_freeze_jetsam_band = 0; /* the jetsam band which will contain P_MEMSTAT_FROZEN processes */ lck_grp_attr_t *freezer_lck_grp_attr; lck_grp_t *freezer_lck_grp; @@ -685,8 +785,11 @@ static lck_mtx_t freezer_mutex; static inline boolean_t memorystatus_can_freeze_processes(void); static boolean_t memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low); - +static boolean_t memorystatus_is_process_eligible_for_freeze(proc_t p); static void memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused); +static boolean_t memorystatus_freeze_thread_should_run(void); + +void memorystatus_disable_freeze(void); /* Thresholds */ static unsigned int memorystatus_freeze_threshold = 0; @@ -697,24 +800,37 @@ static unsigned int memorystatus_freeze_pages_max = 0; static unsigned int memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_DEFAULT; static unsigned int memorystatus_freeze_daily_mb_max = FREEZE_DAILY_MB_MAX_DEFAULT; +static uint64_t memorystatus_freeze_budget_pages_remaining = 0; //remaining # of pages that can be frozen to disk +static boolean_t memorystatus_freeze_degradation = FALSE; //protected by the freezer mutex. Signals we are in a degraded freeze mode. + +static unsigned int memorystatus_max_frozen_demotions_daily = 0; +static unsigned int memorystatus_thaw_count_demotion_threshold = 0; /* Stats */ -static uint64_t memorystatus_freeze_count = 0; static uint64_t memorystatus_freeze_pageouts = 0; /* Throttling */ +#define DEGRADED_WINDOW_MINS (30) +#define NORMAL_WINDOW_MINS (24 * 60) + static throttle_interval_t throttle_intervals[] = { - { 60, 8, 0, 0, { 0, 0 }, FALSE }, /* 1 hour intermediate interval, 8x burst */ - { 24 * 60, 1, 0, 0, { 0, 0 }, FALSE }, /* 24 hour long interval, no burst */ + { DEGRADED_WINDOW_MINS, 1, 0, 0, { 0, 0 }}, + { NORMAL_WINDOW_MINS, 1, 0, 0, { 0, 0 }}, }; +throttle_interval_t *degraded_throttle_window = &throttle_intervals[0]; +throttle_interval_t *normal_throttle_window = &throttle_intervals[1]; -static uint64_t memorystatus_freeze_throttle_count = 0; +extern uint64_t vm_swap_get_free_space(void); +extern boolean_t vm_swap_max_budget(uint64_t *); -static unsigned int memorystatus_suspended_footprint_total = 0; /* pages */ +static void memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed); -extern uint64_t vm_swap_get_free_space(void); +static uint64_t memorystatus_freezer_thread_next_run_ts = 0; -static boolean_t memorystatus_freeze_update_throttle(); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_count, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_thaw_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_thaw_count, 0, ""); +SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_pageouts, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_pageouts, ""); +SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_budget_pages_remaining, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_budget_pages_remaining, ""); #endif /* CONFIG_FREEZE */ @@ -727,7 +843,7 @@ extern struct knote *vm_find_knote_from_pid(pid_t, struct klist *); static unsigned int memorystatus_debug_dump_this_bucket = 0; static void -memorystatus_debug_dump_bucket_locked (unsigned int bucket_index) +memorystatus_debug_dump_bucket_locked(unsigned int bucket_index) { proc_t p = NULL; uint64_t bytes = 0; @@ -735,10 +851,10 @@ memorystatus_debug_dump_bucket_locked (unsigned int bucket_index) unsigned int b = bucket_index; boolean_t traverse_all_buckets = FALSE; - if (bucket_index >= MEMSTAT_BUCKET_COUNT) { + if (bucket_index >= MEMSTAT_BUCKET_COUNT) { traverse_all_buckets = TRUE; b = 0; - } else { + } else { traverse_all_buckets = FALSE; b = bucket_index; } @@ -753,49 +869,49 @@ memorystatus_debug_dump_bucket_locked (unsigned int bucket_index) * F==Fatal, NF==NonFatal */ - printf("memorystatus_debug_dump ***START*(PAGE_SIZE_64=%llu)**\n", PAGE_SIZE_64); + printf("memorystatus_debug_dump ***START*(PAGE_SIZE_64=%llu)**\n", PAGE_SIZE_64); printf("bucket [pid] [pages / MB] [state] [EP / RP] dirty deadline [L-limit / C-limit / A-limit / IA-limit] name\n"); p = memorystatus_get_first_proc_locked(&b, traverse_all_buckets); while (p) { bytes = get_task_phys_footprint(p->task); task_get_phys_footprint_limit(p->task, &ledger_limit); printf("%2d [%5d] [%5lld /%3lldMB] 0x%-8x [%2d / %2d] 0x%-3x %10lld [%3d / %3d%s / %3d%s / %3d%s] %s\n", - b, p->p_pid, - (bytes / PAGE_SIZE_64), /* task's footprint converted from bytes to pages */ - (bytes / (1024ULL * 1024ULL)), /* task's footprint converted from bytes to MB */ - p->p_memstat_state, p->p_memstat_effectivepriority, p->p_memstat_requestedpriority, p->p_memstat_dirty, p->p_memstat_idledeadline, - ledger_limit, - p->p_memstat_memlimit, - (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), - p->p_memstat_memlimit_active, - (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL ? "F " : "NF"), - p->p_memstat_memlimit_inactive, - (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL ? "F " : "NF"), - (*p->p_name ? p->p_name : "unknown")); + b, p->p_pid, + (bytes / PAGE_SIZE_64), /* task's footprint converted from bytes to pages */ + (bytes / (1024ULL * 1024ULL)), /* task's footprint converted from bytes to MB */ + p->p_memstat_state, p->p_memstat_effectivepriority, p->p_memstat_requestedpriority, p->p_memstat_dirty, p->p_memstat_idledeadline, + ledger_limit, + p->p_memstat_memlimit, + (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), + p->p_memstat_memlimit_active, + (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL ? "F " : "NF"), + p->p_memstat_memlimit_inactive, + (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL ? "F " : "NF"), + (*p->p_name ? p->p_name : "unknown")); p = memorystatus_get_next_proc_locked(&b, p, traverse_all_buckets); - } - printf("memorystatus_debug_dump ***END***\n"); + } + printf("memorystatus_debug_dump ***END***\n"); } static int sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS { #pragma unused(oidp, arg2) - int bucket_index = 0; - int error; + int bucket_index = 0; + int error; error = SYSCTL_OUT(req, arg1, sizeof(int)); if (error || !req->newptr) { - return (error); + return error; + } + error = SYSCTL_IN(req, &bucket_index, sizeof(int)); + if (error || !req->newptr) { + return error; } - error = SYSCTL_IN(req, &bucket_index, sizeof(int)); - if (error || !req->newptr) { - return (error); - } if (bucket_index >= MEMSTAT_BUCKET_COUNT) { /* * All jetsam buckets will be dumped. */ - } else { + } else { /* * Only a single bucket will be dumped. */ @@ -805,17 +921,17 @@ sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS memorystatus_debug_dump_bucket_locked(bucket_index); proc_list_unlock(); memorystatus_debug_dump_this_bucket = bucket_index; - return (error); + return error; } /* * Debug aid to look at jetsam buckets and proc jetsam fields. * Use this sysctl to act on a particular jetsam bucket. * Writing the sysctl triggers the dump. - * Usage: sysctl kern.memorystatus_debug_dump_this_bucket= + * Usage: sysctl kern.memorystatus_debug_dump_this_bucket= */ -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", ""); +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", ""); /* Debug aid to aid determination of limit */ @@ -827,17 +943,17 @@ sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS proc_t p; unsigned int b = 0; int error, enable = 0; - boolean_t use_active; /* use the active limit and active limit attributes */ + boolean_t use_active; /* use the active limit and active limit attributes */ boolean_t is_fatal; error = SYSCTL_OUT(req, arg1, sizeof(int)); if (error || !req->newptr) { - return (error); + return error; } error = SYSCTL_IN(req, &enable, sizeof(int)); if (error || !req->newptr) { - return (error); + return error; } if (!(enable == 0 || enable == 1)) { @@ -851,17 +967,11 @@ sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS use_active = proc_jetsam_state_is_active_locked(p); if (enable) { - /* - * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore. - * Background limits are described via the inactive limit slots. - */ - if (use_active == TRUE) { CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); } else { CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); } - } else { /* * Disabling limits does not touch the stored variants. @@ -879,16 +989,15 @@ sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS p = memorystatus_get_next_proc_locked(&b, p, TRUE); } - + memorystatus_highwater_enabled = enable; proc_list_unlock(); return 0; - } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", ""); +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", ""); #if VM_PRESSURE_EVENTS @@ -896,7 +1005,7 @@ SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT|CTLFLAG * This routine is used for targeted notifications regardless of system memory pressure * and regardless of whether or not the process has already been notified. * It bypasses and has no effect on the only-one-notification per soft-limit policy. - * + * * "memnote" is the current user. */ @@ -908,12 +1017,13 @@ sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS int error = 0, pid = 0; struct knote *kn = NULL; boolean_t found_knote = FALSE; - int fflags = 0; /* filter flags for EVFILT_MEMORYSTATUS */ + int fflags = 0; /* filter flags for EVFILT_MEMORYSTATUS */ uint64_t value = 0; error = sysctl_handle_quad(oidp, &value, 0, req); - if (error || !req->newptr) - return (error); + if (error || !req->newptr) { + return error; + } /* * Find the pid in the low 32 bits of value passed in. @@ -937,16 +1047,17 @@ sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS /* * See event.h ... fflags for EVFILT_MEMORYSTATUS */ - if (!((fflags == NOTE_MEMORYSTATUS_PRESSURE_NORMAL)|| - (fflags == NOTE_MEMORYSTATUS_PRESSURE_WARN) || - (fflags == NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) || - (fflags == NOTE_MEMORYSTATUS_LOW_SWAP) || - (fflags == NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) || - (fflags == NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL))) { - + if (!((fflags == NOTE_MEMORYSTATUS_PRESSURE_NORMAL) || + (fflags == NOTE_MEMORYSTATUS_PRESSURE_WARN) || + (fflags == NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) || + (fflags == NOTE_MEMORYSTATUS_LOW_SWAP) || + (fflags == NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) || + (fflags == NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) || + (((fflags & NOTE_MEMORYSTATUS_MSL_STATUS) != 0 && + ((fflags & ~NOTE_MEMORYSTATUS_MSL_STATUS) == 0))))) { printf("memorystatus_vm_pressure_send: notification [0x%x] not supported \n", fflags); error = 1; - return (error); + return error; } /* @@ -979,27 +1090,29 @@ sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS memorystatus_klist_unlock(); - return (error); + return error; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_send, CTLTYPE_QUAD|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_send, CTLTYPE_QUAD | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorystatus_vm_pressure_send, "Q", ""); #endif /* VM_PRESSURE_EVENTS */ -#if CONFIG_JETSAM - -SYSCTL_INT(_kern, OID_AUTO, memorystatus_idle_snapshot, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_idle_snapshot, 0, ""); +SYSCTL_INT(_kern, OID_AUTO, memorystatus_idle_snapshot, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_idle_snapshot, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_available_pages_critical, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_base, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_critical_base, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_idle_offset, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_critical_idle_offset, 0, ""); +#if CONFIG_JETSAM +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages_critical, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_base, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_critical_base, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_idle_offset, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_critical_idle_offset, 0, ""); SYSCTL_UINT(_kern, OID_AUTO, memorystatus_policy_more_free_offset_pages, CTLFLAG_RW, &memorystatus_policy_more_free_offset_pages, 0, ""); +static unsigned int memorystatus_jetsam_panic_debug = 0; +static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic = 0; + /* Diagnostic code */ enum { - kJetsamDiagnosticModeNone = 0, + kJetsamDiagnosticModeNone = 0, kJetsamDiagnosticModeAll = 1, kJetsamDiagnosticModeStopAtFirstActive = 2, kJetsamDiagnosticModeCount @@ -1015,27 +1128,28 @@ sysctl_jetsam_diagnostic_mode SYSCTL_HANDLER_ARGS const char *diagnosticStrings[] = { "jetsam: diagnostic mode: resetting critical level.", "jetsam: diagnostic mode: will examine all processes", - "jetsam: diagnostic mode: will stop at first active process" + "jetsam: diagnostic mode: will stop at first active process" }; - + int error, val = jetsam_diagnostic_mode; boolean_t changed = FALSE; error = sysctl_handle_int(oidp, &val, 0, req); - if (error || !req->newptr) - return (error); + if (error || !req->newptr) { + return error; + } if ((val < 0) || (val >= kJetsamDiagnosticModeCount)) { printf("jetsam: diagnostic mode: invalid value - %d\n", val); return EINVAL; } - + proc_list_lock(); - + if ((unsigned int) val != jetsam_diagnostic_mode) { jetsam_diagnostic_mode = val; memorystatus_jetsam_policy &= ~kPolicyDiagnoseActive; - + switch (jetsam_diagnostic_mode) { case kJetsamDiagnosticModeNone: /* Already cleared */ @@ -1050,28 +1164,28 @@ sysctl_jetsam_diagnostic_mode SYSCTL_HANDLER_ARGS /* Already validated */ break; } - + memorystatus_update_levels_locked(FALSE); changed = TRUE; } - + proc_list_unlock(); - + if (changed) { printf("%s\n", diagnosticStrings[val]); } - - return (0); + + return 0; } -SYSCTL_PROC(_debug, OID_AUTO, jetsam_diagnostic_mode, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED|CTLFLAG_ANYBODY, - &jetsam_diagnostic_mode, 0, sysctl_jetsam_diagnostic_mode, "I", "Jetsam Diagnostic Mode"); +SYSCTL_PROC(_debug, OID_AUTO, jetsam_diagnostic_mode, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY, + &jetsam_diagnostic_mode, 0, sysctl_jetsam_diagnostic_mode, "I", "Jetsam Diagnostic Mode"); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jetsam_policy_offset_pages_diagnostic, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jetsam_policy_offset_pages_diagnostic, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jetsam_policy_offset_pages_diagnostic, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jetsam_policy_offset_pages_diagnostic, 0, ""); #if VM_PRESSURE_EVENTS -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_pressure, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_pressure, 0, ""); #endif /* VM_PRESSURE_EVENTS */ @@ -1079,23 +1193,53 @@ SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW|C #if CONFIG_FREEZE -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_daily_mb_max, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_daily_mb_max, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_jetsam_band, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_jetsam_band, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_daily_mb_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_daily_mb_max, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_degraded_mode, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_degradation, 0, ""); + +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_threshold, 0, ""); + +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_min, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_pages_min, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_pages_max, 0, ""); + +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_refreeze_eligible_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_refreeze_eligible_count, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_processes_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_frozen_processes_max, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_threshold, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_threshold, 0, ""); +/* + * Max. shared-anonymous memory in MB that can be held by frozen processes in the high jetsam band. + * "0" means no limit. + * Default is 10% of system-wide task limit. + */ + +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_frozen_shared_mb_max, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_shared_mb, 0, ""); + +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb_per_process_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_shared_mb_per_process_max, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_private_shared_pages_ratio, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_private_shared_pages_ratio, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_min, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_pages_min, 0, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_max, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_pages_max, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_min_processes, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_suspended_threshold, 0, ""); -SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_count, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_freeze_count, ""); -SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_pageouts, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_freeze_pageouts, ""); -SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_throttle_count, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_freeze_throttle_count, ""); -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_min_processes, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_suspended_threshold, 0, ""); +/* + * max. # of frozen process demotions we will allow in our daily cycle. + */ +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_max_freeze_demotions_daily, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_max_frozen_demotions_daily, 0, ""); +/* + * min # of thaws needed by a process to protect it from getting demoted into the IDLE band. + */ +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_thaw_count_demotion_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_thaw_count_demotion_threshold, 0, ""); boolean_t memorystatus_freeze_throttle_enabled = TRUE; -SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_throttle_enabled, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_throttle_enabled, 0, ""); +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_throttle_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_throttle_enabled, 0, ""); -#define VM_PAGES_FOR_ALL_PROCS (2) -/* +/* + * When set to true, this keeps frozen processes in the compressor pool in memory, instead of swapping them out to disk. + * Exposed via the sysctl kern.memorystatus_freeze_to_memory. + */ +boolean_t memorystatus_freeze_to_memory = FALSE; +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_to_memory, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_to_memory, 0, ""); + +#define VM_PAGES_FOR_ALL_PROCS (2) +/* * Manual trigger of freeze and thaw for dev / debug kernels only. */ static int @@ -1104,14 +1248,17 @@ sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS #pragma unused(arg1, arg2) int error, pid = 0; proc_t p; + int freezer_error_code = 0; if (memorystatus_freeze_enabled == FALSE) { + printf("sysctl_freeze: Freeze is DISABLED\n"); return ENOTSUP; } error = sysctl_handle_int(oidp, &pid, 0, req); - if (error || !req->newptr) - return (error); + if (error || !req->newptr) { + return error; + } if (pid == VM_PAGES_FOR_ALL_PROCS) { vm_pageout_anonymous_pages(); @@ -1123,22 +1270,27 @@ sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS p = proc_find(pid); if (p != NULL) { - uint32_t purgeable, wired, clean, dirty; - boolean_t shared; - uint32_t max_pages = 0; + uint32_t purgeable, wired, clean, dirty, shared; + uint32_t max_pages = 0, state = 0; if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { - - unsigned int avail_swap_space = 0; /* in pages. */ - /* * Freezer backed by the compressor and swap file(s) - * while will hold compressed data. + * will hold compressed data. + * + * Set the sysctl kern.memorystatus_freeze_to_memory to true to keep compressed data from + * being swapped out to disk. Note that this disables freezer swap support globally, + * not just for the process being frozen. + * + * + * We don't care about the global freezer budget or the process's (min/max) budget here. + * The freeze sysctl is meant to force-freeze a process. + * + * We also don't update any global or process stats on this path, so that the jetsam/ freeze + * logic remains unaffected. The tasks we're performing here are: freeze the process, set the + * P_MEMSTAT_FROZEN bit, and elevate the process to a higher band (if the freezer is active). */ - avail_swap_space = vm_swap_get_free_space() / PAGE_SIZE_64; - - max_pages = MIN(avail_swap_space, memorystatus_freeze_pages_max); - + max_pages = memorystatus_freeze_pages_max; } else { /* * We only have the compressor without any swap. @@ -1146,50 +1298,135 @@ sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS max_pages = UINT32_MAX - 1; } - error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, FALSE); - proc_rele(p); + proc_list_lock(); + state = p->p_memstat_state; + proc_list_unlock(); - if (error) - error = EIO; + /* + * The jetsam path also verifies that the process is a suspended App. We don't care about that here. + * We simply ensure that jetsam is not already working on the process and that the process has not + * explicitly disabled freezing. + */ + if (state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED)) { + printf("sysctl_freeze: p_memstat_state check failed, process is%s%s%s\n", + (state & P_MEMSTAT_TERMINATED) ? " terminated" : "", + (state & P_MEMSTAT_LOCKED) ? " locked" : "", + (state & P_MEMSTAT_FREEZE_DISABLED) ? " unfreezable" : ""); - lck_mtx_unlock(&freezer_mutex); - return error; - } + proc_rele(p); + lck_mtx_unlock(&freezer_mutex); + return EPERM; + } - lck_mtx_unlock(&freezer_mutex); - return EINVAL; -} + error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */); -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freeze, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, - 0, 0, &sysctl_memorystatus_freeze, "I", ""); + if (error) { + char reason[128]; + if (freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) { + strlcpy(reason, "too much shared memory", 128); + } -static int -sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS -{ -#pragma unused(arg1, arg2) + if (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) { + strlcpy(reason, "low private-shared pages ratio", 128); + } - int error, pid = 0; - proc_t p; + if (freezer_error_code == FREEZER_ERROR_NO_COMPRESSOR_SPACE) { + strlcpy(reason, "no compressor space", 128); + } - if (memorystatus_freeze_enabled == FALSE) { - return ENOTSUP; - } + if (freezer_error_code == FREEZER_ERROR_NO_SWAP_SPACE) { + strlcpy(reason, "no swap space", 128); + } - error = sysctl_handle_int(oidp, &pid, 0, req); - if (error || !req->newptr) - return (error); + printf("sysctl_freeze: task_freeze failed: %s\n", reason); - if (pid == VM_PAGES_FOR_ALL_PROCS) { - do_fastwake_warmup_all(); + if (error == KERN_NO_SPACE) { + /* Make it easy to distinguish between failures due to low compressor/ swap space and other failures. */ + error = ENOSPC; + } else { + error = EIO; + } + } else { + proc_list_lock(); + if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == 0) { + p->p_memstat_state |= P_MEMSTAT_FROZEN; + memorystatus_frozen_count++; + } + p->p_memstat_frozen_count++; + + + proc_list_unlock(); + + if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + /* + * We elevate only if we are going to swap out the data. + */ + error = memorystatus_update_inactive_jetsam_priority_band(pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, + memorystatus_freeze_jetsam_band, TRUE); + + if (error) { + printf("sysctl_freeze: Elevating frozen process to higher jetsam band failed with %d\n", error); + } + } + } + + proc_rele(p); + + lck_mtx_unlock(&freezer_mutex); + return error; + } else { + printf("sysctl_freeze: Invalid process\n"); + } + + + lck_mtx_unlock(&freezer_mutex); + return EINVAL; +} + +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freeze, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, + 0, 0, &sysctl_memorystatus_freeze, "I", ""); + +static int +sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS +{ +#pragma unused(arg1, arg2) + + int error, pid = 0; + proc_t p; + + if (memorystatus_freeze_enabled == FALSE) { + return ENOTSUP; + } + + error = sysctl_handle_int(oidp, &pid, 0, req); + if (error || !req->newptr) { + return error; + } + + if (pid == VM_PAGES_FOR_ALL_PROCS) { + do_fastwake_warmup_all(); return 0; } else { p = proc_find(pid); if (p != NULL) { error = task_thaw(p->task); - proc_rele(p); - if (error) + if (error) { error = EIO; + } else { + /* + * task_thaw() succeeded. + * + * We increment memorystatus_frozen_count on the sysctl freeze path. + * And so we need the P_MEMSTAT_FROZEN to decrement the frozen count + * when this process exits. + * + * proc_list_lock(); + * p->p_memstat_state &= ~P_MEMSTAT_FROZEN; + * proc_list_unlock(); + */ + } + proc_rele(p); return error; } } @@ -1197,17 +1434,201 @@ sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS return EINVAL; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_thaw, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_thaw, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorystatus_available_pages_thaw, "I", ""); +typedef struct _global_freezable_status { + boolean_t freeze_pages_threshold_crossed; + boolean_t freeze_eligible_procs_available; + boolean_t freeze_scheduled_in_future; +}global_freezable_status_t; + +typedef struct _proc_freezable_status { + boolean_t freeze_has_memstat_state; + boolean_t freeze_has_pages_min; + int freeze_has_probability; + boolean_t freeze_attempted; + uint32_t p_memstat_state; + uint32_t p_pages; + int p_freeze_error_code; + int p_pid; + char p_name[MAXCOMLEN + 1]; +}proc_freezable_status_t; + +#define MAX_FREEZABLE_PROCESSES 100 + +static int +memorystatus_freezer_get_status(user_addr_t buffer, size_t buffer_size, int32_t *retval) +{ + uint32_t proc_count = 0, i = 0; + global_freezable_status_t *list_head; + proc_freezable_status_t *list_entry; + size_t list_size = 0; + proc_t p; + memstat_bucket_t *bucket; + uint32_t state = 0, pages = 0, entry_count = 0; + boolean_t try_freeze = TRUE; + int error = 0, probability_of_use = 0; + + + if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE == FALSE) { + return ENOTSUP; + } + + list_size = sizeof(global_freezable_status_t) + (sizeof(proc_freezable_status_t) * MAX_FREEZABLE_PROCESSES); + + if (buffer_size < list_size) { + return EINVAL; + } + + list_head = (global_freezable_status_t*)kalloc(list_size); + if (list_head == NULL) { + return ENOMEM; + } + + memset(list_head, 0, list_size); + + list_size = sizeof(global_freezable_status_t); + + proc_list_lock(); + + uint64_t curr_time = mach_absolute_time(); + + list_head->freeze_pages_threshold_crossed = (memorystatus_available_pages < memorystatus_freeze_threshold); + list_head->freeze_eligible_procs_available = ((memorystatus_suspended_count - memorystatus_frozen_count) > memorystatus_freeze_suspended_threshold); + list_head->freeze_scheduled_in_future = (curr_time < memorystatus_freezer_thread_next_run_ts); + + list_entry = (proc_freezable_status_t*) ((uintptr_t)list_head + sizeof(global_freezable_status_t)); + + bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; + + entry_count = (memorystatus_global_probabilities_size / sizeof(memorystatus_internal_probabilities_t)); + + p = memorystatus_get_first_proc_locked(&i, FALSE); + proc_count++; + + while ((proc_count <= MAX_FREEZABLE_PROCESSES) && + (p) && + (list_size < buffer_size)) { + if (isApp(p) == FALSE) { + p = memorystatus_get_next_proc_locked(&i, p, FALSE); + proc_count++; + continue; + } + + strlcpy(list_entry->p_name, p->p_name, MAXCOMLEN + 1); + + list_entry->p_pid = p->p_pid; + + state = p->p_memstat_state; + + if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED | P_MEMSTAT_FREEZE_IGNORE)) || + !(state & P_MEMSTAT_SUSPENDED)) { + try_freeze = list_entry->freeze_has_memstat_state = FALSE; + } else { + try_freeze = list_entry->freeze_has_memstat_state = TRUE; + } + + list_entry->p_memstat_state = state; + + memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); + if (pages < memorystatus_freeze_pages_min) { + try_freeze = list_entry->freeze_has_pages_min = FALSE; + } else { + list_entry->freeze_has_pages_min = TRUE; + if (try_freeze != FALSE) { + try_freeze = TRUE; + } + } + + list_entry->p_pages = pages; + + if (entry_count) { + uint32_t j = 0; + for (j = 0; j < entry_count; j++) { + if (strncmp(memorystatus_global_probabilities_table[j].proc_name, + p->p_name, + MAXCOMLEN + 1) == 0) { + probability_of_use = memorystatus_global_probabilities_table[j].use_probability; + break; + } + } + + list_entry->freeze_has_probability = probability_of_use; + + if (probability_of_use && try_freeze != FALSE) { + try_freeze = TRUE; + } else { + try_freeze = FALSE; + } + } else { + if (try_freeze != FALSE) { + try_freeze = TRUE; + } + list_entry->freeze_has_probability = -1; + } + + if (try_freeze) { + uint32_t purgeable, wired, clean, dirty, shared; + uint32_t max_pages = 0; + int freezer_error_code = 0; + + error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, TRUE /* eval only */); + + if (error) { + list_entry->p_freeze_error_code = freezer_error_code; + } + + list_entry->freeze_attempted = TRUE; + } + + list_entry++; + + list_size += sizeof(proc_freezable_status_t); + + p = memorystatus_get_next_proc_locked(&i, p, FALSE); + proc_count++; + } + + proc_list_unlock(); + + buffer_size = list_size; + + error = copyout(list_head, buffer, buffer_size); + if (error == 0) { + *retval = buffer_size; + } else { + *retval = 0; + } + + list_size = sizeof(global_freezable_status_t) + (sizeof(proc_freezable_status_t) * MAX_FREEZABLE_PROCESSES); + kfree(list_head, list_size); + + MEMORYSTATUS_DEBUG(1, "memorystatus_freezer_get_status: returning %d (%lu - size)\n", error, (unsigned long)*list_size); + + return error; +} + +static int +memorystatus_freezer_control(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) +{ + int err = ENOTSUP; + + if (flags == FREEZER_CONTROL_GET_STATUS) { + err = memorystatus_freezer_get_status(buffer, buffer_size, retval); + } + + return err; +} + #endif /* CONFIG_FREEZE */ #endif /* DEVELOPMENT || DEBUG */ extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation, - void *parameter, - integer_t priority, - thread_t *new_thread); + void *parameter, + integer_t priority, + thread_t *new_thread); #if DEVELOPMENT || DEBUG @@ -1215,12 +1636,13 @@ static int sysctl_memorystatus_disconnect_page_mappings SYSCTL_HANDLER_ARGS { #pragma unused(arg1, arg2) - int error = 0, pid = 0; - proc_t p; + int error = 0, pid = 0; + proc_t p; error = sysctl_handle_int(oidp, &pid, 0, req); - if (error || !req->newptr) - return (error); + if (error || !req->newptr) { + return error; + } lck_mtx_lock(&disconnect_page_mappings_mutex); @@ -1234,24 +1656,24 @@ sysctl_memorystatus_disconnect_page_mappings SYSCTL_HANDLER_ARGS proc_rele(p); - if (error) + if (error) { error = EIO; - } else + } + } else { error = EINVAL; + } } lck_mtx_unlock(&disconnect_page_mappings_mutex); return error; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_disconnect_page_mappings, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_disconnect_page_mappings, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorystatus_disconnect_page_mappings, "I", ""); #endif /* DEVELOPMENT || DEBUG */ - -#if CONFIG_JETSAM /* * Picks the sorting routine for a given jetsam priority band. * @@ -1261,37 +1683,44 @@ SYSCTL_PROC(_kern, OID_AUTO, memorystatus_disconnect_page_mappings, CTLTYPE_INT| * Currently sort_order is only meaningful when handling * coalitions. * - * Return: + * Return: * 0 on success - * non-0 on failure + * non-0 on failure */ -static int memorystatus_sort_bucket(unsigned int bucket_index, int sort_order) +static int +memorystatus_sort_bucket(unsigned int bucket_index, int sort_order) { int coal_sort_order; /* * Verify the jetsam priority */ - if (bucket_index >= MEMSTAT_BUCKET_COUNT) { - return(EINVAL); - } + if (bucket_index >= MEMSTAT_BUCKET_COUNT) { + return EINVAL; + } #if DEVELOPMENT || DEBUG - if (sort_order == JETSAM_SORT_DEFAULT) { + if (sort_order == JETSAM_SORT_DEFAULT) { coal_sort_order = COALITION_SORT_DEFAULT; } else { - coal_sort_order = sort_order; /* only used for testing scenarios */ + coal_sort_order = sort_order; /* only used for testing scenarios */ } #else /* Verify default */ - if (sort_order == JETSAM_SORT_DEFAULT) { + if (sort_order == JETSAM_SORT_DEFAULT) { coal_sort_order = COALITION_SORT_DEFAULT; } else { - return(EINVAL); + return EINVAL; } #endif proc_list_lock(); + + if (memstat_bucket[bucket_index].count == 0) { + proc_list_unlock(); + return 0; + } + switch (bucket_index) { case JETSAM_PRIORITY_FOREGROUND: if (memorystatus_sort_by_largest_coalition_locked(bucket_index, coal_sort_order) == 0) { @@ -1306,39 +1735,40 @@ static int memorystatus_sort_bucket(unsigned int bucket_index, int sort_order) break; } proc_list_unlock(); - - return(0); + + return 0; } /* * Sort processes by size for a single jetsam bucket. */ -static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index) +static void +memorystatus_sort_by_largest_process_locked(unsigned int bucket_index) { proc_t p = NULL, insert_after_proc = NULL, max_proc = NULL; proc_t next_p = NULL, prev_max_proc = NULL; uint32_t pages = 0, max_pages = 0; memstat_bucket_t *current_bucket; - + if (bucket_index >= MEMSTAT_BUCKET_COUNT) { return; } - + current_bucket = &memstat_bucket[bucket_index]; p = TAILQ_FIRST(¤t_bucket->list); while (p) { - memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL); + memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); max_pages = pages; max_proc = p; prev_max_proc = p; - + while ((next_p = TAILQ_NEXT(p, p_memstat_list)) != NULL) { /* traversing list until we find next largest process */ - p=next_p; - memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL); + p = next_p; + memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); if (pages > max_pages) { max_pages = pages; max_proc = p; @@ -1362,9 +1792,9 @@ static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_inde } } -#endif /* CONFIG_JETSAM */ - -static proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) { +static proc_t +memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) +{ memstat_bucket_t *current_bucket; proc_t next_p; @@ -1380,14 +1810,16 @@ static proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boo next_p = TAILQ_FIRST(¤t_bucket->list); } } - + return next_p; } -static proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search) { +static proc_t +memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search) +{ memstat_bucket_t *current_bucket; proc_t next_p; - + if (!p || ((*bucket_index) >= MEMSTAT_BUCKET_COUNT)) { return NULL; } @@ -1401,16 +1833,66 @@ static proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc return next_p; } +/* + * Structure to hold state for a jetsam thread. + * Typically there should be a single jetsam thread + * unless parallel jetsam is enabled. + */ +struct jetsam_thread_state { + boolean_t inited; /* if the thread is initialized */ + int memorystatus_wakeup; /* wake channel */ + int index; /* jetsam thread index */ + thread_t thread; /* jetsam thread pointer */ +} *jetsam_threads; + +/* Maximum number of jetsam threads allowed */ +#define JETSAM_THREADS_LIMIT 3 + +/* Number of active jetsam threads */ +_Atomic int active_jetsam_threads = 1; + +/* Number of maximum jetsam threads configured */ +int max_jetsam_threads = JETSAM_THREADS_LIMIT; + +/* + * Global switch for enabling fast jetsam. Fast jetsam is + * hooked up via the system_override() system call. It has the + * following effects: + * - Raise the jetsam threshold ("clear-the-deck") + * - Enabled parallel jetsam on eligible devices + */ +int fast_jetsam_enabled = 0; + +/* Routine to find the jetsam state structure for the current jetsam thread */ +static inline struct jetsam_thread_state * +jetsam_current_thread(void) +{ + for (int thr_id = 0; thr_id < max_jetsam_threads; thr_id++) { + if (jetsam_threads[thr_id].thread == current_thread()) { + return &(jetsam_threads[thr_id]); + } + } + panic("jetsam_current_thread() is being called from a non-jetsam thread\n"); + /* Contol should not reach here */ + return NULL; +} + + __private_extern__ void memorystatus_init(void) { - thread_t thread = THREAD_NULL; kern_return_t result; int i; #if CONFIG_FREEZE + memorystatus_freeze_jetsam_band = JETSAM_PRIORITY_UI_SUPPORT; + memorystatus_frozen_processes_max = FREEZE_PROCESSES_MAX; + memorystatus_frozen_shared_mb_max = ((MAX_FROZEN_SHARED_MB_PERCENT * max_task_footprint_mb) / 100); /* 10% of the system wide task limit */ + memorystatus_freeze_shared_mb_per_process_max = (memorystatus_frozen_shared_mb_max / 4); memorystatus_freeze_pages_min = FREEZE_PAGES_MIN; memorystatus_freeze_pages_max = FREEZE_PAGES_MAX; + memorystatus_max_frozen_demotions_daily = MAX_FROZEN_PROCESS_DEMOTIONS; + memorystatus_thaw_count_demotion_threshold = MIN_THAW_DEMOTION_THRESHOLD; #endif #if DEVELOPMENT || DEBUG @@ -1418,19 +1900,24 @@ memorystatus_init(void) disconnect_page_mappings_lck_grp = lck_grp_alloc_init("disconnect_page_mappings", disconnect_page_mappings_lck_grp_attr); lck_mtx_init(&disconnect_page_mappings_mutex, disconnect_page_mappings_lck_grp, NULL); -#endif - nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); - nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); - + if (kill_on_no_paging_space == TRUE) { + max_kill_priority = JETSAM_PRIORITY_MAX; + } +#endif + + /* Init buckets */ for (i = 0; i < MEMSTAT_BUCKET_COUNT; i++) { TAILQ_INIT(&memstat_bucket[i].list); memstat_bucket[i].count = 0; } - memorystatus_idle_demotion_call = thread_call_allocate((thread_call_func_t)memorystatus_perform_idle_demotion, NULL); +#if CONFIG_JETSAM + nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); + nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); + /* Apply overrides */ PE_get_default("kern.jetsam_delta", &delta_percentage, sizeof(delta_percentage)); if (delta_percentage == 0) { @@ -1445,13 +1932,11 @@ memorystatus_init(void) assert(pressure_threshold_percentage < 100); PE_get_default("kern.jetsam_freeze_threshold", &freeze_threshold_percentage, sizeof(freeze_threshold_percentage)); assert(freeze_threshold_percentage < 100); - - if (!PE_parse_boot_argn("jetsam_aging_policy", &jetsam_aging_policy, - sizeof (jetsam_aging_policy))) { + if (!PE_parse_boot_argn("jetsam_aging_policy", &jetsam_aging_policy, + sizeof(jetsam_aging_policy))) { if (!PE_get_default("kern.jetsam_aging_policy", &jetsam_aging_policy, - sizeof(jetsam_aging_policy))) { - + sizeof(jetsam_aging_policy))) { jetsam_aging_policy = kJetsamAgingPolicyLegacy; } } @@ -1461,34 +1946,33 @@ memorystatus_init(void) } switch (jetsam_aging_policy) { + case kJetsamAgingPolicyNone: + system_procs_aging_band = JETSAM_PRIORITY_IDLE; + applications_aging_band = JETSAM_PRIORITY_IDLE; + break; - case kJetsamAgingPolicyNone: - system_procs_aging_band = JETSAM_PRIORITY_IDLE; - applications_aging_band = JETSAM_PRIORITY_IDLE; - break; - - case kJetsamAgingPolicyLegacy: - /* - * Legacy behavior where some daemons get a 10s protection once - * AND only before the first clean->dirty->clean transition before - * going into IDLE band. - */ - system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; - applications_aging_band = JETSAM_PRIORITY_IDLE; - break; + case kJetsamAgingPolicyLegacy: + /* + * Legacy behavior where some daemons get a 10s protection once + * AND only before the first clean->dirty->clean transition before + * going into IDLE band. + */ + system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; + applications_aging_band = JETSAM_PRIORITY_IDLE; + break; - case kJetsamAgingPolicySysProcsReclaimedFirst: - system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; - applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; - break; + case kJetsamAgingPolicySysProcsReclaimedFirst: + system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; + applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; + break; - case kJetsamAgingPolicyAppsReclaimedFirst: - system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; - applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; - break; + case kJetsamAgingPolicyAppsReclaimedFirst: + system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; + applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; + break; - default: - break; + default: + break; } /* @@ -1500,54 +1984,94 @@ memorystatus_init(void) assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > system_procs_aging_band); assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > applications_aging_band); -#if CONFIG_JETSAM /* Take snapshots for idle-exit kills by default? First check the boot-arg... */ - if (!PE_parse_boot_argn("jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof (memorystatus_idle_snapshot))) { - /* ...no boot-arg, so check the device tree */ - PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot)); + if (!PE_parse_boot_argn("jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot))) { + /* ...no boot-arg, so check the device tree */ + PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot)); } memorystatus_delta = delta_percentage * atop_64(max_mem) / 100; memorystatus_available_pages_critical_idle_offset = idle_offset_percentage * atop_64(max_mem) / 100; memorystatus_available_pages_critical_base = (critical_threshold_percentage / delta_percentage) * memorystatus_delta; memorystatus_policy_more_free_offset_pages = (policy_more_free_offset_percentage / delta_percentage) * memorystatus_delta; - + + /* Jetsam Loop Detection */ + if (max_mem <= (512 * 1024 * 1024)) { + /* 512 MB devices */ + memorystatus_jld_eval_period_msecs = 8000; /* 8000 msecs == 8 second window */ + } else { + /* 1GB and larger devices */ + memorystatus_jld_eval_period_msecs = 6000; /* 6000 msecs == 6 second window */ + } + + memorystatus_jld_enabled = TRUE; + + /* No contention at this point */ + memorystatus_update_levels_locked(FALSE); + +#endif /* CONFIG_JETSAM */ + memorystatus_jetsam_snapshot_max = maxproc; - memorystatus_jetsam_snapshot = - (memorystatus_jetsam_snapshot_t*)kalloc(sizeof(memorystatus_jetsam_snapshot_t) + - sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_max); + + memorystatus_jetsam_snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + + (sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_max); + + memorystatus_jetsam_snapshot = + (memorystatus_jetsam_snapshot_t*)kalloc(memorystatus_jetsam_snapshot_size); if (!memorystatus_jetsam_snapshot) { panic("Could not allocate memorystatus_jetsam_snapshot"); } + memorystatus_jetsam_snapshot_copy = + (memorystatus_jetsam_snapshot_t*)kalloc(memorystatus_jetsam_snapshot_size); + if (!memorystatus_jetsam_snapshot_copy) { + panic("Could not allocate memorystatus_jetsam_snapshot_copy"); + } + nanoseconds_to_absolutetime((uint64_t)JETSAM_SNAPSHOT_TIMEOUT_SECS * NSEC_PER_SEC, &memorystatus_jetsam_snapshot_timeout); memset(&memorystatus_at_boot_snapshot, 0, sizeof(memorystatus_jetsam_snapshot_t)); - /* No contention at this point */ - memorystatus_update_levels_locked(FALSE); - - /* Jetsam Loop Detection */ - if (max_mem <= (512 * 1024 * 1024)) { - /* 512 MB devices */ - memorystatus_jld_eval_period_msecs = 8000; /* 8000 msecs == 8 second window */ - } else { - /* 1GB and larger devices */ - memorystatus_jld_eval_period_msecs = 6000; /* 6000 msecs == 6 second window */ - } -#endif - #if CONFIG_FREEZE memorystatus_freeze_threshold = (freeze_threshold_percentage / delta_percentage) * memorystatus_delta; #endif - - result = kernel_thread_start_priority(memorystatus_thread, NULL, 95 /* MAXPRI_KERNEL */, &thread); - if (result == KERN_SUCCESS) { - thread_deallocate(thread); - } else { - panic("Could not create memorystatus_thread"); + + /* Check the boot-arg to see if fast jetsam is allowed */ + if (!PE_parse_boot_argn("fast_jetsam_enabled", &fast_jetsam_enabled, sizeof(fast_jetsam_enabled))) { + fast_jetsam_enabled = 0; } -} + + /* Check the boot-arg to configure the maximum number of jetsam threads */ + if (!PE_parse_boot_argn("max_jetsam_threads", &max_jetsam_threads, sizeof(max_jetsam_threads))) { + max_jetsam_threads = JETSAM_THREADS_LIMIT; + } + + /* Restrict the maximum number of jetsam threads to JETSAM_THREADS_LIMIT */ + if (max_jetsam_threads > JETSAM_THREADS_LIMIT) { + max_jetsam_threads = JETSAM_THREADS_LIMIT; + } + + /* For low CPU systems disable fast jetsam mechanism */ + if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) { + max_jetsam_threads = 1; + fast_jetsam_enabled = 0; + } + + /* Initialize the jetsam_threads state array */ + jetsam_threads = kalloc(sizeof(struct jetsam_thread_state) * max_jetsam_threads); + + /* Initialize all the jetsam threads */ + for (i = 0; i < max_jetsam_threads; i++) { + result = kernel_thread_start_priority(memorystatus_thread, NULL, 95 /* MAXPRI_KERNEL */, &jetsam_threads[i].thread); + if (result == KERN_SUCCESS) { + jetsam_threads[i].inited = FALSE; + jetsam_threads[i].index = i; + thread_deallocate(jetsam_threads[i].thread); + } else { + panic("Could not create memorystatus_thread %d", i); + } + } +} /* Centralised for the purposes of allowing panic-on-jetsam */ extern void @@ -1555,27 +2079,28 @@ vm_run_compactor(void); /* * The jetsam no frills kill call - * Return: 0 on success + * Return: 0 on success * error code on failure (EINVAL...) */ static int -jetsam_do_kill(proc_t p, int jetsam_flags, os_reason_t jetsam_reason) { +jetsam_do_kill(proc_t p, int jetsam_flags, os_reason_t jetsam_reason) +{ int error = 0; error = exit_with_reason(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags, jetsam_reason); - return(error); + return error; } /* * Wrapper for processes exiting with memorystatus details */ static boolean_t -memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason) { - +memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason) +{ int error = 0; __unused pid_t victim_pid = p->p_pid; - KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_START, - victim_pid, cause, vm_page_free_count, 0, 0); + KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_START, + victim_pid, cause, vm_page_free_count, 0, 0); DTRACE_MEMORYSTATUS3(memorystatus_do_kill, proc_t, p, os_reason_t, jetsam_reason, uint32_t, cause); #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) @@ -1585,24 +2110,37 @@ memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason) { #else #pragma unused(cause) #endif + + if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) { + printf("memorystatus: killing process %d [%s] in high band %s (%d) - memorystatus_available_pages: %llu\n", p->p_pid, + (*p->p_name ? p->p_name : "unknown"), + memorystatus_priority_band_name(p->p_memstat_effectivepriority), p->p_memstat_effectivepriority, + (uint64_t)memorystatus_available_pages); + } + + /* + * The jetsam_reason (os_reason_t) has enough information about the kill cause. + * We don't really need jetsam_flags anymore, so it's okay that not all possible kill causes have been mapped. + */ int jetsam_flags = P_LTERM_JETSAM; switch (cause) { - case kMemorystatusKilledHiwat: jetsam_flags |= P_JETSAM_HIWAT; break; - case kMemorystatusKilledVnodes: jetsam_flags |= P_JETSAM_VNODE; break; - case kMemorystatusKilledVMPageShortage: jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break; - case kMemorystatusKilledVMThrashing: jetsam_flags |= P_JETSAM_VMTHRASHING; break; - case kMemorystatusKilledFCThrashing: jetsam_flags |= P_JETSAM_FCTHRASHING; break; - case kMemorystatusKilledPerProcessLimit: jetsam_flags |= P_JETSAM_PID; break; - case kMemorystatusKilledIdleExit: jetsam_flags |= P_JETSAM_IDLEEXIT; break; + case kMemorystatusKilledHiwat: jetsam_flags |= P_JETSAM_HIWAT; break; + case kMemorystatusKilledVnodes: jetsam_flags |= P_JETSAM_VNODE; break; + case kMemorystatusKilledVMPageShortage: jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break; + case kMemorystatusKilledVMCompressorThrashing: + case kMemorystatusKilledVMCompressorSpaceShortage: jetsam_flags |= P_JETSAM_VMTHRASHING; break; + case kMemorystatusKilledFCThrashing: jetsam_flags |= P_JETSAM_FCTHRASHING; break; + case kMemorystatusKilledPerProcessLimit: jetsam_flags |= P_JETSAM_PID; break; + case kMemorystatusKilledIdleExit: jetsam_flags |= P_JETSAM_IDLEEXIT; break; } error = jetsam_do_kill(p, jetsam_flags, jetsam_reason); - KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_END, - victim_pid, cause, vm_page_free_count, error, 0); + KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_END, + victim_pid, cause, vm_page_free_count, error, 0); vm_run_compactor(); - return (error == 0); + return error == 0; } /* @@ -1610,25 +2148,31 @@ memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason) { */ static void -memorystatus_check_levels_locked(void) { +memorystatus_check_levels_locked(void) +{ #if CONFIG_JETSAM /* Update levels */ memorystatus_update_levels_locked(TRUE); -#endif +#else /* CONFIG_JETSAM */ + /* + * Nothing to do here currently since we update + * memorystatus_available_pages in vm_pressure_response. + */ +#endif /* CONFIG_JETSAM */ } -/* +/* * Pin a process to a particular jetsam band when it is in the background i.e. not doing active work. * For an application: that means no longer in the FG band * For a daemon: that means no longer in its 'requested' jetsam priority band */ int -memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, boolean_t effective_now) +memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, int jetsam_prio, boolean_t effective_now) { - int error = 0; + int error = 0; boolean_t enable = FALSE; - proc_t p = NULL; + proc_t p = NULL; if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE) { enable = TRUE; @@ -1640,15 +2184,12 @@ memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, p = proc_find(pid); if (p != NULL) { - if ((enable && ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) == P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) || (!enable && ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) == 0))) { /* * No change in state. */ - } else { - proc_list_lock(); if (enable) { @@ -1656,8 +2197,8 @@ memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, memorystatus_invalidate_idle_demotion_locked(p, TRUE); if (effective_now) { - if (p->p_memstat_effectivepriority < JETSAM_PRIORITY_ELEVATED_INACTIVE) { - if(memorystatus_highwater_enabled) { + if (p->p_memstat_effectivepriority < jetsam_prio) { + if (memorystatus_highwater_enabled) { /* * Process is about to transition from * inactive --> active @@ -1668,7 +2209,7 @@ memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, use_active, is_fatal); } - memorystatus_update_priority_locked(p, JETSAM_PRIORITY_ELEVATED_INACTIVE, FALSE, FALSE); + memorystatus_update_priority_locked(p, jetsam_prio, FALSE, FALSE); } } else { if (isProcessInAgingBands(p)) { @@ -1676,12 +2217,11 @@ memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, } } } else { - p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; memorystatus_invalidate_idle_demotion_locked(p, TRUE); if (effective_now) { - if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_ELEVATED_INACTIVE) { + if (p->p_memstat_effectivepriority == jetsam_prio) { memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); } } else { @@ -1695,7 +2235,6 @@ memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, } proc_rele(p); error = 0; - } else { error = ESRCH; } @@ -1704,79 +2243,74 @@ memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, } static void -memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2) +memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2) { proc_t p; uint64_t current_time = 0, idle_delay_time = 0; int demote_prio_band = 0; memstat_bucket_t *demotion_bucket; - + MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion()\n"); - + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_START, 0, 0, 0, 0, 0); - - current_time = mach_absolute_time(); - + + current_time = mach_absolute_time(); + proc_list_lock(); demote_prio_band = JETSAM_PRIORITY_IDLE + 1; for (; demote_prio_band < JETSAM_PRIORITY_MAX; demote_prio_band++) { - - if (demote_prio_band != system_procs_aging_band && demote_prio_band != applications_aging_band) + if (demote_prio_band != system_procs_aging_band && demote_prio_band != applications_aging_band) { continue; + } demotion_bucket = &memstat_bucket[demote_prio_band]; p = TAILQ_FIRST(&demotion_bucket->list); - + while (p) { MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion() found %d\n", p->p_pid); - + assert(p->p_memstat_idledeadline); assert(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS); if (current_time >= p->p_memstat_idledeadline) { - if ((isSysProc(p) && - ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_IS_DIRTY)) != P_DIRTY_IDLE_EXIT_ENABLED)) || /* system proc marked dirty*/ - task_has_assertions((struct task *)(p->task))) { /* has outstanding assertions which might indicate outstanding work too */ + ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) != P_DIRTY_IDLE_EXIT_ENABLED)) || /* system proc marked dirty*/ + task_has_assertions((struct task *)(p->task))) { /* has outstanding assertions which might indicate outstanding work too */ idle_delay_time = (isSysProc(p)) ? memorystatus_sysprocs_idle_delay_time : memorystatus_apps_idle_delay_time; p->p_memstat_idledeadline += idle_delay_time; p = TAILQ_NEXT(p, p_memstat_list); - } else { - proc_t next_proc = NULL; next_proc = TAILQ_NEXT(p, p_memstat_list); memorystatus_invalidate_idle_demotion_locked(p, TRUE); memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, false, true); - + p = next_proc; continue; - } } else { // No further candidates break; } } - } memorystatus_reschedule_idle_demotion_locked(); - + proc_list_unlock(); KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_END, 0, 0, 0, 0, 0); } static void -memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) -{ +memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) +{ boolean_t present_in_sysprocs_aging_bucket = FALSE; boolean_t present_in_apps_aging_bucket = FALSE; uint64_t idle_delay_time = 0; @@ -1792,15 +2326,13 @@ memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) return; } - if (isProcessInAgingBands(p)){ - + if (isProcessInAgingBands(p)) { if (jetsam_aging_policy != kJetsamAgingPolicyLegacy) { assert((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) != P_DIRTY_AGING_IN_PROGRESS); } if (isSysProc(p) && system_procs_aging_band) { present_in_sysprocs_aging_bucket = TRUE; - } else if (isApp(p) && applications_aging_band) { present_in_apps_aging_bucket = TRUE; } @@ -1809,10 +2341,10 @@ memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) assert(!present_in_sysprocs_aging_bucket); assert(!present_in_apps_aging_bucket); - 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", + 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", p->p_pid, p->p_memstat_dirty, set_state, (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)); - if(isSysProc(p)) { + if (isSysProc(p)) { assert((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED); } @@ -1822,19 +2354,18 @@ memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) p->p_memstat_dirty |= P_DIRTY_AGING_IN_PROGRESS; p->p_memstat_idledeadline = mach_absolute_time() + idle_delay_time; } - + assert(p->p_memstat_idledeadline); - - if (isSysProc(p) && present_in_sysprocs_aging_bucket == FALSE) { - memorystatus_scheduled_idle_demotions_sysprocs++; + if (isSysProc(p) && present_in_sysprocs_aging_bucket == FALSE) { + memorystatus_scheduled_idle_demotions_sysprocs++; } else if (isApp(p) && present_in_apps_aging_bucket == FALSE) { memorystatus_scheduled_idle_demotions_apps++; } } static void -memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) +memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) { boolean_t present_in_sysprocs_aging_bucket = FALSE; boolean_t present_in_apps_aging_bucket = FALSE; @@ -1848,7 +2379,6 @@ memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) } if (isProcessInAgingBands(p)) { - if (jetsam_aging_policy != kJetsamAgingPolicyLegacy) { assert((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) == P_DIRTY_AGING_IN_PROGRESS); } @@ -1857,7 +2387,6 @@ memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) assert(p->p_memstat_effectivepriority == system_procs_aging_band); assert(p->p_memstat_idledeadline); present_in_sysprocs_aging_bucket = TRUE; - } else if (isApp(p) && applications_aging_band) { assert(p->p_memstat_effectivepriority == applications_aging_band); assert(p->p_memstat_idledeadline); @@ -1865,49 +2394,47 @@ memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) } } - MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for pid %d (clear_state %d, demotions %d).\n", + MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for pid %d (clear_state %d, demotions %d).\n", p->p_pid, clear_state, (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)); - - + + if (clear_state) { - p->p_memstat_idledeadline = 0; - p->p_memstat_dirty &= ~P_DIRTY_AGING_IN_PROGRESS; + p->p_memstat_idledeadline = 0; + p->p_memstat_dirty &= ~P_DIRTY_AGING_IN_PROGRESS; } - - if (isSysProc(p) &&present_in_sysprocs_aging_bucket == TRUE) { + + if (isSysProc(p) && present_in_sysprocs_aging_bucket == TRUE) { memorystatus_scheduled_idle_demotions_sysprocs--; assert(memorystatus_scheduled_idle_demotions_sysprocs >= 0); - } else if (isApp(p) && present_in_apps_aging_bucket == TRUE) { memorystatus_scheduled_idle_demotions_apps--; assert(memorystatus_scheduled_idle_demotions_apps >= 0); } - assert((memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps) >= 0); + assert((memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps) >= 0); } static void -memorystatus_reschedule_idle_demotion_locked(void) { - if (0 == (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)) { - if (memstat_idle_demotion_deadline) { - /* Transitioned 1->0, so cancel next call */ - thread_call_cancel(memorystatus_idle_demotion_call); - memstat_idle_demotion_deadline = 0; - } - } else { - memstat_bucket_t *demotion_bucket; - proc_t p = NULL, p1 = NULL, p2 = NULL; - - if (system_procs_aging_band) { - +memorystatus_reschedule_idle_demotion_locked(void) +{ + if (0 == (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)) { + if (memstat_idle_demotion_deadline) { + /* Transitioned 1->0, so cancel next call */ + thread_call_cancel(memorystatus_idle_demotion_call); + memstat_idle_demotion_deadline = 0; + } + } else { + memstat_bucket_t *demotion_bucket; + proc_t p = NULL, p1 = NULL, p2 = NULL; + + if (system_procs_aging_band) { demotion_bucket = &memstat_bucket[system_procs_aging_band]; p1 = TAILQ_FIRST(&demotion_bucket->list); p = p1; } - if (applications_aging_band) { - + if (applications_aging_band) { demotion_bucket = &memstat_bucket[applications_aging_band]; p2 = TAILQ_FIRST(&demotion_bucket->list); @@ -1916,51 +2443,48 @@ memorystatus_reschedule_idle_demotion_locked(void) { } else { p = (p1 == NULL) ? p2 : p1; } - } assert(p); if (p != NULL) { assert(p && p->p_memstat_idledeadline); - if (memstat_idle_demotion_deadline != p->p_memstat_idledeadline){ + if (memstat_idle_demotion_deadline != p->p_memstat_idledeadline) { thread_call_enter_delayed(memorystatus_idle_demotion_call, p->p_memstat_idledeadline); memstat_idle_demotion_deadline = p->p_memstat_idledeadline; } } - } + } } -/* +/* * List manipulation */ - -int + +int memorystatus_add(proc_t p, boolean_t locked) { memstat_bucket_t *bucket; - + MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding pid %d with priority %d.\n", p->p_pid, p->p_memstat_effectivepriority); if (!locked) { - proc_list_lock(); - } + proc_list_lock(); + } DTRACE_MEMORYSTATUS2(memorystatus_add, proc_t, p, int32_t, p->p_memstat_effectivepriority); /* Processes marked internal do not have priority tracked */ if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { - goto exit; + goto exit; } - + bucket = &memstat_bucket[p->p_memstat_effectivepriority]; - + if (isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) { assert(bucket->count == memorystatus_scheduled_idle_demotions_sysprocs - 1); - } else if (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band)) { assert(bucket->count == memorystatus_scheduled_idle_demotions_apps - 1); - } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { /* * Entering the idle band. @@ -1975,12 +2499,12 @@ memorystatus_add(proc_t p, boolean_t locked) memorystatus_list_count++; memorystatus_check_levels_locked(); - + exit: - if (!locked) { - proc_list_unlock(); - } - + if (!locked) { + proc_list_unlock(); + } + return 0; } @@ -2011,32 +2535,32 @@ void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check) { memstat_bucket_t *old_bucket, *new_bucket; - + assert(priority < MEMSTAT_BUCKET_COUNT); - + /* Ensure that exit isn't underway, leaving the proc retained but removed from its bucket */ if ((p->p_listflag & P_LIST_EXITED) != 0) { return; } MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting %s(%d) to priority %d, inserting at %s\n", - (*p->p_name ? p->p_name : "unknown"), p->p_pid, priority, head_insert ? "head" : "tail"); + (*p->p_name ? p->p_name : "unknown"), p->p_pid, priority, head_insert ? "head" : "tail"); DTRACE_MEMORYSTATUS3(memorystatus_update_priority, proc_t, p, int32_t, p->p_memstat_effectivepriority, int, priority); #if DEVELOPMENT || DEBUG if (priority == JETSAM_PRIORITY_IDLE && /* if the process is on its way into the IDLE band */ - skip_demotion_check == FALSE && /* and it isn't via the path that will set the INACTIVE memlimits */ + skip_demotion_check == FALSE && /* and it isn't via the path that will set the INACTIVE memlimits */ (p->p_memstat_dirty & P_DIRTY_TRACK) && /* and it has 'DIRTY' tracking enabled */ ((p->p_memstat_memlimit != p->p_memstat_memlimit_inactive) || /* and we notice that the current limit isn't the right value (inactive) */ - ((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) ? ( ! (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT)) : (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT)))) /* OR type (fatal vs non-fatal) */ + ((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) ? (!(p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT)) : (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT)))) { /* OR type (fatal vs non-fatal) */ panic("memorystatus_update_priority_locked: on %s with 0x%x, prio: %d and %d\n", p->p_name, p->p_memstat_state, priority, p->p_memstat_memlimit); /* then we must catch this */ + } #endif /* DEVELOPMENT || DEBUG */ old_bucket = &memstat_bucket[p->p_memstat_effectivepriority]; if (skip_demotion_check == FALSE) { - if (isSysProc(p)) { /* * For system processes, the memorystatus_dirty_* routines take care of adding/removing @@ -2044,30 +2568,55 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser * We can, however, override that if the process has an 'elevated inactive jetsam band' attribute. */ - if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE && (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) { - priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; - - assert(! (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); + if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { + /* + * 2 types of processes can use the non-standard elevated inactive band: + * - Frozen processes that always land in memorystatus_freeze_jetsam_band + * OR + * - processes that specifically opt-in to the elevated inactive support e.g. docked processes. + */ +#if CONFIG_FREEZE + if (p->p_memstat_state & P_MEMSTAT_FROZEN) { + if (priority <= memorystatus_freeze_jetsam_band) { + priority = memorystatus_freeze_jetsam_band; + } + } else +#endif /* CONFIG_FREEZE */ + { + if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) { + priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; + } + } + assert(!(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); } } else if (isApp(p)) { - /* * Check to see if the application is being lowered in jetsam priority. If so, and: - * - it has an 'elevated inactive jetsam band' attribute, then put it in the JETSAM_PRIORITY_ELEVATED_INACTIVE band. + * - it has an 'elevated inactive jetsam band' attribute, then put it in the appropriate band. * - it is a normal application, then let it age in the aging band if that policy is in effect. */ - - if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE && (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) { - priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; - } else { + if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { +#if CONFIG_FREEZE + if (p->p_memstat_state & P_MEMSTAT_FROZEN) { + if (priority <= memorystatus_freeze_jetsam_band) { + priority = memorystatus_freeze_jetsam_band; + } + } else +#endif /* CONFIG_FREEZE */ + { + if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) { + priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; + } + } + } else { if (applications_aging_band) { - if (p->p_memstat_effectivepriority == applications_aging_band) { + if (p->p_memstat_effectivepriority == applications_aging_band) { assert(old_bucket->count == (memorystatus_scheduled_idle_demotions_apps + 1)); } if ((jetsam_aging_policy != kJetsamAgingPolicyLegacy) && (priority <= applications_aging_band)) { - assert(! (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); + assert(!(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); priority = applications_aging_band; memorystatus_schedule_idle_demotion_locked(p, TRUE); } @@ -2083,30 +2632,28 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list); old_bucket->count--; - new_bucket = &memstat_bucket[priority]; - if (head_insert) + new_bucket = &memstat_bucket[priority]; + if (head_insert) { TAILQ_INSERT_HEAD(&new_bucket->list, p, p_memstat_list); - else + } else { TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list); + } new_bucket->count++; if (memorystatus_highwater_enabled) { boolean_t is_fatal; boolean_t use_active; - /* + /* * If cached limit data is updated, then the limits * will be enforced by writing to the ledgers. */ boolean_t ledger_update_needed = TRUE; /* - * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore. - * Background limits are described via the inactive limit slots. - * - * Here, we must update the cached memory limit if the task + * Here, we must update the cached memory limit if the task * is transitioning between: - * active <--> inactive + * active <--> inactive * FG <--> BG * but: * dirty <--> clean is ignored @@ -2117,26 +2664,23 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser */ if (p->p_memstat_dirty & P_DIRTY_TRACK) { - if (skip_demotion_check == TRUE && priority == JETSAM_PRIORITY_IDLE) { CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); use_active = FALSE; } else { ledger_update_needed = FALSE; } - } else if ((priority >= JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority < JETSAM_PRIORITY_FOREGROUND)) { /* - * inactive --> active + * inactive --> active * BG --> FG * assign active state */ CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); use_active = TRUE; - } else if ((priority < JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) { /* - * active --> inactive + * active --> inactive * FG --> BG * assign inactive state */ @@ -2159,9 +2703,9 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, use_active, is_fatal); MEMORYSTATUS_DEBUG(3, "memorystatus_update_priority_locked: new limit on pid %d (%dMB %s) priority old --> new (%d --> %d) dirty?=0x%x %s\n", - p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), - (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, priority, p->p_memstat_dirty, - (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); + p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), + (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, priority, p->p_memstat_dirty, + (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); } } @@ -2169,7 +2713,7 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser * Record idle start or idle delta. */ if (p->p_memstat_effectivepriority == priority) { - /* + /* * This process is not transitioning between * jetsam priority buckets. Do nothing. */ @@ -2184,6 +2728,14 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser if (now > p->p_memstat_idle_start) { p->p_memstat_idle_delta = now - p->p_memstat_idle_start; } + + /* + * About to become active and so memory footprint could change. + * So mark it eligible for freeze-considerations next time around. + */ + if (p->p_memstat_state & P_MEMSTAT_FREEZE_IGNORE) { + p->p_memstat_state &= ~P_MEMSTAT_FREEZE_IGNORE; + } } else if (priority == JETSAM_PRIORITY_IDLE) { /* * Transitioning into the idle priority bucket. @@ -2192,6 +2744,8 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser p->p_memstat_idle_start = mach_absolute_time(); } + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CHANGE_PRIORITY), p->p_pid, priority, p->p_memstat_effectivepriority, 0, 0); + p->p_memstat_effectivepriority = priority; #if CONFIG_SECLUDED_MEMORY @@ -2202,7 +2756,7 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser (priority >= JETSAM_PRIORITY_FOREGROUND)); } #endif /* CONFIG_SECLUDED_MEMORY */ - + memorystatus_check_levels_locked(); } @@ -2231,20 +2785,14 @@ memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_inser * memlimit_inactive_is_fatal When a process is inactive and exceeds its memory footprint, * this describes whether or not it should be immediatly fatal. * - * memlimit_background This process has a high-water-mark while in the background. - * No longer meaningful. Background limits are described via - * the inactive slots. Flag is ignored. - * - * * Returns: 0 Success * non-0 Failure */ int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, boolean_t update_memlimit, - int32_t memlimit_active, boolean_t memlimit_active_is_fatal, - int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal, - __unused boolean_t memlimit_background) + int32_t memlimit_active, boolean_t memlimit_active_is_fatal, + int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal) { int ret; boolean_t head_insert = false; @@ -2252,13 +2800,13 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect MEMORYSTATUS_DEBUG(1, "memorystatus_update: changing (%s) pid %d: priority %d, user_data 0x%llx\n", (*p->p_name ? p->p_name : "unknown"), p->p_pid, priority, user_data); KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_START, p->p_pid, priority, user_data, effective, 0); - + if (priority == -1) { /* Use as shorthand for default priority */ priority = JETSAM_PRIORITY_DEFAULT; } else if ((priority == system_procs_aging_band) || (priority == applications_aging_band)) { /* Both the aging bands are reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */ - priority = JETSAM_PRIORITY_IDLE; + priority = JETSAM_PRIORITY_IDLE; } else if (priority == JETSAM_PRIORITY_IDLE_HEAD) { /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */ priority = JETSAM_PRIORITY_IDLE; @@ -2270,14 +2818,14 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect } proc_list_lock(); - + assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL)); if (effective && (p->p_memstat_state & P_MEMSTAT_PRIORITYUPDATED)) { ret = EALREADY; proc_list_unlock(); MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", p->p_pid); - goto out; + goto out; } if ((p->p_memstat_state & P_MEMSTAT_TERMINATED) || ((p->p_listflag & P_LIST_EXITED) != 0)) { @@ -2286,7 +2834,7 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect */ ret = EBUSY; proc_list_unlock(); - goto out; + goto out; } p->p_memstat_state |= P_MEMSTAT_PRIORITYUPDATED; @@ -2304,26 +2852,9 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect */ MEMORYSTATUS_DEBUG(3, "memorystatus_update(enter): pid %d, priority %d, dirty=0x%x, Active(%dMB %s), Inactive(%dMB, %s)\n", - p->p_pid, priority, p->p_memstat_dirty, - memlimit_active, (memlimit_active_is_fatal ? "F " : "NF"), - memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF")); - - if (memlimit_background) { - - /* - * With 2-level HWM support, we no longer honor P_MEMSTAT_MEMLIMIT_BACKGROUND. - * Background limits are described via the inactive limit slots. - */ - - // p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_BACKGROUND; - -#if DEVELOPMENT || DEBUG - printf("memorystatus_update: WARNING %s[%d] set unused flag P_MEMSTAT_MEMLIMIT_BACKGROUND [A==%dMB %s] [IA==%dMB %s]\n", - (*p->p_name ? p->p_name : "unknown"), p->p_pid, - memlimit_active, (memlimit_active_is_fatal ? "F " : "NF"), - memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF")); -#endif /* DEVELOPMENT || DEBUG */ - } + p->p_pid, priority, p->p_memstat_dirty, + memlimit_active, (memlimit_active_is_fatal ? "F " : "NF"), + memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF")); if (memlimit_active <= 0) { /* @@ -2336,7 +2867,7 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect /* * For backward compatibility with some unexplained launchd behavior, * we allow a zero sized limit. But we still enforce system_wide limit - * when written to the ledgers. + * when written to the ledgers. */ if (memlimit_active < 0) { @@ -2389,9 +2920,9 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect task_set_phys_footprint_limit_internal(p->task, ((p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1), NULL, use_active, is_fatal); MEMORYSTATUS_DEBUG(3, "memorystatus_update: init: limit on pid %d (%dMB %s) targeting priority(%d) dirty?=0x%x %s\n", - p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), - (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), priority, p->p_memstat_dirty, - (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); + p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), + (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), priority, p->p_memstat_dirty, + (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); } } @@ -2400,13 +2931,12 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect * But, we could be removing it from those buckets. * Check and take appropriate steps if so. */ - + if (isProcessInAgingBands(p)) { - memorystatus_invalidate_idle_demotion_locked(p, TRUE); memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); } else { - if (jetsam_aging_policy == kJetsamAgingPolicyLegacy && priority == JETSAM_PRIORITY_IDLE) { + if (jetsam_aging_policy == kJetsamAgingPolicyLegacy && priority == JETSAM_PRIORITY_IDLE) { /* * Daemons with 'inactive' limits will go through the dirty tracking codepath. * This path deals with apps that may have 'inactive' limits e.g. WebContent processes. @@ -2435,25 +2965,22 @@ memorystatus_remove(proc_t p, boolean_t locked) { int ret; memstat_bucket_t *bucket; - boolean_t reschedule = FALSE; + boolean_t reschedule = FALSE; MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing pid %d\n", p->p_pid); - if (!locked) { - proc_list_lock(); - } + if (!locked) { + proc_list_lock(); + } assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL)); - + bucket = &memstat_bucket[p->p_memstat_effectivepriority]; if (isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) { - assert(bucket->count == memorystatus_scheduled_idle_demotions_sysprocs); reschedule = TRUE; - } else if (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band)) { - assert(bucket->count == memorystatus_scheduled_idle_demotions_apps); reschedule = TRUE; } @@ -2477,28 +3004,34 @@ memorystatus_remove(proc_t p, boolean_t locked) /* If awaiting demotion to the idle band, clean up */ if (reschedule) { memorystatus_invalidate_idle_demotion_locked(p, TRUE); - memorystatus_reschedule_idle_demotion_locked(); + memorystatus_reschedule_idle_demotion_locked(); } memorystatus_check_levels_locked(); -#if CONFIG_FREEZE +#if CONFIG_FREEZE if (p->p_memstat_state & (P_MEMSTAT_FROZEN)) { + if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { + p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; + memorystatus_refreeze_eligible_count--; + } + memorystatus_frozen_count--; + memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages; + p->p_memstat_freeze_sharedanon_pages = 0; } if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { - memorystatus_suspended_footprint_total -= p->p_memstat_suspendedfootprint; memorystatus_suspended_count--; } #endif - if (!locked) { - proc_list_unlock(); - } + if (!locked) { + proc_list_unlock(); + } if (p) { - ret = 0; + ret = 0; } else { ret = ESRCH; } @@ -2511,59 +3044,65 @@ memorystatus_remove(proc_t p, boolean_t locked) * * Return: * 0 on success - * non-0 on failure + * non-0 on failure * * The proc_list_lock is held by the caller. */ static int -memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol) { +memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol) +{ /* See that the process isn't marked for termination */ if (target_p->p_memstat_dirty & P_DIRTY_TERMINATED) { return EBUSY; } - + /* Idle exit requires that process be tracked */ if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) && - !(pcontrol & PROC_DIRTY_TRACK)) { + !(pcontrol & PROC_DIRTY_TRACK)) { return EINVAL; } /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */ if ((pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) && - !(pcontrol & PROC_DIRTY_TRACK)) { + !(pcontrol & PROC_DIRTY_TRACK)) { + return EINVAL; + } + + /* Only one type of DEFER behavior is allowed.*/ + if ((pcontrol & PROC_DIRTY_DEFER) && + (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) { return EINVAL; } /* Deferral is only relevant if idle exit is specified */ - if ((pcontrol & PROC_DIRTY_DEFER) && - !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) { + if (((pcontrol & PROC_DIRTY_DEFER) || + (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) && + !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) { return EINVAL; } - - return(0); + + return 0; } static void -memorystatus_update_idle_priority_locked(proc_t p) { +memorystatus_update_idle_priority_locked(proc_t p) +{ int32_t priority; MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p->p_pid, p->p_memstat_dirty); - assert(isSysProc(p)); - - if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) { + assert(isSysProc(p)); + if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) { priority = (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) ? system_procs_aging_band : JETSAM_PRIORITY_IDLE; } else { priority = p->p_memstat_requestedpriority; } - - if (priority != p->p_memstat_effectivepriority) { + if (priority != p->p_memstat_effectivepriority) { if ((jetsam_aging_policy == kJetsamAgingPolicyLegacy) && (priority == JETSAM_PRIORITY_IDLE)) { - /* * This process is on its way into the IDLE band. The system is * using 'legacy' jetsam aging policy. That means, this process @@ -2572,13 +3111,24 @@ memorystatus_update_idle_priority_locked(proc_t p) { * explicitly because it won't be going through the demotion paths * that take care to apply the limits appropriately. */ - memorystatus_update_priority_locked(p, priority, false, true); + if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { + /* + * This process has the 'elevated inactive jetsam band' attribute. + * So, there will be no trip to IDLE after all. + * Instead, we pin the process in the elevated band, + * where its ACTIVE limits will apply. + */ + + priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; + } + + memorystatus_update_priority_locked(p, priority, false, true); } else { memorystatus_update_priority_locked(p, priority, false, false); } } -} +} /* * Processes can opt to have their state tracked by the kernel, indicating when they are busy (dirty) or idle @@ -2598,18 +3148,19 @@ memorystatus_update_idle_priority_locked(proc_t p) { */ int -memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { +memorystatus_dirty_track(proc_t p, uint32_t pcontrol) +{ unsigned int old_dirty; boolean_t reschedule = FALSE; boolean_t already_deferred = FALSE; boolean_t defer_now = FALSE; int ret = 0; - + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_TRACK), - p->p_pid, p->p_memstat_dirty, pcontrol, 0, 0); - + p->p_pid, p->p_memstat_dirty, pcontrol, 0, 0); + proc_list_lock(); - + if ((p->p_listflag & P_LIST_EXITED) != 0) { /* * Process is on its way out. @@ -2622,13 +3173,13 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { ret = EPERM; goto exit; } - + if ((ret = memorystatus_validate_track_flags(p, pcontrol)) != 0) { /* error */ goto exit; } - old_dirty = p->p_memstat_dirty; + old_dirty = p->p_memstat_dirty; /* These bits are cumulative, as per */ if (pcontrol & PROC_DIRTY_TRACK) { @@ -2636,7 +3187,7 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { } if (pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) { - p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT; + p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT; } if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) { @@ -2649,37 +3200,38 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { /* This can be set and cleared exactly once. */ - if (pcontrol & PROC_DIRTY_DEFER) { - - if ( !(old_dirty & P_DIRTY_DEFER)) { + if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) { + if ((pcontrol & (PROC_DIRTY_DEFER)) && + !(old_dirty & P_DIRTY_DEFER)) { p->p_memstat_dirty |= P_DIRTY_DEFER; } + if ((pcontrol & (PROC_DIRTY_DEFER_ALWAYS)) && + !(old_dirty & P_DIRTY_DEFER_ALWAYS)) { + p->p_memstat_dirty |= P_DIRTY_DEFER_ALWAYS; + } + defer_now = TRUE; } MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for pid %d\n", - ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) ? "Y" : "N", - defer_now ? "Y" : "N", - p->p_memstat_dirty & P_DIRTY ? "Y" : "N", - p->p_pid); + ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) ? "Y" : "N", + defer_now ? "Y" : "N", + p->p_memstat_dirty & P_DIRTY ? "Y" : "N", + p->p_pid); /* Kick off or invalidate the idle exit deferment if there's a state transition. */ if (!(p->p_memstat_dirty & P_DIRTY_IS_DIRTY)) { if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { - if (defer_now && !already_deferred) { - /* - * Request to defer a clean process that's idle-exit enabled + * Request to defer a clean process that's idle-exit enabled * and not already in the jetsam deferred band. Most likely a * new launch. */ memorystatus_schedule_idle_demotion_locked(p, TRUE); reschedule = TRUE; - } else if (!defer_now) { - /* * The process isn't asking for the 'aging' facility. * Could be that it is: @@ -2691,7 +3243,7 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { * some processes have tried to use this to * opt out of the 'aging' facility. */ - + memorystatus_invalidate_idle_demotion_locked(p, TRUE); } else { /* @@ -2699,7 +3251,7 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { * we'll go ahead and opt it in because this is likely * a new launch (clean process, dirty tracking enabled) */ - + memorystatus_schedule_idle_demotion_locked(p, TRUE); } @@ -2707,10 +3259,9 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { } } } else { - /* * We are trying to operate on a dirty process. Dirty processes have to - * be removed from the deferred band. The question is do we reset the + * be removed from the deferred band. The question is do we reset the * deferred state or not? * * This could be a legal request like: @@ -2735,7 +3286,6 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { memorystatus_invalidate_idle_demotion_locked(p, TRUE); reschedule = TRUE; } else { - boolean_t reset_state = (jetsam_aging_policy != kJetsamAgingPolicyLegacy) ? TRUE : FALSE; memorystatus_invalidate_idle_demotion_locked(p, reset_state); @@ -2744,21 +3294,22 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) { } memorystatus_update_idle_priority_locked(p); - + if (reschedule) { memorystatus_reschedule_idle_demotion_locked(); } - + ret = 0; - -exit: + +exit: proc_list_unlock(); - + return ret; } int -memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { +memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) +{ int ret; boolean_t kill = false; boolean_t reschedule = FALSE; @@ -2783,18 +3334,19 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { goto exit; } - if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) + if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { was_dirty = TRUE; + } if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) { /* Dirty tracking not enabled */ - ret = EINVAL; + ret = EINVAL; } else if (pcontrol && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) { - /* + /* * Process is set to be terminated and we're attempting to mark it dirty. * Set for termination and marking as clean is OK - see . */ - ret = EBUSY; + ret = EBUSY; } else { int flag = (self == TRUE) ? P_DIRTY : P_DIRTY_SHUTDOWN; if (pcontrol && !(p->p_memstat_dirty & flag)) { @@ -2809,7 +3361,7 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { kill = true; } else if ((flag == P_DIRTY) && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) { /* Kill previously terminated processes if set clean */ - kill = true; + kill = true; } p->p_memstat_dirty &= ~flag; memorystatus_dirty_count--; @@ -2824,18 +3376,19 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { goto exit; } - if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) + if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { now_dirty = TRUE; + } if ((was_dirty == TRUE && now_dirty == FALSE) || (was_dirty == FALSE && now_dirty == TRUE)) { - /* Manage idle exit deferral, if applied */ if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { - /* * Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band OR it might be heading back * there once it's clean again. For the legacy case, this only applies if it has some protection window left. + * P_DIRTY_DEFER: one-time protection window given at launch + * P_DIRTY_DEFER_ALWAYS: protection window given for every dirty->clean transition. Like non-legacy mode. * * Non-Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band. It will always stop over * in that band on it's way to IDLE. @@ -2846,28 +3399,29 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE" * * The process will move from its aging band to its higher requested - * jetsam band. + * jetsam band. */ boolean_t reset_state = (jetsam_aging_policy != kJetsamAgingPolicyLegacy) ? TRUE : FALSE; memorystatus_invalidate_idle_demotion_locked(p, reset_state); reschedule = TRUE; } else { - /* * Process is back from "dirty" to "clean". */ if (jetsam_aging_policy == kJetsamAgingPolicyLegacy) { - if (mach_absolute_time() >= p->p_memstat_idledeadline) { + if (((p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) == FALSE) && + (mach_absolute_time() >= p->p_memstat_idledeadline)) { /* - * The process' deadline has expired. It currently + * The process' hasn't enrolled in the "always defer after dirty" + * mode and its deadline has expired. It currently * does not reside in any of the aging buckets. - * - * It's on its way to the JETSAM_PRIORITY_IDLE + * + * It's on its way to the JETSAM_PRIORITY_IDLE * bucket via memorystatus_update_idle_priority_locked() * below. - + * * So all we need to do is reset all the state on the * process that's related to the aging bucket i.e. * the AGING_IN_PROGRESS flag and the timer deadline. @@ -2877,18 +3431,21 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { reschedule = TRUE; } else { /* - * It still has some protection window left and so + * Process enrolled in "always stop in deferral band after dirty" OR + * it still has some protection window left and so * we just re-arm the timer without modifying any * state on the process iff it still wants into that band. */ - if (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) { + if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) { + memorystatus_schedule_idle_demotion_locked(p, TRUE); + reschedule = TRUE; + } else if (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) { memorystatus_schedule_idle_demotion_locked(p, FALSE); reschedule = TRUE; } } } else { - memorystatus_schedule_idle_demotion_locked(p, TRUE); reschedule = TRUE; } @@ -2901,13 +3458,15 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { boolean_t ledger_update_needed = TRUE; boolean_t use_active; boolean_t is_fatal; - /* - * We are in this path because this process transitioned between + /* + * We are in this path because this process transitioned between * dirty <--> clean state. Update the cached memory limits. */ if (proc_jetsam_state_is_active_locked(p) == TRUE) { /* + * process is pinned in elevated band + * or * process is dirty */ CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); @@ -2920,7 +3479,7 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { * out and is entering the IDLE band. * See memorystatus_update_priority_locked() for that. */ - + if (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) { ledger_update_needed = FALSE; } else { @@ -2952,13 +3511,12 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { proc_rele_locked(p); MEMORYSTATUS_DEBUG(3, "memorystatus_dirty_set: new limit on pid %d (%dMB %s) priority(%d) dirty?=0x%x %s\n", - p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), - (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, - (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); + p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), + (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, + (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); } - } - + /* If the deferral state changed, reschedule the demotion timer */ if (reschedule) { memorystatus_reschedule_idle_demotion_locked(); @@ -2973,7 +3531,7 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) { proc_rele_locked(p); } } - + exit: proc_list_unlock(); @@ -2981,12 +3539,12 @@ exit: } int -memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) { - +memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) +{ int ret = 0; MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_clear(): %d 0x%x 0x%x\n", p->p_pid, pcontrol, p->p_memstat_dirty); - + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_CLEAR), p->p_pid, pcontrol, 0, 0, 0); proc_list_lock(); @@ -3006,11 +3564,11 @@ memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) { if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) { /* Dirty tracking not enabled */ - ret = EINVAL; + ret = EINVAL; goto exit; - } + } - if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER)) == 0) { + if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) == 0) { ret = EINVAL; goto exit; } @@ -3020,16 +3578,18 @@ memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) { } /* This can be set and cleared exactly once. */ - if (pcontrol & PROC_DIRTY_DEFER) { - - if (p->p_memstat_dirty & P_DIRTY_DEFER) { - - p->p_memstat_dirty &= ~P_DIRTY_DEFER; + if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) { + if (p->p_memstat_dirty & P_DIRTY_DEFER) { + p->p_memstat_dirty &= ~(P_DIRTY_DEFER); + } - memorystatus_invalidate_idle_demotion_locked(p, TRUE); - memorystatus_update_idle_priority_locked(p); - memorystatus_reschedule_idle_demotion_locked(); + if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) { + p->p_memstat_dirty &= ~(P_DIRTY_DEFER_ALWAYS); } + + memorystatus_invalidate_idle_demotion_locked(p, TRUE); + memorystatus_update_idle_priority_locked(p); + memorystatus_reschedule_idle_demotion_locked(); } ret = 0; @@ -3040,11 +3600,12 @@ exit: } int -memorystatus_dirty_get(proc_t p) { +memorystatus_dirty_get(proc_t p) +{ int ret = 0; - + proc_list_lock(); - + if (p->p_memstat_dirty & P_DIRTY_TRACK) { ret |= PROC_DIRTY_TRACKED; if (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) { @@ -3057,21 +3618,22 @@ memorystatus_dirty_get(proc_t p) { ret |= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS; } } - + proc_list_unlock(); - + return ret; } int -memorystatus_on_terminate(proc_t p) { +memorystatus_on_terminate(proc_t p) +{ int sig; - + proc_list_lock(); - + p->p_memstat_dirty |= P_DIRTY_TERMINATED; - - if ((p->p_memstat_dirty & (P_DIRTY_TRACK|P_DIRTY_IS_DIRTY)) == P_DIRTY_TRACK) { + + if ((p->p_memstat_dirty & (P_DIRTY_TRACK | P_DIRTY_IS_DIRTY)) == P_DIRTY_TRACK) { /* Clean; mark as terminated and issue SIGKILL */ sig = SIGKILL; } else { @@ -3080,7 +3642,7 @@ memorystatus_on_terminate(proc_t p) { } proc_list_unlock(); - + return sig; } @@ -3089,12 +3651,10 @@ memorystatus_on_suspend(proc_t p) { #if CONFIG_FREEZE uint32_t pages; - memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL); + memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); #endif proc_list_lock(); #if CONFIG_FREEZE - p->p_memstat_suspendedfootprint = pages; - memorystatus_suspended_footprint_total += pages; memorystatus_suspended_count++; #endif p->p_memstat_state |= P_MEMSTAT_SUSPENDED; @@ -3114,20 +3674,39 @@ memorystatus_on_resume(proc_t p) #if CONFIG_FREEZE frozen = (p->p_memstat_state & P_MEMSTAT_FROZEN); if (frozen) { - memorystatus_frozen_count--; - p->p_memstat_state |= P_MEMSTAT_PRIOR_THAW; + /* + * Now that we don't _thaw_ a process completely, + * resuming it (and having some on-demand swapins) + * shouldn't preclude it from being counted as frozen. + * + * memorystatus_frozen_count--; + * + * We preserve the P_MEMSTAT_FROZEN state since the process + * could have state on disk AND so will deserve some protection + * in the jetsam bands. + */ + if ((p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) == 0) { + p->p_memstat_state |= P_MEMSTAT_REFREEZE_ELIGIBLE; + memorystatus_refreeze_eligible_count++; + } + p->p_memstat_thaw_count++; + + memorystatus_thaw_count++; } - memorystatus_suspended_footprint_total -= p->p_memstat_suspendedfootprint; memorystatus_suspended_count--; - + pid = p->p_pid; #endif - p->p_memstat_state &= ~(P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN); + /* + * P_MEMSTAT_FROZEN will remain unchanged. This used to be: + * p->p_memstat_state &= ~(P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN); + */ + p->p_memstat_state &= ~P_MEMSTAT_SUSPENDED; proc_list_unlock(); - + #if CONFIG_FREEZE if (frozen) { memorystatus_freeze_entry_t data = { pid, FALSE, 0 }; @@ -3143,16 +3722,17 @@ memorystatus_on_inactivity(proc_t p) #if CONFIG_FREEZE /* Wake the freeze thread */ thread_wakeup((event_t)&memorystatus_freeze_wakeup); -#endif +#endif } /* * The proc_list_lock is held by the caller. -*/ + */ static uint32_t -memorystatus_build_state(proc_t p) { +memorystatus_build_state(proc_t p) +{ uint32_t snapshot_state = 0; - + /* General */ if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { snapshot_state |= kMemorystatusSuspended; @@ -3160,10 +3740,10 @@ memorystatus_build_state(proc_t p) { if (p->p_memstat_state & P_MEMSTAT_FROZEN) { snapshot_state |= kMemorystatusFrozen; } - if (p->p_memstat_state & P_MEMSTAT_PRIOR_THAW) { - snapshot_state |= kMemorystatusWasThawed; + if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { + snapshot_state |= kMemorystatusWasThawed; } - + /* Tracking */ if (p->p_memstat_dirty & P_DIRTY_TRACK) { snapshot_state |= kMemorystatusTracked; @@ -3178,8 +3758,6 @@ memorystatus_build_state(proc_t p) { return snapshot_state; } -#if !CONFIG_JETSAM - static boolean_t kill_idle_exit_proc(void) { @@ -3191,36 +3769,36 @@ kill_idle_exit_proc(void) /* Pick next idle exit victim. */ current_time = mach_absolute_time(); - + jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_IDLE_EXIT); if (jetsam_reason == OS_REASON_NULL) { printf("kill_idle_exit_proc: failed to allocate jetsam reason\n"); } proc_list_lock(); - + p = memorystatus_get_first_proc_locked(&i, FALSE); while (p) { /* No need to look beyond the idle band */ if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) { break; } - - if ((p->p_memstat_dirty & (P_DIRTY_ALLOW_IDLE_EXIT|P_DIRTY_IS_DIRTY|P_DIRTY_TERMINATED)) == (P_DIRTY_ALLOW_IDLE_EXIT)) { + + if ((p->p_memstat_dirty & (P_DIRTY_ALLOW_IDLE_EXIT | P_DIRTY_IS_DIRTY | P_DIRTY_TERMINATED)) == (P_DIRTY_ALLOW_IDLE_EXIT)) { if (current_time >= p->p_memstat_idledeadline) { p->p_memstat_dirty |= P_DIRTY_TERMINATED; victim_p = proc_ref_locked(p); break; } } - + p = memorystatus_get_next_proc_locked(&i, p, FALSE); } - + proc_list_unlock(); - + if (victim_p) { - printf("memorystatus_thread: idle exiting pid %d [%s]\n", victim_p->p_pid, (*victim_p->p_name ? victim_p->p_name : "(unknown)")); + printf("memorystatus: killing_idle_process pid %d [%s]\n", victim_p->p_pid, (*victim_p->p_name ? victim_p->p_name : "unknown")); killed = memorystatus_do_kill(victim_p, kMemorystatusKilledIdleExit, jetsam_reason); proc_rele(victim_p); } else { @@ -3229,13 +3807,38 @@ kill_idle_exit_proc(void) return killed; } -#endif + +static void +memorystatus_thread_wake(void) +{ + int thr_id = 0; + int active_thr = atomic_load(&active_jetsam_threads); + + /* Wakeup all the jetsam threads */ + for (thr_id = 0; thr_id < active_thr; thr_id++) { + thread_wakeup((event_t)&jetsam_threads[thr_id].memorystatus_wakeup); + } +} #if CONFIG_JETSAM + static void -memorystatus_thread_wake(void) { - thread_wakeup((event_t)&memorystatus_wakeup); +memorystatus_thread_pool_max() +{ + /* Increase the jetsam thread pool to max_jetsam_threads */ + int max_threads = max_jetsam_threads; + printf("Expanding memorystatus pool to %d!\n", max_threads); + atomic_store(&active_jetsam_threads, max_threads); } + +static void +memorystatus_thread_pool_default() +{ + /* Restore the jetsam thread pool to a single thread */ + printf("Reverting memorystatus pool back to 1\n"); + atomic_store(&active_jetsam_threads, 1); +} + #endif /* CONFIG_JETSAM */ extern void vm_pressure_response(void); @@ -3243,289 +3846,584 @@ extern void vm_pressure_response(void); static int memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation) { + struct jetsam_thread_state *jetsam_thread = jetsam_current_thread(); + if (interval_ms) { - assert_wait_timeout(&memorystatus_wakeup, THREAD_UNINT, interval_ms, 1000 * NSEC_PER_USEC); + assert_wait_timeout(&jetsam_thread->memorystatus_wakeup, THREAD_UNINT, interval_ms, NSEC_PER_MSEC); } else { - assert_wait(&memorystatus_wakeup, THREAD_UNINT); + assert_wait(&jetsam_thread->memorystatus_wakeup, THREAD_UNINT); } - - return thread_block(continuation); + + return thread_block(continuation); } -static void -memorystatus_thread(void *param __unused, wait_result_t wr __unused) +static boolean_t +memorystatus_avail_pages_below_pressure(void) { - static boolean_t is_vm_privileged = FALSE; +#if CONFIG_EMBEDDED +/* + * Instead of CONFIG_EMBEDDED for these *avail_pages* routines, we should + * key off of the system having dynamic swap support. With full swap support, + * the system shouldn't really need to worry about various page thresholds. + */ + return memorystatus_available_pages <= memorystatus_available_pages_pressure; +#else /* CONFIG_EMBEDDED */ + return FALSE; +#endif /* CONFIG_EMBEDDED */ +} -#if CONFIG_JETSAM - boolean_t post_snapshot = FALSE; - uint32_t errors = 0; - uint32_t hwm_kill = 0; - boolean_t sort_flag = TRUE; - boolean_t corpse_list_purged = FALSE; +static boolean_t +memorystatus_avail_pages_below_critical(void) +{ +#if CONFIG_EMBEDDED + return memorystatus_available_pages <= memorystatus_available_pages_critical; +#else /* CONFIG_EMBEDDED */ + return FALSE; +#endif /* CONFIG_EMBEDDED */ +} + +static boolean_t +memorystatus_post_snapshot(int32_t priority, uint32_t cause) +{ +#if CONFIG_EMBEDDED +#pragma unused(cause) + /* + * Don't generate logs for steady-state idle-exit kills, + * unless it is overridden for debug or by the device + * tree. + */ + + return (priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot; + +#else /* CONFIG_EMBEDDED */ + /* + * Don't generate logs for steady-state idle-exit kills, + * unless + * - it is overridden for debug or by the device + * tree. + * OR + * - the kill causes are important i.e. not kMemorystatusKilledIdleExit + */ + + boolean_t snapshot_eligible_kill_cause = (is_reason_thrashing(cause) || is_reason_zone_map_exhaustion(cause)); + return (priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot || snapshot_eligible_kill_cause; +#endif /* CONFIG_EMBEDDED */ +} + +static boolean_t +memorystatus_action_needed(void) +{ +#if CONFIG_EMBEDDED + return is_reason_thrashing(kill_under_pressure_cause) || + is_reason_zone_map_exhaustion(kill_under_pressure_cause) || + memorystatus_available_pages <= memorystatus_available_pages_pressure; +#else /* CONFIG_EMBEDDED */ + return is_reason_thrashing(kill_under_pressure_cause) || + is_reason_zone_map_exhaustion(kill_under_pressure_cause); +#endif /* CONFIG_EMBEDDED */ +} + +#if CONFIG_FREEZE +extern void vm_swap_consider_defragmenting(int); - /* Jetsam Loop Detection - locals */ +/* + * This routine will _jetsam_ all frozen processes + * and reclaim the swap space immediately. + * + * So freeze has to be DISABLED when we call this routine. + */ + +void +memorystatus_disable_freeze(void) +{ memstat_bucket_t *bucket; - int jld_bucket_count = 0; - struct timeval jld_now_tstamp = {0,0}; - uint64_t jld_now_msecs = 0; - int elevated_bucket_count = 0; - - /* Jetsam Loop Detection - statics */ - static uint64_t jld_timestamp_msecs = 0; - static int jld_idle_kill_candidates = 0; /* Number of available processes in band 0,1 at start */ - static int jld_idle_kills = 0; /* Number of procs killed during eval period */ - static int jld_eval_aggressive_count = 0; /* Bumps the max priority in aggressive loop */ - static int32_t jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; -#endif + int bucket_count = 0, retries = 0; + boolean_t retval = FALSE, killed = FALSE; + uint32_t errors = 0, errors_over_prev_iteration = 0; + os_reason_t jetsam_reason = 0; + unsigned int band = 0; + proc_t p = PROC_NULL, next_p = PROC_NULL; - if (is_vm_privileged == FALSE) { - /* - * It's the first time the thread has run, so just mark the thread as privileged and block. - * This avoids a spurious pass with unset variables, as set out in . - */ - thread_wire(host_priv_self(), current_thread(), TRUE); - is_vm_privileged = TRUE; - - if (vm_restricted_to_single_processor == TRUE) - thread_vm_bind_group_add(); + assert(memorystatus_freeze_enabled == FALSE); - memorystatus_thread_block(0, memorystatus_thread); + jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE); + if (jetsam_reason == OS_REASON_NULL) { + printf("memorystatus_disable_freeze: failed to allocate jetsam reason\n"); } - -#if CONFIG_JETSAM - - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START, - memorystatus_available_pages, memorystatus_jld_enabled, memorystatus_jld_eval_period_msecs, memorystatus_jld_eval_aggressive_count,0); /* - * Jetsam aware version. + * Let's relocate all frozen processes into band 8. Demoted frozen processes + * are sitting in band 0 currently and it's possible to have a frozen process + * in the FG band being actively used. We don't reset its frozen state when + * it is resumed because it has state on disk. * - * The VM pressure notification thread is working it's way through clients in parallel. + * We choose to do this relocation rather than implement a new 'kill frozen' + * process function for these reasons: + * - duplication of code: too many kill functions exist and we need to rework them better. + * - disk-space-shortage kills are rare + * - not having the 'real' jetsam band at time of the this frozen kill won't preclude us + * from answering any imp. questions re. jetsam policy/effectiveness. * - * So, while the pressure notification thread is targeting processes in order of - * increasing jetsam priority, we can hopefully reduce / stop it's work by killing - * any processes that have exceeded their highwater mark. - * - * If we run out of HWM processes and our available pages drops below the critical threshold, then, - * we target the least recently used process in order of increasing jetsam priority (exception: the FG band). + * This is essentially what memorystatus_update_inactive_jetsam_priority_band() does while + * avoiding the application of memory limits. */ - while (is_thrashing(kill_under_pressure_cause) || - memorystatus_available_pages <= memorystatus_available_pages_pressure) { - boolean_t killed; - int32_t priority; - uint32_t cause; - uint64_t jetsam_reason_code = JETSAM_REASON_INVALID; - os_reason_t jetsam_reason = OS_REASON_NULL; - cause = kill_under_pressure_cause; - switch (cause) { - case kMemorystatusKilledFCThrashing: - jetsam_reason_code = JETSAM_REASON_MEMORY_FCTHRASHING; - break; - case kMemorystatusKilledVMThrashing: - jetsam_reason_code = JETSAM_REASON_MEMORY_VMTHRASHING; - break; - case kMemorystatusKilledVMPageShortage: - /* falls through */ - default: - jetsam_reason_code = JETSAM_REASON_MEMORY_VMPAGESHORTAGE; - cause = kMemorystatusKilledVMPageShortage; - break; +again: + proc_list_lock(); + + band = JETSAM_PRIORITY_IDLE; + p = PROC_NULL; + next_p = PROC_NULL; + + next_p = memorystatus_get_first_proc_locked(&band, TRUE); + while (next_p) { + p = next_p; + next_p = memorystatus_get_next_proc_locked(&band, p, TRUE); + + if (p->p_memstat_effectivepriority > JETSAM_PRIORITY_FOREGROUND) { + break; } - /* Highwater */ - killed = memorystatus_kill_hiwat_proc(&errors); - if (killed) { - hwm_kill++; - post_snapshot = TRUE; - goto done; - } else { - memorystatus_hwm_candidates = FALSE; + if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == FALSE) { + continue; + } + + if (p->p_memstat_state & P_MEMSTAT_ERROR) { + p->p_memstat_state &= ~P_MEMSTAT_ERROR; + } + + if (p->p_memstat_effectivepriority == memorystatus_freeze_jetsam_band) { + continue; + } + + /* + * We explicitly add this flag here so the process looks like a normal + * frozen process i.e. P_MEMSTAT_FROZEN and P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND. + * We don't bother with assigning the 'active' memory + * limits at this point because we are going to be killing it soon below. + */ + p->p_memstat_state |= P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; + memorystatus_invalidate_idle_demotion_locked(p, TRUE); + + memorystatus_update_priority_locked(p, memorystatus_freeze_jetsam_band, FALSE, TRUE); + } + + bucket = &memstat_bucket[memorystatus_freeze_jetsam_band]; + bucket_count = bucket->count; + proc_list_unlock(); + + /* + * Bucket count is already stale at this point. But, we don't expect + * freezing to continue since we have already disabled the freeze functionality. + * However, an existing freeze might be in progress. So we might miss that process + * in the first go-around. We hope to catch it in the next. + */ + + errors_over_prev_iteration = 0; + while (bucket_count) { + bucket_count--; + + /* + * memorystatus_kill_elevated_process() drops a reference, + * so take another one so we can continue to use this exit reason + * even after it returns. + */ + + os_reason_ref(jetsam_reason); + retval = memorystatus_kill_elevated_process( + kMemorystatusKilledDiskSpaceShortage, + jetsam_reason, + memorystatus_freeze_jetsam_band, + 0, /* the iteration of aggressive jetsam..ignored here */ + &errors); + + if (errors > 0) { + printf("memorystatus_disable_freeze: memorystatus_kill_elevated_process returned %d error(s)\n", errors); + errors_over_prev_iteration += errors; + errors = 0; } - /* No highwater processes to kill. Continue or stop for now? */ - if (!is_thrashing(kill_under_pressure_cause) && - (memorystatus_available_pages > memorystatus_available_pages_critical)) { + if (retval == 0) { /* - * We are _not_ out of pressure but we are above the critical threshold and there's: - * - no compressor thrashing - * - no more HWM processes left. - * For now, don't kill any other processes. + * No frozen processes left to kill. */ - - if (hwm_kill == 0) { - memorystatus_thread_wasted_wakeup++; + break; + } + + killed = TRUE; + } + + proc_list_lock(); + + if (memorystatus_frozen_count) { + /* + * A frozen process snuck in and so + * go back around to kill it. That + * process may have been resumed and + * put into the FG band too. So we + * have to do the relocation again. + */ + assert(memorystatus_freeze_enabled == FALSE); + + retries++; + if (retries < 3) { + proc_list_unlock(); + goto again; + } +#if DEVELOPMENT || DEBUG + panic("memorystatus_disable_freeze: Failed to kill all frozen processes, memorystatus_frozen_count = %d, errors = %d", + memorystatus_frozen_count, errors_over_prev_iteration); +#endif /* DEVELOPMENT || DEBUG */ + } + proc_list_unlock(); + + os_reason_free(jetsam_reason); + + if (killed) { + vm_swap_consider_defragmenting(VM_SWAP_FLAGS_FORCE_DEFRAG | VM_SWAP_FLAGS_FORCE_RECLAIM); + + proc_list_lock(); + size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + + sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count); + uint64_t timestamp_now = mach_absolute_time(); + memorystatus_jetsam_snapshot->notification_time = timestamp_now; + memorystatus_jetsam_snapshot->js_gencount++; + if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || + timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { + proc_list_unlock(); + int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); + if (!ret) { + proc_list_lock(); + memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; + proc_list_unlock(); } + } else { + proc_list_unlock(); + } + } - break; + return; +} +#endif /* CONFIG_FREEZE */ + +static boolean_t +memorystatus_act_on_hiwat_processes(uint32_t *errors, uint32_t *hwm_kill, boolean_t *post_snapshot, __unused boolean_t *is_critical) +{ + boolean_t purged = FALSE; + boolean_t killed = memorystatus_kill_hiwat_proc(errors, &purged); + + if (killed) { + *hwm_kill = *hwm_kill + 1; + *post_snapshot = TRUE; + return TRUE; + } else { + if (purged == FALSE) { + /* couldn't purge and couldn't kill */ + memorystatus_hwm_candidates = FALSE; } + } - jetsam_reason = os_reason_create(OS_REASON_JETSAM, jetsam_reason_code); - if (jetsam_reason == OS_REASON_NULL) { - printf("memorystatus_thread: failed to allocate jetsam reason\n"); +#if CONFIG_JETSAM + /* No highwater processes to kill. Continue or stop for now? */ + if (!is_reason_thrashing(kill_under_pressure_cause) && + !is_reason_zone_map_exhaustion(kill_under_pressure_cause) && + (memorystatus_available_pages > memorystatus_available_pages_critical)) { + /* + * We are _not_ out of pressure but we are above the critical threshold and there's: + * - no compressor thrashing + * - enough zone memory + * - no more HWM processes left. + * For now, don't kill any other processes. + */ + + if (*hwm_kill == 0) { + memorystatus_thread_wasted_wakeup++; } - if (memorystatus_jld_enabled == TRUE) { + *is_critical = FALSE; - /* - * Jetsam Loop Detection: attempt to detect - * rapid daemon relaunches in the lower bands. - */ - - microuptime(&jld_now_tstamp); + return TRUE; + } +#endif /* CONFIG_JETSAM */ - /* - * Ignore usecs in this calculation. - * msecs granularity is close enough. - */ - jld_now_msecs = (jld_now_tstamp.tv_sec * 1000); + return FALSE; +} - proc_list_lock(); - switch (jetsam_aging_policy) { - case kJetsamAgingPolicyLegacy: - bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; - jld_bucket_count = bucket->count; - bucket = &memstat_bucket[JETSAM_PRIORITY_AGING_BAND1]; - jld_bucket_count += bucket->count; - break; - case kJetsamAgingPolicySysProcsReclaimedFirst: - case kJetsamAgingPolicyAppsReclaimedFirst: - bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; - jld_bucket_count = bucket->count; - bucket = &memstat_bucket[system_procs_aging_band]; - jld_bucket_count += bucket->count; - bucket = &memstat_bucket[applications_aging_band]; - jld_bucket_count += bucket->count; - break; - case kJetsamAgingPolicyNone: - default: - bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; - jld_bucket_count = bucket->count; - break; - } +static boolean_t +memorystatus_act_aggressive(uint32_t cause, os_reason_t jetsam_reason, int *jld_idle_kills, boolean_t *corpse_list_purged, boolean_t *post_snapshot) +{ + if (memorystatus_jld_enabled == TRUE) { + boolean_t killed; + uint32_t errors = 0; + + /* Jetsam Loop Detection - locals */ + memstat_bucket_t *bucket; + int jld_bucket_count = 0; + struct timeval jld_now_tstamp = {0, 0}; + uint64_t jld_now_msecs = 0; + int elevated_bucket_count = 0; + + /* Jetsam Loop Detection - statics */ + static uint64_t jld_timestamp_msecs = 0; + static int jld_idle_kill_candidates = 0; /* Number of available processes in band 0,1 at start */ + static int jld_eval_aggressive_count = 0; /* Bumps the max priority in aggressive loop */ + static int32_t jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; + /* + * Jetsam Loop Detection: attempt to detect + * rapid daemon relaunches in the lower bands. + */ - bucket = &memstat_bucket[JETSAM_PRIORITY_ELEVATED_INACTIVE]; - elevated_bucket_count = bucket->count; + microuptime(&jld_now_tstamp); - proc_list_unlock(); + /* + * Ignore usecs in this calculation. + * msecs granularity is close enough. + */ + jld_now_msecs = (jld_now_tstamp.tv_sec * 1000); + + proc_list_lock(); + switch (jetsam_aging_policy) { + case kJetsamAgingPolicyLegacy: + bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; + jld_bucket_count = bucket->count; + bucket = &memstat_bucket[JETSAM_PRIORITY_AGING_BAND1]; + jld_bucket_count += bucket->count; + break; + case kJetsamAgingPolicySysProcsReclaimedFirst: + case kJetsamAgingPolicyAppsReclaimedFirst: + bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; + jld_bucket_count = bucket->count; + bucket = &memstat_bucket[system_procs_aging_band]; + jld_bucket_count += bucket->count; + bucket = &memstat_bucket[applications_aging_band]; + jld_bucket_count += bucket->count; + break; + case kJetsamAgingPolicyNone: + default: + bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; + jld_bucket_count = bucket->count; + break; + } + + bucket = &memstat_bucket[JETSAM_PRIORITY_ELEVATED_INACTIVE]; + elevated_bucket_count = bucket->count; + + proc_list_unlock(); + /* + * memorystatus_jld_eval_period_msecs is a tunable + * memorystatus_jld_eval_aggressive_count is a tunable + * memorystatus_jld_eval_aggressive_priority_band_max is a tunable + */ + if ((jld_bucket_count == 0) || + (jld_now_msecs > (jld_timestamp_msecs + memorystatus_jld_eval_period_msecs))) { /* - * memorystatus_jld_eval_period_msecs is a tunable - * memorystatus_jld_eval_aggressive_count is a tunable - * memorystatus_jld_eval_aggressive_priority_band_max is a tunable + * Refresh evaluation parameters */ - if ( (jld_bucket_count == 0) || - (jld_now_msecs > (jld_timestamp_msecs + memorystatus_jld_eval_period_msecs))) { - - /* - * Refresh evaluation parameters - */ - jld_timestamp_msecs = jld_now_msecs; - jld_idle_kill_candidates = jld_bucket_count; - jld_idle_kills = 0; - jld_eval_aggressive_count = 0; - jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; - } + jld_timestamp_msecs = jld_now_msecs; + jld_idle_kill_candidates = jld_bucket_count; + *jld_idle_kills = 0; + jld_eval_aggressive_count = 0; + jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; + } - if (jld_idle_kills > jld_idle_kill_candidates) { - jld_eval_aggressive_count++; + if (*jld_idle_kills > jld_idle_kill_candidates) { + jld_eval_aggressive_count++; #if DEVELOPMENT || DEBUG - printf("memorystatus: aggressive%d: beginning of window: %lld ms, : timestamp now: %lld ms\n", - jld_eval_aggressive_count, - jld_timestamp_msecs, - jld_now_msecs); - printf("memorystatus: aggressive%d: idle candidates: %d, idle kills: %d\n", - jld_eval_aggressive_count, - jld_idle_kill_candidates, - jld_idle_kills); + printf("memorystatus: aggressive%d: beginning of window: %lld ms, : timestamp now: %lld ms\n", + jld_eval_aggressive_count, + jld_timestamp_msecs, + jld_now_msecs); + printf("memorystatus: aggressive%d: idle candidates: %d, idle kills: %d\n", + jld_eval_aggressive_count, + jld_idle_kill_candidates, + *jld_idle_kills); #endif /* DEVELOPMENT || DEBUG */ - if ((jld_eval_aggressive_count == memorystatus_jld_eval_aggressive_count) && - (total_corpses_count > 0) && (corpse_list_purged == FALSE)) { + if ((jld_eval_aggressive_count == memorystatus_jld_eval_aggressive_count) && + (total_corpses_count() > 0) && (*corpse_list_purged == FALSE)) { + /* + * If we reach this aggressive cycle, corpses might be causing memory pressure. + * So, in an effort to avoid jetsams in the FG band, we will attempt to purge + * corpse memory prior to this final march through JETSAM_PRIORITY_UI_SUPPORT. + */ + task_purge_all_corpses(); + *corpse_list_purged = TRUE; + } else if (jld_eval_aggressive_count > memorystatus_jld_eval_aggressive_count) { + /* + * Bump up the jetsam priority limit (eg: the bucket index) + * Enforce bucket index sanity. + */ + if ((memorystatus_jld_eval_aggressive_priority_band_max < 0) || + (memorystatus_jld_eval_aggressive_priority_band_max >= MEMSTAT_BUCKET_COUNT)) { /* - * If we reach this aggressive cycle, corpses might be causing memory pressure. - * So, in an effort to avoid jetsams in the FG band, we will attempt to purge - * corpse memory prior to this final march through JETSAM_PRIORITY_UI_SUPPORT. + * Do nothing. Stick with the default level. */ - task_purge_all_corpses(); - corpse_list_purged = TRUE; + } else { + jld_priority_band_max = memorystatus_jld_eval_aggressive_priority_band_max; } - else if (jld_eval_aggressive_count > memorystatus_jld_eval_aggressive_count) { - /* - * Bump up the jetsam priority limit (eg: the bucket index) - * Enforce bucket index sanity. - */ - if ((memorystatus_jld_eval_aggressive_priority_band_max < 0) || - (memorystatus_jld_eval_aggressive_priority_band_max >= MEMSTAT_BUCKET_COUNT)) { + } + + /* Visit elevated processes first */ + while (elevated_bucket_count) { + elevated_bucket_count--; + + /* + * memorystatus_kill_elevated_process() drops a reference, + * so take another one so we can continue to use this exit reason + * even after it returns. + */ + + os_reason_ref(jetsam_reason); + killed = memorystatus_kill_elevated_process( + cause, + jetsam_reason, + JETSAM_PRIORITY_ELEVATED_INACTIVE, + jld_eval_aggressive_count, + &errors); + + if (killed) { + *post_snapshot = TRUE; + if (memorystatus_avail_pages_below_pressure()) { /* - * Do nothing. Stick with the default level. + * Still under pressure. + * Find another pinned processes. */ + continue; } else { - jld_priority_band_max = memorystatus_jld_eval_aggressive_priority_band_max; + return TRUE; } + } else { + /* + * No pinned processes left to kill. + * Abandon elevated band. + */ + break; } + } - /* Visit elevated processes first */ - while (elevated_bucket_count) { + /* + * memorystatus_kill_top_process_aggressive() allocates its own + * jetsam_reason so the kMemorystatusKilledProcThrashing cause + * is consistent throughout the aggressive march. + */ + killed = memorystatus_kill_top_process_aggressive( + kMemorystatusKilledProcThrashing, + jld_eval_aggressive_count, + jld_priority_band_max, + &errors); - elevated_bucket_count--; + if (killed) { + /* Always generate logs after aggressive kill */ + *post_snapshot = TRUE; + *jld_idle_kills = 0; + return TRUE; + } + } - /* - * memorystatus_kill_elevated_process() drops a reference, - * so take another one so we can continue to use this exit reason - * even after it returns. - */ + return FALSE; + } - os_reason_ref(jetsam_reason); - killed = memorystatus_kill_elevated_process( - kMemorystatusKilledVMThrashing, - jetsam_reason, - jld_eval_aggressive_count, - &errors); + return FALSE; +} - if (killed) { - post_snapshot = TRUE; - if (memorystatus_available_pages <= memorystatus_available_pages_pressure) { - /* - * Still under pressure. - * Find another pinned processes. - */ - continue; - } else { - goto done; - } - } else { - /* - * No pinned processes left to kill. - * Abandon elevated band. - */ - break; - } - } +static void +memorystatus_thread(void *param __unused, wait_result_t wr __unused) +{ + boolean_t post_snapshot = FALSE; + uint32_t errors = 0; + uint32_t hwm_kill = 0; + boolean_t sort_flag = TRUE; + boolean_t corpse_list_purged = FALSE; + int jld_idle_kills = 0; + struct jetsam_thread_state *jetsam_thread = jetsam_current_thread(); + + if (jetsam_thread->inited == FALSE) { + /* + * It's the first time the thread has run, so just mark the thread as privileged and block. + * This avoids a spurious pass with unset variables, as set out in . + */ + + char name[32]; + thread_wire(host_priv_self(), current_thread(), TRUE); + snprintf(name, 32, "VM_memorystatus_%d", jetsam_thread->index + 1); + + if (jetsam_thread->index == 0) { + if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) { + thread_vm_bind_group_add(); + } + } + thread_set_thread_name(current_thread(), name); + jetsam_thread->inited = TRUE; + memorystatus_thread_block(0, memorystatus_thread); + } + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START, + memorystatus_available_pages, memorystatus_jld_enabled, memorystatus_jld_eval_period_msecs, memorystatus_jld_eval_aggressive_count, 0); + + /* + * Jetsam aware version. + * + * The VM pressure notification thread is working it's way through clients in parallel. + * + * So, while the pressure notification thread is targeting processes in order of + * increasing jetsam priority, we can hopefully reduce / stop it's work by killing + * any processes that have exceeded their highwater mark. + * + * If we run out of HWM processes and our available pages drops below the critical threshold, then, + * we target the least recently used process in order of increasing jetsam priority (exception: the FG band). + */ + while (memorystatus_action_needed()) { + boolean_t killed; + int32_t priority; + uint32_t cause; + uint64_t jetsam_reason_code = JETSAM_REASON_INVALID; + os_reason_t jetsam_reason = OS_REASON_NULL; + + cause = kill_under_pressure_cause; + switch (cause) { + case kMemorystatusKilledFCThrashing: + jetsam_reason_code = JETSAM_REASON_MEMORY_FCTHRASHING; + break; + case kMemorystatusKilledVMCompressorThrashing: + jetsam_reason_code = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING; + break; + case kMemorystatusKilledVMCompressorSpaceShortage: + jetsam_reason_code = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE; + break; + case kMemorystatusKilledZoneMapExhaustion: + jetsam_reason_code = JETSAM_REASON_ZONE_MAP_EXHAUSTION; + break; + case kMemorystatusKilledVMPageShortage: + /* falls through */ + default: + jetsam_reason_code = JETSAM_REASON_MEMORY_VMPAGESHORTAGE; + cause = kMemorystatusKilledVMPageShortage; + break; + } + + /* Highwater */ + boolean_t is_critical = TRUE; + if (memorystatus_act_on_hiwat_processes(&errors, &hwm_kill, &post_snapshot, &is_critical)) { + if (is_critical == FALSE) { /* - * memorystatus_kill_top_process_aggressive() drops a reference, - * so take another one so we can continue to use this exit reason - * even after it returns + * For now, don't kill any other processes. */ - os_reason_ref(jetsam_reason); - killed = memorystatus_kill_top_process_aggressive( - TRUE, - kMemorystatusKilledVMThrashing, - jetsam_reason, - jld_eval_aggressive_count, - jld_priority_band_max, - &errors); - - if (killed) { - /* Always generate logs after aggressive kill */ - post_snapshot = TRUE; - jld_idle_kills = 0; - goto done; - } - } + break; + } else { + goto done; + } + } + + jetsam_reason = os_reason_create(OS_REASON_JETSAM, jetsam_reason_code); + if (jetsam_reason == OS_REASON_NULL) { + printf("memorystatus_thread: failed to allocate jetsam reason\n"); + } + + if (memorystatus_act_aggressive(cause, jetsam_reason, &jld_idle_kills, &corpse_list_purged, &post_snapshot)) { + goto done; } /* @@ -3540,13 +4438,8 @@ memorystatus_thread(void *param __unused, wait_result_t wr __unused) sort_flag = FALSE; if (killed) { - /* - * Don't generate logs for steady-state idle-exit kills, - * unless it is overridden for debug or by the device - * tree. - */ - if ((priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot) { - post_snapshot = TRUE; + if (memorystatus_post_snapshot(priority, cause) == TRUE) { + post_snapshot = TRUE; } /* Jetsam Loop Detection */ @@ -3561,7 +4454,7 @@ memorystatus_thread(void *param __unused, wait_result_t wr __unused) } } - if ((priority >= JETSAM_PRIORITY_UI_SUPPORT) && (total_corpses_count > 0) && (corpse_list_purged == FALSE)) { + if ((priority >= JETSAM_PRIORITY_UI_SUPPORT) && (total_corpses_count() > 0) && (corpse_list_purged == FALSE)) { /* * If we have jetsammed a process in or above JETSAM_PRIORITY_UI_SUPPORT * then we attempt to relieve pressure by purging corpse memory. @@ -3571,65 +4464,58 @@ memorystatus_thread(void *param __unused, wait_result_t wr __unused) } goto done; } - - if (memorystatus_available_pages <= memorystatus_available_pages_critical) { + + if (memorystatus_avail_pages_below_critical()) { /* * Still under pressure and unable to kill a process - purge corpse memory */ - if (total_corpses_count > 0) { + if (total_corpses_count() > 0) { task_purge_all_corpses(); corpse_list_purged = TRUE; } - if (memorystatus_available_pages <= memorystatus_available_pages_critical) { + if (memorystatus_avail_pages_below_critical()) { /* * Still under pressure and unable to kill a process - panic */ - panic("memorystatus_jetsam_thread: no victim! available pages:%d\n", memorystatus_available_pages); + panic("memorystatus_jetsam_thread: no victim! available pages:%llu\n", (uint64_t)memorystatus_available_pages); } } - -done: + +done: /* * We do not want to over-kill when thrashing has been detected. * To avoid that, we reset the flag here and notify the * compressor. */ - if (is_thrashing(kill_under_pressure_cause)) { + if (is_reason_thrashing(kill_under_pressure_cause)) { kill_under_pressure_cause = 0; +#if CONFIG_JETSAM vm_thrashing_jetsam_done(); +#endif /* CONFIG_JETSAM */ + } else if (is_reason_zone_map_exhaustion(kill_under_pressure_cause)) { + kill_under_pressure_cause = 0; } os_reason_free(jetsam_reason); } kill_under_pressure_cause = 0; - + if (errors) { memorystatus_clear_errors(); } -#if VM_PRESSURE_EVENTS - /* - * LD: We used to target the foreground process first and foremost here. - * Now, we target all processes, starting from the non-suspended, background - * processes first. We will target foreground too. - * - * memorystatus_update_vm_pressure(TRUE); - */ - //vm_pressure_response(); -#endif - if (post_snapshot) { proc_list_lock(); size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + - sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count); + sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count); uint64_t timestamp_now = mach_absolute_time(); memorystatus_jetsam_snapshot->notification_time = timestamp_now; memorystatus_jetsam_snapshot->js_gencount++; if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || - timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { + timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { proc_list_unlock(); int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); if (!ret) { @@ -3643,31 +4529,34 @@ done: } KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_END, - memorystatus_available_pages, 0, 0, 0, 0); - -#else /* CONFIG_JETSAM */ - - /* - * Jetsam not enabled - */ - -#endif /* CONFIG_JETSAM */ + memorystatus_available_pages, 0, 0, 0, 0); memorystatus_thread_block(0, memorystatus_thread); } -#if !CONFIG_JETSAM /* * Returns TRUE: - * when an idle-exitable proc was killed + * when an idle-exitable proc was killed * Returns FALSE: * when there are no more idle-exitable procs found - * when the attempt to kill an idle-exitable proc failed + * when the attempt to kill an idle-exitable proc failed */ -boolean_t memorystatus_idle_exit_from_VM(void) { - return(kill_idle_exit_proc()); +boolean_t +memorystatus_idle_exit_from_VM(void) +{ + /* + * This routine should no longer be needed since we are + * now using jetsam bands on all platforms and so will deal + * with IDLE processes within the memorystatus thread itself. + * + * But we still use it because we observed that macos systems + * started heavy compression/swapping with a bunch of + * idle-exitable processes alive and doing nothing. We decided + * to rather kill those processes than start swapping earlier. + */ + + return kill_idle_exit_proc(); } -#endif /* !CONFIG_JETSAM */ /* * Callback invoked when allowable physical memory footprint exceeded @@ -3689,9 +4578,9 @@ memorystatus_on_ledger_footprint_exceeded(boolean_t warning, boolean_t memlimit_ * This is a warning path which implies that the current process is close, but has * not yet exceeded its per-process memory limit. */ - if (memorystatus_warn_process(p->p_pid, memlimit_is_active, memlimit_is_fatal, FALSE /* not exceeded */) != TRUE) { + if (memorystatus_warn_process(p->p_pid, memlimit_is_active, memlimit_is_fatal, FALSE /* not exceeded */) != TRUE) { /* Print warning, since it's possible that task has not registered for pressure notifications */ - printf("task_exceeded_footprint: failed to warn the current task (%d exiting, or no handler registered?).\n", p->p_pid); + os_log(OS_LOG_DEFAULT, "memorystatus_on_ledger_footprint_exceeded: failed to warn the current task (%d exiting, or no handler registered?).\n", p->p_pid); } return; } @@ -3705,7 +4594,7 @@ memorystatus_on_ledger_footprint_exceeded(boolean_t warning, boolean_t memlimit_ jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_PERPROCESSLIMIT); if (jetsam_reason == NULL) { printf("task_exceeded footprint: failed to allocate jetsam reason\n"); - } else if (corpse_for_fatal_memkill != 0) { + } else if (corpse_for_fatal_memkill != 0 && proc_send_synchronous_EXC_RESOURCE(p) == FALSE) { /* Set OS_REASON_FLAG_GENERATE_CRASH_REPORT to generate corpse */ jetsam_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT; } @@ -3743,9 +4632,10 @@ memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_act * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit. */ - printf("process %d (%s) exceeded physical memory footprint, the %s%sMemoryLimit of %d MB\n", - p->p_pid, (*p->p_name ? p->p_name : "unknown"), (memlimit_is_active ? "Active" : "Inactive"), - (memlimit_is_fatal ? "Hard" : "Soft"), max_footprint_mb); + os_log_with_startup_serial(OS_LOG_DEFAULT, "EXC_RESOURCE -> %s[%d] exceeded mem limit: %s%s %d MB (%s)\n", + (*p->p_name ? p->p_name : "unknown"), p->p_pid, (memlimit_is_active ? "Active" : "Inactive"), + (memlimit_is_fatal ? "Hard" : "Soft"), max_footprint_mb, + (memlimit_is_fatal ? "fatal" : "non-fatal")); return; } @@ -3753,7 +4643,13 @@ memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_act /* * Description: - * Evaluates active vs. inactive process state. + * Evaluates process state to determine which limit + * should be applied (active vs. inactive limit). + * + * Processes that have the 'elevated inactive jetsam band' attribute + * are first evaluated based on their current priority band. + * presently elevated ==> active + * * Processes that opt into dirty tracking are evaluated * based on clean vs dirty state. * dirty ==> active @@ -3769,9 +4665,17 @@ memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_act */ static boolean_t -proc_jetsam_state_is_active_locked(proc_t p) { - - if (p->p_memstat_dirty & P_DIRTY_TRACK) { +proc_jetsam_state_is_active_locked(proc_t p) +{ + if ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) && + (p->p_memstat_effectivepriority == JETSAM_PRIORITY_ELEVATED_INACTIVE)) { + /* + * process has the 'elevated inactive jetsam band' attribute + * and process is present in the elevated band + * implies active state + */ + return TRUE; + } else if (p->p_memstat_dirty & P_DIRTY_TRACK) { /* * process has opted into dirty tracking * active state is based on dirty vs. clean @@ -3804,11 +4708,11 @@ proc_jetsam_state_is_active_locked(proc_t p) { } } -static boolean_t -memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) { +static boolean_t +memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) +{ boolean_t res; -#if CONFIG_JETSAM uint32_t errors = 0; if (victim_pid == -1) { @@ -3817,7 +4721,7 @@ memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jet } else { res = memorystatus_kill_specific_process(victim_pid, cause, jetsam_reason); } - + if (errors) { memorystatus_clear_errors(); } @@ -3825,12 +4729,12 @@ memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jet if (res == TRUE) { /* Fire off snapshot notification */ proc_list_lock(); - size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + - sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count; + size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + + sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count; uint64_t timestamp_now = mach_absolute_time(); memorystatus_jetsam_snapshot->notification_time = timestamp_now; if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || - timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { + timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { proc_list_unlock(); int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); if (!ret) { @@ -3842,26 +4746,22 @@ memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jet proc_list_unlock(); } } -#else /* !CONFIG_JETSAM */ - - res = memorystatus_kill_specific_process(victim_pid, cause, jetsam_reason); -#endif /* CONFIG_JETSAM */ - return res; } /* * Jetsam a specific process. */ -static boolean_t -memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) { +static boolean_t +memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) +{ boolean_t killed; proc_t p; uint64_t killtime = 0; - clock_sec_t tv_sec; - clock_usec_t tv_usec; - uint32_t tv_msec; + clock_sec_t tv_sec; + clock_usec_t tv_usec; + uint32_t tv_msec; /* TODO - add a victim queue and push this into the main jetsam thread */ @@ -3873,36 +4773,25 @@ memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t proc_list_lock(); -#if CONFIG_JETSAM if (memorystatus_jetsam_snapshot_count == 0) { - memorystatus_init_jetsam_snapshot_locked(NULL,0); + memorystatus_init_jetsam_snapshot_locked(NULL, 0); } killtime = mach_absolute_time(); - absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); - tv_msec = tv_usec / 1000; + absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); + tv_msec = tv_usec / 1000; memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); proc_list_unlock(); - printf("%lu.%02d memorystatus: specifically killing pid %d [%s] (%s %d) - memorystatus_available_pages: %d\n", - (unsigned long)tv_sec, tv_msec, victim_pid, (*p->p_name ? p->p_name : "(unknown)"), - jetsam_kill_cause_name[cause], p->p_memstat_effectivepriority, memorystatus_available_pages); -#else /* !CONFIG_JETSAM */ - proc_list_unlock(); + os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: killing_specific_process pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", + (unsigned long)tv_sec, tv_msec, victim_pid, (*p->p_name ? p->p_name : "unknown"), + memorystatus_kill_cause_name[cause], p->p_memstat_effectivepriority, (uint64_t)memorystatus_available_pages); - killtime = mach_absolute_time(); - absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); - tv_msec = tv_usec / 1000; - printf("%lu.%02d memorystatus: specifically killing pid %d [%s] (%s %d)\n", - (unsigned long)tv_sec, tv_msec, victim_pid, (*p->p_name ? p->p_name : "(unknown)"), - jetsam_kill_cause_name[cause], p->p_memstat_effectivepriority); -#endif /* CONFIG_JETSAM */ - killed = memorystatus_do_kill(p, cause, jetsam_reason); proc_rele(p); - + return killed; } @@ -3924,57 +4813,207 @@ proc_memstat_terminated(proc_t p, boolean_t set) } proc_list_unlock(); } -#else -#pragma unused(p, set) +#else +#pragma unused(p, set) + /* + * do nothing + */ +#endif /* DEVELOPMENT || DEBUG */ + return; +} + + +#if CONFIG_JETSAM +/* + * This is invoked when cpulimits have been exceeded while in fatal mode. + * The jetsam_flags do not apply as those are for memory related kills. + * We call this routine so that the offending process is killed with + * a non-zero exit status. + */ +void +jetsam_on_ledger_cpulimit_exceeded(void) +{ + int retval = 0; + int jetsam_flags = 0; /* make it obvious */ + proc_t p = current_proc(); + os_reason_t jetsam_reason = OS_REASON_NULL; + + printf("task_exceeded_cpulimit: killing pid %d [%s]\n", + p->p_pid, (*p->p_name ? p->p_name : "(unknown)")); + + jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_CPULIMIT); + if (jetsam_reason == OS_REASON_NULL) { + printf("task_exceeded_cpulimit: unable to allocate memory for jetsam reason\n"); + } + + retval = jetsam_do_kill(p, jetsam_flags, jetsam_reason); + + if (retval) { + printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n"); + } +} + +#endif /* CONFIG_JETSAM */ + +static void +memorystatus_get_task_memory_region_count(task_t task, uint64_t *count) +{ + assert(task); + assert(count); + + *count = get_task_memory_region_count(task); +} + + +#define MEMORYSTATUS_VM_MAP_FORK_ALLOWED 0x100000000 +#define MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED 0x200000000 + +#if DEVELOPMENT || DEBUG + +/* + * Sysctl only used to test memorystatus_allowed_vm_map_fork() path. + * set a new pidwatch value + * or + * get the current pidwatch value + * + * The pidwatch_val starts out with a PID to watch for in the map_fork path. + * Its value is: + * - OR'd with MEMORYSTATUS_VM_MAP_FORK_ALLOWED if we allow the map_fork. + * - OR'd with MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED if we disallow the map_fork. + * - set to -1ull if the map_fork() is aborted for other reasons. + */ + +uint64_t memorystatus_vm_map_fork_pidwatch_val = 0; + +static int sysctl_memorystatus_vm_map_fork_pidwatch SYSCTL_HANDLER_ARGS { +#pragma unused(oidp, arg1, arg2) + + uint64_t new_value = 0; + uint64_t old_value = 0; + int error = 0; + + /* + * The pid is held in the low 32 bits. + * The 'allowed' flags are in the upper 32 bits. + */ + old_value = memorystatus_vm_map_fork_pidwatch_val; + + error = sysctl_io_number(req, old_value, sizeof(old_value), &new_value, NULL); + + if (error || !req->newptr) { + /* + * No new value passed in. + */ + return error; + } + /* - * do nothing + * A new pid was passed in via req->newptr. + * Ignore any attempt to set the higher order bits. */ -#endif /* DEVELOPMENT || DEBUG */ - return; + memorystatus_vm_map_fork_pidwatch_val = new_value & 0xFFFFFFFF; + printf("memorystatus: pidwatch old_value = 0x%llx, new_value = 0x%llx \n", old_value, new_value); + + return error; } +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_map_fork_pidwatch, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, + 0, 0, sysctl_memorystatus_vm_map_fork_pidwatch, "Q", "get/set pid watched for in vm_map_fork"); + -#if CONFIG_JETSAM /* - * This is invoked when cpulimits have been exceeded while in fatal mode. - * The jetsam_flags do not apply as those are for memory related kills. - * We call this routine so that the offending process is killed with - * a non-zero exit status. + * Record if a watched process fails to qualify for a vm_map_fork(). */ void -jetsam_on_ledger_cpulimit_exceeded(void) +memorystatus_abort_vm_map_fork(task_t task) { - int retval = 0; - int jetsam_flags = 0; /* make it obvious */ - proc_t p = current_proc(); - os_reason_t jetsam_reason = OS_REASON_NULL; - - printf("task_exceeded_cpulimit: killing pid %d [%s]\n", - p->p_pid, (*p->p_name ? p->p_name : "(unknown)")); - - jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_CPULIMIT); - if (jetsam_reason == OS_REASON_NULL) { - printf("task_exceeded_cpulimit: unable to allocate memory for jetsam reason\n"); + if (memorystatus_vm_map_fork_pidwatch_val != 0) { + proc_t p = get_bsdtask_info(task); + if (p != NULL && memorystatus_vm_map_fork_pidwatch_val == (uint64_t)p->p_pid) { + memorystatus_vm_map_fork_pidwatch_val = -1ull; + } } +} - retval = jetsam_do_kill(p, jetsam_flags, jetsam_reason); - - if (retval) { - printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n"); +static void +set_vm_map_fork_pidwatch(task_t task, uint64_t x) +{ + if (memorystatus_vm_map_fork_pidwatch_val != 0) { + proc_t p = get_bsdtask_info(task); + if (p && (memorystatus_vm_map_fork_pidwatch_val == (uint64_t)p->p_pid)) { + memorystatus_vm_map_fork_pidwatch_val |= x; + } } } +#else /* DEVELOPMENT || DEBUG */ + + static void -memorystatus_get_task_memory_region_count(task_t task, uint64_t *count) +set_vm_map_fork_pidwatch(task_t task, uint64_t x) { - assert(task); - assert(count); +#pragma unused(task) +#pragma unused(x) +} - *count = get_task_memory_region_count(task); +#endif /* DEVELOPMENT || DEBUG */ + +/* + * Called during EXC_RESOURCE handling when a process exceeds a soft + * memory limit. This is the corpse fork path and here we decide if + * vm_map_fork will be allowed when creating the corpse. + * The task being considered is suspended. + * + * By default, a vm_map_fork is allowed to proceed. + * + * A few simple policy assumptions: + * Desktop platform is not considered in this path. + * The vm_map_fork is always allowed. + * + * If the device has a zero system-wide task limit, + * then the vm_map_fork is allowed. + * + * And if a process's memory footprint calculates less + * than or equal to half of the system-wide task limit, + * then the vm_map_fork is allowed. This calculation + * is based on the assumption that a process can + * munch memory up to the system-wide task limit. + */ +boolean_t +memorystatus_allowed_vm_map_fork(task_t task) +{ + boolean_t is_allowed = TRUE; /* default */ + +#if CONFIG_EMBEDDED + + uint64_t footprint_in_bytes; + uint64_t max_allowed_bytes; + + if (max_task_footprint_mb == 0) { + set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED); + return is_allowed; + } + + footprint_in_bytes = get_task_phys_footprint(task); + + /* + * Maximum is 1/4 of the system-wide task limit. + */ + max_allowed_bytes = ((uint64_t)max_task_footprint_mb * 1024 * 1024) >> 2; + + if (footprint_in_bytes > max_allowed_bytes) { + printf("memorystatus disallowed vm_map_fork %lld %lld\n", footprint_in_bytes, max_allowed_bytes); + set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED); + return !is_allowed; + } +#endif /* CONFIG_EMBEDDED */ + + set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED); + return is_allowed; } 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) +memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages) { assert(task); assert(footprint); @@ -3985,13 +5024,8 @@ memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *ma assert(((uint32_t)pages) == pages); *footprint = (uint32_t)pages; - if (max_footprint) { - pages = (get_task_phys_footprint_max(task) / PAGE_SIZE_64); - assert(((uint32_t)pages) == pages); - *max_footprint = (uint32_t)pages; - } if (max_footprint_lifetime) { - pages = (get_task_resident_max(task) / PAGE_SIZE_64); + pages = (get_task_phys_footprint_lifetime_max(task) / PAGE_SIZE_64); assert(((uint32_t)pages) == pages); *max_footprint_lifetime = (uint32_t)pages; } @@ -4004,10 +5038,10 @@ memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *ma static void memorystatus_get_task_phys_footprint_page_counts(task_t task, - uint64_t *internal_pages, uint64_t *internal_compressed_pages, - uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, - uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, - uint64_t *iokit_mapped_pages, uint64_t *page_table_pages) + uint64_t *internal_pages, uint64_t *internal_compressed_pages, + uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, + uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, + uint64_t *iokit_mapped_pages, uint64_t *page_table_pages) { assert(task); @@ -4058,6 +5092,8 @@ memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, unsigned int i; + LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); + if (memorystatus_jetsam_snapshot_count == 0) { /* * No active snapshot. @@ -4077,7 +5113,6 @@ memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, for (i = 0; i < memorystatus_jetsam_snapshot_count; i++) { if (snapshot_list[i].pid == p->p_pid) { - entry = &snapshot_list[i]; if (entry->killed || entry->jse_killtime) { @@ -4098,63 +5133,65 @@ memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, entry->jse_killtime = killtime; entry->jse_gencount = snapshot->js_gencount; entry->jse_idle_delta = p->p_memstat_idle_delta; +#if CONFIG_FREEZE + entry->jse_thaw_count = p->p_memstat_thaw_count; +#else /* CONFIG_FREEZE */ + entry->jse_thaw_count = 0; +#endif /* CONFIG_FREEZE */ /* * If a process has moved between bands since snapshot was * initialized, then likely these fields changed too. */ - if (entry->priority != p->p_memstat_effectivepriority) { - + if (entry->priority != p->p_memstat_effectivepriority) { strlcpy(entry->name, p->p_name, sizeof(entry->name)); entry->priority = p->p_memstat_effectivepriority; entry->state = memorystatus_build_state(p); entry->user_data = p->p_memstat_userdata; entry->fds = p->p_fd->fd_nfiles; - } - - /* - * Always update the page counts on a kill. - */ - - uint32_t pages = 0; - uint32_t max_pages = 0; - uint32_t max_pages_lifetime = 0; - uint32_t purgeable_pages = 0; - - memorystatus_get_task_page_counts(p->task, &pages, &max_pages, &max_pages_lifetime, &purgeable_pages); - entry->pages = (uint64_t)pages; - entry->max_pages = (uint64_t)max_pages; - entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; - entry->purgeable_pages = (uint64_t)purgeable_pages; - - uint64_t internal_pages = 0; - uint64_t internal_compressed_pages = 0; - uint64_t purgeable_nonvolatile_pages = 0; - uint64_t purgeable_nonvolatile_compressed_pages = 0; - uint64_t alternate_accounting_pages = 0; - uint64_t alternate_accounting_compressed_pages = 0; - uint64_t iokit_mapped_pages = 0; - uint64_t page_table_pages = 0; - - memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, - &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, - &alternate_accounting_pages, &alternate_accounting_compressed_pages, - &iokit_mapped_pages, &page_table_pages); - - entry->jse_internal_pages = internal_pages; - entry->jse_internal_compressed_pages = internal_compressed_pages; - entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages; - entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages; - entry->jse_alternate_accounting_pages = alternate_accounting_pages; - entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages; - entry->jse_iokit_mapped_pages = iokit_mapped_pages; - entry->jse_page_table_pages = page_table_pages; - - uint64_t region_count = 0; - memorystatus_get_task_memory_region_count(p->task, ®ion_count); - entry->jse_memory_region_count = region_count; - - goto exit; + } + + /* + * Always update the page counts on a kill. + */ + + uint32_t pages = 0; + uint32_t max_pages_lifetime = 0; + uint32_t purgeable_pages = 0; + + memorystatus_get_task_page_counts(p->task, &pages, &max_pages_lifetime, &purgeable_pages); + entry->pages = (uint64_t)pages; + entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; + entry->purgeable_pages = (uint64_t)purgeable_pages; + + uint64_t internal_pages = 0; + uint64_t internal_compressed_pages = 0; + uint64_t purgeable_nonvolatile_pages = 0; + uint64_t purgeable_nonvolatile_compressed_pages = 0; + uint64_t alternate_accounting_pages = 0; + uint64_t alternate_accounting_compressed_pages = 0; + uint64_t iokit_mapped_pages = 0; + uint64_t page_table_pages = 0; + + memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, + &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, + &alternate_accounting_pages, &alternate_accounting_compressed_pages, + &iokit_mapped_pages, &page_table_pages); + + entry->jse_internal_pages = internal_pages; + entry->jse_internal_compressed_pages = internal_compressed_pages; + entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages; + entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages; + entry->jse_alternate_accounting_pages = alternate_accounting_pages; + entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages; + entry->jse_iokit_mapped_pages = iokit_mapped_pages; + entry->jse_page_table_pages = page_table_pages; + + uint64_t region_count = 0; + memorystatus_get_task_memory_region_count(p->task, ®ion_count); + entry->jse_memory_region_count = region_count; + + goto exit; } } @@ -4173,8 +5210,7 @@ memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, unsigned int next = memorystatus_jetsam_snapshot_count; - if(memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[next], (snapshot->js_gencount)) == TRUE) { - + if (memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[next], (snapshot->js_gencount)) == TRUE) { entry = &snapshot_list[next]; entry->killed = kill_cause; entry->jse_killtime = killtime; @@ -4189,7 +5225,7 @@ memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, * when we notice we've hit the max. */ printf("memorystatus: WARNING snapshot buffer is full, count %d\n", - memorystatus_jetsam_snapshot_count); + memorystatus_jetsam_snapshot_count); } } } @@ -4207,43 +5243,63 @@ exit: */ MEMORYSTATUS_DEBUG(4, "memorystatus_update_jetsam_snapshot_entry_locked: failed to update pid %d, priority %d, count %d\n", - p->p_pid, p->p_memstat_effectivepriority, memorystatus_jetsam_snapshot_count); + p->p_pid, p->p_memstat_effectivepriority, memorystatus_jetsam_snapshot_count); } return; } -void memorystatus_pages_update(unsigned int pages_avail) +#if CONFIG_JETSAM +void +memorystatus_pages_update(unsigned int pages_avail) { memorystatus_available_pages = pages_avail; #if VM_PRESSURE_EVENTS /* * Since memorystatus_available_pages changes, we should - * re-evaluate the pressure levels on the system and + * re-evaluate the pressure levels on the system and * check if we need to wake the pressure thread. * We also update memorystatus_level in that routine. - */ + */ vm_pressure_response(); if (memorystatus_available_pages <= memorystatus_available_pages_pressure) { - if (memorystatus_hwm_candidates || (memorystatus_available_pages <= memorystatus_available_pages_critical)) { memorystatus_thread_wake(); } } +#if CONFIG_FREEZE + /* + * We can't grab the freezer_mutex here even though that synchronization would be correct to inspect + * the # of frozen processes and wakeup the freezer thread. Reason being that we come here into this + * code with (possibly) the page-queue locks held and preemption disabled. So trying to grab a mutex here + * will result in the "mutex with preemption disabled" panic. + */ + + if (memorystatus_freeze_thread_should_run() == TRUE) { + /* + * The freezer thread is usually woken up by some user-space call i.e. pid_hibernate(any process). + * That trigger isn't invoked often enough and so we are enabling this explicit wakeup here. + */ + if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + thread_wakeup((event_t)&memorystatus_freeze_wakeup); + } + } +#endif /* CONFIG_FREEZE */ + #else /* VM_PRESSURE_EVENTS */ boolean_t critical, delta; - + if (!memorystatus_delta) { - return; + return; } - + critical = (pages_avail < memorystatus_available_pages_critical) ? TRUE : FALSE; - delta = ((pages_avail >= (memorystatus_available_pages + memorystatus_delta)) - || (memorystatus_available_pages >= (pages_avail + memorystatus_delta))) ? TRUE : FALSE; - + delta = ((pages_avail >= (memorystatus_available_pages + memorystatus_delta)) + || (memorystatus_available_pages >= (pages_avail + memorystatus_delta))) ? TRUE : FALSE; + if (critical || delta) { unsigned int total_pages; @@ -4256,25 +5312,25 @@ void memorystatus_pages_update(unsigned int pages_avail) } #endif /* VM_PRESSURE_EVENTS */ } +#endif /* CONFIG_JETSAM */ static boolean_t memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount) -{ +{ clock_sec_t tv_sec; clock_usec_t tv_usec; uint32_t pages = 0; - uint32_t max_pages = 0; uint32_t max_pages_lifetime = 0; uint32_t purgeable_pages = 0; - uint64_t internal_pages = 0; - uint64_t internal_compressed_pages = 0; - uint64_t purgeable_nonvolatile_pages = 0; - uint64_t purgeable_nonvolatile_compressed_pages = 0; - uint64_t alternate_accounting_pages = 0; - uint64_t alternate_accounting_compressed_pages = 0; - uint64_t iokit_mapped_pages = 0; - uint64_t page_table_pages =0; - uint64_t region_count = 0; + uint64_t internal_pages = 0; + uint64_t internal_compressed_pages = 0; + uint64_t purgeable_nonvolatile_pages = 0; + uint64_t purgeable_nonvolatile_compressed_pages = 0; + uint64_t alternate_accounting_pages = 0; + uint64_t alternate_accounting_compressed_pages = 0; + uint64_t iokit_mapped_pages = 0; + uint64_t page_table_pages = 0; + uint64_t region_count = 0; uint64_t cids[COALITION_NUM_TYPES]; memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t)); @@ -4283,16 +5339,15 @@ memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_sna strlcpy(&entry->name[0], p->p_name, sizeof(entry->name)); entry->priority = p->p_memstat_effectivepriority; - memorystatus_get_task_page_counts(p->task, &pages, &max_pages, &max_pages_lifetime, &purgeable_pages); + memorystatus_get_task_page_counts(p->task, &pages, &max_pages_lifetime, &purgeable_pages); entry->pages = (uint64_t)pages; - entry->max_pages = (uint64_t)max_pages; entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; entry->purgeable_pages = (uint64_t)purgeable_pages; memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, - &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, - &alternate_accounting_pages, &alternate_accounting_compressed_pages, - &iokit_mapped_pages, &page_table_pages); + &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, + &alternate_accounting_pages, &alternate_accounting_compressed_pages, + &iokit_mapped_pages, &page_table_pages); entry->jse_internal_pages = internal_pages; entry->jse_internal_compressed_pages = internal_compressed_pages; @@ -4312,40 +5367,46 @@ memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_sna entry->fds = p->p_fd->fd_nfiles; absolutetime_to_microtime(get_task_cpu_time(p->task), &tv_sec, &tv_usec); - entry->cpu_time.tv_sec = tv_sec; - entry->cpu_time.tv_usec = tv_usec; + entry->cpu_time.tv_sec = (int64_t)tv_sec; + entry->cpu_time.tv_usec = (int64_t)tv_usec; assert(p->p_stats != NULL); - entry->jse_starttime = p->p_stats->ps_start; /* abstime process started */ - entry->jse_killtime = 0; /* abstime jetsam chose to kill process */ - entry->killed = 0; /* the jetsam kill cause */ - entry->jse_gencount = gencount; /* indicates a pass through jetsam thread, when process was targeted to be killed */ + entry->jse_starttime = p->p_stats->ps_start; /* abstime process started */ + entry->jse_killtime = 0; /* abstime jetsam chose to kill process */ + entry->killed = 0; /* the jetsam kill cause */ + entry->jse_gencount = gencount; /* indicates a pass through jetsam thread, when process was targeted to be killed */ entry->jse_idle_delta = p->p_memstat_idle_delta; /* Most recent timespan spent in idle-band */ +#if CONFIG_FREEZE + entry->jse_thaw_count = p->p_memstat_thaw_count; +#else /* CONFIG_FREEZE */ + entry->jse_thaw_count = 0; +#endif /* CONFIG_FREEZE */ + proc_coalitionids(p, cids); entry->jse_coalition_jetsam_id = cids[COALITION_TYPE_JETSAM]; - return TRUE; + return TRUE; } static void memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot) { kern_return_t kr = KERN_SUCCESS; - mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; - vm_statistics64_data_t vm_stat; + mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; + vm_statistics64_data_t vm_stat; - if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count) != KERN_SUCCESS)) { + if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count)) != KERN_SUCCESS) { printf("memorystatus_init_jetsam_snapshot_stats: host_statistics64 failed with %d\n", kr); memset(&snapshot->stats, 0, sizeof(snapshot->stats)); } else { - snapshot->stats.free_pages = vm_stat.free_count; - snapshot->stats.active_pages = vm_stat.active_count; - snapshot->stats.inactive_pages = vm_stat.inactive_count; - snapshot->stats.throttled_pages = vm_stat.throttled_count; - snapshot->stats.purgeable_pages = vm_stat.purgeable_count; - snapshot->stats.wired_pages = vm_stat.wire_count; + snapshot->stats.free_pages = vm_stat.free_count; + snapshot->stats.active_pages = vm_stat.active_count; + snapshot->stats.inactive_pages = vm_stat.inactive_count; + snapshot->stats.throttled_pages = vm_stat.throttled_count; + snapshot->stats.purgeable_pages = vm_stat.purgeable_count; + snapshot->stats.wired_pages = vm_stat.wire_count; snapshot->stats.speculative_pages = vm_stat.speculative_count; snapshot->stats.filebacked_pages = vm_stat.external_page_count; @@ -4355,6 +5416,10 @@ memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot) snapshot->stats.compressor_pages = vm_stat.compressor_page_count; snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor; } + + get_zone_map_size(&snapshot->stats.zone_map_size, &snapshot->stats.zone_map_capacity); + get_largest_zone_info(snapshot->stats.largest_zone_name, sizeof(snapshot->stats.largest_zone_name), + &snapshot->stats.largest_zone_size); } /* @@ -4363,7 +5428,8 @@ memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot) * Data can be consumed at any time. */ void -memorystatus_init_at_boot_snapshot() { +memorystatus_init_at_boot_snapshot() +{ memorystatus_init_snapshot_vmstats(&memorystatus_at_boot_snapshot); memorystatus_at_boot_snapshot.entry_count = 0; memorystatus_at_boot_snapshot.notification_time = 0; /* updated when consumed */ @@ -4380,6 +5446,8 @@ memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snap memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL; unsigned int snapshot_max = 0; + LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); + if (od_snapshot) { /* * This is an on_demand snapshot @@ -4408,19 +5476,19 @@ memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snap while (next_p) { p = next_p; next_p = memorystatus_get_next_proc_locked(&b, p, TRUE); - + if (FALSE == memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i], snapshot->js_gencount)) { continue; } - + 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", - p->p_pid, - 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], - 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]); + p->p_pid, + 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], + 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]); if (++i == snapshot_max) { break; - } + } } snapshot->entry_count = i; @@ -4433,11 +5501,13 @@ memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snap #if DEVELOPMENT || DEBUG +#if CONFIG_JETSAM static int -memorystatus_cmd_set_panic_bits(user_addr_t buffer, uint32_t buffer_size) { +memorystatus_cmd_set_panic_bits(user_addr_t buffer, uint32_t buffer_size) +{ int ret; memorystatus_jetsam_panic_options_t debug; - + if (buffer_size != sizeof(memorystatus_jetsam_panic_options_t)) { return EINVAL; } @@ -4446,16 +5516,17 @@ memorystatus_cmd_set_panic_bits(user_addr_t buffer, uint32_t buffer_size) { if (ret) { return ret; } - + /* Panic bits match kMemorystatusKilled* enum */ memorystatus_jetsam_panic_debug = (memorystatus_jetsam_panic_debug & ~debug.mask) | (debug.data & debug.mask); - + /* Copyout new value */ debug.data = memorystatus_jetsam_panic_debug; ret = copyout(&debug, buffer, sizeof(memorystatus_jetsam_panic_options_t)); - + return ret; } +#endif /* CONFIG_JETSAM */ /* * Triggers a sort_order on a specified jetsam priority band. @@ -4463,8 +5534,8 @@ memorystatus_cmd_set_panic_bits(user_addr_t buffer, uint32_t buffer_size) { * function. */ static int -memorystatus_cmd_test_jetsam_sort(int priority, int sort_order) { - +memorystatus_cmd_test_jetsam_sort(int priority, int sort_order) +{ int error = 0; unsigned int bucket_index = 0; @@ -4478,80 +5549,248 @@ memorystatus_cmd_test_jetsam_sort(int priority, int sort_order) { error = memorystatus_sort_bucket(bucket_index, sort_order); - return (error); + return error; } #endif /* DEVELOPMENT || DEBUG */ +/* + * Prepare the process to be killed (set state, update snapshot) and kill it. + */ +static uint64_t memorystatus_purge_before_jetsam_success = 0; + +static boolean_t +memorystatus_kill_proc(proc_t p, uint32_t cause, os_reason_t jetsam_reason, boolean_t *killed) +{ + pid_t aPid = 0; + uint32_t aPid_ep = 0; + + uint64_t killtime = 0; + clock_sec_t tv_sec; + clock_usec_t tv_usec; + uint32_t tv_msec; + boolean_t retval = FALSE; + uint64_t num_pages_purged = 0; + + aPid = p->p_pid; + aPid_ep = p->p_memstat_effectivepriority; + + if (cause != kMemorystatusKilledVnodes && cause != kMemorystatusKilledZoneMapExhaustion) { + /* + * Genuine memory pressure and not other (vnode/zone) resource exhaustion. + */ + boolean_t success = FALSE; + + networking_memstatus_callout(p, cause); + num_pages_purged = vm_purgeable_purge_task_owned(p->task); + + if (num_pages_purged) { + /* + * We actually purged something and so let's + * check if we need to continue with the kill. + */ + if (cause == kMemorystatusKilledHiwat) { + uint64_t footprint_in_bytes = get_task_phys_footprint(p->task); + uint64_t memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ + success = (footprint_in_bytes <= memlimit_in_bytes); + } else { + success = (memorystatus_avail_pages_below_pressure() == FALSE); + } + + if (success) { + memorystatus_purge_before_jetsam_success++; + + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: purged %llu pages from pid %d [%s] and avoided %s\n", + num_pages_purged, aPid, (*p->p_name ? p->p_name : "unknown"), memorystatus_kill_cause_name[cause]); + + *killed = FALSE; + + return TRUE; + } + } + } + +#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) + MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %lld Mb > 1 (%d Mb)\n", + (memorystatus_jetsam_policy & kPolicyDiagnoseActive) ? "suspending": "killing", + aPid, (*p->p_name ? p->p_name : "unknown"), + (footprint_in_bytes / (1024ULL * 1024ULL)), /* converted bytes to MB */ + p->p_memstat_memlimit); +#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ + + killtime = mach_absolute_time(); + absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); + tv_msec = tv_usec / 1000; + +#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) + if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) { + if (cause == kMemorystatusKilledHiwat) { + MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] for diagnosis - memorystatus_available_pages: %d\n", + aPid, (*p->p_name ? p->p_name: "(unknown)"), memorystatus_available_pages); + } else { + int activeProcess = p->p_memstat_state & P_MEMSTAT_FOREGROUND; + if (activeProcess) { + MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memorystatus_available_pages: %d\n", + aPid, (*p->p_name ? p->p_name: "(unknown)"), memorystatus_available_pages); + + if (memorystatus_jetsam_policy & kPolicyDiagnoseFirst) { + jetsam_diagnostic_suspended_one_active_proc = 1; + printf("jetsam: returning after suspending first active proc - %d\n", aPid); + } + } + } + + proc_list_lock(); + /* This diagnostic code is going away soon. Ignore the kMemorystatusInvalid cause here. */ + memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusInvalid, killtime); + proc_list_unlock(); + + p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED; + + if (p) { + task_suspend(p->task); + *killed = TRUE; + } + } else +#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ + { + proc_list_lock(); + memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); + proc_list_unlock(); + + char kill_reason_string[128]; + + if (cause == kMemorystatusKilledHiwat) { + strlcpy(kill_reason_string, "killing_highwater_process", 128); + } else { + if (aPid_ep == JETSAM_PRIORITY_IDLE) { + strlcpy(kill_reason_string, "killing_idle_process", 128); + } else { + strlcpy(kill_reason_string, "killing_top_process", 128); + } + } + + os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: %s pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", + (unsigned long)tv_sec, tv_msec, kill_reason_string, + aPid, (*p->p_name ? p->p_name : "unknown"), + memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)memorystatus_available_pages); + + /* + * memorystatus_do_kill drops a reference, so take another one so we can + * continue to use this exit reason even after memorystatus_do_kill() + * returns + */ + os_reason_ref(jetsam_reason); + + retval = memorystatus_do_kill(p, cause, jetsam_reason); + + *killed = retval; + } + + return retval; +} + /* * Jetsam the first process in the queue. */ static boolean_t memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, os_reason_t jetsam_reason, - int32_t *priority, uint32_t *errors) + int32_t *priority, uint32_t *errors) { pid_t aPid; proc_t p = PROC_NULL, next_p = PROC_NULL; - boolean_t new_snapshot = FALSE, killed = FALSE; - int kill_count = 0; + boolean_t new_snapshot = FALSE, force_new_snapshot = FALSE, killed = FALSE, freed_mem = FALSE; unsigned int i = 0; uint32_t aPid_ep; - uint64_t killtime = 0; - clock_sec_t tv_sec; - clock_usec_t tv_usec; - uint32_t tv_msec; + int32_t local_max_kill_prio = JETSAM_PRIORITY_IDLE; #ifndef CONFIG_FREEZE #pragma unused(any) #endif - + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, - memorystatus_available_pages, 0, 0, 0, 0); + memorystatus_available_pages, 0, 0, 0, 0); +#if CONFIG_JETSAM if (sort_flag == TRUE) { (void)memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT); } + local_max_kill_prio = max_kill_priority; + + force_new_snapshot = FALSE; + +#else /* CONFIG_JETSAM */ + + if (sort_flag == TRUE) { + (void)memorystatus_sort_bucket(JETSAM_PRIORITY_IDLE, JETSAM_SORT_DEFAULT); + } + + /* + * On macos, we currently only have 2 reasons to be here: + * + * kMemorystatusKilledZoneMapExhaustion + * AND + * kMemorystatusKilledVMCompressorSpaceShortage + * + * If we are here because of kMemorystatusKilledZoneMapExhaustion, we will consider + * any and all processes as eligible kill candidates since we need to avoid a panic. + * + * Since this function can be called async. it is harder to toggle the max_kill_priority + * value before and after a call. And so we use this local variable to set the upper band + * on the eligible kill bands. + */ + if (cause == kMemorystatusKilledZoneMapExhaustion) { + local_max_kill_prio = JETSAM_PRIORITY_MAX; + } else { + local_max_kill_prio = max_kill_priority; + } + + /* + * And, because we are here under extreme circumstances, we force a snapshot even for + * IDLE kills. + */ + force_new_snapshot = TRUE; + +#endif /* CONFIG_JETSAM */ + proc_list_lock(); next_p = memorystatus_get_first_proc_locked(&i, TRUE); - while (next_p) { + while (next_p && (next_p->p_memstat_effectivepriority <= local_max_kill_prio)) { #if DEVELOPMENT || DEBUG - int activeProcess; int procSuspendedForDiagnosis; #endif /* DEVELOPMENT || DEBUG */ - + p = next_p; next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); - + #if DEVELOPMENT || DEBUG - activeProcess = p->p_memstat_state & P_MEMSTAT_FOREGROUND; procSuspendedForDiagnosis = p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED; #endif /* DEVELOPMENT || DEBUG */ - + aPid = p->p_pid; aPid_ep = p->p_memstat_effectivepriority; if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { continue; /* with lock held */ } - -#if DEVELOPMENT || DEBUG + +#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && procSuspendedForDiagnosis) { printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid); continue; } -#endif /* DEVELOPMENT || DEBUG */ +#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ - if (cause == kMemorystatusKilledVnodes) - { + if (cause == kMemorystatusKilledVnodes) { /* * If the system runs out of vnodes, we systematically jetsam * processes in hopes of stumbling onto a vnode gain that helps * the system recover. The process that happens to trigger - * this path has no known relationship to the vnode consumption. - * We attempt to safeguard that process e.g: do not jetsam it. + * this path has no known relationship to the vnode shortage. + * Deadlock avoidance: attempt to safeguard the caller. */ if (p == current_proc()) { @@ -4562,128 +5801,88 @@ memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause #if CONFIG_FREEZE boolean_t skip; - boolean_t reclaim_proc = !(p->p_memstat_state & (P_MEMSTAT_LOCKED | P_MEMSTAT_NORECLAIM)); + boolean_t reclaim_proc = !(p->p_memstat_state & P_MEMSTAT_LOCKED); if (any || reclaim_proc) { skip = FALSE; } else { skip = TRUE; } - + if (skip) { continue; } else #endif { - /* - * Capture a snapshot if none exists and: - * - priority was not requested (this is something other than an ambient kill) - * - the priority was requested *and* the targeted process is not at idle priority - */ - if ((memorystatus_jetsam_snapshot_count == 0) && - (memorystatus_idle_snapshot || ((!priority) || (priority && (aPid_ep != JETSAM_PRIORITY_IDLE))))) { - memorystatus_init_jetsam_snapshot_locked(NULL,0); - new_snapshot = TRUE; - } - - /* - * Mark as terminated so that if exit1() indicates success, but the process (for example) - * is blocked in task_exception_notify(), it'll be skipped if encountered again - see - * . This is cheaper than examining P_LEXIT, which requires the - * acquisition of the proc lock. - */ - p->p_memstat_state |= P_MEMSTAT_TERMINATED; - - killtime = mach_absolute_time(); - absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); - tv_msec = tv_usec / 1000; - -#if DEVELOPMENT || DEBUG - if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && activeProcess) { - MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memory_status_level: %d\n", - aPid, (*p->p_name ? p->p_name: "(unknown)"), memorystatus_level); - memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusKilledDiagnostic, killtime); - p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED; - if (memorystatus_jetsam_policy & kPolicyDiagnoseFirst) { - jetsam_diagnostic_suspended_one_active_proc = 1; - printf("jetsam: returning after suspending first active proc - %d\n", aPid); - } - - p = proc_ref_locked(p); - proc_list_unlock(); - if (p) { - task_suspend(p->task); - if (priority) { - *priority = aPid_ep; - } - proc_rele(p); - killed = TRUE; - } - - goto exit; - } else -#endif /* DEVELOPMENT || DEBUG */ - { - /* Shift queue, update stats */ - memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); - - if (proc_ref_locked(p) == p) { - proc_list_unlock(); - printf("%lu.%02d memorystatus: %s %d [%s] (%s %d) - memorystatus_available_pages: %d\n", - (unsigned long)tv_sec, tv_msec, - ((aPid_ep == JETSAM_PRIORITY_IDLE) ? "idle exiting pid" : "jetsam killing top process pid"), - aPid, (*p->p_name ? p->p_name : "(unknown)"), - jetsam_kill_cause_name[cause], aPid_ep, memorystatus_available_pages); + if (proc_ref_locked(p) == p) { + /* + * Mark as terminated so that if exit1() indicates success, but the process (for example) + * is blocked in task_exception_notify(), it'll be skipped if encountered again - see + * . This is cheaper than examining P_LEXIT, which requires the + * acquisition of the proc lock. + */ + p->p_memstat_state |= P_MEMSTAT_TERMINATED; + } else { + /* + * We need to restart the search again because + * proc_ref_locked _can_ drop the proc_list lock + * and we could have lost our stored next_p via + * an exit() on another core. + */ + i = 0; + next_p = memorystatus_get_first_proc_locked(&i, TRUE); + continue; + } - /* - * memorystatus_do_kill() drops a reference, so take another one so we can - * continue to use this exit reason even after memorystatus_do_kill() - * returns. - */ - os_reason_ref(jetsam_reason); + /* + * Capture a snapshot if none exists and: + * - we are forcing a new snapshot creation, either because: + * - on a particular platform we need these snapshots every time, OR + * - a boot-arg/embedded device tree property has been set. + * - priority was not requested (this is something other than an ambient kill) + * - the priority was requested *and* the targeted process is not at idle priority + */ + if ((memorystatus_jetsam_snapshot_count == 0) && + (force_new_snapshot || memorystatus_idle_snapshot || ((!priority) || (priority && (aPid_ep != JETSAM_PRIORITY_IDLE))))) { + memorystatus_init_jetsam_snapshot_locked(NULL, 0); + new_snapshot = TRUE; + } - killed = memorystatus_do_kill(p, cause, jetsam_reason); + proc_list_unlock(); - /* Success? */ - if (killed) { - if (priority) { - *priority = aPid_ep; - } - proc_rele(p); - kill_count++; - goto exit; + freed_mem = memorystatus_kill_proc(p, cause, jetsam_reason, &killed); /* purged and/or killed 'p' */ + /* Success? */ + if (freed_mem) { + if (killed) { + if (priority) { + *priority = aPid_ep; } - - /* - * Failure - first unwind the state, - * then fall through to restart the search. - */ + } else { + /* purged */ proc_list_lock(); - proc_rele_locked(p); p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; - p->p_memstat_state |= P_MEMSTAT_ERROR; - *errors += 1; + proc_list_unlock(); } - - /* - * Failure - restart the search. - * - * We might have raced with "p" exiting on another core, resulting in no - * ref on "p". Or, we may have failed to kill "p". - * - * Either way, we fall thru to here, leaving the proc in the - * P_MEMSTAT_TERMINATED state. - * - * And, we hold the the proc_list_lock at this point. - */ - - i = 0; - next_p = memorystatus_get_first_proc_locked(&i, TRUE); + proc_rele(p); + goto exit; } + + /* + * Failure - first unwind the state, + * then fall through to restart the search. + */ + proc_list_lock(); + proc_rele_locked(p); + p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; + p->p_memstat_state |= P_MEMSTAT_ERROR; + *errors += 1; + + i = 0; + next_p = memorystatus_get_first_proc_locked(&i, TRUE); } } - + proc_list_unlock(); - + exit: os_reason_free(jetsam_reason); @@ -4693,19 +5892,19 @@ exit: memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; proc_list_unlock(); } - + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, - memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); + memorystatus_available_pages, killed ? aPid : 0, 0, 0, 0); return killed; } /* - * Jetsam aggressively + * Jetsam aggressively */ static boolean_t -memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reason_t jetsam_reason, int aggr_count, - int32_t priority_max, uint32_t *errors) +memorystatus_kill_top_process_aggressive(uint32_t cause, int aggr_count, + int32_t priority_max, uint32_t *errors) { pid_t aPid; proc_t p = PROC_NULL, next_p = PROC_NULL; @@ -4715,17 +5914,21 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso int32_t aPid_ep = 0; unsigned int memorystatus_level_snapshot = 0; uint64_t killtime = 0; - clock_sec_t tv_sec; - clock_usec_t tv_usec; - uint32_t tv_msec; - -#pragma unused(any) + clock_sec_t tv_sec; + clock_usec_t tv_usec; + uint32_t tv_msec; + os_reason_t jetsam_reason = OS_REASON_NULL; KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, - memorystatus_available_pages, priority_max, 0, 0, 0); + memorystatus_available_pages, priority_max, 0, 0, 0); memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT); + jetsam_reason = os_reason_create(OS_REASON_JETSAM, cause); + if (jetsam_reason == OS_REASON_NULL) { + printf("memorystatus_kill_top_process_aggressive: failed to allocate exit reason\n"); + } + proc_list_lock(); next_p = memorystatus_get_first_proc_locked(&i, TRUE); @@ -4735,20 +5938,21 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso int procSuspendedForDiagnosis; #endif /* DEVELOPMENT || DEBUG */ - if ((unsigned int)(next_p->p_memstat_effectivepriority) != i) { - + if (((next_p->p_listflag & P_LIST_EXITED) != 0) || + ((unsigned int)(next_p->p_memstat_effectivepriority) != i)) { /* - * We have raced with next_p running on another core, as it has - * moved to a different jetsam priority band. This means we have - * lost our place in line while traversing the jetsam list. We + * We have raced with next_p running on another core. + * It may be exiting or it may have moved to a different + * jetsam priority band. This means we have lost our + * place in line while traversing the jetsam list. We * attempt to recover by rewinding to the beginning of the band * we were already traversing. By doing this, we do not guarantee * that no process escapes this aggressive march, but we can make * skipping an entire range of processes less likely. (PR-21069019) */ - MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: rewinding %s moved from band %d --> %d\n", - aggr_count, (*next_p->p_name ? next_p->p_name : "unknown"), i, next_p->p_memstat_effectivepriority); + MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: rewinding band %d, %s(%d) moved or exiting.\n", + aggr_count, i, (*next_p->p_name ? next_p->p_name : "unknown"), next_p->p_pid); next_p = memorystatus_get_first_proc_locked(&i, TRUE); continue; @@ -4758,47 +5962,47 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); if (p->p_memstat_effectivepriority > priority_max) { - /* + /* * Bail out of this killing spree if we have * reached beyond the priority_max jetsam band. - * That is, we kill up to and through the + * That is, we kill up to and through the * priority_max jetsam band. */ proc_list_unlock(); goto exit; } - + #if DEVELOPMENT || DEBUG activeProcess = p->p_memstat_state & P_MEMSTAT_FOREGROUND; procSuspendedForDiagnosis = p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED; #endif /* DEVELOPMENT || DEBUG */ - + aPid = p->p_pid; aPid_ep = p->p_memstat_effectivepriority; if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { continue; } - -#if DEVELOPMENT || DEBUG + +#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && procSuspendedForDiagnosis) { printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid); continue; } -#endif /* DEVELOPMENT || DEBUG */ +#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ /* * Capture a snapshot if none exists. */ if (memorystatus_jetsam_snapshot_count == 0) { - memorystatus_init_jetsam_snapshot_locked(NULL,0); + memorystatus_init_jetsam_snapshot_locked(NULL, 0); new_snapshot = TRUE; } - - /* + + /* * Mark as terminated so that if exit1() indicates success, but the process (for example) - * is blocked in task_exception_notify(), it'll be skipped if encountered again - see - * . This is cheaper than examining P_LEXIT, which requires the + * is blocked in task_exception_notify(), it'll be skipped if encountered again - see + * . This is cheaper than examining P_LEXIT, which requires the * acquisition of the proc lock. */ p->p_memstat_state |= P_MEMSTAT_TERMINATED; @@ -4806,7 +6010,7 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso killtime = mach_absolute_time(); absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); tv_msec = tv_usec / 1000; - + /* Shift queue, update stats */ memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); @@ -4822,25 +6026,25 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso while (next_p && (proc_ref_locked(next_p) != next_p)) { proc_t temp_p; - /* - * We must have raced with next_p exiting on another core. - * Recover by getting the next eligible process in the band. - */ + /* + * We must have raced with next_p exiting on another core. + * Recover by getting the next eligible process in the band. + */ MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: skipping %d [%s] (exiting?)\n", - aggr_count, next_p->p_pid, (*next_p->p_name ? next_p->p_name : "(unknown)")); + aggr_count, next_p->p_pid, (*next_p->p_name ? next_p->p_name : "(unknown)")); temp_p = next_p; next_p = memorystatus_get_next_proc_locked(&i, temp_p, TRUE); - } + } } proc_list_unlock(); - printf("%lu.%01d memorystatus: aggressive%d: %s %d [%s] (%s %d) - memorystatus_available_pages: %d\n", - (unsigned long)tv_sec, tv_msec, aggr_count, - ((aPid_ep == JETSAM_PRIORITY_IDLE) ? "idle exiting pid" : "jetsam killing pid"), - aPid, (*p->p_name ? p->p_name : "(unknown)"), - jetsam_kill_cause_name[cause], aPid_ep, memorystatus_available_pages); + printf("%lu.%03d memorystatus: %s%d pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", + (unsigned long)tv_sec, tv_msec, + ((aPid_ep == JETSAM_PRIORITY_IDLE) ? "killing_idle_process_aggressive" : "killing_top_process_aggressive"), + aggr_count, aPid, (*p->p_name ? p->p_name : "unknown"), + memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)memorystatus_available_pages); memorystatus_level_snapshot = memorystatus_level; @@ -4859,7 +6063,7 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso p = NULL; killed = FALSE; - /* + /* * Continue the killing spree. */ proc_list_lock(); @@ -4879,7 +6083,7 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso continue; } - + /* * Failure - first unwind the state, * then fall through to restart the search. @@ -4902,7 +6106,7 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso * We might have raced with "p" exiting on another core, resulting in no * ref on "p". Or, we may have failed to kill "p". * - * Either way, we fall thru to here, leaving the proc in the + * Either way, we fall thru to here, leaving the proc in the * P_MEMSTAT_TERMINATED or P_MEMSTAT_ERROR state. * * And, we hold the the proc_list_lock at this point. @@ -4910,52 +6114,48 @@ memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, os_reaso next_p = memorystatus_get_first_proc_locked(&i, TRUE); } - + proc_list_unlock(); - + exit: os_reason_free(jetsam_reason); /* Clear snapshot if freshly captured and no target was found */ if (new_snapshot && (kill_count == 0)) { - memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; + proc_list_lock(); + memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; + proc_list_unlock(); } - + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, - memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); + memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); if (kill_count > 0) { - return(TRUE); - } - else { - return(FALSE); + return TRUE; + } else { + return FALSE; } } static boolean_t -memorystatus_kill_hiwat_proc(uint32_t *errors) +memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged) { pid_t aPid = 0; proc_t p = PROC_NULL, next_p = PROC_NULL; - boolean_t new_snapshot = FALSE, killed = FALSE; - int kill_count = 0; + boolean_t new_snapshot = FALSE, killed = FALSE, freed_mem = FALSE; unsigned int i = 0; uint32_t aPid_ep; - uint64_t killtime = 0; - clock_sec_t tv_sec; - clock_usec_t tv_usec; - uint32_t tv_msec; os_reason_t jetsam_reason = OS_REASON_NULL; KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_START, - memorystatus_available_pages, 0, 0, 0, 0); - + memorystatus_available_pages, 0, 0, 0, 0); + jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_HIGHWATER); if (jetsam_reason == OS_REASON_NULL) { printf("memorystatus_kill_hiwat_proc: failed to allocate exit reason\n"); } proc_list_lock(); - + next_p = memorystatus_get_first_proc_locked(&i, TRUE); while (next_p) { uint64_t footprint_in_bytes = 0; @@ -4964,43 +6164,30 @@ memorystatus_kill_hiwat_proc(uint32_t *errors) p = next_p; next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); - + aPid = p->p_pid; aPid_ep = p->p_memstat_effectivepriority; - + if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { continue; } - + /* skip if no limit set */ if (p->p_memstat_memlimit <= 0) { continue; } -#if 0 - /* - * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore. - * Background limits are described via the inactive limit slots. - * Their fatal/non-fatal setting will drive whether or not to be - * considered in this kill path. - */ - - /* skip if a currently inapplicable limit is encountered */ - if ((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) { - continue; - } -#endif footprint_in_bytes = get_task_phys_footprint(p->task); - memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ + memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ skip = (footprint_in_bytes <= memlimit_in_bytes); -#if DEVELOPMENT || DEBUG +#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) if (!skip && (memorystatus_jetsam_policy & kPolicyDiagnoseActive)) { if (p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED) { continue; } } -#endif /* DEVELOPMENT || DEBUG */ +#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ #if CONFIG_FREEZE if (!skip) { @@ -5008,105 +6195,72 @@ memorystatus_kill_hiwat_proc(uint32_t *errors) skip = TRUE; } else { skip = FALSE; - } + } } #endif if (skip) { continue; } else { -#if DEVELOPMENT || DEBUG - MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %lld Mb > 1 (%d Mb)\n", - (memorystatus_jetsam_policy & kPolicyDiagnoseActive) ? "suspending": "killing", - aPid, (*p->p_name ? p->p_name : "unknown"), - (footprint_in_bytes / (1024ULL * 1024ULL)), /* converted bytes to MB */ - p->p_memstat_memlimit); -#endif /* DEVELOPMENT || DEBUG */ - if (memorystatus_jetsam_snapshot_count == 0) { - memorystatus_init_jetsam_snapshot_locked(NULL,0); - new_snapshot = TRUE; - } - - p->p_memstat_state |= P_MEMSTAT_TERMINATED; - - killtime = mach_absolute_time(); - absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); - tv_msec = tv_usec / 1000; - -#if DEVELOPMENT || DEBUG - if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) { - MEMORYSTATUS_DEBUG(1, "jetsam: pid %d suspended for diagnosis - memorystatus_available_pages: %d\n", aPid, memorystatus_available_pages); - memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusKilledDiagnostic, killtime); - p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED; - - p = proc_ref_locked(p); - proc_list_unlock(); - if (p) { - task_suspend(p->task); - proc_rele(p); - killed = TRUE; - } - - goto exit; - } else -#endif /* DEVELOPMENT || DEBUG */ - { - memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusKilledHiwat, killtime); - - if (proc_ref_locked(p) == p) { - proc_list_unlock(); + memorystatus_init_jetsam_snapshot_locked(NULL, 0); + new_snapshot = TRUE; + } - printf("%lu.%02d memorystatus: jetsam killing pid %d [%s] (highwater %d) - memorystatus_available_pages: %d\n", - (unsigned long)tv_sec, tv_msec, aPid, (*p->p_name ? p->p_name : "(unknown)"), aPid_ep, memorystatus_available_pages); + if (proc_ref_locked(p) == p) { + /* + * Mark as terminated so that if exit1() indicates success, but the process (for example) + * is blocked in task_exception_notify(), it'll be skipped if encountered again - see + * . This is cheaper than examining P_LEXIT, which requires the + * acquisition of the proc lock. + */ + p->p_memstat_state |= P_MEMSTAT_TERMINATED; - /* - * memorystatus_do_kill drops a reference, so take another one so we can - * continue to use this exit reason even after memorystatus_do_kill() - * returns - */ - os_reason_ref(jetsam_reason); + proc_list_unlock(); + } else { + /* + * We need to restart the search again because + * proc_ref_locked _can_ drop the proc_list lock + * and we could have lost our stored next_p via + * an exit() on another core. + */ + i = 0; + next_p = memorystatus_get_first_proc_locked(&i, TRUE); + continue; + } - killed = memorystatus_do_kill(p, kMemorystatusKilledHiwat, jetsam_reason); + freed_mem = memorystatus_kill_proc(p, kMemorystatusKilledHiwat, jetsam_reason, &killed); /* purged and/or killed 'p' */ - /* Success? */ - if (killed) { - proc_rele(p); - kill_count++; - goto exit; - } + /* Success? */ + if (freed_mem) { + if (killed == FALSE) { + /* purged 'p'..don't reset HWM candidate count */ + *purged = TRUE; - /* - * Failure - first unwind the state, - * then fall through to restart the search. - */ proc_list_lock(); - proc_rele_locked(p); p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; - p->p_memstat_state |= P_MEMSTAT_ERROR; - *errors += 1; + proc_list_unlock(); } - - /* - * Failure - restart the search. - * - * We might have raced with "p" exiting on another core, resulting in no - * ref on "p". Or, we may have failed to kill "p". - * - * Either way, we fall thru to here, leaving the proc in the - * P_MEMSTAT_TERMINATED state. - * - * And, we hold the the proc_list_lock at this point. - */ - - i = 0; - next_p = memorystatus_get_first_proc_locked(&i, TRUE); + proc_rele(p); + goto exit; } + /* + * Failure - first unwind the state, + * then fall through to restart the search. + */ + proc_list_lock(); + proc_rele_locked(p); + p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; + p->p_memstat_state |= P_MEMSTAT_ERROR; + *errors += 1; + + i = 0; + next_p = memorystatus_get_first_proc_locked(&i, TRUE); } } - + proc_list_unlock(); - + exit: os_reason_free(jetsam_reason); @@ -5116,9 +6270,9 @@ exit: memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; proc_list_unlock(); } - - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END, - memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END, + memorystatus_available_pages, killed ? aPid : 0, 0, 0, 0); return killed; } @@ -5130,30 +6284,36 @@ exit: * false -- no pinned process was jetsammed */ static boolean_t -memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, int aggr_count, uint32_t *errors) +memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, uint32_t *errors) { pid_t aPid = 0; proc_t p = PROC_NULL, next_p = PROC_NULL; boolean_t new_snapshot = FALSE, killed = FALSE; int kill_count = 0; - unsigned int i = JETSAM_PRIORITY_ELEVATED_INACTIVE; uint32_t aPid_ep; uint64_t killtime = 0; - clock_sec_t tv_sec; - clock_usec_t tv_usec; - uint32_t tv_msec; + clock_sec_t tv_sec; + clock_usec_t tv_usec; + uint32_t tv_msec; KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, - memorystatus_available_pages, 0, 0, 0, 0); + memorystatus_available_pages, 0, 0, 0, 0); + +#if CONFIG_FREEZE + boolean_t consider_frozen_only = FALSE; + + if (band == (unsigned int) memorystatus_freeze_jetsam_band) { + consider_frozen_only = TRUE; + } +#endif /* CONFIG_FREEZE */ proc_list_lock(); - next_p = memorystatus_get_first_proc_locked(&i, FALSE); + next_p = memorystatus_get_first_proc_locked(&band, FALSE); while (next_p) { - p = next_p; - next_p = memorystatus_get_next_proc_locked(&i, p, FALSE); + next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); aPid = p->p_pid; aPid_ep = p->p_memstat_effectivepriority; @@ -5170,20 +6330,24 @@ memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, in } #if CONFIG_FREEZE + if (consider_frozen_only && !(p->p_memstat_state & P_MEMSTAT_FROZEN)) { + continue; + } + if (p->p_memstat_state & P_MEMSTAT_LOCKED) { continue; } -#endif +#endif /* CONFIG_FREEZE */ #if DEVELOPMENT || DEBUG MEMORYSTATUS_DEBUG(1, "jetsam: elevated%d process pid %d [%s] - memorystatus_available_pages: %d\n", - aggr_count, - aPid, (*p->p_name ? p->p_name : "unknown"), - memorystatus_available_pages); + aggr_count, + aPid, (*p->p_name ? p->p_name : "unknown"), + memorystatus_available_pages); #endif /* DEVELOPMENT || DEBUG */ if (memorystatus_jetsam_snapshot_count == 0) { - memorystatus_init_jetsam_snapshot_locked(NULL,0); + memorystatus_init_jetsam_snapshot_locked(NULL, 0); new_snapshot = TRUE; } @@ -5196,14 +6360,13 @@ memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, in memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); if (proc_ref_locked(p) == p) { - proc_list_unlock(); - printf("%lu.%01d memorystatus: elevated%d: jetsam killing pid %d [%s] (%s %d) - memorystatus_available_pages: %d\n", - (unsigned long)tv_sec, tv_msec, - aggr_count, - aPid, (*p->p_name ? p->p_name : "(unknown)"), - jetsam_kill_cause_name[cause], aPid_ep, memorystatus_available_pages); + os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: killing_top_process_elevated%d pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", + (unsigned long)tv_sec, tv_msec, + aggr_count, + aPid, (*p->p_name ? p->p_name : "unknown"), + memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)memorystatus_available_pages); /* * memorystatus_do_kill drops a reference, so take another one so we can @@ -5243,7 +6406,7 @@ memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, in * And, we hold the the proc_list_lock at this point. */ - next_p = memorystatus_get_first_proc_locked(&i, FALSE); + next_p = memorystatus_get_first_proc_locked(&band, FALSE); } proc_list_unlock(); @@ -5259,61 +6422,83 @@ exit: } KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, - memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); + memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); - return (killed); + return killed; } -static boolean_t -memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause) { +static boolean_t +memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause) +{ /* * TODO: allow a general async path * * NOTE: If a new async kill cause is added, make sure to update memorystatus_thread() to * add the appropriate exit reason code mapping. */ - if ((victim_pid != -1) || (cause != kMemorystatusKilledVMPageShortage && cause != kMemorystatusKilledVMThrashing && - cause != kMemorystatusKilledFCThrashing)) { + if ((victim_pid != -1) || + (cause != kMemorystatusKilledVMPageShortage && + cause != kMemorystatusKilledVMCompressorThrashing && + cause != kMemorystatusKilledVMCompressorSpaceShortage && + cause != kMemorystatusKilledFCThrashing && + cause != kMemorystatusKilledZoneMapExhaustion)) { return FALSE; } - + kill_under_pressure_cause = cause; memorystatus_thread_wake(); return TRUE; } -boolean_t -memorystatus_kill_on_VM_page_shortage(boolean_t async) { +boolean_t +memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async) +{ if (async) { - return memorystatus_kill_process_async(-1, kMemorystatusKilledVMPageShortage); + return memorystatus_kill_process_async(-1, kMemorystatusKilledVMCompressorSpaceShortage); } else { - os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMPAGESHORTAGE); + os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE); if (jetsam_reason == OS_REASON_NULL) { - printf("memorystatus_kill_on_VM_page_shortage -- sync: failed to allocate jetsam reason\n"); + printf("memorystatus_kill_on_VM_compressor_space_shortage -- sync: failed to allocate jetsam reason\n"); } - return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMPageShortage, jetsam_reason); + return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMCompressorSpaceShortage, jetsam_reason); } } +#if CONFIG_JETSAM boolean_t -memorystatus_kill_on_VM_thrashing(boolean_t async) { +memorystatus_kill_on_VM_compressor_thrashing(boolean_t async) +{ if (async) { - return memorystatus_kill_process_async(-1, kMemorystatusKilledVMThrashing); + return memorystatus_kill_process_async(-1, kMemorystatusKilledVMCompressorThrashing); } else { - os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMTHRASHING); + os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING); if (jetsam_reason == OS_REASON_NULL) { - printf("memorystatus_kill_on_VM_thrashing -- sync: failed to allocate jetsam reason\n"); + printf("memorystatus_kill_on_VM_compressor_thrashing -- sync: failed to allocate jetsam reason\n"); } - return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMThrashing, jetsam_reason); + return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMCompressorThrashing, jetsam_reason); } } boolean_t -memorystatus_kill_on_FC_thrashing(boolean_t async) { +memorystatus_kill_on_VM_page_shortage(boolean_t async) +{ + if (async) { + return memorystatus_kill_process_async(-1, kMemorystatusKilledVMPageShortage); + } else { + os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMPAGESHORTAGE); + if (jetsam_reason == OS_REASON_NULL) { + printf("memorystatus_kill_on_VM_page_shortage -- sync: failed to allocate jetsam reason\n"); + } + return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMPageShortage, jetsam_reason); + } +} +boolean_t +memorystatus_kill_on_FC_thrashing(boolean_t async) +{ if (async) { return memorystatus_kill_process_async(-1, kMemorystatusKilledFCThrashing); } else { @@ -5326,8 +6511,9 @@ memorystatus_kill_on_FC_thrashing(boolean_t async) { } } -boolean_t -memorystatus_kill_on_vnode_limit(void) { +boolean_t +memorystatus_kill_on_vnode_limit(void) +{ os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_VNODE); if (jetsam_reason == OS_REASON_NULL) { printf("memorystatus_kill_on_vnode_limit: failed to allocate jetsam reason\n"); @@ -5338,6 +6524,23 @@ memorystatus_kill_on_vnode_limit(void) { #endif /* CONFIG_JETSAM */ +boolean_t +memorystatus_kill_on_zone_map_exhaustion(pid_t pid) +{ + boolean_t res = FALSE; + if (pid == -1) { + res = memorystatus_kill_process_async(-1, kMemorystatusKilledZoneMapExhaustion); + } else { + os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_ZONE_MAP_EXHAUSTION); + if (jetsam_reason == OS_REASON_NULL) { + printf("memorystatus_kill_on_zone_map_exhaustion: failed to allocate jetsam reason\n"); + } + + res = memorystatus_kill_process_sync(pid, kMemorystatusKilledZoneMapExhaustion, jetsam_reason); + } + return res; +} + #if CONFIG_FREEZE __private_extern__ void @@ -5350,18 +6553,86 @@ memorystatus_freeze_init(void) freezer_lck_grp = lck_grp_alloc_init("freezer", freezer_lck_grp_attr); lck_mtx_init(&freezer_mutex, freezer_lck_grp, NULL); - + + /* + * This is just the default value if the underlying + * storage device doesn't have any specific budget. + * We check with the storage layer in memorystatus_freeze_update_throttle() + * before we start our freezing the first time. + */ + memorystatus_freeze_budget_pages_remaining = (memorystatus_freeze_daily_mb_max * 1024 * 1024) / PAGE_SIZE; + result = kernel_thread_start(memorystatus_freeze_thread, NULL, &thread); if (result == KERN_SUCCESS) { + proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2); + proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE); + thread_set_thread_name(thread, "VM_freezer"); + thread_deallocate(thread); } else { panic("Could not create memorystatus_freeze_thread"); } } +static boolean_t +memorystatus_is_process_eligible_for_freeze(proc_t p) +{ + /* + * Called with proc_list_lock held. + */ + + LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); + + boolean_t should_freeze = FALSE; + uint32_t state = 0, entry_count = 0, pages = 0, i = 0; + int probability_of_use = 0; + + if (isApp(p) == FALSE) { + goto out; + } + + state = p->p_memstat_state; + + if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED | P_MEMSTAT_FREEZE_IGNORE)) || + !(state & P_MEMSTAT_SUSPENDED)) { + goto out; + } + + /* Only freeze processes meeting our minimum resident page criteria */ + memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); + if (pages < memorystatus_freeze_pages_min) { + goto out; + } + + entry_count = (memorystatus_global_probabilities_size / sizeof(memorystatus_internal_probabilities_t)); + + if (entry_count) { + for (i = 0; i < entry_count; i++) { + if (strncmp(memorystatus_global_probabilities_table[i].proc_name, + p->p_name, + MAXCOMLEN + 1) == 0) { + probability_of_use = memorystatus_global_probabilities_table[i].use_probability; + break; + } + } + + if (probability_of_use == 0) { + goto out; + } + } + + should_freeze = TRUE; +out: + return should_freeze; +} + /* * Synchronously freeze the passed proc. Called with a reference to the proc held. * + * Doesn't deal with re-freezing because this is called on a specific process and + * not by the freezer thread. If that changes, we'll have to teach it about + * refreezing a frozen process. + * * Returns EINVAL or the value returned by task_freeze(). */ int @@ -5370,69 +6641,47 @@ memorystatus_freeze_process_sync(proc_t p) int ret = EINVAL; pid_t aPid = 0; boolean_t memorystatus_freeze_swap_low = FALSE; - - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, - memorystatus_available_pages, 0, 0, 0, 0); + int freezer_error_code = 0; lck_mtx_lock(&freezer_mutex); if (p == NULL) { + printf("memorystatus_freeze_process_sync: Invalid process\n"); goto exit; } if (memorystatus_freeze_enabled == FALSE) { + printf("memorystatus_freeze_process_sync: Freezing is DISABLED\n"); goto exit; } if (!memorystatus_can_freeze(&memorystatus_freeze_swap_low)) { + printf("memorystatus_freeze_process_sync: Low compressor and/or low swap space...skipping freeze\n"); goto exit; } - if (memorystatus_freeze_update_throttle()) { - printf("memorystatus_freeze_process_sync: in throttle, ignorning freeze\n"); - memorystatus_freeze_throttle_count++; + memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining); + if (!memorystatus_freeze_budget_pages_remaining) { + printf("memorystatus_freeze_process_sync: exit with NO available budget\n"); goto exit; } proc_list_lock(); if (p != NULL) { - uint32_t purgeable, wired, clean, dirty, state; - uint32_t max_pages, pages, i; - boolean_t shared; + uint32_t purgeable, wired, clean, dirty, shared; + uint32_t max_pages, i; aPid = p->p_pid; - state = p->p_memstat_state; /* Ensure the process is eligible for freezing */ - if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FROZEN)) || !(state & P_MEMSTAT_SUSPENDED)) { - proc_list_unlock(); - goto exit; - } - - /* Only freeze processes meeting our minimum resident page criteria */ - memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL); - if (pages < memorystatus_freeze_pages_min) { + if (memorystatus_is_process_eligible_for_freeze(p) == FALSE) { proc_list_unlock(); goto exit; } if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { - - unsigned int avail_swap_space = 0; /* in pages. */ - - /* - * Freezer backed by the compressor and swap file(s) - * while will hold compressed data. - */ - avail_swap_space = vm_swap_get_free_space() / PAGE_SIZE_64; - - max_pages = MIN(avail_swap_space, memorystatus_freeze_pages_max); - - if (max_pages < memorystatus_freeze_pages_min) { - proc_list_unlock(); - goto exit; - } + max_pages = MIN(memorystatus_freeze_pages_max, memorystatus_freeze_budget_pages_remaining); } else { /* * We only have the compressor without any swap. @@ -5444,202 +6693,389 @@ memorystatus_freeze_process_sync(proc_t p) p->p_memstat_state |= P_MEMSTAT_LOCKED; proc_list_unlock(); - ret = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, FALSE); + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, + memorystatus_available_pages, 0, 0, 0, 0); + + ret = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */); + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, + memorystatus_available_pages, aPid, 0, 0, 0); + + DTRACE_MEMORYSTATUS6(memorystatus_freeze, proc_t, p, unsigned int, memorystatus_available_pages, boolean_t, purgeable, unsigned int, wired, uint32_t, clean, uint32_t, dirty); + + MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_process_sync: task_freeze %s for pid %d [%s] - " + "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n", + (ret == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"), + memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared); + + proc_list_lock(); + + if (ret == KERN_SUCCESS) { + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (specific) pid %d [%s]...done", + aPid, (*p->p_name ? p->p_name : "unknown")); + + memorystatus_freeze_entry_t data = { aPid, TRUE, dirty }; + + p->p_memstat_freeze_sharedanon_pages += shared; + + memorystatus_frozen_shared_mb += shared; + + if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == 0) { + p->p_memstat_state |= P_MEMSTAT_FROZEN; + memorystatus_frozen_count++; + } - DTRACE_MEMORYSTATUS6(memorystatus_freeze, proc_t, p, unsigned int, memorystatus_available_pages, boolean_t, purgeable, unsigned int, wired, uint32_t, clean, uint32_t, dirty); + p->p_memstat_frozen_count++; - MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_process_sync: task_freeze %s for pid %d [%s] - " - "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n", - (ret == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"), - memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared); + /* + * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process + * to its higher jetsam band. + */ + proc_list_unlock(); - proc_list_lock(); - p->p_memstat_state &= ~P_MEMSTAT_LOCKED; + memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); - if (ret == KERN_SUCCESS) { - memorystatus_freeze_entry_t data = { aPid, TRUE, dirty }; + if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + ret = memorystatus_update_inactive_jetsam_priority_band(p->p_pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, + memorystatus_freeze_jetsam_band, TRUE); - memorystatus_frozen_count++; + if (ret) { + printf("Elevating the frozen process failed with %d\n", ret); + /* not fatal */ + ret = 0; + } - p->p_memstat_state |= (P_MEMSTAT_FROZEN | (shared ? 0: P_MEMSTAT_NORECLAIM)); + proc_list_lock(); - if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { /* Update stats */ for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) { throttle_intervals[i].pageouts += dirty; } + } else { + proc_list_lock(); } memorystatus_freeze_pageouts += dirty; - memorystatus_freeze_count++; - - proc_list_unlock(); - memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); + if (memorystatus_frozen_count == (memorystatus_frozen_processes_max - 1)) { + /* + * Add some eviction logic here? At some point should we + * jetsam a process to get back its swap space so that we + * can freeze a more eligible process at this moment in time? + */ + } } else { - proc_list_unlock(); + char reason[128]; + if (freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) { + strlcpy(reason, "too much shared memory", 128); + } + + if (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) { + strlcpy(reason, "low private-shared pages ratio", 128); + } + + if (freezer_error_code == FREEZER_ERROR_NO_COMPRESSOR_SPACE) { + strlcpy(reason, "no compressor space", 128); + } + + if (freezer_error_code == FREEZER_ERROR_NO_SWAP_SPACE) { + strlcpy(reason, "no swap space", 128); + } + + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (specific) pid %d [%s]...skipped (%s)", + aPid, (*p->p_name ? p->p_name : "unknown"), reason); + p->p_memstat_state |= P_MEMSTAT_FREEZE_IGNORE; } + + p->p_memstat_state &= ~P_MEMSTAT_LOCKED; + proc_list_unlock(); } exit: lck_mtx_unlock(&freezer_mutex); - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, - memorystatus_available_pages, aPid, 0, 0, 0); return ret; } static int -memorystatus_freeze_top_process(boolean_t *memorystatus_freeze_swap_low) +memorystatus_freeze_top_process(void) { pid_t aPid = 0; int ret = -1; proc_t p = PROC_NULL, next_p = PROC_NULL; unsigned int i = 0; - - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, - memorystatus_available_pages, 0, 0, 0, 0); + unsigned int band = JETSAM_PRIORITY_IDLE; + boolean_t refreeze_processes = FALSE; proc_list_lock(); - - next_p = memorystatus_get_first_proc_locked(&i, TRUE); + + if (memorystatus_frozen_count >= memorystatus_frozen_processes_max) { + /* + * Freezer is already full but we are here and so let's + * try to refreeze any processes we might have thawed + * in the past and push out their compressed state out. + */ + refreeze_processes = TRUE; + band = (unsigned int) memorystatus_freeze_jetsam_band; + } + +freeze_process: + + next_p = memorystatus_get_first_proc_locked(&band, FALSE); while (next_p) { kern_return_t kr; - uint32_t purgeable, wired, clean, dirty; - boolean_t shared; - uint32_t pages; + uint32_t purgeable, wired, clean, dirty, shared; uint32_t max_pages = 0; - uint32_t state; - + int freezer_error_code = 0; + p = next_p; - next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); + next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); aPid = p->p_pid; - state = p->p_memstat_state; - /* Ensure the process is eligible for freezing */ - if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FROZEN)) || !(state & P_MEMSTAT_SUSPENDED)) { - continue; // with lock held + if (p->p_memstat_effectivepriority != (int32_t) band) { + /* + * We shouldn't be freezing processes outside the + * prescribed band. + */ + break; } - - /* Only freeze processes meeting our minimum resident page criteria */ - memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL); - if (pages < memorystatus_freeze_pages_min) { - continue; // with lock held - } - - if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { - /* Ensure there's enough free space to freeze this process. */ + /* Ensure the process is eligible for (re-)freezing */ + if (refreeze_processes) { + /* + * Has to have been frozen once before. + */ + if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == FALSE) { + continue; + } - unsigned int avail_swap_space = 0; /* in pages. */ + /* + * Has to have been resumed once before. + */ + if ((p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) == FALSE) { + continue; + } /* - * Freezer backed by the compressor and swap file(s) - * while will hold compressed data. + * Not currently being looked at for something. */ - avail_swap_space = vm_swap_get_free_space() / PAGE_SIZE_64; + if (p->p_memstat_state & P_MEMSTAT_LOCKED) { + continue; + } - max_pages = MIN(avail_swap_space, memorystatus_freeze_pages_max); + /* + * We are going to try and refreeze and so re-evaluate + * the process. We don't want to double count the shared + * memory. So deduct the old snapshot here. + */ + memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages; + p->p_memstat_freeze_sharedanon_pages = 0; - if (max_pages < memorystatus_freeze_pages_min) { - *memorystatus_freeze_swap_low = TRUE; - proc_list_unlock(); - goto exit; + p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; + memorystatus_refreeze_eligible_count--; + } else { + if (memorystatus_is_process_eligible_for_freeze(p) == FALSE) { + continue; // with lock held } + } + + if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + /* + * Freezer backed by the compressor and swap file(s) + * will hold compressed data. + */ + + max_pages = MIN(memorystatus_freeze_pages_max, memorystatus_freeze_budget_pages_remaining); } else { /* * We only have the compressor pool. */ max_pages = UINT32_MAX - 1; } - + /* Mark as locked temporarily to avoid kill */ p->p_memstat_state |= P_MEMSTAT_LOCKED; p = proc_ref_locked(p); - proc_list_unlock(); if (!p) { - goto exit; + break; } - - kr = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, FALSE); - + + proc_list_unlock(); + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, + memorystatus_available_pages, 0, 0, 0, 0); + + kr = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */); + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, + memorystatus_available_pages, aPid, 0, 0, 0); + MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_top_process: task_freeze %s for pid %d [%s] - " - "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n", - (kr == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"), - memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared); - + "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n", + (kr == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"), + memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared); + proc_list_lock(); - p->p_memstat_state &= ~P_MEMSTAT_LOCKED; - + /* Success? */ if (KERN_SUCCESS == kr) { + if (refreeze_processes) { + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: Refreezing (general) pid %d [%s]...done", + aPid, (*p->p_name ? p->p_name : "unknown")); + } else { + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (general) pid %d [%s]...done", + aPid, (*p->p_name ? p->p_name : "unknown")); + } + memorystatus_freeze_entry_t data = { aPid, TRUE, dirty }; - - memorystatus_frozen_count++; - - p->p_memstat_state |= (P_MEMSTAT_FROZEN | (shared ? 0: P_MEMSTAT_NORECLAIM)); + + p->p_memstat_freeze_sharedanon_pages += shared; + + memorystatus_frozen_shared_mb += shared; + + if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == 0) { + p->p_memstat_state |= P_MEMSTAT_FROZEN; + memorystatus_frozen_count++; + } + + p->p_memstat_frozen_count++; + + /* + * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process + * to its higher jetsam band. + */ + proc_list_unlock(); + + memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + ret = memorystatus_update_inactive_jetsam_priority_band(p->p_pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, memorystatus_freeze_jetsam_band, TRUE); + + if (ret) { + printf("Elevating the frozen process failed with %d\n", ret); + /* not fatal */ + ret = 0; + } + + proc_list_lock(); + /* Update stats */ for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) { throttle_intervals[i].pageouts += dirty; } + } else { + proc_list_lock(); } memorystatus_freeze_pageouts += dirty; - memorystatus_freeze_count++; - - proc_list_unlock(); - memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); + if (memorystatus_frozen_count == (memorystatus_frozen_processes_max - 1)) { + /* + * Add some eviction logic here? At some point should we + * jetsam a process to get back its swap space so that we + * can freeze a more eligible process at this moment in time? + */ + } - /* Return KERN_SUCESS */ + /* Return KERN_SUCCESS */ ret = kr; + p->p_memstat_state &= ~P_MEMSTAT_LOCKED; + proc_rele_locked(p); + + /* + * We froze a process successfully. We can stop now + * and see if that helped. + */ + + break; } else { - proc_list_unlock(); + p->p_memstat_state &= ~P_MEMSTAT_LOCKED; + + if (refreeze_processes == TRUE) { + if ((freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) || + (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO)) { + /* + * Keeping this prior-frozen process in this high band when + * we failed to re-freeze it due to bad shared memory usage + * could cause excessive pressure on the lower bands. + * We need to demote it for now. It'll get re-evaluated next + * time because we don't set the P_MEMSTAT_FREEZE_IGNORE + * bit. + */ + + p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; + memorystatus_invalidate_idle_demotion_locked(p, TRUE); + memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, TRUE, TRUE); + } + } else { + p->p_memstat_state |= P_MEMSTAT_FREEZE_IGNORE; + } + + proc_rele_locked(p); + + char reason[128]; + if (freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) { + strlcpy(reason, "too much shared memory", 128); + } + + if (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) { + strlcpy(reason, "low private-shared pages ratio", 128); + } + + if (freezer_error_code == FREEZER_ERROR_NO_COMPRESSOR_SPACE) { + strlcpy(reason, "no compressor space", 128); + } + + if (freezer_error_code == FREEZER_ERROR_NO_SWAP_SPACE) { + strlcpy(reason, "no swap space", 128); + } + + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (general) pid %d [%s]...skipped (%s)", + aPid, (*p->p_name ? p->p_name : "unknown"), reason); + + if (vm_compressor_low_on_space() || vm_swap_low_on_space()) { + break; + } } - - proc_rele(p); - goto exit; } - + + if ((ret == -1) && + (memorystatus_refreeze_eligible_count >= MIN_THAW_REFREEZE_THRESHOLD) && + (refreeze_processes == FALSE)) { + /* + * We failed to freeze a process from the IDLE + * band AND we have some thawed processes + * AND haven't tried refreezing as yet. + * Let's try and re-freeze processes in the + * frozen band that have been resumed in the past + * and so have brought in state from disk. + */ + + band = (unsigned int) memorystatus_freeze_jetsam_band; + + refreeze_processes = TRUE; + + goto freeze_process; + } + proc_list_unlock(); - -exit: - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, - memorystatus_available_pages, aPid, 0, 0, 0); - + return ret; } -static inline boolean_t -memorystatus_can_freeze_processes(void) +static inline boolean_t +memorystatus_can_freeze_processes(void) { boolean_t ret; - + proc_list_lock(); - + if (memorystatus_suspended_count) { - uint32_t average_resident_pages, estimated_processes; - - /* Estimate the number of suspended processes we can fit */ - average_resident_pages = memorystatus_suspended_footprint_total / memorystatus_suspended_count; - estimated_processes = memorystatus_suspended_count + - ((memorystatus_available_pages - memorystatus_available_pages_critical) / average_resident_pages); - - /* If it's predicted that no freeze will occur, lower the threshold temporarily */ - if (estimated_processes <= FREEZE_SUSPENDED_THRESHOLD_DEFAULT) { - memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_LOW; - } else { - memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_DEFAULT; - } + memorystatus_freeze_suspended_threshold = MIN(memorystatus_freeze_suspended_threshold, FREEZE_SUSPENDED_THRESHOLD_DEFAULT); - MEMORYSTATUS_DEBUG(1, "memorystatus_can_freeze_processes: %d suspended processes, %d average resident pages / process, %d suspended processes estimated\n", - memorystatus_suspended_count, average_resident_pages, estimated_processes); - if ((memorystatus_suspended_count - memorystatus_frozen_count) > memorystatus_freeze_suspended_threshold) { ret = TRUE; } else { @@ -5648,127 +7084,302 @@ memorystatus_can_freeze_processes(void) } else { ret = FALSE; } - + proc_list_unlock(); - + return ret; } -static boolean_t +static boolean_t memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low) { boolean_t can_freeze = TRUE; /* Only freeze if we're sufficiently low on memory; this holds off freeze right - after boot, and is generally is a no-op once we've reached steady state. */ + * after boot, and is generally is a no-op once we've reached steady state. */ if (memorystatus_available_pages > memorystatus_freeze_threshold) { return FALSE; } - + /* Check minimum suspended process threshold. */ if (!memorystatus_can_freeze_processes()) { return FALSE; } assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); - if ( !VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { + /* + * In-core compressor used for freezing WITHOUT on-disk swap support. + */ + if (vm_compressor_low_on_space()) { + if (*memorystatus_freeze_swap_low) { + *memorystatus_freeze_swap_low = TRUE; + } + + can_freeze = FALSE; + } else { + if (*memorystatus_freeze_swap_low) { + *memorystatus_freeze_swap_low = FALSE; + } + + can_freeze = TRUE; + } + } else { + /* + * Freezing WITH on-disk swap support. + * + * In-core compressor fronts the swap. + */ + if (vm_swap_low_on_space()) { + if (*memorystatus_freeze_swap_low) { + *memorystatus_freeze_swap_low = TRUE; + } + + can_freeze = FALSE; + } + } + + return can_freeze; +} + +/* + * This function evaluates if the currently frozen processes deserve + * to stay in the higher jetsam band. If the # of thaws of a process + * is below our threshold, then we will demote that process into the IDLE + * band and put it at the head. We don't immediately kill the process here + * because it already has state on disk and so it might be worth giving + * it another shot at getting thawed/resumed and used. + */ +static void +memorystatus_demote_frozen_processes(void) +{ + unsigned int band = (unsigned int) memorystatus_freeze_jetsam_band; + unsigned int demoted_proc_count = 0; + proc_t p = PROC_NULL, next_p = PROC_NULL; + + proc_list_lock(); + + if (memorystatus_freeze_enabled == FALSE) { + /* + * Freeze has been disabled likely to + * reclaim swap space. So don't change + * any state on the frozen processes. + */ + proc_list_unlock(); + return; + } + + next_p = memorystatus_get_first_proc_locked(&band, FALSE); + while (next_p) { + p = next_p; + next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); + + if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == FALSE) { + continue; + } + + if (p->p_memstat_state & P_MEMSTAT_LOCKED) { + continue; + } + + if (p->p_memstat_thaw_count < memorystatus_thaw_count_demotion_threshold) { + p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; + memorystatus_invalidate_idle_demotion_locked(p, TRUE); + + memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, TRUE, TRUE); +#if DEVELOPMENT || DEBUG + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus_demote_frozen_process pid %d [%s]", + p->p_pid, (*p->p_name ? p->p_name : "unknown")); +#endif /* DEVELOPMENT || DEBUG */ + + /* + * The freezer thread will consider this a normal app to be frozen + * because it is in the IDLE band. So we don't need the + * P_MEMSTAT_REFREEZE_ELIGIBLE state here. Also, if it gets resumed + * we'll correctly count it as eligible for re-freeze again. + * + * We don't drop the frozen count because this process still has + * state on disk. So there's a chance it gets resumed and then it + * should land in the higher jetsam band. For that it needs to + * remain marked frozen. + */ + if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { + p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; + memorystatus_refreeze_eligible_count--; + } + + demoted_proc_count++; + } + + if (demoted_proc_count == memorystatus_max_frozen_demotions_daily) { + break; + } + } + + memorystatus_thaw_count = 0; + proc_list_unlock(); +} + + +/* + * This function will do 4 things: + * + * 1) check to see if we are currently in a degraded freezer mode, and if so: + * - check to see if our window has expired and we should exit this mode, OR, + * - return a budget based on the degraded throttle window's max. pageouts vs current pageouts. + * + * 2) check to see if we are in a NEW normal window and update the normal throttle window's params. + * + * 3) check what the current normal window allows for a budget. + * + * 4) calculate the current rate of pageouts for DEGRADED_WINDOW_MINS duration. If that rate is below + * what we would normally expect, then we are running low on our daily budget and need to enter + * degraded perf. mode. + */ + +static void +memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed) +{ + clock_sec_t sec; + clock_nsec_t nsec; + mach_timespec_t ts; + + unsigned int freeze_daily_pageouts_max = 0; + +#if DEVELOPMENT || DEBUG + if (!memorystatus_freeze_throttle_enabled) { /* - * In-core compressor used for freezing WITHOUT on-disk swap support. + * No throttling...we can use the full budget everytime. */ - if (vm_compressor_low_on_space()) { - if (*memorystatus_freeze_swap_low) { - *memorystatus_freeze_swap_low = TRUE; - } + *budget_pages_allowed = UINT64_MAX; + return; + } +#endif - can_freeze = FALSE; + clock_get_system_nanotime(&sec, &nsec); + ts.tv_sec = sec; + ts.tv_nsec = nsec; - } else { - if (*memorystatus_freeze_swap_low) { - *memorystatus_freeze_swap_low = FALSE; - } + struct throttle_interval_t *interval = NULL; - can_freeze = TRUE; + if (memorystatus_freeze_degradation == TRUE) { + interval = degraded_throttle_window; + + if (CMP_MACH_TIMESPEC(&ts, &interval->ts) >= 0) { + memorystatus_freeze_degradation = FALSE; + interval->pageouts = 0; + interval->max_pageouts = 0; + } else { + *budget_pages_allowed = interval->max_pageouts - interval->pageouts; } - } else { + } + + interval = normal_throttle_window; + + if (CMP_MACH_TIMESPEC(&ts, &interval->ts) >= 0) { /* - * Freezing WITH on-disk swap support. - * - * In-core compressor fronts the swap. + * New throttle window. + * Rollover any unused budget. + * Also ask the storage layer what the new budget needs to be. */ - if (vm_swap_low_on_space()) { - if (*memorystatus_freeze_swap_low) { - *memorystatus_freeze_swap_low = TRUE; - } + uint64_t freeze_daily_budget = 0; + unsigned int daily_budget_pageouts = 0; - can_freeze = FALSE; + if (vm_swap_max_budget(&freeze_daily_budget)) { + memorystatus_freeze_daily_mb_max = (freeze_daily_budget / (1024 * 1024)); + os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: memorystatus_freeze_daily_mb_max set to %dMB\n", memorystatus_freeze_daily_mb_max); } - } - - return can_freeze; -} + freeze_daily_pageouts_max = memorystatus_freeze_daily_mb_max * (1024 * 1024 / PAGE_SIZE); + + daily_budget_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * freeze_daily_pageouts_max) / NORMAL_WINDOW_MINS)); + interval->max_pageouts = (interval->max_pageouts - interval->pageouts) + daily_budget_pageouts; -static void -memorystatus_freeze_update_throttle_interval(mach_timespec_t *ts, struct throttle_interval_t *interval) -{ - unsigned int freeze_daily_pageouts_max = memorystatus_freeze_daily_mb_max * (1024 * 1024 / PAGE_SIZE); - if (CMP_MACH_TIMESPEC(ts, &interval->ts) >= 0) { - if (!interval->max_pageouts) { - interval->max_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * freeze_daily_pageouts_max) / (24 * 60))); - } else { - printf("memorystatus_freeze_update_throttle_interval: %d minute throttle timeout, resetting\n", interval->mins); - } interval->ts.tv_sec = interval->mins * 60; interval->ts.tv_nsec = 0; - ADD_MACH_TIMESPEC(&interval->ts, ts); + ADD_MACH_TIMESPEC(&interval->ts, &ts); /* Since we update the throttle stats pre-freeze, adjust for overshoot here */ if (interval->pageouts > interval->max_pageouts) { interval->pageouts -= interval->max_pageouts; } else { interval->pageouts = 0; } - interval->throttle = FALSE; - } else if (!interval->throttle && interval->pageouts >= interval->max_pageouts) { - printf("memorystatus_freeze_update_throttle_interval: %d minute pageout limit exceeded; enabling throttle\n", interval->mins); - interval->throttle = TRUE; - } + *budget_pages_allowed = interval->max_pageouts; - MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining; throttle %s\n", - interval->pageouts, interval->max_pageouts, interval->mins, (interval->ts.tv_sec - ts->tv_sec) / 60, - interval->throttle ? "on" : "off"); -} - -static boolean_t -memorystatus_freeze_update_throttle(void) -{ - clock_sec_t sec; - clock_nsec_t nsec; - mach_timespec_t ts; - uint32_t i; - boolean_t throttled = FALSE; + memorystatus_demote_frozen_processes(); + } else { + /* + * Current throttle window. + * Deny freezing if we have no budget left. + * Try graceful degradation if we are within 25% of: + * - the daily budget, and + * - the current budget left is below our normal budget expectations. + */ #if DEVELOPMENT || DEBUG - if (!memorystatus_freeze_throttle_enabled) - return FALSE; -#endif + /* + * This can only happen in the INTERNAL configs because we allow modifying the daily budget for testing. + */ - clock_get_system_nanotime(&sec, &nsec); - ts.tv_sec = sec; - ts.tv_nsec = nsec; - - /* Check freeze pageouts over multiple intervals and throttle if we've exceeded our budget. - * - * This ensures that periods of inactivity can't be used as 'credit' towards freeze if the device has - * remained dormant for a long period. We do, however, allow increased thresholds for shorter intervals in - * order to allow for bursts of activity. - */ - for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) { - memorystatus_freeze_update_throttle_interval(&ts, &throttle_intervals[i]); - if (throttle_intervals[i].throttle == TRUE) - throttled = TRUE; - } + if (freeze_daily_pageouts_max > interval->max_pageouts) { + /* + * We just bumped the daily budget. Re-evaluate our normal window params. + */ + interval->max_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * freeze_daily_pageouts_max) / NORMAL_WINDOW_MINS)); + memorystatus_freeze_degradation = FALSE; //we'll re-evaluate this below... + } +#endif /* DEVELOPMENT || DEBUG */ + + if (memorystatus_freeze_degradation == FALSE) { + if (interval->pageouts >= interval->max_pageouts) { + *budget_pages_allowed = 0; + } else { + int budget_left = interval->max_pageouts - interval->pageouts; + int budget_threshold = (freeze_daily_pageouts_max * FREEZE_DEGRADATION_BUDGET_THRESHOLD) / 100; + + mach_timespec_t time_left = {0, 0}; + + time_left.tv_sec = interval->ts.tv_sec; + time_left.tv_nsec = 0; + + SUB_MACH_TIMESPEC(&time_left, &ts); + + if (budget_left <= budget_threshold) { + /* + * For the current normal window, calculate how much we would pageout in a DEGRADED_WINDOW_MINS duration. + * And also calculate what we would pageout for the same DEGRADED_WINDOW_MINS duration if we had the full + * daily pageout budget. + */ + + unsigned int current_budget_rate_allowed = ((budget_left / time_left.tv_sec) / 60) * DEGRADED_WINDOW_MINS; + unsigned int normal_budget_rate_allowed = (freeze_daily_pageouts_max / NORMAL_WINDOW_MINS) * DEGRADED_WINDOW_MINS; + + /* + * The current rate of pageouts is below what we would expect for + * the normal rate i.e. we have below normal budget left and so... + */ + + if (current_budget_rate_allowed < normal_budget_rate_allowed) { + memorystatus_freeze_degradation = TRUE; + degraded_throttle_window->max_pageouts = current_budget_rate_allowed; + degraded_throttle_window->pageouts = 0; - return throttled; + /* + * Switch over to the degraded throttle window so the budget + * doled out is based on that window. + */ + interval = degraded_throttle_window; + } + } + + *budget_pages_allowed = interval->max_pageouts - interval->pageouts; + } + } + } + + MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining; throttle %s\n", + interval->pageouts, interval->max_pageouts, interval->mins, (interval->ts.tv_sec - ts->tv_sec) / 60, + interval->throttle ? "on" : "off"); } static void @@ -5777,21 +7388,72 @@ memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused) static boolean_t memorystatus_freeze_swap_low = FALSE; lck_mtx_lock(&freezer_mutex); + if (memorystatus_freeze_enabled) { - if (memorystatus_can_freeze(&memorystatus_freeze_swap_low)) { - /* Only freeze if we've not exceeded our pageout budgets.*/ - if (!memorystatus_freeze_update_throttle()) { - memorystatus_freeze_top_process(&memorystatus_freeze_swap_low); - } else { - printf("memorystatus_freeze_thread: in throttle, ignoring freeze\n"); - memorystatus_freeze_throttle_count++; /* Throttled, update stats */ + if ((memorystatus_frozen_count < memorystatus_frozen_processes_max) || + (memorystatus_refreeze_eligible_count >= MIN_THAW_REFREEZE_THRESHOLD)) { + if (memorystatus_can_freeze(&memorystatus_freeze_swap_low)) { + /* Only freeze if we've not exceeded our pageout budgets.*/ + memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining); + + if (memorystatus_freeze_budget_pages_remaining) { + memorystatus_freeze_top_process(); + } } } } - lck_mtx_unlock(&freezer_mutex); + + /* + * We use memorystatus_apps_idle_delay_time because if/when we adopt aging for applications, + * it'll tie neatly into running the freezer once we age an application. + * + * Till then, it serves as a good interval that can be tuned via a sysctl too. + */ + memorystatus_freezer_thread_next_run_ts = mach_absolute_time() + memorystatus_apps_idle_delay_time; assert_wait((event_t) &memorystatus_freeze_wakeup, THREAD_UNINT); - thread_block((thread_continue_t) memorystatus_freeze_thread); + lck_mtx_unlock(&freezer_mutex); + + thread_block((thread_continue_t) memorystatus_freeze_thread); +} + +static boolean_t +memorystatus_freeze_thread_should_run(void) +{ + /* + * No freezer_mutex held here...see why near call-site + * within memorystatus_pages_update(). + */ + + boolean_t should_run = FALSE; + + if (memorystatus_freeze_enabled == FALSE) { + goto out; + } + + if (memorystatus_available_pages > memorystatus_freeze_threshold) { + goto out; + } + + if ((memorystatus_frozen_count >= memorystatus_frozen_processes_max) && + (memorystatus_refreeze_eligible_count < MIN_THAW_REFREEZE_THRESHOLD)) { + goto out; + } + + if (memorystatus_frozen_shared_mb_max && (memorystatus_frozen_shared_mb >= memorystatus_frozen_shared_mb_max)) { + goto out; + } + + uint64_t curr_time = mach_absolute_time(); + + if (curr_time < memorystatus_freezer_thread_next_run_ts) { + goto out; + } + + should_run = TRUE; + +out: + return should_run; } static int @@ -5813,7 +7475,7 @@ sysctl_memorystatus_do_fastwake_warmup_all SYSCTL_HANDLER_ARGS return 0; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_do_fastwake_warmup_all, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_do_fastwake_warmup_all, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorystatus_do_fastwake_warmup_all, "I", ""); #endif /* CONFIG_FREEZE */ @@ -5823,7 +7485,8 @@ SYSCTL_PROC(_kern, OID_AUTO, memorystatus_do_fastwake_warmup_all, CTLTYPE_INT|CT #if CONFIG_MEMORYSTATUS static int -memorystatus_send_note(int event_code, void *data, size_t data_length) { +memorystatus_send_note(int event_code, void *data, size_t data_length) +{ int ret; struct kev_msg ev_msg; @@ -5841,13 +7504,13 @@ memorystatus_send_note(int event_code, void *data, size_t data_length) { if (ret) { printf("%s: kev_post_msg() failed, err %d\n", __func__, ret); } - + return ret; } boolean_t -memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused boolean_t is_fatal, boolean_t limit_exceeded) { - +memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused boolean_t is_fatal, boolean_t limit_exceeded) +{ boolean_t ret = FALSE; boolean_t found_knote = FALSE; struct knote *kn = NULL; @@ -5873,8 +7536,39 @@ memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused bool * filt_memorystatus(). */ +#if CONFIG_EMBEDDED if (!limit_exceeded) { + /* + * Intentionally set either the unambiguous limit warning, + * the system-wide critical or the system-wide warning + * notification bit. + */ + if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { + kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; + found_knote = TRUE; + send_knote_count++; + } else if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) { + kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; + found_knote = TRUE; + send_knote_count++; + } else if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) { + kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN; + found_knote = TRUE; + send_knote_count++; + } + } else { + /* + * Send this notification when a process has exceeded a soft limit. + */ + if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { + kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; + found_knote = TRUE; + send_knote_count++; + } + } +#else /* CONFIG_EMBEDDED */ + if (!limit_exceeded) { /* * Processes on desktop are not expecting to handle a system-wide * critical or system-wide warning notification from this path. @@ -5885,7 +7579,7 @@ memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused bool */ if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { - found_knote=TRUE; + found_knote = TRUE; if (!is_fatal) { /* * Restrict proc_limit_warn notifications when @@ -5893,28 +7587,28 @@ memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused bool */ if (is_active) { if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE) { - /* - * Mark this knote for delivery. - */ - kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; + /* + * Mark this knote for delivery. + */ + kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; /* * And suppress it from future notifications. - */ - kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; + */ + kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; send_knote_count++; - } + } } else { if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE) { - /* - * Mark this knote for delivery. - */ - kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; + /* + * Mark this knote for delivery. + */ + kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; /* * And suppress it from future notifications. - */ - kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; + */ + kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; send_knote_count++; - } + } } } else { /* @@ -5946,7 +7640,6 @@ memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused bool kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE; kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; send_knote_count++; - } } else { if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE) { @@ -5969,6 +7662,7 @@ memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused bool } } } +#endif /* CONFIG_EMBEDDED */ } } @@ -6002,18 +7696,19 @@ memorystatus_low_mem_privileged_listener(uint32_t op_flags) return EINVAL; } - return (task_low_mem_privileged_listener(current_task(), set_privilege, NULL)); + return task_low_mem_privileged_listener(current_task(), set_privilege, NULL); } int -memorystatus_send_pressure_note(pid_t pid) { - MEMORYSTATUS_DEBUG(1, "memorystatus_send_pressure_note(): pid %d\n", pid); - return memorystatus_send_note(kMemorystatusPressureNote, &pid, sizeof(pid)); +memorystatus_send_pressure_note(pid_t pid) +{ + MEMORYSTATUS_DEBUG(1, "memorystatus_send_pressure_note(): pid %d\n", pid); + return memorystatus_send_note(kMemorystatusPressureNote, &pid, sizeof(pid)); } void -memorystatus_send_low_swap_note(void) { - +memorystatus_send_low_swap_note(void) +{ struct knote *kn = NULL; memorystatus_klist_lock(); @@ -6032,41 +7727,58 @@ memorystatus_send_low_swap_note(void) { } boolean_t -memorystatus_bg_pressure_eligible(proc_t p) { - boolean_t eligible = FALSE; - +memorystatus_bg_pressure_eligible(proc_t p) +{ + boolean_t eligible = FALSE; + proc_list_lock(); - + MEMORYSTATUS_DEBUG(1, "memorystatus_bg_pressure_eligible: pid %d, state 0x%x\n", p->p_pid, p->p_memstat_state); - - /* Foreground processes have already been dealt with at this point, so just test for eligibility */ - if (!(p->p_memstat_state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN))) { - eligible = TRUE; + + /* Foreground processes have already been dealt with at this point, so just test for eligibility */ + if (!(p->p_memstat_state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN))) { + eligible = TRUE; + } + + if (p->p_memstat_effectivepriority < JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC) { + /* + * IDLE and IDLE_DEFERRED bands contain processes + * that have dropped memory to be under their inactive + * memory limits. And so they can't really give back + * anything. + */ + eligible = FALSE; } - + proc_list_unlock(); - - return eligible; + + return eligible; } boolean_t -memorystatus_is_foreground_locked(proc_t p) { - return ((p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND) || - (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND_SUPPORT)); +memorystatus_is_foreground_locked(proc_t p) +{ + return (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND) || + (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND_SUPPORT); } /* * This is meant for stackshot and kperf -- it does not take the proc_list_lock * to access the p_memstat_dirty field. */ -boolean_t -memorystatus_proc_is_dirty_unsafe(void *v) +void +memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit) { if (!v) { - return FALSE; + *is_dirty = FALSE; + *is_dirty_tracked = FALSE; + *allow_idle_exit = FALSE; + } else { + proc_t p = (proc_t)v; + *is_dirty = (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) != 0; + *is_dirty_tracked = (p->p_memstat_dirty & P_DIRTY_TRACK) != 0; + *allow_idle_exit = (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) != 0; } - proc_t p = (proc_t)v; - return (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) != 0; } #endif /* CONFIG_MEMORYSTATUS */ @@ -6075,25 +7787,21 @@ memorystatus_proc_is_dirty_unsafe(void *v) * Trigger levels to test the mechanism. * Can be used via a sysctl. */ -#define TEST_LOW_MEMORY_TRIGGER_ONE 1 -#define TEST_LOW_MEMORY_TRIGGER_ALL 2 -#define TEST_PURGEABLE_TRIGGER_ONE 3 -#define TEST_PURGEABLE_TRIGGER_ALL 4 -#define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE 5 -#define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL 6 +#define TEST_LOW_MEMORY_TRIGGER_ONE 1 +#define TEST_LOW_MEMORY_TRIGGER_ALL 2 +#define TEST_PURGEABLE_TRIGGER_ONE 3 +#define TEST_PURGEABLE_TRIGGER_ALL 4 +#define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE 5 +#define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL 6 -boolean_t memorystatus_manual_testing_on = FALSE; -vm_pressure_level_t memorystatus_manual_testing_level = kVMPressureNormal; +boolean_t memorystatus_manual_testing_on = FALSE; +vm_pressure_level_t memorystatus_manual_testing_level = kVMPressureNormal; extern struct knote * vm_pressure_select_optimal_candidate_to_notify(struct klist *, int, boolean_t); -/* - * This value is the threshold that a process must meet to be considered for scavenging. - */ -#define VM_PRESSURE_MINIMUM_RSIZE 10 /* MB */ -#define VM_PRESSURE_NOTIFY_WAIT_PERIOD 10000 /* milliseconds */ +#define VM_PRESSURE_NOTIFY_WAIT_PERIOD 10000 /* milliseconds */ #if DEBUG #define VM_PRESSURE_DEBUG(cond, format, ...) \ @@ -6104,9 +7812,11 @@ do { \ #define VM_PRESSURE_DEBUG(cond, format, ...) #endif -#define INTER_NOTIFICATION_DELAY (250000) /* .25 second */ +#define INTER_NOTIFICATION_DELAY (250000) /* .25 second */ -void memorystatus_on_pageout_scan_end(void) { +void +memorystatus_on_pageout_scan_end(void) +{ /* No-op */ } @@ -6127,9 +7837,7 @@ boolean_t 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) { if (kn_max->kn_sfflags & knote_pressure_level) { - if (pressure_level_to_clear && task_has_been_notified(task, pressure_level_to_clear) == TRUE) { - task_clear_has_been_notified(task, pressure_level_to_clear); } @@ -6146,10 +7854,9 @@ memorystatus_klist_reset_all_for_level(vm_pressure_level_t pressure_level_to_cle struct knote *kn = NULL; memorystatus_klist_lock(); - SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) { - - proc_t p = PROC_NULL; - struct task* t = TASK_NULL; + SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) { + proc_t p = PROC_NULL; + struct task* t = TASK_NULL; p = knote_get_kq(kn)->kq_p; proc_list_lock(); @@ -6182,11 +7889,13 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int static void vm_dispatch_memory_pressure(void); void consider_vm_pressure_events(void); -void consider_vm_pressure_events(void) +void +consider_vm_pressure_events(void) { vm_dispatch_memory_pressure(); } -static void vm_dispatch_memory_pressure(void) +static void +vm_dispatch_memory_pressure(void) { memorystatus_update_vm_pressure(FALSE); } @@ -6197,13 +7906,13 @@ convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t); struct knote * vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int level, boolean_t target_foreground_process) { - struct knote *kn = NULL, *kn_max = NULL; - uint64_t resident_max = 0; /* MB */ - struct timeval curr_tstamp = {0, 0}; - int elapsed_msecs = 0; - int selected_task_importance = 0; - static int pressure_snapshot = -1; - boolean_t pressure_increase = FALSE; + struct knote *kn = NULL, *kn_max = NULL; + uint64_t resident_max = 0; /* MB */ + struct timeval curr_tstamp = {0, 0}; + int elapsed_msecs = 0; + int selected_task_importance = 0; + static int pressure_snapshot = -1; + boolean_t pressure_increase = FALSE; if (pressure_snapshot == -1) { /* @@ -6212,8 +7921,7 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int pressure_snapshot = level; pressure_increase = TRUE; } else { - - if (level >= pressure_snapshot) { + if (level && (level >= pressure_snapshot)) { pressure_increase = TRUE; } else { pressure_increase = FALSE; @@ -6238,14 +7946,13 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int microuptime(&curr_tstamp); - SLIST_FOREACH(kn, candidate_list, kn_selnext) { - - uint64_t resident_size = 0; /* MB */ - proc_t p = PROC_NULL; - struct task* t = TASK_NULL; - int curr_task_importance = 0; - boolean_t consider_knote = FALSE; - boolean_t privileged_listener = FALSE; + SLIST_FOREACH(kn, candidate_list, kn_selnext) { + uint64_t resident_size = 0; /* MB */ + proc_t p = PROC_NULL; + struct task* t = TASK_NULL; + int curr_task_importance = 0; + boolean_t consider_knote = FALSE; + boolean_t privileged_listener = FALSE; p = knote_get_kq(kn)->kq_p; proc_list_lock(); @@ -6286,21 +7993,22 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int } #endif /* CONFIG_MEMORYSTATUS */ +#if CONFIG_EMBEDDED + curr_task_importance = p->p_memstat_effectivepriority; +#else /* CONFIG_EMBEDDED */ curr_task_importance = task_importance_estimate(t); +#endif /* CONFIG_EMBEDDED */ /* * Privileged listeners are only considered in the multi-level pressure scheme * AND only if the pressure is increasing. */ if (level > 0) { - if (task_has_been_notified(t, level) == FALSE) { - /* * Is this a privileged listener? */ if (task_low_mem_privileged_listener(t, FALSE, &privileged_listener) == 0) { - if (privileged_listener) { kn_max = kn; proc_rele(p); @@ -6312,7 +8020,6 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int continue; } } else if (level == 0) { - /* * Task wasn't notified when the pressure was increasing and so * no need to notify it that the pressure is decreasing. @@ -6324,21 +8031,19 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int } /* - * We don't want a small process to block large processes from - * being notified again. - */ - resident_size = (get_task_phys_footprint(t))/(1024*1024ULL); /* MB */ - - if (resident_size >= VM_PRESSURE_MINIMUM_RSIZE) { + * We don't want a small process to block large processes from + * being notified again. + */ + resident_size = (get_task_phys_footprint(t)) / (1024 * 1024ULL); /* MB */ + if (resident_size >= vm_pressure_task_footprint_min) { if (level > 0) { /* * Warning or Critical Pressure. */ - if (pressure_increase) { + if (pressure_increase) { if ((curr_task_importance < selected_task_importance) || ((curr_task_importance == selected_task_importance) && (resident_size > resident_max))) { - /* * We have found a candidate process which is: * a) at a lower importance than the current selected process @@ -6351,7 +8056,6 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int } else { if ((curr_task_importance > selected_task_importance) || ((curr_task_importance == selected_task_importance) && (resident_size > resident_max))) { - /* * We have found a candidate process which is: * a) at a higher importance than the current selected process @@ -6368,7 +8072,6 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int */ if ((curr_task_importance > selected_task_importance) || ((curr_task_importance == selected_task_importance) && (resident_size > resident_max))) { - consider_knote = TRUE; } } @@ -6379,12 +8082,12 @@ vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int selected_task_importance = curr_task_importance; consider_knote = FALSE; /* reset for the next candidate */ } - } else { - /* There was no candidate with enough resident memory to scavenge */ - VM_PRESSURE_DEBUG(0, "[vm_pressure] threshold failed for pid %d with %llu resident...\n", p->p_pid, resident_size); - } + } else { + /* There was no candidate with enough resident memory to scavenge */ + VM_PRESSURE_DEBUG(0, "[vm_pressure] threshold failed for pid %d with %llu resident...\n", p->p_pid, resident_size); + } proc_rele(p); - } + } done_scanning: if (kn_max) { @@ -6395,36 +8098,36 @@ done_scanning: return kn_max; } -#define VM_PRESSURE_DECREASED_SMOOTHING_PERIOD 5000 /* milliseconds */ -#define WARNING_NOTIFICATION_RESTING_PERIOD 25 /* seconds */ -#define CRITICAL_NOTIFICATION_RESTING_PERIOD 25 /* seconds */ +#define VM_PRESSURE_DECREASED_SMOOTHING_PERIOD 5000 /* milliseconds */ +#define WARNING_NOTIFICATION_RESTING_PERIOD 25 /* seconds */ +#define CRITICAL_NOTIFICATION_RESTING_PERIOD 25 /* seconds */ uint64_t next_warning_notification_sent_at_ts = 0; uint64_t next_critical_notification_sent_at_ts = 0; kern_return_t -memorystatus_update_vm_pressure(boolean_t target_foreground_process) -{ - struct knote *kn_max = NULL; - struct knote *kn_cur = NULL, *kn_temp = NULL; /* for safe list traversal */ - pid_t target_pid = -1; - struct klist dispatch_klist = { NULL }; - proc_t target_proc = PROC_NULL; - struct task *task = NULL; - boolean_t found_candidate = FALSE; - - static vm_pressure_level_t level_snapshot = kVMPressureNormal; - static vm_pressure_level_t prev_level_snapshot = kVMPressureNormal; - boolean_t smoothing_window_started = FALSE; - struct timeval smoothing_window_start_tstamp = {0, 0}; - struct timeval curr_tstamp = {0, 0}; - int elapsed_msecs = 0; - uint64_t curr_ts = mach_absolute_time(); +memorystatus_update_vm_pressure(boolean_t target_foreground_process) +{ + struct knote *kn_max = NULL; + struct knote *kn_cur = NULL, *kn_temp = NULL; /* for safe list traversal */ + pid_t target_pid = -1; + struct klist dispatch_klist = { NULL }; + proc_t target_proc = PROC_NULL; + struct task *task = NULL; + boolean_t found_candidate = FALSE; + + static vm_pressure_level_t level_snapshot = kVMPressureNormal; + static vm_pressure_level_t prev_level_snapshot = kVMPressureNormal; + boolean_t smoothing_window_started = FALSE; + struct timeval smoothing_window_start_tstamp = {0, 0}; + struct timeval curr_tstamp = {0, 0}; + int elapsed_msecs = 0; + uint64_t curr_ts = mach_absolute_time(); #if !CONFIG_JETSAM -#define MAX_IDLE_KILLS 100 /* limit the number of idle kills allowed */ +#define MAX_IDLE_KILLS 100 /* limit the number of idle kills allowed */ - int idle_kill_counter = 0; + int idle_kill_counter = 0; /* * On desktop we take this opportunity to free up memory pressure @@ -6451,7 +8154,6 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) #endif /* !CONFIG_JETSAM */ if (level_snapshot != kVMPressureNormal) { - /* * Check to see if we are still in the 'resting' period * after having notified all clients interested in @@ -6461,22 +8163,28 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) level_snapshot = memorystatus_vm_pressure_level; if (level_snapshot == kVMPressureWarning || level_snapshot == kVMPressureUrgent) { + if (next_warning_notification_sent_at_ts) { + if (curr_ts < next_warning_notification_sent_at_ts) { + delay(INTER_NOTIFICATION_DELAY * 4 /* 1 sec */); + return KERN_SUCCESS; + } - if (curr_ts < next_warning_notification_sent_at_ts) { - delay(INTER_NOTIFICATION_DELAY * 4 /* 1 sec */); - return KERN_SUCCESS; + next_warning_notification_sent_at_ts = 0; + memorystatus_klist_reset_all_for_level(kVMPressureWarning); } } else if (level_snapshot == kVMPressureCritical) { - - if (curr_ts < next_critical_notification_sent_at_ts) { - delay(INTER_NOTIFICATION_DELAY * 4 /* 1 sec */); - return KERN_SUCCESS; + if (next_critical_notification_sent_at_ts) { + if (curr_ts < next_critical_notification_sent_at_ts) { + delay(INTER_NOTIFICATION_DELAY * 4 /* 1 sec */); + return KERN_SUCCESS; + } + next_critical_notification_sent_at_ts = 0; + memorystatus_klist_reset_all_for_level(kVMPressureCritical); } } } while (1) { - /* * There is a race window here. But it's not clear * how much we benefit from having extra synchronization. @@ -6489,7 +8197,6 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) * and see if this condition stays. */ if (smoothing_window_started == FALSE) { - smoothing_window_started = TRUE; microuptime(&smoothing_window_start_tstamp); } @@ -6499,7 +8206,6 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) elapsed_msecs = curr_tstamp.tv_sec * 1000 + curr_tstamp.tv_usec / 1000; if (elapsed_msecs < VM_PRESSURE_DECREASED_SMOOTHING_PERIOD) { - delay(INTER_NOTIFICATION_DELAY); continue; } @@ -6511,7 +8217,7 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) memorystatus_klist_lock(); kn_max = vm_pressure_select_optimal_candidate_to_notify(&memorystatus_klist, level_snapshot, target_foreground_process); - if (kn_max == NULL) { + if (kn_max == NULL) { memorystatus_klist_unlock(); /* @@ -6523,23 +8229,23 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) if (level_snapshot != kVMPressureNormal) { if (level_snapshot == kVMPressureWarning || level_snapshot == kVMPressureUrgent) { nanoseconds_to_absolutetime(WARNING_NOTIFICATION_RESTING_PERIOD * NSEC_PER_SEC, &curr_ts); - next_warning_notification_sent_at_ts = mach_absolute_time() + curr_ts; - memorystatus_klist_reset_all_for_level(kVMPressureWarning); + /* Next warning notification (if nothing changes) won't be sent before...*/ + next_warning_notification_sent_at_ts = mach_absolute_time() + curr_ts; } if (level_snapshot == kVMPressureCritical) { nanoseconds_to_absolutetime(CRITICAL_NOTIFICATION_RESTING_PERIOD * NSEC_PER_SEC, &curr_ts); - next_critical_notification_sent_at_ts = mach_absolute_time() + curr_ts; - memorystatus_klist_reset_all_for_level(kVMPressureCritical); + /* Next critical notification (if nothing changes) won't be sent before...*/ + next_critical_notification_sent_at_ts = mach_absolute_time() + curr_ts; } } return KERN_FAILURE; } - + target_proc = knote_get_kq(kn_max)->kq_p; - + proc_list_lock(); if (target_proc != proc_ref_locked(target_proc)) { target_proc = PROC_NULL; @@ -6548,21 +8254,18 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) continue; } proc_list_unlock(); - + target_pid = target_proc->p_pid; task = (struct task *)(target_proc->task); - - if (level_snapshot != kVMPressureNormal) { + if (level_snapshot != kVMPressureNormal) { if (level_snapshot == kVMPressureWarning || level_snapshot == kVMPressureUrgent) { - if (is_knote_registered_modify_task_pressure_bits(kn_max, NOTE_MEMORYSTATUS_PRESSURE_WARN, task, 0, kVMPressureWarning) == TRUE) { found_candidate = TRUE; } } else { if (level_snapshot == kVMPressureCritical) { - if (is_knote_registered_modify_task_pressure_bits(kn_max, NOTE_MEMORYSTATUS_PRESSURE_CRITICAL, task, 0, kVMPressureCritical) == TRUE) { found_candidate = TRUE; } @@ -6570,7 +8273,6 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) } } else { if (kn_max->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { - task_clear_has_been_notified(task, kVMPressureWarning); task_clear_has_been_notified(task, kVMPressureCritical); @@ -6585,7 +8287,6 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) } SLIST_FOREACH_SAFE(kn_cur, &memorystatus_klist, kn_selnext, kn_temp) { - int knote_pressure_level = convert_internal_pressure_level_to_dispatch_level(level_snapshot); if (is_knote_registered_modify_task_pressure_bits(kn_cur, knote_pressure_level, task, 0, level_snapshot) == TRUE) { @@ -6620,14 +8321,13 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) * No need for delays etc. */ } else { - uint32_t sleep_interval = INTER_NOTIFICATION_DELAY; #if CONFIG_JETSAM unsigned int page_delta = 0; unsigned int skip_delay_page_threshold = 0; assert(memorystatus_available_pages_pressure >= memorystatus_available_pages_critical_base); - + page_delta = (memorystatus_available_pages_pressure - memorystatus_available_pages_critical_base) / 2; skip_delay_page_threshold = memorystatus_available_pages_pressure - page_delta; @@ -6639,7 +8339,7 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) sleep_interval = 0; } #endif /* CONFIG_JETSAM */ - + if (sleep_interval) { delay(sleep_interval); } @@ -6652,31 +8352,30 @@ memorystatus_update_vm_pressure(boolean_t target_foreground_process) vm_pressure_level_t convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t internal_pressure_level) { - vm_pressure_level_t dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; - - switch (internal_pressure_level) { + vm_pressure_level_t dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; - case kVMPressureNormal: - { - dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; - break; - } + switch (internal_pressure_level) { + case kVMPressureNormal: + { + dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; + break; + } - case kVMPressureWarning: - case kVMPressureUrgent: - { - dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_WARN; - break; - } + case kVMPressureWarning: + case kVMPressureUrgent: + { + dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_WARN; + break; + } - case kVMPressureCritical: - { - dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; - break; - } + case kVMPressureCritical: + { + dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; + break; + } - default: - break; + default: + break; } return dispatch_level; @@ -6686,6 +8385,15 @@ static int sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS { #pragma unused(arg1, arg2, oidp) +#if CONFIG_EMBEDDED + int error = 0; + + error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0); + if (error) { + return error; + } + +#endif /* CONFIG_EMBEDDED */ vm_pressure_level_t dispatch_level = convert_internal_pressure_level_to_dispatch_level(memorystatus_vm_pressure_level); return SYSCTL_OUT(req, &dispatch_level, sizeof(dispatch_level)); @@ -6693,18 +8401,16 @@ sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS #if DEBUG || DEVELOPMENT -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_LOCKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, &sysctl_memorystatus_vm_pressure_level, "I", ""); #else /* DEBUG || DEVELOPMENT */ -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorystatus_vm_pressure_level, "I", ""); #endif /* DEBUG || DEVELOPMENT */ -extern int memorystatus_purge_on_warning; -extern int memorystatus_purge_on_critical; static int sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS @@ -6719,13 +8425,13 @@ sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS error = sysctl_handle_int(oidp, &level, 0, req); if (error || !req->newptr) { - return (error); + return error; } memorystatus_manual_testing_on = TRUE; trigger_request = (level >> 16) & 0xFFFF; - pressure_level = (level & 0xFFFF); + pressure_level = (level & 0xFFFF); if (trigger_request < TEST_LOW_MEMORY_TRIGGER_ONE || trigger_request > TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL) { @@ -6746,19 +8452,14 @@ sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS * So we translate those events to our internal levels here. */ if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { - memorystatus_manual_testing_level = kVMPressureNormal; force_purge = 0; - } else if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_WARN) { - memorystatus_manual_testing_level = kVMPressureWarning; - force_purge = memorystatus_purge_on_warning; - + force_purge = vm_pageout_state.memorystatus_purge_on_warning; } else if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) { - memorystatus_manual_testing_level = kVMPressureCritical; - force_purge = memorystatus_purge_on_critical; + force_purge = vm_pageout_state.memorystatus_purge_on_critical; } memorystatus_vm_pressure_level = memorystatus_manual_testing_level; @@ -6779,24 +8480,24 @@ sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS /* no purging requested */ break; } - while (vm_purgeable_object_purge_one_unlocked(force_purge)); + while (vm_purgeable_object_purge_one_unlocked(force_purge)) { + ; + } break; } if ((trigger_request == TEST_LOW_MEMORY_TRIGGER_ONE) || (trigger_request == TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE)) { - memorystatus_update_vm_pressure(TRUE); } if ((trigger_request == TEST_LOW_MEMORY_TRIGGER_ALL) || (trigger_request == TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL)) { - while (memorystatus_update_vm_pressure(FALSE) == KERN_SUCCESS) { continue; } } - + if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { memorystatus_manual_testing_on = FALSE; } @@ -6804,44 +8505,43 @@ sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS return 0; } -SYSCTL_PROC(_kern, OID_AUTO, memorypressure_manual_trigger, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorypressure_manual_trigger, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_memorypressure_manual_trigger, "I", ""); -extern int memorystatus_purge_on_warning; -extern int memorystatus_purge_on_urgent; -extern int memorystatus_purge_on_critical; - -SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_warning, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_purge_on_warning, 0, ""); -SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_urgent, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_purge_on_urgent, 0, ""); -SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_critical, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_purge_on_critical, 0, ""); +SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_warning, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.memorystatus_purge_on_warning, 0, ""); +SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_urgent, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.memorystatus_purge_on_urgent, 0, ""); +SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_critical, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.memorystatus_purge_on_critical, 0, ""); +#if DEBUG || DEVELOPMENT +SYSCTL_UINT(_kern, OID_AUTO, memorystatus_vm_pressure_events_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pressure_events_enabled, 0, ""); +#endif #endif /* VM_PRESSURE_EVENTS */ /* Return both allocated and actual size, since there's a race between allocation and list compilation */ static int -memorystatus_get_priority_list(memorystatus_priority_entry_t **list_ptr, size_t *buffer_size, size_t *list_size, boolean_t size_only) +memorystatus_get_priority_list(memorystatus_priority_entry_t **list_ptr, size_t *buffer_size, size_t *list_size, boolean_t size_only) { - uint32_t list_count, i = 0; + uint32_t list_count, i = 0; memorystatus_priority_entry_t *list_entry; proc_t p; - list_count = memorystatus_list_count; + list_count = memorystatus_list_count; *list_size = sizeof(memorystatus_priority_entry_t) * list_count; /* Just a size check? */ if (size_only) { return 0; } - + /* Otherwise, validate the size of the buffer */ if (*buffer_size < *list_size) { return EINVAL; } - *list_ptr = (memorystatus_priority_entry_t*)kalloc(*list_size); - if (!list_ptr) { + *list_ptr = (memorystatus_priority_entry_t*)kalloc(*list_size); + if (!*list_ptr) { return ENOMEM; } @@ -6860,75 +8560,112 @@ memorystatus_get_priority_list(memorystatus_priority_entry_t **list_ptr, size_t list_entry->priority = p->p_memstat_effectivepriority; list_entry->user_data = p->p_memstat_userdata; - /* - * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore. - * Background limits are described via the inactive limit slots. - * So, here, the cached limit should always be valid. - */ - if (p->p_memstat_memlimit <= 0) { - task_get_phys_footprint_limit(p->task, &list_entry->limit); - } else { - list_entry->limit = p->p_memstat_memlimit; - } + task_get_phys_footprint_limit(p->task, &list_entry->limit); + } else { + list_entry->limit = p->p_memstat_memlimit; + } list_entry->state = memorystatus_build_state(p); list_entry++; *list_size += sizeof(memorystatus_priority_entry_t); - + p = memorystatus_get_next_proc_locked(&i, p, TRUE); } - + proc_list_unlock(); - + MEMORYSTATUS_DEBUG(1, "memorystatus_get_priority_list: returning %lu for size\n", (unsigned long)*list_size); - + return 0; } static int -memorystatus_cmd_get_priority_list(user_addr_t buffer, size_t buffer_size, int32_t *retval) { - int error = EINVAL; +memorystatus_get_priority_pid(pid_t pid, user_addr_t buffer, size_t buffer_size) +{ + int error = 0; + memorystatus_priority_entry_t mp_entry; + + /* Validate inputs */ + if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_entry_t))) { + return EINVAL; + } + + proc_t p = proc_find(pid); + if (!p) { + return ESRCH; + } + + memset(&mp_entry, 0, sizeof(memorystatus_priority_entry_t)); + + mp_entry.pid = p->p_pid; + mp_entry.priority = p->p_memstat_effectivepriority; + mp_entry.user_data = p->p_memstat_userdata; + if (p->p_memstat_memlimit <= 0) { + task_get_phys_footprint_limit(p->task, &mp_entry.limit); + } else { + mp_entry.limit = p->p_memstat_memlimit; + } + mp_entry.state = memorystatus_build_state(p); + + proc_rele(p); + + error = copyout(&mp_entry, buffer, buffer_size); + + return error; +} + +static int +memorystatus_cmd_get_priority_list(pid_t pid, user_addr_t buffer, size_t buffer_size, int32_t *retval) +{ + int error = 0; boolean_t size_only; - memorystatus_priority_entry_t *list = NULL; size_t list_size; - + + /* + * When a non-zero pid is provided, the 'list' has only one entry. + */ + size_only = ((buffer == USER_ADDR_NULL) ? TRUE: FALSE); - - error = memorystatus_get_priority_list(&list, &buffer_size, &list_size, size_only); - if (error) { - goto out; - } - if (!size_only) { - error = copyout(list, buffer, list_size); + if (pid != 0) { + list_size = sizeof(memorystatus_priority_entry_t) * 1; + if (!size_only) { + error = memorystatus_get_priority_pid(pid, buffer, buffer_size); + } + } else { + memorystatus_priority_entry_t *list = NULL; + error = memorystatus_get_priority_list(&list, &buffer_size, &list_size, size_only); + + if (error == 0) { + if (!size_only) { + error = copyout(list, buffer, list_size); + } + } + + if (list) { + kfree(list, buffer_size); + } } - + if (error == 0) { *retval = list_size; } -out: - - if (list) { - kfree(list, buffer_size); - } return error; } -#if CONFIG_JETSAM - -static void +static void memorystatus_clear_errors(void) { proc_t p; unsigned int i = 0; KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_START, 0, 0, 0, 0, 0); - + proc_list_lock(); - + p = memorystatus_get_first_proc_locked(&i, TRUE); while (p) { if (p->p_memstat_state & P_MEMSTAT_ERROR) { @@ -6936,15 +8673,16 @@ memorystatus_clear_errors(void) } p = memorystatus_get_next_proc_locked(&i, p, TRUE); } - + proc_list_unlock(); KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_END, 0, 0, 0, 0, 0); } +#if CONFIG_JETSAM static void -memorystatus_update_levels_locked(boolean_t critical_only) { - +memorystatus_update_levels_locked(boolean_t critical_only) +{ memorystatus_available_pages_critical = memorystatus_available_pages_critical_base; /* @@ -6955,8 +8693,8 @@ memorystatus_update_levels_locked(boolean_t critical_only) { if (first_bucket->count) { memorystatus_available_pages_critical += memorystatus_available_pages_critical_idle_offset; - if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure ) { - /* + if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure) { + /* * The critical threshold must never exceed the pressure threshold */ memorystatus_available_pages_critical = memorystatus_available_pages_pressure; @@ -6967,14 +8705,14 @@ memorystatus_update_levels_locked(boolean_t critical_only) { if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) { memorystatus_available_pages_critical += memorystatus_jetsam_policy_offset_pages_diagnostic; - if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure ) { - /* + if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure) { + /* * The critical threshold must never exceed the pressure threshold */ memorystatus_available_pages_critical = memorystatus_available_pages_pressure; } } -#endif +#endif /* DEBUG || DEVELOPMENT */ if (memorystatus_jetsam_policy & kPolicyMoreFree) { memorystatus_available_pages_critical += memorystatus_policy_more_free_offset_pages; @@ -6983,7 +8721,7 @@ memorystatus_update_levels_locked(boolean_t critical_only) { if (critical_only) { return; } - + #if VM_PRESSURE_EVENTS memorystatus_available_pages_pressure = (pressure_threshold_percentage / delta_percentage) * memorystatus_delta; #if DEBUG || DEVELOPMENT @@ -6994,6 +8732,36 @@ memorystatus_update_levels_locked(boolean_t critical_only) { #endif } +void +memorystatus_fast_jetsam_override(boolean_t enable_override) +{ + /* If fast jetsam is not enabled, simply return */ + if (!fast_jetsam_enabled) { + return; + } + + if (enable_override) { + if ((memorystatus_jetsam_policy & kPolicyMoreFree) == kPolicyMoreFree) { + return; + } + proc_list_lock(); + memorystatus_jetsam_policy |= kPolicyMoreFree; + memorystatus_thread_pool_max(); + memorystatus_update_levels_locked(TRUE); + proc_list_unlock(); + } else { + if ((memorystatus_jetsam_policy & kPolicyMoreFree) == 0) { + return; + } + proc_list_lock(); + memorystatus_jetsam_policy &= ~kPolicyMoreFree; + memorystatus_thread_pool_default(); + memorystatus_update_levels_locked(TRUE); + proc_list_unlock(); + } +} + + static int sysctl_kern_memorystatus_policy_more_free SYSCTL_HANDLER_ARGS { @@ -7009,40 +8777,29 @@ sysctl_kern_memorystatus_policy_more_free SYSCTL_HANDLER_ARGS */ error = sysctl_handle_int(oidp, &more_free, 0, req); - if (error || !req->newptr) - return (error); - - if ((more_free && ((memorystatus_jetsam_policy & kPolicyMoreFree) == kPolicyMoreFree)) || - (!more_free && ((memorystatus_jetsam_policy & kPolicyMoreFree) == 0))) { - - /* - * No change in state. - */ - return 0; + if (error || !req->newptr) { + return error; } - proc_list_lock(); - if (more_free) { - memorystatus_jetsam_policy |= kPolicyMoreFree; + memorystatus_fast_jetsam_override(true); } else { - memorystatus_jetsam_policy &= ~kPolicyMoreFree; + memorystatus_fast_jetsam_override(false); } - memorystatus_update_levels_locked(TRUE); - - proc_list_unlock(); - return 0; } -SYSCTL_PROC(_kern, OID_AUTO, memorystatus_policy_more_free, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED, +SYSCTL_PROC(_kern, OID_AUTO, memorystatus_policy_more_free, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_kern_memorystatus_policy_more_free, "I", ""); +#endif /* CONFIG_JETSAM */ + /* * Get the at_boot snapshot */ static int -memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) { +memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) +{ size_t input_size = *snapshot_size; /* @@ -7068,15 +8825,46 @@ memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, siz *snapshot = &memorystatus_at_boot_snapshot; MEMORYSTATUS_DEBUG(7, "memorystatus_get_at_boot_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%d)\n", - (long)input_size, (long)*snapshot_size, 0); + (long)input_size, (long)*snapshot_size, 0); + return 0; +} + +/* + * Get the previous fully populated snapshot + */ +static int +memorystatus_get_jetsam_snapshot_copy(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) +{ + size_t input_size = *snapshot_size; + + if (memorystatus_jetsam_snapshot_copy_count > 0) { + *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_copy_count)); + } else { + *snapshot_size = 0; + } + + if (size_only) { + return 0; + } + + if (input_size < *snapshot_size) { + return EINVAL; + } + + *snapshot = memorystatus_jetsam_snapshot_copy; + + MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot_copy: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", + (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_copy_count); + return 0; } static int -memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) { +memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) +{ size_t input_size = *snapshot_size; uint32_t ods_list_count = memorystatus_list_count; - memorystatus_jetsam_snapshot_t *ods = NULL; /* The on_demand snapshot buffer */ + memorystatus_jetsam_snapshot_t *ods = NULL; /* The on_demand snapshot buffer */ *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (ods_list_count)); @@ -7099,7 +8887,7 @@ memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, s */ ods = (memorystatus_jetsam_snapshot_t *)kalloc(*snapshot_size); if (!ods) { - return (ENOMEM); + return ENOMEM; } memset(ods, 0, *snapshot_size); @@ -7117,13 +8905,14 @@ memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, s *snapshot = ods; MEMORYSTATUS_DEBUG(7, "memorystatus_get_on_demand_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", - (long)input_size, (long)*snapshot_size, (long)ods_list_count); - + (long)input_size, (long)*snapshot_size, (long)ods_list_count); + return 0; } static int -memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) { +memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) +{ size_t input_size = *snapshot_size; if (memorystatus_jetsam_snapshot_count > 0) { @@ -7143,14 +8932,15 @@ memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size *snapshot = memorystatus_jetsam_snapshot; MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", - (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_count); + (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_count); return 0; } static int -memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) { +memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) +{ int error = EINVAL; boolean_t size_only; boolean_t is_default_snapshot = FALSE; @@ -7165,17 +8955,16 @@ memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t b is_default_snapshot = TRUE; error = memorystatus_get_jetsam_snapshot(&snapshot, &buffer_size, size_only); } else { - if (flags & ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT)) { + if (flags & ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT | MEMORYSTATUS_SNAPSHOT_COPY)) { /* * Unsupported bit set in flag. */ return EINVAL; } - if ((flags & (MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT)) == - (MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT)) { + if (flags & (flags - 0x1)) { /* - * Can't have both set at the same time. + * Can't have multiple flags set at the same time. */ return EINVAL; } @@ -7187,10 +8976,11 @@ memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t b * an on_demand snapshot buffer, which is freed below. */ error = memorystatus_get_on_demand_snapshot(&snapshot, &buffer_size, size_only); - } else if (flags & MEMORYSTATUS_SNAPSHOT_AT_BOOT) { is_at_boot_snapshot = TRUE; error = memorystatus_get_at_boot_snapshot(&snapshot, &buffer_size, size_only); + } else if (flags & MEMORYSTATUS_SNAPSHOT_COPY) { + error = memorystatus_get_jetsam_snapshot_copy(&snapshot, &buffer_size, size_only); } else { /* * Invalid flag setting. @@ -7211,14 +9001,20 @@ memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t b * clearing the buffer means, free it. * If working with the at_boot snapshot * there is nothing to clear or update. + * If working with a copy of the snapshot + * there is nothing to clear or update. */ if (!size_only) { if ((error = copyout(snapshot, buffer, buffer_size)) == 0) { if (is_default_snapshot) { /* * The jetsam snapshot is never freed, its count is simply reset. + * However, we make a copy for any parties that might be interested + * in the previous fully populated snapshot. */ proc_list_lock(); + memcpy(memorystatus_jetsam_snapshot_copy, memorystatus_jetsam_snapshot, memorystatus_jetsam_snapshot_size); + memorystatus_jetsam_snapshot_copy_count = memorystatus_jetsam_snapshot_count; snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; memorystatus_jetsam_snapshot_last_timestamp = 0; proc_list_unlock(); @@ -7230,7 +9026,7 @@ memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t b * The on_demand snapshot is always freed, * even if the copyout failed. */ - if(snapshot) { + if (snapshot) { kfree(snapshot, buffer_size); } } @@ -7244,10 +9040,9 @@ out: } /* - * Routine: memorystatus_cmd_grp_set_properties - * Purpose: Update properties for a group of processes. + * Routine: memorystatus_cmd_grp_set_priorities + * Purpose: Update priorities for a group of processes. * - * Supported Properties: * [priority] * Move each process out of its effective priority * band and into a new priority band. @@ -7259,7 +9054,7 @@ out: * [17 | p55, p67, p19 ] * [12 | p103 p10 ] * [ 7 | p25 ] - * [ 0 | p71, p82, ] + * [ 0 | p71, p82, ] * * after [ new band | pid] * [ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101] @@ -7279,29 +9074,24 @@ out: */ -/* This internal structure can expand when we add support for more properties */ -typedef struct memorystatus_internal_properties -{ - proc_t proc; - int32_t priority; /* see memorytstatus_priority_entry_t : priority */ -} memorystatus_internal_properties_t; - - static int -memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) { - -#pragma unused (flags) - +memorystatus_cmd_grp_set_priorities(user_addr_t buffer, size_t buffer_size) +{ /* * We only handle setting priority * per process */ int error = 0; - memorystatus_priority_entry_t *entries = NULL; + memorystatus_properties_entry_v1_t *entries = NULL; uint32_t entry_count = 0; /* This will be the ordered proc list */ + typedef struct memorystatus_internal_properties { + proc_t proc; + int32_t priority; + } memorystatus_internal_properties_t; + memorystatus_internal_properties_t *table = NULL; size_t table_size = 0; uint32_t table_count = 0; @@ -7310,29 +9100,39 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu uint32_t bucket_index = 0; boolean_t head_insert; int32_t new_priority; - + proc_t p; /* Verify inputs */ - if ((buffer == USER_ADDR_NULL) || (buffer_size == 0) || ((buffer_size % sizeof(memorystatus_priority_entry_t)) != 0)) { + if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) { error = EINVAL; goto out; } - entry_count = (buffer_size / sizeof(memorystatus_priority_entry_t)); - if ((entries = (memorystatus_priority_entry_t *)kalloc(buffer_size)) == NULL) { + entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t)); + if ((entries = (memorystatus_properties_entry_v1_t *)kalloc(buffer_size)) == NULL) { error = ENOMEM; goto out; } - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_START, entry_count, 0, 0, 0, 0); + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_START, MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY, entry_count, 0, 0, 0); if ((error = copyin(buffer, entries, buffer_size)) != 0) { goto out; } /* Verify sanity of input priorities */ - for (i=0; i < entry_count; i++) { + if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) { + if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) { + error = EINVAL; + goto out; + } + } else { + error = EINVAL; + goto out; + } + + for (i = 0; i < entry_count; i++) { if (entries[i].priority == -1) { /* Use as shorthand for default priority */ entries[i].priority = JETSAM_PRIORITY_DEFAULT; @@ -7340,7 +9140,7 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu /* Both the aging bands are reserved for internal use; * if requested, adjust to JETSAM_PRIORITY_IDLE. */ entries[i].priority = JETSAM_PRIORITY_IDLE; - } else if (entries[i].priority == JETSAM_PRIORITY_IDLE_HEAD) { + } else if (entries[i].priority == JETSAM_PRIORITY_IDLE_HEAD) { /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle * queue */ /* Deal with this later */ @@ -7352,7 +9152,7 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu } table_size = sizeof(memorystatus_internal_properties_t) * entry_count; - if ( (table = (memorystatus_internal_properties_t *)kalloc(table_size)) == NULL) { + if ((table = (memorystatus_internal_properties_t *)kalloc(table_size)) == NULL) { error = ENOMEM; goto out; } @@ -7367,14 +9167,14 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu * to highest priority. */ - bucket_index=0; - + bucket_index = 0; + proc_list_lock(); /* Create the ordered table */ - p = memorystatus_get_first_proc_locked(&bucket_index, TRUE); + p = memorystatus_get_first_proc_locked(&bucket_index, TRUE); while (p && (table_count < entry_count)) { - for (i=0; i < entry_count; i++ ) { + for (i = 0; i < entry_count; i++) { if (p->p_pid == entries[i].pid) { /* Build the table data */ table[table_count].proc = p; @@ -7385,9 +9185,9 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu } p = memorystatus_get_next_proc_locked(&bucket_index, p, TRUE); } - + /* We now have ordered list of procs ready to move */ - for (i=0; i < table_count; i++) { + for (i = 0; i < table_count; i++) { p = table[i].proc; assert(p != NULL); @@ -7399,7 +9199,7 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu new_priority = table[i].priority; head_insert = false; } - + /* Not allowed */ if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { continue; @@ -7423,26 +9223,141 @@ memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t bu * then some pids were not found in a jetsam band. * harmless but interesting... */ - KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_END, entry_count, table_count, 0, 0, 0); - out: - if (entries) + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_END, MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY, entry_count, table_count, 0, 0); + + if (entries) { kfree(entries, buffer_size); - if (table) + } + if (table) { kfree(table, table_size); + } + + return error; +} + +static int +memorystatus_cmd_grp_set_probabilities(user_addr_t buffer, size_t buffer_size) +{ + int error = 0; + memorystatus_properties_entry_v1_t *entries = NULL; + uint32_t entry_count = 0, i = 0; + memorystatus_internal_probabilities_t *tmp_table_new = NULL, *tmp_table_old = NULL; + size_t tmp_table_new_size = 0, tmp_table_old_size = 0; + + /* Verify inputs */ + if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) { + error = EINVAL; + goto out; + } + + entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t)); + + if ((entries = (memorystatus_properties_entry_v1_t *) kalloc(buffer_size)) == NULL) { + error = ENOMEM; + goto out; + } + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_START, MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY, entry_count, 0, 0, 0); + + if ((error = copyin(buffer, entries, buffer_size)) != 0) { + goto out; + } + + if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) { + if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) { + error = EINVAL; + goto out; + } + } else { + error = EINVAL; + goto out; + } + + /* Verify sanity of input priorities */ + for (i = 0; i < entry_count; i++) { + /* + * 0 - low probability of use. + * 1 - high probability of use. + * + * Keeping this field an int (& not a bool) to allow + * us to experiment with different values/approaches + * later on. + */ + if (entries[i].use_probability > 1) { + error = EINVAL; + goto out; + } + } + + tmp_table_new_size = sizeof(memorystatus_internal_probabilities_t) * entry_count; + + if ((tmp_table_new = (memorystatus_internal_probabilities_t *) kalloc(tmp_table_new_size)) == NULL) { + error = ENOMEM; + goto out; + } + memset(tmp_table_new, 0, tmp_table_new_size); + + proc_list_lock(); + + if (memorystatus_global_probabilities_table) { + tmp_table_old = memorystatus_global_probabilities_table; + tmp_table_old_size = memorystatus_global_probabilities_size; + } + + memorystatus_global_probabilities_table = tmp_table_new; + memorystatus_global_probabilities_size = tmp_table_new_size; + tmp_table_new = NULL; + + for (i = 0; i < entry_count; i++) { + /* Build the table data */ + strlcpy(memorystatus_global_probabilities_table[i].proc_name, entries[i].proc_name, MAXCOMLEN + 1); + memorystatus_global_probabilities_table[i].use_probability = entries[i].use_probability; + } + + proc_list_unlock(); + +out: + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_END, MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY, entry_count, tmp_table_new_size, 0, 0); - return (error); + if (entries) { + kfree(entries, buffer_size); + entries = NULL; + } + + if (tmp_table_old) { + kfree(tmp_table_old, tmp_table_old_size); + tmp_table_old = NULL; + } + + return error; } +static int +memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) +{ + int error = 0; + + if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) == MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) { + error = memorystatus_cmd_grp_set_priorities(buffer, buffer_size); + } else if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) == MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) { + error = memorystatus_cmd_grp_set_probabilities(buffer, buffer_size); + } else { + error = EINVAL; + } + + return error; +} /* * This routine is used to update a process's jetsam priority position and stored user_data. * It is not used for the setting of memory limits, which is why the last 6 args to the * memorystatus_update() call are 0 or FALSE. */ - + static int -memorystatus_cmd_set_priority_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) { +memorystatus_cmd_set_priority_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) +{ int error = 0; memorystatus_priority_properties_t mpp_entry; @@ -7450,31 +9365,32 @@ memorystatus_cmd_set_priority_properties(pid_t pid, user_addr_t buffer, size_t b if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_properties_t))) { return EINVAL; } - + error = copyin(buffer, &mpp_entry, buffer_size); if (error == 0) { proc_t p; - + p = proc_find(pid); if (!p) { return ESRCH; } - + if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { proc_rele(p); return EPERM; } - - error = memorystatus_update(p, mpp_entry.priority, mpp_entry.user_data, FALSE, FALSE, 0, 0, FALSE, FALSE, FALSE); + + error = memorystatus_update(p, mpp_entry.priority, mpp_entry.user_data, FALSE, FALSE, 0, 0, FALSE, FALSE); proc_rele(p); } - - return(error); + + return error; } static int -memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) { +memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) +{ int error = 0; memorystatus_memlimit_properties_t mmp_entry; @@ -7489,7 +9405,7 @@ memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t b error = memorystatus_set_memlimit_properties(pid, &mmp_entry); } - return(error); + return error; } /* @@ -7500,7 +9416,8 @@ memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t b * to the task's ledgers via task_set_phys_footprint_limit(). */ static int -memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) { +memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) +{ int error = 0; memorystatus_memlimit_properties_t mmp_entry; @@ -7509,7 +9426,7 @@ memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t b return EINVAL; } - memset (&mmp_entry, 0, sizeof(memorystatus_memlimit_properties_t)); + memset(&mmp_entry, 0, sizeof(memorystatus_memlimit_properties_t)); proc_t p = proc_find(pid); if (!p) { @@ -7521,7 +9438,7 @@ memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t b * No locks taken since we hold a reference to the proc. */ - if (p->p_memstat_memlimit_active > 0 ) { + if (p->p_memstat_memlimit_active > 0) { mmp_entry.memlimit_active = p->p_memstat_memlimit_active; } else { task_convert_phys_footprint_limit(-1, &mmp_entry.memlimit_active); @@ -7546,7 +9463,7 @@ memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t b error = copyout(&mmp_entry, buffer, buffer_size); - return(error); + return error; } @@ -7558,7 +9475,8 @@ memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t b * The delta is returned as bytes in excess or zero. */ static int -memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) { +memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) +{ int error = 0; uint64_t footprint_in_bytes = 0; uint64_t delta_in_bytes = 0; @@ -7567,7 +9485,7 @@ memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t b /* Validate inputs */ if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(uint64_t)) || (flags != 0)) { - return EINVAL; + return EINVAL; } proc_t p = proc_find(pid); @@ -7590,7 +9508,7 @@ memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t b proc_rele(p); - memlimit_bytes = memlimit_mb * 1024 * 1024; /* MB to bytes */ + memlimit_bytes = memlimit_mb * 1024 * 1024; /* MB to bytes */ /* * Computed delta always returns >= 0 bytes @@ -7601,28 +9519,30 @@ memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t b error = copyout(&delta_in_bytes, buffer, sizeof(delta_in_bytes)); - return(error); + return error; } static int -memorystatus_cmd_get_pressure_status(int32_t *retval) { +memorystatus_cmd_get_pressure_status(int32_t *retval) +{ int error; - + /* Need privilege for check */ error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0); if (error) { - return (error); + return error; } - + /* Inherently racy, so it's not worth taking a lock here */ *retval = (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0; - + return error; } int -memorystatus_get_pressure_status_kdp() { +memorystatus_get_pressure_status_kdp() +{ return (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0; } @@ -7643,8 +9563,10 @@ memorystatus_get_pressure_status_kdp() { * - so mapping is (active/fatal, inactive/fatal) */ +#if CONFIG_JETSAM 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) { +memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit) +{ int error = 0; memorystatus_memlimit_properties_t entry; @@ -7659,19 +9581,20 @@ memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __u } error = memorystatus_set_memlimit_properties(pid, &entry); - return (error); + return error; } +#endif /* CONFIG_JETSAM */ static int -memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry) { - +memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry) +{ int32_t memlimit_active; boolean_t memlimit_active_is_fatal; int32_t memlimit_inactive; boolean_t memlimit_inactive_is_fatal; uint32_t valid_attrs = 0; int error = 0; - + proc_t p = proc_find(pid); if (!p) { return ESRCH; @@ -7754,10 +9677,6 @@ memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties if (memorystatus_highwater_enabled) { boolean_t is_fatal; boolean_t use_active; - /* - * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore. - * Background limits are described via the inactive limit slots. - */ if (proc_jetsam_state_is_active_locked(p) == TRUE) { CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); @@ -7771,15 +9690,15 @@ memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties error = (task_set_phys_footprint_limit_internal(p->task, ((p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1), NULL, use_active, is_fatal) == 0) ? 0 : EINVAL; MEMORYSTATUS_DEBUG(3, "memorystatus_set_memlimit_properties: new limit on pid %d (%dMB %s) current priority (%d) dirty_state?=0x%x %s\n", - p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), - (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, - (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); + p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), + (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, + (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); DTRACE_MEMORYSTATUS2(memorystatus_set_memlimit, proc_t, p, int32_t, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1)); } proc_list_unlock(); proc_rele(p); - + return error; } @@ -7800,11 +9719,132 @@ proc_get_memstat_priority(proc_t p, boolean_t effective_priority) return 0; } -#endif /* CONFIG_JETSAM */ +static int +memorystatus_get_process_is_managed(pid_t pid, int *is_managed) +{ + proc_t p = NULL; + + /* Validate inputs */ + if (pid == 0) { + return EINVAL; + } + + p = proc_find(pid); + if (!p) { + return ESRCH; + } + + proc_list_lock(); + *is_managed = ((p->p_memstat_state & P_MEMSTAT_MANAGED) ? 1 : 0); + proc_rele_locked(p); + proc_list_unlock(); + + return 0; +} + +static int +memorystatus_set_process_is_managed(pid_t pid, boolean_t set_managed) +{ + proc_t p = NULL; + + /* Validate inputs */ + if (pid == 0) { + return EINVAL; + } + + p = proc_find(pid); + if (!p) { + return ESRCH; + } + + proc_list_lock(); + if (set_managed == TRUE) { + p->p_memstat_state |= P_MEMSTAT_MANAGED; + } else { + p->p_memstat_state &= ~P_MEMSTAT_MANAGED; + } + proc_rele_locked(p); + proc_list_unlock(); + + return 0; +} + +static int +memorystatus_get_process_is_freezable(pid_t pid, int *is_freezable) +{ + proc_t p = PROC_NULL; + + if (pid == 0) { + return EINVAL; + } + + p = proc_find(pid); + if (!p) { + return ESRCH; + } + + /* + * Only allow this on the current proc for now. + * We can check for privileges and allow targeting another process in the future. + */ + if (p != current_proc()) { + proc_rele(p); + return EPERM; + } + + proc_list_lock(); + *is_freezable = ((p->p_memstat_state & P_MEMSTAT_FREEZE_DISABLED) ? 0 : 1); + proc_rele_locked(p); + proc_list_unlock(); + + return 0; +} + +static int +memorystatus_set_process_is_freezable(pid_t pid, boolean_t is_freezable) +{ + proc_t p = PROC_NULL; + + if (pid == 0) { + return EINVAL; + } + + p = proc_find(pid); + if (!p) { + return ESRCH; + } + + /* + * Only allow this on the current proc for now. + * We can check for privileges and allow targeting another process in the future. + */ + if (p != current_proc()) { + proc_rele(p); + return EPERM; + } + + proc_list_lock(); + if (is_freezable == FALSE) { + /* Freeze preference set to FALSE. Set the P_MEMSTAT_FREEZE_DISABLED bit. */ + p->p_memstat_state |= P_MEMSTAT_FREEZE_DISABLED; + printf("memorystatus_set_process_is_freezable: disabling freeze for pid %d [%s]\n", + p->p_pid, (*p->p_name ? p->p_name : "unknown")); + } else { + p->p_memstat_state &= ~P_MEMSTAT_FREEZE_DISABLED; + printf("memorystatus_set_process_is_freezable: enabling freeze for pid %d [%s]\n", + p->p_pid, (*p->p_name ? p->p_name : "unknown")); + } + proc_rele_locked(p); + proc_list_unlock(); + + return 0; +} int -memorystatus_control(struct proc *p __unused, struct memorystatus_control_args *args, int *ret) { +memorystatus_control(struct proc *p __unused, struct memorystatus_control_args *args, int *ret) +{ int error = EINVAL; + boolean_t skip_auth_check = FALSE; os_reason_t jetsam_reason = OS_REASON_NULL; #if !CONFIG_JETSAM @@ -7812,8 +9852,13 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args * #pragma unused(jetsam_reason) #endif - /* Need to be root or have entitlement */ - if (!kauth_cred_issuser(kauth_cred_get()) && !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT)) { + /* We don't need entitlements if we're setting/ querying the freeze preference for a process. Skip the check below. */ + if (args->command == MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE || args->command == MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE) { + skip_auth_check = TRUE; + } + + /* Need to be root or have entitlement. */ + if (!kauth_cred_issuser(kauth_cred_get()) && !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT) && !skip_auth_check) { error = EPERM; goto out; } @@ -7831,9 +9876,8 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args * switch (args->command) { case MEMORYSTATUS_CMD_GET_PRIORITY_LIST: - error = memorystatus_cmd_get_priority_list(args->buffer, args->buffersize, ret); + error = memorystatus_cmd_get_priority_list(args->pid, args->buffer, args->buffersize, ret); break; -#if CONFIG_JETSAM case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES: error = memorystatus_cmd_set_priority_properties(args->pid, args->buffer, args->buffersize, ret); break; @@ -7848,13 +9892,14 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args * break; case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES: error = memorystatus_cmd_grp_set_properties((int32_t)args->flags, args->buffer, args->buffersize, ret); - break; + break; case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT: error = memorystatus_cmd_get_jetsam_snapshot((int32_t)args->flags, args->buffer, args->buffersize, ret); break; case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS: error = memorystatus_cmd_get_pressure_status(ret); break; +#if CONFIG_JETSAM case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK: /* * This call does not distinguish between active and inactive limits. @@ -7871,7 +9916,8 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args * */ error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, TRUE); break; - /* Test commands */ +#endif /* CONFIG_JETSAM */ + /* Test commands */ #if DEVELOPMENT || DEBUG case MEMORYSTATUS_CMD_TEST_JETSAM: jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_GENERIC); @@ -7884,9 +9930,11 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args * case MEMORYSTATUS_CMD_TEST_JETSAM_SORT: error = memorystatus_cmd_test_jetsam_sort(args->pid, (int32_t)args->flags); break; +#if CONFIG_JETSAM case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS: error = memorystatus_cmd_set_panic_bits(args->buffer, args->buffersize); break; +#endif /* CONFIG_JETSAM */ #else /* DEVELOPMENT || DEBUG */ #pragma unused(jetsam_reason) #endif /* DEVELOPMENT || DEBUG */ @@ -7909,18 +9957,38 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args * memorystatus_aggressive_jetsam_lenient = FALSE; error = 0; break; -#endif /* CONFIG_JETSAM */ case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE: case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE: error = memorystatus_low_mem_privileged_listener(args->command); break; -#if CONFIG_JETSAM case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE: case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE: - error = memorystatus_update_inactive_jetsam_priority_band(args->pid, args->command, args->flags ? TRUE : FALSE); + error = memorystatus_update_inactive_jetsam_priority_band(args->pid, args->command, JETSAM_PRIORITY_ELEVATED_INACTIVE, args->flags ? TRUE : FALSE); + break; + case MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED: + error = memorystatus_set_process_is_managed(args->pid, args->flags); + break; + + case MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED: + error = memorystatus_get_process_is_managed(args->pid, ret); break; -#endif /* CONFIG_JETSAM */ + + case MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE: + error = memorystatus_set_process_is_freezable(args->pid, args->flags ? TRUE : FALSE); + break; + + case MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE: + error = memorystatus_get_process_is_freezable(args->pid, ret); + break; + +#if CONFIG_FREEZE +#if DEVELOPMENT || DEBUG + case MEMORYSTATUS_CMD_FREEZER_CONTROL: + error = memorystatus_freezer_control(args->flags, args->buffer, args->buffersize, ret); + break; +#endif /* DEVELOPMENT || DEBUG */ +#endif /* CONFIG_FREEZE */ default: break; @@ -7932,8 +10000,8 @@ out: static int -filt_memorystatusattach(struct knote *kn) -{ +filt_memorystatusattach(struct knote *kn, __unused struct kevent_internal_s *kev) +{ int error; kn->kn_flags |= EV_CLEAR; @@ -7967,7 +10035,6 @@ filt_memorystatus(struct knote *kn __unused, long hint) kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN; } } else if (memorystatus_vm_pressure_level == kVMPressureCritical) { - if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) { kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; } @@ -7981,15 +10048,15 @@ filt_memorystatus(struct knote *kn __unused, long hint) case kMemorystatusProcLimitWarn: if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { - kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; - } - break; + kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; + } + break; case kMemorystatusProcLimitCritical: if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { - kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; - } - break; + kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; + } + break; default: break; @@ -8002,11 +10069,11 @@ filt_memorystatus(struct knote *kn __unused, long hint) pid_t knote_pid = knote_proc->p_pid; printf("filt_memorystatus: sending kn 0x%lx (event 0x%x) for pid (%d)\n", - (unsigned long)kn, kn->kn_fflags, knote_pid); + (unsigned long)kn, kn->kn_fflags, knote_pid); } #endif - return (kn->kn_fflags != 0); + return kn->kn_fflags != 0; } static int @@ -8025,6 +10092,7 @@ filt_memorystatustouch(struct knote *kn, struct kevent_internal_s *kev) prev_kn_sfflags = kn->kn_sfflags; kn->kn_sfflags = (kev->fflags & EVFILT_MEMORYSTATUS_ALL_MASK); +#if !CONFIG_EMBEDDED /* * Only on desktop do we restrict notifications to * one per active/inactive state (soft limits only). @@ -8078,9 +10146,7 @@ filt_memorystatustouch(struct knote *kn, struct kevent_internal_s *kev) kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE; } } - - if ((kn->kn_status & KN_UDATA_SPECIFIC) == 0) - kn->kn_udata = kev->udata; +#endif /* !CONFIG_EMBEDDED */ /* * reset the output flags based on a @@ -8116,23 +10182,27 @@ filt_memorystatusprocess(struct knote *kn, struct filt_process_s *data, struct k } static void -memorystatus_klist_lock(void) { +memorystatus_klist_lock(void) +{ lck_mtx_lock(&memorystatus_klist_mutex); } static void -memorystatus_klist_unlock(void) { +memorystatus_klist_unlock(void) +{ lck_mtx_unlock(&memorystatus_klist_mutex); } -void -memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr) { +void +memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr) +{ lck_mtx_init(&memorystatus_klist_mutex, grp, attr); klist_init(&memorystatus_klist); } int -memorystatus_knote_register(struct knote *kn) { +memorystatus_knote_register(struct knote *kn) +{ int error = 0; memorystatus_klist_lock(); @@ -8140,8 +10210,8 @@ memorystatus_knote_register(struct knote *kn) { /* * Support only userspace visible flags. */ - if ((kn->kn_sfflags & EVFILT_MEMORYSTATUS_ALL_MASK) == kn->kn_sfflags) { - + if ((kn->kn_sfflags & EVFILT_MEMORYSTATUS_ALL_MASK) == (unsigned int) kn->kn_sfflags) { +#if !CONFIG_EMBEDDED if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; @@ -8151,20 +10221,21 @@ memorystatus_knote_register(struct knote *kn) { kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE; kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE; } +#endif /* !CONFIG_EMBEDDED */ KNOTE_ATTACH(&memorystatus_klist, kn); - - } else { + } else { error = ENOTSUP; } - + memorystatus_klist_unlock(); - + return error; } void -memorystatus_knote_unregister(struct knote *kn __unused) { +memorystatus_knote_unregister(struct knote *kn __unused) +{ memorystatus_klist_lock(); KNOTE_DETACH(&memorystatus_klist, kn); memorystatus_klist_unlock(); @@ -8174,7 +10245,8 @@ memorystatus_knote_unregister(struct knote *kn __unused) { #if 0 #if CONFIG_JETSAM && VM_PRESSURE_EVENTS static boolean_t -memorystatus_issue_pressure_kevent(boolean_t pressured) { +memorystatus_issue_pressure_kevent(boolean_t pressured) +{ memorystatus_klist_lock(); KNOTE(&memorystatus_klist, pressured ? kMemorystatusPressure : kMemorystatusNoPressure); memorystatus_klist_unlock(); @@ -8183,30 +10255,30 @@ memorystatus_issue_pressure_kevent(boolean_t pressured) { #endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */ #endif /* 0 */ -#if CONFIG_JETSAM /* Coalition support */ /* sorting info for a particular priority bucket */ typedef struct memstat_sort_info { - coalition_t msi_coal; - uint64_t msi_page_count; - pid_t msi_pid; - int msi_ntasks; + coalition_t msi_coal; + uint64_t msi_page_count; + pid_t msi_pid; + int msi_ntasks; } memstat_sort_info_t; -/* +/* * qsort from smallest page count to largest page count * * return < 0 for a < b * 0 for a == b * > 0 for a > b */ -static int memstat_asc_cmp(const void *a, const void *b) +static int +memstat_asc_cmp(const void *a, const void *b) { - const memstat_sort_info_t *msA = (const memstat_sort_info_t *)a; - const memstat_sort_info_t *msB = (const memstat_sort_info_t *)b; + const memstat_sort_info_t *msA = (const memstat_sort_info_t *)a; + const memstat_sort_info_t *msB = (const memstat_sort_info_t *)b; - return (int)((uint64_t)msA->msi_page_count - (uint64_t)msB->msi_page_count); + return (int)((uint64_t)msA->msi_page_count - (uint64_t)msB->msi_page_count); } /* @@ -8215,8 +10287,8 @@ static int memstat_asc_cmp(const void *a, const void *b) static int memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order) { -#define MAX_SORT_PIDS 80 -#define MAX_COAL_LEADERS 10 +#define MAX_SORT_PIDS 80 +#define MAX_COAL_LEADERS 10 unsigned int b = bucket_index; int nleaders = 0; @@ -8227,7 +10299,7 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa int total_pids_moved = 0; int i; - /* + /* * The system is typically under memory pressure when in this * path, hence, we want to avoid dynamic memory allocation. */ @@ -8235,51 +10307,51 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa pid_t pid_list[MAX_SORT_PIDS]; if (bucket_index >= MEMSTAT_BUCKET_COUNT) { - return(0); - } + return 0; + } /* * Clear the array that holds coalition leader information */ - for (i=0; i < MAX_COAL_LEADERS; i++) { + for (i = 0; i < MAX_COAL_LEADERS; i++) { leaders[i].msi_coal = COALITION_NULL; - leaders[i].msi_page_count = 0; /* will hold total coalition page count */ - leaders[i].msi_pid = 0; /* will hold coalition leader pid */ - leaders[i].msi_ntasks = 0; /* will hold the number of tasks in a coalition */ + leaders[i].msi_page_count = 0; /* will hold total coalition page count */ + leaders[i].msi_pid = 0; /* will hold coalition leader pid */ + leaders[i].msi_ntasks = 0; /* will hold the number of tasks in a coalition */ } - p = memorystatus_get_first_proc_locked(&b, FALSE); - while (p) { - if (coalition_is_leader(p->task, COALITION_TYPE_JETSAM, &coal)) { + p = memorystatus_get_first_proc_locked(&b, FALSE); + while (p) { + if (coalition_is_leader(p->task, COALITION_TYPE_JETSAM, &coal)) { if (nleaders < MAX_COAL_LEADERS) { int coal_ntasks = 0; uint64_t coal_page_count = coalition_get_page_count(coal, &coal_ntasks); leaders[nleaders].msi_coal = coal; leaders[nleaders].msi_page_count = coal_page_count; - leaders[nleaders].msi_pid = p->p_pid; /* the coalition leader */ + leaders[nleaders].msi_pid = p->p_pid; /* the coalition leader */ leaders[nleaders].msi_ntasks = coal_ntasks; nleaders++; } else { - /* + /* * We've hit MAX_COAL_LEADERS meaning we can handle no more coalitions. - * Abandoned coalitions will linger at the tail of the priority band + * Abandoned coalitions will linger at the tail of the priority band * when this sort session ends. * TODO: should this be an assert? */ printf("%s: WARNING: more than %d leaders in priority band [%d]\n", - __FUNCTION__, MAX_COAL_LEADERS, bucket_index); + __FUNCTION__, MAX_COAL_LEADERS, bucket_index); break; } - } - p=memorystatus_get_next_proc_locked(&b, p, FALSE); - } + } + p = memorystatus_get_next_proc_locked(&b, p, FALSE); + } if (nleaders == 0) { /* Nothing to sort */ - return(0); + return 0; } - /* + /* * Sort the coalition leader array, from smallest coalition page count * to largest coalition page count. When inserted in the priority bucket, * smallest coalition is handled first, resulting in the last to be jetsammed. @@ -8291,8 +10363,8 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa #if 0 for (i = 0; i < nleaders; i++) { printf("%s: coal_leader[%d of %d] pid[%d] pages[%llu] ntasks[%d]\n", - __FUNCTION__, i, nleaders, leaders[i].msi_pid, leaders[i].msi_page_count, - leaders[i].msi_ntasks); + __FUNCTION__, i, nleaders, leaders[i].msi_pid, leaders[i].msi_page_count, + leaders[i].msi_ntasks); } #endif @@ -8315,8 +10387,7 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa * based on their coalition role. */ total_pids_moved = 0; - for (i=0; i < nleaders; i++) { - + for (i = 0; i < nleaders; i++) { /* a bit of bookkeeping */ pids_moved = 0; @@ -8325,30 +10396,30 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, 1); /* xpc services should jetsam after extensions */ - ntasks = coalition_get_pid_list (leaders[i].msi_coal, COALITION_ROLEMASK_XPC, - coal_sort_order, pid_list, MAX_SORT_PIDS); + ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_XPC, + coal_sort_order, pid_list, MAX_SORT_PIDS); if (ntasks > 0) { - pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, - (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); + pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, + (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); } /* extensions should jetsam after unmarked processes */ - ntasks = coalition_get_pid_list (leaders[i].msi_coal, COALITION_ROLEMASK_EXT, - coal_sort_order, pid_list, MAX_SORT_PIDS); + ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_EXT, + coal_sort_order, pid_list, MAX_SORT_PIDS); if (ntasks > 0) { pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, - (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); + (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); } /* undefined coalition members should be the first to jetsam */ - ntasks = coalition_get_pid_list (leaders[i].msi_coal, COALITION_ROLEMASK_UNDEF, - coal_sort_order, pid_list, MAX_SORT_PIDS); + ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_UNDEF, + coal_sort_order, pid_list, MAX_SORT_PIDS); if (ntasks > 0) { - pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, - (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); + pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, + (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); } #if 0 @@ -8357,27 +10428,26 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa * All the pids in the coalition were found in this band. */ printf("%s: pids_moved[%d] equal total coalition ntasks[%d] \n", __FUNCTION__, - pids_moved, leaders[i].msi_ntasks); + pids_moved, leaders[i].msi_ntasks); } else if (pids_moved > leaders[i].msi_ntasks) { /* * Apparently new coalition members showed up during the sort? */ printf("%s: pids_moved[%d] were greater than expected coalition ntasks[%d] \n", __FUNCTION__, - pids_moved, leaders[i].msi_ntasks); + pids_moved, leaders[i].msi_ntasks); } else { /* * Apparently not all the pids in the coalition were found in this band? */ printf("%s: pids_moved[%d] were less than expected coalition ntasks[%d] \n", __FUNCTION__, - pids_moved, leaders[i].msi_ntasks); + pids_moved, leaders[i].msi_ntasks); } #endif total_pids_moved += pids_moved; - } /* end for */ - return(total_pids_moved); + return total_pids_moved; } @@ -8385,17 +10455,17 @@ memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coa * Traverse a list of pids, searching for each within the priority band provided. * If pid is found, move it to the front of the priority band. * Never searches outside the priority band provided. - * + * * Input: * bucket_index - jetsam priority band. * pid_list - pointer to a list of pids. * list_sz - number of pids in the list. * - * Pid list ordering is important in that, + * Pid list ordering is important in that, * pid_list[n] is expected to jetsam ahead of pid_list[n+1]. * The sort_order is set by the coalition default. * - * Return: + * Return: * the number of pids found and hence moved within the priority band. */ static int @@ -8406,15 +10476,15 @@ memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int li int found_pids = 0; if ((pid_list == NULL) || (list_sz <= 0)) { - return(0); + return 0; } if (bucket_index >= MEMSTAT_BUCKET_COUNT) { - return(0); - } + return 0; + } current_bucket = &memstat_bucket[bucket_index]; - for (i=0; i < list_sz; i++) { + for (i = 0; i < list_sz; i++) { unsigned int b = bucket_index; proc_t p = NULL; proc_t aProc = NULL; @@ -8422,27 +10492,166 @@ memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int li int list_index; list_index = ((list_sz - 1) - i); - aPid = pid_list[list_index]; - - /* never search beyond bucket_index provided */ - p = memorystatus_get_first_proc_locked(&b, FALSE); - while (p) { - if (p->p_pid == aPid) { - aProc = p; - break; - } - p = memorystatus_get_next_proc_locked(&b, p, FALSE); - } - - if (aProc == NULL) { + aPid = pid_list[list_index]; + + /* never search beyond bucket_index provided */ + p = memorystatus_get_first_proc_locked(&b, FALSE); + while (p) { + if (p->p_pid == aPid) { + aProc = p; + break; + } + p = memorystatus_get_next_proc_locked(&b, p, FALSE); + } + + if (aProc == NULL) { /* pid not found in this band, just skip it */ - continue; - } else { - TAILQ_REMOVE(¤t_bucket->list, aProc, p_memstat_list); - TAILQ_INSERT_HEAD(¤t_bucket->list, aProc, p_memstat_list); + continue; + } else { + TAILQ_REMOVE(¤t_bucket->list, aProc, p_memstat_list); + TAILQ_INSERT_HEAD(¤t_bucket->list, aProc, p_memstat_list); found_pids++; - } - } - return(found_pids); + } + } + return found_pids; +} + +int +memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index) +{ + int32_t i = JETSAM_PRIORITY_IDLE; + int count = 0; + + if (max_bucket_index >= MEMSTAT_BUCKET_COUNT) { + return -1; + } + + while (i <= max_bucket_index) { + count += memstat_bucket[i++].count; + } + + return count; +} + +int +memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap) +{ +#if !CONFIG_JETSAM + if (!p || (!isApp(p)) || (p->p_memstat_state & (P_MEMSTAT_INTERNAL | P_MEMSTAT_MANAGED))) { + /* + * Ineligible processes OR system processes e.g. launchd. + * + * We also skip processes that have the P_MEMSTAT_MANAGED bit set, i.e. + * they're managed by assertiond. These are iOS apps that have been ported + * to macOS. assertiond might be in the process of modifying the app's + * priority / memory limit - so it might have the proc_list lock, and then try + * to take the task lock. Meanwhile we've entered this function with the task lock + * held, and we need the proc_list lock below. So we'll deadlock with assertiond. + * + * It should be fine to read the P_MEMSTAT_MANAGED bit without the proc_list + * lock here, since assertiond only sets this bit on process launch. + */ + return -1; + } + + /* + * For macOS only: + * We would like to use memorystatus_update() here to move the processes + * within the bands. Unfortunately memorystatus_update() calls + * memorystatus_update_priority_locked() which uses any band transitions + * as an indication to modify ledgers. For that it needs the task lock + * and since we came into this function with the task lock held, we'll deadlock. + * + * Unfortunately we can't completely disable ledger updates because we still + * need the ledger updates for a subset of processes i.e. daemons. + * When all processes on all platforms support memory limits, we can simply call + * memorystatus_update(). + * + * It also has some logic to deal with 'aging' which, currently, is only applicable + * on CONFIG_JETSAM configs. So, till every platform has CONFIG_JETSAM we'll need + * to do this explicit band transition. + */ + + memstat_bucket_t *current_bucket, *new_bucket; + int32_t priority = 0; + + proc_list_lock(); + + if (((p->p_listflag & P_LIST_EXITED) != 0) || + (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED))) { + /* + * If the process is on its way out OR + * jetsam has alread tried and failed to kill this process, + * let's skip the whole jetsam band transition. + */ + proc_list_unlock(); + return 0; + } + + if (is_appnap) { + current_bucket = &memstat_bucket[p->p_memstat_effectivepriority]; + new_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; + priority = JETSAM_PRIORITY_IDLE; + } else { + if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) { + /* + * It is possible that someone pulled this process + * out of the IDLE band without updating its app-nap + * parameters. + */ + proc_list_unlock(); + return 0; + } + + current_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; + new_bucket = &memstat_bucket[p->p_memstat_requestedpriority]; + priority = p->p_memstat_requestedpriority; + } + + TAILQ_REMOVE(¤t_bucket->list, p, p_memstat_list); + current_bucket->count--; + + TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list); + new_bucket->count++; + + /* + * Record idle start or idle delta. + */ + if (p->p_memstat_effectivepriority == priority) { + /* + * This process is not transitioning between + * jetsam priority buckets. Do nothing. + */ + } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { + uint64_t now; + /* + * Transitioning out of the idle priority bucket. + * Record idle delta. + */ + assert(p->p_memstat_idle_start != 0); + now = mach_absolute_time(); + if (now > p->p_memstat_idle_start) { + p->p_memstat_idle_delta = now - p->p_memstat_idle_start; + } + } else if (priority == JETSAM_PRIORITY_IDLE) { + /* + * Transitioning into the idle priority bucket. + * Record idle start. + */ + p->p_memstat_idle_start = mach_absolute_time(); + } + + KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CHANGE_PRIORITY), p->p_pid, priority, p->p_memstat_effectivepriority, 0, 0); + + p->p_memstat_effectivepriority = priority; + + proc_list_unlock(); + + return 0; + +#else /* !CONFIG_JETSAM */ + #pragma unused(p) + #pragma unused(is_appnap) + return -1; +#endif /* !CONFIG_JETSAM */ } -#endif /* CONFIG_JETSAM */