+#endif /* CONFIG_JETSAM */
+
+static proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) {
+ memstat_bucket_t *current_bucket;
+ proc_t next_p;
+
+ if ((*bucket_index) >= MEMSTAT_BUCKET_COUNT) {
+ return NULL;
+ }
+
+ current_bucket = &memstat_bucket[*bucket_index];
+ next_p = TAILQ_FIRST(¤t_bucket->list);
+ if (!next_p && search) {
+ while (!next_p && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) {
+ current_bucket = &memstat_bucket[*bucket_index];
+ 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) {
+ memstat_bucket_t *current_bucket;
+ proc_t next_p;
+
+ if (!p || ((*bucket_index) >= MEMSTAT_BUCKET_COUNT)) {
+ return NULL;
+ }
+
+ next_p = TAILQ_NEXT(p, p_memstat_list);
+ while (!next_p && search && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) {
+ current_bucket = &memstat_bucket[*bucket_index];
+ next_p = TAILQ_FIRST(¤t_bucket->list);
+ }
+
+ return next_p;
+}
+
+__private_extern__ void
+memorystatus_init(void)
+{
+ thread_t thread = THREAD_NULL;
+ kern_return_t result;
+ int i;
+
+#if CONFIG_FREEZE
+ memorystatus_freeze_pages_min = FREEZE_PAGES_MIN;
+ memorystatus_freeze_pages_max = FREEZE_PAGES_MAX;
+#endif
+
+ nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_idle_delay_time);
+
+ /* 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);
+
+ /* Apply overrides */
+ PE_get_default("kern.jetsam_delta", &delta_percentage, sizeof(delta_percentage));
+ assert(delta_percentage < 100);
+ PE_get_default("kern.jetsam_critical_threshold", &critical_threshold_percentage, sizeof(critical_threshold_percentage));
+ assert(critical_threshold_percentage < 100);
+ PE_get_default("kern.jetsam_idle_offset", &idle_offset_percentage, sizeof(idle_offset_percentage));
+ assert(idle_offset_percentage < 100);
+ PE_get_default("kern.jetsam_pressure_threshold", &pressure_threshold_percentage, sizeof(pressure_threshold_percentage));
+ 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 CONFIG_JETSAM
+ 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_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);
+ if (!memorystatus_jetsam_snapshot) {
+ panic("Could not allocate memorystatus_jetsam_snapshot");
+ }
+
+ /* No contention at this point */
+ memorystatus_update_levels_locked(FALSE);
+#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");
+ }
+}
+
+/* Centralised for the purposes of allowing panic-on-jetsam */
+extern void
+vm_wake_compactor_swapper(void);
+
+/*
+ * The jetsam no frills kill call
+ * Return: 0 on success
+ * error code on failure (EINVAL...)
+ */
+static int
+jetsam_do_kill(proc_t p, int jetsam_flags) {
+ int error = 0;
+ error = exit1_internal(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags);
+ return(error);
+}
+
+/*
+ * Wrapper for processes exiting with memorystatus details
+ */
+static boolean_t
+memorystatus_do_kill(proc_t p, uint32_t cause) {
+
+ 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);
+
+#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
+ if (memorystatus_jetsam_panic_debug & (1 << cause)) {
+ panic("memorystatus_do_kill(): jetsam debug panic (cause: %d)", cause);
+ }
+#else
+#pragma unused(cause)
+#endif
+ 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;
+ }
+ error = jetsam_do_kill(p, jetsam_flags);
+
+ KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_END,
+ victim_pid, cause, vm_page_free_count, error, 0);
+
+ if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
+ vm_wake_compactor_swapper();
+ }
+
+ return (error == 0);
+}
+
+/*
+ * Node manipulation
+ */
+
+static void
+memorystatus_check_levels_locked(void) {
+#if CONFIG_JETSAM
+ /* Update levels */
+ memorystatus_update_levels_locked(TRUE);
+#endif
+}
+
+static void
+memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2)
+{
+ proc_t p;
+ uint64_t current_time;
+ 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();
+
+ proc_list_lock();
+
+ demotion_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE_DEFERRED];
+ 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_DEFER_IN_PROGRESS);
+ assert((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED);
+
+ if (current_time >= p->p_memstat_idledeadline) {
+#if DEBUG || DEVELOPMENT
+ if (!(p->p_memstat_dirty & P_DIRTY_MARKED)) {
+ printf("memorystatus_perform_idle_demotion: moving process %d [%s] to idle band, but never dirtied (0x%x)!\n",
+ p->p_pid, (p->p_comm ? p->p_comm : "(unknown)"), p->p_memstat_dirty);
+ }
+#endif
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, false);
+
+ // The prior process has moved out of the demotion bucket, so grab the new head and continue
+ p = TAILQ_FIRST(&demotion_bucket->list);
+ continue;
+ }
+
+ // 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)
+{
+ boolean_t present_in_deferred_bucket = FALSE;
+
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ present_in_deferred_bucket = TRUE;
+ }
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_schedule_idle_demotion_locked: scheduling demotion to idle band for process %d (dirty:0x%x, set_state %d, demotions %d).\n",
+ p->p_pid, p->p_memstat_dirty, set_state, memorystatus_scheduled_idle_demotions);
+
+ assert((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED);
+
+ if (set_state) {
+ assert(p->p_memstat_idledeadline == 0);
+ p->p_memstat_dirty |= P_DIRTY_DEFER_IN_PROGRESS;
+ p->p_memstat_idledeadline = mach_absolute_time() + memorystatus_idle_delay_time;
+ }
+
+ assert(p->p_memstat_idledeadline);
+
+ if (present_in_deferred_bucket == FALSE) {
+ memorystatus_scheduled_idle_demotions++;
+ }
+}
+
+static void
+memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state)
+{
+ boolean_t present_in_deferred_bucket = FALSE;
+
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ present_in_deferred_bucket = TRUE;
+ assert(p->p_memstat_idledeadline);
+ }
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for process %d (clear_state %d, demotions %d).\n",
+ p->p_pid, clear_state, memorystatus_scheduled_idle_demotions);
+
+
+ if (clear_state) {
+ p->p_memstat_idledeadline = 0;
+ p->p_memstat_dirty &= ~P_DIRTY_DEFER_IN_PROGRESS;
+ }
+
+ if (present_in_deferred_bucket == TRUE) {
+ memorystatus_scheduled_idle_demotions--;
+ }
+
+ assert(memorystatus_scheduled_idle_demotions >= 0);
+}
+
+static void
+memorystatus_reschedule_idle_demotion_locked(void) {
+ if (0 == memorystatus_scheduled_idle_demotions) {
+ 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;
+ demotion_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE_DEFERRED];
+ p = TAILQ_FIRST(&demotion_bucket->list);
+
+ assert(p && 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
+memorystatus_add(proc_t p, boolean_t locked)
+{
+ memstat_bucket_t *bucket;
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding process %d with priority %d.\n", p->p_pid, p->p_memstat_effectivepriority);
+
+ if (!locked) {
+ proc_list_lock();
+ }
+
+ /* Processes marked internal do not have priority tracked */
+ if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
+ goto exit;
+ }
+
+ bucket = &memstat_bucket[p->p_memstat_effectivepriority];
+
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ assert(bucket->count == memorystatus_scheduled_idle_demotions);
+ }
+
+ TAILQ_INSERT_TAIL(&bucket->list, p, p_memstat_list);
+ bucket->count++;
+
+ memorystatus_list_count++;
+
+ memorystatus_check_levels_locked();
+
+exit:
+ if (!locked) {
+ proc_list_unlock();
+ }
+
+ return 0;
+}
+
+static void
+memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert)
+{
+ 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 process %d to priority %d, inserting at %s\n",
+ p->p_pid, priority, head_insert ? "head" : "tail");
+
+ old_bucket = &memstat_bucket[p->p_memstat_effectivepriority];
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ assert(old_bucket->count == (memorystatus_scheduled_idle_demotions + 1));
+ }
+
+ TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list);
+ old_bucket->count--;
+
+ new_bucket = &memstat_bucket[priority];
+ if (head_insert)
+ TAILQ_INSERT_HEAD(&new_bucket->list, p, p_memstat_list);
+ else
+ TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list);
+ new_bucket->count++;
+
+#if CONFIG_JETSAM
+ if (memorystatus_highwater_enabled && (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND)) {
+
+ /*
+ * Adjust memory limit based on if the task is going to/from foreground and background.
+ */
+
+ if (((priority >= JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority < JETSAM_PRIORITY_FOREGROUND)) ||
+ ((priority < JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND))) {
+ int32_t memlimit = (priority >= JETSAM_PRIORITY_FOREGROUND) ? -1 : p->p_memstat_memlimit;
+ task_set_phys_footprint_limit_internal(p->task, (memlimit > 0) ? memlimit : -1, NULL, TRUE);
+
+ if (memlimit <= 0) {
+ p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;
+ } else {
+ p->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT;
+ }
+ }
+ }
+#endif
+
+ p->p_memstat_effectivepriority = priority;
+
+ memorystatus_check_levels_locked();
+}
+
+int
+memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, boolean_t update_memlimit, int32_t memlimit, boolean_t memlimit_background, boolean_t is_fatal_limit)
+{
+ int ret;
+ boolean_t head_insert = false;
+
+#if !CONFIG_JETSAM
+#pragma unused(update_memlimit, memlimit, memlimit_background, is_fatal_limit)
+#endif
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_update: changing process %d: priority %d, user_data 0x%llx\n", 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 == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use; if requested, adjust to 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;
+ head_insert = true;
+ } else if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) {
+ /* Sanity check */
+ ret = EINVAL;
+ goto out;
+ }
+
+ 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;
+ }
+
+ if ((p->p_memstat_state & P_MEMSTAT_TERMINATED) || ((p->p_listflag & P_LIST_EXITED) != 0)) {
+ /*
+ * This could happen when a process calling posix_spawn() is exiting on the jetsam thread.
+ */
+ ret = EBUSY;
+ proc_list_unlock();
+ goto out;
+ }
+
+ p->p_memstat_state |= P_MEMSTAT_PRIORITYUPDATED;
+ p->p_memstat_userdata = user_data;
+ p->p_memstat_requestedpriority = priority;
+
+#if CONFIG_JETSAM
+ if (update_memlimit) {
+ p->p_memstat_memlimit = memlimit;
+ if (memlimit_background) {
+ /* Will be set as priority is updated */
+ p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_BACKGROUND;
+
+ /* Cannot have a background memory limit and be fatal. */
+ is_fatal_limit = FALSE;
+
+ } else {
+ /* Otherwise, apply now */
+ if (memorystatus_highwater_enabled) {
+ task_set_phys_footprint_limit_internal(p->task, (memlimit > 0) ? memlimit : -1, NULL, TRUE);
+ }
+ }
+
+ if (is_fatal_limit || memlimit <= 0) {
+ p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;
+ } else {
+ p->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT;
+ }
+ }
+#endif
+
+ /*
+ * We can't add to the JETSAM_PRIORITY_IDLE_DEFERRED bucket here.
+ * But, we could be removing it from the bucket.
+ * Check and take appropriate steps if so.
+ */
+
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ }
+
+ memorystatus_update_priority_locked(p, priority, head_insert);
+
+ proc_list_unlock();
+ ret = 0;
+
+out:
+ KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_END, ret, 0, 0, 0, 0);
+
+ return ret;
+}
+
+int
+memorystatus_remove(proc_t p, boolean_t locked)
+{
+ int ret;
+ memstat_bucket_t *bucket;
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing process %d\n", p->p_pid);
+
+ if (!locked) {
+ proc_list_lock();
+ }
+
+ assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));
+
+ bucket = &memstat_bucket[p->p_memstat_effectivepriority];
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ assert(bucket->count == memorystatus_scheduled_idle_demotions);
+ }
+
+ TAILQ_REMOVE(&bucket->list, p, p_memstat_list);
+ bucket->count--;
+
+ memorystatus_list_count--;
+
+ /* If awaiting demotion to the idle band, clean up */
+ if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ memorystatus_reschedule_idle_demotion_locked();
+ }
+
+ memorystatus_check_levels_locked();
+
+#if CONFIG_FREEZE
+ if (p->p_memstat_state & (P_MEMSTAT_FROZEN)) {
+ memorystatus_frozen_count--;
+ }
+
+ 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 (p) {
+ ret = 0;
+ } else {
+ ret = ESRCH;
+ }
+
+ return ret;
+}
+
+static boolean_t
+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 FALSE;
+ }
+
+ /* Idle exit requires that process be tracked */
+ if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) &&
+ !(pcontrol & PROC_DIRTY_TRACK)) {
+ return FALSE;
+ }
+
+ /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */
+ if ((pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) &&
+ !(pcontrol & PROC_DIRTY_TRACK)) {
+ return FALSE;
+ }
+
+ /* Deferral is only relevant if idle exit is specified */
+ if ((pcontrol & PROC_DIRTY_DEFER) &&
+ !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static void
+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);
+
+ 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_DEFER_IN_PROGRESS) ? JETSAM_PRIORITY_IDLE_DEFERRED : JETSAM_PRIORITY_IDLE;
+ } else {
+ priority = p->p_memstat_requestedpriority;
+ }
+
+ if (priority != p->p_memstat_effectivepriority) {
+ memorystatus_update_priority_locked(p, priority, false);
+ }
+}
+
+/*
+ * Processes can opt to have their state tracked by the kernel, indicating when they are busy (dirty) or idle
+ * (clean). They may also indicate that they support termination when idle, with the result that they are promoted
+ * to their desired, higher, jetsam priority when dirty (and are therefore killed later), and demoted to the low
+ * priority idle band when clean (and killed earlier, protecting higher priority procesess).
+ *
+ * If the deferral flag is set, then newly tracked processes will be protected for an initial period (as determined by
+ * memorystatus_idle_delay_time); if they go clean during this time, then they will be moved to a deferred-idle band
+ * with a slightly higher priority, guarding against immediate termination under memory pressure and being unable to
+ * make forward progress. Finally, when the guard expires, they will be moved to the standard, lowest-priority, idle
+ * band. The deferral can be cleared early by clearing the appropriate flag.
+ *
+ * The deferral timer is active only for the duration that the process is marked as guarded and clean; if the process
+ * is marked dirty, the timer will be cancelled. Upon being subsequently marked clean, the deferment will either be
+ * re-enabled or the guard state cleared, depending on whether the guard deadline has passed.
+ */
+
+int
+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;
+
+ KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_TRACK),
+ 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.
+ */
+ ret = EBUSY;
+ goto exit;
+ }
+
+ if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
+ ret = EPERM;
+ goto exit;
+ }
+
+ if (!memorystatus_validate_track_flags(p, pcontrol)) {
+ ret = EINVAL;
+ goto exit;
+ }
+
+ old_dirty = p->p_memstat_dirty;
+
+ /* These bits are cumulative, as per <rdar://problem/11159924> */
+ if (pcontrol & PROC_DIRTY_TRACK) {
+ p->p_memstat_dirty |= P_DIRTY_TRACK;
+ }
+
+ if (pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) {
+ p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT;
+ }
+
+ if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) {
+ p->p_memstat_dirty |= P_DIRTY_LAUNCH_IN_PROGRESS;
+ }
+
+ if (old_dirty & P_DIRTY_DEFER_IN_PROGRESS) {
+ already_deferred = TRUE;
+ }
+
+ /* This can be set and cleared exactly once. */
+ if (pcontrol & PROC_DIRTY_DEFER) {
+
+ if ( !(old_dirty & P_DIRTY_DEFER)) {
+ p->p_memstat_dirty |= P_DIRTY_DEFER;
+ }
+
+ defer_now = TRUE;
+ }
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for process %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);
+
+ /* 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) &&
+ defer_now && !already_deferred) {
+
+ /*
+ * Request to defer a clean process that's idle-exit enabled
+ * and not already in the jetsam deferred band.
+ */
+ memorystatus_schedule_idle_demotion_locked(p, TRUE);
+ reschedule = TRUE;
+
+ } else if (!defer_now && already_deferred) {
+
+ /*
+ * Either the process is no longer idle-exit enabled OR
+ * there's a request to cancel a currently active deferral.
+ */
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ reschedule = TRUE;
+ }
+ } 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
+ * deferred state or not?
+ *
+ * This could be a legal request like:
+ * - this process had opted into the JETSAM_DEFERRED band
+ * - but it's now dirty and requests to opt out.
+ * In this case, we remove the process from the band and reset its
+ * state too. It'll opt back in properly when needed.
+ *
+ * OR, this request could be a user-space bug. E.g.:
+ * - this process had opted into the JETSAM_DEFERRED band when clean
+ * - and, then issues another request to again put it into the band except
+ * this time the process is dirty.
+ * The process going dirty, as a transition in memorystatus_dirty_set(), will pull the process out of
+ * the deferred band with its state intact. So our request below is no-op.
+ * But we do it here anyways for coverage.
+ *
+ * memorystatus_update_idle_priority_locked()
+ * single-mindedly treats a dirty process as "cannot be in the deferred band".
+ */
+
+ if (!defer_now && already_deferred) {
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ reschedule = TRUE;
+ } else {
+ memorystatus_invalidate_idle_demotion_locked(p, FALSE);
+ reschedule = TRUE;
+ }
+ }
+
+ memorystatus_update_idle_priority_locked(p);
+
+ if (reschedule) {
+ memorystatus_reschedule_idle_demotion_locked();
+ }
+
+ ret = 0;
+
+exit:
+ proc_list_unlock();
+
+ return ret;
+}
+
+int
+memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) {
+ int ret;
+ boolean_t kill = false;
+ boolean_t reschedule = FALSE;
+ boolean_t was_dirty = FALSE;
+ boolean_t now_dirty = FALSE;
+
+ MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self, p->p_pid, pcontrol, p->p_memstat_dirty);
+
+ KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_SET), p->p_pid, self, pcontrol, 0, 0);
+
+ proc_list_lock();
+
+ if ((p->p_listflag & P_LIST_EXITED) != 0) {
+ /*
+ * Process is on its way out.
+ */
+ ret = EBUSY;
+ goto exit;
+ }
+
+ if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
+ ret = EPERM;
+ goto exit;
+ }
+
+ 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;
+ } 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 <rdar://problem/10594349>.
+ */
+ ret = EBUSY;
+ } else {
+ int flag = (self == TRUE) ? P_DIRTY : P_DIRTY_SHUTDOWN;
+ if (pcontrol && !(p->p_memstat_dirty & flag)) {
+ /* Mark the process as having been dirtied at some point */
+ p->p_memstat_dirty |= (flag | P_DIRTY_MARKED);
+ memorystatus_dirty_count++;
+ ret = 0;
+ } else if ((pcontrol == 0) && (p->p_memstat_dirty & flag)) {
+ if ((flag == P_DIRTY_SHUTDOWN) && (!p->p_memstat_dirty & P_DIRTY)) {
+ /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */
+ p->p_memstat_dirty |= P_DIRTY_TERMINATED;
+ kill = true;
+ } else if ((flag == P_DIRTY) && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) {
+ /* Kill previously terminated processes if set clean */
+ kill = true;
+ }
+ p->p_memstat_dirty &= ~flag;
+ memorystatus_dirty_count--;
+ ret = 0;
+ } else {
+ /* Already set */
+ ret = EALREADY;
+ }
+ }
+
+ if (ret != 0) {
+ goto exit;
+ }
+
+ 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_DEFER_IN_PROGRESS)) ==
+ (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS)) {
+
+ /*
+ * P_DIRTY_DEFER_IN_PROGRESS means the process is in the deferred band OR it might be heading back
+ * there once it's clean again and has some protection window left.
+ */
+
+ if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) {
+ /*
+ * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE"
+ *
+ * The process will move from the deferred band to its higher requested
+ * jetsam band. But we don't clear its state i.e. we want to remember that
+ * this process was part of the "deferred" band and will return to it.
+ *
+ * This way, we don't let it age beyond the protection
+ * window when it returns to "clean". All the while giving
+ * it a chance to perform its work while "dirty".
+ *
+ */
+ memorystatus_invalidate_idle_demotion_locked(p, FALSE);
+ reschedule = TRUE;
+ } else {
+
+ /*
+ * Process is back from "dirty" to "clean".
+ *
+ * Is its timer up OR does it still have some protection
+ * window left?
+ */
+
+ if (mach_absolute_time() >= p->p_memstat_idledeadline) {
+ /*
+ * The process' deadline has expired. It currently
+ * does not reside in the DEFERRED bucket.
+ *
+ * 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 DEFERRED bucket i.e.
+ * the DIRTY_DEFER_IN_PROGRESS flag and the timer deadline.
+ *
+ */
+
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ reschedule = TRUE;
+ } else {
+ /*
+ * It still has some protection window left and so
+ * we just re-arm the timer without modifying any
+ * state on the process.
+ */
+ memorystatus_schedule_idle_demotion_locked(p, FALSE);
+ reschedule = TRUE;
+ }
+ }
+ }
+
+ memorystatus_update_idle_priority_locked(p);
+
+ /* If the deferral state changed, reschedule the demotion timer */
+ if (reschedule) {
+ memorystatus_reschedule_idle_demotion_locked();
+ }
+ }
+
+ if (kill) {
+ psignal(p, SIGKILL);
+ }
+
+exit:
+ proc_list_unlock();
+
+ return ret;
+}
+
+int
+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();
+
+ if ((p->p_listflag & P_LIST_EXITED) != 0) {
+ /*
+ * Process is on its way out.
+ */
+ ret = EBUSY;
+ goto exit;
+ }
+
+ if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
+ ret = EPERM;
+ goto exit;
+ }
+
+ if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) {
+ /* Dirty tracking not enabled */
+ ret = EINVAL;
+ goto exit;
+ }
+
+ if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER)) == 0) {
+ ret = EINVAL;
+ goto exit;
+ }
+
+ if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) {
+ p->p_memstat_dirty &= ~P_DIRTY_LAUNCH_IN_PROGRESS;
+ }
+
+ /* 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;
+
+ memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+ memorystatus_update_idle_priority_locked(p);
+ memorystatus_reschedule_idle_demotion_locked();
+ }
+ }
+
+ ret = 0;
+exit:
+ proc_list_unlock();
+
+ return ret;
+}
+
+int
+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) {
+ ret |= PROC_DIRTY_ALLOWS_IDLE_EXIT;
+ }
+ if (p->p_memstat_dirty & P_DIRTY) {
+ ret |= PROC_DIRTY_IS_DIRTY;
+ }
+ if (p->p_memstat_dirty & P_DIRTY_LAUNCH_IN_PROGRESS) {
+ ret |= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS;
+ }
+ }
+
+ proc_list_unlock();
+
+ return ret;
+}
+
+int
+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) {
+ /* Clean; mark as terminated and issue SIGKILL */
+ sig = SIGKILL;
+ } else {
+ /* Dirty, terminated, or state tracking is unsupported; issue SIGTERM to allow cleanup */
+ sig = SIGTERM;
+ }
+
+ proc_list_unlock();
+
+ return sig;
+}
+
+void
+memorystatus_on_suspend(proc_t p)
+{
+#if CONFIG_FREEZE
+ uint32_t pages;
+ memorystatus_get_task_page_counts(p->task, &pages, NULL, 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;
+ proc_list_unlock();
+}
+
+void
+memorystatus_on_resume(proc_t p)
+{
+#if CONFIG_FREEZE
+ boolean_t frozen;
+ pid_t pid;
+#endif
+
+ proc_list_lock();
+
+#if CONFIG_FREEZE
+ frozen = (p->p_memstat_state & P_MEMSTAT_FROZEN);
+ if (frozen) {
+ memorystatus_frozen_count--;
+ p->p_memstat_state |= P_MEMSTAT_PRIOR_THAW;
+ }
+
+ 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);
+
+ proc_list_unlock();
+
+#if CONFIG_FREEZE
+ if (frozen) {
+ memorystatus_freeze_entry_t data = { pid, FALSE, 0 };
+ memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data));
+ }
+#endif
+}
+
+void
+memorystatus_on_inactivity(proc_t p)
+{
+#pragma unused(p)
+#if CONFIG_FREEZE
+ /* Wake the freeze thread */
+ thread_wakeup((event_t)&memorystatus_freeze_wakeup);
+#endif
+}
+
+static uint32_t
+memorystatus_build_state(proc_t p) {
+ uint32_t snapshot_state = 0;
+
+ /* General */
+ if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) {
+ snapshot_state |= kMemorystatusSuspended;
+ }
+ if (p->p_memstat_state & P_MEMSTAT_FROZEN) {
+ snapshot_state |= kMemorystatusFrozen;
+ }
+ if (p->p_memstat_state & P_MEMSTAT_PRIOR_THAW) {
+ snapshot_state |= kMemorystatusWasThawed;
+ }
+
+ /* Tracking */
+ if (p->p_memstat_dirty & P_DIRTY_TRACK) {
+ snapshot_state |= kMemorystatusTracked;
+ }
+ if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) {
+ snapshot_state |= kMemorystatusSupportsIdleExit;
+ }
+ if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) {
+ snapshot_state |= kMemorystatusDirty;
+ }
+
+ return snapshot_state;
+}
+
+#if !CONFIG_JETSAM
+
+static boolean_t
+kill_idle_exit_proc(void)
+{
+ proc_t p, victim_p = PROC_NULL;
+ uint64_t current_time;
+ boolean_t killed = FALSE;
+ unsigned int i = 0;
+
+ /* Pick next idle exit victim. */
+ current_time = mach_absolute_time();
+
+ 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 (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_comm ? victim_p->p_comm : "(unknown)"));
+ killed = memorystatus_do_kill(victim_p, kMemorystatusKilledIdleExit);
+ proc_rele(victim_p);
+ }
+
+ return killed;
+}
+#endif
+
+#if CONFIG_JETSAM
+static void
+memorystatus_thread_wake(void) {
+ thread_wakeup((event_t)&memorystatus_wakeup);
+}
+#endif /* CONFIG_JETSAM */
+
+extern void vm_pressure_response(void);
+
+static int
+memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation)
+{
+ if (interval_ms) {
+ assert_wait_timeout(&memorystatus_wakeup, THREAD_UNINT, interval_ms, 1000 * NSEC_PER_USEC);
+ } else {
+ assert_wait(&memorystatus_wakeup, THREAD_UNINT);
+ }
+
+ return thread_block(continuation);
+}
+
+static void
+memorystatus_thread(void *param __unused, wait_result_t wr __unused)
+{
+ static boolean_t is_vm_privileged = FALSE;
+#if CONFIG_JETSAM
+ boolean_t post_snapshot = FALSE;
+ uint32_t errors = 0;
+ uint32_t hwm_kill = 0;
+#endif
+
+ 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 <rdar://problem/9609402>.
+ */
+ thread_wire(host_priv_self(), current_thread(), TRUE);
+ is_vm_privileged = TRUE;
+
+ memorystatus_thread_block(0, memorystatus_thread);
+ }
+
+#if CONFIG_JETSAM
+
+ KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START,
+ memorystatus_available_pages, 0, 0, 0, 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 (is_thrashing(kill_under_pressure_cause) ||
+ memorystatus_available_pages <= memorystatus_available_pages_pressure) {
+ boolean_t killed;
+ int32_t priority;
+ uint32_t cause;
+
+ if (kill_under_pressure_cause) {
+ cause = kill_under_pressure_cause;
+ } else {
+ cause = kMemorystatusKilledVMPageShortage;
+ }
+
+#if LEGACY_HIWATER
+ /* Highwater */
+ killed = memorystatus_kill_hiwat_proc(&errors);
+ if (killed) {
+ hwm_kill++;
+ post_snapshot = TRUE;
+ goto done;
+ } else {
+ memorystatus_hwm_candidates = FALSE;
+ }
+
+ /* No highwater processes to kill. Continue or stop for now? */
+ if (!is_thrashing(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
+ * - no more HWM processes left.
+ * For now, don't kill any other processes.
+ */
+
+ if (hwm_kill == 0) {
+ memorystatus_thread_wasted_wakeup++;
+ }
+
+ break;
+ }
+#endif
+
+ /* LRU */
+ killed = memorystatus_kill_top_process(TRUE, cause, &priority, &errors);
+ if (killed) {
+ /* Don't generate logs for steady-state idle-exit kills (unless overridden for debug) */
+ if ((priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot) {
+ post_snapshot = TRUE;
+ }
+ goto done;
+ }
+
+ if (memorystatus_available_pages <= memorystatus_available_pages_critical) {
+ /* Under pressure and unable to kill a process - panic */
+ panic("memorystatus_jetsam_thread: no victim! available pages:%d\n", memorystatus_available_pages);
+ }
+
+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)) {
+ kill_under_pressure_cause = 0;
+ vm_thrashing_jetsam_done();
+ }
+ }
+
+ 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) {
+ size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) +
+ sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count);
+ memorystatus_jetsam_snapshot->notification_time = mach_absolute_time();
+ memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
+ }
+
+ 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_thread_block(0, memorystatus_thread);
+}
+
+#if !CONFIG_JETSAM
+/*
+ * Returns TRUE:
+ * 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
+ */
+boolean_t memorystatus_idle_exit_from_VM(void) {
+ return(kill_idle_exit_proc());
+}
+#endif /* !CONFIG_JETSAM */
+
+#if CONFIG_JETSAM
+
+/*
+ * Callback invoked when allowable physical memory footprint exceeded
+ * (dirty pages + IOKit mappings)
+ *
+ * This is invoked for both advisory, non-fatal per-task high watermarks,
+ * as well as the fatal task memory limits.
+ */
+void
+memorystatus_on_ledger_footprint_exceeded(boolean_t warning, const int max_footprint_mb)
+{
+ proc_t p = current_proc();
+
+ if (warning == FALSE) {
+ printf("process %d (%s) exceeded physical memory footprint limit of %d MB\n",
+ p->p_pid, p->p_comm, max_footprint_mb);
+ }
+
+#if VM_PRESSURE_EVENTS
+ if (warning == TRUE) {
+ if (memorystatus_warn_process(p->p_pid, TRUE /* critical? */) != 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 (exiting, or no handler registered?).\n");
+ }
+ return;
+ }
+#endif /* VM_PRESSURE_EVENTS */
+
+ if ((p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT) == P_MEMSTAT_FATAL_MEMLIMIT) {
+ /*
+ * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task
+ * has violated either the system-wide per-task memory limit OR its own task limit.
+ */
+ if (memorystatus_kill_process_sync(p->p_pid, kMemorystatusKilledPerProcessLimit) != TRUE) {
+ printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n");
+ }
+ } else {
+ /*
+ * HWM offender exists. Done without locks or synchronization.
+ * See comment near its declaration for more details.
+ */
+ memorystatus_hwm_candidates = TRUE;
+ }
+}
+
+/*
+ * 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();
+
+ printf("task_exceeded_cpulimit: killing pid %d [%s]\n",
+ p->p_pid, (p->p_comm ? p->p_comm : "(unknown)"));
+
+ retval = jetsam_do_kill(p, jetsam_flags);
+
+ if (retval) {
+ printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n");
+ }
+}
+
+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)
+{
+ assert(task);
+ assert(footprint);
+
+ *footprint = (uint32_t)(get_task_phys_footprint(task) / PAGE_SIZE_64);
+ if (max_footprint) {
+ *max_footprint = (uint32_t)(get_task_phys_footprint_max(task) / PAGE_SIZE_64);
+ }
+ if (max_footprint_lifetime) {
+ *max_footprint_lifetime = (uint32_t)(get_task_resident_max(task) / PAGE_SIZE_64);
+ }
+ if (purgeable_pages) {
+ *purgeable_pages = (uint32_t)(get_task_purgeable_size(task) / PAGE_SIZE_64);
+ }
+}
+
+
+static void
+memorystatus_update_snapshot_locked(proc_t p, uint32_t kill_cause)
+{
+ unsigned int i;
+
+ for (i = 0; i < memorystatus_jetsam_snapshot_count; i++) {
+ if (memorystatus_jetsam_snapshot_list[i].pid == p->p_pid) {
+ /* Update if the priority has changed since the snapshot was taken */
+ if (memorystatus_jetsam_snapshot_list[i].priority != p->p_memstat_effectivepriority) {
+ memorystatus_jetsam_snapshot_list[i].priority = p->p_memstat_effectivepriority;
+ strlcpy(memorystatus_jetsam_snapshot_list[i].name, p->p_comm, MAXCOMLEN+1);
+ memorystatus_jetsam_snapshot_list[i].state = memorystatus_build_state(p);
+ memorystatus_jetsam_snapshot_list[i].user_data = p->p_memstat_userdata;
+ memorystatus_jetsam_snapshot_list[i].fds = p->p_fd->fd_nfiles;
+ }
+ memorystatus_jetsam_snapshot_list[i].killed = kill_cause;
+ return;
+ }
+ }
+}
+
+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
+ * 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();
+ }
+ }
+#else /* VM_PRESSURE_EVENTS */
+
+ boolean_t critical, delta;
+
+ if (!memorystatus_delta) {
+ 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;
+
+ if (critical || delta) {
+ memorystatus_level = memorystatus_available_pages * 100 / atop_64(max_mem);
+ memorystatus_thread_wake();
+ }
+#endif /* VM_PRESSURE_EVENTS */
+}
+
+static boolean_t
+memorystatus_get_snapshot_properties_for_proc_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry)
+{
+ clock_sec_t tv_sec;
+ clock_usec_t tv_usec;
+
+ memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t));
+
+ entry->pid = p->p_pid;
+ strlcpy(&entry->name[0], p->p_comm, MAXCOMLEN+1);
+ entry->priority = p->p_memstat_effectivepriority;
+ memorystatus_get_task_page_counts(p->task, &entry->pages, &entry->max_pages, &entry->max_pages_lifetime, &entry->purgeable_pages);
+ entry->state = memorystatus_build_state(p);
+ entry->user_data = p->p_memstat_userdata;
+ memcpy(&entry->uuid[0], &p->p_uuid[0], sizeof(p->p_uuid));
+ 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;
+
+ return TRUE;
+}
+
+static void
+memorystatus_jetsam_snapshot_procs_locked(void)
+{
+ proc_t p, next_p;
+ unsigned int b = 0, i = 0;
+ kern_return_t kr = KERN_SUCCESS;
+
+ 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)) {
+ printf("memorystatus_jetsam_snapshot_procs_locked: host_statistics64 failed with %d\n", kr);
+ memset(&memorystatus_jetsam_snapshot->stats, 0, sizeof(memorystatus_jetsam_snapshot->stats));
+ } else {
+ memorystatus_jetsam_snapshot->stats.free_pages = vm_stat.free_count;
+ memorystatus_jetsam_snapshot->stats.active_pages = vm_stat.active_count;
+ memorystatus_jetsam_snapshot->stats.inactive_pages = vm_stat.inactive_count;
+ memorystatus_jetsam_snapshot->stats.throttled_pages = vm_stat.throttled_count;
+ memorystatus_jetsam_snapshot->stats.purgeable_pages = vm_stat.purgeable_count;
+ memorystatus_jetsam_snapshot->stats.wired_pages = vm_stat.wire_count;
+
+ memorystatus_jetsam_snapshot->stats.speculative_pages = vm_stat.speculative_count;
+ memorystatus_jetsam_snapshot->stats.filebacked_pages = vm_stat.external_page_count;
+ memorystatus_jetsam_snapshot->stats.anonymous_pages = vm_stat.internal_page_count;
+ memorystatus_jetsam_snapshot->stats.compressions = vm_stat.compressions;
+ memorystatus_jetsam_snapshot->stats.decompressions = vm_stat.decompressions;
+ memorystatus_jetsam_snapshot->stats.compressor_pages = vm_stat.compressor_page_count;
+ memorystatus_jetsam_snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor;
+ }
+
+ next_p = memorystatus_get_first_proc_locked(&b, TRUE);
+ while (next_p) {
+ p = next_p;
+ next_p = memorystatus_get_next_proc_locked(&b, p, TRUE);
+
+ if (FALSE == memorystatus_get_snapshot_properties_for_proc_locked(p, &memorystatus_jetsam_snapshot_list[i])) {
+ 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]);
+
+ if (++i == memorystatus_jetsam_snapshot_max) {
+ break;
+ }
+ }
+
+ memorystatus_jetsam_snapshot->snapshot_time = mach_absolute_time();
+ memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = i;
+}
+
+#if DEVELOPMENT || DEBUG
+
+static int
+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;
+ }
+
+ ret = copyin(buffer, &debug, 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
+
+/*
+ * Jetsam a specific process.
+ */
+static boolean_t
+memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause) {
+ boolean_t killed;