]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
cb323159 | 2 | * Copyright (c) 2006-2019 Apple Inc. All rights reserved. |
2d21ac55 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | * | |
28 | */ | |
2d21ac55 | 29 | |
2d21ac55 | 30 | #include <kern/sched_prim.h> |
6d2010ae | 31 | #include <kern/kalloc.h> |
316670eb | 32 | #include <kern/assert.h> |
6d2010ae | 33 | #include <kern/debug.h> |
fe8ab488 | 34 | #include <kern/locks.h> |
2d21ac55 A |
35 | #include <kern/task.h> |
36 | #include <kern/thread.h> | |
316670eb | 37 | #include <kern/host.h> |
39037602 | 38 | #include <kern/policy_internal.h> |
5ba3f43e | 39 | #include <kern/thread_group.h> |
39037602 | 40 | |
cb323159 | 41 | #include <corpses/task_corpse.h> |
2d21ac55 | 42 | #include <libkern/libkern.h> |
3e170ce0 | 43 | #include <mach/coalition.h> |
316670eb | 44 | #include <mach/mach_time.h> |
b0d623f7 | 45 | #include <mach/task.h> |
316670eb | 46 | #include <mach/host_priv.h> |
39236c6e | 47 | #include <mach/mach_host.h> |
5ba3f43e | 48 | #include <os/log.h> |
39236c6e | 49 | #include <pexpert/pexpert.h> |
3e170ce0 | 50 | #include <sys/coalition.h> |
316670eb | 51 | #include <sys/kern_event.h> |
b0d623f7 | 52 | #include <sys/proc.h> |
39236c6e | 53 | #include <sys/proc_info.h> |
39037602 | 54 | #include <sys/reason.h> |
b0d623f7 A |
55 | #include <sys/signal.h> |
56 | #include <sys/signalvar.h> | |
2d21ac55 | 57 | #include <sys/sysctl.h> |
316670eb | 58 | #include <sys/sysproto.h> |
b0d623f7 | 59 | #include <sys/wait.h> |
6d2010ae | 60 | #include <sys/tree.h> |
316670eb | 61 | #include <sys/priv.h> |
f427ee49 | 62 | #include <vm/pmap.h> |
39236c6e A |
63 | #include <vm/vm_pageout.h> |
64 | #include <vm/vm_protos.h> | |
cb323159 A |
65 | #include <mach/machine/sdt.h> |
66 | #include <libkern/section_keywords.h> | |
67 | #include <stdatomic.h> | |
6d2010ae | 68 | |
f427ee49 A |
69 | #include <IOKit/IOBSD.h> |
70 | ||
6d2010ae | 71 | #if CONFIG_FREEZE |
6d2010ae | 72 | #include <vm/vm_map.h> |
39236c6e | 73 | #endif /* CONFIG_FREEZE */ |
6d2010ae | 74 | |
0a7de745 | 75 | #include <sys/kern_memorystatus.h> |
cb323159 A |
76 | #include <sys/kern_memorystatus_freeze.h> |
77 | #include <sys/kern_memorystatus_notify.h> | |
39037602 | 78 | |
fe8ab488 | 79 | /* For logging clarity */ |
5ba3f43e | 80 | static const char *memorystatus_kill_cause_name[] = { |
0a7de745 A |
81 | "", /* kMemorystatusInvalid */ |
82 | "jettisoned", /* kMemorystatusKilled */ | |
83 | "highwater", /* kMemorystatusKilledHiwat */ | |
84 | "vnode-limit", /* kMemorystatusKilledVnodes */ | |
85 | "vm-pageshortage", /* kMemorystatusKilledVMPageShortage */ | |
86 | "proc-thrashing", /* kMemorystatusKilledProcThrashing */ | |
87 | "fc-thrashing", /* kMemorystatusKilledFCThrashing */ | |
88 | "per-process-limit", /* kMemorystatusKilledPerProcessLimit */ | |
89 | "disk-space-shortage", /* kMemorystatusKilledDiskSpaceShortage */ | |
90 | "idle-exit", /* kMemorystatusKilledIdleExit */ | |
91 | "zone-map-exhaustion", /* kMemorystatusKilledZoneMapExhaustion */ | |
92 | "vm-compressor-thrashing", /* kMemorystatusKilledVMCompressorThrashing */ | |
93 | "vm-compressor-space-shortage", /* kMemorystatusKilledVMCompressorSpaceShortage */ | |
fe8ab488 A |
94 | }; |
95 | ||
5ba3f43e A |
96 | static const char * |
97 | memorystatus_priority_band_name(int32_t priority) | |
98 | { | |
99 | switch (priority) { | |
100 | case JETSAM_PRIORITY_FOREGROUND: | |
101 | return "FOREGROUND"; | |
102 | case JETSAM_PRIORITY_AUDIO_AND_ACCESSORY: | |
103 | return "AUDIO_AND_ACCESSORY"; | |
104 | case JETSAM_PRIORITY_CONDUCTOR: | |
105 | return "CONDUCTOR"; | |
cb323159 A |
106 | case JETSAM_PRIORITY_DRIVER_APPLE: |
107 | return "DRIVER_APPLE"; | |
5ba3f43e A |
108 | case JETSAM_PRIORITY_HOME: |
109 | return "HOME"; | |
110 | case JETSAM_PRIORITY_EXECUTIVE: | |
111 | return "EXECUTIVE"; | |
112 | case JETSAM_PRIORITY_IMPORTANT: | |
113 | return "IMPORTANT"; | |
114 | case JETSAM_PRIORITY_CRITICAL: | |
115 | return "CRITICAL"; | |
116 | } | |
117 | ||
0a7de745 | 118 | return "?"; |
5ba3f43e A |
119 | } |
120 | ||
fe8ab488 A |
121 | /* Does cause indicate vm or fc thrashing? */ |
122 | static boolean_t | |
5ba3f43e | 123 | is_reason_thrashing(unsigned cause) |
fe8ab488 A |
124 | { |
125 | switch (cause) { | |
fe8ab488 | 126 | case kMemorystatusKilledFCThrashing: |
d9a64523 A |
127 | case kMemorystatusKilledVMCompressorThrashing: |
128 | case kMemorystatusKilledVMCompressorSpaceShortage: | |
fe8ab488 A |
129 | return TRUE; |
130 | default: | |
131 | return FALSE; | |
132 | } | |
133 | } | |
134 | ||
5ba3f43e A |
135 | /* Is the zone map almost full? */ |
136 | static boolean_t | |
137 | is_reason_zone_map_exhaustion(unsigned cause) | |
138 | { | |
0a7de745 | 139 | if (cause == kMemorystatusKilledZoneMapExhaustion) { |
5ba3f43e | 140 | return TRUE; |
0a7de745 | 141 | } |
5ba3f43e A |
142 | return FALSE; |
143 | } | |
144 | ||
145 | /* | |
146 | * Returns the current zone map size and capacity to include in the jetsam snapshot. | |
147 | * Defined in zalloc.c | |
148 | */ | |
149 | extern void get_zone_map_size(uint64_t *current_size, uint64_t *capacity); | |
150 | ||
151 | /* | |
152 | * Returns the name of the largest zone and its size to include in the jetsam snapshot. | |
153 | * Defined in zalloc.c | |
154 | */ | |
155 | extern void get_largest_zone_info(char *zone_name, size_t zone_name_len, uint64_t *zone_size); | |
fe8ab488 | 156 | |
3e170ce0 A |
157 | /* |
158 | * Active / Inactive limit support | |
159 | * proc list must be locked | |
160 | * | |
161 | * The SET_*** macros are used to initialize a limit | |
162 | * for the first time. | |
163 | * | |
164 | * The CACHE_*** macros are use to cache the limit that will | |
165 | * soon be in effect down in the ledgers. | |
166 | */ | |
167 | ||
0a7de745 A |
168 | #define SET_ACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ |
169 | MACRO_BEGIN \ | |
170 | (p)->p_memstat_memlimit_active = (limit); \ | |
171 | if (is_fatal) { \ | |
172 | (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ | |
173 | } else { \ | |
174 | (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ | |
175 | } \ | |
3e170ce0 A |
176 | MACRO_END |
177 | ||
0a7de745 A |
178 | #define SET_INACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ |
179 | MACRO_BEGIN \ | |
180 | (p)->p_memstat_memlimit_inactive = (limit); \ | |
181 | if (is_fatal) { \ | |
182 | (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ | |
183 | } else { \ | |
184 | (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ | |
185 | } \ | |
3e170ce0 A |
186 | MACRO_END |
187 | ||
0a7de745 A |
188 | #define CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal) \ |
189 | MACRO_BEGIN \ | |
190 | (p)->p_memstat_memlimit = (p)->p_memstat_memlimit_active; \ | |
191 | if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { \ | |
192 | (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ | |
193 | is_fatal = TRUE; \ | |
194 | } else { \ | |
195 | (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ | |
196 | is_fatal = FALSE; \ | |
197 | } \ | |
3e170ce0 A |
198 | MACRO_END |
199 | ||
0a7de745 A |
200 | #define CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal) \ |
201 | MACRO_BEGIN \ | |
202 | (p)->p_memstat_memlimit = (p)->p_memstat_memlimit_inactive; \ | |
203 | if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { \ | |
204 | (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ | |
205 | is_fatal = TRUE; \ | |
206 | } else { \ | |
207 | (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ | |
208 | is_fatal = FALSE; \ | |
209 | } \ | |
3e170ce0 A |
210 | MACRO_END |
211 | ||
212 | ||
39236c6e A |
213 | /* General tunables */ |
214 | ||
215 | unsigned long delta_percentage = 5; | |
216 | unsigned long critical_threshold_percentage = 5; | |
cb323159 A |
217 | // On embedded devices with more than 3GB of memory we lower the critical percentage. |
218 | uint64_t config_jetsam_large_memory_cutoff = 3UL * (1UL << 30); | |
219 | unsigned long critical_threshold_percentage_larger_devices = 4; | |
220 | unsigned long delta_percentage_larger_devices = 4; | |
39236c6e A |
221 | unsigned long idle_offset_percentage = 5; |
222 | unsigned long pressure_threshold_percentage = 15; | |
39037602 | 223 | unsigned long policy_more_free_offset_percentage = 5; |
cb323159 | 224 | unsigned long sysproc_aging_aggr_threshold_percentage = 7; |
39236c6e A |
225 | |
226 | /* | |
cb323159 | 227 | * default jetsam snapshot support |
39236c6e | 228 | */ |
cb323159 A |
229 | memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot; |
230 | memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy; | |
f427ee49 A |
231 | |
232 | #if CONFIG_FREEZE | |
233 | memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_freezer; | |
234 | /* | |
235 | * The size of the freezer snapshot is given by memorystatus_jetsam_snapshot_max / JETSAM_SNAPSHOT_FREEZER_MAX_FACTOR | |
236 | * The freezer snapshot can be much smaller than the default snapshot | |
237 | * because it only includes apps that have been killed and dasd consumes it every 30 minutes. | |
238 | * Since the snapshots are always wired we don't want to overallocate too much. | |
239 | */ | |
240 | #define JETSAM_SNAPSHOT_FREEZER_MAX_FACTOR 20 | |
241 | unsigned int memorystatus_jetsam_snapshot_freezer_max; | |
242 | unsigned int memorystatus_jetsam_snapshot_freezer_size; | |
243 | TUNABLE(bool, memorystatus_jetsam_use_freezer_snapshot, "kern.jetsam_user_freezer_snapshot", true); | |
244 | #endif /* CONFIG_FREEZE */ | |
245 | ||
cb323159 A |
246 | unsigned int memorystatus_jetsam_snapshot_count = 0; |
247 | unsigned int memorystatus_jetsam_snapshot_copy_count = 0; | |
248 | unsigned int memorystatus_jetsam_snapshot_max = 0; | |
249 | unsigned int memorystatus_jetsam_snapshot_size = 0; | |
250 | uint64_t memorystatus_jetsam_snapshot_last_timestamp = 0; | |
251 | uint64_t memorystatus_jetsam_snapshot_timeout = 0; | |
39236c6e | 252 | |
f427ee49 A |
253 | #if DEVELOPMENT || DEBUG |
254 | /* | |
255 | * On development and debug kernels, we allow one pid to take ownership | |
256 | * of the memorystatus snapshot (via memorystatus_control). | |
257 | * If there's an owner, then only they may consume the snapshot. | |
258 | * This is used when testing the snapshot interface to avoid racing with other | |
259 | * processes on the system that consume snapshots. | |
260 | */ | |
261 | static pid_t memorystatus_snapshot_owner = 0; | |
262 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_snapshot_owner, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_snapshot_owner, 0, ""); | |
263 | #endif /* DEVELOPMENT || DEBUG */ | |
264 | static void memorystatus_init_jetsam_snapshot_header(memorystatus_jetsam_snapshot_t *snapshot); | |
265 | ||
cb323159 | 266 | /* General memorystatus stuff */ |
39236c6e | 267 | |
cb323159 A |
268 | uint64_t memorystatus_sysprocs_idle_delay_time = 0; |
269 | uint64_t memorystatus_apps_idle_delay_time = 0; | |
f427ee49 A |
270 | /* Some devices give entitled apps a higher memory limit */ |
271 | #if __arm64__ | |
272 | int32_t memorystatus_entitled_max_task_footprint_mb = 0; | |
273 | ||
274 | #if DEVELOPMENT || DEBUG | |
275 | SYSCTL_INT(_kern, OID_AUTO, entitled_max_task_pmem, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_entitled_max_task_footprint_mb, 0, ""); | |
276 | #endif /* DEVELOPMENT || DEBUG */ | |
277 | #endif /* __arm64__ */ | |
cb323159 A |
278 | |
279 | static lck_grp_attr_t *memorystatus_jetsam_fg_band_lock_grp_attr; | |
280 | static lck_grp_t *memorystatus_jetsam_fg_band_lock_grp; | |
281 | lck_mtx_t memorystatus_jetsam_fg_band_lock; | |
39236c6e A |
282 | |
283 | /* Idle guard handling */ | |
284 | ||
39037602 A |
285 | static int32_t memorystatus_scheduled_idle_demotions_sysprocs = 0; |
286 | static int32_t memorystatus_scheduled_idle_demotions_apps = 0; | |
39236c6e | 287 | |
39236c6e A |
288 | static void memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2); |
289 | static void memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state); | |
39236c6e | 290 | static void memorystatus_reschedule_idle_demotion_locked(void); |
5ba3f43e | 291 | int memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap); |
39037602 | 292 | vm_pressure_level_t convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t); |
fe8ab488 | 293 | boolean_t is_knote_registered_modify_task_pressure_bits(struct knote*, int, task_t, vm_pressure_level_t, vm_pressure_level_t); |
39037602 | 294 | void memorystatus_klist_reset_all_for_level(vm_pressure_level_t pressure_level_to_clear); |
fe8ab488 | 295 | void memorystatus_send_low_swap_note(void); |
cb323159 A |
296 | int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index); |
297 | boolean_t memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, | |
298 | uint32_t *errors, uint64_t *memory_reclaimed); | |
299 | uint64_t memorystatus_available_memory_internal(proc_t p); | |
39236c6e | 300 | |
39236c6e | 301 | unsigned int memorystatus_level = 0; |
316670eb | 302 | static int memorystatus_list_count = 0; |
39236c6e | 303 | memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT]; |
cb323159 | 304 | static thread_call_t memorystatus_idle_demotion_call; |
39236c6e | 305 | uint64_t memstat_idle_demotion_deadline = 0; |
39037602 A |
306 | int system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; |
307 | int applications_aging_band = JETSAM_PRIORITY_IDLE; | |
308 | ||
0a7de745 | 309 | #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))) |
d9a64523 | 310 | |
0a7de745 A |
311 | #define kJetsamAgingPolicyNone (0) |
312 | #define kJetsamAgingPolicyLegacy (1) | |
313 | #define kJetsamAgingPolicySysProcsReclaimedFirst (2) | |
314 | #define kJetsamAgingPolicyAppsReclaimedFirst (3) | |
315 | #define kJetsamAgingPolicyMax kJetsamAgingPolicyAppsReclaimedFirst | |
39037602 | 316 | |
cb323159 | 317 | unsigned int jetsam_aging_policy = kJetsamAgingPolicySysProcsReclaimedFirst; |
39037602 A |
318 | |
319 | extern int corpse_for_fatal_memkill; | |
a39ff7e2 A |
320 | extern uint64_t vm_purgeable_purge_task_owned(task_t task); |
321 | boolean_t memorystatus_allowed_vm_map_fork(task_t); | |
322 | #if DEVELOPMENT || DEBUG | |
323 | void memorystatus_abort_vm_map_fork(task_t); | |
324 | #endif | |
39037602 | 325 | |
cb323159 A |
326 | /* |
327 | * Idle delay timeout factors for daemons based on relaunch behavior. Only used in | |
328 | * kJetsamAgingPolicySysProcsReclaimedFirst aging policy. | |
329 | */ | |
330 | #define kJetsamSysProcsIdleDelayTimeLowRatio (5) | |
331 | #define kJetsamSysProcsIdleDelayTimeMedRatio (2) | |
332 | #define kJetsamSysProcsIdleDelayTimeHighRatio (1) | |
333 | static_assert(kJetsamSysProcsIdleDelayTimeLowRatio <= DEFERRED_IDLE_EXIT_TIME_SECS, "sysproc idle delay time for low relaunch daemons would be 0"); | |
334 | ||
335 | /* | |
336 | * For the kJetsamAgingPolicySysProcsReclaimedFirst aging policy, treat apps as well | |
337 | * behaved daemons for aging purposes. | |
338 | */ | |
339 | #define kJetsamAppsIdleDelayTimeRatio (kJetsamSysProcsIdleDelayTimeLowRatio) | |
340 | ||
341 | static uint64_t | |
342 | memorystatus_sysprocs_idle_time(proc_t p) | |
343 | { | |
344 | /* | |
345 | * The kJetsamAgingPolicySysProcsReclaimedFirst aging policy uses the relaunch behavior to | |
346 | * determine the exact idle deferred time provided to the daemons. For all other aging | |
347 | * policies, simply return the default aging idle time. | |
348 | */ | |
349 | if (jetsam_aging_policy != kJetsamAgingPolicySysProcsReclaimedFirst) { | |
350 | return memorystatus_sysprocs_idle_delay_time; | |
351 | } | |
352 | ||
353 | uint64_t idle_delay_time = 0; | |
354 | /* | |
355 | * For system processes, base the idle delay time on the | |
356 | * jetsam relaunch behavior specified by launchd. The idea | |
357 | * is to provide extra protection to the daemons which would | |
358 | * relaunch immediately after jetsam. | |
359 | */ | |
360 | switch (p->p_memstat_relaunch_flags) { | |
361 | case P_MEMSTAT_RELAUNCH_UNKNOWN: | |
362 | case P_MEMSTAT_RELAUNCH_LOW: | |
363 | idle_delay_time = memorystatus_sysprocs_idle_delay_time / kJetsamSysProcsIdleDelayTimeLowRatio; | |
364 | break; | |
365 | case P_MEMSTAT_RELAUNCH_MED: | |
366 | idle_delay_time = memorystatus_sysprocs_idle_delay_time / kJetsamSysProcsIdleDelayTimeMedRatio; | |
367 | break; | |
368 | case P_MEMSTAT_RELAUNCH_HIGH: | |
369 | idle_delay_time = memorystatus_sysprocs_idle_delay_time / kJetsamSysProcsIdleDelayTimeHighRatio; | |
370 | break; | |
371 | default: | |
372 | panic("Unknown relaunch flags on process!"); | |
373 | break; | |
374 | } | |
375 | return idle_delay_time; | |
376 | } | |
377 | ||
378 | static uint64_t | |
379 | memorystatus_apps_idle_time(__unused proc_t p) | |
380 | { | |
381 | /* | |
382 | * For kJetsamAgingPolicySysProcsReclaimedFirst, the Apps are considered as low | |
383 | * relaunch candidates. So only provide limited protection to them. In the other | |
384 | * aging policies, return the default aging idle time. | |
385 | */ | |
386 | if (jetsam_aging_policy != kJetsamAgingPolicySysProcsReclaimedFirst) { | |
387 | return memorystatus_apps_idle_delay_time; | |
388 | } | |
389 | ||
390 | return memorystatus_apps_idle_delay_time / kJetsamAppsIdleDelayTimeRatio; | |
391 | } | |
392 | ||
393 | ||
39037602 A |
394 | #if 0 |
395 | ||
396 | /* Keeping around for future use if we need a utility that can do this OR an app that needs a dynamic adjustment. */ | |
397 | ||
398 | static int | |
399 | sysctl_set_jetsam_aging_policy SYSCTL_HANDLER_ARGS | |
400 | { | |
401 | #pragma unused(oidp, arg1, arg2) | |
402 | ||
403 | int error = 0, val = 0; | |
404 | memstat_bucket_t *old_bucket = 0; | |
405 | int old_system_procs_aging_band = 0, new_system_procs_aging_band = 0; | |
406 | int old_applications_aging_band = 0, new_applications_aging_band = 0; | |
407 | proc_t p = NULL, next_proc = NULL; | |
408 | ||
409 | ||
410 | error = sysctl_io_number(req, jetsam_aging_policy, sizeof(int), &val, NULL); | |
411 | if (error || !req->newptr) { | |
0a7de745 | 412 | return error; |
39037602 A |
413 | } |
414 | ||
415 | if ((val < 0) || (val > kJetsamAgingPolicyMax)) { | |
416 | printf("jetsam: ordering policy sysctl has invalid value - %d\n", val); | |
417 | return EINVAL; | |
418 | } | |
419 | ||
420 | /* | |
421 | * We need to synchronize with any potential adding/removal from aging bands | |
422 | * that might be in progress currently. We use the proc_list_lock() just for | |
423 | * consistency with all the routines dealing with 'aging' processes. We need | |
424 | * a lighterweight lock. | |
0a7de745 | 425 | */ |
39037602 A |
426 | proc_list_lock(); |
427 | ||
428 | old_system_procs_aging_band = system_procs_aging_band; | |
429 | old_applications_aging_band = applications_aging_band; | |
39037602 | 430 | |
0a7de745 A |
431 | switch (val) { |
432 | case kJetsamAgingPolicyNone: | |
433 | new_system_procs_aging_band = JETSAM_PRIORITY_IDLE; | |
434 | new_applications_aging_band = JETSAM_PRIORITY_IDLE; | |
435 | break; | |
39037602 | 436 | |
0a7de745 A |
437 | case kJetsamAgingPolicyLegacy: |
438 | /* | |
439 | * Legacy behavior where some daemons get a 10s protection once and only before the first clean->dirty->clean transition before going into IDLE band. | |
440 | */ | |
441 | new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
442 | new_applications_aging_band = JETSAM_PRIORITY_IDLE; | |
443 | break; | |
39037602 | 444 | |
0a7de745 A |
445 | case kJetsamAgingPolicySysProcsReclaimedFirst: |
446 | new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
447 | new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
448 | break; | |
39037602 | 449 | |
0a7de745 A |
450 | case kJetsamAgingPolicyAppsReclaimedFirst: |
451 | new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
452 | new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
453 | break; | |
39037602 | 454 | |
0a7de745 A |
455 | default: |
456 | break; | |
39037602 A |
457 | } |
458 | ||
459 | if (old_system_procs_aging_band && (old_system_procs_aging_band != new_system_procs_aging_band)) { | |
39037602 A |
460 | old_bucket = &memstat_bucket[old_system_procs_aging_band]; |
461 | p = TAILQ_FIRST(&old_bucket->list); | |
0a7de745 | 462 | |
39037602 | 463 | while (p) { |
39037602 A |
464 | next_proc = TAILQ_NEXT(p, p_memstat_list); |
465 | ||
466 | if (isSysProc(p)) { | |
467 | if (new_system_procs_aging_band == JETSAM_PRIORITY_IDLE) { | |
468 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
469 | } | |
470 | ||
471 | memorystatus_update_priority_locked(p, new_system_procs_aging_band, false, true); | |
472 | } | |
473 | ||
474 | p = next_proc; | |
475 | continue; | |
476 | } | |
477 | } | |
478 | ||
479 | if (old_applications_aging_band && (old_applications_aging_band != new_applications_aging_band)) { | |
39037602 A |
480 | old_bucket = &memstat_bucket[old_applications_aging_band]; |
481 | p = TAILQ_FIRST(&old_bucket->list); | |
39037602 | 482 | |
0a7de745 | 483 | while (p) { |
39037602 A |
484 | next_proc = TAILQ_NEXT(p, p_memstat_list); |
485 | ||
486 | if (isApp(p)) { | |
487 | if (new_applications_aging_band == JETSAM_PRIORITY_IDLE) { | |
488 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
489 | } | |
490 | ||
491 | memorystatus_update_priority_locked(p, new_applications_aging_band, false, true); | |
492 | } | |
493 | ||
494 | p = next_proc; | |
495 | continue; | |
496 | } | |
497 | } | |
498 | ||
499 | jetsam_aging_policy = val; | |
500 | system_procs_aging_band = new_system_procs_aging_band; | |
501 | applications_aging_band = new_applications_aging_band; | |
502 | ||
503 | proc_list_unlock(); | |
504 | ||
0a7de745 | 505 | return 0; |
39037602 A |
506 | } |
507 | ||
0a7de745 A |
508 | SYSCTL_PROC(_kern, OID_AUTO, set_jetsam_aging_policy, CTLTYPE_INT | CTLFLAG_RW, |
509 | 0, 0, sysctl_set_jetsam_aging_policy, "I", "Jetsam Aging Policy"); | |
39037602 A |
510 | #endif /*0*/ |
511 | ||
512 | static int | |
513 | sysctl_jetsam_set_sysprocs_idle_delay_time SYSCTL_HANDLER_ARGS | |
514 | { | |
515 | #pragma unused(oidp, arg1, arg2) | |
516 | ||
517 | int error = 0, val = 0, old_time_in_secs = 0; | |
518 | uint64_t old_time_in_ns = 0; | |
519 | ||
520 | absolutetime_to_nanoseconds(memorystatus_sysprocs_idle_delay_time, &old_time_in_ns); | |
f427ee49 | 521 | old_time_in_secs = (int) (old_time_in_ns / NSEC_PER_SEC); |
39037602 A |
522 | |
523 | error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL); | |
524 | if (error || !req->newptr) { | |
0a7de745 | 525 | return error; |
39037602 A |
526 | } |
527 | ||
528 | if ((val < 0) || (val > INT32_MAX)) { | |
529 | printf("jetsam: new idle delay interval has invalid value.\n"); | |
530 | return EINVAL; | |
531 | } | |
532 | ||
533 | nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); | |
0a7de745 A |
534 | |
535 | return 0; | |
39037602 A |
536 | } |
537 | ||
0a7de745 A |
538 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_sysprocs_idle_delay_time, CTLTYPE_INT | CTLFLAG_RW, |
539 | 0, 0, sysctl_jetsam_set_sysprocs_idle_delay_time, "I", "Aging window for system processes"); | |
39037602 A |
540 | |
541 | ||
542 | static int | |
543 | sysctl_jetsam_set_apps_idle_delay_time SYSCTL_HANDLER_ARGS | |
544 | { | |
545 | #pragma unused(oidp, arg1, arg2) | |
546 | ||
547 | int error = 0, val = 0, old_time_in_secs = 0; | |
548 | uint64_t old_time_in_ns = 0; | |
549 | ||
550 | absolutetime_to_nanoseconds(memorystatus_apps_idle_delay_time, &old_time_in_ns); | |
f427ee49 | 551 | old_time_in_secs = (int) (old_time_in_ns / NSEC_PER_SEC); |
39037602 A |
552 | |
553 | error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL); | |
554 | if (error || !req->newptr) { | |
0a7de745 | 555 | return error; |
39037602 A |
556 | } |
557 | ||
558 | if ((val < 0) || (val > INT32_MAX)) { | |
559 | printf("jetsam: new idle delay interval has invalid value.\n"); | |
560 | return EINVAL; | |
561 | } | |
562 | ||
563 | nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); | |
0a7de745 A |
564 | |
565 | return 0; | |
39037602 A |
566 | } |
567 | ||
0a7de745 A |
568 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_apps_idle_delay_time, CTLTYPE_INT | CTLFLAG_RW, |
569 | 0, 0, sysctl_jetsam_set_apps_idle_delay_time, "I", "Aging window for applications"); | |
39037602 | 570 | |
0a7de745 | 571 | SYSCTL_INT(_kern, OID_AUTO, jetsam_aging_policy, CTLTYPE_INT | CTLFLAG_RD, &jetsam_aging_policy, 0, ""); |
39037602 | 572 | |
316670eb | 573 | static unsigned int memorystatus_dirty_count = 0; |
6d2010ae | 574 | |
0a7de745 | 575 | SYSCTL_INT(_kern, OID_AUTO, max_task_pmem, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED, &max_task_footprint_mb, 0, ""); |
3e170ce0 | 576 | |
cb323159 A |
577 | static int memorystatus_highwater_enabled = 1; /* Update the cached memlimit data. */ |
578 | static boolean_t proc_jetsam_state_is_active_locked(proc_t); | |
579 | ||
580 | #if __arm64__ | |
cb323159 A |
581 | int legacy_footprint_bonus_mb = 50; /* This value was chosen after looking at the top 30 apps |
582 | * that needed the additional room in their footprint when | |
583 | * the 'correct' accounting methods were applied to them. | |
584 | */ | |
585 | ||
586 | #if DEVELOPMENT || DEBUG | |
587 | SYSCTL_INT(_kern, OID_AUTO, legacy_footprint_bonus_mb, CTLFLAG_RW | CTLFLAG_LOCKED, &legacy_footprint_bonus_mb, 0, ""); | |
588 | #endif /* DEVELOPMENT || DEBUG */ | |
f427ee49 A |
589 | /* |
590 | * Raise the inactive and active memory limits to new values. | |
591 | * Will only raise the limits and will do nothing if either of the current | |
592 | * limits are 0. | |
593 | * Caller must hold the proc_list_lock | |
594 | */ | |
595 | static void | |
596 | memorystatus_raise_memlimit(proc_t p, int new_memlimit_active, int new_memlimit_inactive) | |
cb323159 A |
597 | { |
598 | int memlimit_mb_active = 0, memlimit_mb_inactive = 0; | |
f427ee49 | 599 | boolean_t memlimit_active_is_fatal = FALSE, memlimit_inactive_is_fatal = FALSE, use_active_limit = FALSE; |
cb323159 | 600 | |
f427ee49 | 601 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); |
cb323159 A |
602 | |
603 | if (p->p_memstat_memlimit_active > 0) { | |
604 | memlimit_mb_active = p->p_memstat_memlimit_active; | |
605 | } else if (p->p_memstat_memlimit_active == -1) { | |
606 | memlimit_mb_active = max_task_footprint_mb; | |
607 | } else { | |
608 | /* | |
609 | * Nothing to do for '0' which is | |
610 | * a special value only used internally | |
611 | * to test 'no limits'. | |
612 | */ | |
cb323159 A |
613 | return; |
614 | } | |
615 | ||
616 | if (p->p_memstat_memlimit_inactive > 0) { | |
617 | memlimit_mb_inactive = p->p_memstat_memlimit_inactive; | |
618 | } else if (p->p_memstat_memlimit_inactive == -1) { | |
619 | memlimit_mb_inactive = max_task_footprint_mb; | |
620 | } else { | |
621 | /* | |
622 | * Nothing to do for '0' which is | |
623 | * a special value only used internally | |
624 | * to test 'no limits'. | |
625 | */ | |
cb323159 A |
626 | return; |
627 | } | |
628 | ||
f427ee49 A |
629 | memlimit_mb_active = MAX(new_memlimit_active, memlimit_mb_active); |
630 | memlimit_mb_inactive = MAX(new_memlimit_inactive, memlimit_mb_inactive); | |
cb323159 A |
631 | |
632 | memlimit_active_is_fatal = (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL); | |
633 | memlimit_inactive_is_fatal = (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL); | |
634 | ||
635 | SET_ACTIVE_LIMITS_LOCKED(p, memlimit_mb_active, memlimit_active_is_fatal); | |
636 | SET_INACTIVE_LIMITS_LOCKED(p, memlimit_mb_inactive, memlimit_inactive_is_fatal); | |
637 | ||
638 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { | |
639 | use_active_limit = TRUE; | |
640 | CACHE_ACTIVE_LIMITS_LOCKED(p, memlimit_active_is_fatal); | |
641 | } else { | |
642 | CACHE_INACTIVE_LIMITS_LOCKED(p, memlimit_inactive_is_fatal); | |
643 | } | |
644 | ||
cb323159 A |
645 | if (memorystatus_highwater_enabled) { |
646 | task_set_phys_footprint_limit_internal(p->task, | |
647 | (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, | |
648 | NULL, /*return old value */ | |
649 | use_active_limit, /*active limit?*/ | |
650 | (use_active_limit ? memlimit_active_is_fatal : memlimit_inactive_is_fatal)); | |
651 | } | |
cb323159 A |
652 | } |
653 | ||
94ff46dc | 654 | void |
f427ee49 | 655 | memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase) |
94ff46dc A |
656 | { |
657 | int memlimit_mb_active = 0, memlimit_mb_inactive = 0; | |
94ff46dc | 658 | |
f427ee49 | 659 | if (p == NULL) { |
94ff46dc A |
660 | return; |
661 | } | |
662 | ||
663 | proc_list_lock(); | |
664 | ||
665 | if (p->p_memstat_memlimit_active > 0) { | |
666 | memlimit_mb_active = p->p_memstat_memlimit_active; | |
667 | } else if (p->p_memstat_memlimit_active == -1) { | |
668 | memlimit_mb_active = max_task_footprint_mb; | |
669 | } else { | |
670 | /* | |
671 | * Nothing to do for '0' which is | |
672 | * a special value only used internally | |
673 | * to test 'no limits'. | |
674 | */ | |
675 | proc_list_unlock(); | |
676 | return; | |
677 | } | |
678 | ||
679 | if (p->p_memstat_memlimit_inactive > 0) { | |
680 | memlimit_mb_inactive = p->p_memstat_memlimit_inactive; | |
681 | } else if (p->p_memstat_memlimit_inactive == -1) { | |
682 | memlimit_mb_inactive = max_task_footprint_mb; | |
683 | } else { | |
684 | /* | |
685 | * Nothing to do for '0' which is | |
686 | * a special value only used internally | |
687 | * to test 'no limits'. | |
688 | */ | |
689 | proc_list_unlock(); | |
690 | return; | |
691 | } | |
692 | ||
f427ee49 A |
693 | if (footprint_increase) { |
694 | memlimit_mb_active += legacy_footprint_bonus_mb; | |
695 | memlimit_mb_inactive += legacy_footprint_bonus_mb; | |
94ff46dc | 696 | } else { |
f427ee49 A |
697 | memlimit_mb_active -= legacy_footprint_bonus_mb; |
698 | if (memlimit_mb_active == max_task_footprint_mb) { | |
699 | memlimit_mb_active = -1; /* reverting back to default system limit */ | |
700 | } | |
701 | ||
702 | memlimit_mb_inactive -= legacy_footprint_bonus_mb; | |
703 | if (memlimit_mb_inactive == max_task_footprint_mb) { | |
704 | memlimit_mb_inactive = -1; /* reverting back to default system limit */ | |
705 | } | |
94ff46dc | 706 | } |
f427ee49 | 707 | memorystatus_raise_memlimit(p, memlimit_mb_active, memlimit_mb_inactive); |
94ff46dc | 708 | |
f427ee49 A |
709 | proc_list_unlock(); |
710 | } | |
94ff46dc | 711 | |
f427ee49 A |
712 | void |
713 | memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p) | |
714 | { | |
715 | if (max_mem < 1500ULL * 1024 * 1024 || | |
716 | max_mem > 2ULL * 1024 * 1024 * 1024) { | |
717 | /* ios13extended_footprint is only for 2GB devices */ | |
718 | return; | |
94ff46dc | 719 | } |
f427ee49 A |
720 | /* limit to "almost 2GB" */ |
721 | proc_list_lock(); | |
722 | memorystatus_raise_memlimit(p, 1800, 1800); | |
94ff46dc A |
723 | proc_list_unlock(); |
724 | } | |
725 | ||
f427ee49 A |
726 | void |
727 | memorystatus_act_on_entitled_task_limit(proc_t p) | |
728 | { | |
729 | if (memorystatus_entitled_max_task_footprint_mb == 0) { | |
730 | // Entitlement is not supported on this device. | |
731 | return; | |
732 | } | |
733 | proc_list_lock(); | |
734 | memorystatus_raise_memlimit(p, memorystatus_entitled_max_task_footprint_mb, memorystatus_entitled_max_task_footprint_mb); | |
735 | proc_list_unlock(); | |
736 | } | |
cb323159 A |
737 | #endif /* __arm64__ */ |
738 | ||
0a7de745 | 739 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_level, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_level, 0, ""); |
5ba3f43e | 740 | |
39236c6e A |
741 | int |
742 | memorystatus_get_level(__unused struct proc *p, struct memorystatus_get_level_args *args, __unused int *ret) | |
743 | { | |
0a7de745 A |
744 | user_addr_t level = 0; |
745 | ||
39236c6e | 746 | level = args->level; |
0a7de745 | 747 | |
39236c6e A |
748 | if (copyout(&memorystatus_level, level, sizeof(memorystatus_level)) != 0) { |
749 | return EFAULT; | |
750 | } | |
0a7de745 | 751 | |
39236c6e A |
752 | return 0; |
753 | } | |
754 | ||
39236c6e | 755 | static void memorystatus_thread(void *param __unused, wait_result_t wr __unused); |
6d2010ae | 756 | |
39037602 A |
757 | /* Memory Limits */ |
758 | ||
39037602 A |
759 | static boolean_t memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason); |
760 | static boolean_t memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason); | |
761 | ||
762 | ||
3e170ce0 A |
763 | static int memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval); |
764 | ||
765 | static int memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry); | |
766 | ||
767 | static int memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval); | |
768 | ||
39037602 | 769 | 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); |
3e170ce0 | 770 | |
cb323159 A |
771 | static void memorystatus_get_memlimit_properties_internal(proc_t p, memorystatus_memlimit_properties_t *p_entry); |
772 | static int memorystatus_set_memlimit_properties_internal(proc_t p, memorystatus_memlimit_properties_t *p_entry); | |
773 | ||
fe8ab488 A |
774 | int proc_get_memstat_priority(proc_t, boolean_t); |
775 | ||
fe8ab488 | 776 | static boolean_t memorystatus_idle_snapshot = 0; |
39236c6e | 777 | |
316670eb A |
778 | unsigned int memorystatus_delta = 0; |
779 | ||
3e170ce0 | 780 | /* Jetsam Loop Detection */ |
0a7de745 A |
781 | static boolean_t memorystatus_jld_enabled = FALSE; /* Enable jetsam loop detection */ |
782 | static uint32_t memorystatus_jld_eval_period_msecs = 0; /* Init pass sets this based on device memory size */ | |
783 | static int memorystatus_jld_eval_aggressive_count = 3; /* Raise the priority max after 'n' aggressive loops */ | |
3e170ce0 A |
784 | static int memorystatus_jld_eval_aggressive_priority_band_max = 15; /* Kill aggressively up through this band */ |
785 | ||
490019cf A |
786 | /* |
787 | * A FG app can request that the aggressive jetsam mechanism display some leniency in the FG band. This 'lenient' mode is described as: | |
788 | * --- if aggressive jetsam kills an app in the FG band and gets back >=AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD memory, it will stop the aggressive march further into and up the jetsam bands. | |
789 | * | |
790 | * RESTRICTIONS: | |
791 | * - Such a request is respected/acknowledged only once while that 'requesting' app is in the FG band i.e. if aggressive jetsam was | |
0a7de745 | 792 | * needed and the 'lenient' mode was deployed then that's it for this special mode while the app is in the FG band. |
490019cf A |
793 | * |
794 | * - 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. | |
795 | * | |
796 | * - Also, the transition of the 'requesting' app away from the FG band will void this special behavior. | |
797 | */ | |
798 | ||
0a7de745 A |
799 | #define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD 25 |
800 | boolean_t memorystatus_aggressive_jetsam_lenient_allowed = FALSE; | |
801 | boolean_t memorystatus_aggressive_jetsam_lenient = FALSE; | |
490019cf | 802 | |
3e170ce0 | 803 | #if DEVELOPMENT || DEBUG |
0a7de745 | 804 | /* |
3e170ce0 A |
805 | * Jetsam Loop Detection tunables. |
806 | */ | |
807 | ||
0a7de745 A |
808 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_period_msecs, 0, ""); |
809 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_count, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_count, 0, ""); | |
810 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_priority_band_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_priority_band_max, 0, ""); | |
3e170ce0 A |
811 | #endif /* DEVELOPMENT || DEBUG */ |
812 | ||
fe8ab488 | 813 | static uint32_t kill_under_pressure_cause = 0; |
316670eb | 814 | |
3e170ce0 A |
815 | /* |
816 | * snapshot support for memstats collected at boot. | |
817 | */ | |
818 | static memorystatus_jetsam_snapshot_t memorystatus_at_boot_snapshot; | |
316670eb | 819 | |
39037602 A |
820 | static void memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count); |
821 | static boolean_t memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount); | |
822 | static void memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime); | |
823 | ||
39236c6e | 824 | static void memorystatus_clear_errors(void); |
39037602 | 825 | static void memorystatus_get_task_phys_footprint_page_counts(task_t task, |
0a7de745 A |
826 | uint64_t *internal_pages, uint64_t *internal_compressed_pages, |
827 | uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, | |
828 | uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, | |
f427ee49 | 829 | uint64_t *iokit_mapped_pages, uint64_t *page_table_pages, uint64_t *frozen_to_swap_pages); |
39037602 A |
830 | |
831 | static void memorystatus_get_task_memory_region_count(task_t task, uint64_t *count); | |
832 | ||
39236c6e | 833 | static uint32_t memorystatus_build_state(proc_t p); |
fe8ab488 | 834 | //static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured); |
39236c6e | 835 | |
cb323159 A |
836 | 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, |
837 | uint32_t *errors, uint64_t *memory_reclaimed); | |
838 | static boolean_t memorystatus_kill_processes_aggressive(uint32_t cause, int aggr_count, int32_t priority_max, uint32_t *errors, uint64_t *memory_reclaimed); | |
839 | static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged, uint64_t *memory_reclaimed); | |
39236c6e A |
840 | |
841 | static boolean_t memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause); | |
316670eb | 842 | |
3e170ce0 A |
843 | /* Priority Band Sorting Routines */ |
844 | static int memorystatus_sort_bucket(unsigned int bucket_index, int sort_order); | |
845 | static int memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order); | |
846 | static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index); | |
847 | static int memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int list_sz); | |
848 | ||
849 | /* qsort routines */ | |
850 | typedef int (*cmpfunc_t)(const void *a, const void *b); | |
851 | extern void qsort(void *a, size_t n, size_t es, cmpfunc_t cmp); | |
852 | static int memstat_asc_cmp(const void *a, const void *b); | |
853 | ||
316670eb | 854 | /* VM pressure */ |
6d2010ae | 855 | |
fe8ab488 A |
856 | extern unsigned int vm_page_free_count; |
857 | extern unsigned int vm_page_active_count; | |
858 | extern unsigned int vm_page_inactive_count; | |
859 | extern unsigned int vm_page_throttled_count; | |
860 | extern unsigned int vm_page_purgeable_count; | |
861 | extern unsigned int vm_page_wire_count; | |
f427ee49 A |
862 | extern unsigned int vm_page_speculative_count; |
863 | ||
864 | #if CONFIG_JETSAM | |
865 | #define MEMORYSTATUS_LOG_AVAILABLE_PAGES memorystatus_available_pages | |
866 | #else /* CONFIG_JETSAM */ | |
867 | #define MEMORYSTATUS_LOG_AVAILABLE_PAGES (vm_page_active_count + vm_page_inactive_count + vm_page_free_count + vm_page_speculative_count) | |
868 | #endif /* CONFIG_JETSAM */ | |
39037602 | 869 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 870 | extern unsigned int vm_page_secluded_count; |
cb323159 | 871 | extern unsigned int vm_page_secluded_count_over_target; |
39037602 | 872 | #endif /* CONFIG_SECLUDED_MEMORY */ |
fe8ab488 | 873 | |
cb323159 A |
874 | /* Aggressive jetsam pages threshold for sysproc aging policy */ |
875 | unsigned int memorystatus_sysproc_aging_aggr_pages = 0; | |
876 | ||
5ba3f43e | 877 | #if CONFIG_JETSAM |
fe8ab488 A |
878 | unsigned int memorystatus_available_pages = (unsigned int)-1; |
879 | unsigned int memorystatus_available_pages_pressure = 0; | |
880 | unsigned int memorystatus_available_pages_critical = 0; | |
cb323159 A |
881 | unsigned int memorystatus_available_pages_critical_base = 0; |
882 | unsigned int memorystatus_available_pages_critical_idle_offset = 0; | |
fe8ab488 | 883 | |
00867663 A |
884 | #if DEVELOPMENT || DEBUG |
885 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); | |
886 | #else | |
5ba3f43e | 887 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); |
00867663 | 888 | #endif /* DEVELOPMENT || DEBUG */ |
5ba3f43e A |
889 | |
890 | static unsigned int memorystatus_jetsam_policy = kPolicyDefault; | |
891 | unsigned int memorystatus_policy_more_free_offset_pages = 0; | |
892 | static void memorystatus_update_levels_locked(boolean_t critical_only); | |
893 | static unsigned int memorystatus_thread_wasted_wakeup = 0; | |
894 | ||
895 | /* Callback into vm_compressor.c to signal that thrashing has been mitigated. */ | |
896 | extern void vm_thrashing_jetsam_done(void); | |
897 | 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); | |
cb323159 A |
898 | #if DEVELOPMENT || DEBUG |
899 | static inline uint32_t | |
900 | roundToNearestMB(uint32_t in) | |
901 | { | |
902 | return (in + ((1 << 20) - 1)) >> 20; | |
903 | } | |
904 | ||
905 | static int memorystatus_cmd_increase_jetsam_task_limit(pid_t pid, uint32_t byte_increase); | |
906 | #endif | |
5ba3f43e A |
907 | |
908 | int32_t max_kill_priority = JETSAM_PRIORITY_MAX; | |
909 | ||
910 | #else /* CONFIG_JETSAM */ | |
911 | ||
912 | uint64_t memorystatus_available_pages = (uint64_t)-1; | |
913 | uint64_t memorystatus_available_pages_pressure = (uint64_t)-1; | |
914 | uint64_t memorystatus_available_pages_critical = (uint64_t)-1; | |
915 | ||
916 | int32_t max_kill_priority = JETSAM_PRIORITY_IDLE; | |
00867663 A |
917 | #endif /* CONFIG_JETSAM */ |
918 | ||
39037602 A |
919 | #if DEVELOPMENT || DEBUG |
920 | ||
921 | lck_grp_attr_t *disconnect_page_mappings_lck_grp_attr; | |
922 | lck_grp_t *disconnect_page_mappings_lck_grp; | |
923 | static lck_mtx_t disconnect_page_mappings_mutex; | |
924 | ||
f427ee49 | 925 | extern bool kill_on_no_paging_space; |
5ba3f43e | 926 | #endif /* DEVELOPMENT || DEBUG */ |
39037602 A |
927 | |
928 | ||
316670eb | 929 | /* Debug */ |
6d2010ae | 930 | |
fe8ab488 A |
931 | extern struct knote *vm_find_knote_from_pid(pid_t, struct klist *); |
932 | ||
6d2010ae | 933 | #if DEVELOPMENT || DEBUG |
6d2010ae | 934 | |
39037602 | 935 | static unsigned int memorystatus_debug_dump_this_bucket = 0; |
39236c6e | 936 | |
3e170ce0 | 937 | static void |
0a7de745 | 938 | memorystatus_debug_dump_bucket_locked(unsigned int bucket_index) |
3e170ce0 A |
939 | { |
940 | proc_t p = NULL; | |
39037602 A |
941 | uint64_t bytes = 0; |
942 | int ledger_limit = 0; | |
3e170ce0 A |
943 | unsigned int b = bucket_index; |
944 | boolean_t traverse_all_buckets = FALSE; | |
945 | ||
0a7de745 | 946 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
3e170ce0 A |
947 | traverse_all_buckets = TRUE; |
948 | b = 0; | |
0a7de745 | 949 | } else { |
3e170ce0 A |
950 | traverse_all_buckets = FALSE; |
951 | b = bucket_index; | |
952 | } | |
953 | ||
954 | /* | |
39037602 A |
955 | * footprint reported in [pages / MB ] |
956 | * limits reported as: | |
957 | * L-limit proc's Ledger limit | |
958 | * C-limit proc's Cached limit, should match Ledger | |
959 | * A-limit proc's Active limit | |
960 | * IA-limit proc's Inactive limit | |
961 | * F==Fatal, NF==NonFatal | |
3e170ce0 | 962 | */ |
39037602 | 963 | |
0a7de745 | 964 | printf("memorystatus_debug_dump ***START*(PAGE_SIZE_64=%llu)**\n", PAGE_SIZE_64); |
cb323159 | 965 | printf("bucket [pid] [pages / MB] [state] [EP / RP / AP] dirty deadline [L-limit / C-limit / A-limit / IA-limit] name\n"); |
3e170ce0 A |
966 | p = memorystatus_get_first_proc_locked(&b, traverse_all_buckets); |
967 | while (p) { | |
39037602 A |
968 | bytes = get_task_phys_footprint(p->task); |
969 | task_get_phys_footprint_limit(p->task, &ledger_limit); | |
cb323159 | 970 | printf("%2d [%5d] [%5lld /%3lldMB] 0x%-8x [%2d / %2d / %2d] 0x%-3x %10lld [%3d / %3d%s / %3d%s / %3d%s] %s\n", |
0a7de745 A |
971 | b, p->p_pid, |
972 | (bytes / PAGE_SIZE_64), /* task's footprint converted from bytes to pages */ | |
973 | (bytes / (1024ULL * 1024ULL)), /* task's footprint converted from bytes to MB */ | |
cb323159 A |
974 | p->p_memstat_state, p->p_memstat_effectivepriority, p->p_memstat_requestedpriority, p->p_memstat_assertionpriority, |
975 | p->p_memstat_dirty, p->p_memstat_idledeadline, | |
0a7de745 A |
976 | ledger_limit, |
977 | p->p_memstat_memlimit, | |
978 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), | |
979 | p->p_memstat_memlimit_active, | |
980 | (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL ? "F " : "NF"), | |
981 | p->p_memstat_memlimit_inactive, | |
982 | (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL ? "F " : "NF"), | |
983 | (*p->p_name ? p->p_name : "unknown")); | |
3e170ce0 | 984 | p = memorystatus_get_next_proc_locked(&b, p, traverse_all_buckets); |
0a7de745 A |
985 | } |
986 | printf("memorystatus_debug_dump ***END***\n"); | |
3e170ce0 A |
987 | } |
988 | ||
989 | static int | |
990 | sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS | |
991 | { | |
992 | #pragma unused(oidp, arg2) | |
0a7de745 A |
993 | int bucket_index = 0; |
994 | int error; | |
3e170ce0 A |
995 | error = SYSCTL_OUT(req, arg1, sizeof(int)); |
996 | if (error || !req->newptr) { | |
0a7de745 A |
997 | return error; |
998 | } | |
999 | error = SYSCTL_IN(req, &bucket_index, sizeof(int)); | |
1000 | if (error || !req->newptr) { | |
1001 | return error; | |
3e170ce0 | 1002 | } |
3e170ce0 A |
1003 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
1004 | /* | |
1005 | * All jetsam buckets will be dumped. | |
1006 | */ | |
0a7de745 | 1007 | } else { |
3e170ce0 A |
1008 | /* |
1009 | * Only a single bucket will be dumped. | |
1010 | */ | |
1011 | } | |
1012 | ||
1013 | proc_list_lock(); | |
1014 | memorystatus_debug_dump_bucket_locked(bucket_index); | |
1015 | proc_list_unlock(); | |
1016 | memorystatus_debug_dump_this_bucket = bucket_index; | |
0a7de745 | 1017 | return error; |
3e170ce0 A |
1018 | } |
1019 | ||
1020 | /* | |
1021 | * Debug aid to look at jetsam buckets and proc jetsam fields. | |
1022 | * Use this sysctl to act on a particular jetsam bucket. | |
1023 | * Writing the sysctl triggers the dump. | |
0a7de745 | 1024 | * Usage: sysctl kern.memorystatus_debug_dump_this_bucket=<bucket_index> |
3e170ce0 A |
1025 | */ |
1026 | ||
0a7de745 | 1027 | 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", ""); |
3e170ce0 A |
1028 | |
1029 | ||
39236c6e A |
1030 | /* Debug aid to aid determination of limit */ |
1031 | ||
1032 | static int | |
1033 | sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS | |
1034 | { | |
1035 | #pragma unused(oidp, arg2) | |
1036 | proc_t p; | |
1037 | unsigned int b = 0; | |
1038 | int error, enable = 0; | |
0a7de745 | 1039 | boolean_t use_active; /* use the active limit and active limit attributes */ |
813fb2f6 | 1040 | boolean_t is_fatal; |
39236c6e A |
1041 | |
1042 | error = SYSCTL_OUT(req, arg1, sizeof(int)); | |
1043 | if (error || !req->newptr) { | |
0a7de745 | 1044 | return error; |
39236c6e A |
1045 | } |
1046 | ||
1047 | error = SYSCTL_IN(req, &enable, sizeof(int)); | |
1048 | if (error || !req->newptr) { | |
0a7de745 | 1049 | return error; |
39236c6e A |
1050 | } |
1051 | ||
1052 | if (!(enable == 0 || enable == 1)) { | |
1053 | return EINVAL; | |
1054 | } | |
1055 | ||
1056 | proc_list_lock(); | |
1057 | ||
1058 | p = memorystatus_get_first_proc_locked(&b, TRUE); | |
1059 | while (p) { | |
813fb2f6 | 1060 | use_active = proc_jetsam_state_is_active_locked(p); |
3e170ce0 | 1061 | |
39236c6e | 1062 | if (enable) { |
813fb2f6 A |
1063 | if (use_active == TRUE) { |
1064 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
39236c6e | 1065 | } else { |
813fb2f6 | 1066 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
39236c6e A |
1067 | } |
1068 | } else { | |
3e170ce0 A |
1069 | /* |
1070 | * Disabling limits does not touch the stored variants. | |
1071 | * Set the cached limit fields to system_wide defaults. | |
1072 | */ | |
1073 | p->p_memstat_memlimit = -1; | |
1074 | p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; | |
813fb2f6 | 1075 | is_fatal = TRUE; |
fe8ab488 | 1076 | } |
3e170ce0 A |
1077 | |
1078 | /* | |
1079 | * Enforce the cached limit by writing to the ledger. | |
1080 | */ | |
813fb2f6 | 1081 | task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit: -1, NULL, use_active, is_fatal); |
3e170ce0 | 1082 | |
39236c6e A |
1083 | p = memorystatus_get_next_proc_locked(&b, p, TRUE); |
1084 | } | |
0a7de745 | 1085 | |
39236c6e A |
1086 | memorystatus_highwater_enabled = enable; |
1087 | ||
1088 | proc_list_unlock(); | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | ||
0a7de745 | 1093 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", ""); |
39236c6e | 1094 | |
cb323159 A |
1095 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_idle_snapshot, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_idle_snapshot, 0, ""); |
1096 | ||
1097 | #if CONFIG_JETSAM | |
1098 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages_critical, 0, ""); | |
1099 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_base, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_critical_base, 0, ""); | |
1100 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_idle_offset, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_critical_idle_offset, 0, ""); | |
1101 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_policy_more_free_offset_pages, CTLFLAG_RW, &memorystatus_policy_more_free_offset_pages, 0, ""); | |
1102 | ||
1103 | static unsigned int memorystatus_jetsam_panic_debug = 0; | |
1104 | ||
39037602 A |
1105 | #if VM_PRESSURE_EVENTS |
1106 | ||
cb323159 | 1107 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_pressure, 0, ""); |
39037602 | 1108 | |
cb323159 | 1109 | #endif /* VM_PRESSURE_EVENTS */ |
39037602 | 1110 | |
cb323159 | 1111 | #endif /* CONFIG_JETSAM */ |
39037602 | 1112 | |
cb323159 | 1113 | #endif /* DEVELOPMENT || DEBUG */ |
39037602 | 1114 | |
cb323159 A |
1115 | extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation, |
1116 | void *parameter, | |
1117 | integer_t priority, | |
1118 | thread_t *new_thread); | |
39037602 | 1119 | |
cb323159 | 1120 | #if DEVELOPMENT || DEBUG |
39037602 | 1121 | |
cb323159 A |
1122 | static int |
1123 | sysctl_memorystatus_disconnect_page_mappings SYSCTL_HANDLER_ARGS | |
1124 | { | |
1125 | #pragma unused(arg1, arg2) | |
1126 | int error = 0, pid = 0; | |
1127 | proc_t p; | |
39037602 | 1128 | |
cb323159 A |
1129 | error = sysctl_handle_int(oidp, &pid, 0, req); |
1130 | if (error || !req->newptr) { | |
0a7de745 | 1131 | return error; |
39037602 A |
1132 | } |
1133 | ||
cb323159 A |
1134 | lck_mtx_lock(&disconnect_page_mappings_mutex); |
1135 | ||
1136 | if (pid == -1) { | |
1137 | vm_pageout_disconnect_all_pages(); | |
1138 | } else { | |
1139 | p = proc_find(pid); | |
39037602 | 1140 | |
cb323159 A |
1141 | if (p != NULL) { |
1142 | error = task_disconnect_page_mappings(p->task); | |
39037602 | 1143 | |
cb323159 | 1144 | proc_rele(p); |
39037602 | 1145 | |
cb323159 A |
1146 | if (error) { |
1147 | error = EIO; | |
1148 | } | |
1149 | } else { | |
1150 | error = EINVAL; | |
39037602 A |
1151 | } |
1152 | } | |
cb323159 | 1153 | lck_mtx_unlock(&disconnect_page_mappings_mutex); |
39037602 | 1154 | |
0a7de745 | 1155 | return error; |
39037602 A |
1156 | } |
1157 | ||
cb323159 A |
1158 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_disconnect_page_mappings, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
1159 | 0, 0, &sysctl_memorystatus_disconnect_page_mappings, "I", ""); | |
39236c6e | 1160 | |
cb323159 | 1161 | #endif /* DEVELOPMENT || DEBUG */ |
316670eb | 1162 | |
f427ee49 A |
1163 | /* |
1164 | * Sorts the given bucket. | |
1165 | * | |
1166 | * Input: | |
1167 | * bucket_index - jetsam priority band to be sorted. | |
1168 | * sort_order - JETSAM_SORT_xxx from kern_memorystatus.h | |
1169 | * Currently sort_order is only meaningful when handling | |
1170 | * coalitions. | |
1171 | * | |
1172 | * proc_list_lock must be held by the caller. | |
1173 | */ | |
1174 | static void | |
1175 | memorystatus_sort_bucket_locked(unsigned int bucket_index, int sort_order) | |
1176 | { | |
1177 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
1178 | if (memstat_bucket[bucket_index].count == 0) { | |
1179 | return; | |
1180 | } | |
1181 | ||
1182 | switch (bucket_index) { | |
1183 | case JETSAM_PRIORITY_FOREGROUND: | |
1184 | if (memorystatus_sort_by_largest_coalition_locked(bucket_index, sort_order) == 0) { | |
1185 | /* | |
1186 | * Fall back to per process sorting when zero coalitions are found. | |
1187 | */ | |
1188 | memorystatus_sort_by_largest_process_locked(bucket_index); | |
1189 | } | |
1190 | break; | |
1191 | default: | |
1192 | memorystatus_sort_by_largest_process_locked(bucket_index); | |
1193 | break; | |
1194 | } | |
1195 | } | |
316670eb | 1196 | |
cb323159 A |
1197 | /* |
1198 | * Picks the sorting routine for a given jetsam priority band. | |
1199 | * | |
1200 | * Input: | |
1201 | * bucket_index - jetsam priority band to be sorted. | |
1202 | * sort_order - JETSAM_SORT_xxx from kern_memorystatus.h | |
1203 | * Currently sort_order is only meaningful when handling | |
1204 | * coalitions. | |
1205 | * | |
1206 | * Return: | |
1207 | * 0 on success | |
1208 | * non-0 on failure | |
1209 | */ | |
316670eb | 1210 | static int |
cb323159 | 1211 | memorystatus_sort_bucket(unsigned int bucket_index, int sort_order) |
316670eb | 1212 | { |
cb323159 | 1213 | int coal_sort_order; |
0a7de745 | 1214 | |
cb323159 A |
1215 | /* |
1216 | * Verify the jetsam priority | |
1217 | */ | |
1218 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
1219 | return EINVAL; | |
1220 | } | |
316670eb | 1221 | |
cb323159 A |
1222 | #if DEVELOPMENT || DEBUG |
1223 | if (sort_order == JETSAM_SORT_DEFAULT) { | |
1224 | coal_sort_order = COALITION_SORT_DEFAULT; | |
1225 | } else { | |
1226 | coal_sort_order = sort_order; /* only used for testing scenarios */ | |
0a7de745 | 1227 | } |
cb323159 A |
1228 | #else |
1229 | /* Verify default */ | |
1230 | if (sort_order == JETSAM_SORT_DEFAULT) { | |
1231 | coal_sort_order = COALITION_SORT_DEFAULT; | |
1232 | } else { | |
316670eb A |
1233 | return EINVAL; |
1234 | } | |
cb323159 | 1235 | #endif |
0a7de745 | 1236 | |
39236c6e | 1237 | proc_list_lock(); |
f427ee49 | 1238 | memorystatus_sort_bucket_locked(bucket_index, coal_sort_order); |
39236c6e | 1239 | proc_list_unlock(); |
0a7de745 | 1240 | |
0a7de745 | 1241 | return 0; |
316670eb A |
1242 | } |
1243 | ||
0a7de745 | 1244 | /* |
cb323159 | 1245 | * Sort processes by size for a single jetsam bucket. |
0a7de745 | 1246 | */ |
0a7de745 | 1247 | |
cb323159 A |
1248 | static void |
1249 | memorystatus_sort_by_largest_process_locked(unsigned int bucket_index) | |
316670eb | 1250 | { |
cb323159 A |
1251 | proc_t p = NULL, insert_after_proc = NULL, max_proc = NULL; |
1252 | proc_t next_p = NULL, prev_max_proc = NULL; | |
1253 | uint32_t pages = 0, max_pages = 0; | |
1254 | memstat_bucket_t *current_bucket; | |
3e170ce0 | 1255 | |
cb323159 A |
1256 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
1257 | return; | |
3e170ce0 A |
1258 | } |
1259 | ||
cb323159 | 1260 | current_bucket = &memstat_bucket[bucket_index]; |
d9a64523 | 1261 | |
cb323159 | 1262 | p = TAILQ_FIRST(¤t_bucket->list); |
316670eb | 1263 | |
cb323159 A |
1264 | while (p) { |
1265 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); | |
1266 | max_pages = pages; | |
1267 | max_proc = p; | |
1268 | prev_max_proc = p; | |
d9a64523 | 1269 | |
cb323159 A |
1270 | while ((next_p = TAILQ_NEXT(p, p_memstat_list)) != NULL) { |
1271 | /* traversing list until we find next largest process */ | |
1272 | p = next_p; | |
1273 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); | |
1274 | if (pages > max_pages) { | |
1275 | max_pages = pages; | |
1276 | max_proc = p; | |
d9a64523 | 1277 | } |
cb323159 | 1278 | } |
0a7de745 | 1279 | |
cb323159 A |
1280 | if (prev_max_proc != max_proc) { |
1281 | /* found a larger process, place it in the list */ | |
1282 | TAILQ_REMOVE(¤t_bucket->list, max_proc, p_memstat_list); | |
1283 | if (insert_after_proc == NULL) { | |
1284 | TAILQ_INSERT_HEAD(¤t_bucket->list, max_proc, p_memstat_list); | |
d9a64523 | 1285 | } else { |
cb323159 | 1286 | TAILQ_INSERT_AFTER(¤t_bucket->list, insert_after_proc, max_proc, p_memstat_list); |
d9a64523 | 1287 | } |
cb323159 | 1288 | prev_max_proc = max_proc; |
d9a64523 A |
1289 | } |
1290 | ||
cb323159 | 1291 | insert_after_proc = max_proc; |
3e170ce0 | 1292 | |
cb323159 | 1293 | p = TAILQ_NEXT(max_proc, p_memstat_list); |
39236c6e | 1294 | } |
316670eb A |
1295 | } |
1296 | ||
cb323159 A |
1297 | proc_t |
1298 | memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) | |
316670eb | 1299 | { |
cb323159 A |
1300 | memstat_bucket_t *current_bucket; |
1301 | proc_t next_p; | |
fe8ab488 | 1302 | |
cb323159 A |
1303 | if ((*bucket_index) >= MEMSTAT_BUCKET_COUNT) { |
1304 | return NULL; | |
0a7de745 | 1305 | } |
316670eb | 1306 | |
cb323159 A |
1307 | current_bucket = &memstat_bucket[*bucket_index]; |
1308 | next_p = TAILQ_FIRST(¤t_bucket->list); | |
1309 | if (!next_p && search) { | |
1310 | while (!next_p && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) { | |
1311 | current_bucket = &memstat_bucket[*bucket_index]; | |
1312 | next_p = TAILQ_FIRST(¤t_bucket->list); | |
d190cdc3 | 1313 | } |
316670eb A |
1314 | } |
1315 | ||
cb323159 | 1316 | return next_p; |
316670eb A |
1317 | } |
1318 | ||
cb323159 A |
1319 | proc_t |
1320 | memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search) | |
d9a64523 | 1321 | { |
cb323159 A |
1322 | memstat_bucket_t *current_bucket; |
1323 | proc_t next_p; | |
d9a64523 | 1324 | |
cb323159 A |
1325 | if (!p || ((*bucket_index) >= MEMSTAT_BUCKET_COUNT)) { |
1326 | return NULL; | |
d9a64523 A |
1327 | } |
1328 | ||
cb323159 A |
1329 | next_p = TAILQ_NEXT(p, p_memstat_list); |
1330 | while (!next_p && search && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) { | |
1331 | current_bucket = &memstat_bucket[*bucket_index]; | |
1332 | next_p = TAILQ_FIRST(¤t_bucket->list); | |
d9a64523 A |
1333 | } |
1334 | ||
cb323159 A |
1335 | return next_p; |
1336 | } | |
d9a64523 | 1337 | |
cb323159 A |
1338 | /* |
1339 | * Structure to hold state for a jetsam thread. | |
1340 | * Typically there should be a single jetsam thread | |
1341 | * unless parallel jetsam is enabled. | |
1342 | */ | |
1343 | struct jetsam_thread_state { | |
1344 | uint8_t inited; /* boolean - if the thread is initialized */ | |
1345 | uint8_t limit_to_low_bands; /* boolean */ | |
1346 | int memorystatus_wakeup; /* wake channel */ | |
1347 | int index; /* jetsam thread index */ | |
1348 | thread_t thread; /* jetsam thread pointer */ | |
1349 | } *jetsam_threads; | |
d9a64523 | 1350 | |
cb323159 A |
1351 | /* Maximum number of jetsam threads allowed */ |
1352 | #define JETSAM_THREADS_LIMIT 3 | |
d9a64523 | 1353 | |
cb323159 A |
1354 | /* Number of active jetsam threads */ |
1355 | _Atomic int active_jetsam_threads = 1; | |
d9a64523 | 1356 | |
cb323159 A |
1357 | /* Number of maximum jetsam threads configured */ |
1358 | int max_jetsam_threads = JETSAM_THREADS_LIMIT; | |
d9a64523 | 1359 | |
cb323159 A |
1360 | /* |
1361 | * Global switch for enabling fast jetsam. Fast jetsam is | |
1362 | * hooked up via the system_override() system call. It has the | |
1363 | * following effects: | |
1364 | * - Raise the jetsam threshold ("clear-the-deck") | |
1365 | * - Enabled parallel jetsam on eligible devices | |
1366 | */ | |
c6bf4f31 A |
1367 | #if __AMP__ |
1368 | int fast_jetsam_enabled = 1; | |
1369 | #else /* __AMP__ */ | |
cb323159 | 1370 | int fast_jetsam_enabled = 0; |
c6bf4f31 | 1371 | #endif /* __AMP__ */ |
d9a64523 | 1372 | |
f427ee49 A |
1373 | #if CONFIG_DIRTYSTATUS_TRACKING |
1374 | int dirtystatus_tracking_enabled = 0; | |
1375 | SYSCTL_INT(_kern, OID_AUTO, dirtystatus_tracking_enabled, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &dirtystatus_tracking_enabled, 0, ""); | |
1376 | #endif | |
1377 | ||
cb323159 A |
1378 | /* Routine to find the jetsam state structure for the current jetsam thread */ |
1379 | static inline struct jetsam_thread_state * | |
1380 | jetsam_current_thread(void) | |
1381 | { | |
1382 | for (int thr_id = 0; thr_id < max_jetsam_threads; thr_id++) { | |
1383 | if (jetsam_threads[thr_id].thread == current_thread()) { | |
1384 | return &(jetsam_threads[thr_id]); | |
1385 | } | |
1386 | } | |
1387 | return NULL; | |
1388 | } | |
0a7de745 | 1389 | |
d9a64523 | 1390 | |
cb323159 A |
1391 | __private_extern__ void |
1392 | memorystatus_init(void) | |
1393 | { | |
1394 | kern_return_t result; | |
1395 | int i; | |
d9a64523 | 1396 | |
cb323159 A |
1397 | #if CONFIG_FREEZE |
1398 | memorystatus_freeze_jetsam_band = JETSAM_PRIORITY_UI_SUPPORT; | |
1399 | memorystatus_frozen_processes_max = FREEZE_PROCESSES_MAX; | |
1400 | memorystatus_frozen_shared_mb_max = ((MAX_FROZEN_SHARED_MB_PERCENT * max_task_footprint_mb) / 100); /* 10% of the system wide task limit */ | |
1401 | memorystatus_freeze_shared_mb_per_process_max = (memorystatus_frozen_shared_mb_max / 4); | |
1402 | memorystatus_freeze_pages_min = FREEZE_PAGES_MIN; | |
1403 | memorystatus_freeze_pages_max = FREEZE_PAGES_MAX; | |
1404 | memorystatus_max_frozen_demotions_daily = MAX_FROZEN_PROCESS_DEMOTIONS; | |
1405 | memorystatus_thaw_count_demotion_threshold = MIN_THAW_DEMOTION_THRESHOLD; | |
1406 | #endif | |
d9a64523 | 1407 | |
cb323159 A |
1408 | #if DEVELOPMENT || DEBUG |
1409 | disconnect_page_mappings_lck_grp_attr = lck_grp_attr_alloc_init(); | |
1410 | disconnect_page_mappings_lck_grp = lck_grp_alloc_init("disconnect_page_mappings", disconnect_page_mappings_lck_grp_attr); | |
0a7de745 | 1411 | |
cb323159 | 1412 | lck_mtx_init(&disconnect_page_mappings_mutex, disconnect_page_mappings_lck_grp, NULL); |
d9a64523 | 1413 | |
f427ee49 | 1414 | if (kill_on_no_paging_space) { |
cb323159 A |
1415 | max_kill_priority = JETSAM_PRIORITY_MAX; |
1416 | } | |
1417 | #endif | |
d9a64523 | 1418 | |
cb323159 A |
1419 | memorystatus_jetsam_fg_band_lock_grp_attr = lck_grp_attr_alloc_init(); |
1420 | memorystatus_jetsam_fg_band_lock_grp = | |
1421 | lck_grp_alloc_init("memorystatus_jetsam_fg_band", memorystatus_jetsam_fg_band_lock_grp_attr); | |
1422 | lck_mtx_init(&memorystatus_jetsam_fg_band_lock, memorystatus_jetsam_fg_band_lock_grp, NULL); | |
d9a64523 | 1423 | |
cb323159 A |
1424 | /* Init buckets */ |
1425 | for (i = 0; i < MEMSTAT_BUCKET_COUNT; i++) { | |
1426 | TAILQ_INIT(&memstat_bucket[i].list); | |
1427 | memstat_bucket[i].count = 0; | |
1428 | memstat_bucket[i].relaunch_high_count = 0; | |
1429 | } | |
1430 | memorystatus_idle_demotion_call = thread_call_allocate((thread_call_func_t)memorystatus_perform_idle_demotion, NULL); | |
d9a64523 | 1431 | |
cb323159 A |
1432 | nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); |
1433 | nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); | |
0a7de745 | 1434 | |
cb323159 A |
1435 | #if CONFIG_JETSAM |
1436 | /* Apply overrides */ | |
1437 | if (!PE_parse_boot_argn("kern.jetsam_delta", &delta_percentage, sizeof(delta_percentage))) { | |
1438 | PE_get_default("kern.jetsam_delta", &delta_percentage, sizeof(delta_percentage)); | |
1439 | } | |
1440 | if (delta_percentage == 0) { | |
1441 | delta_percentage = 5; | |
1442 | } | |
1443 | if (max_mem > config_jetsam_large_memory_cutoff) { | |
1444 | critical_threshold_percentage = critical_threshold_percentage_larger_devices; | |
1445 | delta_percentage = delta_percentage_larger_devices; | |
1446 | } | |
1447 | assert(delta_percentage < 100); | |
1448 | if (!PE_parse_boot_argn("kern.jetsam_critical_threshold", &critical_threshold_percentage, sizeof(critical_threshold_percentage))) { | |
1449 | PE_get_default("kern.jetsam_critical_threshold", &critical_threshold_percentage, sizeof(critical_threshold_percentage)); | |
1450 | } | |
1451 | assert(critical_threshold_percentage < 100); | |
1452 | PE_get_default("kern.jetsam_idle_offset", &idle_offset_percentage, sizeof(idle_offset_percentage)); | |
1453 | assert(idle_offset_percentage < 100); | |
1454 | PE_get_default("kern.jetsam_pressure_threshold", &pressure_threshold_percentage, sizeof(pressure_threshold_percentage)); | |
1455 | assert(pressure_threshold_percentage < 100); | |
1456 | PE_get_default("kern.jetsam_freeze_threshold", &freeze_threshold_percentage, sizeof(freeze_threshold_percentage)); | |
1457 | assert(freeze_threshold_percentage < 100); | |
d9a64523 | 1458 | |
d9a64523 | 1459 | |
cb323159 A |
1460 | if (!PE_parse_boot_argn("jetsam_aging_policy", &jetsam_aging_policy, |
1461 | sizeof(jetsam_aging_policy))) { | |
1462 | if (!PE_get_default("kern.jetsam_aging_policy", &jetsam_aging_policy, | |
1463 | sizeof(jetsam_aging_policy))) { | |
1464 | jetsam_aging_policy = kJetsamAgingPolicySysProcsReclaimedFirst; | |
d9a64523 | 1465 | } |
cb323159 | 1466 | } |
d9a64523 | 1467 | |
cb323159 A |
1468 | if (jetsam_aging_policy > kJetsamAgingPolicyMax) { |
1469 | jetsam_aging_policy = kJetsamAgingPolicySysProcsReclaimedFirst; | |
1470 | } | |
d9a64523 | 1471 | |
cb323159 A |
1472 | switch (jetsam_aging_policy) { |
1473 | case kJetsamAgingPolicyNone: | |
1474 | system_procs_aging_band = JETSAM_PRIORITY_IDLE; | |
1475 | applications_aging_band = JETSAM_PRIORITY_IDLE; | |
1476 | break; | |
d9a64523 | 1477 | |
cb323159 A |
1478 | case kJetsamAgingPolicyLegacy: |
1479 | /* | |
1480 | * Legacy behavior where some daemons get a 10s protection once | |
1481 | * AND only before the first clean->dirty->clean transition before | |
1482 | * going into IDLE band. | |
1483 | */ | |
1484 | system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
1485 | applications_aging_band = JETSAM_PRIORITY_IDLE; | |
1486 | break; | |
d9a64523 | 1487 | |
cb323159 A |
1488 | case kJetsamAgingPolicySysProcsReclaimedFirst: |
1489 | system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
1490 | applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
1491 | break; | |
d9a64523 | 1492 | |
cb323159 A |
1493 | case kJetsamAgingPolicyAppsReclaimedFirst: |
1494 | system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
1495 | applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
1496 | break; | |
0a7de745 | 1497 | |
cb323159 A |
1498 | default: |
1499 | break; | |
d9a64523 | 1500 | } |
0a7de745 | 1501 | |
cb323159 A |
1502 | /* |
1503 | * The aging bands cannot overlap with the JETSAM_PRIORITY_ELEVATED_INACTIVE | |
1504 | * band and must be below it in priority. This is so that we don't have to make | |
1505 | * our 'aging' code worry about a mix of processes, some of which need to age | |
1506 | * and some others that need to stay elevated in the jetsam bands. | |
1507 | */ | |
1508 | assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > system_procs_aging_band); | |
1509 | assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > applications_aging_band); | |
1510 | ||
1511 | /* Take snapshots for idle-exit kills by default? First check the boot-arg... */ | |
1512 | if (!PE_parse_boot_argn("jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot))) { | |
1513 | /* ...no boot-arg, so check the device tree */ | |
1514 | PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot)); | |
1515 | } | |
d9a64523 | 1516 | |
f427ee49 A |
1517 | memorystatus_delta = (unsigned int) (delta_percentage * atop_64(max_mem) / 100); |
1518 | memorystatus_available_pages_critical_idle_offset = (unsigned int) (idle_offset_percentage * atop_64(max_mem) / 100); | |
1519 | memorystatus_available_pages_critical_base = (unsigned int) ((critical_threshold_percentage / delta_percentage) * memorystatus_delta); | |
1520 | memorystatus_policy_more_free_offset_pages = (unsigned int) ((policy_more_free_offset_percentage / delta_percentage) * memorystatus_delta); | |
1521 | memorystatus_sysproc_aging_aggr_pages = (unsigned int) (sysproc_aging_aggr_threshold_percentage * atop_64(max_mem) / 100); | |
d9a64523 | 1522 | |
cb323159 A |
1523 | /* Jetsam Loop Detection */ |
1524 | if (max_mem <= (512 * 1024 * 1024)) { | |
1525 | /* 512 MB devices */ | |
1526 | memorystatus_jld_eval_period_msecs = 8000; /* 8000 msecs == 8 second window */ | |
d9a64523 | 1527 | } else { |
cb323159 A |
1528 | /* 1GB and larger devices */ |
1529 | memorystatus_jld_eval_period_msecs = 6000; /* 6000 msecs == 6 second window */ | |
d9a64523 A |
1530 | } |
1531 | ||
cb323159 | 1532 | memorystatus_jld_enabled = TRUE; |
0a7de745 | 1533 | |
cb323159 A |
1534 | /* No contention at this point */ |
1535 | memorystatus_update_levels_locked(FALSE); | |
d9a64523 | 1536 | |
cb323159 | 1537 | #endif /* CONFIG_JETSAM */ |
d9a64523 | 1538 | |
f427ee49 A |
1539 | #if __arm64__ |
1540 | if (!PE_parse_boot_argn("entitled_max_task_pmem", &memorystatus_entitled_max_task_footprint_mb, | |
1541 | sizeof(memorystatus_entitled_max_task_footprint_mb))) { | |
1542 | if (!PE_get_default("kern.entitled_max_task_pmem", &memorystatus_entitled_max_task_footprint_mb, | |
1543 | sizeof(memorystatus_entitled_max_task_footprint_mb))) { | |
1544 | // entitled_max_task_pmem is not supported on this system. | |
1545 | memorystatus_entitled_max_task_footprint_mb = 0; | |
1546 | } | |
1547 | } | |
1548 | if (memorystatus_entitled_max_task_footprint_mb > max_mem / (1UL << 20) || memorystatus_entitled_max_task_footprint_mb < 0) { | |
1549 | os_log_with_startup_serial(OS_LOG_DEFAULT, "Invalid value (%d) for entitled_max_task_pmem. Setting to 0", | |
1550 | memorystatus_entitled_max_task_footprint_mb); | |
1551 | } | |
1552 | #endif /* __arm64__ */ | |
1553 | ||
cb323159 | 1554 | memorystatus_jetsam_snapshot_max = maxproc; |
d9a64523 | 1555 | |
cb323159 A |
1556 | memorystatus_jetsam_snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + |
1557 | (sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_max); | |
d9a64523 | 1558 | |
f427ee49 | 1559 | memorystatus_jetsam_snapshot = kalloc_flags(memorystatus_jetsam_snapshot_size, Z_WAITOK | Z_ZERO); |
cb323159 A |
1560 | if (!memorystatus_jetsam_snapshot) { |
1561 | panic("Could not allocate memorystatus_jetsam_snapshot"); | |
1562 | } | |
2d21ac55 | 1563 | |
f427ee49 | 1564 | memorystatus_jetsam_snapshot_copy = kalloc_flags(memorystatus_jetsam_snapshot_size, Z_WAITOK | Z_ZERO); |
cb323159 A |
1565 | if (!memorystatus_jetsam_snapshot_copy) { |
1566 | panic("Could not allocate memorystatus_jetsam_snapshot_copy"); | |
1567 | } | |
fe8ab488 | 1568 | |
f427ee49 A |
1569 | #if CONFIG_FREEZE |
1570 | memorystatus_jetsam_snapshot_freezer_max = memorystatus_jetsam_snapshot_max / JETSAM_SNAPSHOT_FREEZER_MAX_FACTOR; | |
1571 | memorystatus_jetsam_snapshot_freezer_size = sizeof(memorystatus_jetsam_snapshot_t) + | |
1572 | (sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_freezer_max); | |
1573 | ||
1574 | memorystatus_jetsam_snapshot_freezer = kalloc_flags(memorystatus_jetsam_snapshot_freezer_size, Z_WAITOK | Z_ZERO); | |
1575 | if (!memorystatus_jetsam_snapshot_freezer) { | |
1576 | panic("Could not allocate memorystatus_jetsam_snapshot_freezer"); | |
1577 | } | |
1578 | #endif /* CONFIG_FREEZE */ | |
1579 | ||
cb323159 | 1580 | nanoseconds_to_absolutetime((uint64_t)JETSAM_SNAPSHOT_TIMEOUT_SECS * NSEC_PER_SEC, &memorystatus_jetsam_snapshot_timeout); |
39236c6e | 1581 | |
cb323159 | 1582 | memset(&memorystatus_at_boot_snapshot, 0, sizeof(memorystatus_jetsam_snapshot_t)); |
39037602 | 1583 | |
cb323159 | 1584 | #if CONFIG_FREEZE |
f427ee49 | 1585 | memorystatus_freeze_threshold = (unsigned int) ((freeze_threshold_percentage / delta_percentage) * memorystatus_delta); |
cb323159 | 1586 | #endif |
39037602 | 1587 | |
cb323159 A |
1588 | /* Check the boot-arg to see if fast jetsam is allowed */ |
1589 | if (!PE_parse_boot_argn("fast_jetsam_enabled", &fast_jetsam_enabled, sizeof(fast_jetsam_enabled))) { | |
1590 | fast_jetsam_enabled = 0; | |
0a7de745 | 1591 | } |
39037602 | 1592 | |
cb323159 A |
1593 | /* Check the boot-arg to configure the maximum number of jetsam threads */ |
1594 | if (!PE_parse_boot_argn("max_jetsam_threads", &max_jetsam_threads, sizeof(max_jetsam_threads))) { | |
1595 | max_jetsam_threads = JETSAM_THREADS_LIMIT; | |
1596 | } | |
39037602 | 1597 | |
cb323159 A |
1598 | /* Restrict the maximum number of jetsam threads to JETSAM_THREADS_LIMIT */ |
1599 | if (max_jetsam_threads > JETSAM_THREADS_LIMIT) { | |
1600 | max_jetsam_threads = JETSAM_THREADS_LIMIT; | |
1601 | } | |
39037602 | 1602 | |
cb323159 A |
1603 | /* For low CPU systems disable fast jetsam mechanism */ |
1604 | if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) { | |
1605 | max_jetsam_threads = 1; | |
1606 | fast_jetsam_enabled = 0; | |
1607 | } | |
39037602 | 1608 | |
cb323159 | 1609 | /* Initialize the jetsam_threads state array */ |
f427ee49 A |
1610 | jetsam_threads = zalloc_permanent(sizeof(struct jetsam_thread_state) * |
1611 | max_jetsam_threads, ZALIGN(struct jetsam_thread_state)); | |
39037602 | 1612 | |
cb323159 A |
1613 | /* Initialize all the jetsam threads */ |
1614 | for (i = 0; i < max_jetsam_threads; i++) { | |
1615 | jetsam_threads[i].inited = FALSE; | |
1616 | jetsam_threads[i].index = i; | |
1617 | result = kernel_thread_start_priority(memorystatus_thread, NULL, 95 /* MAXPRI_KERNEL */, &jetsam_threads[i].thread); | |
1618 | if (result != KERN_SUCCESS) { | |
1619 | panic("Could not create memorystatus_thread %d", i); | |
0a7de745 | 1620 | } |
cb323159 | 1621 | thread_deallocate(jetsam_threads[i].thread); |
39037602 | 1622 | } |
39037602 A |
1623 | } |
1624 | ||
cb323159 A |
1625 | /* Centralised for the purposes of allowing panic-on-jetsam */ |
1626 | extern void | |
1627 | vm_run_compactor(void); | |
39037602 | 1628 | |
3e170ce0 | 1629 | /* |
cb323159 A |
1630 | * The jetsam no frills kill call |
1631 | * Return: 0 on success | |
1632 | * error code on failure (EINVAL...) | |
3e170ce0 | 1633 | */ |
0a7de745 | 1634 | static int |
cb323159 | 1635 | jetsam_do_kill(proc_t p, int jetsam_flags, os_reason_t jetsam_reason) |
3e170ce0 | 1636 | { |
cb323159 A |
1637 | int error = 0; |
1638 | error = exit_with_reason(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags, jetsam_reason); | |
1639 | return error; | |
1640 | } | |
3e170ce0 | 1641 | |
cb323159 A |
1642 | /* |
1643 | * Wrapper for processes exiting with memorystatus details | |
1644 | */ | |
1645 | static boolean_t | |
1646 | memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason, uint64_t *footprint_of_killed_proc) | |
1647 | { | |
1648 | int error = 0; | |
1649 | __unused pid_t victim_pid = p->p_pid; | |
1650 | uint64_t footprint = get_task_phys_footprint(p->task); | |
1651 | #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) | |
1652 | int32_t memstat_effectivepriority = p->p_memstat_effectivepriority; | |
1653 | #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */ | |
3e170ce0 | 1654 | |
cb323159 A |
1655 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_START, |
1656 | victim_pid, cause, vm_page_free_count, footprint, 0); | |
1657 | DTRACE_MEMORYSTATUS4(memorystatus_do_kill, proc_t, p, os_reason_t, jetsam_reason, uint32_t, cause, uint64_t, footprint); | |
1658 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) | |
1659 | if (memorystatus_jetsam_panic_debug & (1 << cause)) { | |
1660 | panic("memorystatus_do_kill(): jetsam debug panic (cause: %d)", cause); | |
3e170ce0 A |
1661 | } |
1662 | #else | |
cb323159 | 1663 | #pragma unused(cause) |
3e170ce0 A |
1664 | #endif |
1665 | ||
cb323159 A |
1666 | if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) { |
1667 | printf("memorystatus: killing process %d [%s] in high band %s (%d) - memorystatus_available_pages: %llu\n", p->p_pid, | |
1668 | (*p->p_name ? p->p_name : "unknown"), | |
1669 | memorystatus_priority_band_name(p->p_memstat_effectivepriority), p->p_memstat_effectivepriority, | |
f427ee49 | 1670 | (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
5ba3f43e A |
1671 | } |
1672 | ||
cb323159 A |
1673 | /* |
1674 | * The jetsam_reason (os_reason_t) has enough information about the kill cause. | |
1675 | * We don't really need jetsam_flags anymore, so it's okay that not all possible kill causes have been mapped. | |
1676 | */ | |
1677 | int jetsam_flags = P_LTERM_JETSAM; | |
1678 | switch (cause) { | |
1679 | case kMemorystatusKilledHiwat: jetsam_flags |= P_JETSAM_HIWAT; break; | |
1680 | case kMemorystatusKilledVnodes: jetsam_flags |= P_JETSAM_VNODE; break; | |
1681 | case kMemorystatusKilledVMPageShortage: jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break; | |
1682 | case kMemorystatusKilledVMCompressorThrashing: | |
1683 | case kMemorystatusKilledVMCompressorSpaceShortage: jetsam_flags |= P_JETSAM_VMTHRASHING; break; | |
1684 | case kMemorystatusKilledFCThrashing: jetsam_flags |= P_JETSAM_FCTHRASHING; break; | |
1685 | case kMemorystatusKilledPerProcessLimit: jetsam_flags |= P_JETSAM_PID; break; | |
1686 | case kMemorystatusKilledIdleExit: jetsam_flags |= P_JETSAM_IDLEEXIT; break; | |
3e170ce0 | 1687 | } |
cb323159 A |
1688 | error = jetsam_do_kill(p, jetsam_flags, jetsam_reason); |
1689 | *footprint_of_killed_proc = ((error == 0) ? footprint : 0); | |
0a7de745 | 1690 | |
cb323159 A |
1691 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_END, |
1692 | victim_pid, memstat_effectivepriority, vm_page_free_count, error, 0); | |
1693 | ||
1694 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_COMPACTOR_RUN)) | DBG_FUNC_START, | |
1695 | victim_pid, cause, vm_page_free_count, *footprint_of_killed_proc, 0); | |
1696 | ||
1697 | vm_run_compactor(); | |
1698 | ||
1699 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_COMPACTOR_RUN)) | DBG_FUNC_END, | |
1700 | victim_pid, cause, vm_page_free_count, 0, 0); | |
1701 | ||
1702 | return error == 0; | |
3e170ce0 A |
1703 | } |
1704 | ||
fe8ab488 | 1705 | /* |
cb323159 | 1706 | * Node manipulation |
fe8ab488 A |
1707 | */ |
1708 | ||
0a7de745 | 1709 | static void |
cb323159 | 1710 | memorystatus_check_levels_locked(void) |
fe8ab488 | 1711 | { |
cb323159 A |
1712 | #if CONFIG_JETSAM |
1713 | /* Update levels */ | |
1714 | memorystatus_update_levels_locked(TRUE); | |
1715 | #else /* CONFIG_JETSAM */ | |
1716 | /* | |
1717 | * Nothing to do here currently since we update | |
1718 | * memorystatus_available_pages in vm_pressure_response. | |
1719 | */ | |
1720 | #endif /* CONFIG_JETSAM */ | |
1721 | } | |
0a7de745 | 1722 | |
cb323159 A |
1723 | /* |
1724 | * Pin a process to a particular jetsam band when it is in the background i.e. not doing active work. | |
1725 | * For an application: that means no longer in the FG band | |
1726 | * For a daemon: that means no longer in its 'requested' jetsam priority band | |
1727 | */ | |
0a7de745 | 1728 | |
cb323159 A |
1729 | int |
1730 | memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, int jetsam_prio, boolean_t effective_now) | |
1731 | { | |
1732 | int error = 0; | |
1733 | boolean_t enable = FALSE; | |
1734 | proc_t p = NULL; | |
fe8ab488 | 1735 | |
cb323159 A |
1736 | if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE) { |
1737 | enable = TRUE; | |
1738 | } else if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE) { | |
1739 | enable = FALSE; | |
1740 | } else { | |
1741 | return EINVAL; | |
1742 | } | |
fe8ab488 | 1743 | |
cb323159 A |
1744 | p = proc_find(pid); |
1745 | if (p != NULL) { | |
1746 | if ((enable && ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) == P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) || | |
1747 | (!enable && ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) == 0))) { | |
1748 | /* | |
1749 | * No change in state. | |
1750 | */ | |
1751 | } else { | |
1752 | proc_list_lock(); | |
0a7de745 | 1753 | |
cb323159 A |
1754 | if (enable) { |
1755 | p->p_memstat_state |= P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; | |
1756 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
fe8ab488 | 1757 | |
cb323159 A |
1758 | if (effective_now) { |
1759 | if (p->p_memstat_effectivepriority < jetsam_prio) { | |
1760 | if (memorystatus_highwater_enabled) { | |
1761 | /* | |
1762 | * Process is about to transition from | |
1763 | * inactive --> active | |
1764 | * assign active state | |
1765 | */ | |
1766 | boolean_t is_fatal; | |
1767 | boolean_t use_active = TRUE; | |
1768 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
1769 | task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, use_active, is_fatal); | |
1770 | } | |
1771 | memorystatus_update_priority_locked(p, jetsam_prio, FALSE, FALSE); | |
1772 | } | |
1773 | } else { | |
1774 | if (isProcessInAgingBands(p)) { | |
1775 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); | |
1776 | } | |
1777 | } | |
fe8ab488 | 1778 | } else { |
cb323159 A |
1779 | p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; |
1780 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
fe8ab488 | 1781 | |
cb323159 A |
1782 | if (effective_now) { |
1783 | if (p->p_memstat_effectivepriority == jetsam_prio) { | |
1784 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); | |
1785 | } | |
1786 | } else { | |
1787 | if (isProcessInAgingBands(p)) { | |
1788 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); | |
1789 | } | |
1790 | } | |
1791 | } | |
fe8ab488 | 1792 | |
cb323159 A |
1793 | proc_list_unlock(); |
1794 | } | |
1795 | proc_rele(p); | |
1796 | error = 0; | |
1797 | } else { | |
1798 | error = ESRCH; | |
fe8ab488 | 1799 | } |
cb323159 A |
1800 | |
1801 | return error; | |
fe8ab488 A |
1802 | } |
1803 | ||
cb323159 A |
1804 | static void |
1805 | memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2) | |
0a7de745 | 1806 | { |
cb323159 A |
1807 | proc_t p; |
1808 | uint64_t current_time = 0, idle_delay_time = 0; | |
1809 | int demote_prio_band = 0; | |
1810 | memstat_bucket_t *demotion_bucket; | |
39236c6e | 1811 | |
cb323159 | 1812 | MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion()\n"); |
39236c6e | 1813 | |
cb323159 | 1814 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_START, 0, 0, 0, 0, 0); |
0a7de745 | 1815 | |
cb323159 | 1816 | current_time = mach_absolute_time(); |
39236c6e | 1817 | |
cb323159 | 1818 | proc_list_lock(); |
0a7de745 | 1819 | |
cb323159 | 1820 | demote_prio_band = JETSAM_PRIORITY_IDLE + 1; |
39236c6e | 1821 | |
cb323159 A |
1822 | for (; demote_prio_band < JETSAM_PRIORITY_MAX; demote_prio_band++) { |
1823 | if (demote_prio_band != system_procs_aging_band && demote_prio_band != applications_aging_band) { | |
1824 | continue; | |
1825 | } | |
39236c6e | 1826 | |
cb323159 A |
1827 | demotion_bucket = &memstat_bucket[demote_prio_band]; |
1828 | p = TAILQ_FIRST(&demotion_bucket->list); | |
d9a64523 | 1829 | |
cb323159 A |
1830 | while (p) { |
1831 | MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion() found %d\n", p->p_pid); | |
d9a64523 | 1832 | |
cb323159 | 1833 | assert(p->p_memstat_idledeadline); |
d9a64523 | 1834 | |
cb323159 | 1835 | assert(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS); |
d9a64523 | 1836 | |
cb323159 A |
1837 | if (current_time >= p->p_memstat_idledeadline) { |
1838 | if ((isSysProc(p) && | |
1839 | ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) != P_DIRTY_IDLE_EXIT_ENABLED)) || /* system proc marked dirty*/ | |
1840 | task_has_assertions((struct task *)(p->task))) { /* has outstanding assertions which might indicate outstanding work too */ | |
1841 | idle_delay_time = (isSysProc(p)) ? memorystatus_sysprocs_idle_time(p) : memorystatus_apps_idle_time(p); | |
d9a64523 | 1842 | |
cb323159 A |
1843 | p->p_memstat_idledeadline += idle_delay_time; |
1844 | p = TAILQ_NEXT(p, p_memstat_list); | |
1845 | } else { | |
1846 | proc_t next_proc = NULL; | |
d9a64523 | 1847 | |
cb323159 A |
1848 | next_proc = TAILQ_NEXT(p, p_memstat_list); |
1849 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
d9a64523 | 1850 | |
cb323159 | 1851 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, false, true); |
39236c6e | 1852 | |
cb323159 A |
1853 | p = next_proc; |
1854 | continue; | |
1855 | } | |
1856 | } else { | |
1857 | // No further candidates | |
1858 | break; | |
1859 | } | |
1860 | } | |
1861 | } | |
fe8ab488 | 1862 | |
cb323159 | 1863 | memorystatus_reschedule_idle_demotion_locked(); |
39037602 | 1864 | |
cb323159 | 1865 | proc_list_unlock(); |
5ba3f43e | 1866 | |
cb323159 A |
1867 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_END, 0, 0, 0, 0, 0); |
1868 | } | |
0a7de745 | 1869 | |
cb323159 A |
1870 | static void |
1871 | memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) | |
1872 | { | |
1873 | boolean_t present_in_sysprocs_aging_bucket = FALSE; | |
1874 | boolean_t present_in_apps_aging_bucket = FALSE; | |
1875 | uint64_t idle_delay_time = 0; | |
39037602 | 1876 | |
cb323159 A |
1877 | if (jetsam_aging_policy == kJetsamAgingPolicyNone) { |
1878 | return; | |
39236c6e | 1879 | } |
0a7de745 | 1880 | |
cb323159 A |
1881 | if ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) || |
1882 | (p->p_memstat_state & P_MEMSTAT_PRIORITY_ASSERTION)) { | |
1883 | /* | |
1884 | * This process isn't going to be making the trip to the lower bands. | |
1885 | */ | |
1886 | return; | |
39037602 | 1887 | } |
39037602 | 1888 | |
cb323159 A |
1889 | if (isProcessInAgingBands(p)) { |
1890 | if (jetsam_aging_policy != kJetsamAgingPolicyLegacy) { | |
1891 | assert((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) != P_DIRTY_AGING_IN_PROGRESS); | |
39037602 | 1892 | } |
39037602 | 1893 | |
cb323159 A |
1894 | if (isSysProc(p) && system_procs_aging_band) { |
1895 | present_in_sysprocs_aging_bucket = TRUE; | |
1896 | } else if (isApp(p) && applications_aging_band) { | |
1897 | present_in_apps_aging_bucket = TRUE; | |
1898 | } | |
39037602 A |
1899 | } |
1900 | ||
cb323159 A |
1901 | assert(!present_in_sysprocs_aging_bucket); |
1902 | assert(!present_in_apps_aging_bucket); | |
39037602 | 1903 | |
cb323159 A |
1904 | 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", |
1905 | p->p_pid, p->p_memstat_dirty, set_state, (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)); | |
39037602 | 1906 | |
cb323159 A |
1907 | if (isSysProc(p)) { |
1908 | assert((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED); | |
1909 | } | |
39037602 | 1910 | |
cb323159 A |
1911 | idle_delay_time = (isSysProc(p)) ? memorystatus_sysprocs_idle_time(p) : memorystatus_apps_idle_time(p); |
1912 | if (set_state) { | |
1913 | p->p_memstat_dirty |= P_DIRTY_AGING_IN_PROGRESS; | |
1914 | p->p_memstat_idledeadline = mach_absolute_time() + idle_delay_time; | |
39037602 A |
1915 | } |
1916 | ||
cb323159 | 1917 | assert(p->p_memstat_idledeadline); |
39037602 | 1918 | |
cb323159 A |
1919 | if (isSysProc(p) && present_in_sysprocs_aging_bucket == FALSE) { |
1920 | memorystatus_scheduled_idle_demotions_sysprocs++; | |
1921 | } else if (isApp(p) && present_in_apps_aging_bucket == FALSE) { | |
1922 | memorystatus_scheduled_idle_demotions_apps++; | |
39037602 | 1923 | } |
cb323159 | 1924 | } |
3e170ce0 | 1925 | |
cb323159 A |
1926 | void |
1927 | memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) | |
1928 | { | |
1929 | boolean_t present_in_sysprocs_aging_bucket = FALSE; | |
1930 | boolean_t present_in_apps_aging_bucket = FALSE; | |
0a7de745 | 1931 | |
cb323159 A |
1932 | if (!system_procs_aging_band && !applications_aging_band) { |
1933 | return; | |
5ba3f43e A |
1934 | } |
1935 | ||
cb323159 A |
1936 | if ((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) == 0) { |
1937 | return; | |
1938 | } | |
5ba3f43e | 1939 | |
cb323159 A |
1940 | if (isProcessInAgingBands(p)) { |
1941 | if (jetsam_aging_policy != kJetsamAgingPolicyLegacy) { | |
1942 | assert((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) == P_DIRTY_AGING_IN_PROGRESS); | |
1943 | } | |
5ba3f43e | 1944 | |
cb323159 A |
1945 | if (isSysProc(p) && system_procs_aging_band) { |
1946 | assert(p->p_memstat_effectivepriority == system_procs_aging_band); | |
1947 | assert(p->p_memstat_idledeadline); | |
1948 | present_in_sysprocs_aging_bucket = TRUE; | |
1949 | } else if (isApp(p) && applications_aging_band) { | |
1950 | assert(p->p_memstat_effectivepriority == applications_aging_band); | |
1951 | assert(p->p_memstat_idledeadline); | |
1952 | present_in_apps_aging_bucket = TRUE; | |
1953 | } | |
1954 | } | |
5ba3f43e | 1955 | |
cb323159 A |
1956 | MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for pid %d (clear_state %d, demotions %d).\n", |
1957 | p->p_pid, clear_state, (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)); | |
d9a64523 | 1958 | |
d9a64523 | 1959 | |
cb323159 A |
1960 | if (clear_state) { |
1961 | p->p_memstat_idledeadline = 0; | |
1962 | p->p_memstat_dirty &= ~P_DIRTY_AGING_IN_PROGRESS; | |
39236c6e A |
1963 | } |
1964 | ||
cb323159 A |
1965 | if (isSysProc(p) && present_in_sysprocs_aging_bucket == TRUE) { |
1966 | memorystatus_scheduled_idle_demotions_sysprocs--; | |
1967 | assert(memorystatus_scheduled_idle_demotions_sysprocs >= 0); | |
1968 | } else if (isApp(p) && present_in_apps_aging_bucket == TRUE) { | |
1969 | memorystatus_scheduled_idle_demotions_apps--; | |
1970 | assert(memorystatus_scheduled_idle_demotions_apps >= 0); | |
d9a64523 A |
1971 | } |
1972 | ||
cb323159 A |
1973 | assert((memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps) >= 0); |
1974 | } | |
0a7de745 | 1975 | |
cb323159 A |
1976 | static void |
1977 | memorystatus_reschedule_idle_demotion_locked(void) | |
1978 | { | |
1979 | if (0 == (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)) { | |
1980 | if (memstat_idle_demotion_deadline) { | |
1981 | /* Transitioned 1->0, so cancel next call */ | |
1982 | thread_call_cancel(memorystatus_idle_demotion_call); | |
1983 | memstat_idle_demotion_deadline = 0; | |
1984 | } | |
1985 | } else { | |
1986 | memstat_bucket_t *demotion_bucket; | |
1987 | proc_t p = NULL, p1 = NULL, p2 = NULL; | |
d9a64523 | 1988 | |
cb323159 A |
1989 | if (system_procs_aging_band) { |
1990 | demotion_bucket = &memstat_bucket[system_procs_aging_band]; | |
1991 | p1 = TAILQ_FIRST(&demotion_bucket->list); | |
d9a64523 | 1992 | |
cb323159 A |
1993 | p = p1; |
1994 | } | |
d9a64523 | 1995 | |
cb323159 A |
1996 | if (applications_aging_band) { |
1997 | demotion_bucket = &memstat_bucket[applications_aging_band]; | |
1998 | p2 = TAILQ_FIRST(&demotion_bucket->list); | |
d9a64523 | 1999 | |
cb323159 A |
2000 | if (p1 && p2) { |
2001 | p = (p1->p_memstat_idledeadline > p2->p_memstat_idledeadline) ? p2 : p1; | |
2002 | } else { | |
2003 | p = (p1 == NULL) ? p2 : p1; | |
2004 | } | |
2005 | } | |
d9a64523 | 2006 | |
cb323159 A |
2007 | assert(p); |
2008 | ||
2009 | if (p != NULL) { | |
2010 | assert(p && p->p_memstat_idledeadline); | |
2011 | if (memstat_idle_demotion_deadline != p->p_memstat_idledeadline) { | |
2012 | thread_call_enter_delayed(memorystatus_idle_demotion_call, p->p_memstat_idledeadline); | |
2013 | memstat_idle_demotion_deadline = p->p_memstat_idledeadline; | |
2014 | } | |
d9a64523 | 2015 | } |
316670eb | 2016 | } |
39236c6e | 2017 | } |
316670eb | 2018 | |
fe8ab488 | 2019 | /* |
cb323159 | 2020 | * List manipulation |
fe8ab488 | 2021 | */ |
fe8ab488 | 2022 | |
cb323159 A |
2023 | int |
2024 | memorystatus_add(proc_t p, boolean_t locked) | |
0a7de745 | 2025 | { |
cb323159 | 2026 | memstat_bucket_t *bucket; |
fe8ab488 | 2027 | |
cb323159 | 2028 | MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding pid %d with priority %d.\n", p->p_pid, p->p_memstat_effectivepriority); |
39236c6e | 2029 | |
cb323159 A |
2030 | if (!locked) { |
2031 | proc_list_lock(); | |
316670eb | 2032 | } |
5ba3f43e | 2033 | |
cb323159 A |
2034 | DTRACE_MEMORYSTATUS2(memorystatus_add, proc_t, p, int32_t, p->p_memstat_effectivepriority); |
2035 | ||
2036 | /* Processes marked internal do not have priority tracked */ | |
2037 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { | |
2038 | goto exit; | |
5ba3f43e A |
2039 | } |
2040 | ||
d9a64523 | 2041 | /* |
cb323159 A |
2042 | * Opt out system processes from being frozen by default. |
2043 | * For coalition-based freezing, we only want to freeze sysprocs that have specifically opted in. | |
d9a64523 | 2044 | */ |
cb323159 A |
2045 | if (isSysProc(p)) { |
2046 | p->p_memstat_state |= P_MEMSTAT_FREEZE_DISABLED; | |
39236c6e | 2047 | } |
2a1bd2d3 A |
2048 | #if CONFIG_FREEZE |
2049 | memorystatus_freeze_init_proc(p); | |
2050 | #endif | |
fe8ab488 | 2051 | |
cb323159 | 2052 | bucket = &memstat_bucket[p->p_memstat_effectivepriority]; |
39236c6e | 2053 | |
cb323159 A |
2054 | if (isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) { |
2055 | assert(bucket->count == memorystatus_scheduled_idle_demotions_sysprocs - 1); | |
2056 | } else if (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band)) { | |
2057 | assert(bucket->count == memorystatus_scheduled_idle_demotions_apps - 1); | |
2058 | } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { | |
2059 | /* | |
2060 | * Entering the idle band. | |
2061 | * Record idle start time. | |
2062 | */ | |
2063 | p->p_memstat_idle_start = mach_absolute_time(); | |
2064 | } | |
fe8ab488 | 2065 | |
cb323159 A |
2066 | TAILQ_INSERT_TAIL(&bucket->list, p, p_memstat_list); |
2067 | bucket->count++; | |
2068 | if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) { | |
2069 | bucket->relaunch_high_count++; | |
2070 | } | |
316670eb | 2071 | |
cb323159 | 2072 | memorystatus_list_count++; |
316670eb | 2073 | |
cb323159 A |
2074 | memorystatus_check_levels_locked(); |
2075 | ||
2076 | exit: | |
2077 | if (!locked) { | |
2078 | proc_list_unlock(); | |
2079 | } | |
2080 | ||
2081 | return 0; | |
39236c6e | 2082 | } |
316670eb | 2083 | |
0a7de745 | 2084 | /* |
cb323159 A |
2085 | * Description: |
2086 | * Moves a process from one jetsam bucket to another. | |
2087 | * which changes the LRU position of the process. | |
2088 | * | |
2089 | * Monitors transition between buckets and if necessary | |
2090 | * will update cached memory limits accordingly. | |
2091 | * | |
2092 | * skip_demotion_check: | |
2093 | * - if the 'jetsam aging policy' is NOT 'legacy': | |
2094 | * When this flag is TRUE, it means we are going | |
2095 | * to age the ripe processes out of the aging bands and into the | |
2096 | * IDLE band and apply their inactive memory limits. | |
2097 | * | |
2098 | * - if the 'jetsam aging policy' is 'legacy': | |
2099 | * When this flag is TRUE, it might mean the above aging mechanism | |
2100 | * OR | |
2101 | * It might be that we have a process that has used up its 'idle deferral' | |
2102 | * stay that is given to it once per lifetime. And in this case, the process | |
2103 | * won't be going through any aging codepaths. But we still need to apply | |
2104 | * the right inactive limits and so we explicitly set this to TRUE if the | |
2105 | * new priority for the process is the IDLE band. | |
39037602 | 2106 | */ |
cb323159 A |
2107 | void |
2108 | memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check) | |
39037602 | 2109 | { |
cb323159 | 2110 | memstat_bucket_t *old_bucket, *new_bucket; |
39037602 | 2111 | |
cb323159 A |
2112 | assert(priority < MEMSTAT_BUCKET_COUNT); |
2113 | ||
2114 | /* Ensure that exit isn't underway, leaving the proc retained but removed from its bucket */ | |
2115 | if ((p->p_listflag & P_LIST_EXITED) != 0) { | |
2116 | return; | |
39037602 A |
2117 | } |
2118 | ||
cb323159 A |
2119 | MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting %s(%d) to priority %d, inserting at %s\n", |
2120 | (*p->p_name ? p->p_name : "unknown"), p->p_pid, priority, head_insert ? "head" : "tail"); | |
2121 | ||
2122 | DTRACE_MEMORYSTATUS3(memorystatus_update_priority, proc_t, p, int32_t, p->p_memstat_effectivepriority, int, priority); | |
2123 | ||
2124 | old_bucket = &memstat_bucket[p->p_memstat_effectivepriority]; | |
2125 | ||
2126 | if (skip_demotion_check == FALSE) { | |
2127 | if (isSysProc(p)) { | |
39037602 | 2128 | /* |
cb323159 A |
2129 | * For system processes, the memorystatus_dirty_* routines take care of adding/removing |
2130 | * the processes from the aging bands and balancing the demotion counts. | |
2131 | * We can, however, override that if the process has an 'elevated inactive jetsam band' attribute. | |
39037602 | 2132 | */ |
39037602 | 2133 | |
cb323159 A |
2134 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { |
2135 | /* | |
2136 | * 2 types of processes can use the non-standard elevated inactive band: | |
2137 | * - Frozen processes that always land in memorystatus_freeze_jetsam_band | |
2138 | * OR | |
2139 | * - processes that specifically opt-in to the elevated inactive support e.g. docked processes. | |
2140 | */ | |
2141 | #if CONFIG_FREEZE | |
2142 | if (p->p_memstat_state & P_MEMSTAT_FROZEN) { | |
2143 | if (priority <= memorystatus_freeze_jetsam_band) { | |
2144 | priority = memorystatus_freeze_jetsam_band; | |
2145 | } | |
2146 | } else | |
2147 | #endif /* CONFIG_FREEZE */ | |
2148 | { | |
2149 | if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) { | |
2150 | priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; | |
39037602 A |
2151 | } |
2152 | } | |
cb323159 A |
2153 | assert(!(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); |
2154 | } | |
2155 | } else if (isApp(p)) { | |
2156 | /* | |
2157 | * Check to see if the application is being lowered in jetsam priority. If so, and: | |
2158 | * - it has an 'elevated inactive jetsam band' attribute, then put it in the appropriate band. | |
2159 | * - it is a normal application, then let it age in the aging band if that policy is in effect. | |
2160 | */ | |
39037602 | 2161 | |
cb323159 A |
2162 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { |
2163 | #if CONFIG_FREEZE | |
2164 | if (p->p_memstat_state & P_MEMSTAT_FROZEN) { | |
2165 | if (priority <= memorystatus_freeze_jetsam_band) { | |
2166 | priority = memorystatus_freeze_jetsam_band; | |
39037602 | 2167 | } |
cb323159 A |
2168 | } else |
2169 | #endif /* CONFIG_FREEZE */ | |
2170 | { | |
2171 | if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) { | |
2172 | priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; | |
39037602 A |
2173 | } |
2174 | } | |
cb323159 A |
2175 | } else { |
2176 | if (applications_aging_band) { | |
2177 | if (p->p_memstat_effectivepriority == applications_aging_band) { | |
2178 | assert(old_bucket->count == (memorystatus_scheduled_idle_demotions_apps + 1)); | |
2179 | } | |
39037602 | 2180 | |
cb323159 A |
2181 | if ((jetsam_aging_policy != kJetsamAgingPolicyLegacy) && (priority <= applications_aging_band)) { |
2182 | assert(!(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); | |
2183 | priority = applications_aging_band; | |
2184 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
2185 | } | |
2186 | } | |
2187 | } | |
39037602 | 2188 | } |
39037602 A |
2189 | } |
2190 | ||
cb323159 A |
2191 | if ((system_procs_aging_band && (priority == system_procs_aging_band)) || (applications_aging_band && (priority == applications_aging_band))) { |
2192 | assert(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS); | |
2193 | } | |
0a7de745 | 2194 | |
cb323159 A |
2195 | #if DEVELOPMENT || DEBUG |
2196 | if (priority == JETSAM_PRIORITY_IDLE && /* if the process is on its way into the IDLE band */ | |
2197 | skip_demotion_check == FALSE && /* and it isn't via the path that will set the INACTIVE memlimits */ | |
2198 | (p->p_memstat_dirty & P_DIRTY_TRACK) && /* and it has 'DIRTY' tracking enabled */ | |
2199 | ((p->p_memstat_memlimit != p->p_memstat_memlimit_inactive) || /* and we notice that the current limit isn't the right value (inactive) */ | |
2200 | ((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) */ | |
2201 | printf("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 */ | |
2202 | } | |
2203 | #endif /* DEVELOPMENT || DEBUG */ | |
39037602 | 2204 | |
cb323159 A |
2205 | TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list); |
2206 | old_bucket->count--; | |
2207 | if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) { | |
2208 | old_bucket->relaunch_high_count--; | |
2209 | } | |
39037602 | 2210 | |
cb323159 A |
2211 | new_bucket = &memstat_bucket[priority]; |
2212 | if (head_insert) { | |
2213 | TAILQ_INSERT_HEAD(&new_bucket->list, p, p_memstat_list); | |
2214 | } else { | |
2215 | TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list); | |
2216 | } | |
2217 | new_bucket->count++; | |
2218 | if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) { | |
2219 | new_bucket->relaunch_high_count++; | |
2220 | } | |
39037602 | 2221 | |
cb323159 A |
2222 | if (memorystatus_highwater_enabled) { |
2223 | boolean_t is_fatal; | |
2224 | boolean_t use_active; | |
39037602 | 2225 | |
cb323159 A |
2226 | /* |
2227 | * If cached limit data is updated, then the limits | |
2228 | * will be enforced by writing to the ledgers. | |
2229 | */ | |
2230 | boolean_t ledger_update_needed = TRUE; | |
39037602 | 2231 | |
cb323159 A |
2232 | /* |
2233 | * Here, we must update the cached memory limit if the task | |
2234 | * is transitioning between: | |
2235 | * active <--> inactive | |
2236 | * FG <--> BG | |
2237 | * but: | |
2238 | * dirty <--> clean is ignored | |
2239 | * | |
2240 | * We bypass non-idle processes that have opted into dirty tracking because | |
2241 | * a move between buckets does not imply a transition between the | |
2242 | * dirty <--> clean state. | |
2243 | */ | |
0a7de745 | 2244 | |
cb323159 A |
2245 | if (p->p_memstat_dirty & P_DIRTY_TRACK) { |
2246 | if (skip_demotion_check == TRUE && priority == JETSAM_PRIORITY_IDLE) { | |
2247 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); | |
2248 | use_active = FALSE; | |
39037602 | 2249 | } else { |
cb323159 | 2250 | ledger_update_needed = FALSE; |
39037602 | 2251 | } |
cb323159 A |
2252 | } else if ((priority >= JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority < JETSAM_PRIORITY_FOREGROUND)) { |
2253 | /* | |
2254 | * inactive --> active | |
2255 | * BG --> FG | |
2256 | * assign active state | |
2257 | */ | |
2258 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
2259 | use_active = TRUE; | |
2260 | } else if ((priority < JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) { | |
2261 | /* | |
2262 | * active --> inactive | |
2263 | * FG --> BG | |
2264 | * assign inactive state | |
2265 | */ | |
2266 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); | |
2267 | use_active = FALSE; | |
2268 | } else { | |
2269 | /* | |
2270 | * The transition between jetsam priority buckets apparently did | |
2271 | * not affect active/inactive state. | |
2272 | * This is not unusual... especially during startup when | |
2273 | * processes are getting established in their respective bands. | |
2274 | */ | |
2275 | ledger_update_needed = FALSE; | |
316670eb | 2276 | } |
316670eb | 2277 | |
cb323159 A |
2278 | /* |
2279 | * Enforce the new limits by writing to the ledger | |
2280 | */ | |
2281 | if (ledger_update_needed) { | |
2282 | task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, use_active, is_fatal); | |
39037602 | 2283 | |
cb323159 A |
2284 | MEMORYSTATUS_DEBUG(3, "memorystatus_update_priority_locked: new limit on pid %d (%dMB %s) priority old --> new (%d --> %d) dirty?=0x%x %s\n", |
2285 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), | |
2286 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, priority, p->p_memstat_dirty, | |
2287 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
2288 | } | |
39037602 A |
2289 | } |
2290 | ||
cb323159 A |
2291 | /* |
2292 | * Record idle start or idle delta. | |
2293 | */ | |
2294 | if (p->p_memstat_effectivepriority == priority) { | |
39037602 | 2295 | /* |
cb323159 A |
2296 | * This process is not transitioning between |
2297 | * jetsam priority buckets. Do nothing. | |
39037602 | 2298 | */ |
cb323159 A |
2299 | } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { |
2300 | uint64_t now; | |
2301 | /* | |
2302 | * Transitioning out of the idle priority bucket. | |
2303 | * Record idle delta. | |
2304 | */ | |
2305 | assert(p->p_memstat_idle_start != 0); | |
2306 | now = mach_absolute_time(); | |
2307 | if (now > p->p_memstat_idle_start) { | |
2308 | p->p_memstat_idle_delta = now - p->p_memstat_idle_start; | |
39037602 A |
2309 | } |
2310 | ||
cb323159 A |
2311 | /* |
2312 | * About to become active and so memory footprint could change. | |
2313 | * So mark it eligible for freeze-considerations next time around. | |
2314 | */ | |
2315 | if (p->p_memstat_state & P_MEMSTAT_FREEZE_IGNORE) { | |
2316 | p->p_memstat_state &= ~P_MEMSTAT_FREEZE_IGNORE; | |
39037602 | 2317 | } |
cb323159 A |
2318 | } else if (priority == JETSAM_PRIORITY_IDLE) { |
2319 | /* | |
2320 | * Transitioning into the idle priority bucket. | |
2321 | * Record idle start. | |
2322 | */ | |
2323 | p->p_memstat_idle_start = mach_absolute_time(); | |
39037602 A |
2324 | } |
2325 | ||
cb323159 | 2326 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CHANGE_PRIORITY), p->p_pid, priority, p->p_memstat_effectivepriority, 0, 0); |
39037602 | 2327 | |
cb323159 | 2328 | p->p_memstat_effectivepriority = priority; |
316670eb | 2329 | |
cb323159 A |
2330 | #if CONFIG_SECLUDED_MEMORY |
2331 | if (secluded_for_apps && | |
2332 | task_could_use_secluded_mem(p->task)) { | |
2333 | task_set_can_use_secluded_mem( | |
2334 | p->task, | |
2335 | (priority >= JETSAM_PRIORITY_FOREGROUND)); | |
39037602 | 2336 | } |
cb323159 | 2337 | #endif /* CONFIG_SECLUDED_MEMORY */ |
39037602 | 2338 | |
cb323159 A |
2339 | memorystatus_check_levels_locked(); |
2340 | } | |
316670eb | 2341 | |
cb323159 A |
2342 | int |
2343 | memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags) | |
2344 | { | |
2345 | p->p_memstat_relaunch_flags = relaunch_flags; | |
2346 | KDBG(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_RELAUNCH_FLAGS), p->p_pid, relaunch_flags, 0, 0, 0); | |
2347 | return 0; | |
316670eb A |
2348 | } |
2349 | ||
cb323159 A |
2350 | /* |
2351 | * | |
2352 | * Description: Update the jetsam priority and memory limit attributes for a given process. | |
2353 | * | |
2354 | * Parameters: | |
2355 | * p init this process's jetsam information. | |
2356 | * priority The jetsam priority band | |
2357 | * user_data user specific data, unused by the kernel | |
2358 | * is_assertion When true, a priority update is driven by an assertion. | |
2359 | * effective guards against race if process's update already occurred | |
2360 | * update_memlimit When true we know this is the init step via the posix_spawn path. | |
2361 | * | |
2362 | * memlimit_active Value in megabytes; The monitored footprint level while the | |
2363 | * process is active. Exceeding it may result in termination | |
2364 | * based on it's associated fatal flag. | |
2365 | * | |
2366 | * memlimit_active_is_fatal When a process is active and exceeds its memory footprint, | |
2367 | * this describes whether or not it should be immediately fatal. | |
2368 | * | |
2369 | * memlimit_inactive Value in megabytes; The monitored footprint level while the | |
2370 | * process is inactive. Exceeding it may result in termination | |
2371 | * based on it's associated fatal flag. | |
2372 | * | |
2373 | * memlimit_inactive_is_fatal When a process is inactive and exceeds its memory footprint, | |
2374 | * this describes whether or not it should be immediatly fatal. | |
2375 | * | |
2376 | * Returns: 0 Success | |
2377 | * non-0 Failure | |
2378 | */ | |
39037602 | 2379 | |
cb323159 A |
2380 | int |
2381 | memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t is_assertion, boolean_t effective, boolean_t update_memlimit, | |
2382 | int32_t memlimit_active, boolean_t memlimit_active_is_fatal, | |
2383 | int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal) | |
2384 | { | |
2385 | int ret; | |
2386 | boolean_t head_insert = false; | |
39037602 | 2387 | |
cb323159 | 2388 | 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); |
39037602 | 2389 | |
cb323159 | 2390 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_START, p->p_pid, priority, user_data, effective, 0); |
39037602 | 2391 | |
cb323159 A |
2392 | if (priority == -1) { |
2393 | /* Use as shorthand for default priority */ | |
2394 | priority = JETSAM_PRIORITY_DEFAULT; | |
2395 | } else if ((priority == system_procs_aging_band) || (priority == applications_aging_band)) { | |
2396 | /* Both the aging bands are reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */ | |
2397 | priority = JETSAM_PRIORITY_IDLE; | |
2398 | } else if (priority == JETSAM_PRIORITY_IDLE_HEAD) { | |
2399 | /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */ | |
2400 | priority = JETSAM_PRIORITY_IDLE; | |
2401 | head_insert = TRUE; | |
2402 | } else if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) { | |
2403 | /* Sanity check */ | |
2404 | ret = EINVAL; | |
2405 | goto out; | |
fe8ab488 A |
2406 | } |
2407 | ||
cb323159 | 2408 | proc_list_lock(); |
0a7de745 | 2409 | |
cb323159 | 2410 | assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL)); |
0a7de745 | 2411 | |
cb323159 A |
2412 | if (effective && (p->p_memstat_state & P_MEMSTAT_PRIORITYUPDATED)) { |
2413 | ret = EALREADY; | |
2414 | proc_list_unlock(); | |
2415 | MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", p->p_pid); | |
2416 | goto out; | |
316670eb | 2417 | } |
0a7de745 | 2418 | |
cb323159 A |
2419 | if ((p->p_memstat_state & P_MEMSTAT_TERMINATED) || ((p->p_listflag & P_LIST_EXITED) != 0)) { |
2420 | /* | |
2421 | * This could happen when a process calling posix_spawn() is exiting on the jetsam thread. | |
2422 | */ | |
2423 | ret = EBUSY; | |
2424 | proc_list_unlock(); | |
2425 | goto out; | |
fe8ab488 A |
2426 | } |
2427 | ||
cb323159 A |
2428 | p->p_memstat_state |= P_MEMSTAT_PRIORITYUPDATED; |
2429 | p->p_memstat_userdata = user_data; | |
316670eb | 2430 | |
cb323159 A |
2431 | if (is_assertion) { |
2432 | if (priority == JETSAM_PRIORITY_IDLE) { | |
2433 | /* | |
2434 | * Assertions relinquish control when the process is heading to IDLE. | |
2435 | */ | |
2436 | if (p->p_memstat_state & P_MEMSTAT_PRIORITY_ASSERTION) { | |
2437 | /* | |
2438 | * Mark the process as no longer being managed by assertions. | |
2439 | */ | |
2440 | p->p_memstat_state &= ~P_MEMSTAT_PRIORITY_ASSERTION; | |
2441 | } else { | |
2442 | /* | |
2443 | * Ignore an idle priority transition if the process is not | |
2444 | * already managed by assertions. We won't treat this as | |
2445 | * an error, but we will log the unexpected behavior and bail. | |
2446 | */ | |
2447 | os_log(OS_LOG_DEFAULT, "memorystatus: Ignore assertion driven idle priority. Process not previously controlled %s:%d\n", | |
2448 | (*p->p_name ? p->p_name : "unknown"), p->p_pid); | |
2449 | ||
2450 | ret = 0; | |
2451 | proc_list_unlock(); | |
2452 | goto out; | |
2453 | } | |
2454 | } else { | |
2455 | /* | |
2456 | * Process is now being managed by assertions, | |
2457 | */ | |
2458 | p->p_memstat_state |= P_MEMSTAT_PRIORITY_ASSERTION; | |
0a7de745 | 2459 | } |
0a7de745 | 2460 | |
cb323159 | 2461 | /* Always update the assertion priority in this path */ |
39037602 | 2462 | |
cb323159 | 2463 | p->p_memstat_assertionpriority = priority; |
39037602 | 2464 | |
cb323159 | 2465 | int memstat_dirty_flags = memorystatus_dirty_get(p, TRUE); /* proc_list_lock is held */ |
39037602 | 2466 | |
cb323159 A |
2467 | if (memstat_dirty_flags != 0) { |
2468 | /* | |
2469 | * Calculate maximum priority only when dirty tracking processes are involved. | |
2470 | */ | |
2471 | int maxpriority; | |
2472 | if (memstat_dirty_flags & PROC_DIRTY_IS_DIRTY) { | |
2473 | maxpriority = MAX(p->p_memstat_assertionpriority, p->p_memstat_requestedpriority); | |
39037602 | 2474 | } else { |
cb323159 | 2475 | /* clean */ |
39037602 | 2476 | |
cb323159 A |
2477 | if (memstat_dirty_flags & PROC_DIRTY_ALLOWS_IDLE_EXIT) { |
2478 | /* | |
2479 | * The aging policy must be evaluated and applied here because runnningboardd | |
2480 | * has relinquished its hold on the jetsam priority by attempting to move a | |
2481 | * clean process to the idle band. | |
2482 | */ | |
39037602 | 2483 | |
cb323159 A |
2484 | int newpriority = JETSAM_PRIORITY_IDLE; |
2485 | if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) { | |
2486 | newpriority = (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) ? system_procs_aging_band : JETSAM_PRIORITY_IDLE; | |
2487 | } | |
2488 | ||
2489 | maxpriority = MAX(p->p_memstat_assertionpriority, newpriority ); | |
2490 | ||
2491 | if (newpriority == system_procs_aging_band) { | |
2492 | memorystatus_schedule_idle_demotion_locked(p, FALSE); | |
2493 | } | |
2494 | } else { | |
2495 | /* | |
2496 | * Preserves requestedpriority when the process does not support pressured exit. | |
2497 | */ | |
2498 | maxpriority = MAX(p->p_memstat_assertionpriority, p->p_memstat_requestedpriority); | |
2499 | } | |
39037602 | 2500 | } |
cb323159 | 2501 | priority = maxpriority; |
39236c6e | 2502 | } |
cb323159 A |
2503 | } else { |
2504 | p->p_memstat_requestedpriority = priority; | |
0a7de745 | 2505 | } |
316670eb | 2506 | |
cb323159 A |
2507 | if (update_memlimit) { |
2508 | boolean_t is_fatal; | |
2509 | boolean_t use_active; | |
0a7de745 | 2510 | |
cb323159 A |
2511 | /* |
2512 | * Posix_spawn'd processes come through this path to instantiate ledger limits. | |
2513 | * Forked processes do not come through this path, so no ledger limits exist. | |
2514 | * (That's why forked processes can consume unlimited memory.) | |
2515 | */ | |
0a7de745 | 2516 | |
cb323159 A |
2517 | MEMORYSTATUS_DEBUG(3, "memorystatus_update(enter): pid %d, priority %d, dirty=0x%x, Active(%dMB %s), Inactive(%dMB, %s)\n", |
2518 | p->p_pid, priority, p->p_memstat_dirty, | |
2519 | memlimit_active, (memlimit_active_is_fatal ? "F " : "NF"), | |
2520 | memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF")); | |
39037602 | 2521 | |
cb323159 A |
2522 | if (memlimit_active <= 0) { |
2523 | /* | |
2524 | * This process will have a system_wide task limit when active. | |
2525 | * System_wide task limit is always fatal. | |
2526 | * It's quite common to see non-fatal flag passed in here. | |
2527 | * It's not an error, we just ignore it. | |
2528 | */ | |
39037602 | 2529 | |
cb323159 A |
2530 | /* |
2531 | * For backward compatibility with some unexplained launchd behavior, | |
2532 | * we allow a zero sized limit. But we still enforce system_wide limit | |
2533 | * when written to the ledgers. | |
2534 | */ | |
39037602 | 2535 | |
cb323159 A |
2536 | if (memlimit_active < 0) { |
2537 | memlimit_active = -1; /* enforces system_wide task limit */ | |
2538 | } | |
2539 | memlimit_active_is_fatal = TRUE; | |
2540 | } | |
0a7de745 | 2541 | |
cb323159 A |
2542 | if (memlimit_inactive <= 0) { |
2543 | /* | |
2544 | * This process will have a system_wide task limit when inactive. | |
2545 | * System_wide task limit is always fatal. | |
2546 | */ | |
2547 | ||
2548 | memlimit_inactive = -1; | |
2549 | memlimit_inactive_is_fatal = TRUE; | |
2550 | } | |
0a7de745 | 2551 | |
39037602 | 2552 | /* |
cb323159 | 2553 | * Initialize the active limit variants for this process. |
39037602 | 2554 | */ |
cb323159 | 2555 | SET_ACTIVE_LIMITS_LOCKED(p, memlimit_active, memlimit_active_is_fatal); |
316670eb | 2556 | |
cb323159 A |
2557 | /* |
2558 | * Initialize the inactive limit variants for this process. | |
2559 | */ | |
2560 | SET_INACTIVE_LIMITS_LOCKED(p, memlimit_inactive, memlimit_inactive_is_fatal); | |
316670eb | 2561 | |
cb323159 A |
2562 | /* |
2563 | * Initialize the cached limits for target process. | |
2564 | * When the target process is dirty tracked, it's typically | |
2565 | * in a clean state. Non dirty tracked processes are | |
2566 | * typically active (Foreground or above). | |
2567 | * But just in case, we don't make assumptions... | |
2568 | */ | |
0a7de745 | 2569 | |
cb323159 A |
2570 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { |
2571 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
2572 | use_active = TRUE; | |
2573 | } else { | |
2574 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); | |
2575 | use_active = FALSE; | |
2576 | } | |
2577 | ||
2578 | /* | |
2579 | * Enforce the cached limit by writing to the ledger. | |
2580 | */ | |
2581 | if (memorystatus_highwater_enabled) { | |
2582 | /* apply now */ | |
2583 | task_set_phys_footprint_limit_internal(p->task, ((p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1), NULL, use_active, is_fatal); | |
2584 | ||
2585 | MEMORYSTATUS_DEBUG(3, "memorystatus_update: init: limit on pid %d (%dMB %s) targeting priority(%d) dirty?=0x%x %s\n", | |
2586 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), | |
2587 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), priority, p->p_memstat_dirty, | |
2588 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
2589 | } | |
0a7de745 A |
2590 | } |
2591 | ||
cb323159 A |
2592 | /* |
2593 | * We can't add to the aging bands buckets here. | |
2594 | * But, we could be removing it from those buckets. | |
2595 | * Check and take appropriate steps if so. | |
2596 | */ | |
2597 | ||
2598 | if (isProcessInAgingBands(p)) { | |
2599 | if ((jetsam_aging_policy != kJetsamAgingPolicyLegacy) && isApp(p) && (priority > applications_aging_band)) { | |
2600 | /* | |
2601 | * Runningboardd is pulling up an application that is in the aging band. | |
2602 | * We reset the app's state here so that it'll get a fresh stay in the | |
2603 | * aging band on the way back. | |
2604 | * | |
2605 | * We always handled the app 'aging' in the memorystatus_update_priority_locked() | |
2606 | * function. Daemons used to be handled via the dirty 'set/clear/track' path. | |
2607 | * But with extensions (daemon-app hybrid), runningboardd is now going through | |
2608 | * this routine for daemons too and things have gotten a bit tangled. This should | |
2609 | * be simplified/untangled at some point and might require some assistance from | |
2610 | * runningboardd. | |
2611 | */ | |
2612 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2613 | } else { | |
2614 | memorystatus_invalidate_idle_demotion_locked(p, FALSE); | |
2615 | } | |
2616 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); | |
2617 | } else { | |
2618 | if (jetsam_aging_policy == kJetsamAgingPolicyLegacy && priority == JETSAM_PRIORITY_IDLE) { | |
2619 | /* | |
2620 | * Daemons with 'inactive' limits will go through the dirty tracking codepath. | |
2621 | * This path deals with apps that may have 'inactive' limits e.g. WebContent processes. | |
2622 | * If this is the legacy aging policy we explicitly need to apply those limits. If it | |
2623 | * is any other aging policy, then we don't need to worry because all processes | |
2624 | * will go through the aging bands and then the demotion thread will take care to | |
2625 | * move them into the IDLE band and apply the required limits. | |
2626 | */ | |
2627 | memorystatus_update_priority_locked(p, priority, head_insert, TRUE); | |
2628 | } | |
2629 | } | |
2630 | ||
2631 | memorystatus_update_priority_locked(p, priority, head_insert, FALSE); | |
2632 | ||
2633 | proc_list_unlock(); | |
2634 | ret = 0; | |
2635 | ||
2636 | out: | |
2637 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_END, ret, 0, 0, 0, 0); | |
2638 | ||
2639 | return ret; | |
39236c6e | 2640 | } |
316670eb | 2641 | |
cb323159 A |
2642 | int |
2643 | memorystatus_remove(proc_t p) | |
39236c6e | 2644 | { |
cb323159 A |
2645 | int ret; |
2646 | memstat_bucket_t *bucket; | |
2647 | boolean_t reschedule = FALSE; | |
0a7de745 | 2648 | |
cb323159 | 2649 | MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing pid %d\n", p->p_pid); |
0a7de745 | 2650 | |
cb323159 A |
2651 | /* |
2652 | * Check if this proc is locked (because we're performing a freeze). | |
2653 | * If so, we fail and instruct the caller to try again later. | |
2654 | */ | |
2655 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { | |
2656 | return EAGAIN; | |
316670eb | 2657 | } |
39037602 | 2658 | |
cb323159 | 2659 | assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL)); |
39037602 | 2660 | |
cb323159 | 2661 | bucket = &memstat_bucket[p->p_memstat_effectivepriority]; |
39037602 | 2662 | |
cb323159 A |
2663 | if (isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) { |
2664 | assert(bucket->count == memorystatus_scheduled_idle_demotions_sysprocs); | |
2665 | reschedule = TRUE; | |
2666 | } else if (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band)) { | |
2667 | assert(bucket->count == memorystatus_scheduled_idle_demotions_apps); | |
2668 | reschedule = TRUE; | |
0a7de745 | 2669 | } |
316670eb | 2670 | |
cb323159 A |
2671 | /* |
2672 | * Record idle delta | |
2673 | */ | |
39037602 | 2674 | |
cb323159 A |
2675 | if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { |
2676 | uint64_t now = mach_absolute_time(); | |
2677 | if (now > p->p_memstat_idle_start) { | |
2678 | p->p_memstat_idle_delta = now - p->p_memstat_idle_start; | |
2679 | } | |
2680 | } | |
39037602 | 2681 | |
cb323159 A |
2682 | TAILQ_REMOVE(&bucket->list, p, p_memstat_list); |
2683 | bucket->count--; | |
2684 | if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) { | |
2685 | bucket->relaunch_high_count--; | |
2686 | } | |
0a7de745 | 2687 | |
cb323159 | 2688 | memorystatus_list_count--; |
39037602 | 2689 | |
cb323159 A |
2690 | /* If awaiting demotion to the idle band, clean up */ |
2691 | if (reschedule) { | |
2692 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2693 | memorystatus_reschedule_idle_demotion_locked(); | |
39037602 A |
2694 | } |
2695 | ||
cb323159 A |
2696 | memorystatus_check_levels_locked(); |
2697 | ||
2698 | #if CONFIG_FREEZE | |
2699 | if (p->p_memstat_state & (P_MEMSTAT_FROZEN)) { | |
2700 | if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { | |
2701 | p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; | |
2702 | memorystatus_refreeze_eligible_count--; | |
2703 | } | |
2704 | ||
2705 | memorystatus_frozen_count--; | |
2706 | memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages; | |
2707 | p->p_memstat_freeze_sharedanon_pages = 0; | |
fe8ab488 A |
2708 | } |
2709 | ||
cb323159 A |
2710 | if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { |
2711 | memorystatus_suspended_count--; | |
2712 | } | |
2713 | #endif | |
39037602 | 2714 | |
f427ee49 A |
2715 | #if DEVELOPMENT || DEBUG |
2716 | if (p->p_pid == memorystatus_snapshot_owner) { | |
2717 | memorystatus_snapshot_owner = 0; | |
2718 | } | |
2719 | #endif /* DEVELOPMENT || DEBUG */ | |
2720 | ||
cb323159 A |
2721 | if (p) { |
2722 | ret = 0; | |
0a7de745 | 2723 | } else { |
cb323159 | 2724 | ret = ESRCH; |
0a7de745 | 2725 | } |
3e170ce0 | 2726 | |
cb323159 A |
2727 | return ret; |
2728 | } | |
fe8ab488 | 2729 | |
cb323159 A |
2730 | /* |
2731 | * Validate dirty tracking flags with process state. | |
2732 | * | |
2733 | * Return: | |
2734 | * 0 on success | |
2735 | * non-0 on failure | |
2736 | * | |
2737 | * The proc_list_lock is held by the caller. | |
2738 | */ | |
fe8ab488 | 2739 | |
cb323159 A |
2740 | static int |
2741 | memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol) | |
2742 | { | |
2743 | /* See that the process isn't marked for termination */ | |
2744 | if (target_p->p_memstat_dirty & P_DIRTY_TERMINATED) { | |
2745 | return EBUSY; | |
2746 | } | |
3e170ce0 | 2747 | |
cb323159 A |
2748 | /* Idle exit requires that process be tracked */ |
2749 | if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) && | |
2750 | !(pcontrol & PROC_DIRTY_TRACK)) { | |
2751 | return EINVAL; | |
2752 | } | |
3e170ce0 | 2753 | |
cb323159 A |
2754 | /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */ |
2755 | if ((pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) && | |
2756 | !(pcontrol & PROC_DIRTY_TRACK)) { | |
2757 | return EINVAL; | |
39236c6e | 2758 | } |
3e170ce0 | 2759 | |
cb323159 A |
2760 | /* Only one type of DEFER behavior is allowed.*/ |
2761 | if ((pcontrol & PROC_DIRTY_DEFER) && | |
2762 | (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) { | |
2763 | return EINVAL; | |
2764 | } | |
2765 | ||
2766 | /* Deferral is only relevant if idle exit is specified */ | |
2767 | if (((pcontrol & PROC_DIRTY_DEFER) || | |
2768 | (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) && | |
2769 | !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) { | |
2770 | return EINVAL; | |
2771 | } | |
2772 | ||
2773 | return 0; | |
2774 | } | |
2775 | ||
2776 | static void | |
2777 | memorystatus_update_idle_priority_locked(proc_t p) | |
2778 | { | |
2779 | int32_t priority; | |
2780 | ||
2781 | MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p->p_pid, p->p_memstat_dirty); | |
2782 | ||
2783 | assert(isSysProc(p)); | |
2784 | ||
2785 | if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) { | |
2786 | priority = (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) ? system_procs_aging_band : JETSAM_PRIORITY_IDLE; | |
2787 | } else { | |
2788 | priority = p->p_memstat_requestedpriority; | |
2789 | } | |
d9a64523 | 2790 | |
cb323159 | 2791 | if (p->p_memstat_state & P_MEMSTAT_PRIORITY_ASSERTION) { |
d9a64523 | 2792 | /* |
cb323159 A |
2793 | * This process has a jetsam priority managed by an assertion. |
2794 | * Policy is to choose the max priority. | |
d9a64523 | 2795 | */ |
cb323159 A |
2796 | if (p->p_memstat_assertionpriority > priority) { |
2797 | os_log(OS_LOG_DEFAULT, "memorystatus: assertion priority %d overrides priority %d for %s:%d\n", | |
2798 | p->p_memstat_assertionpriority, priority, | |
2799 | (*p->p_name ? p->p_name : "unknown"), p->p_pid); | |
2800 | priority = p->p_memstat_assertionpriority; | |
d9a64523 | 2801 | } |
39037602 A |
2802 | } |
2803 | ||
cb323159 A |
2804 | if (priority != p->p_memstat_effectivepriority) { |
2805 | if ((jetsam_aging_policy == kJetsamAgingPolicyLegacy) && | |
2806 | (priority == JETSAM_PRIORITY_IDLE)) { | |
2807 | /* | |
2808 | * This process is on its way into the IDLE band. The system is | |
2809 | * using 'legacy' jetsam aging policy. That means, this process | |
2810 | * has already used up its idle-deferral aging time that is given | |
2811 | * once per its lifetime. So we need to set the INACTIVE limits | |
2812 | * explicitly because it won't be going through the demotion paths | |
2813 | * that take care to apply the limits appropriately. | |
2814 | */ | |
d9a64523 | 2815 | |
cb323159 A |
2816 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { |
2817 | /* | |
2818 | * This process has the 'elevated inactive jetsam band' attribute. | |
2819 | * So, there will be no trip to IDLE after all. | |
2820 | * Instead, we pin the process in the elevated band, | |
2821 | * where its ACTIVE limits will apply. | |
2822 | */ | |
39037602 | 2823 | |
cb323159 A |
2824 | priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; |
2825 | } | |
0a7de745 | 2826 | |
cb323159 A |
2827 | memorystatus_update_priority_locked(p, priority, false, true); |
2828 | } else { | |
2829 | memorystatus_update_priority_locked(p, priority, false, false); | |
2830 | } | |
2831 | } | |
316670eb A |
2832 | } |
2833 | ||
3e170ce0 | 2834 | /* |
cb323159 A |
2835 | * Processes can opt to have their state tracked by the kernel, indicating when they are busy (dirty) or idle |
2836 | * (clean). They may also indicate that they support termination when idle, with the result that they are promoted | |
2837 | * to their desired, higher, jetsam priority when dirty (and are therefore killed later), and demoted to the low | |
2838 | * priority idle band when clean (and killed earlier, protecting higher priority procesess). | |
3e170ce0 | 2839 | * |
cb323159 A |
2840 | * If the deferral flag is set, then newly tracked processes will be protected for an initial period (as determined by |
2841 | * memorystatus_sysprocs_idle_delay_time); if they go clean during this time, then they will be moved to a deferred-idle band | |
2842 | * with a slightly higher priority, guarding against immediate termination under memory pressure and being unable to | |
2843 | * make forward progress. Finally, when the guard expires, they will be moved to the standard, lowest-priority, idle | |
2844 | * band. The deferral can be cleared early by clearing the appropriate flag. | |
3e170ce0 | 2845 | * |
cb323159 A |
2846 | * The deferral timer is active only for the duration that the process is marked as guarded and clean; if the process |
2847 | * is marked dirty, the timer will be cancelled. Upon being subsequently marked clean, the deferment will either be | |
2848 | * re-enabled or the guard state cleared, depending on whether the guard deadline has passed. | |
3e170ce0 A |
2849 | */ |
2850 | ||
39236c6e | 2851 | int |
cb323159 | 2852 | memorystatus_dirty_track(proc_t p, uint32_t pcontrol) |
316670eb | 2853 | { |
cb323159 A |
2854 | unsigned int old_dirty; |
2855 | boolean_t reschedule = FALSE; | |
2856 | boolean_t already_deferred = FALSE; | |
2857 | boolean_t defer_now = FALSE; | |
2858 | int ret = 0; | |
0a7de745 | 2859 | |
cb323159 A |
2860 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_TRACK), |
2861 | p->p_pid, p->p_memstat_dirty, pcontrol, 0, 0); | |
3e170ce0 | 2862 | |
39236c6e | 2863 | proc_list_lock(); |
0a7de745 | 2864 | |
cb323159 | 2865 | if ((p->p_listflag & P_LIST_EXITED) != 0) { |
fe8ab488 | 2866 | /* |
cb323159 | 2867 | * Process is on its way out. |
fe8ab488 A |
2868 | */ |
2869 | ret = EBUSY; | |
cb323159 | 2870 | goto exit; |
316670eb A |
2871 | } |
2872 | ||
cb323159 A |
2873 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
2874 | ret = EPERM; | |
2875 | goto exit; | |
2876 | } | |
3e170ce0 | 2877 | |
cb323159 A |
2878 | if ((ret = memorystatus_validate_track_flags(p, pcontrol)) != 0) { |
2879 | /* error */ | |
2880 | goto exit; | |
2881 | } | |
3e170ce0 | 2882 | |
cb323159 | 2883 | old_dirty = p->p_memstat_dirty; |
3e170ce0 | 2884 | |
cb323159 A |
2885 | /* These bits are cumulative, as per <rdar://problem/11159924> */ |
2886 | if (pcontrol & PROC_DIRTY_TRACK) { | |
2887 | p->p_memstat_dirty |= P_DIRTY_TRACK; | |
2888 | } | |
3e170ce0 | 2889 | |
cb323159 A |
2890 | if (pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) { |
2891 | p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT; | |
2892 | } | |
3e170ce0 | 2893 | |
cb323159 A |
2894 | if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) { |
2895 | p->p_memstat_dirty |= P_DIRTY_LAUNCH_IN_PROGRESS; | |
2896 | } | |
3e170ce0 | 2897 | |
cb323159 A |
2898 | if (old_dirty & P_DIRTY_AGING_IN_PROGRESS) { |
2899 | already_deferred = TRUE; | |
2900 | } | |
3e170ce0 | 2901 | |
cb323159 A |
2902 | |
2903 | /* This can be set and cleared exactly once. */ | |
2904 | if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) { | |
2905 | if ((pcontrol & (PROC_DIRTY_DEFER)) && | |
2906 | !(old_dirty & P_DIRTY_DEFER)) { | |
2907 | p->p_memstat_dirty |= P_DIRTY_DEFER; | |
fe8ab488 | 2908 | } |
316670eb | 2909 | |
cb323159 A |
2910 | if ((pcontrol & (PROC_DIRTY_DEFER_ALWAYS)) && |
2911 | !(old_dirty & P_DIRTY_DEFER_ALWAYS)) { | |
2912 | p->p_memstat_dirty |= P_DIRTY_DEFER_ALWAYS; | |
2913 | } | |
3e170ce0 | 2914 | |
cb323159 A |
2915 | defer_now = TRUE; |
2916 | } | |
3e170ce0 | 2917 | |
cb323159 A |
2918 | MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for pid %d\n", |
2919 | ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) ? "Y" : "N", | |
2920 | defer_now ? "Y" : "N", | |
2921 | p->p_memstat_dirty & P_DIRTY ? "Y" : "N", | |
2922 | p->p_pid); | |
3e170ce0 | 2923 | |
cb323159 A |
2924 | /* Kick off or invalidate the idle exit deferment if there's a state transition. */ |
2925 | if (!(p->p_memstat_dirty & P_DIRTY_IS_DIRTY)) { | |
2926 | if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { | |
2927 | if (defer_now && !already_deferred) { | |
2928 | /* | |
2929 | * Request to defer a clean process that's idle-exit enabled | |
2930 | * and not already in the jetsam deferred band. Most likely a | |
2931 | * new launch. | |
2932 | */ | |
2933 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
2934 | reschedule = TRUE; | |
2935 | } else if (!defer_now) { | |
2936 | /* | |
2937 | * The process isn't asking for the 'aging' facility. | |
2938 | * Could be that it is: | |
2939 | */ | |
2940 | ||
2941 | if (already_deferred) { | |
2942 | /* | |
2943 | * already in the aging bands. Traditionally, | |
2944 | * some processes have tried to use this to | |
2945 | * opt out of the 'aging' facility. | |
2946 | */ | |
2947 | ||
2948 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2949 | } else { | |
2950 | /* | |
2951 | * agnostic to the 'aging' facility. In that case, | |
2952 | * we'll go ahead and opt it in because this is likely | |
2953 | * a new launch (clean process, dirty tracking enabled) | |
2954 | */ | |
2955 | ||
2956 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
2957 | } | |
3e170ce0 | 2958 | |
cb323159 A |
2959 | reschedule = TRUE; |
2960 | } | |
2961 | } | |
2962 | } else { | |
3e170ce0 | 2963 | /* |
cb323159 A |
2964 | * We are trying to operate on a dirty process. Dirty processes have to |
2965 | * be removed from the deferred band. The question is do we reset the | |
2966 | * deferred state or not? | |
2967 | * | |
2968 | * This could be a legal request like: | |
2969 | * - this process had opted into the 'aging' band | |
2970 | * - but it's now dirty and requests to opt out. | |
2971 | * In this case, we remove the process from the band and reset its | |
2972 | * state too. It'll opt back in properly when needed. | |
2973 | * | |
2974 | * OR, this request could be a user-space bug. E.g.: | |
2975 | * - this process had opted into the 'aging' band when clean | |
2976 | * - and, then issues another request to again put it into the band except | |
2977 | * this time the process is dirty. | |
2978 | * The process going dirty, as a transition in memorystatus_dirty_set(), will pull the process out of | |
2979 | * the deferred band with its state intact. So our request below is no-op. | |
2980 | * But we do it here anyways for coverage. | |
2981 | * | |
2982 | * memorystatus_update_idle_priority_locked() | |
2983 | * single-mindedly treats a dirty process as "cannot be in the aging band". | |
3e170ce0 | 2984 | */ |
3e170ce0 | 2985 | |
cb323159 A |
2986 | if (!defer_now && already_deferred) { |
2987 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2988 | reschedule = TRUE; | |
2989 | } else { | |
2990 | boolean_t reset_state = (jetsam_aging_policy != kJetsamAgingPolicyLegacy) ? TRUE : FALSE; | |
2991 | ||
2992 | memorystatus_invalidate_idle_demotion_locked(p, reset_state); | |
2993 | reschedule = TRUE; | |
3e170ce0 A |
2994 | } |
2995 | } | |
3e170ce0 | 2996 | |
cb323159 | 2997 | memorystatus_update_idle_priority_locked(p); |
0a7de745 | 2998 | |
cb323159 A |
2999 | if (reschedule) { |
3000 | memorystatus_reschedule_idle_demotion_locked(); | |
fe8ab488 | 3001 | } |
39037602 | 3002 | |
39236c6e | 3003 | ret = 0; |
316670eb | 3004 | |
cb323159 A |
3005 | exit: |
3006 | proc_list_unlock(); | |
39236c6e | 3007 | |
316670eb A |
3008 | return ret; |
3009 | } | |
3010 | ||
39236c6e | 3011 | int |
cb323159 | 3012 | memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) |
316670eb | 3013 | { |
39236c6e | 3014 | int ret; |
cb323159 A |
3015 | boolean_t kill = false; |
3016 | boolean_t reschedule = FALSE; | |
3017 | boolean_t was_dirty = FALSE; | |
3018 | boolean_t now_dirty = FALSE; | |
f427ee49 A |
3019 | #if CONFIG_DIRTYSTATUS_TRACKING |
3020 | boolean_t notify_change = FALSE; | |
3021 | dirty_status_change_event_t change_event; | |
3022 | #endif | |
316670eb | 3023 | |
cb323159 A |
3024 | MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self, p->p_pid, pcontrol, p->p_memstat_dirty); |
3025 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_SET), p->p_pid, self, pcontrol, 0, 0); | |
316670eb | 3026 | |
cb323159 | 3027 | proc_list_lock(); |
0a7de745 | 3028 | |
cb323159 A |
3029 | if ((p->p_listflag & P_LIST_EXITED) != 0) { |
3030 | /* | |
3031 | * Process is on its way out. | |
3032 | */ | |
3033 | ret = EBUSY; | |
3034 | goto exit; | |
3035 | } | |
39037602 | 3036 | |
cb323159 A |
3037 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
3038 | ret = EPERM; | |
3039 | goto exit; | |
39037602 A |
3040 | } |
3041 | ||
cb323159 A |
3042 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { |
3043 | was_dirty = TRUE; | |
3044 | } | |
39037602 | 3045 | |
cb323159 A |
3046 | if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) { |
3047 | /* Dirty tracking not enabled */ | |
3048 | ret = EINVAL; | |
3049 | } else if (pcontrol && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) { | |
3050 | /* | |
3051 | * Process is set to be terminated and we're attempting to mark it dirty. | |
3052 | * Set for termination and marking as clean is OK - see <rdar://problem/10594349>. | |
3053 | */ | |
3054 | ret = EBUSY; | |
3055 | } else { | |
3056 | int flag = (self == TRUE) ? P_DIRTY : P_DIRTY_SHUTDOWN; | |
3057 | if (pcontrol && !(p->p_memstat_dirty & flag)) { | |
3058 | /* Mark the process as having been dirtied at some point */ | |
3059 | p->p_memstat_dirty |= (flag | P_DIRTY_MARKED); | |
3060 | memorystatus_dirty_count++; | |
3061 | ret = 0; | |
3062 | } else if ((pcontrol == 0) && (p->p_memstat_dirty & flag)) { | |
3063 | if ((flag == P_DIRTY_SHUTDOWN) && (!(p->p_memstat_dirty & P_DIRTY))) { | |
3064 | /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */ | |
3065 | p->p_memstat_dirty |= P_DIRTY_TERMINATED; | |
3066 | kill = true; | |
3067 | } else if ((flag == P_DIRTY) && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) { | |
3068 | /* Kill previously terminated processes if set clean */ | |
3069 | kill = true; | |
3070 | } | |
3071 | p->p_memstat_dirty &= ~flag; | |
3072 | memorystatus_dirty_count--; | |
3073 | ret = 0; | |
3074 | } else { | |
3075 | /* Already set */ | |
3076 | ret = EALREADY; | |
39037602 | 3077 | } |
fe8ab488 A |
3078 | } |
3079 | ||
cb323159 A |
3080 | if (ret != 0) { |
3081 | goto exit; | |
3082 | } | |
fe8ab488 | 3083 | |
cb323159 A |
3084 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { |
3085 | now_dirty = TRUE; | |
3086 | } | |
39037602 | 3087 | |
cb323159 A |
3088 | if ((was_dirty == TRUE && now_dirty == FALSE) || |
3089 | (was_dirty == FALSE && now_dirty == TRUE)) { | |
f427ee49 A |
3090 | #if CONFIG_DIRTYSTATUS_TRACKING |
3091 | if (dirtystatus_tracking_enabled) { | |
3092 | uint32_t pages = 0; | |
3093 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); | |
3094 | change_event.dsc_pid = p->p_pid; | |
3095 | change_event.dsc_event_type = (now_dirty == TRUE) ? kDirtyStatusChangedDirty : kDirtyStatusChangedClean; | |
3096 | change_event.dsc_time = mach_absolute_time(); | |
3097 | change_event.dsc_pages = pages; | |
3098 | change_event.dsc_priority = p->p_memstat_effectivepriority; | |
3099 | strlcpy(&change_event.dsc_process_name[0], p->p_name, sizeof(change_event.dsc_process_name)); | |
3100 | notify_change = TRUE; | |
3101 | } | |
3102 | #endif | |
3103 | ||
cb323159 A |
3104 | /* Manage idle exit deferral, if applied */ |
3105 | if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { | |
3106 | /* | |
3107 | * Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band OR it might be heading back | |
3108 | * there once it's clean again. For the legacy case, this only applies if it has some protection window left. | |
3109 | * P_DIRTY_DEFER: one-time protection window given at launch | |
3110 | * P_DIRTY_DEFER_ALWAYS: protection window given for every dirty->clean transition. Like non-legacy mode. | |
3111 | * | |
3112 | * Non-Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band. It will always stop over | |
3113 | * in that band on it's way to IDLE. | |
3114 | */ | |
39037602 | 3115 | |
cb323159 A |
3116 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { |
3117 | /* | |
3118 | * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE" | |
3119 | * | |
3120 | * The process will move from its aging band to its higher requested | |
3121 | * jetsam band. | |
3122 | */ | |
3123 | boolean_t reset_state = (jetsam_aging_policy != kJetsamAgingPolicyLegacy) ? TRUE : FALSE; | |
39037602 | 3124 | |
cb323159 A |
3125 | memorystatus_invalidate_idle_demotion_locked(p, reset_state); |
3126 | reschedule = TRUE; | |
3127 | } else { | |
3128 | /* | |
3129 | * Process is back from "dirty" to "clean". | |
3130 | */ | |
39037602 | 3131 | |
cb323159 A |
3132 | if (jetsam_aging_policy == kJetsamAgingPolicyLegacy) { |
3133 | if (((p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) == FALSE) && | |
3134 | (mach_absolute_time() >= p->p_memstat_idledeadline)) { | |
3135 | /* | |
3136 | * The process' hasn't enrolled in the "always defer after dirty" | |
3137 | * mode and its deadline has expired. It currently | |
3138 | * does not reside in any of the aging buckets. | |
3139 | * | |
3140 | * It's on its way to the JETSAM_PRIORITY_IDLE | |
3141 | * bucket via memorystatus_update_idle_priority_locked() | |
3142 | * below. | |
3143 | * | |
3144 | * So all we need to do is reset all the state on the | |
3145 | * process that's related to the aging bucket i.e. | |
3146 | * the AGING_IN_PROGRESS flag and the timer deadline. | |
3147 | */ | |
39037602 | 3148 | |
cb323159 A |
3149 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
3150 | reschedule = TRUE; | |
3151 | } else { | |
3152 | /* | |
3153 | * Process enrolled in "always stop in deferral band after dirty" OR | |
3154 | * it still has some protection window left and so | |
3155 | * we just re-arm the timer without modifying any | |
3156 | * state on the process iff it still wants into that band. | |
3157 | */ | |
39037602 | 3158 | |
cb323159 A |
3159 | if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) { |
3160 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
3161 | reschedule = TRUE; | |
3162 | } else if (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) { | |
3163 | memorystatus_schedule_idle_demotion_locked(p, FALSE); | |
3164 | reschedule = TRUE; | |
3165 | } | |
3166 | } | |
3167 | } else { | |
3168 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
3169 | reschedule = TRUE; | |
3170 | } | |
3171 | } | |
3172 | } | |
fe8ab488 | 3173 | |
cb323159 | 3174 | memorystatus_update_idle_priority_locked(p); |
316670eb | 3175 | |
cb323159 A |
3176 | if (memorystatus_highwater_enabled) { |
3177 | boolean_t ledger_update_needed = TRUE; | |
3178 | boolean_t use_active; | |
3179 | boolean_t is_fatal; | |
3180 | /* | |
3181 | * We are in this path because this process transitioned between | |
3182 | * dirty <--> clean state. Update the cached memory limits. | |
3183 | */ | |
39037602 | 3184 | |
cb323159 A |
3185 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { |
3186 | /* | |
3187 | * process is pinned in elevated band | |
3188 | * or | |
3189 | * process is dirty | |
3190 | */ | |
3191 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
3192 | use_active = TRUE; | |
3193 | ledger_update_needed = TRUE; | |
3194 | } else { | |
3195 | /* | |
3196 | * process is clean...but if it has opted into pressured-exit | |
3197 | * we don't apply the INACTIVE limit till the process has aged | |
3198 | * out and is entering the IDLE band. | |
3199 | * See memorystatus_update_priority_locked() for that. | |
3200 | */ | |
39037602 | 3201 | |
cb323159 A |
3202 | if (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) { |
3203 | ledger_update_needed = FALSE; | |
3204 | } else { | |
3205 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); | |
3206 | use_active = FALSE; | |
3207 | ledger_update_needed = TRUE; | |
3208 | } | |
3209 | } | |
d9a64523 | 3210 | |
cb323159 A |
3211 | /* |
3212 | * Enforce the new limits by writing to the ledger. | |
3213 | * | |
3214 | * This is a hot path and holding the proc_list_lock while writing to the ledgers, | |
3215 | * (where the task lock is taken) is bad. So, we temporarily drop the proc_list_lock. | |
3216 | * We aren't traversing the jetsam bucket list here, so we should be safe. | |
3217 | * See rdar://21394491. | |
3218 | */ | |
39037602 | 3219 | |
cb323159 A |
3220 | if (ledger_update_needed && proc_ref_locked(p) == p) { |
3221 | int ledger_limit; | |
3222 | if (p->p_memstat_memlimit > 0) { | |
3223 | ledger_limit = p->p_memstat_memlimit; | |
3224 | } else { | |
3225 | ledger_limit = -1; | |
3226 | } | |
3227 | proc_list_unlock(); | |
3228 | task_set_phys_footprint_limit_internal(p->task, ledger_limit, NULL, use_active, is_fatal); | |
3229 | proc_list_lock(); | |
3230 | proc_rele_locked(p); | |
b0d623f7 | 3231 | |
cb323159 A |
3232 | MEMORYSTATUS_DEBUG(3, "memorystatus_dirty_set: new limit on pid %d (%dMB %s) priority(%d) dirty?=0x%x %s\n", |
3233 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), | |
3234 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, | |
3235 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
3236 | } | |
3237 | } | |
39236c6e | 3238 | |
cb323159 A |
3239 | /* If the deferral state changed, reschedule the demotion timer */ |
3240 | if (reschedule) { | |
3241 | memorystatus_reschedule_idle_demotion_locked(); | |
3242 | } | |
3243 | } | |
3e170ce0 | 3244 | |
cb323159 A |
3245 | if (kill) { |
3246 | if (proc_ref_locked(p) == p) { | |
3247 | proc_list_unlock(); | |
3248 | psignal(p, SIGKILL); | |
3249 | proc_list_lock(); | |
3250 | proc_rele_locked(p); | |
3251 | } | |
3e170ce0 | 3252 | } |
5ba3f43e | 3253 | |
cb323159 A |
3254 | exit: |
3255 | proc_list_unlock(); | |
3e170ce0 | 3256 | |
f427ee49 A |
3257 | #if CONFIG_DIRTYSTATUS_TRACKING |
3258 | // Before returning, let's notify the dirtiness status if we have to | |
3259 | if (notify_change) { | |
3260 | memorystatus_send_dirty_status_change_note(&change_event, sizeof(change_event)); | |
3261 | } | |
3262 | #endif | |
3263 | ||
cb323159 | 3264 | return ret; |
3e170ce0 A |
3265 | } |
3266 | ||
cb323159 A |
3267 | int |
3268 | memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) | |
3e170ce0 | 3269 | { |
cb323159 | 3270 | int ret = 0; |
3e170ce0 | 3271 | |
cb323159 | 3272 | MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_clear(): %d 0x%x 0x%x\n", p->p_pid, pcontrol, p->p_memstat_dirty); |
3e170ce0 | 3273 | |
cb323159 | 3274 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_CLEAR), p->p_pid, pcontrol, 0, 0, 0); |
d9a64523 | 3275 | |
cb323159 A |
3276 | proc_list_lock(); |
3277 | ||
3278 | if ((p->p_listflag & P_LIST_EXITED) != 0) { | |
3e170ce0 | 3279 | /* |
cb323159 A |
3280 | * Process is on its way out. |
3281 | */ | |
3282 | ret = EBUSY; | |
3283 | goto exit; | |
39236c6e A |
3284 | } |
3285 | ||
cb323159 A |
3286 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
3287 | ret = EPERM; | |
3288 | goto exit; | |
3289 | } | |
3e170ce0 | 3290 | |
cb323159 A |
3291 | if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) { |
3292 | /* Dirty tracking not enabled */ | |
3293 | ret = EINVAL; | |
3294 | goto exit; | |
3295 | } | |
0a7de745 | 3296 | |
cb323159 A |
3297 | if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) == 0) { |
3298 | ret = EINVAL; | |
3299 | goto exit; | |
3300 | } | |
0a7de745 | 3301 | |
cb323159 A |
3302 | if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) { |
3303 | p->p_memstat_dirty &= ~P_DIRTY_LAUNCH_IN_PROGRESS; | |
3304 | } | |
316670eb | 3305 | |
cb323159 A |
3306 | /* This can be set and cleared exactly once. */ |
3307 | if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) { | |
3308 | if (p->p_memstat_dirty & P_DIRTY_DEFER) { | |
3309 | p->p_memstat_dirty &= ~(P_DIRTY_DEFER); | |
0a7de745 | 3310 | } |
39236c6e | 3311 | |
cb323159 A |
3312 | if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) { |
3313 | p->p_memstat_dirty &= ~(P_DIRTY_DEFER_ALWAYS); | |
3314 | } | |
3e170ce0 | 3315 | |
cb323159 A |
3316 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
3317 | memorystatus_update_idle_priority_locked(p); | |
3318 | memorystatus_reschedule_idle_demotion_locked(); | |
3e170ce0 | 3319 | } |
b0d623f7 | 3320 | |
cb323159 A |
3321 | ret = 0; |
3322 | exit: | |
3323 | proc_list_unlock(); | |
b0d623f7 | 3324 | |
cb323159 A |
3325 | return ret; |
3326 | } | |
3327 | ||
3328 | int | |
3329 | memorystatus_dirty_get(proc_t p, boolean_t locked) | |
0a7de745 | 3330 | { |
cb323159 | 3331 | int ret = 0; |
0a7de745 | 3332 | |
cb323159 A |
3333 | if (!locked) { |
3334 | proc_list_lock(); | |
b0d623f7 | 3335 | } |
39236c6e | 3336 | |
cb323159 A |
3337 | if (p->p_memstat_dirty & P_DIRTY_TRACK) { |
3338 | ret |= PROC_DIRTY_TRACKED; | |
3339 | if (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) { | |
3340 | ret |= PROC_DIRTY_ALLOWS_IDLE_EXIT; | |
3341 | } | |
3342 | if (p->p_memstat_dirty & P_DIRTY) { | |
3343 | ret |= PROC_DIRTY_IS_DIRTY; | |
3344 | } | |
3345 | if (p->p_memstat_dirty & P_DIRTY_LAUNCH_IN_PROGRESS) { | |
3346 | ret |= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS; | |
3347 | } | |
39236c6e | 3348 | } |
0a7de745 | 3349 | |
cb323159 A |
3350 | if (!locked) { |
3351 | proc_list_unlock(); | |
3352 | } | |
0a7de745 | 3353 | |
39236c6e | 3354 | return ret; |
b0d623f7 A |
3355 | } |
3356 | ||
cb323159 A |
3357 | int |
3358 | memorystatus_on_terminate(proc_t p) | |
0a7de745 | 3359 | { |
cb323159 | 3360 | int sig; |
3e170ce0 | 3361 | |
cb323159 | 3362 | proc_list_lock(); |
3e170ce0 | 3363 | |
cb323159 A |
3364 | p->p_memstat_dirty |= P_DIRTY_TERMINATED; |
3365 | ||
f427ee49 A |
3366 | if (((p->p_memstat_dirty & (P_DIRTY_TRACK | P_DIRTY_IS_DIRTY)) == P_DIRTY_TRACK) || |
3367 | (p->p_memstat_state & P_MEMSTAT_SUSPENDED)) { | |
3368 | /* | |
3369 | * Mark as terminated and issue SIGKILL if:- | |
3370 | * - process is clean, or, | |
3371 | * - if process is dirty but suspended. This case is likely | |
3372 | * an extension because apps don't opt into dirty-tracking | |
3373 | * and daemons aren't suspended. | |
3374 | */ | |
3375 | #if DEVELOPMENT || DEBUG | |
3376 | if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { | |
3377 | os_log(OS_LOG_DEFAULT, "memorystatus: sending suspended process %s (pid %d) SIGKILL", | |
3378 | (*p->p_name ? p->p_name : "unknown"), p->p_pid); | |
3379 | } | |
3380 | #endif /* DEVELOPMENT || DEBUG */ | |
cb323159 | 3381 | sig = SIGKILL; |
3e170ce0 | 3382 | } else { |
cb323159 A |
3383 | /* Dirty, terminated, or state tracking is unsupported; issue SIGTERM to allow cleanup */ |
3384 | sig = SIGTERM; | |
3e170ce0 A |
3385 | } |
3386 | ||
cb323159 | 3387 | proc_list_unlock(); |
3e170ce0 | 3388 | |
cb323159 | 3389 | return sig; |
3e170ce0 A |
3390 | } |
3391 | ||
cb323159 A |
3392 | void |
3393 | memorystatus_on_suspend(proc_t p) | |
a39ff7e2 | 3394 | { |
cb323159 A |
3395 | #if CONFIG_FREEZE |
3396 | uint32_t pages; | |
3397 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); | |
3398 | #endif | |
3399 | proc_list_lock(); | |
3400 | #if CONFIG_FREEZE | |
3401 | memorystatus_suspended_count++; | |
3402 | #endif | |
3403 | p->p_memstat_state |= P_MEMSTAT_SUSPENDED; | |
3404 | proc_list_unlock(); | |
3405 | } | |
a39ff7e2 | 3406 | |
f427ee49 A |
3407 | extern uint64_t memorystatus_thaw_count_since_boot; |
3408 | ||
cb323159 A |
3409 | void |
3410 | memorystatus_on_resume(proc_t p) | |
3411 | { | |
3412 | #if CONFIG_FREEZE | |
3413 | boolean_t frozen; | |
3414 | pid_t pid; | |
3415 | #endif | |
a39ff7e2 | 3416 | |
cb323159 | 3417 | proc_list_lock(); |
a39ff7e2 | 3418 | |
cb323159 A |
3419 | #if CONFIG_FREEZE |
3420 | frozen = (p->p_memstat_state & P_MEMSTAT_FROZEN); | |
3421 | if (frozen) { | |
a39ff7e2 | 3422 | /* |
cb323159 A |
3423 | * Now that we don't _thaw_ a process completely, |
3424 | * resuming it (and having some on-demand swapins) | |
3425 | * shouldn't preclude it from being counted as frozen. | |
3426 | * | |
3427 | * memorystatus_frozen_count--; | |
3428 | * | |
3429 | * We preserve the P_MEMSTAT_FROZEN state since the process | |
3430 | * could have state on disk AND so will deserve some protection | |
3431 | * in the jetsam bands. | |
a39ff7e2 | 3432 | */ |
cb323159 A |
3433 | if ((p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) == 0) { |
3434 | p->p_memstat_state |= P_MEMSTAT_REFREEZE_ELIGIBLE; | |
3435 | memorystatus_refreeze_eligible_count++; | |
3436 | } | |
3437 | p->p_memstat_thaw_count++; | |
a39ff7e2 | 3438 | |
cb323159 | 3439 | memorystatus_thaw_count++; |
f427ee49 | 3440 | memorystatus_thaw_count_since_boot++; |
cb323159 | 3441 | } |
a39ff7e2 | 3442 | |
cb323159 | 3443 | memorystatus_suspended_count--; |
a39ff7e2 | 3444 | |
cb323159 A |
3445 | pid = p->p_pid; |
3446 | #endif | |
3447 | ||
3448 | /* | |
3449 | * P_MEMSTAT_FROZEN will remain unchanged. This used to be: | |
3450 | * p->p_memstat_state &= ~(P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN); | |
3451 | */ | |
3452 | p->p_memstat_state &= ~P_MEMSTAT_SUSPENDED; | |
a39ff7e2 | 3453 | |
cb323159 | 3454 | proc_list_unlock(); |
a39ff7e2 | 3455 | |
cb323159 A |
3456 | #if CONFIG_FREEZE |
3457 | if (frozen) { | |
3458 | memorystatus_freeze_entry_t data = { pid, FALSE, 0 }; | |
3459 | memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); | |
3460 | } | |
3461 | #endif | |
3462 | } | |
a39ff7e2 | 3463 | |
cb323159 A |
3464 | void |
3465 | memorystatus_on_inactivity(proc_t p) | |
3466 | { | |
3467 | #pragma unused(p) | |
3468 | #if CONFIG_FREEZE | |
3469 | /* Wake the freeze thread */ | |
3470 | thread_wakeup((event_t)&memorystatus_freeze_wakeup); | |
3471 | #endif | |
3472 | } | |
3473 | ||
3474 | /* | |
3475 | * The proc_list_lock is held by the caller. | |
3476 | */ | |
3477 | static uint32_t | |
3478 | memorystatus_build_state(proc_t p) | |
3479 | { | |
3480 | uint32_t snapshot_state = 0; | |
3481 | ||
3482 | /* General */ | |
3483 | if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { | |
3484 | snapshot_state |= kMemorystatusSuspended; | |
3485 | } | |
3486 | if (p->p_memstat_state & P_MEMSTAT_FROZEN) { | |
3487 | snapshot_state |= kMemorystatusFrozen; | |
3488 | } | |
3489 | if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { | |
3490 | snapshot_state |= kMemorystatusWasThawed; | |
3491 | } | |
3492 | if (p->p_memstat_state & P_MEMSTAT_PRIORITY_ASSERTION) { | |
3493 | snapshot_state |= kMemorystatusAssertion; | |
3494 | } | |
3495 | ||
3496 | /* Tracking */ | |
3497 | if (p->p_memstat_dirty & P_DIRTY_TRACK) { | |
3498 | snapshot_state |= kMemorystatusTracked; | |
3499 | } | |
3500 | if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { | |
3501 | snapshot_state |= kMemorystatusSupportsIdleExit; | |
3502 | } | |
3503 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { | |
3504 | snapshot_state |= kMemorystatusDirty; | |
a39ff7e2 A |
3505 | } |
3506 | ||
cb323159 A |
3507 | return snapshot_state; |
3508 | } | |
a39ff7e2 | 3509 | |
cb323159 A |
3510 | static boolean_t |
3511 | kill_idle_exit_proc(void) | |
3512 | { | |
3513 | proc_t p, victim_p = PROC_NULL; | |
3514 | uint64_t current_time, footprint_of_killed_proc; | |
3515 | boolean_t killed = FALSE; | |
3516 | unsigned int i = 0; | |
3517 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
a39ff7e2 | 3518 | |
cb323159 A |
3519 | /* Pick next idle exit victim. */ |
3520 | current_time = mach_absolute_time(); | |
a39ff7e2 | 3521 | |
cb323159 A |
3522 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_IDLE_EXIT); |
3523 | if (jetsam_reason == OS_REASON_NULL) { | |
3524 | printf("kill_idle_exit_proc: failed to allocate jetsam reason\n"); | |
3525 | } | |
d9a64523 | 3526 | |
cb323159 | 3527 | proc_list_lock(); |
a39ff7e2 | 3528 | |
cb323159 A |
3529 | p = memorystatus_get_first_proc_locked(&i, FALSE); |
3530 | while (p) { | |
3531 | /* No need to look beyond the idle band */ | |
3532 | if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) { | |
3533 | break; | |
a39ff7e2 | 3534 | } |
a39ff7e2 | 3535 | |
cb323159 A |
3536 | if ((p->p_memstat_dirty & (P_DIRTY_ALLOW_IDLE_EXIT | P_DIRTY_IS_DIRTY | P_DIRTY_TERMINATED)) == (P_DIRTY_ALLOW_IDLE_EXIT)) { |
3537 | if (current_time >= p->p_memstat_idledeadline) { | |
3538 | p->p_memstat_dirty |= P_DIRTY_TERMINATED; | |
3539 | victim_p = proc_ref_locked(p); | |
3540 | break; | |
a39ff7e2 A |
3541 | } |
3542 | } | |
3543 | ||
cb323159 A |
3544 | p = memorystatus_get_next_proc_locked(&i, p, FALSE); |
3545 | } | |
a39ff7e2 | 3546 | |
cb323159 | 3547 | proc_list_unlock(); |
a39ff7e2 | 3548 | |
cb323159 A |
3549 | if (victim_p) { |
3550 | printf("memorystatus: killing_idle_process pid %d [%s] jetsam_reason->osr_code: %llu\n", victim_p->p_pid, (*victim_p->p_name ? victim_p->p_name : "unknown"), jetsam_reason->osr_code); | |
3551 | killed = memorystatus_do_kill(victim_p, kMemorystatusKilledIdleExit, jetsam_reason, &footprint_of_killed_proc); | |
3552 | proc_rele(victim_p); | |
3553 | } else { | |
3554 | os_reason_free(jetsam_reason); | |
a39ff7e2 A |
3555 | } |
3556 | ||
cb323159 | 3557 | return killed; |
a39ff7e2 A |
3558 | } |
3559 | ||
cb323159 A |
3560 | static void |
3561 | memorystatus_thread_wake(void) | |
39236c6e | 3562 | { |
cb323159 A |
3563 | int thr_id = 0; |
3564 | int active_thr = atomic_load(&active_jetsam_threads); | |
316670eb | 3565 | |
cb323159 A |
3566 | /* Wakeup all the jetsam threads */ |
3567 | for (thr_id = 0; thr_id < active_thr; thr_id++) { | |
3568 | thread_wakeup((event_t)&jetsam_threads[thr_id].memorystatus_wakeup); | |
5ba3f43e | 3569 | } |
cb323159 | 3570 | } |
5ba3f43e | 3571 | |
cb323159 | 3572 | #if CONFIG_JETSAM |
5ba3f43e | 3573 | |
cb323159 A |
3574 | static void |
3575 | memorystatus_thread_pool_max() | |
3576 | { | |
3577 | /* Increase the jetsam thread pool to max_jetsam_threads */ | |
3578 | int max_threads = max_jetsam_threads; | |
3579 | printf("Expanding memorystatus pool to %d!\n", max_threads); | |
3580 | atomic_store(&active_jetsam_threads, max_threads); | |
3581 | } | |
3e170ce0 | 3582 | |
cb323159 A |
3583 | static void |
3584 | memorystatus_thread_pool_default() | |
3585 | { | |
3586 | /* Restore the jetsam thread pool to a single thread */ | |
3587 | printf("Reverting memorystatus pool back to 1\n"); | |
3588 | atomic_store(&active_jetsam_threads, 1); | |
3589 | } | |
5ba3f43e A |
3590 | |
3591 | #endif /* CONFIG_JETSAM */ | |
3592 | ||
cb323159 | 3593 | extern void vm_pressure_response(void); |
0a7de745 | 3594 | |
cb323159 A |
3595 | static int |
3596 | memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation) | |
3597 | { | |
3598 | struct jetsam_thread_state *jetsam_thread = jetsam_current_thread(); | |
0a7de745 | 3599 | |
cb323159 A |
3600 | assert(jetsam_thread != NULL); |
3601 | if (interval_ms) { | |
3602 | assert_wait_timeout(&jetsam_thread->memorystatus_wakeup, THREAD_UNINT, interval_ms, NSEC_PER_MSEC); | |
3603 | } else { | |
3604 | assert_wait(&jetsam_thread->memorystatus_wakeup, THREAD_UNINT); | |
3605 | } | |
0a7de745 | 3606 | |
cb323159 A |
3607 | return thread_block(continuation); |
3608 | } | |
316670eb | 3609 | |
cb323159 A |
3610 | static boolean_t |
3611 | memorystatus_avail_pages_below_pressure(void) | |
3612 | { | |
f427ee49 | 3613 | #if CONFIG_JETSAM |
cb323159 | 3614 | return memorystatus_available_pages <= memorystatus_available_pages_pressure; |
f427ee49 | 3615 | #else /* CONFIG_JETSAM */ |
cb323159 | 3616 | return FALSE; |
f427ee49 | 3617 | #endif /* CONFIG_JETSAM */ |
cb323159 | 3618 | } |
0a7de745 | 3619 | |
cb323159 A |
3620 | static boolean_t |
3621 | memorystatus_avail_pages_below_critical(void) | |
3622 | { | |
f427ee49 | 3623 | #if CONFIG_JETSAM |
cb323159 | 3624 | return memorystatus_available_pages <= memorystatus_available_pages_critical; |
f427ee49 | 3625 | #else /* CONFIG_JETSAM */ |
cb323159 | 3626 | return FALSE; |
f427ee49 | 3627 | #endif /* CONFIG_JETSAM */ |
cb323159 | 3628 | } |
316670eb | 3629 | |
cb323159 A |
3630 | static boolean_t |
3631 | memorystatus_post_snapshot(int32_t priority, uint32_t cause) | |
3632 | { | |
3633 | boolean_t is_idle_priority; | |
fe8ab488 | 3634 | |
cb323159 A |
3635 | if (jetsam_aging_policy == kJetsamAgingPolicyLegacy) { |
3636 | is_idle_priority = (priority == JETSAM_PRIORITY_IDLE); | |
3637 | } else { | |
3638 | is_idle_priority = (priority == JETSAM_PRIORITY_IDLE || priority == JETSAM_PRIORITY_IDLE_DEFERRED); | |
3639 | } | |
f427ee49 | 3640 | #if CONFIG_JETSAM |
cb323159 A |
3641 | #pragma unused(cause) |
3642 | /* | |
3643 | * Don't generate logs for steady-state idle-exit kills, | |
3644 | * unless it is overridden for debug or by the device | |
3645 | * tree. | |
3646 | */ | |
fe8ab488 | 3647 | |
cb323159 | 3648 | return !is_idle_priority || memorystatus_idle_snapshot; |
0a7de745 | 3649 | |
f427ee49 | 3650 | #else /* CONFIG_JETSAM */ |
cb323159 A |
3651 | /* |
3652 | * Don't generate logs for steady-state idle-exit kills, | |
3653 | * unless | |
3654 | * - it is overridden for debug or by the device | |
3655 | * tree. | |
3656 | * OR | |
3657 | * - the kill causes are important i.e. not kMemorystatusKilledIdleExit | |
3658 | */ | |
a39ff7e2 | 3659 | |
cb323159 A |
3660 | boolean_t snapshot_eligible_kill_cause = (is_reason_thrashing(cause) || is_reason_zone_map_exhaustion(cause)); |
3661 | return !is_idle_priority || memorystatus_idle_snapshot || snapshot_eligible_kill_cause; | |
f427ee49 | 3662 | #endif /* CONFIG_JETSAM */ |
cb323159 | 3663 | } |
39037602 | 3664 | |
cb323159 A |
3665 | static boolean_t |
3666 | memorystatus_action_needed(void) | |
3667 | { | |
f427ee49 | 3668 | #if CONFIG_JETSAM |
cb323159 A |
3669 | return is_reason_thrashing(kill_under_pressure_cause) || |
3670 | is_reason_zone_map_exhaustion(kill_under_pressure_cause) || | |
3671 | memorystatus_available_pages <= memorystatus_available_pages_pressure; | |
f427ee49 | 3672 | #else /* CONFIG_JETSAM */ |
cb323159 A |
3673 | return is_reason_thrashing(kill_under_pressure_cause) || |
3674 | is_reason_zone_map_exhaustion(kill_under_pressure_cause); | |
f427ee49 | 3675 | #endif /* CONFIG_JETSAM */ |
cb323159 | 3676 | } |
d9a64523 | 3677 | |
cb323159 A |
3678 | static boolean_t |
3679 | memorystatus_act_on_hiwat_processes(uint32_t *errors, uint32_t *hwm_kill, boolean_t *post_snapshot, __unused boolean_t *is_critical, uint64_t *memory_reclaimed) | |
3680 | { | |
3681 | boolean_t purged = FALSE, killed = FALSE; | |
0a7de745 | 3682 | |
cb323159 A |
3683 | *memory_reclaimed = 0; |
3684 | killed = memorystatus_kill_hiwat_proc(errors, &purged, memory_reclaimed); | |
a39ff7e2 | 3685 | |
cb323159 A |
3686 | if (killed) { |
3687 | *hwm_kill = *hwm_kill + 1; | |
3688 | *post_snapshot = TRUE; | |
3689 | return TRUE; | |
3690 | } else { | |
3691 | if (purged == FALSE) { | |
3692 | /* couldn't purge and couldn't kill */ | |
3693 | memorystatus_hwm_candidates = FALSE; | |
b0d623f7 | 3694 | } |
b0d623f7 | 3695 | } |
0a7de745 | 3696 | |
cb323159 A |
3697 | #if CONFIG_JETSAM |
3698 | /* No highwater processes to kill. Continue or stop for now? */ | |
3699 | if (!is_reason_thrashing(kill_under_pressure_cause) && | |
3700 | !is_reason_zone_map_exhaustion(kill_under_pressure_cause) && | |
3701 | (memorystatus_available_pages > memorystatus_available_pages_critical)) { | |
3702 | /* | |
3703 | * We are _not_ out of pressure but we are above the critical threshold and there's: | |
3704 | * - no compressor thrashing | |
3705 | * - enough zone memory | |
3706 | * - no more HWM processes left. | |
3707 | * For now, don't kill any other processes. | |
3708 | */ | |
0a7de745 | 3709 | |
cb323159 A |
3710 | if (*hwm_kill == 0) { |
3711 | memorystatus_thread_wasted_wakeup++; | |
3712 | } | |
39037602 | 3713 | |
cb323159 | 3714 | *is_critical = FALSE; |
0a7de745 | 3715 | |
cb323159 A |
3716 | return TRUE; |
3717 | } | |
3718 | #endif /* CONFIG_JETSAM */ | |
b0d623f7 | 3719 | |
cb323159 | 3720 | return FALSE; |
316670eb A |
3721 | } |
3722 | ||
3e170ce0 | 3723 | /* |
cb323159 A |
3724 | * kJetsamHighRelaunchCandidatesThreshold defines the percentage of candidates |
3725 | * in the idle & deferred bands that need to be bad candidates in order to trigger | |
3726 | * aggressive jetsam. | |
3e170ce0 | 3727 | */ |
cb323159 A |
3728 | #define kJetsamHighRelaunchCandidatesThreshold (100) |
3729 | ||
3730 | /* kJetsamMinCandidatesThreshold defines the minimum number of candidates in the | |
3731 | * idle/deferred bands to trigger aggressive jetsam. This value basically decides | |
3732 | * how much memory the system is ready to hold in the lower bands without triggering | |
3733 | * aggressive jetsam. This number should ideally be tuned based on the memory config | |
3734 | * of the device. | |
3735 | */ | |
3736 | #define kJetsamMinCandidatesThreshold (5) | |
3737 | ||
39236c6e | 3738 | static boolean_t |
cb323159 | 3739 | memorystatus_aggressive_jetsam_needed_sysproc_aging(__unused int jld_eval_aggressive_count, __unused int *jld_idle_kills, __unused int jld_idle_kill_candidates, int *total_candidates, int *elevated_bucket_count) |
d1ecb069 | 3740 | { |
cb323159 | 3741 | boolean_t aggressive_jetsam_needed = false; |
3e170ce0 | 3742 | |
cb323159 A |
3743 | /* |
3744 | * For the kJetsamAgingPolicySysProcsReclaimedFirst aging policy, we maintain the jetsam | |
3745 | * relaunch behavior for all daemons. Also, daemons and apps are aged in deferred bands on | |
3746 | * every dirty->clean transition. For this aging policy, the best way to determine if | |
3747 | * aggressive jetsam is needed, is to see if the kill candidates are mostly bad candidates. | |
3748 | * If yes, then we need to go to higher bands to reclaim memory. | |
3749 | */ | |
3750 | proc_list_lock(); | |
3751 | /* Get total candidate counts for idle and idle deferred bands */ | |
3752 | *total_candidates = memstat_bucket[JETSAM_PRIORITY_IDLE].count + memstat_bucket[system_procs_aging_band].count; | |
3753 | /* Get counts of bad kill candidates in idle and idle deferred bands */ | |
3754 | int bad_candidates = memstat_bucket[JETSAM_PRIORITY_IDLE].relaunch_high_count + memstat_bucket[system_procs_aging_band].relaunch_high_count; | |
3e170ce0 | 3755 | |
cb323159 | 3756 | *elevated_bucket_count = memstat_bucket[JETSAM_PRIORITY_ELEVATED_INACTIVE].count; |
490019cf | 3757 | |
cb323159 | 3758 | proc_list_unlock(); |
5ba3f43e | 3759 | |
cb323159 A |
3760 | /* Check if the number of bad candidates is greater than kJetsamHighRelaunchCandidatesThreshold % */ |
3761 | aggressive_jetsam_needed = (((bad_candidates * 100) / *total_candidates) >= kJetsamHighRelaunchCandidatesThreshold); | |
3762 | ||
3763 | /* | |
3764 | * Since the new aging policy bases the aggressive jetsam trigger on percentage of | |
3765 | * bad candidates, it is prone to being overly aggressive. In order to mitigate that, | |
3766 | * make sure the system is really under memory pressure before triggering aggressive | |
3767 | * jetsam. | |
3768 | */ | |
3769 | if (memorystatus_available_pages > memorystatus_sysproc_aging_aggr_pages) { | |
3770 | aggressive_jetsam_needed = false; | |
3771 | } | |
3e170ce0 | 3772 | |
3e170ce0 | 3773 | #if DEVELOPMENT || DEBUG |
cb323159 A |
3774 | printf("memorystatus: aggressive%d: [%s] Bad Candidate Threshold Check (total: %d, bad: %d, threshold: %d %%); Memory Pressure Check (available_pgs: %llu, threshold_pgs: %llu)\n", |
3775 | jld_eval_aggressive_count, aggressive_jetsam_needed ? "PASSED" : "FAILED", *total_candidates, bad_candidates, | |
f427ee49 | 3776 | kJetsamHighRelaunchCandidatesThreshold, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, (uint64_t)memorystatus_sysproc_aging_aggr_pages); |
3e170ce0 | 3777 | #endif /* DEVELOPMENT || DEBUG */ |
cb323159 A |
3778 | return aggressive_jetsam_needed; |
3779 | } | |
39236c6e | 3780 | |
f427ee49 A |
3781 | /* |
3782 | * Gets memory back from various system caches. | |
3783 | * Called before jetsamming in the foreground band in the hope that we'll | |
3784 | * avoid a jetsam. | |
3785 | */ | |
3786 | static void | |
3787 | memorystatus_approaching_fg_band(boolean_t *corpse_list_purged) | |
3788 | { | |
3789 | assert(corpse_list_purged != NULL); | |
3790 | pmap_release_pages_fast(); | |
3791 | memorystatus_issue_fg_band_notify(); | |
3792 | if (total_corpses_count() > 0 && !*corpse_list_purged) { | |
3793 | task_purge_all_corpses(); | |
3794 | *corpse_list_purged = TRUE; | |
3795 | } | |
3796 | } | |
3797 | ||
cb323159 A |
3798 | static boolean_t |
3799 | memorystatus_aggressive_jetsam_needed_default(__unused int jld_eval_aggressive_count, int *jld_idle_kills, int jld_idle_kill_candidates, int *total_candidates, int *elevated_bucket_count) | |
3800 | { | |
3801 | boolean_t aggressive_jetsam_needed = false; | |
3802 | /* Jetsam Loop Detection - locals */ | |
3803 | memstat_bucket_t *bucket; | |
3804 | int jld_bucket_count = 0; | |
3e170ce0 | 3805 | |
cb323159 A |
3806 | proc_list_lock(); |
3807 | switch (jetsam_aging_policy) { | |
3808 | case kJetsamAgingPolicyLegacy: | |
3809 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
3810 | jld_bucket_count = bucket->count; | |
3811 | bucket = &memstat_bucket[JETSAM_PRIORITY_AGING_BAND1]; | |
3812 | jld_bucket_count += bucket->count; | |
3813 | break; | |
3814 | case kJetsamAgingPolicyAppsReclaimedFirst: | |
3815 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
3816 | jld_bucket_count = bucket->count; | |
3817 | bucket = &memstat_bucket[system_procs_aging_band]; | |
3818 | jld_bucket_count += bucket->count; | |
3819 | bucket = &memstat_bucket[applications_aging_band]; | |
3820 | jld_bucket_count += bucket->count; | |
3821 | break; | |
3822 | case kJetsamAgingPolicyNone: | |
3823 | default: | |
3824 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
3825 | jld_bucket_count = bucket->count; | |
3826 | break; | |
3827 | } | |
3e170ce0 | 3828 | |
cb323159 A |
3829 | bucket = &memstat_bucket[JETSAM_PRIORITY_ELEVATED_INACTIVE]; |
3830 | *elevated_bucket_count = bucket->count; | |
3831 | *total_candidates = jld_bucket_count; | |
3832 | proc_list_unlock(); | |
3e170ce0 | 3833 | |
cb323159 | 3834 | aggressive_jetsam_needed = (*jld_idle_kills > jld_idle_kill_candidates); |
0a7de745 | 3835 | |
3e170ce0 | 3836 | #if DEVELOPMENT || DEBUG |
cb323159 A |
3837 | if (aggressive_jetsam_needed) { |
3838 | printf("memorystatus: aggressive%d: idle candidates: %d, idle kills: %d\n", | |
3839 | jld_eval_aggressive_count, | |
3840 | jld_idle_kill_candidates, | |
3841 | *jld_idle_kills); | |
3842 | } | |
3e170ce0 | 3843 | #endif /* DEVELOPMENT || DEBUG */ |
cb323159 A |
3844 | return aggressive_jetsam_needed; |
3845 | } | |
0a7de745 | 3846 | |
cb323159 A |
3847 | static boolean_t |
3848 | memorystatus_act_aggressive(uint32_t cause, os_reason_t jetsam_reason, int *jld_idle_kills, boolean_t *corpse_list_purged, boolean_t *post_snapshot, uint64_t *memory_reclaimed) | |
3849 | { | |
3850 | boolean_t aggressive_jetsam_needed = false; | |
3851 | boolean_t killed; | |
3852 | uint32_t errors = 0; | |
3853 | uint64_t footprint_of_killed_proc = 0; | |
3854 | int elevated_bucket_count = 0; | |
3855 | int total_candidates = 0; | |
3856 | *memory_reclaimed = 0; | |
0a7de745 | 3857 | |
cb323159 A |
3858 | /* |
3859 | * The aggressive jetsam logic looks at the number of times it has been in the | |
3860 | * aggressive loop to determine the max priority band it should kill upto. The | |
3861 | * static variables below are used to track that property. | |
3862 | * | |
3863 | * To reset those values, the implementation checks if it has been | |
3864 | * memorystatus_jld_eval_period_msecs since the parameters were reset. | |
3865 | */ | |
3866 | static int jld_eval_aggressive_count = 0; | |
3867 | static int32_t jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; | |
3868 | static uint64_t jld_timestamp_msecs = 0; | |
3869 | static int jld_idle_kill_candidates = 0; | |
39037602 | 3870 | |
cb323159 A |
3871 | if (memorystatus_jld_enabled == FALSE) { |
3872 | /* If aggressive jetsam is disabled, nothing to do here */ | |
3873 | return FALSE; | |
3874 | } | |
0a7de745 | 3875 | |
cb323159 A |
3876 | /* Get current timestamp (msecs only) */ |
3877 | struct timeval jld_now_tstamp = {0, 0}; | |
3878 | uint64_t jld_now_msecs = 0; | |
3879 | microuptime(&jld_now_tstamp); | |
3880 | jld_now_msecs = (jld_now_tstamp.tv_sec * 1000); | |
3e170ce0 | 3881 | |
cb323159 A |
3882 | /* |
3883 | * The aggressive jetsam logic looks at the number of candidates and their | |
3884 | * properties to decide if aggressive jetsam should be engaged. | |
3885 | */ | |
3886 | if (jetsam_aging_policy == kJetsamAgingPolicySysProcsReclaimedFirst) { | |
3e170ce0 | 3887 | /* |
cb323159 A |
3888 | * For the kJetsamAgingPolicySysProcsReclaimedFirst aging policy, the logic looks at the number of |
3889 | * candidates in the idle and deferred band and how many out of them are marked as high relaunch | |
3890 | * probability. | |
3891 | */ | |
3892 | aggressive_jetsam_needed = memorystatus_aggressive_jetsam_needed_sysproc_aging(jld_eval_aggressive_count, | |
3893 | jld_idle_kills, jld_idle_kill_candidates, &total_candidates, &elevated_bucket_count); | |
3894 | } else { | |
3895 | /* | |
3896 | * The other aging policies look at number of candidate processes over a specific time window and | |
3897 | * evaluate if the system is in a jetsam loop. If yes, aggressive jetsam is triggered. | |
3898 | */ | |
3899 | aggressive_jetsam_needed = memorystatus_aggressive_jetsam_needed_default(jld_eval_aggressive_count, | |
3900 | jld_idle_kills, jld_idle_kill_candidates, &total_candidates, &elevated_bucket_count); | |
3901 | } | |
490019cf | 3902 | |
cb323159 A |
3903 | /* |
3904 | * Check if its been really long since the aggressive jetsam evaluation | |
3905 | * parameters have been refreshed. This logic also resets the jld_eval_aggressive_count | |
3906 | * counter to make sure we reset the aggressive jetsam severity. | |
3907 | */ | |
3908 | boolean_t param_reval = false; | |
39037602 | 3909 | |
cb323159 A |
3910 | if ((total_candidates == 0) || |
3911 | (jld_now_msecs > (jld_timestamp_msecs + memorystatus_jld_eval_period_msecs))) { | |
3912 | jld_timestamp_msecs = jld_now_msecs; | |
3913 | jld_idle_kill_candidates = total_candidates; | |
3914 | *jld_idle_kills = 0; | |
3915 | jld_eval_aggressive_count = 0; | |
3916 | jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; | |
3917 | param_reval = true; | |
3918 | } | |
3e170ce0 | 3919 | |
cb323159 A |
3920 | /* |
3921 | * If the parameters have been updated, re-evaluate the aggressive_jetsam_needed condition for | |
3922 | * the non kJetsamAgingPolicySysProcsReclaimedFirst policy since its based on jld_idle_kill_candidates etc. | |
3923 | */ | |
3924 | if ((param_reval == true) && (jetsam_aging_policy != kJetsamAgingPolicySysProcsReclaimedFirst)) { | |
3925 | aggressive_jetsam_needed = (*jld_idle_kills > jld_idle_kill_candidates); | |
3926 | } | |
490019cf | 3927 | |
cb323159 A |
3928 | /* |
3929 | * It is also possible that the system is down to a very small number of processes in the candidate | |
3930 | * bands. In that case, the decisions made by the memorystatus_aggressive_jetsam_needed_* routines | |
3931 | * would not be useful. In that case, do not trigger aggressive jetsam. | |
3932 | */ | |
3933 | if (total_candidates < kJetsamMinCandidatesThreshold) { | |
490019cf | 3934 | #if DEVELOPMENT || DEBUG |
cb323159 | 3935 | printf("memorystatus: aggressive: [FAILED] Low Candidate Count (current: %d, threshold: %d)\n", total_candidates, kJetsamMinCandidatesThreshold); |
490019cf | 3936 | #endif /* DEVELOPMENT || DEBUG */ |
cb323159 A |
3937 | aggressive_jetsam_needed = false; |
3938 | } | |
490019cf | 3939 | |
cb323159 A |
3940 | if (aggressive_jetsam_needed == false) { |
3941 | /* Either the aging policy or the candidate count decided that aggressive jetsam is not needed. Nothing more to do here. */ | |
3942 | return FALSE; | |
3943 | } | |
0a7de745 | 3944 | |
cb323159 A |
3945 | /* Looks like aggressive jetsam is needed */ |
3946 | jld_eval_aggressive_count++; | |
3947 | ||
3948 | if (jld_eval_aggressive_count == memorystatus_jld_eval_aggressive_count) { | |
f427ee49 | 3949 | memorystatus_approaching_fg_band(corpse_list_purged); |
cb323159 A |
3950 | } else if (jld_eval_aggressive_count > memorystatus_jld_eval_aggressive_count) { |
3951 | /* | |
3952 | * Bump up the jetsam priority limit (eg: the bucket index) | |
3953 | * Enforce bucket index sanity. | |
3954 | */ | |
3955 | if ((memorystatus_jld_eval_aggressive_priority_band_max < 0) || | |
3956 | (memorystatus_jld_eval_aggressive_priority_band_max >= MEMSTAT_BUCKET_COUNT)) { | |
3957 | /* | |
3958 | * Do nothing. Stick with the default level. | |
3959 | */ | |
3960 | } else { | |
3961 | jld_priority_band_max = memorystatus_jld_eval_aggressive_priority_band_max; | |
3962 | } | |
3e170ce0 | 3963 | } |
0a7de745 | 3964 | |
cb323159 A |
3965 | /* Visit elevated processes first */ |
3966 | while (elevated_bucket_count) { | |
3967 | elevated_bucket_count--; | |
0a7de745 | 3968 | |
cb323159 A |
3969 | /* |
3970 | * memorystatus_kill_elevated_process() drops a reference, | |
3971 | * so take another one so we can continue to use this exit reason | |
3972 | * even after it returns. | |
3973 | */ | |
39037602 | 3974 | |
cb323159 A |
3975 | os_reason_ref(jetsam_reason); |
3976 | killed = memorystatus_kill_elevated_process( | |
3977 | cause, | |
3978 | jetsam_reason, | |
3979 | JETSAM_PRIORITY_ELEVATED_INACTIVE, | |
3980 | jld_eval_aggressive_count, | |
3981 | &errors, &footprint_of_killed_proc); | |
3982 | if (killed) { | |
3983 | *post_snapshot = TRUE; | |
3984 | *memory_reclaimed += footprint_of_killed_proc; | |
3985 | if (memorystatus_avail_pages_below_pressure()) { | |
3986 | /* | |
3987 | * Still under pressure. | |
3988 | * Find another pinned processes. | |
3989 | */ | |
3990 | continue; | |
3991 | } else { | |
3992 | return TRUE; | |
3993 | } | |
3994 | } else { | |
3995 | /* | |
3996 | * No pinned processes left to kill. | |
3997 | * Abandon elevated band. | |
3998 | */ | |
3999 | break; | |
4000 | } | |
3e170ce0 | 4001 | } |
0a7de745 | 4002 | |
cb323159 A |
4003 | /* |
4004 | * memorystatus_kill_processes_aggressive() allocates its own | |
4005 | * jetsam_reason so the kMemorystatusKilledProcThrashing cause | |
4006 | * is consistent throughout the aggressive march. | |
4007 | */ | |
4008 | killed = memorystatus_kill_processes_aggressive( | |
4009 | kMemorystatusKilledProcThrashing, | |
4010 | jld_eval_aggressive_count, | |
4011 | jld_priority_band_max, | |
4012 | &errors, &footprint_of_killed_proc); | |
3e170ce0 | 4013 | |
cb323159 A |
4014 | if (killed) { |
4015 | /* Always generate logs after aggressive kill */ | |
4016 | *post_snapshot = TRUE; | |
4017 | *memory_reclaimed += footprint_of_killed_proc; | |
4018 | *jld_idle_kills = 0; | |
0a7de745 | 4019 | return TRUE; |
3e170ce0 | 4020 | } |
cb323159 A |
4021 | |
4022 | return FALSE; | |
3e170ce0 A |
4023 | } |
4024 | ||
cb323159 A |
4025 | |
4026 | static void | |
4027 | memorystatus_thread(void *param __unused, wait_result_t wr __unused) | |
3e170ce0 | 4028 | { |
cb323159 A |
4029 | boolean_t post_snapshot = FALSE; |
4030 | uint32_t errors = 0; | |
4031 | uint32_t hwm_kill = 0; | |
4032 | boolean_t sort_flag = TRUE; | |
4033 | boolean_t corpse_list_purged = FALSE; | |
4034 | int jld_idle_kills = 0; | |
4035 | struct jetsam_thread_state *jetsam_thread = jetsam_current_thread(); | |
4036 | uint64_t total_memory_reclaimed = 0; | |
0a7de745 | 4037 | |
cb323159 A |
4038 | assert(jetsam_thread != NULL); |
4039 | if (jetsam_thread->inited == FALSE) { | |
4040 | /* | |
4041 | * It's the first time the thread has run, so just mark the thread as privileged and block. | |
4042 | * This avoids a spurious pass with unset variables, as set out in <rdar://problem/9609402>. | |
4043 | */ | |
4044 | ||
4045 | char name[32]; | |
4046 | thread_wire(host_priv_self(), current_thread(), TRUE); | |
4047 | snprintf(name, 32, "VM_memorystatus_%d", jetsam_thread->index + 1); | |
4048 | ||
4049 | /* Limit all but one thread to the lower jetsam bands, as that's where most of the victims are. */ | |
4050 | if (jetsam_thread->index == 0) { | |
4051 | if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) { | |
4052 | thread_vm_bind_group_add(); | |
4053 | } | |
4054 | jetsam_thread->limit_to_low_bands = FALSE; | |
4055 | } else { | |
4056 | jetsam_thread->limit_to_low_bands = TRUE; | |
4057 | } | |
f427ee49 A |
4058 | #if CONFIG_THREAD_GROUPS |
4059 | thread_group_vm_add(); | |
4060 | #endif | |
cb323159 A |
4061 | thread_set_thread_name(current_thread(), name); |
4062 | jetsam_thread->inited = TRUE; | |
4063 | memorystatus_thread_block(0, memorystatus_thread); | |
39037602 A |
4064 | } |
4065 | ||
cb323159 | 4066 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START, |
f427ee49 | 4067 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, memorystatus_jld_enabled, memorystatus_jld_eval_period_msecs, memorystatus_jld_eval_aggressive_count, 0); |
0a7de745 | 4068 | |
cb323159 A |
4069 | /* |
4070 | * Jetsam aware version. | |
4071 | * | |
4072 | * The VM pressure notification thread is working it's way through clients in parallel. | |
4073 | * | |
4074 | * So, while the pressure notification thread is targeting processes in order of | |
4075 | * increasing jetsam priority, we can hopefully reduce / stop it's work by killing | |
4076 | * any processes that have exceeded their highwater mark. | |
4077 | * | |
4078 | * If we run out of HWM processes and our available pages drops below the critical threshold, then, | |
4079 | * we target the least recently used process in order of increasing jetsam priority (exception: the FG band). | |
4080 | */ | |
4081 | while (memorystatus_action_needed()) { | |
4082 | boolean_t killed; | |
4083 | int32_t priority; | |
4084 | uint32_t cause; | |
4085 | uint64_t memory_reclaimed = 0; | |
4086 | uint64_t jetsam_reason_code = JETSAM_REASON_INVALID; | |
4087 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
3e170ce0 | 4088 | |
cb323159 A |
4089 | cause = kill_under_pressure_cause; |
4090 | switch (cause) { | |
4091 | case kMemorystatusKilledFCThrashing: | |
4092 | jetsam_reason_code = JETSAM_REASON_MEMORY_FCTHRASHING; | |
4093 | break; | |
4094 | case kMemorystatusKilledVMCompressorThrashing: | |
4095 | jetsam_reason_code = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING; | |
4096 | break; | |
4097 | case kMemorystatusKilledVMCompressorSpaceShortage: | |
4098 | jetsam_reason_code = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE; | |
4099 | break; | |
4100 | case kMemorystatusKilledZoneMapExhaustion: | |
4101 | jetsam_reason_code = JETSAM_REASON_ZONE_MAP_EXHAUSTION; | |
4102 | break; | |
4103 | case kMemorystatusKilledVMPageShortage: | |
4104 | /* falls through */ | |
4105 | default: | |
4106 | jetsam_reason_code = JETSAM_REASON_MEMORY_VMPAGESHORTAGE; | |
4107 | cause = kMemorystatusKilledVMPageShortage; | |
4108 | break; | |
4109 | } | |
0a7de745 | 4110 | |
cb323159 A |
4111 | /* Highwater */ |
4112 | boolean_t is_critical = TRUE; | |
4113 | if (memorystatus_act_on_hiwat_processes(&errors, &hwm_kill, &post_snapshot, &is_critical, &memory_reclaimed)) { | |
4114 | total_memory_reclaimed += memory_reclaimed; | |
4115 | if (is_critical == FALSE) { | |
4116 | /* | |
4117 | * For now, don't kill any other processes. | |
4118 | */ | |
4119 | break; | |
4120 | } else { | |
4121 | goto done; | |
4122 | } | |
4123 | } | |
0a7de745 | 4124 | |
cb323159 A |
4125 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, jetsam_reason_code); |
4126 | if (jetsam_reason == OS_REASON_NULL) { | |
4127 | printf("memorystatus_thread: failed to allocate jetsam reason\n"); | |
39236c6e | 4128 | } |
0a7de745 | 4129 | |
cb323159 A |
4130 | /* Only unlimited jetsam threads should act aggressive */ |
4131 | if (!jetsam_thread->limit_to_low_bands && | |
4132 | memorystatus_act_aggressive(cause, jetsam_reason, &jld_idle_kills, &corpse_list_purged, &post_snapshot, &memory_reclaimed)) { | |
4133 | total_memory_reclaimed += memory_reclaimed; | |
4134 | goto done; | |
d1ecb069 | 4135 | } |
3e170ce0 | 4136 | |
cb323159 A |
4137 | /* |
4138 | * memorystatus_kill_top_process() drops a reference, | |
4139 | * so take another one so we can continue to use this exit reason | |
4140 | * even after it returns | |
4141 | */ | |
4142 | os_reason_ref(jetsam_reason); | |
3e170ce0 | 4143 | |
cb323159 A |
4144 | /* LRU */ |
4145 | killed = memorystatus_kill_top_process(TRUE, sort_flag, cause, jetsam_reason, &priority, &errors, &memory_reclaimed); | |
4146 | sort_flag = FALSE; | |
4147 | ||
4148 | if (killed) { | |
4149 | total_memory_reclaimed += memory_reclaimed; | |
4150 | if (memorystatus_post_snapshot(priority, cause) == TRUE) { | |
4151 | post_snapshot = TRUE; | |
4152 | } | |
4153 | ||
4154 | /* Jetsam Loop Detection */ | |
4155 | if (memorystatus_jld_enabled == TRUE) { | |
4156 | if ((priority == JETSAM_PRIORITY_IDLE) || (priority == system_procs_aging_band) || (priority == applications_aging_band)) { | |
4157 | jld_idle_kills++; | |
4158 | } else { | |
4159 | /* | |
4160 | * We've reached into bands beyond idle deferred. | |
4161 | * We make no attempt to monitor them | |
4162 | */ | |
4163 | } | |
6d2010ae | 4164 | } |
316670eb | 4165 | |
cb323159 A |
4166 | /* |
4167 | * If we have jetsammed a process in or above JETSAM_PRIORITY_UI_SUPPORT | |
4168 | * then we attempt to relieve pressure by purging corpse memory and notifying | |
4169 | * anybody wanting to know this. | |
4170 | */ | |
4171 | if (priority >= JETSAM_PRIORITY_UI_SUPPORT) { | |
f427ee49 | 4172 | memorystatus_approaching_fg_band(&corpse_list_purged); |
0a7de745 | 4173 | } |
cb323159 | 4174 | goto done; |
39236c6e | 4175 | } |
316670eb | 4176 | |
cb323159 A |
4177 | if (memorystatus_avail_pages_below_critical()) { |
4178 | /* | |
4179 | * Still under pressure and unable to kill a process - purge corpse memory | |
f427ee49 | 4180 | * and get everything back from the pmap. |
cb323159 | 4181 | */ |
f427ee49 | 4182 | pmap_release_pages_fast(); |
cb323159 A |
4183 | if (total_corpses_count() > 0) { |
4184 | task_purge_all_corpses(); | |
4185 | corpse_list_purged = TRUE; | |
a39ff7e2 | 4186 | } |
0a7de745 | 4187 | |
cb323159 | 4188 | if (!jetsam_thread->limit_to_low_bands && memorystatus_avail_pages_below_critical()) { |
a39ff7e2 | 4189 | /* |
cb323159 | 4190 | * Still under pressure and unable to kill a process - panic |
a39ff7e2 | 4191 | */ |
f427ee49 | 4192 | panic("memorystatus_jetsam_thread: no victim! available pages:%llu\n", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
a39ff7e2 | 4193 | } |
cb323159 | 4194 | } |
0a7de745 | 4195 | |
cb323159 | 4196 | done: |
a39ff7e2 | 4197 | |
cb323159 A |
4198 | /* |
4199 | * We do not want to over-kill when thrashing has been detected. | |
4200 | * To avoid that, we reset the flag here and notify the | |
4201 | * compressor. | |
4202 | */ | |
4203 | if (is_reason_thrashing(kill_under_pressure_cause)) { | |
4204 | kill_under_pressure_cause = 0; | |
4205 | #if CONFIG_JETSAM | |
4206 | vm_thrashing_jetsam_done(); | |
4207 | #endif /* CONFIG_JETSAM */ | |
4208 | } else if (is_reason_zone_map_exhaustion(kill_under_pressure_cause)) { | |
4209 | kill_under_pressure_cause = 0; | |
6d2010ae | 4210 | } |
cb323159 A |
4211 | |
4212 | os_reason_free(jetsam_reason); | |
6d2010ae | 4213 | } |
0a7de745 | 4214 | |
cb323159 | 4215 | kill_under_pressure_cause = 0; |
0a7de745 | 4216 | |
cb323159 A |
4217 | if (errors) { |
4218 | memorystatus_clear_errors(); | |
4219 | } | |
39037602 | 4220 | |
cb323159 | 4221 | if (post_snapshot) { |
39037602 | 4222 | proc_list_lock(); |
cb323159 A |
4223 | size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + |
4224 | sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count); | |
4225 | uint64_t timestamp_now = mach_absolute_time(); | |
4226 | memorystatus_jetsam_snapshot->notification_time = timestamp_now; | |
4227 | memorystatus_jetsam_snapshot->js_gencount++; | |
4228 | if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || | |
4229 | timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { | |
4230 | proc_list_unlock(); | |
4231 | int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); | |
4232 | if (!ret) { | |
4233 | proc_list_lock(); | |
4234 | memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; | |
4235 | proc_list_unlock(); | |
4236 | } | |
4237 | } else { | |
4238 | proc_list_unlock(); | |
4239 | } | |
39037602 | 4240 | } |
0a7de745 | 4241 | |
cb323159 | 4242 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_END, |
f427ee49 | 4243 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, total_memory_reclaimed, 0, 0, 0); |
39037602 | 4244 | |
cb323159 | 4245 | memorystatus_thread_block(0, memorystatus_thread); |
39037602 A |
4246 | } |
4247 | ||
4248 | /* | |
cb323159 A |
4249 | * Returns TRUE: |
4250 | * when an idle-exitable proc was killed | |
4251 | * Returns FALSE: | |
4252 | * when there are no more idle-exitable procs found | |
4253 | * when the attempt to kill an idle-exitable proc failed | |
39037602 | 4254 | */ |
cb323159 A |
4255 | boolean_t |
4256 | memorystatus_idle_exit_from_VM(void) | |
39037602 | 4257 | { |
cb323159 A |
4258 | /* |
4259 | * This routine should no longer be needed since we are | |
4260 | * now using jetsam bands on all platforms and so will deal | |
4261 | * with IDLE processes within the memorystatus thread itself. | |
4262 | * | |
4263 | * But we still use it because we observed that macos systems | |
4264 | * started heavy compression/swapping with a bunch of | |
4265 | * idle-exitable processes alive and doing nothing. We decided | |
4266 | * to rather kill those processes than start swapping earlier. | |
4267 | */ | |
d9a64523 | 4268 | |
cb323159 A |
4269 | return kill_idle_exit_proc(); |
4270 | } | |
39037602 | 4271 | |
cb323159 A |
4272 | /* |
4273 | * Callback invoked when allowable physical memory footprint exceeded | |
4274 | * (dirty pages + IOKit mappings) | |
4275 | * | |
4276 | * This is invoked for both advisory, non-fatal per-task high watermarks, | |
4277 | * as well as the fatal task memory limits. | |
4278 | */ | |
4279 | void | |
4280 | memorystatus_on_ledger_footprint_exceeded(boolean_t warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal) | |
4281 | { | |
4282 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
39037602 | 4283 | |
cb323159 | 4284 | proc_t p = current_proc(); |
39037602 | 4285 | |
cb323159 A |
4286 | #if VM_PRESSURE_EVENTS |
4287 | if (warning == TRUE) { | |
39037602 | 4288 | /* |
cb323159 A |
4289 | * This is a warning path which implies that the current process is close, but has |
4290 | * not yet exceeded its per-process memory limit. | |
4291 | */ | |
f427ee49 | 4292 | if (memorystatus_warn_process(p, memlimit_is_active, memlimit_is_fatal, FALSE /* not exceeded */) != TRUE) { |
cb323159 A |
4293 | /* Print warning, since it's possible that task has not registered for pressure notifications */ |
4294 | 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); | |
39037602 | 4295 | } |
cb323159 A |
4296 | return; |
4297 | } | |
4298 | #endif /* VM_PRESSURE_EVENTS */ | |
39037602 | 4299 | |
cb323159 | 4300 | if (memlimit_is_fatal) { |
39037602 | 4301 | /* |
cb323159 A |
4302 | * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task |
4303 | * has violated either the system-wide per-task memory limit OR its own task limit. | |
39037602 | 4304 | */ |
cb323159 A |
4305 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_PERPROCESSLIMIT); |
4306 | if (jetsam_reason == NULL) { | |
4307 | printf("task_exceeded footprint: failed to allocate jetsam reason\n"); | |
4308 | } else if (corpse_for_fatal_memkill != 0 && proc_send_synchronous_EXC_RESOURCE(p) == FALSE) { | |
4309 | /* Set OS_REASON_FLAG_GENERATE_CRASH_REPORT to generate corpse */ | |
4310 | jetsam_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT; | |
4311 | } | |
39037602 | 4312 | |
cb323159 A |
4313 | if (memorystatus_kill_process_sync(p->p_pid, kMemorystatusKilledPerProcessLimit, jetsam_reason) != TRUE) { |
4314 | printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n"); | |
4315 | } | |
4316 | } else { | |
4317 | /* | |
4318 | * HWM offender exists. Done without locks or synchronization. | |
4319 | * See comment near its declaration for more details. | |
4320 | */ | |
4321 | memorystatus_hwm_candidates = TRUE; | |
39037602 | 4322 | |
cb323159 A |
4323 | #if VM_PRESSURE_EVENTS |
4324 | /* | |
4325 | * The current process is not in the warning path. | |
4326 | * This path implies the current process has exceeded a non-fatal (soft) memory limit. | |
4327 | * Failure to send note is ignored here. | |
4328 | */ | |
f427ee49 | 4329 | (void)memorystatus_warn_process(p, memlimit_is_active, memlimit_is_fatal, TRUE /* exceeded */); |
39037602 | 4330 | |
cb323159 | 4331 | #endif /* VM_PRESSURE_EVENTS */ |
316670eb | 4332 | } |
316670eb | 4333 | } |
2d21ac55 | 4334 | |
cb323159 A |
4335 | void |
4336 | memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal) | |
0a7de745 | 4337 | { |
cb323159 A |
4338 | proc_t p = current_proc(); |
4339 | ||
39037602 | 4340 | /* |
cb323159 A |
4341 | * The limit violation is logged here, but only once per process per limit. |
4342 | * Soft memory limit is a non-fatal high-water-mark | |
4343 | * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit. | |
39037602 | 4344 | */ |
0a7de745 | 4345 | |
cb323159 A |
4346 | os_log_with_startup_serial(OS_LOG_DEFAULT, "EXC_RESOURCE -> %s[%d] exceeded mem limit: %s%s %d MB (%s)\n", |
4347 | ((p && *p->p_name) ? p->p_name : "unknown"), (p ? p->p_pid : -1), (memlimit_is_active ? "Active" : "Inactive"), | |
4348 | (memlimit_is_fatal ? "Hard" : "Soft"), max_footprint_mb, | |
4349 | (memlimit_is_fatal ? "fatal" : "non-fatal")); | |
4350 | ||
4351 | return; | |
39236c6e | 4352 | } |
2d21ac55 | 4353 | |
39037602 | 4354 | |
cb323159 A |
4355 | /* |
4356 | * Description: | |
4357 | * Evaluates process state to determine which limit | |
4358 | * should be applied (active vs. inactive limit). | |
4359 | * | |
4360 | * Processes that have the 'elevated inactive jetsam band' attribute | |
4361 | * are first evaluated based on their current priority band. | |
4362 | * presently elevated ==> active | |
4363 | * | |
4364 | * Processes that opt into dirty tracking are evaluated | |
4365 | * based on clean vs dirty state. | |
4366 | * dirty ==> active | |
4367 | * clean ==> inactive | |
4368 | * | |
4369 | * Process that do not opt into dirty tracking are | |
4370 | * evalulated based on priority level. | |
4371 | * Foreground or above ==> active | |
4372 | * Below Foreground ==> inactive | |
4373 | * | |
4374 | * Return: TRUE if active | |
4375 | * False if inactive | |
4376 | */ | |
2d21ac55 | 4377 | |
cb323159 A |
4378 | static boolean_t |
4379 | proc_jetsam_state_is_active_locked(proc_t p) | |
0a7de745 | 4380 | { |
cb323159 A |
4381 | if ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) && |
4382 | (p->p_memstat_effectivepriority == JETSAM_PRIORITY_ELEVATED_INACTIVE)) { | |
4383 | /* | |
4384 | * process has the 'elevated inactive jetsam band' attribute | |
4385 | * and process is present in the elevated band | |
4386 | * implies active state | |
4387 | */ | |
4388 | return TRUE; | |
4389 | } else if (p->p_memstat_dirty & P_DIRTY_TRACK) { | |
4390 | /* | |
4391 | * process has opted into dirty tracking | |
4392 | * active state is based on dirty vs. clean | |
4393 | */ | |
4394 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { | |
4395 | /* | |
4396 | * process is dirty | |
4397 | * implies active state | |
4398 | */ | |
4399 | return TRUE; | |
4400 | } else { | |
4401 | /* | |
4402 | * process is clean | |
4403 | * implies inactive state | |
4404 | */ | |
4405 | return FALSE; | |
d9a64523 | 4406 | } |
cb323159 A |
4407 | } else if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) { |
4408 | /* | |
4409 | * process is Foreground or higher | |
4410 | * implies active state | |
4411 | */ | |
4412 | return TRUE; | |
4413 | } else { | |
4414 | /* | |
4415 | * process found below Foreground | |
4416 | * implies inactive state | |
4417 | */ | |
4418 | return FALSE; | |
d9a64523 A |
4419 | } |
4420 | } | |
4421 | ||
cb323159 A |
4422 | static boolean_t |
4423 | memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) | |
0a7de745 | 4424 | { |
cb323159 | 4425 | boolean_t res; |
39037602 | 4426 | |
cb323159 A |
4427 | uint32_t errors = 0; |
4428 | uint64_t memory_reclaimed = 0; | |
b0d623f7 | 4429 | |
cb323159 A |
4430 | if (victim_pid == -1) { |
4431 | /* No pid, so kill first process */ | |
4432 | res = memorystatus_kill_top_process(TRUE, TRUE, cause, jetsam_reason, NULL, &errors, &memory_reclaimed); | |
fe8ab488 | 4433 | } else { |
cb323159 A |
4434 | res = memorystatus_kill_specific_process(victim_pid, cause, jetsam_reason); |
4435 | } | |
39037602 | 4436 | |
cb323159 A |
4437 | if (errors) { |
4438 | memorystatus_clear_errors(); | |
fe8ab488 | 4439 | } |
fe8ab488 | 4440 | |
cb323159 A |
4441 | if (res == TRUE) { |
4442 | /* Fire off snapshot notification */ | |
4443 | proc_list_lock(); | |
4444 | size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + | |
4445 | sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count; | |
4446 | uint64_t timestamp_now = mach_absolute_time(); | |
4447 | memorystatus_jetsam_snapshot->notification_time = timestamp_now; | |
4448 | if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || | |
4449 | timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { | |
4450 | proc_list_unlock(); | |
4451 | int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); | |
4452 | if (!ret) { | |
4453 | proc_list_lock(); | |
4454 | memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; | |
4455 | proc_list_unlock(); | |
4456 | } | |
4457 | } else { | |
4458 | proc_list_unlock(); | |
4459 | } | |
39037602 A |
4460 | } |
4461 | ||
cb323159 | 4462 | return res; |
39236c6e A |
4463 | } |
4464 | ||
cb323159 A |
4465 | /* |
4466 | * Jetsam a specific process. | |
4467 | */ | |
4468 | static boolean_t | |
4469 | memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) | |
0a7de745 | 4470 | { |
cb323159 A |
4471 | boolean_t killed; |
4472 | proc_t p; | |
4473 | uint64_t killtime = 0; | |
4474 | uint64_t footprint_of_killed_proc; | |
4475 | clock_sec_t tv_sec; | |
4476 | clock_usec_t tv_usec; | |
4477 | uint32_t tv_msec; | |
4478 | ||
4479 | /* TODO - add a victim queue and push this into the main jetsam thread */ | |
5ba3f43e | 4480 | |
cb323159 A |
4481 | p = proc_find(victim_pid); |
4482 | if (!p) { | |
4483 | os_reason_free(jetsam_reason); | |
4484 | return FALSE; | |
5ba3f43e | 4485 | } |
5ba3f43e | 4486 | |
cb323159 | 4487 | proc_list_lock(); |
6d2010ae | 4488 | |
cb323159 A |
4489 | if (memorystatus_jetsam_snapshot_count == 0) { |
4490 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); | |
4491 | } | |
3e170ce0 | 4492 | |
cb323159 A |
4493 | killtime = mach_absolute_time(); |
4494 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
4495 | tv_msec = tv_usec / 1000; | |
3e170ce0 | 4496 | |
cb323159 | 4497 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); |
d9a64523 | 4498 | |
cb323159 | 4499 | proc_list_unlock(); |
d9a64523 | 4500 | |
cb323159 | 4501 | killed = memorystatus_do_kill(p, cause, jetsam_reason, &footprint_of_killed_proc); |
d9a64523 | 4502 | |
cb323159 A |
4503 | os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: killing_specific_process pid %d [%s] (%s %d) %lluKB - memorystatus_available_pages: %llu\n", |
4504 | (unsigned long)tv_sec, tv_msec, victim_pid, ((p && *p->p_name) ? p->p_name : "unknown"), | |
4505 | memorystatus_kill_cause_name[cause], (p ? p->p_memstat_effectivepriority: -1), | |
f427ee49 | 4506 | footprint_of_killed_proc >> 10, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
cb323159 A |
4507 | |
4508 | proc_rele(p); | |
4509 | ||
4510 | return killed; | |
6d2010ae A |
4511 | } |
4512 | ||
cb323159 A |
4513 | |
4514 | /* | |
4515 | * Toggle the P_MEMSTAT_TERMINATED state. | |
4516 | * Takes the proc_list_lock. | |
4517 | */ | |
4518 | void | |
4519 | proc_memstat_terminated(proc_t p, boolean_t set) | |
d9a64523 | 4520 | { |
cb323159 A |
4521 | #if DEVELOPMENT || DEBUG |
4522 | if (p) { | |
4523 | proc_list_lock(); | |
4524 | if (set == TRUE) { | |
4525 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
4526 | } else { | |
4527 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
4528 | } | |
4529 | proc_list_unlock(); | |
4530 | } | |
4531 | #else | |
4532 | #pragma unused(p, set) | |
d9a64523 | 4533 | /* |
cb323159 | 4534 | * do nothing |
d9a64523 | 4535 | */ |
cb323159 A |
4536 | #endif /* DEVELOPMENT || DEBUG */ |
4537 | return; | |
4538 | } | |
d9a64523 | 4539 | |
d9a64523 | 4540 | |
cb323159 A |
4541 | #if CONFIG_JETSAM |
4542 | /* | |
4543 | * This is invoked when cpulimits have been exceeded while in fatal mode. | |
4544 | * The jetsam_flags do not apply as those are for memory related kills. | |
4545 | * We call this routine so that the offending process is killed with | |
4546 | * a non-zero exit status. | |
4547 | */ | |
4548 | void | |
4549 | jetsam_on_ledger_cpulimit_exceeded(void) | |
4550 | { | |
4551 | int retval = 0; | |
4552 | int jetsam_flags = 0; /* make it obvious */ | |
4553 | proc_t p = current_proc(); | |
4554 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
4555 | ||
4556 | printf("task_exceeded_cpulimit: killing pid %d [%s]\n", | |
4557 | p->p_pid, (*p->p_name ? p->p_name : "(unknown)")); | |
d9a64523 | 4558 | |
cb323159 A |
4559 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_CPULIMIT); |
4560 | if (jetsam_reason == OS_REASON_NULL) { | |
4561 | printf("task_exceeded_cpulimit: unable to allocate memory for jetsam reason\n"); | |
d9a64523 A |
4562 | } |
4563 | ||
cb323159 | 4564 | retval = jetsam_do_kill(p, jetsam_flags, jetsam_reason); |
d9a64523 | 4565 | |
cb323159 A |
4566 | if (retval) { |
4567 | printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n"); | |
d9a64523 | 4568 | } |
cb323159 | 4569 | } |
d9a64523 | 4570 | |
cb323159 | 4571 | #endif /* CONFIG_JETSAM */ |
d9a64523 | 4572 | |
cb323159 A |
4573 | static void |
4574 | memorystatus_get_task_memory_region_count(task_t task, uint64_t *count) | |
4575 | { | |
4576 | assert(task); | |
4577 | assert(count); | |
d9a64523 | 4578 | |
cb323159 A |
4579 | *count = get_task_memory_region_count(task); |
4580 | } | |
d9a64523 | 4581 | |
d9a64523 | 4582 | |
cb323159 A |
4583 | #define MEMORYSTATUS_VM_MAP_FORK_ALLOWED 0x100000000 |
4584 | #define MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED 0x200000000 | |
4585 | ||
4586 | #if DEVELOPMENT || DEBUG | |
d9a64523 | 4587 | |
3e170ce0 | 4588 | /* |
cb323159 A |
4589 | * Sysctl only used to test memorystatus_allowed_vm_map_fork() path. |
4590 | * set a new pidwatch value | |
4591 | * or | |
4592 | * get the current pidwatch value | |
d9a64523 | 4593 | * |
cb323159 A |
4594 | * The pidwatch_val starts out with a PID to watch for in the map_fork path. |
4595 | * Its value is: | |
4596 | * - OR'd with MEMORYSTATUS_VM_MAP_FORK_ALLOWED if we allow the map_fork. | |
4597 | * - OR'd with MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED if we disallow the map_fork. | |
4598 | * - set to -1ull if the map_fork() is aborted for other reasons. | |
3e170ce0 | 4599 | */ |
3e170ce0 | 4600 | |
cb323159 | 4601 | uint64_t memorystatus_vm_map_fork_pidwatch_val = 0; |
3e170ce0 | 4602 | |
cb323159 A |
4603 | static int sysctl_memorystatus_vm_map_fork_pidwatch SYSCTL_HANDLER_ARGS { |
4604 | #pragma unused(oidp, arg1, arg2) | |
3e170ce0 | 4605 | |
cb323159 A |
4606 | uint64_t new_value = 0; |
4607 | uint64_t old_value = 0; | |
4608 | int error = 0; | |
3e170ce0 | 4609 | |
cb323159 A |
4610 | /* |
4611 | * The pid is held in the low 32 bits. | |
4612 | * The 'allowed' flags are in the upper 32 bits. | |
4613 | */ | |
4614 | old_value = memorystatus_vm_map_fork_pidwatch_val; | |
3e170ce0 | 4615 | |
cb323159 A |
4616 | error = sysctl_io_number(req, old_value, sizeof(old_value), &new_value, NULL); |
4617 | ||
4618 | if (error || !req->newptr) { | |
4619 | /* | |
4620 | * No new value passed in. | |
4621 | */ | |
4622 | return error; | |
3e170ce0 A |
4623 | } |
4624 | ||
cb323159 A |
4625 | /* |
4626 | * A new pid was passed in via req->newptr. | |
4627 | * Ignore any attempt to set the higher order bits. | |
4628 | */ | |
4629 | memorystatus_vm_map_fork_pidwatch_val = new_value & 0xFFFFFFFF; | |
4630 | printf("memorystatus: pidwatch old_value = 0x%llx, new_value = 0x%llx \n", old_value, new_value); | |
3e170ce0 | 4631 | |
cb323159 A |
4632 | return error; |
4633 | } | |
3e170ce0 | 4634 | |
cb323159 A |
4635 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_map_fork_pidwatch, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
4636 | 0, 0, sysctl_memorystatus_vm_map_fork_pidwatch, "Q", "get/set pid watched for in vm_map_fork"); | |
3e170ce0 | 4637 | |
3e170ce0 | 4638 | |
cb323159 A |
4639 | /* |
4640 | * Record if a watched process fails to qualify for a vm_map_fork(). | |
4641 | */ | |
4642 | void | |
4643 | memorystatus_abort_vm_map_fork(task_t task) | |
4644 | { | |
4645 | if (memorystatus_vm_map_fork_pidwatch_val != 0) { | |
4646 | proc_t p = get_bsdtask_info(task); | |
4647 | if (p != NULL && memorystatus_vm_map_fork_pidwatch_val == (uint64_t)p->p_pid) { | |
4648 | memorystatus_vm_map_fork_pidwatch_val = -1ull; | |
3e170ce0 | 4649 | } |
cb323159 A |
4650 | } |
4651 | } | |
3e170ce0 | 4652 | |
cb323159 A |
4653 | static void |
4654 | set_vm_map_fork_pidwatch(task_t task, uint64_t x) | |
4655 | { | |
4656 | if (memorystatus_vm_map_fork_pidwatch_val != 0) { | |
4657 | proc_t p = get_bsdtask_info(task); | |
4658 | if (p && (memorystatus_vm_map_fork_pidwatch_val == (uint64_t)p->p_pid)) { | |
4659 | memorystatus_vm_map_fork_pidwatch_val |= x; | |
4660 | } | |
4661 | } | |
4662 | } | |
d9a64523 | 4663 | |
cb323159 | 4664 | #else /* DEVELOPMENT || DEBUG */ |
3e170ce0 | 4665 | |
39037602 | 4666 | |
cb323159 A |
4667 | static void |
4668 | set_vm_map_fork_pidwatch(task_t task, uint64_t x) | |
4669 | { | |
4670 | #pragma unused(task) | |
4671 | #pragma unused(x) | |
4672 | } | |
3e170ce0 | 4673 | |
cb323159 | 4674 | #endif /* DEVELOPMENT || DEBUG */ |
3e170ce0 | 4675 | |
cb323159 A |
4676 | /* |
4677 | * Called during EXC_RESOURCE handling when a process exceeds a soft | |
4678 | * memory limit. This is the corpse fork path and here we decide if | |
4679 | * vm_map_fork will be allowed when creating the corpse. | |
4680 | * The task being considered is suspended. | |
4681 | * | |
4682 | * By default, a vm_map_fork is allowed to proceed. | |
4683 | * | |
4684 | * A few simple policy assumptions: | |
cb323159 | 4685 | * If the device has a zero system-wide task limit, |
f427ee49 A |
4686 | * then the vm_map_fork is allowed. macOS always has a zero |
4687 | * system wide task limit (unless overriden by a boot-arg). | |
cb323159 A |
4688 | * |
4689 | * And if a process's memory footprint calculates less | |
94ff46dc | 4690 | * than or equal to quarter of the system-wide task limit, |
cb323159 A |
4691 | * then the vm_map_fork is allowed. This calculation |
4692 | * is based on the assumption that a process can | |
4693 | * munch memory up to the system-wide task limit. | |
4694 | */ | |
94ff46dc | 4695 | extern boolean_t corpse_threshold_system_limit; |
cb323159 A |
4696 | boolean_t |
4697 | memorystatus_allowed_vm_map_fork(task_t task) | |
4698 | { | |
4699 | boolean_t is_allowed = TRUE; /* default */ | |
d9a64523 | 4700 | |
cb323159 A |
4701 | uint64_t footprint_in_bytes; |
4702 | uint64_t max_allowed_bytes; | |
3e170ce0 | 4703 | |
cb323159 A |
4704 | if (max_task_footprint_mb == 0) { |
4705 | set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED); | |
4706 | return is_allowed; | |
4707 | } | |
3e170ce0 | 4708 | |
cb323159 | 4709 | footprint_in_bytes = get_task_phys_footprint(task); |
3e170ce0 | 4710 | |
cb323159 | 4711 | /* |
94ff46dc | 4712 | * Maximum is 1/4 of the system-wide task limit by default. |
cb323159 A |
4713 | */ |
4714 | max_allowed_bytes = ((uint64_t)max_task_footprint_mb * 1024 * 1024) >> 2; | |
3e170ce0 | 4715 | |
94ff46dc A |
4716 | #if DEBUG || DEVELOPMENT |
4717 | if (corpse_threshold_system_limit) { | |
4718 | max_allowed_bytes = (uint64_t)max_task_footprint_mb * (1UL << 20); | |
4719 | } | |
4720 | #endif /* DEBUG || DEVELOPMENT */ | |
4721 | ||
cb323159 A |
4722 | if (footprint_in_bytes > max_allowed_bytes) { |
4723 | printf("memorystatus disallowed vm_map_fork %lld %lld\n", footprint_in_bytes, max_allowed_bytes); | |
4724 | set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED); | |
4725 | return !is_allowed; | |
4726 | } | |
3e170ce0 | 4727 | |
cb323159 A |
4728 | set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED); |
4729 | return is_allowed; | |
4730 | } | |
d9a64523 | 4731 | |
cb323159 A |
4732 | void |
4733 | memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages) | |
4734 | { | |
4735 | assert(task); | |
4736 | assert(footprint); | |
d9a64523 | 4737 | |
cb323159 | 4738 | uint64_t pages; |
d9a64523 | 4739 | |
cb323159 A |
4740 | pages = (get_task_phys_footprint(task) / PAGE_SIZE_64); |
4741 | assert(((uint32_t)pages) == pages); | |
4742 | *footprint = (uint32_t)pages; | |
d9a64523 | 4743 | |
cb323159 A |
4744 | if (max_footprint_lifetime) { |
4745 | pages = (get_task_phys_footprint_lifetime_max(task) / PAGE_SIZE_64); | |
4746 | assert(((uint32_t)pages) == pages); | |
4747 | *max_footprint_lifetime = (uint32_t)pages; | |
4748 | } | |
4749 | if (purgeable_pages) { | |
4750 | pages = (get_task_purgeable_size(task) / PAGE_SIZE_64); | |
4751 | assert(((uint32_t)pages) == pages); | |
4752 | *purgeable_pages = (uint32_t)pages; | |
4753 | } | |
4754 | } | |
d9a64523 | 4755 | |
cb323159 A |
4756 | static void |
4757 | memorystatus_get_task_phys_footprint_page_counts(task_t task, | |
4758 | uint64_t *internal_pages, uint64_t *internal_compressed_pages, | |
4759 | uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, | |
4760 | uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, | |
f427ee49 | 4761 | uint64_t *iokit_mapped_pages, uint64_t *page_table_pages, uint64_t *frozen_to_swap_pages) |
cb323159 A |
4762 | { |
4763 | assert(task); | |
d9a64523 | 4764 | |
cb323159 A |
4765 | if (internal_pages) { |
4766 | *internal_pages = (get_task_internal(task) / PAGE_SIZE_64); | |
4767 | } | |
d9a64523 | 4768 | |
cb323159 A |
4769 | if (internal_compressed_pages) { |
4770 | *internal_compressed_pages = (get_task_internal_compressed(task) / PAGE_SIZE_64); | |
4771 | } | |
d9a64523 | 4772 | |
cb323159 A |
4773 | if (purgeable_nonvolatile_pages) { |
4774 | *purgeable_nonvolatile_pages = (get_task_purgeable_nonvolatile(task) / PAGE_SIZE_64); | |
4775 | } | |
d9a64523 | 4776 | |
cb323159 A |
4777 | if (purgeable_nonvolatile_compressed_pages) { |
4778 | *purgeable_nonvolatile_compressed_pages = (get_task_purgeable_nonvolatile_compressed(task) / PAGE_SIZE_64); | |
4779 | } | |
d9a64523 | 4780 | |
cb323159 A |
4781 | if (alternate_accounting_pages) { |
4782 | *alternate_accounting_pages = (get_task_alternate_accounting(task) / PAGE_SIZE_64); | |
4783 | } | |
d9a64523 | 4784 | |
cb323159 A |
4785 | if (alternate_accounting_compressed_pages) { |
4786 | *alternate_accounting_compressed_pages = (get_task_alternate_accounting_compressed(task) / PAGE_SIZE_64); | |
3e170ce0 A |
4787 | } |
4788 | ||
cb323159 A |
4789 | if (iokit_mapped_pages) { |
4790 | *iokit_mapped_pages = (get_task_iokit_mapped(task) / PAGE_SIZE_64); | |
4791 | } | |
3e170ce0 | 4792 | |
cb323159 A |
4793 | if (page_table_pages) { |
4794 | *page_table_pages = (get_task_page_table(task) / PAGE_SIZE_64); | |
4795 | } | |
f427ee49 A |
4796 | |
4797 | #if CONFIG_FREEZE | |
4798 | if (frozen_to_swap_pages) { | |
4799 | *frozen_to_swap_pages = (get_task_frozen_to_swap(task) / PAGE_SIZE_64); | |
4800 | } | |
4801 | #else /* CONFIG_FREEZE */ | |
4802 | #pragma unused(frozen_to_swap_pages) | |
4803 | #endif /* CONFIG_FREEZE */ | |
4804 | } | |
4805 | ||
4806 | #if CONFIG_FREEZE | |
4807 | /* | |
4808 | * Copies the source entry into the destination snapshot. | |
4809 | * Returns true on success. Fails if the destination snapshot is full. | |
4810 | * Caller must hold the proc list lock. | |
4811 | */ | |
4812 | static bool | |
4813 | memorystatus_jetsam_snapshot_copy_entry_locked(memorystatus_jetsam_snapshot_t *dst_snapshot, unsigned int dst_snapshot_size, const memorystatus_jetsam_snapshot_entry_t *src_entry) | |
4814 | { | |
4815 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
4816 | assert(dst_snapshot); | |
4817 | ||
4818 | if (dst_snapshot->entry_count == dst_snapshot_size) { | |
4819 | /* Destination snapshot is full. Can not be updated until it is consumed. */ | |
4820 | return false; | |
4821 | } | |
4822 | if (dst_snapshot->entry_count == 0) { | |
4823 | memorystatus_init_jetsam_snapshot_header(dst_snapshot); | |
4824 | } | |
4825 | memorystatus_jetsam_snapshot_entry_t *dst_entry = &dst_snapshot->entries[dst_snapshot->entry_count++]; | |
4826 | memcpy(dst_entry, src_entry, sizeof(memorystatus_jetsam_snapshot_entry_t)); | |
4827 | return true; | |
4828 | } | |
4829 | #endif /* CONFIG_FREEZE */ | |
4830 | ||
4831 | static bool | |
4832 | memorystatus_init_jetsam_snapshot_entry_with_kill_locked(memorystatus_jetsam_snapshot_t *snapshot, proc_t p, uint32_t kill_cause, uint64_t killtime, memorystatus_jetsam_snapshot_entry_t **entry) | |
4833 | { | |
4834 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
4835 | memorystatus_jetsam_snapshot_entry_t *snapshot_list = snapshot->entries; | |
4836 | size_t i = snapshot->entry_count; | |
4837 | ||
4838 | if (memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i], (snapshot->js_gencount)) == TRUE) { | |
4839 | *entry = &snapshot_list[i]; | |
4840 | (*entry)->killed = kill_cause; | |
4841 | (*entry)->jse_killtime = killtime; | |
4842 | ||
4843 | snapshot->entry_count = i + 1; | |
4844 | return true; | |
4845 | } | |
4846 | return false; | |
3e170ce0 A |
4847 | } |
4848 | ||
cb323159 A |
4849 | /* |
4850 | * This routine only acts on the global jetsam event snapshot. | |
4851 | * Updating the process's entry can race when the memorystatus_thread | |
4852 | * has chosen to kill a process that is racing to exit on another core. | |
4853 | */ | |
4854 | static void | |
4855 | memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime) | |
6d2010ae | 4856 | { |
cb323159 A |
4857 | memorystatus_jetsam_snapshot_entry_t *entry = NULL; |
4858 | memorystatus_jetsam_snapshot_t *snapshot = NULL; | |
4859 | memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL; | |
39236c6e | 4860 | |
cb323159 | 4861 | unsigned int i; |
f427ee49 A |
4862 | #if CONFIG_FREEZE |
4863 | bool copied_to_freezer_snapshot = false; | |
4864 | #endif /* CONFIG_FREEZE */ | |
cb323159 A |
4865 | |
4866 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 | 4867 | |
cb323159 | 4868 | if (memorystatus_jetsam_snapshot_count == 0) { |
d9a64523 | 4869 | /* |
cb323159 A |
4870 | * No active snapshot. |
4871 | * Nothing to do. | |
d9a64523 | 4872 | */ |
f427ee49 | 4873 | goto exit; |
d9a64523 A |
4874 | } |
4875 | ||
cb323159 A |
4876 | /* |
4877 | * Sanity check as this routine should only be called | |
4878 | * from a jetsam kill path. | |
4879 | */ | |
4880 | assert(kill_cause != 0 && killtime != 0); | |
6d2010ae | 4881 | |
cb323159 A |
4882 | snapshot = memorystatus_jetsam_snapshot; |
4883 | snapshot_list = memorystatus_jetsam_snapshot->entries; | |
6d2010ae | 4884 | |
cb323159 A |
4885 | for (i = 0; i < memorystatus_jetsam_snapshot_count; i++) { |
4886 | if (snapshot_list[i].pid == p->p_pid) { | |
4887 | entry = &snapshot_list[i]; | |
6d2010ae | 4888 | |
cb323159 A |
4889 | if (entry->killed || entry->jse_killtime) { |
4890 | /* | |
4891 | * We apparently raced on the exit path | |
4892 | * for this process, as it's snapshot entry | |
4893 | * has already recorded a kill. | |
4894 | */ | |
4895 | assert(entry->killed && entry->jse_killtime); | |
4896 | break; | |
d9a64523 A |
4897 | } |
4898 | ||
4899 | /* | |
cb323159 | 4900 | * Update the entry we just found in the snapshot. |
d9a64523 | 4901 | */ |
d9a64523 | 4902 | |
cb323159 A |
4903 | entry->killed = kill_cause; |
4904 | entry->jse_killtime = killtime; | |
4905 | entry->jse_gencount = snapshot->js_gencount; | |
4906 | entry->jse_idle_delta = p->p_memstat_idle_delta; | |
4907 | #if CONFIG_FREEZE | |
4908 | entry->jse_thaw_count = p->p_memstat_thaw_count; | |
2a1bd2d3 | 4909 | entry->jse_freeze_skip_reason = p->p_memstat_freeze_skip_reason; |
cb323159 A |
4910 | #else /* CONFIG_FREEZE */ |
4911 | entry->jse_thaw_count = 0; | |
2a1bd2d3 | 4912 | entry->jse_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone; |
cb323159 | 4913 | #endif /* CONFIG_FREEZE */ |
3e170ce0 | 4914 | |
d9a64523 | 4915 | /* |
cb323159 A |
4916 | * If a process has moved between bands since snapshot was |
4917 | * initialized, then likely these fields changed too. | |
d9a64523 | 4918 | */ |
cb323159 A |
4919 | if (entry->priority != p->p_memstat_effectivepriority) { |
4920 | strlcpy(entry->name, p->p_name, sizeof(entry->name)); | |
4921 | entry->priority = p->p_memstat_effectivepriority; | |
4922 | entry->state = memorystatus_build_state(p); | |
4923 | entry->user_data = p->p_memstat_userdata; | |
4924 | entry->fds = p->p_fd->fd_nfiles; | |
d9a64523 | 4925 | } |
3e170ce0 | 4926 | |
3e170ce0 | 4927 | /* |
cb323159 | 4928 | * Always update the page counts on a kill. |
3e170ce0 | 4929 | */ |
d9a64523 | 4930 | |
cb323159 A |
4931 | uint32_t pages = 0; |
4932 | uint32_t max_pages_lifetime = 0; | |
4933 | uint32_t purgeable_pages = 0; | |
0a7de745 | 4934 | |
cb323159 A |
4935 | memorystatus_get_task_page_counts(p->task, &pages, &max_pages_lifetime, &purgeable_pages); |
4936 | entry->pages = (uint64_t)pages; | |
4937 | entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; | |
4938 | entry->purgeable_pages = (uint64_t)purgeable_pages; | |
d9a64523 | 4939 | |
cb323159 A |
4940 | uint64_t internal_pages = 0; |
4941 | uint64_t internal_compressed_pages = 0; | |
4942 | uint64_t purgeable_nonvolatile_pages = 0; | |
4943 | uint64_t purgeable_nonvolatile_compressed_pages = 0; | |
4944 | uint64_t alternate_accounting_pages = 0; | |
4945 | uint64_t alternate_accounting_compressed_pages = 0; | |
4946 | uint64_t iokit_mapped_pages = 0; | |
4947 | uint64_t page_table_pages = 0; | |
f427ee49 | 4948 | uint64_t frozen_to_swap_pages = 0; |
0a7de745 | 4949 | |
cb323159 A |
4950 | memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, |
4951 | &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, | |
4952 | &alternate_accounting_pages, &alternate_accounting_compressed_pages, | |
f427ee49 | 4953 | &iokit_mapped_pages, &page_table_pages, &frozen_to_swap_pages); |
d9a64523 | 4954 | |
cb323159 A |
4955 | entry->jse_internal_pages = internal_pages; |
4956 | entry->jse_internal_compressed_pages = internal_compressed_pages; | |
4957 | entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages; | |
4958 | entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages; | |
4959 | entry->jse_alternate_accounting_pages = alternate_accounting_pages; | |
4960 | entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages; | |
4961 | entry->jse_iokit_mapped_pages = iokit_mapped_pages; | |
4962 | entry->jse_page_table_pages = page_table_pages; | |
f427ee49 | 4963 | entry->jse_frozen_to_swap_pages = frozen_to_swap_pages; |
d9a64523 | 4964 | |
cb323159 A |
4965 | uint64_t region_count = 0; |
4966 | memorystatus_get_task_memory_region_count(p->task, ®ion_count); | |
4967 | entry->jse_memory_region_count = region_count; | |
d9a64523 | 4968 | |
cb323159 A |
4969 | goto exit; |
4970 | } | |
4971 | } | |
d9a64523 | 4972 | |
cb323159 A |
4973 | if (entry == NULL) { |
4974 | /* | |
4975 | * The entry was not found in the snapshot, so the process must have | |
4976 | * launched after the snapshot was initialized. | |
4977 | * Let's try to append the new entry. | |
4978 | */ | |
4979 | if (memorystatus_jetsam_snapshot_count < memorystatus_jetsam_snapshot_max) { | |
d9a64523 | 4980 | /* |
cb323159 A |
4981 | * A populated snapshot buffer exists |
4982 | * and there is room to init a new entry. | |
d9a64523 | 4983 | */ |
cb323159 | 4984 | assert(memorystatus_jetsam_snapshot_count == snapshot->entry_count); |
6d2010ae | 4985 | |
f427ee49 A |
4986 | if (memorystatus_init_jetsam_snapshot_entry_with_kill_locked(snapshot, p, kill_cause, killtime, &entry)) { |
4987 | memorystatus_jetsam_snapshot_count++; | |
d9a64523 | 4988 | |
cb323159 | 4989 | if (memorystatus_jetsam_snapshot_count >= memorystatus_jetsam_snapshot_max) { |
d9a64523 | 4990 | /* |
cb323159 A |
4991 | * We just used the last slot in the snapshot buffer. |
4992 | * We only want to log it once... so we do it here | |
4993 | * when we notice we've hit the max. | |
d9a64523 | 4994 | */ |
cb323159 A |
4995 | printf("memorystatus: WARNING snapshot buffer is full, count %d\n", |
4996 | memorystatus_jetsam_snapshot_count); | |
d9a64523 | 4997 | } |
d9a64523 | 4998 | } |
cb323159 A |
4999 | } |
5000 | } | |
d9a64523 | 5001 | |
cb323159 | 5002 | exit: |
f427ee49 A |
5003 | if (entry) { |
5004 | #if CONFIG_FREEZE | |
5005 | if (memorystatus_jetsam_use_freezer_snapshot && isApp(p)) { | |
5006 | /* This is an app kill. Record it in the freezer snapshot so dasd can incorporate this in its recommendations. */ | |
5007 | copied_to_freezer_snapshot = memorystatus_jetsam_snapshot_copy_entry_locked(memorystatus_jetsam_snapshot_freezer, memorystatus_jetsam_snapshot_freezer_max, entry); | |
5008 | if (copied_to_freezer_snapshot && memorystatus_jetsam_snapshot_freezer->entry_count == memorystatus_jetsam_snapshot_freezer_max) { | |
5009 | /* | |
5010 | * We just used the last slot in the freezer snapshot buffer. | |
5011 | * We only want to log it once... so we do it here | |
5012 | * when we notice we've hit the max. | |
5013 | */ | |
5014 | os_log_error(OS_LOG_DEFAULT, "memorystatus: WARNING freezer snapshot buffer is full, count %zu", | |
5015 | memorystatus_jetsam_snapshot_freezer->entry_count); | |
5016 | } | |
5017 | } | |
5018 | #endif /* CONFIG_FREEZE */ | |
5019 | } else { | |
cb323159 A |
5020 | /* |
5021 | * If we reach here, the snapshot buffer could not be updated. | |
5022 | * Most likely, the buffer is full, in which case we would have | |
5023 | * logged a warning in the previous call. | |
5024 | * | |
5025 | * For now, we will stop appending snapshot entries. | |
5026 | * When the buffer is consumed, the snapshot state will reset. | |
5027 | */ | |
d9a64523 | 5028 | |
cb323159 A |
5029 | MEMORYSTATUS_DEBUG(4, "memorystatus_update_jetsam_snapshot_entry_locked: failed to update pid %d, priority %d, count %d\n", |
5030 | p->p_pid, p->p_memstat_effectivepriority, memorystatus_jetsam_snapshot_count); | |
f427ee49 A |
5031 | |
5032 | #if CONFIG_FREEZE | |
5033 | /* We still attempt to record this in the freezer snapshot */ | |
5034 | if (memorystatus_jetsam_use_freezer_snapshot && isApp(p)) { | |
5035 | snapshot = memorystatus_jetsam_snapshot_freezer; | |
5036 | if (snapshot->entry_count < memorystatus_jetsam_snapshot_freezer_max) { | |
5037 | copied_to_freezer_snapshot = memorystatus_init_jetsam_snapshot_entry_with_kill_locked(snapshot, p, kill_cause, killtime, &entry); | |
5038 | if (copied_to_freezer_snapshot && memorystatus_jetsam_snapshot_freezer->entry_count == memorystatus_jetsam_snapshot_freezer_max) { | |
5039 | /* | |
5040 | * We just used the last slot in the freezer snapshot buffer. | |
5041 | * We only want to log it once... so we do it here | |
5042 | * when we notice we've hit the max. | |
5043 | */ | |
5044 | os_log_error(OS_LOG_DEFAULT, "memorystatus: WARNING freezer snapshot buffer is full, count %zu", | |
5045 | memorystatus_jetsam_snapshot_freezer->entry_count); | |
5046 | } | |
5047 | } | |
5048 | } | |
5049 | #endif /* CONFIG_FREEZE */ | |
cb323159 | 5050 | } |
d9a64523 | 5051 | |
cb323159 A |
5052 | return; |
5053 | } | |
d9a64523 | 5054 | |
cb323159 A |
5055 | #if CONFIG_JETSAM |
5056 | void | |
5057 | memorystatus_pages_update(unsigned int pages_avail) | |
5058 | { | |
5059 | memorystatus_available_pages = pages_avail; | |
d9a64523 | 5060 | |
cb323159 A |
5061 | #if VM_PRESSURE_EVENTS |
5062 | /* | |
5063 | * Since memorystatus_available_pages changes, we should | |
5064 | * re-evaluate the pressure levels on the system and | |
5065 | * check if we need to wake the pressure thread. | |
5066 | * We also update memorystatus_level in that routine. | |
5067 | */ | |
5068 | vm_pressure_response(); | |
d9a64523 | 5069 | |
cb323159 A |
5070 | if (memorystatus_available_pages <= memorystatus_available_pages_pressure) { |
5071 | if (memorystatus_hwm_candidates || (memorystatus_available_pages <= memorystatus_available_pages_critical)) { | |
5072 | memorystatus_thread_wake(); | |
316670eb | 5073 | } |
d9a64523 | 5074 | } |
cb323159 A |
5075 | #if CONFIG_FREEZE |
5076 | /* | |
5077 | * We can't grab the freezer_mutex here even though that synchronization would be correct to inspect | |
5078 | * the # of frozen processes and wakeup the freezer thread. Reason being that we come here into this | |
5079 | * code with (possibly) the page-queue locks held and preemption disabled. So trying to grab a mutex here | |
5080 | * will result in the "mutex with preemption disabled" panic. | |
5081 | */ | |
d9a64523 | 5082 | |
cb323159 | 5083 | if (memorystatus_freeze_thread_should_run() == TRUE) { |
d9a64523 | 5084 | /* |
cb323159 A |
5085 | * The freezer thread is usually woken up by some user-space call i.e. pid_hibernate(any process). |
5086 | * That trigger isn't invoked often enough and so we are enabling this explicit wakeup here. | |
d9a64523 | 5087 | */ |
cb323159 A |
5088 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { |
5089 | thread_wakeup((event_t)&memorystatus_freeze_wakeup); | |
5090 | } | |
5091 | } | |
5092 | #endif /* CONFIG_FREEZE */ | |
d9a64523 | 5093 | |
cb323159 | 5094 | #else /* VM_PRESSURE_EVENTS */ |
d9a64523 | 5095 | |
cb323159 | 5096 | boolean_t critical, delta; |
d9a64523 | 5097 | |
cb323159 A |
5098 | if (!memorystatus_delta) { |
5099 | return; | |
6d2010ae | 5100 | } |
0a7de745 | 5101 | |
cb323159 A |
5102 | critical = (pages_avail < memorystatus_available_pages_critical) ? TRUE : FALSE; |
5103 | delta = ((pages_avail >= (memorystatus_available_pages + memorystatus_delta)) | |
5104 | || (memorystatus_available_pages >= (pages_avail + memorystatus_delta))) ? TRUE : FALSE; | |
0a7de745 | 5105 | |
cb323159 A |
5106 | if (critical || delta) { |
5107 | unsigned int total_pages; | |
5108 | ||
5109 | total_pages = (unsigned int) atop_64(max_mem); | |
5110 | #if CONFIG_SECLUDED_MEMORY | |
5111 | total_pages -= vm_page_secluded_count; | |
5112 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
5113 | memorystatus_level = memorystatus_available_pages * 100 / total_pages; | |
5114 | memorystatus_thread_wake(); | |
5115 | } | |
5116 | #endif /* VM_PRESSURE_EVENTS */ | |
6d2010ae | 5117 | } |
cb323159 | 5118 | #endif /* CONFIG_JETSAM */ |
6d2010ae | 5119 | |
cb323159 A |
5120 | static boolean_t |
5121 | memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount) | |
6d2010ae | 5122 | { |
cb323159 A |
5123 | clock_sec_t tv_sec; |
5124 | clock_usec_t tv_usec; | |
5125 | uint32_t pages = 0; | |
5126 | uint32_t max_pages_lifetime = 0; | |
5127 | uint32_t purgeable_pages = 0; | |
5128 | uint64_t internal_pages = 0; | |
5129 | uint64_t internal_compressed_pages = 0; | |
5130 | uint64_t purgeable_nonvolatile_pages = 0; | |
5131 | uint64_t purgeable_nonvolatile_compressed_pages = 0; | |
5132 | uint64_t alternate_accounting_pages = 0; | |
5133 | uint64_t alternate_accounting_compressed_pages = 0; | |
5134 | uint64_t iokit_mapped_pages = 0; | |
5135 | uint64_t page_table_pages = 0; | |
f427ee49 | 5136 | uint64_t frozen_to_swap_pages = 0; |
cb323159 A |
5137 | uint64_t region_count = 0; |
5138 | uint64_t cids[COALITION_NUM_TYPES]; | |
0a7de745 | 5139 | |
cb323159 | 5140 | memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t)); |
6d2010ae | 5141 | |
cb323159 A |
5142 | entry->pid = p->p_pid; |
5143 | strlcpy(&entry->name[0], p->p_name, sizeof(entry->name)); | |
5144 | entry->priority = p->p_memstat_effectivepriority; | |
0a7de745 | 5145 | |
cb323159 A |
5146 | memorystatus_get_task_page_counts(p->task, &pages, &max_pages_lifetime, &purgeable_pages); |
5147 | entry->pages = (uint64_t)pages; | |
5148 | entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; | |
5149 | entry->purgeable_pages = (uint64_t)purgeable_pages; | |
0a7de745 | 5150 | |
cb323159 A |
5151 | memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, |
5152 | &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, | |
5153 | &alternate_accounting_pages, &alternate_accounting_compressed_pages, | |
f427ee49 | 5154 | &iokit_mapped_pages, &page_table_pages, &frozen_to_swap_pages); |
0a7de745 | 5155 | |
cb323159 A |
5156 | entry->jse_internal_pages = internal_pages; |
5157 | entry->jse_internal_compressed_pages = internal_compressed_pages; | |
5158 | entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages; | |
5159 | entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages; | |
5160 | entry->jse_alternate_accounting_pages = alternate_accounting_pages; | |
5161 | entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages; | |
5162 | entry->jse_iokit_mapped_pages = iokit_mapped_pages; | |
5163 | entry->jse_page_table_pages = page_table_pages; | |
f427ee49 | 5164 | entry->jse_frozen_to_swap_pages = frozen_to_swap_pages; |
6d2010ae | 5165 | |
cb323159 A |
5166 | memorystatus_get_task_memory_region_count(p->task, ®ion_count); |
5167 | entry->jse_memory_region_count = region_count; | |
3e170ce0 | 5168 | |
cb323159 A |
5169 | entry->state = memorystatus_build_state(p); |
5170 | entry->user_data = p->p_memstat_userdata; | |
5171 | memcpy(&entry->uuid[0], &p->p_uuid[0], sizeof(p->p_uuid)); | |
5172 | entry->fds = p->p_fd->fd_nfiles; | |
0a7de745 | 5173 | |
cb323159 A |
5174 | absolutetime_to_microtime(get_task_cpu_time(p->task), &tv_sec, &tv_usec); |
5175 | entry->cpu_time.tv_sec = (int64_t)tv_sec; | |
5176 | entry->cpu_time.tv_usec = (int64_t)tv_usec; | |
6d2010ae | 5177 | |
cb323159 A |
5178 | assert(p->p_stats != NULL); |
5179 | entry->jse_starttime = p->p_stats->ps_start; /* abstime process started */ | |
5180 | entry->jse_killtime = 0; /* abstime jetsam chose to kill process */ | |
5181 | entry->killed = 0; /* the jetsam kill cause */ | |
5182 | entry->jse_gencount = gencount; /* indicates a pass through jetsam thread, when process was targeted to be killed */ | |
3e170ce0 | 5183 | |
cb323159 | 5184 | entry->jse_idle_delta = p->p_memstat_idle_delta; /* Most recent timespan spent in idle-band */ |
3e170ce0 | 5185 | |
cb323159 | 5186 | #if CONFIG_FREEZE |
2a1bd2d3 | 5187 | entry->jse_freeze_skip_reason = p->p_memstat_freeze_skip_reason; |
cb323159 A |
5188 | entry->jse_thaw_count = p->p_memstat_thaw_count; |
5189 | #else /* CONFIG_FREEZE */ | |
5190 | entry->jse_thaw_count = 0; | |
2a1bd2d3 | 5191 | entry->jse_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone; |
cb323159 A |
5192 | #endif /* CONFIG_FREEZE */ |
5193 | ||
5194 | proc_coalitionids(p, cids); | |
5195 | entry->jse_coalition_jetsam_id = cids[COALITION_TYPE_JETSAM]; | |
5196 | ||
5197 | return TRUE; | |
5198 | } | |
5199 | ||
5200 | static void | |
5201 | memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot) | |
5202 | { | |
5203 | kern_return_t kr = KERN_SUCCESS; | |
5204 | mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; | |
5205 | vm_statistics64_data_t vm_stat; | |
5206 | ||
5207 | if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count)) != KERN_SUCCESS) { | |
5208 | printf("memorystatus_init_jetsam_snapshot_stats: host_statistics64 failed with %d\n", kr); | |
5209 | memset(&snapshot->stats, 0, sizeof(snapshot->stats)); | |
3e170ce0 | 5210 | } else { |
cb323159 A |
5211 | snapshot->stats.free_pages = vm_stat.free_count; |
5212 | snapshot->stats.active_pages = vm_stat.active_count; | |
5213 | snapshot->stats.inactive_pages = vm_stat.inactive_count; | |
5214 | snapshot->stats.throttled_pages = vm_stat.throttled_count; | |
5215 | snapshot->stats.purgeable_pages = vm_stat.purgeable_count; | |
5216 | snapshot->stats.wired_pages = vm_stat.wire_count; | |
3e170ce0 | 5217 | |
cb323159 A |
5218 | snapshot->stats.speculative_pages = vm_stat.speculative_count; |
5219 | snapshot->stats.filebacked_pages = vm_stat.external_page_count; | |
5220 | snapshot->stats.anonymous_pages = vm_stat.internal_page_count; | |
5221 | snapshot->stats.compressions = vm_stat.compressions; | |
5222 | snapshot->stats.decompressions = vm_stat.decompressions; | |
5223 | snapshot->stats.compressor_pages = vm_stat.compressor_page_count; | |
5224 | snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor; | |
6d2010ae | 5225 | } |
0a7de745 | 5226 | |
cb323159 A |
5227 | get_zone_map_size(&snapshot->stats.zone_map_size, &snapshot->stats.zone_map_capacity); |
5228 | ||
5229 | bzero(snapshot->stats.largest_zone_name, sizeof(snapshot->stats.largest_zone_name)); | |
5230 | get_largest_zone_info(snapshot->stats.largest_zone_name, sizeof(snapshot->stats.largest_zone_name), | |
5231 | &snapshot->stats.largest_zone_size); | |
6d2010ae A |
5232 | } |
5233 | ||
d9a64523 | 5234 | /* |
cb323159 A |
5235 | * Collect vm statistics at boot. |
5236 | * Called only once (see kern_exec.c) | |
5237 | * Data can be consumed at any time. | |
d9a64523 | 5238 | */ |
cb323159 A |
5239 | void |
5240 | memorystatus_init_at_boot_snapshot() | |
5241 | { | |
5242 | memorystatus_init_snapshot_vmstats(&memorystatus_at_boot_snapshot); | |
5243 | memorystatus_at_boot_snapshot.entry_count = 0; | |
5244 | memorystatus_at_boot_snapshot.notification_time = 0; /* updated when consumed */ | |
5245 | memorystatus_at_boot_snapshot.snapshot_time = mach_absolute_time(); | |
5246 | } | |
5247 | ||
f427ee49 A |
5248 | static void |
5249 | memorystatus_init_jetsam_snapshot_header(memorystatus_jetsam_snapshot_t *snapshot) | |
5250 | { | |
5251 | memorystatus_init_snapshot_vmstats(snapshot); | |
5252 | snapshot->snapshot_time = mach_absolute_time(); | |
5253 | snapshot->notification_time = 0; | |
5254 | snapshot->js_gencount = 0; | |
5255 | } | |
5256 | ||
6d2010ae | 5257 | static void |
cb323159 | 5258 | memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count ) |
6d2010ae | 5259 | { |
cb323159 A |
5260 | proc_t p, next_p; |
5261 | unsigned int b = 0, i = 0; | |
d9a64523 | 5262 | |
cb323159 A |
5263 | memorystatus_jetsam_snapshot_t *snapshot = NULL; |
5264 | memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL; | |
5265 | unsigned int snapshot_max = 0; | |
5266 | ||
5267 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
d9a64523 | 5268 | |
cb323159 | 5269 | if (od_snapshot) { |
d9a64523 | 5270 | /* |
cb323159 | 5271 | * This is an on_demand snapshot |
d9a64523 | 5272 | */ |
cb323159 A |
5273 | snapshot = od_snapshot; |
5274 | snapshot_list = od_snapshot->entries; | |
5275 | snapshot_max = ods_list_count; | |
5276 | } else { | |
5277 | /* | |
5278 | * This is a jetsam event snapshot | |
5279 | */ | |
5280 | snapshot = memorystatus_jetsam_snapshot; | |
5281 | snapshot_list = memorystatus_jetsam_snapshot->entries; | |
5282 | snapshot_max = memorystatus_jetsam_snapshot_max; | |
d9a64523 A |
5283 | } |
5284 | ||
f427ee49 | 5285 | memorystatus_init_jetsam_snapshot_header(snapshot); |
cb323159 A |
5286 | |
5287 | next_p = memorystatus_get_first_proc_locked(&b, TRUE); | |
d9a64523 | 5288 | while (next_p) { |
d9a64523 | 5289 | p = next_p; |
cb323159 | 5290 | next_p = memorystatus_get_next_proc_locked(&b, p, TRUE); |
d9a64523 | 5291 | |
cb323159 | 5292 | if (FALSE == memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i], snapshot->js_gencount)) { |
d9a64523 | 5293 | continue; |
6d2010ae | 5294 | } |
316670eb | 5295 | |
cb323159 A |
5296 | 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", |
5297 | p->p_pid, | |
5298 | 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], | |
5299 | 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]); | |
d9a64523 | 5300 | |
cb323159 | 5301 | if (++i == snapshot_max) { |
d9a64523 A |
5302 | break; |
5303 | } | |
5304 | } | |
5305 | ||
cb323159 A |
5306 | snapshot->entry_count = i; |
5307 | ||
5308 | if (!od_snapshot) { | |
5309 | /* update the system buffer count */ | |
5310 | memorystatus_jetsam_snapshot_count = i; | |
5311 | } | |
6d2010ae A |
5312 | } |
5313 | ||
cb323159 | 5314 | #if DEVELOPMENT || DEBUG |
d9a64523 | 5315 | |
cb323159 A |
5316 | #if CONFIG_JETSAM |
5317 | static int | |
f427ee49 | 5318 | memorystatus_cmd_set_panic_bits(user_addr_t buffer, size_t buffer_size) |
6d2010ae | 5319 | { |
cb323159 A |
5320 | int ret; |
5321 | memorystatus_jetsam_panic_options_t debug; | |
d9a64523 | 5322 | |
cb323159 A |
5323 | if (buffer_size != sizeof(memorystatus_jetsam_panic_options_t)) { |
5324 | return EINVAL; | |
5325 | } | |
6d2010ae | 5326 | |
cb323159 A |
5327 | ret = copyin(buffer, &debug, buffer_size); |
5328 | if (ret) { | |
5329 | return ret; | |
d9a64523 | 5330 | } |
6d2010ae | 5331 | |
cb323159 A |
5332 | /* Panic bits match kMemorystatusKilled* enum */ |
5333 | memorystatus_jetsam_panic_debug = (memorystatus_jetsam_panic_debug & ~debug.mask) | (debug.data & debug.mask); | |
5334 | ||
5335 | /* Copyout new value */ | |
5336 | debug.data = memorystatus_jetsam_panic_debug; | |
5337 | ret = copyout(&debug, buffer, sizeof(memorystatus_jetsam_panic_options_t)); | |
5338 | ||
5339 | return ret; | |
5340 | } | |
5341 | #endif /* CONFIG_JETSAM */ | |
6d2010ae | 5342 | |
f427ee49 A |
5343 | /* |
5344 | * Verify that the given bucket has been sorted correctly. | |
5345 | * | |
5346 | * Walks through the bucket and verifies that all pids in the | |
5347 | * expected_order buffer are in that bucket and in the same | |
5348 | * relative order. | |
5349 | * | |
5350 | * The proc_list_lock must be held by the caller. | |
5351 | */ | |
5352 | static int | |
5353 | memorystatus_verify_sort_order(unsigned int bucket_index, pid_t *expected_order, size_t num_pids) | |
5354 | { | |
5355 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
5356 | ||
5357 | int error = 0; | |
5358 | proc_t p = NULL; | |
5359 | size_t i = 0; | |
5360 | ||
5361 | /* | |
5362 | * NB: We allow other procs to be mixed in within the expected ones. | |
5363 | * We just need the expected procs to be in the right order relative to each other. | |
5364 | */ | |
5365 | p = memorystatus_get_first_proc_locked(&bucket_index, FALSE); | |
5366 | while (p) { | |
5367 | if (p->p_pid == expected_order[i]) { | |
5368 | i++; | |
5369 | } | |
5370 | if (i == num_pids) { | |
5371 | break; | |
5372 | } | |
5373 | p = memorystatus_get_next_proc_locked(&bucket_index, p, FALSE); | |
5374 | } | |
5375 | if (i != num_pids) { | |
5376 | char buffer[128]; | |
5377 | size_t len = sizeof(buffer); | |
5378 | size_t buffer_idx = 0; | |
5379 | os_log_error(OS_LOG_DEFAULT, "memorystatus_verify_sort_order: Processes in bucket %d were not sorted properly\n", bucket_index); | |
5380 | for (i = 0; i < num_pids; i++) { | |
5381 | int num_written = snprintf(buffer + buffer_idx, len - buffer_idx, "%d,", expected_order[i]); | |
5382 | if (num_written <= 0) { | |
5383 | break; | |
5384 | } | |
5385 | if (buffer_idx + (unsigned int) num_written >= len) { | |
5386 | break; | |
5387 | } | |
5388 | buffer_idx += num_written; | |
5389 | } | |
5390 | os_log_error(OS_LOG_DEFAULT, "memorystatus_verify_sort_order: Expected order [%s]", buffer); | |
5391 | memset(buffer, 0, len); | |
5392 | buffer_idx = 0; | |
5393 | p = memorystatus_get_first_proc_locked(&bucket_index, FALSE); | |
5394 | i = 0; | |
5395 | os_log_error(OS_LOG_DEFAULT, "memorystatus_verify_sort_order: Actual order:"); | |
5396 | while (p) { | |
5397 | int num_written; | |
5398 | if (buffer_idx == 0) { | |
5399 | num_written = snprintf(buffer + buffer_idx, len - buffer_idx, "%zu: %d,", i, p->p_pid); | |
5400 | } else { | |
5401 | num_written = snprintf(buffer + buffer_idx, len - buffer_idx, "%d,", p->p_pid); | |
5402 | } | |
5403 | if (num_written <= 0) { | |
5404 | break; | |
5405 | } | |
5406 | buffer_idx += (unsigned int) num_written; | |
5407 | assert(buffer_idx <= len); | |
5408 | if (i % 10 == 0) { | |
5409 | os_log_error(OS_LOG_DEFAULT, "memorystatus_verify_sort_order: %s", buffer); | |
5410 | buffer_idx = 0; | |
5411 | } | |
5412 | p = memorystatus_get_next_proc_locked(&bucket_index, p, FALSE); | |
5413 | i++; | |
5414 | } | |
5415 | if (buffer_idx != 0) { | |
5416 | os_log_error(OS_LOG_DEFAULT, "memorystatus_verify_sort_order: %s", buffer); | |
5417 | } | |
5418 | error = EINVAL; | |
5419 | } | |
5420 | return error; | |
5421 | } | |
5422 | ||
cb323159 A |
5423 | /* |
5424 | * Triggers a sort_order on a specified jetsam priority band. | |
5425 | * This is for testing only, used to force a path through the sort | |
5426 | * function. | |
5427 | */ | |
5428 | static int | |
f427ee49 A |
5429 | memorystatus_cmd_test_jetsam_sort(int priority, |
5430 | int sort_order, | |
5431 | user_addr_t expected_order_user, | |
5432 | size_t expected_order_user_len) | |
cb323159 A |
5433 | { |
5434 | int error = 0; | |
cb323159 | 5435 | unsigned int bucket_index = 0; |
f427ee49 A |
5436 | static size_t kMaxPids = 8; |
5437 | pid_t expected_order[kMaxPids]; | |
5438 | size_t copy_size = sizeof(expected_order); | |
5439 | size_t num_pids; | |
5440 | ||
5441 | if (expected_order_user_len < copy_size) { | |
5442 | copy_size = expected_order_user_len; | |
5443 | } | |
5444 | num_pids = copy_size / sizeof(pid_t); | |
5445 | ||
5446 | error = copyin(expected_order_user, expected_order, copy_size); | |
5447 | if (error != 0) { | |
5448 | return error; | |
5449 | } | |
d9a64523 | 5450 | |
cb323159 A |
5451 | if (priority == -1) { |
5452 | /* Use as shorthand for default priority */ | |
5453 | bucket_index = JETSAM_PRIORITY_DEFAULT; | |
5454 | } else { | |
5455 | bucket_index = (unsigned int)priority; | |
d9a64523 A |
5456 | } |
5457 | ||
f427ee49 A |
5458 | /* |
5459 | * Acquire lock before sorting so we can check the sort order | |
5460 | * while still holding the lock. | |
5461 | */ | |
5462 | proc_list_lock(); | |
5463 | ||
5464 | memorystatus_sort_bucket_locked(bucket_index, sort_order); | |
5465 | ||
5466 | if (expected_order_user != CAST_USER_ADDR_T(NULL) && expected_order_user_len > 0) { | |
5467 | error = memorystatus_verify_sort_order(bucket_index, expected_order, num_pids); | |
5468 | } | |
5469 | ||
5470 | proc_list_unlock(); | |
d9a64523 | 5471 | |
cb323159 A |
5472 | return error; |
5473 | } | |
d9a64523 | 5474 | |
cb323159 | 5475 | #endif /* DEVELOPMENT || DEBUG */ |
d9a64523 | 5476 | |
cb323159 A |
5477 | /* |
5478 | * Prepare the process to be killed (set state, update snapshot) and kill it. | |
5479 | */ | |
5480 | static uint64_t memorystatus_purge_before_jetsam_success = 0; | |
d9a64523 | 5481 | |
cb323159 A |
5482 | static boolean_t |
5483 | memorystatus_kill_proc(proc_t p, uint32_t cause, os_reason_t jetsam_reason, boolean_t *killed, uint64_t *footprint_of_killed_proc) | |
5484 | { | |
5485 | pid_t aPid = 0; | |
5486 | uint32_t aPid_ep = 0; | |
d9a64523 | 5487 | |
cb323159 A |
5488 | uint64_t killtime = 0; |
5489 | clock_sec_t tv_sec; | |
5490 | clock_usec_t tv_usec; | |
5491 | uint32_t tv_msec; | |
5492 | boolean_t retval = FALSE; | |
d9a64523 | 5493 | |
cb323159 A |
5494 | aPid = p->p_pid; |
5495 | aPid_ep = p->p_memstat_effectivepriority; | |
d9a64523 | 5496 | |
cb323159 | 5497 | if (cause != kMemorystatusKilledVnodes && cause != kMemorystatusKilledZoneMapExhaustion) { |
d9a64523 | 5498 | /* |
cb323159 | 5499 | * Genuine memory pressure and not other (vnode/zone) resource exhaustion. |
0a7de745 | 5500 | */ |
cb323159 A |
5501 | boolean_t success = FALSE; |
5502 | uint64_t num_pages_purged; | |
5503 | uint64_t num_pages_reclaimed = 0; | |
5504 | uint64_t num_pages_unsecluded = 0; | |
d9a64523 | 5505 | |
cb323159 A |
5506 | networking_memstatus_callout(p, cause); |
5507 | num_pages_purged = vm_purgeable_purge_task_owned(p->task); | |
5508 | num_pages_reclaimed += num_pages_purged; | |
5509 | #if CONFIG_SECLUDED_MEMORY | |
5510 | if (cause == kMemorystatusKilledVMPageShortage && | |
5511 | vm_page_secluded_count > 0 && | |
5512 | task_can_use_secluded_mem(p->task, FALSE)) { | |
d9a64523 | 5513 | /* |
cb323159 A |
5514 | * We're about to kill a process that has access |
5515 | * to the secluded pool. Drain that pool into the | |
5516 | * free or active queues to make these pages re-appear | |
5517 | * as "available", which might make us no longer need | |
5518 | * to kill that process. | |
5519 | * Since the secluded pool does not get refilled while | |
5520 | * a process has access to it, it should remain | |
5521 | * drained. | |
d9a64523 | 5522 | */ |
cb323159 A |
5523 | num_pages_unsecluded = vm_page_secluded_drain(); |
5524 | num_pages_reclaimed += num_pages_unsecluded; | |
d9a64523 | 5525 | } |
cb323159 | 5526 | #endif /* CONFIG_SECLUDED_MEMORY */ |
d9a64523 | 5527 | |
cb323159 A |
5528 | if (num_pages_reclaimed) { |
5529 | /* | |
5530 | * We actually reclaimed something and so let's | |
5531 | * check if we need to continue with the kill. | |
5532 | */ | |
5533 | if (cause == kMemorystatusKilledHiwat) { | |
5534 | uint64_t footprint_in_bytes = get_task_phys_footprint(p->task); | |
5535 | uint64_t memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ | |
5536 | success = (footprint_in_bytes <= memlimit_in_bytes); | |
d9a64523 | 5537 | } else { |
cb323159 A |
5538 | success = (memorystatus_avail_pages_below_pressure() == FALSE); |
5539 | #if CONFIG_SECLUDED_MEMORY | |
5540 | if (!success && num_pages_unsecluded) { | |
d9a64523 | 5541 | /* |
cb323159 A |
5542 | * We just drained the secluded pool |
5543 | * because we're about to kill a | |
5544 | * process that has access to it. | |
5545 | * This is an important process and | |
5546 | * we'd rather not kill it unless | |
5547 | * absolutely necessary, so declare | |
5548 | * success even if draining the pool | |
5549 | * did not quite get us out of the | |
5550 | * "pressure" level but still got | |
5551 | * us out of the "critical" level. | |
d9a64523 | 5552 | */ |
cb323159 A |
5553 | success = (memorystatus_avail_pages_below_critical() == FALSE); |
5554 | } | |
5555 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
5556 | } | |
d9a64523 | 5557 | |
cb323159 A |
5558 | if (success) { |
5559 | memorystatus_purge_before_jetsam_success++; | |
d9a64523 | 5560 | |
cb323159 A |
5561 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: reclaimed %llu pages (%llu purged, %llu unsecluded) from pid %d [%s] and avoided %s\n", |
5562 | num_pages_reclaimed, num_pages_purged, num_pages_unsecluded, aPid, ((p && *p->p_name) ? p->p_name : "unknown"), memorystatus_kill_cause_name[cause]); | |
d9a64523 | 5563 | |
cb323159 | 5564 | *killed = FALSE; |
d9a64523 | 5565 | |
cb323159 | 5566 | return TRUE; |
d9a64523 A |
5567 | } |
5568 | } | |
5569 | } | |
5570 | ||
cb323159 A |
5571 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
5572 | MEMORYSTATUS_DEBUG(1, "jetsam: killing pid %d [%s] - %lld Mb > 1 (%d Mb)\n", | |
5573 | aPid, (*p->p_name ? p->p_name : "unknown"), | |
5574 | (footprint_in_bytes / (1024ULL * 1024ULL)), /* converted bytes to MB */ | |
5575 | p->p_memstat_memlimit); | |
5576 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ | |
6d2010ae | 5577 | |
cb323159 A |
5578 | killtime = mach_absolute_time(); |
5579 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
5580 | tv_msec = tv_usec / 1000; | |
3e170ce0 | 5581 | |
cb323159 A |
5582 | proc_list_lock(); |
5583 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); | |
5584 | proc_list_unlock(); | |
d9a64523 | 5585 | |
cb323159 | 5586 | char kill_reason_string[128]; |
d9a64523 | 5587 | |
cb323159 A |
5588 | if (cause == kMemorystatusKilledHiwat) { |
5589 | strlcpy(kill_reason_string, "killing_highwater_process", 128); | |
5590 | } else { | |
5591 | if (aPid_ep == JETSAM_PRIORITY_IDLE) { | |
5592 | strlcpy(kill_reason_string, "killing_idle_process", 128); | |
5593 | } else { | |
5594 | strlcpy(kill_reason_string, "killing_top_process", 128); | |
316670eb A |
5595 | } |
5596 | } | |
d9a64523 A |
5597 | |
5598 | /* | |
cb323159 A |
5599 | * memorystatus_do_kill drops a reference, so take another one so we can |
5600 | * continue to use this exit reason even after memorystatus_do_kill() | |
5601 | * returns | |
d9a64523 | 5602 | */ |
cb323159 | 5603 | os_reason_ref(jetsam_reason); |
6d2010ae | 5604 | |
cb323159 A |
5605 | retval = memorystatus_do_kill(p, cause, jetsam_reason, footprint_of_killed_proc); |
5606 | *killed = retval; | |
d9a64523 | 5607 | |
cb323159 A |
5608 | os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: %s pid %d [%s] (%s %d) %lluKB - memorystatus_available_pages: %llu", |
5609 | (unsigned long)tv_sec, tv_msec, kill_reason_string, | |
5610 | aPid, ((p && *p->p_name) ? p->p_name : "unknown"), | |
5611 | memorystatus_kill_cause_name[cause], aPid_ep, | |
f427ee49 | 5612 | (*footprint_of_killed_proc) >> 10, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
cb323159 A |
5613 | |
5614 | return retval; | |
316670eb A |
5615 | } |
5616 | ||
cb323159 A |
5617 | /* |
5618 | * Jetsam the first process in the queue. | |
5619 | */ | |
d9a64523 | 5620 | static boolean_t |
cb323159 A |
5621 | memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, os_reason_t jetsam_reason, |
5622 | int32_t *priority, uint32_t *errors, uint64_t *memory_reclaimed) | |
d9a64523 | 5623 | { |
cb323159 A |
5624 | pid_t aPid; |
5625 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
5626 | boolean_t new_snapshot = FALSE, force_new_snapshot = FALSE, killed = FALSE, freed_mem = FALSE; | |
5627 | unsigned int i = 0; | |
5628 | uint32_t aPid_ep; | |
5629 | int32_t local_max_kill_prio = JETSAM_PRIORITY_IDLE; | |
5630 | uint64_t footprint_of_killed_proc = 0; | |
d9a64523 | 5631 | |
cb323159 A |
5632 | #ifndef CONFIG_FREEZE |
5633 | #pragma unused(any) | |
5634 | #endif | |
d9a64523 | 5635 | |
cb323159 | 5636 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, |
f427ee49 | 5637 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, 0, 0, 0, 0); |
d9a64523 | 5638 | |
d9a64523 | 5639 | |
cb323159 A |
5640 | #if CONFIG_JETSAM |
5641 | if (sort_flag == TRUE) { | |
5642 | (void)memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT); | |
d9a64523 A |
5643 | } |
5644 | ||
cb323159 | 5645 | local_max_kill_prio = max_kill_priority; |
d9a64523 | 5646 | |
cb323159 | 5647 | force_new_snapshot = FALSE; |
d9a64523 | 5648 | |
cb323159 | 5649 | #else /* CONFIG_JETSAM */ |
d190cdc3 | 5650 | |
cb323159 A |
5651 | if (sort_flag == TRUE) { |
5652 | (void)memorystatus_sort_bucket(JETSAM_PRIORITY_IDLE, JETSAM_SORT_DEFAULT); | |
d190cdc3 A |
5653 | } |
5654 | ||
cb323159 A |
5655 | /* |
5656 | * On macos, we currently only have 2 reasons to be here: | |
5657 | * | |
5658 | * kMemorystatusKilledZoneMapExhaustion | |
5659 | * AND | |
5660 | * kMemorystatusKilledVMCompressorSpaceShortage | |
5661 | * | |
5662 | * If we are here because of kMemorystatusKilledZoneMapExhaustion, we will consider | |
5663 | * any and all processes as eligible kill candidates since we need to avoid a panic. | |
5664 | * | |
5665 | * Since this function can be called async. it is harder to toggle the max_kill_priority | |
5666 | * value before and after a call. And so we use this local variable to set the upper band | |
5667 | * on the eligible kill bands. | |
5668 | */ | |
5669 | if (cause == kMemorystatusKilledZoneMapExhaustion) { | |
5670 | local_max_kill_prio = JETSAM_PRIORITY_MAX; | |
5671 | } else { | |
5672 | local_max_kill_prio = max_kill_priority; | |
d190cdc3 A |
5673 | } |
5674 | ||
cb323159 A |
5675 | /* |
5676 | * And, because we are here under extreme circumstances, we force a snapshot even for | |
5677 | * IDLE kills. | |
5678 | */ | |
5679 | force_new_snapshot = TRUE; | |
fe8ab488 | 5680 | |
cb323159 | 5681 | #endif /* CONFIG_JETSAM */ |
fe8ab488 | 5682 | |
cb323159 A |
5683 | if (cause != kMemorystatusKilledZoneMapExhaustion && |
5684 | jetsam_current_thread() != NULL && | |
5685 | jetsam_current_thread()->limit_to_low_bands && | |
5686 | local_max_kill_prio > JETSAM_PRIORITY_BACKGROUND) { | |
5687 | local_max_kill_prio = JETSAM_PRIORITY_BACKGROUND; | |
316670eb | 5688 | } |
0a7de745 | 5689 | |
cb323159 | 5690 | proc_list_lock(); |
316670eb | 5691 | |
cb323159 A |
5692 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
5693 | while (next_p && (next_p->p_memstat_effectivepriority <= local_max_kill_prio)) { | |
5694 | p = next_p; | |
5695 | next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
316670eb | 5696 | |
39236c6e | 5697 | |
cb323159 A |
5698 | aPid = p->p_pid; |
5699 | aPid_ep = p->p_memstat_effectivepriority; | |
3e170ce0 | 5700 | |
cb323159 A |
5701 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { |
5702 | continue; /* with lock held */ | |
5703 | } | |
3e170ce0 | 5704 | |
cb323159 | 5705 | if (cause == kMemorystatusKilledVnodes) { |
3e170ce0 | 5706 | /* |
cb323159 A |
5707 | * If the system runs out of vnodes, we systematically jetsam |
5708 | * processes in hopes of stumbling onto a vnode gain that helps | |
5709 | * the system recover. The process that happens to trigger | |
5710 | * this path has no known relationship to the vnode shortage. | |
5711 | * Deadlock avoidance: attempt to safeguard the caller. | |
3e170ce0 | 5712 | */ |
39037602 | 5713 | |
cb323159 A |
5714 | if (p == current_proc()) { |
5715 | /* do not jetsam the current process */ | |
5716 | continue; | |
5717 | } | |
5718 | } | |
5719 | ||
5720 | #if CONFIG_FREEZE | |
5721 | boolean_t skip; | |
5722 | boolean_t reclaim_proc = !(p->p_memstat_state & P_MEMSTAT_LOCKED); | |
5723 | if (any || reclaim_proc) { | |
5724 | skip = FALSE; | |
5725 | } else { | |
5726 | skip = TRUE; | |
5727 | } | |
5728 | ||
5729 | if (skip) { | |
5730 | continue; | |
5731 | } else | |
5732 | #endif | |
5733 | { | |
5734 | if (proc_ref_locked(p) == p) { | |
5ba3f43e | 5735 | /* |
cb323159 A |
5736 | * Mark as terminated so that if exit1() indicates success, but the process (for example) |
5737 | * is blocked in task_exception_notify(), it'll be skipped if encountered again - see | |
5738 | * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the | |
5739 | * acquisition of the proc lock. | |
5ba3f43e | 5740 | */ |
cb323159 | 5741 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; |
5ba3f43e A |
5742 | } else { |
5743 | /* | |
cb323159 A |
5744 | * We need to restart the search again because |
5745 | * proc_ref_locked _can_ drop the proc_list lock | |
5746 | * and we could have lost our stored next_p via | |
5747 | * an exit() on another core. | |
5ba3f43e | 5748 | */ |
cb323159 A |
5749 | i = 0; |
5750 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
5751 | continue; | |
5ba3f43e | 5752 | } |
39037602 | 5753 | |
cb323159 A |
5754 | /* |
5755 | * Capture a snapshot if none exists and: | |
5756 | * - we are forcing a new snapshot creation, either because: | |
5757 | * - on a particular platform we need these snapshots every time, OR | |
5758 | * - a boot-arg/embedded device tree property has been set. | |
5759 | * - priority was not requested (this is something other than an ambient kill) | |
5760 | * - the priority was requested *and* the targeted process is not at idle priority | |
5761 | */ | |
5762 | if ((memorystatus_jetsam_snapshot_count == 0) && | |
5763 | (force_new_snapshot || memorystatus_idle_snapshot || ((!priority) || (priority && (aPid_ep != JETSAM_PRIORITY_IDLE))))) { | |
5764 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); | |
5765 | new_snapshot = TRUE; | |
5766 | } | |
813fb2f6 | 5767 | |
cb323159 | 5768 | proc_list_unlock(); |
813fb2f6 | 5769 | |
cb323159 A |
5770 | freed_mem = memorystatus_kill_proc(p, cause, jetsam_reason, &killed, &footprint_of_killed_proc); /* purged and/or killed 'p' */ |
5771 | /* Success? */ | |
5772 | if (freed_mem) { | |
5773 | if (killed) { | |
5774 | *memory_reclaimed = footprint_of_killed_proc; | |
5775 | if (priority) { | |
5776 | *priority = aPid_ep; | |
813fb2f6 | 5777 | } |
cb323159 A |
5778 | } else { |
5779 | /* purged */ | |
5780 | proc_list_lock(); | |
5781 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
5782 | proc_list_unlock(); | |
3e170ce0 | 5783 | } |
cb323159 A |
5784 | proc_rele(p); |
5785 | goto exit; | |
3e170ce0 | 5786 | } |
3e170ce0 | 5787 | |
cb323159 A |
5788 | /* |
5789 | * Failure - first unwind the state, | |
5790 | * then fall through to restart the search. | |
5791 | */ | |
5792 | proc_list_lock(); | |
5793 | proc_rele_locked(p); | |
5794 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
5795 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
5796 | *errors += 1; | |
5797 | ||
5798 | i = 0; | |
5799 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
813fb2f6 | 5800 | } |
6d2010ae | 5801 | } |
3e170ce0 | 5802 | |
cb323159 | 5803 | proc_list_unlock(); |
3e170ce0 | 5804 | |
cb323159 A |
5805 | exit: |
5806 | os_reason_free(jetsam_reason); | |
3e170ce0 | 5807 | |
cb323159 A |
5808 | if (!killed) { |
5809 | *memory_reclaimed = 0; | |
6d2010ae | 5810 | |
cb323159 A |
5811 | /* Clear snapshot if freshly captured and no target was found */ |
5812 | if (new_snapshot) { | |
5813 | proc_list_lock(); | |
5814 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
5815 | proc_list_unlock(); | |
fe8ab488 A |
5816 | } |
5817 | } | |
3e170ce0 | 5818 | |
cb323159 | 5819 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, |
f427ee49 | 5820 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, killed ? aPid : 0, killed, *memory_reclaimed, 0); |
cb323159 A |
5821 | |
5822 | return killed; | |
fe8ab488 A |
5823 | } |
5824 | ||
cb323159 A |
5825 | /* |
5826 | * Jetsam aggressively | |
5827 | */ | |
5828 | static boolean_t | |
5829 | memorystatus_kill_processes_aggressive(uint32_t cause, int aggr_count, | |
5830 | int32_t priority_max, uint32_t *errors, uint64_t *memory_reclaimed) | |
0a7de745 | 5831 | { |
cb323159 A |
5832 | pid_t aPid; |
5833 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
5834 | boolean_t new_snapshot = FALSE, killed = FALSE; | |
5835 | int kill_count = 0; | |
5836 | unsigned int i = 0; | |
5837 | int32_t aPid_ep = 0; | |
5838 | unsigned int memorystatus_level_snapshot = 0; | |
5839 | uint64_t killtime = 0; | |
5840 | clock_sec_t tv_sec; | |
5841 | clock_usec_t tv_usec; | |
5842 | uint32_t tv_msec; | |
5843 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
5844 | uint64_t footprint_of_killed_proc = 0; | |
0a7de745 | 5845 | |
cb323159 | 5846 | *memory_reclaimed = 0; |
0a7de745 | 5847 | |
cb323159 | 5848 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, |
f427ee49 | 5849 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, priority_max, 0, 0, 0); |
d9a64523 | 5850 | |
cb323159 | 5851 | if (priority_max >= JETSAM_PRIORITY_FOREGROUND) { |
d9a64523 | 5852 | /* |
cb323159 A |
5853 | * Check if aggressive jetsam has been asked to kill upto or beyond the |
5854 | * JETSAM_PRIORITY_FOREGROUND bucket. If yes, sort the FG band based on | |
5855 | * coalition footprint. | |
d9a64523 | 5856 | */ |
cb323159 | 5857 | memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT); |
d9a64523 A |
5858 | } |
5859 | ||
cb323159 A |
5860 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, cause); |
5861 | if (jetsam_reason == OS_REASON_NULL) { | |
5862 | printf("memorystatus_kill_processes_aggressive: failed to allocate exit reason\n"); | |
39037602 | 5863 | } |
39236c6e | 5864 | |
cb323159 | 5865 | proc_list_lock(); |
39236c6e | 5866 | |
cb323159 A |
5867 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
5868 | while (next_p) { | |
5869 | if (((next_p->p_listflag & P_LIST_EXITED) != 0) || | |
5870 | ((unsigned int)(next_p->p_memstat_effectivepriority) != i)) { | |
5871 | /* | |
5872 | * We have raced with next_p running on another core. | |
5873 | * It may be exiting or it may have moved to a different | |
5874 | * jetsam priority band. This means we have lost our | |
5875 | * place in line while traversing the jetsam list. We | |
5876 | * attempt to recover by rewinding to the beginning of the band | |
5877 | * we were already traversing. By doing this, we do not guarantee | |
5878 | * that no process escapes this aggressive march, but we can make | |
5879 | * skipping an entire range of processes less likely. (PR-21069019) | |
5880 | */ | |
39236c6e | 5881 | |
cb323159 A |
5882 | MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: rewinding band %d, %s(%d) moved or exiting.\n", |
5883 | aggr_count, i, (*next_p->p_name ? next_p->p_name : "unknown"), next_p->p_pid); | |
39236c6e | 5884 | |
cb323159 A |
5885 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
5886 | continue; | |
39037602 A |
5887 | } |
5888 | ||
cb323159 A |
5889 | p = next_p; |
5890 | next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
39037602 | 5891 | |
cb323159 A |
5892 | if (p->p_memstat_effectivepriority > priority_max) { |
5893 | /* | |
5894 | * Bail out of this killing spree if we have | |
5895 | * reached beyond the priority_max jetsam band. | |
5896 | * That is, we kill up to and through the | |
5897 | * priority_max jetsam band. | |
5898 | */ | |
39037602 | 5899 | proc_list_unlock(); |
cb323159 | 5900 | goto exit; |
39037602 | 5901 | } |
39037602 | 5902 | |
cb323159 A |
5903 | aPid = p->p_pid; |
5904 | aPid_ep = p->p_memstat_effectivepriority; | |
39037602 | 5905 | |
cb323159 A |
5906 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { |
5907 | continue; | |
5908 | } | |
39037602 | 5909 | |
39037602 | 5910 | /* |
cb323159 | 5911 | * Capture a snapshot if none exists. |
39037602 | 5912 | */ |
cb323159 A |
5913 | if (memorystatus_jetsam_snapshot_count == 0) { |
5914 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); | |
5915 | new_snapshot = TRUE; | |
39037602 A |
5916 | } |
5917 | ||
39037602 | 5918 | /* |
cb323159 A |
5919 | * Mark as terminated so that if exit1() indicates success, but the process (for example) |
5920 | * is blocked in task_exception_notify(), it'll be skipped if encountered again - see | |
5921 | * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the | |
5922 | * acquisition of the proc lock. | |
39037602 | 5923 | */ |
cb323159 | 5924 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; |
39037602 | 5925 | |
cb323159 A |
5926 | killtime = mach_absolute_time(); |
5927 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
5928 | tv_msec = tv_usec / 1000; | |
39037602 | 5929 | |
cb323159 A |
5930 | /* Shift queue, update stats */ |
5931 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); | |
39037602 | 5932 | |
cb323159 A |
5933 | /* |
5934 | * In order to kill the target process, we will drop the proc_list_lock. | |
5935 | * To guaranteee that p and next_p don't disappear out from under the lock, | |
5936 | * we must take a ref on both. | |
5937 | * If we cannot get a reference, then it's likely we've raced with | |
5938 | * that process exiting on another core. | |
5939 | */ | |
5940 | if (proc_ref_locked(p) == p) { | |
5941 | if (next_p) { | |
5942 | while (next_p && (proc_ref_locked(next_p) != next_p)) { | |
5943 | proc_t temp_p; | |
39037602 | 5944 | |
cb323159 A |
5945 | /* |
5946 | * We must have raced with next_p exiting on another core. | |
5947 | * Recover by getting the next eligible process in the band. | |
5948 | */ | |
39037602 | 5949 | |
cb323159 A |
5950 | MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: skipping %d [%s] (exiting?)\n", |
5951 | aggr_count, next_p->p_pid, (*next_p->p_name ? next_p->p_name : "(unknown)")); | |
39037602 | 5952 | |
cb323159 A |
5953 | temp_p = next_p; |
5954 | next_p = memorystatus_get_next_proc_locked(&i, temp_p, TRUE); | |
5955 | } | |
5956 | } | |
5957 | proc_list_unlock(); | |
39037602 | 5958 | |
cb323159 A |
5959 | printf("%lu.%03d memorystatus: %s%d pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", |
5960 | (unsigned long)tv_sec, tv_msec, | |
5961 | ((aPid_ep == JETSAM_PRIORITY_IDLE) ? "killing_idle_process_aggressive" : "killing_top_process_aggressive"), | |
5962 | aggr_count, aPid, (*p->p_name ? p->p_name : "unknown"), | |
f427ee49 | 5963 | memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
39037602 | 5964 | |
cb323159 | 5965 | memorystatus_level_snapshot = memorystatus_level; |
39037602 | 5966 | |
cb323159 A |
5967 | /* |
5968 | * memorystatus_do_kill() drops a reference, so take another one so we can | |
5969 | * continue to use this exit reason even after memorystatus_do_kill() | |
5970 | * returns. | |
5971 | */ | |
5972 | os_reason_ref(jetsam_reason); | |
5973 | killed = memorystatus_do_kill(p, cause, jetsam_reason, &footprint_of_killed_proc); | |
39037602 | 5974 | |
cb323159 A |
5975 | /* Success? */ |
5976 | if (killed) { | |
5977 | *memory_reclaimed += footprint_of_killed_proc; | |
5978 | proc_rele(p); | |
5979 | kill_count++; | |
5980 | p = NULL; | |
5981 | killed = FALSE; | |
39037602 | 5982 | |
39037602 | 5983 | /* |
cb323159 | 5984 | * Continue the killing spree. |
39037602 | 5985 | */ |
cb323159 A |
5986 | proc_list_lock(); |
5987 | if (next_p) { | |
5988 | proc_rele_locked(next_p); | |
5989 | } | |
5990 | ||
5991 | if (aPid_ep == JETSAM_PRIORITY_FOREGROUND && memorystatus_aggressive_jetsam_lenient == TRUE) { | |
5992 | if (memorystatus_level > memorystatus_level_snapshot && ((memorystatus_level - memorystatus_level_snapshot) >= AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD)) { | |
5993 | #if DEVELOPMENT || DEBUG | |
5994 | printf("Disabling Lenient mode after one-time deployment.\n"); | |
5995 | #endif /* DEVELOPMENT || DEBUG */ | |
5996 | memorystatus_aggressive_jetsam_lenient = FALSE; | |
5997 | break; | |
39037602 A |
5998 | } |
5999 | } | |
cb323159 | 6000 | |
39037602 A |
6001 | continue; |
6002 | } | |
cb323159 | 6003 | |
39037602 | 6004 | /* |
cb323159 A |
6005 | * Failure - first unwind the state, |
6006 | * then fall through to restart the search. | |
39037602 | 6007 | */ |
cb323159 A |
6008 | proc_list_lock(); |
6009 | proc_rele_locked(p); | |
6010 | if (next_p) { | |
6011 | proc_rele_locked(next_p); | |
39037602 | 6012 | } |
cb323159 A |
6013 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; |
6014 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
6015 | *errors += 1; | |
6016 | p = NULL; | |
39037602 A |
6017 | } |
6018 | ||
6019 | /* | |
cb323159 A |
6020 | * Failure - restart the search at the beginning of |
6021 | * the band we were already traversing. | |
6022 | * | |
6023 | * We might have raced with "p" exiting on another core, resulting in no | |
6024 | * ref on "p". Or, we may have failed to kill "p". | |
6025 | * | |
6026 | * Either way, we fall thru to here, leaving the proc in the | |
6027 | * P_MEMSTAT_TERMINATED or P_MEMSTAT_ERROR state. | |
6028 | * | |
6029 | * And, we hold the the proc_list_lock at this point. | |
0a7de745 | 6030 | */ |
39037602 | 6031 | |
cb323159 A |
6032 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
6033 | } | |
39236c6e | 6034 | |
cb323159 | 6035 | proc_list_unlock(); |
39236c6e | 6036 | |
cb323159 A |
6037 | exit: |
6038 | os_reason_free(jetsam_reason); | |
39037602 | 6039 | |
cb323159 A |
6040 | /* Clear snapshot if freshly captured and no target was found */ |
6041 | if (new_snapshot && (kill_count == 0)) { | |
6042 | proc_list_lock(); | |
6043 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
6044 | proc_list_unlock(); | |
0a7de745 | 6045 | } |
39037602 | 6046 | |
cb323159 | 6047 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, |
f427ee49 | 6048 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, 0, kill_count, *memory_reclaimed, 0); |
39236c6e | 6049 | |
cb323159 A |
6050 | if (kill_count > 0) { |
6051 | return TRUE; | |
6052 | } else { | |
6053 | return FALSE; | |
6054 | } | |
39236c6e A |
6055 | } |
6056 | ||
cb323159 A |
6057 | static boolean_t |
6058 | memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged, uint64_t *memory_reclaimed) | |
6059 | { | |
6060 | pid_t aPid = 0; | |
6061 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
6062 | boolean_t new_snapshot = FALSE, killed = FALSE, freed_mem = FALSE; | |
6063 | unsigned int i = 0; | |
6064 | uint32_t aPid_ep; | |
6065 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
6066 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_START, | |
f427ee49 | 6067 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, 0, 0, 0, 0); |
39037602 | 6068 | |
cb323159 A |
6069 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_HIGHWATER); |
6070 | if (jetsam_reason == OS_REASON_NULL) { | |
6071 | printf("memorystatus_kill_hiwat_proc: failed to allocate exit reason\n"); | |
6072 | } | |
39236c6e | 6073 | |
cb323159 | 6074 | proc_list_lock(); |
0a7de745 | 6075 | |
cb323159 A |
6076 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
6077 | while (next_p) { | |
6078 | uint64_t footprint_in_bytes = 0; | |
6079 | uint64_t memlimit_in_bytes = 0; | |
6080 | boolean_t skip = 0; | |
fe8ab488 | 6081 | |
cb323159 A |
6082 | p = next_p; |
6083 | next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
fe8ab488 | 6084 | |
cb323159 A |
6085 | aPid = p->p_pid; |
6086 | aPid_ep = p->p_memstat_effectivepriority; | |
fe8ab488 | 6087 | |
cb323159 A |
6088 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { |
6089 | continue; | |
fe8ab488 | 6090 | } |
3e170ce0 | 6091 | |
cb323159 A |
6092 | /* skip if no limit set */ |
6093 | if (p->p_memstat_memlimit <= 0) { | |
6094 | continue; | |
3e170ce0 | 6095 | } |
fe8ab488 | 6096 | |
cb323159 A |
6097 | footprint_in_bytes = get_task_phys_footprint(p->task); |
6098 | memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ | |
6099 | skip = (footprint_in_bytes <= memlimit_in_bytes); | |
6100 | ||
6101 | #if CONFIG_FREEZE | |
6102 | if (!skip) { | |
6103 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { | |
6104 | skip = TRUE; | |
6105 | } else { | |
6106 | skip = FALSE; | |
6107 | } | |
6108 | } | |
6109 | #endif | |
6110 | ||
6111 | if (skip) { | |
6112 | continue; | |
6113 | } else { | |
6114 | if (memorystatus_jetsam_snapshot_count == 0) { | |
6115 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); | |
6116 | new_snapshot = TRUE; | |
6117 | } | |
6118 | ||
6119 | if (proc_ref_locked(p) == p) { | |
6120 | /* | |
6121 | * Mark as terminated so that if exit1() indicates success, but the process (for example) | |
6122 | * is blocked in task_exception_notify(), it'll be skipped if encountered again - see | |
6123 | * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the | |
6124 | * acquisition of the proc lock. | |
6125 | */ | |
6126 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
6127 | ||
6128 | proc_list_unlock(); | |
6129 | } else { | |
6130 | /* | |
6131 | * We need to restart the search again because | |
6132 | * proc_ref_locked _can_ drop the proc_list lock | |
6133 | * and we could have lost our stored next_p via | |
6134 | * an exit() on another core. | |
6135 | */ | |
6136 | i = 0; | |
6137 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
6138 | continue; | |
6139 | } | |
39037602 | 6140 | |
cb323159 A |
6141 | footprint_in_bytes = 0; |
6142 | freed_mem = memorystatus_kill_proc(p, kMemorystatusKilledHiwat, jetsam_reason, &killed, &footprint_in_bytes); /* purged and/or killed 'p' */ | |
6143 | ||
6144 | /* Success? */ | |
6145 | if (freed_mem) { | |
6146 | if (killed == FALSE) { | |
6147 | /* purged 'p'..don't reset HWM candidate count */ | |
6148 | *purged = TRUE; | |
39037602 | 6149 | |
cb323159 A |
6150 | proc_list_lock(); |
6151 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
6152 | proc_list_unlock(); | |
6153 | } else { | |
6154 | *memory_reclaimed = footprint_in_bytes; | |
5ba3f43e | 6155 | } |
cb323159 A |
6156 | proc_rele(p); |
6157 | goto exit; | |
6158 | } | |
6159 | /* | |
6160 | * Failure - first unwind the state, | |
6161 | * then fall through to restart the search. | |
6162 | */ | |
6163 | proc_list_lock(); | |
6164 | proc_rele_locked(p); | |
6165 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
6166 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
6167 | *errors += 1; | |
5ba3f43e | 6168 | |
cb323159 A |
6169 | i = 0; |
6170 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
39037602 A |
6171 | } |
6172 | } | |
6173 | ||
cb323159 | 6174 | proc_list_unlock(); |
39236c6e | 6175 | |
cb323159 A |
6176 | exit: |
6177 | os_reason_free(jetsam_reason); | |
fe8ab488 | 6178 | |
cb323159 A |
6179 | if (!killed) { |
6180 | *memory_reclaimed = 0; | |
fe8ab488 | 6181 | |
cb323159 A |
6182 | /* Clear snapshot if freshly captured and no target was found */ |
6183 | if (new_snapshot) { | |
6184 | proc_list_lock(); | |
6185 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
6186 | proc_list_unlock(); | |
fe8ab488 | 6187 | } |
cb323159 A |
6188 | } |
6189 | ||
6190 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END, | |
f427ee49 | 6191 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, killed ? aPid : 0, killed, *memory_reclaimed, 0); |
fe8ab488 | 6192 | |
cb323159 A |
6193 | return killed; |
6194 | } | |
fe8ab488 | 6195 | |
cb323159 A |
6196 | /* |
6197 | * Jetsam a process pinned in the elevated band. | |
6198 | * | |
6199 | * Return: true -- a pinned process was jetsammed | |
6200 | * false -- no pinned process was jetsammed | |
6201 | */ | |
6202 | boolean_t | |
6203 | memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, uint32_t *errors, uint64_t *memory_reclaimed) | |
6204 | { | |
6205 | pid_t aPid = 0; | |
6206 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
6207 | boolean_t new_snapshot = FALSE, killed = FALSE; | |
6208 | int kill_count = 0; | |
6209 | uint32_t aPid_ep; | |
6210 | uint64_t killtime = 0; | |
6211 | clock_sec_t tv_sec; | |
6212 | clock_usec_t tv_usec; | |
6213 | uint32_t tv_msec; | |
6214 | uint64_t footprint_of_killed_proc = 0; | |
39236c6e | 6215 | |
39236c6e | 6216 | |
cb323159 | 6217 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, |
f427ee49 | 6218 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, 0, 0, 0, 0); |
39236c6e | 6219 | |
cb323159 A |
6220 | #if CONFIG_FREEZE |
6221 | boolean_t consider_frozen_only = FALSE; | |
39037602 | 6222 | |
cb323159 A |
6223 | if (band == (unsigned int) memorystatus_freeze_jetsam_band) { |
6224 | consider_frozen_only = TRUE; | |
6225 | } | |
6226 | #endif /* CONFIG_FREEZE */ | |
39037602 | 6227 | |
cb323159 | 6228 | proc_list_lock(); |
39037602 | 6229 | |
cb323159 A |
6230 | next_p = memorystatus_get_first_proc_locked(&band, FALSE); |
6231 | while (next_p) { | |
6232 | p = next_p; | |
6233 | next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); | |
0a7de745 | 6234 | |
cb323159 A |
6235 | aPid = p->p_pid; |
6236 | aPid_ep = p->p_memstat_effectivepriority; | |
0a7de745 | 6237 | |
cb323159 A |
6238 | /* |
6239 | * Only pick a process pinned in this elevated band | |
6240 | */ | |
6241 | if (!(p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) { | |
39236c6e A |
6242 | continue; |
6243 | } | |
39236c6e | 6244 | |
cb323159 A |
6245 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { |
6246 | continue; | |
6d2010ae | 6247 | } |
39236c6e | 6248 | |
cb323159 A |
6249 | #if CONFIG_FREEZE |
6250 | if (consider_frozen_only && !(p->p_memstat_state & P_MEMSTAT_FROZEN)) { | |
39236c6e A |
6251 | continue; |
6252 | } | |
6253 | ||
cb323159 A |
6254 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { |
6255 | continue; | |
3e170ce0 | 6256 | } |
cb323159 | 6257 | #endif /* CONFIG_FREEZE */ |
39236c6e | 6258 | |
cb323159 A |
6259 | #if DEVELOPMENT || DEBUG |
6260 | MEMORYSTATUS_DEBUG(1, "jetsam: elevated%d process pid %d [%s] - memorystatus_available_pages: %d\n", | |
6261 | aggr_count, | |
6262 | aPid, (*p->p_name ? p->p_name : "unknown"), | |
f427ee49 | 6263 | MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
cb323159 | 6264 | #endif /* DEVELOPMENT || DEBUG */ |
39236c6e | 6265 | |
cb323159 A |
6266 | if (memorystatus_jetsam_snapshot_count == 0) { |
6267 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); | |
6268 | new_snapshot = TRUE; | |
3e170ce0 A |
6269 | } |
6270 | ||
cb323159 A |
6271 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; |
6272 | ||
6273 | killtime = mach_absolute_time(); | |
6274 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
6275 | tv_msec = tv_usec / 1000; | |
39236c6e | 6276 | |
cb323159 | 6277 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); |
39236c6e | 6278 | |
cb323159 A |
6279 | if (proc_ref_locked(p) == p) { |
6280 | proc_list_unlock(); | |
39236c6e | 6281 | |
fe8ab488 | 6282 | /* |
cb323159 A |
6283 | * memorystatus_do_kill drops a reference, so take another one so we can |
6284 | * continue to use this exit reason even after memorystatus_do_kill() | |
6285 | * returns | |
fe8ab488 | 6286 | */ |
cb323159 A |
6287 | os_reason_ref(jetsam_reason); |
6288 | killed = memorystatus_do_kill(p, cause, jetsam_reason, &footprint_of_killed_proc); | |
0a7de745 | 6289 | |
cb323159 A |
6290 | os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: killing_top_process_elevated%d pid %d [%s] (%s %d) %lluKB - memorystatus_available_pages: %llu\n", |
6291 | (unsigned long)tv_sec, tv_msec, | |
6292 | aggr_count, | |
6293 | aPid, ((p && *p->p_name) ? p->p_name : "unknown"), | |
6294 | memorystatus_kill_cause_name[cause], aPid_ep, | |
f427ee49 | 6295 | footprint_of_killed_proc >> 10, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES); |
fe8ab488 | 6296 | |
cb323159 A |
6297 | /* Success? */ |
6298 | if (killed) { | |
6299 | *memory_reclaimed = footprint_of_killed_proc; | |
6300 | proc_rele(p); | |
6301 | kill_count++; | |
6302 | goto exit; | |
fe8ab488 | 6303 | } |
0a7de745 | 6304 | |
cb323159 A |
6305 | /* |
6306 | * Failure - first unwind the state, | |
6307 | * then fall through to restart the search. | |
6308 | */ | |
6309 | proc_list_lock(); | |
6310 | proc_rele_locked(p); | |
6311 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
6312 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
6313 | *errors += 1; | |
39236c6e | 6314 | } |
6d2010ae | 6315 | |
cb323159 A |
6316 | /* |
6317 | * Failure - restart the search. | |
6318 | * | |
6319 | * We might have raced with "p" exiting on another core, resulting in no | |
6320 | * ref on "p". Or, we may have failed to kill "p". | |
6321 | * | |
6322 | * Either way, we fall thru to here, leaving the proc in the | |
6323 | * P_MEMSTAT_TERMINATED state or P_MEMSTAT_ERROR state. | |
6324 | * | |
6325 | * And, we hold the the proc_list_lock at this point. | |
6326 | */ | |
39236c6e | 6327 | |
cb323159 | 6328 | next_p = memorystatus_get_first_proc_locked(&band, FALSE); |
0a7de745 | 6329 | } |
39236c6e | 6330 | |
cb323159 | 6331 | proc_list_unlock(); |
39236c6e | 6332 | |
cb323159 A |
6333 | exit: |
6334 | os_reason_free(jetsam_reason); | |
39236c6e | 6335 | |
cb323159 A |
6336 | if (kill_count == 0) { |
6337 | *memory_reclaimed = 0; | |
6338 | ||
6339 | /* Clear snapshot if freshly captured and no target was found */ | |
6340 | if (new_snapshot) { | |
6341 | proc_list_lock(); | |
6342 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
6343 | proc_list_unlock(); | |
6344 | } | |
39236c6e | 6345 | } |
316670eb | 6346 | |
cb323159 | 6347 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, |
f427ee49 | 6348 | MEMORYSTATUS_LOG_AVAILABLE_PAGES, killed ? aPid : 0, kill_count, *memory_reclaimed, 0); |
cb323159 A |
6349 | |
6350 | return killed; | |
39236c6e | 6351 | } |
6d2010ae | 6352 | |
cb323159 A |
6353 | static boolean_t |
6354 | memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause) | |
b0d623f7 | 6355 | { |
cb323159 A |
6356 | /* |
6357 | * TODO: allow a general async path | |
6358 | * | |
6359 | * NOTE: If a new async kill cause is added, make sure to update memorystatus_thread() to | |
6360 | * add the appropriate exit reason code mapping. | |
6361 | */ | |
6362 | if ((victim_pid != -1) || | |
6363 | (cause != kMemorystatusKilledVMPageShortage && | |
6364 | cause != kMemorystatusKilledVMCompressorThrashing && | |
6365 | cause != kMemorystatusKilledVMCompressorSpaceShortage && | |
6366 | cause != kMemorystatusKilledFCThrashing && | |
6367 | cause != kMemorystatusKilledZoneMapExhaustion)) { | |
6368 | return FALSE; | |
0a7de745 | 6369 | } |
5ba3f43e | 6370 | |
cb323159 A |
6371 | kill_under_pressure_cause = cause; |
6372 | memorystatus_thread_wake(); | |
6373 | return TRUE; | |
39236c6e A |
6374 | } |
6375 | ||
cb323159 A |
6376 | boolean_t |
6377 | memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async) | |
39236c6e | 6378 | { |
cb323159 A |
6379 | if (async) { |
6380 | return memorystatus_kill_process_async(-1, kMemorystatusKilledVMCompressorSpaceShortage); | |
6381 | } else { | |
6382 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE); | |
6383 | if (jetsam_reason == OS_REASON_NULL) { | |
6384 | printf("memorystatus_kill_on_VM_compressor_space_shortage -- sync: failed to allocate jetsam reason\n"); | |
6385 | } | |
39236c6e | 6386 | |
cb323159 | 6387 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMCompressorSpaceShortage, jetsam_reason); |
39236c6e | 6388 | } |
cb323159 | 6389 | } |
39236c6e | 6390 | |
cb323159 A |
6391 | #if CONFIG_JETSAM |
6392 | boolean_t | |
6393 | memorystatus_kill_on_VM_compressor_thrashing(boolean_t async) | |
6394 | { | |
6395 | if (async) { | |
6396 | return memorystatus_kill_process_async(-1, kMemorystatusKilledVMCompressorThrashing); | |
6397 | } else { | |
6398 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING); | |
6399 | if (jetsam_reason == OS_REASON_NULL) { | |
6400 | printf("memorystatus_kill_on_VM_compressor_thrashing -- sync: failed to allocate jetsam reason\n"); | |
6401 | } | |
39236c6e | 6402 | |
cb323159 | 6403 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMCompressorThrashing, jetsam_reason); |
b0d623f7 | 6404 | } |
cb323159 | 6405 | } |
b0d623f7 | 6406 | |
cb323159 A |
6407 | boolean_t |
6408 | memorystatus_kill_on_VM_page_shortage(boolean_t async) | |
6409 | { | |
6410 | if (async) { | |
6411 | return memorystatus_kill_process_async(-1, kMemorystatusKilledVMPageShortage); | |
6412 | } else { | |
6413 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMPAGESHORTAGE); | |
6414 | if (jetsam_reason == OS_REASON_NULL) { | |
6415 | printf("memorystatus_kill_on_VM_page_shortage -- sync: failed to allocate jetsam reason\n"); | |
0a7de745 | 6416 | } |
39236c6e | 6417 | |
cb323159 | 6418 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMPageShortage, jetsam_reason); |
39236c6e | 6419 | } |
cb323159 | 6420 | } |
39236c6e | 6421 | |
cb323159 A |
6422 | boolean_t |
6423 | memorystatus_kill_on_FC_thrashing(boolean_t async) | |
6424 | { | |
6425 | if (async) { | |
6426 | return memorystatus_kill_process_async(-1, kMemorystatusKilledFCThrashing); | |
6427 | } else { | |
6428 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_FCTHRASHING); | |
6429 | if (jetsam_reason == OS_REASON_NULL) { | |
6430 | printf("memorystatus_kill_on_FC_thrashing -- sync: failed to allocate jetsam reason\n"); | |
39236c6e | 6431 | } |
cb323159 A |
6432 | |
6433 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledFCThrashing, jetsam_reason); | |
39236c6e | 6434 | } |
cb323159 | 6435 | } |
0a7de745 | 6436 | |
cb323159 A |
6437 | boolean_t |
6438 | memorystatus_kill_on_vnode_limit(void) | |
6439 | { | |
6440 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_VNODE); | |
6441 | if (jetsam_reason == OS_REASON_NULL) { | |
6442 | printf("memorystatus_kill_on_vnode_limit: failed to allocate jetsam reason\n"); | |
39236c6e A |
6443 | } |
6444 | ||
cb323159 | 6445 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVnodes, jetsam_reason); |
b0d623f7 A |
6446 | } |
6447 | ||
cb323159 | 6448 | #endif /* CONFIG_JETSAM */ |
39236c6e | 6449 | |
cb323159 A |
6450 | boolean_t |
6451 | memorystatus_kill_on_zone_map_exhaustion(pid_t pid) | |
6452 | { | |
6453 | boolean_t res = FALSE; | |
6454 | if (pid == -1) { | |
6455 | res = memorystatus_kill_process_async(-1, kMemorystatusKilledZoneMapExhaustion); | |
6456 | } else { | |
6457 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_ZONE_MAP_EXHAUSTION); | |
6458 | if (jetsam_reason == OS_REASON_NULL) { | |
6459 | printf("memorystatus_kill_on_zone_map_exhaustion: failed to allocate jetsam reason\n"); | |
6460 | } | |
39236c6e | 6461 | |
cb323159 A |
6462 | res = memorystatus_kill_process_sync(pid, kMemorystatusKilledZoneMapExhaustion, jetsam_reason); |
6463 | } | |
6464 | return res; | |
6465 | } | |
39236c6e | 6466 | |
cb323159 A |
6467 | void |
6468 | memorystatus_on_pageout_scan_end(void) | |
6469 | { | |
6470 | /* No-op */ | |
6471 | } | |
39236c6e A |
6472 | |
6473 | /* Return both allocated and actual size, since there's a race between allocation and list compilation */ | |
b0d623f7 | 6474 | static int |
0a7de745 | 6475 | memorystatus_get_priority_list(memorystatus_priority_entry_t **list_ptr, size_t *buffer_size, size_t *list_size, boolean_t size_only) |
b0d623f7 | 6476 | { |
0a7de745 | 6477 | uint32_t list_count, i = 0; |
39236c6e A |
6478 | memorystatus_priority_entry_t *list_entry; |
6479 | proc_t p; | |
6480 | ||
0a7de745 | 6481 | list_count = memorystatus_list_count; |
39236c6e A |
6482 | *list_size = sizeof(memorystatus_priority_entry_t) * list_count; |
6483 | ||
6484 | /* Just a size check? */ | |
6485 | if (size_only) { | |
6486 | return 0; | |
6487 | } | |
0a7de745 | 6488 | |
39236c6e A |
6489 | /* Otherwise, validate the size of the buffer */ |
6490 | if (*buffer_size < *list_size) { | |
6491 | return EINVAL; | |
6492 | } | |
6493 | ||
f427ee49 | 6494 | *list_ptr = kheap_alloc(KHEAP_TEMP, *list_size, Z_WAITOK | Z_ZERO); |
a39ff7e2 | 6495 | if (!*list_ptr) { |
316670eb A |
6496 | return ENOMEM; |
6497 | } | |
6498 | ||
39236c6e A |
6499 | *buffer_size = *list_size; |
6500 | *list_size = 0; | |
6501 | ||
6502 | list_entry = *list_ptr; | |
6503 | ||
6504 | proc_list_lock(); | |
6505 | ||
6506 | p = memorystatus_get_first_proc_locked(&i, TRUE); | |
6507 | while (p && (*list_size < *buffer_size)) { | |
6508 | list_entry->pid = p->p_pid; | |
6509 | list_entry->priority = p->p_memstat_effectivepriority; | |
6510 | list_entry->user_data = p->p_memstat_userdata; | |
3e170ce0 | 6511 | |
3e170ce0 | 6512 | if (p->p_memstat_memlimit <= 0) { |
0a7de745 A |
6513 | task_get_phys_footprint_limit(p->task, &list_entry->limit); |
6514 | } else { | |
6515 | list_entry->limit = p->p_memstat_memlimit; | |
6516 | } | |
39037602 | 6517 | |
39236c6e A |
6518 | list_entry->state = memorystatus_build_state(p); |
6519 | list_entry++; | |
6520 | ||
6521 | *list_size += sizeof(memorystatus_priority_entry_t); | |
0a7de745 | 6522 | |
39236c6e | 6523 | p = memorystatus_get_next_proc_locked(&i, p, TRUE); |
316670eb | 6524 | } |
0a7de745 | 6525 | |
39236c6e | 6526 | proc_list_unlock(); |
0a7de745 | 6527 | |
39236c6e | 6528 | MEMORYSTATUS_DEBUG(1, "memorystatus_get_priority_list: returning %lu for size\n", (unsigned long)*list_size); |
0a7de745 | 6529 | |
39236c6e A |
6530 | return 0; |
6531 | } | |
b0d623f7 | 6532 | |
39236c6e | 6533 | static int |
0a7de745 A |
6534 | memorystatus_get_priority_pid(pid_t pid, user_addr_t buffer, size_t buffer_size) |
6535 | { | |
6536 | int error = 0; | |
6537 | memorystatus_priority_entry_t mp_entry; | |
cb323159 | 6538 | kern_return_t ret; |
5ba3f43e | 6539 | |
0a7de745 A |
6540 | /* Validate inputs */ |
6541 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_entry_t))) { | |
6542 | return EINVAL; | |
6543 | } | |
5ba3f43e A |
6544 | |
6545 | proc_t p = proc_find(pid); | |
0a7de745 A |
6546 | if (!p) { |
6547 | return ESRCH; | |
6548 | } | |
5ba3f43e | 6549 | |
0a7de745 | 6550 | memset(&mp_entry, 0, sizeof(memorystatus_priority_entry_t)); |
5ba3f43e | 6551 | |
0a7de745 A |
6552 | mp_entry.pid = p->p_pid; |
6553 | mp_entry.priority = p->p_memstat_effectivepriority; | |
6554 | mp_entry.user_data = p->p_memstat_userdata; | |
6555 | if (p->p_memstat_memlimit <= 0) { | |
cb323159 A |
6556 | ret = task_get_phys_footprint_limit(p->task, &mp_entry.limit); |
6557 | if (ret != KERN_SUCCESS) { | |
6558 | proc_rele(p); | |
6559 | return EINVAL; | |
6560 | } | |
0a7de745 A |
6561 | } else { |
6562 | mp_entry.limit = p->p_memstat_memlimit; | |
6563 | } | |
6564 | mp_entry.state = memorystatus_build_state(p); | |
5ba3f43e | 6565 | |
0a7de745 | 6566 | proc_rele(p); |
5ba3f43e | 6567 | |
0a7de745 | 6568 | error = copyout(&mp_entry, buffer, buffer_size); |
5ba3f43e | 6569 | |
0a7de745 | 6570 | return error; |
5ba3f43e A |
6571 | } |
6572 | ||
6573 | static int | |
0a7de745 A |
6574 | memorystatus_cmd_get_priority_list(pid_t pid, user_addr_t buffer, size_t buffer_size, int32_t *retval) |
6575 | { | |
5ba3f43e | 6576 | int error = 0; |
39236c6e | 6577 | boolean_t size_only; |
39236c6e | 6578 | size_t list_size; |
5ba3f43e A |
6579 | |
6580 | /* | |
6581 | * When a non-zero pid is provided, the 'list' has only one entry. | |
6582 | */ | |
0a7de745 | 6583 | |
39236c6e | 6584 | size_only = ((buffer == USER_ADDR_NULL) ? TRUE: FALSE); |
39236c6e | 6585 | |
5ba3f43e A |
6586 | if (pid != 0) { |
6587 | list_size = sizeof(memorystatus_priority_entry_t) * 1; | |
6588 | if (!size_only) { | |
6589 | error = memorystatus_get_priority_pid(pid, buffer, buffer_size); | |
6590 | } | |
6591 | } else { | |
6592 | memorystatus_priority_entry_t *list = NULL; | |
6593 | error = memorystatus_get_priority_list(&list, &buffer_size, &list_size, size_only); | |
6594 | ||
6595 | if (error == 0) { | |
6596 | if (!size_only) { | |
6597 | error = copyout(list, buffer, list_size); | |
6598 | } | |
6599 | } | |
6600 | ||
6601 | if (list) { | |
f427ee49 | 6602 | kheap_free(KHEAP_TEMP, list, buffer_size); |
5ba3f43e | 6603 | } |
39236c6e | 6604 | } |
5ba3f43e | 6605 | |
39236c6e | 6606 | if (error == 0) { |
f427ee49 A |
6607 | assert(list_size <= INT32_MAX); |
6608 | *retval = (int32_t) list_size; | |
39236c6e | 6609 | } |
39236c6e | 6610 | |
0a7de745 | 6611 | return error; |
316670eb | 6612 | } |
b0d623f7 | 6613 | |
0a7de745 | 6614 | static void |
39236c6e A |
6615 | memorystatus_clear_errors(void) |
6616 | { | |
6617 | proc_t p; | |
6618 | unsigned int i = 0; | |
6619 | ||
6620 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_START, 0, 0, 0, 0, 0); | |
0a7de745 | 6621 | |
39236c6e | 6622 | proc_list_lock(); |
0a7de745 | 6623 | |
39236c6e A |
6624 | p = memorystatus_get_first_proc_locked(&i, TRUE); |
6625 | while (p) { | |
6626 | if (p->p_memstat_state & P_MEMSTAT_ERROR) { | |
6627 | p->p_memstat_state &= ~P_MEMSTAT_ERROR; | |
6628 | } | |
6629 | p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
6630 | } | |
0a7de745 | 6631 | |
39236c6e A |
6632 | proc_list_unlock(); |
6633 | ||
6634 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_END, 0, 0, 0, 0, 0); | |
6635 | } | |
b0d623f7 | 6636 | |
5ba3f43e | 6637 | #if CONFIG_JETSAM |
316670eb | 6638 | static void |
0a7de745 A |
6639 | memorystatus_update_levels_locked(boolean_t critical_only) |
6640 | { | |
39236c6e | 6641 | memorystatus_available_pages_critical = memorystatus_available_pages_critical_base; |
fe8ab488 A |
6642 | |
6643 | /* | |
6644 | * If there's an entry in the first bucket, we have idle processes. | |
6645 | */ | |
39037602 | 6646 | |
fe8ab488 A |
6647 | memstat_bucket_t *first_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; |
6648 | if (first_bucket->count) { | |
6649 | memorystatus_available_pages_critical += memorystatus_available_pages_critical_idle_offset; | |
6650 | ||
0a7de745 A |
6651 | if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure) { |
6652 | /* | |
fe8ab488 A |
6653 | * The critical threshold must never exceed the pressure threshold |
6654 | */ | |
6655 | memorystatus_available_pages_critical = memorystatus_available_pages_pressure; | |
39236c6e A |
6656 | } |
6657 | } | |
fe8ab488 | 6658 | |
39037602 A |
6659 | if (memorystatus_jetsam_policy & kPolicyMoreFree) { |
6660 | memorystatus_available_pages_critical += memorystatus_policy_more_free_offset_pages; | |
6661 | } | |
6662 | ||
39236c6e A |
6663 | if (critical_only) { |
6664 | return; | |
6665 | } | |
0a7de745 | 6666 | |
316670eb | 6667 | #if VM_PRESSURE_EVENTS |
f427ee49 | 6668 | memorystatus_available_pages_pressure = (int32_t)(pressure_threshold_percentage * (atop_64(max_mem) / 100)); |
39236c6e A |
6669 | #endif |
6670 | } | |
6671 | ||
d9a64523 A |
6672 | void |
6673 | memorystatus_fast_jetsam_override(boolean_t enable_override) | |
6674 | { | |
6675 | /* If fast jetsam is not enabled, simply return */ | |
0a7de745 | 6676 | if (!fast_jetsam_enabled) { |
d9a64523 | 6677 | return; |
0a7de745 | 6678 | } |
d9a64523 A |
6679 | |
6680 | if (enable_override) { | |
0a7de745 | 6681 | if ((memorystatus_jetsam_policy & kPolicyMoreFree) == kPolicyMoreFree) { |
d9a64523 | 6682 | return; |
0a7de745 | 6683 | } |
d9a64523 A |
6684 | proc_list_lock(); |
6685 | memorystatus_jetsam_policy |= kPolicyMoreFree; | |
6686 | memorystatus_thread_pool_max(); | |
6687 | memorystatus_update_levels_locked(TRUE); | |
6688 | proc_list_unlock(); | |
6689 | } else { | |
0a7de745 | 6690 | if ((memorystatus_jetsam_policy & kPolicyMoreFree) == 0) { |
d9a64523 | 6691 | return; |
0a7de745 | 6692 | } |
d9a64523 A |
6693 | proc_list_lock(); |
6694 | memorystatus_jetsam_policy &= ~kPolicyMoreFree; | |
6695 | memorystatus_thread_pool_default(); | |
6696 | memorystatus_update_levels_locked(TRUE); | |
6697 | proc_list_unlock(); | |
6698 | } | |
6699 | } | |
6700 | ||
5ba3f43e | 6701 | |
39037602 A |
6702 | static int |
6703 | sysctl_kern_memorystatus_policy_more_free SYSCTL_HANDLER_ARGS | |
6704 | { | |
6705 | #pragma unused(arg1, arg2, oidp) | |
6706 | int error = 0, more_free = 0; | |
6707 | ||
6708 | /* | |
6709 | * TODO: Enable this privilege check? | |
6710 | * | |
6711 | * error = priv_check_cred(kauth_cred_get(), PRIV_VM_JETSAM, 0); | |
6712 | * if (error) | |
6713 | * return (error); | |
6714 | */ | |
6715 | ||
6716 | error = sysctl_handle_int(oidp, &more_free, 0, req); | |
0a7de745 A |
6717 | if (error || !req->newptr) { |
6718 | return error; | |
6719 | } | |
39037602 | 6720 | |
39037602 | 6721 | if (more_free) { |
d9a64523 | 6722 | memorystatus_fast_jetsam_override(true); |
39037602 | 6723 | } else { |
d9a64523 | 6724 | memorystatus_fast_jetsam_override(false); |
39037602 A |
6725 | } |
6726 | ||
39037602 A |
6727 | return 0; |
6728 | } | |
0a7de745 | 6729 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_policy_more_free, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
39037602 A |
6730 | 0, 0, &sysctl_kern_memorystatus_policy_more_free, "I", ""); |
6731 | ||
5ba3f43e A |
6732 | #endif /* CONFIG_JETSAM */ |
6733 | ||
3e170ce0 A |
6734 | /* |
6735 | * Get the at_boot snapshot | |
6736 | */ | |
39236c6e | 6737 | static int |
0a7de745 A |
6738 | memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
6739 | { | |
39236c6e | 6740 | size_t input_size = *snapshot_size; |
3e170ce0 A |
6741 | |
6742 | /* | |
6743 | * The at_boot snapshot has no entry list. | |
6744 | */ | |
6745 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t); | |
6746 | ||
6747 | if (size_only) { | |
6748 | return 0; | |
6749 | } | |
6750 | ||
6751 | /* | |
6752 | * Validate the size of the snapshot buffer | |
6753 | */ | |
6754 | if (input_size < *snapshot_size) { | |
6755 | return EINVAL; | |
6756 | } | |
6757 | ||
6758 | /* | |
6759 | * Update the notification_time only | |
6760 | */ | |
6761 | memorystatus_at_boot_snapshot.notification_time = mach_absolute_time(); | |
6762 | *snapshot = &memorystatus_at_boot_snapshot; | |
6763 | ||
6764 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_at_boot_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%d)\n", | |
0a7de745 | 6765 | (long)input_size, (long)*snapshot_size, 0); |
3e170ce0 A |
6766 | return 0; |
6767 | } | |
6768 | ||
d9a64523 A |
6769 | /* |
6770 | * Get the previous fully populated snapshot | |
6771 | */ | |
6772 | static int | |
0a7de745 A |
6773 | memorystatus_get_jetsam_snapshot_copy(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
6774 | { | |
d9a64523 A |
6775 | size_t input_size = *snapshot_size; |
6776 | ||
6777 | if (memorystatus_jetsam_snapshot_copy_count > 0) { | |
6778 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_copy_count)); | |
6779 | } else { | |
6780 | *snapshot_size = 0; | |
6781 | } | |
6782 | ||
6783 | if (size_only) { | |
6784 | return 0; | |
6785 | } | |
6786 | ||
6787 | if (input_size < *snapshot_size) { | |
6788 | return EINVAL; | |
6789 | } | |
6790 | ||
6791 | *snapshot = memorystatus_jetsam_snapshot_copy; | |
6792 | ||
6793 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot_copy: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
0a7de745 | 6794 | (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_copy_count); |
d9a64523 A |
6795 | |
6796 | return 0; | |
6797 | } | |
6798 | ||
f427ee49 A |
6799 | #if CONFIG_FREEZE |
6800 | static int | |
6801 | memorystatus_get_jetsam_snapshot_freezer(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) | |
6802 | { | |
6803 | size_t input_size = *snapshot_size; | |
6804 | ||
6805 | if (memorystatus_jetsam_snapshot_freezer->entry_count > 0) { | |
6806 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_freezer->entry_count)); | |
6807 | } else { | |
6808 | *snapshot_size = 0; | |
6809 | } | |
6810 | assert(*snapshot_size <= memorystatus_jetsam_snapshot_freezer_size); | |
6811 | ||
6812 | if (size_only) { | |
6813 | return 0; | |
6814 | } | |
6815 | ||
6816 | if (input_size < *snapshot_size) { | |
6817 | return EINVAL; | |
6818 | } | |
6819 | ||
6820 | *snapshot = memorystatus_jetsam_snapshot_freezer; | |
6821 | ||
6822 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot_freezer: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
6823 | (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_freezer->entry_count); | |
6824 | ||
6825 | return 0; | |
6826 | } | |
6827 | #endif /* CONFIG_FREEZE */ | |
6828 | ||
3e170ce0 | 6829 | static int |
0a7de745 A |
6830 | memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
6831 | { | |
3e170ce0 A |
6832 | size_t input_size = *snapshot_size; |
6833 | uint32_t ods_list_count = memorystatus_list_count; | |
0a7de745 | 6834 | memorystatus_jetsam_snapshot_t *ods = NULL; /* The on_demand snapshot buffer */ |
3e170ce0 A |
6835 | |
6836 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (ods_list_count)); | |
6837 | ||
6838 | if (size_only) { | |
6839 | return 0; | |
6840 | } | |
6841 | ||
6842 | /* | |
6843 | * Validate the size of the snapshot buffer. | |
6844 | * This is inherently racey. May want to revisit | |
6845 | * this error condition and trim the output when | |
6846 | * it doesn't fit. | |
6847 | */ | |
6848 | if (input_size < *snapshot_size) { | |
6849 | return EINVAL; | |
6850 | } | |
6851 | ||
6852 | /* | |
6853 | * Allocate and initialize a snapshot buffer. | |
6854 | */ | |
f427ee49 | 6855 | ods = kalloc(*snapshot_size); |
3e170ce0 | 6856 | if (!ods) { |
0a7de745 | 6857 | return ENOMEM; |
3e170ce0 A |
6858 | } |
6859 | ||
6860 | memset(ods, 0, *snapshot_size); | |
6861 | ||
6862 | proc_list_lock(); | |
6863 | memorystatus_init_jetsam_snapshot_locked(ods, ods_list_count); | |
6864 | proc_list_unlock(); | |
6865 | ||
6866 | /* | |
6867 | * Return the kernel allocated, on_demand buffer. | |
6868 | * The caller of this routine will copy the data out | |
6869 | * to user space and then free the kernel allocated | |
6870 | * buffer. | |
6871 | */ | |
6872 | *snapshot = ods; | |
6873 | ||
6874 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_on_demand_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
0a7de745 A |
6875 | (long)input_size, (long)*snapshot_size, (long)ods_list_count); |
6876 | ||
3e170ce0 A |
6877 | return 0; |
6878 | } | |
6879 | ||
6880 | static int | |
0a7de745 A |
6881 | memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
6882 | { | |
3e170ce0 A |
6883 | size_t input_size = *snapshot_size; |
6884 | ||
39236c6e A |
6885 | if (memorystatus_jetsam_snapshot_count > 0) { |
6886 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count)); | |
6887 | } else { | |
6888 | *snapshot_size = 0; | |
6889 | } | |
6890 | ||
6891 | if (size_only) { | |
6892 | return 0; | |
316670eb | 6893 | } |
39236c6e A |
6894 | |
6895 | if (input_size < *snapshot_size) { | |
6896 | return EINVAL; | |
6897 | } | |
6898 | ||
6899 | *snapshot = memorystatus_jetsam_snapshot; | |
3e170ce0 A |
6900 | |
6901 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
0a7de745 | 6902 | (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_count); |
3e170ce0 | 6903 | |
39236c6e | 6904 | return 0; |
316670eb A |
6905 | } |
6906 | ||
fe8ab488 | 6907 | |
316670eb | 6908 | static int |
0a7de745 A |
6909 | memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) |
6910 | { | |
39236c6e A |
6911 | int error = EINVAL; |
6912 | boolean_t size_only; | |
3e170ce0 A |
6913 | boolean_t is_default_snapshot = FALSE; |
6914 | boolean_t is_on_demand_snapshot = FALSE; | |
6915 | boolean_t is_at_boot_snapshot = FALSE; | |
f427ee49 A |
6916 | #if CONFIG_FREEZE |
6917 | bool is_freezer_snapshot = false; | |
6918 | #endif /* CONFIG_FREEZE */ | |
39236c6e | 6919 | memorystatus_jetsam_snapshot_t *snapshot; |
3e170ce0 | 6920 | |
39236c6e | 6921 | size_only = ((buffer == USER_ADDR_NULL) ? TRUE : FALSE); |
3e170ce0 A |
6922 | |
6923 | if (flags == 0) { | |
6924 | /* Default */ | |
6925 | is_default_snapshot = TRUE; | |
6926 | error = memorystatus_get_jetsam_snapshot(&snapshot, &buffer_size, size_only); | |
6927 | } else { | |
f427ee49 | 6928 | if (flags & ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT | MEMORYSTATUS_SNAPSHOT_COPY | MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER)) { |
3e170ce0 A |
6929 | /* |
6930 | * Unsupported bit set in flag. | |
6931 | */ | |
6932 | return EINVAL; | |
6933 | } | |
6934 | ||
d9a64523 | 6935 | if (flags & (flags - 0x1)) { |
3e170ce0 | 6936 | /* |
d9a64523 | 6937 | * Can't have multiple flags set at the same time. |
3e170ce0 A |
6938 | */ |
6939 | return EINVAL; | |
6940 | } | |
6941 | ||
6942 | if (flags & MEMORYSTATUS_SNAPSHOT_ON_DEMAND) { | |
6943 | is_on_demand_snapshot = TRUE; | |
6944 | /* | |
6945 | * When not requesting the size only, the following call will allocate | |
6946 | * an on_demand snapshot buffer, which is freed below. | |
6947 | */ | |
6948 | error = memorystatus_get_on_demand_snapshot(&snapshot, &buffer_size, size_only); | |
3e170ce0 A |
6949 | } else if (flags & MEMORYSTATUS_SNAPSHOT_AT_BOOT) { |
6950 | is_at_boot_snapshot = TRUE; | |
6951 | error = memorystatus_get_at_boot_snapshot(&snapshot, &buffer_size, size_only); | |
d9a64523 A |
6952 | } else if (flags & MEMORYSTATUS_SNAPSHOT_COPY) { |
6953 | error = memorystatus_get_jetsam_snapshot_copy(&snapshot, &buffer_size, size_only); | |
f427ee49 A |
6954 | #if CONFIG_FREEZE |
6955 | } else if (flags & MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER) { | |
6956 | is_freezer_snapshot = true; | |
6957 | error = memorystatus_get_jetsam_snapshot_freezer(&snapshot, &buffer_size, size_only); | |
6958 | #endif /* CONFIG_FREEZE */ | |
3e170ce0 A |
6959 | } else { |
6960 | /* | |
6961 | * Invalid flag setting. | |
6962 | */ | |
6963 | return EINVAL; | |
6964 | } | |
6965 | } | |
6966 | ||
39236c6e A |
6967 | if (error) { |
6968 | goto out; | |
6969 | } | |
316670eb | 6970 | |
3e170ce0 A |
6971 | /* |
6972 | * Copy the data out to user space and clear the snapshot buffer. | |
6973 | * If working with the jetsam snapshot, | |
6974 | * clearing the buffer means, reset the count. | |
6975 | * If working with an on_demand snapshot | |
6976 | * clearing the buffer means, free it. | |
6977 | * If working with the at_boot snapshot | |
6978 | * there is nothing to clear or update. | |
d9a64523 A |
6979 | * If working with a copy of the snapshot |
6980 | * there is nothing to clear or update. | |
f427ee49 A |
6981 | * If working with the freezer snapshot |
6982 | * clearing the buffer means, reset the count. | |
3e170ce0 | 6983 | */ |
39236c6e A |
6984 | if (!size_only) { |
6985 | if ((error = copyout(snapshot, buffer, buffer_size)) == 0) { | |
f427ee49 A |
6986 | #if CONFIG_FREEZE |
6987 | if (is_default_snapshot || is_freezer_snapshot) { | |
6988 | #else | |
3e170ce0 | 6989 | if (is_default_snapshot) { |
f427ee49 | 6990 | #endif /* CONFIG_FREEZE */ |
3e170ce0 A |
6991 | /* |
6992 | * The jetsam snapshot is never freed, its count is simply reset. | |
d9a64523 A |
6993 | * However, we make a copy for any parties that might be interested |
6994 | * in the previous fully populated snapshot. | |
3e170ce0 | 6995 | */ |
3e170ce0 | 6996 | proc_list_lock(); |
f427ee49 A |
6997 | #if DEVELOPMENT || DEBUG |
6998 | if (memorystatus_snapshot_owner != 0 && memorystatus_snapshot_owner != current_proc()->p_pid) { | |
6999 | /* Snapshot is currently owned by someone else. Don't consume it. */ | |
7000 | proc_list_unlock(); | |
7001 | goto out; | |
7002 | } | |
7003 | #endif /* (DEVELOPMENT || DEBUG)*/ | |
7004 | if (is_default_snapshot) { | |
7005 | memcpy(memorystatus_jetsam_snapshot_copy, memorystatus_jetsam_snapshot, memorystatus_jetsam_snapshot_size); | |
7006 | memorystatus_jetsam_snapshot_copy_count = memorystatus_jetsam_snapshot_count; | |
7007 | snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
7008 | memorystatus_jetsam_snapshot_last_timestamp = 0; | |
7009 | } | |
7010 | #if CONFIG_FREEZE | |
7011 | else if (is_freezer_snapshot) { | |
7012 | memorystatus_jetsam_snapshot_freezer->entry_count = 0; | |
7013 | } | |
7014 | #endif /* CONFIG_FREEZE */ | |
3e170ce0 A |
7015 | proc_list_unlock(); |
7016 | } | |
7017 | } | |
7018 | ||
7019 | if (is_on_demand_snapshot) { | |
7020 | /* | |
7021 | * The on_demand snapshot is always freed, | |
7022 | * even if the copyout failed. | |
7023 | */ | |
0a7de745 | 7024 | if (snapshot) { |
3e170ce0 A |
7025 | kfree(snapshot, buffer_size); |
7026 | } | |
39236c6e A |
7027 | } |
7028 | } | |
316670eb | 7029 | |
f427ee49 | 7030 | out: |
39236c6e | 7031 | if (error == 0) { |
f427ee49 A |
7032 | assert(buffer_size <= INT32_MAX); |
7033 | *retval = (int32_t) buffer_size; | |
39236c6e | 7034 | } |
39236c6e A |
7035 | return error; |
7036 | } | |
316670eb | 7037 | |
f427ee49 A |
7038 | #if DEVELOPMENT || DEBUG |
7039 | static int | |
7040 | memorystatus_cmd_set_jetsam_snapshot_ownership(int32_t flags) | |
7041 | { | |
7042 | int error = EINVAL; | |
7043 | proc_t caller = current_proc(); | |
7044 | assert(caller != kernproc); | |
7045 | proc_list_lock(); | |
7046 | if (flags & MEMORYSTATUS_FLAGS_SNAPSHOT_TAKE_OWNERSHIP) { | |
7047 | if (memorystatus_snapshot_owner == 0) { | |
7048 | memorystatus_snapshot_owner = caller->p_pid; | |
7049 | error = 0; | |
7050 | } else if (memorystatus_snapshot_owner == caller->p_pid) { | |
7051 | error = 0; | |
7052 | } else { | |
7053 | /* We don't allow ownership to be taken from another proc. */ | |
7054 | error = EBUSY; | |
7055 | } | |
7056 | } else if (flags & MEMORYSTATUS_FLAGS_SNAPSHOT_DROP_OWNERSHIP) { | |
7057 | if (memorystatus_snapshot_owner == caller->p_pid) { | |
7058 | memorystatus_snapshot_owner = 0; | |
7059 | error = 0; | |
7060 | } else if (memorystatus_snapshot_owner != 0) { | |
7061 | /* We don't allow ownership to be taken from another proc. */ | |
7062 | error = EPERM; | |
7063 | } | |
7064 | } | |
7065 | proc_list_unlock(); | |
7066 | ||
7067 | return error; | |
7068 | } | |
7069 | #endif /* DEVELOPMENT || DEBUG */ | |
7070 | ||
fe8ab488 | 7071 | /* |
0a7de745 | 7072 | * Routine: memorystatus_cmd_grp_set_priorities |
d9a64523 | 7073 | * Purpose: Update priorities for a group of processes. |
fe8ab488 | 7074 | * |
fe8ab488 A |
7075 | * [priority] |
7076 | * Move each process out of its effective priority | |
7077 | * band and into a new priority band. | |
7078 | * Maintains relative order from lowest to highest priority. | |
7079 | * In single band, maintains relative order from head to tail. | |
7080 | * | |
7081 | * eg: before [effectivepriority | pid] | |
7082 | * [18 | p101 ] | |
7083 | * [17 | p55, p67, p19 ] | |
7084 | * [12 | p103 p10 ] | |
7085 | * [ 7 | p25 ] | |
0a7de745 | 7086 | * [ 0 | p71, p82, ] |
fe8ab488 A |
7087 | * |
7088 | * after [ new band | pid] | |
7089 | * [ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101] | |
7090 | * | |
7091 | * Returns: 0 on success, else non-zero. | |
7092 | * | |
7093 | * Caveat: We know there is a race window regarding recycled pids. | |
7094 | * A process could be killed before the kernel can act on it here. | |
7095 | * If a pid cannot be found in any of the jetsam priority bands, | |
7096 | * then we simply ignore it. No harm. | |
7097 | * But, if the pid has been recycled then it could be an issue. | |
7098 | * In that scenario, we might move an unsuspecting process to the new | |
7099 | * priority band. It's not clear how the kernel can safeguard | |
7100 | * against this, but it would be an extremely rare case anyway. | |
7101 | * The caller of this api might avoid such race conditions by | |
7102 | * ensuring that the processes passed in the pid list are suspended. | |
7103 | */ | |
7104 | ||
7105 | ||
fe8ab488 | 7106 | static int |
d9a64523 A |
7107 | memorystatus_cmd_grp_set_priorities(user_addr_t buffer, size_t buffer_size) |
7108 | { | |
fe8ab488 A |
7109 | /* |
7110 | * We only handle setting priority | |
7111 | * per process | |
7112 | */ | |
7113 | ||
7114 | int error = 0; | |
d9a64523 | 7115 | memorystatus_properties_entry_v1_t *entries = NULL; |
f427ee49 | 7116 | size_t entry_count = 0; |
fe8ab488 A |
7117 | |
7118 | /* This will be the ordered proc list */ | |
d9a64523 A |
7119 | typedef struct memorystatus_internal_properties { |
7120 | proc_t proc; | |
7121 | int32_t priority; | |
7122 | } memorystatus_internal_properties_t; | |
7123 | ||
fe8ab488 A |
7124 | memorystatus_internal_properties_t *table = NULL; |
7125 | size_t table_size = 0; | |
7126 | uint32_t table_count = 0; | |
7127 | ||
f427ee49 | 7128 | size_t i = 0; |
fe8ab488 A |
7129 | uint32_t bucket_index = 0; |
7130 | boolean_t head_insert; | |
7131 | int32_t new_priority; | |
0a7de745 | 7132 | |
fe8ab488 A |
7133 | proc_t p; |
7134 | ||
7135 | /* Verify inputs */ | |
d9a64523 | 7136 | if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) { |
fe8ab488 A |
7137 | error = EINVAL; |
7138 | goto out; | |
7139 | } | |
7140 | ||
d9a64523 | 7141 | entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t)); |
f427ee49 A |
7142 | if (entry_count == 0) { |
7143 | /* buffer size was not large enough for a single entry */ | |
7144 | error = EINVAL; | |
7145 | goto out; | |
7146 | } | |
7147 | ||
7148 | if ((entries = kheap_alloc(KHEAP_TEMP, buffer_size, Z_WAITOK)) == NULL) { | |
fe8ab488 A |
7149 | error = ENOMEM; |
7150 | goto out; | |
7151 | } | |
7152 | ||
d9a64523 | 7153 | 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); |
fe8ab488 A |
7154 | |
7155 | if ((error = copyin(buffer, entries, buffer_size)) != 0) { | |
7156 | goto out; | |
7157 | } | |
7158 | ||
7159 | /* Verify sanity of input priorities */ | |
d9a64523 A |
7160 | if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) { |
7161 | if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) { | |
7162 | error = EINVAL; | |
7163 | goto out; | |
7164 | } | |
7165 | } else { | |
7166 | error = EINVAL; | |
7167 | goto out; | |
7168 | } | |
0a7de745 A |
7169 | |
7170 | for (i = 0; i < entry_count; i++) { | |
fe8ab488 A |
7171 | if (entries[i].priority == -1) { |
7172 | /* Use as shorthand for default priority */ | |
7173 | entries[i].priority = JETSAM_PRIORITY_DEFAULT; | |
39037602 A |
7174 | } else if ((entries[i].priority == system_procs_aging_band) || (entries[i].priority == applications_aging_band)) { |
7175 | /* Both the aging bands are reserved for internal use; | |
fe8ab488 A |
7176 | * if requested, adjust to JETSAM_PRIORITY_IDLE. */ |
7177 | entries[i].priority = JETSAM_PRIORITY_IDLE; | |
0a7de745 | 7178 | } else if (entries[i].priority == JETSAM_PRIORITY_IDLE_HEAD) { |
fe8ab488 A |
7179 | /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle |
7180 | * queue */ | |
7181 | /* Deal with this later */ | |
7182 | } else if ((entries[i].priority < 0) || (entries[i].priority >= MEMSTAT_BUCKET_COUNT)) { | |
7183 | /* Sanity check */ | |
7184 | error = EINVAL; | |
7185 | goto out; | |
7186 | } | |
7187 | } | |
7188 | ||
7189 | table_size = sizeof(memorystatus_internal_properties_t) * entry_count; | |
f427ee49 | 7190 | if ((table = kheap_alloc(KHEAP_TEMP, table_size, Z_WAITOK | Z_ZERO)) == NULL) { |
fe8ab488 A |
7191 | error = ENOMEM; |
7192 | goto out; | |
7193 | } | |
fe8ab488 A |
7194 | |
7195 | ||
7196 | /* | |
7197 | * For each jetsam bucket entry, spin through the input property list. | |
7198 | * When a matching pid is found, populate an adjacent table with the | |
7199 | * appropriate proc pointer and new property values. | |
7200 | * This traversal automatically preserves order from lowest | |
7201 | * to highest priority. | |
7202 | */ | |
7203 | ||
0a7de745 A |
7204 | bucket_index = 0; |
7205 | ||
fe8ab488 A |
7206 | proc_list_lock(); |
7207 | ||
7208 | /* Create the ordered table */ | |
0a7de745 | 7209 | p = memorystatus_get_first_proc_locked(&bucket_index, TRUE); |
fe8ab488 | 7210 | while (p && (table_count < entry_count)) { |
0a7de745 | 7211 | for (i = 0; i < entry_count; i++) { |
fe8ab488 A |
7212 | if (p->p_pid == entries[i].pid) { |
7213 | /* Build the table data */ | |
7214 | table[table_count].proc = p; | |
7215 | table[table_count].priority = entries[i].priority; | |
7216 | table_count++; | |
7217 | break; | |
7218 | } | |
7219 | } | |
7220 | p = memorystatus_get_next_proc_locked(&bucket_index, p, TRUE); | |
7221 | } | |
0a7de745 | 7222 | |
fe8ab488 | 7223 | /* We now have ordered list of procs ready to move */ |
0a7de745 | 7224 | for (i = 0; i < table_count; i++) { |
fe8ab488 A |
7225 | p = table[i].proc; |
7226 | assert(p != NULL); | |
7227 | ||
7228 | /* Allow head inserts -- but relative order is now */ | |
7229 | if (table[i].priority == JETSAM_PRIORITY_IDLE_HEAD) { | |
7230 | new_priority = JETSAM_PRIORITY_IDLE; | |
7231 | head_insert = true; | |
7232 | } else { | |
7233 | new_priority = table[i].priority; | |
7234 | head_insert = false; | |
7235 | } | |
0a7de745 | 7236 | |
fe8ab488 A |
7237 | /* Not allowed */ |
7238 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { | |
7239 | continue; | |
7240 | } | |
7241 | ||
7242 | /* | |
39037602 A |
7243 | * Take appropriate steps if moving proc out of |
7244 | * either of the aging bands. | |
fe8ab488 | 7245 | */ |
39037602 | 7246 | if ((p->p_memstat_effectivepriority == system_procs_aging_band) || (p->p_memstat_effectivepriority == applications_aging_band)) { |
fe8ab488 A |
7247 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
7248 | } | |
7249 | ||
39037602 | 7250 | memorystatus_update_priority_locked(p, new_priority, head_insert, false); |
fe8ab488 A |
7251 | } |
7252 | ||
7253 | proc_list_unlock(); | |
7254 | ||
7255 | /* | |
7256 | * if (table_count != entry_count) | |
7257 | * then some pids were not found in a jetsam band. | |
7258 | * harmless but interesting... | |
7259 | */ | |
fe8ab488 | 7260 | out: |
d9a64523 | 7261 | 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); |
0a7de745 A |
7262 | |
7263 | if (entries) { | |
f427ee49 | 7264 | kheap_free(KHEAP_TEMP, entries, buffer_size); |
0a7de745 A |
7265 | } |
7266 | if (table) { | |
f427ee49 | 7267 | kheap_free(KHEAP_TEMP, table, table_size); |
0a7de745 | 7268 | } |
fe8ab488 | 7269 | |
0a7de745 | 7270 | return error; |
fe8ab488 A |
7271 | } |
7272 | ||
cb323159 A |
7273 | memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table = NULL; |
7274 | size_t memorystatus_global_probabilities_size = 0; | |
7275 | ||
d9a64523 A |
7276 | static int |
7277 | memorystatus_cmd_grp_set_probabilities(user_addr_t buffer, size_t buffer_size) | |
7278 | { | |
7279 | int error = 0; | |
7280 | memorystatus_properties_entry_v1_t *entries = NULL; | |
f427ee49 | 7281 | size_t entry_count = 0, i = 0; |
d9a64523 A |
7282 | memorystatus_internal_probabilities_t *tmp_table_new = NULL, *tmp_table_old = NULL; |
7283 | size_t tmp_table_new_size = 0, tmp_table_old_size = 0; | |
7284 | ||
7285 | /* Verify inputs */ | |
7286 | if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) { | |
7287 | error = EINVAL; | |
7288 | goto out; | |
7289 | } | |
7290 | ||
7291 | entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t)); | |
7292 | ||
f427ee49 | 7293 | if ((entries = kheap_alloc(KHEAP_TEMP, buffer_size, Z_WAITOK)) == NULL) { |
d9a64523 A |
7294 | error = ENOMEM; |
7295 | goto out; | |
7296 | } | |
7297 | ||
7298 | 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); | |
7299 | ||
7300 | if ((error = copyin(buffer, entries, buffer_size)) != 0) { | |
7301 | goto out; | |
7302 | } | |
7303 | ||
7304 | if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) { | |
7305 | if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) { | |
7306 | error = EINVAL; | |
7307 | goto out; | |
7308 | } | |
7309 | } else { | |
7310 | error = EINVAL; | |
7311 | goto out; | |
7312 | } | |
0a7de745 | 7313 | |
d9a64523 | 7314 | /* Verify sanity of input priorities */ |
0a7de745 | 7315 | for (i = 0; i < entry_count; i++) { |
d9a64523 A |
7316 | /* |
7317 | * 0 - low probability of use. | |
7318 | * 1 - high probability of use. | |
7319 | * | |
0a7de745 | 7320 | * Keeping this field an int (& not a bool) to allow |
d9a64523 A |
7321 | * us to experiment with different values/approaches |
7322 | * later on. | |
7323 | */ | |
7324 | if (entries[i].use_probability > 1) { | |
7325 | error = EINVAL; | |
7326 | goto out; | |
7327 | } | |
7328 | } | |
7329 | ||
7330 | tmp_table_new_size = sizeof(memorystatus_internal_probabilities_t) * entry_count; | |
7331 | ||
f427ee49 | 7332 | if ((tmp_table_new = kalloc_flags(tmp_table_new_size, Z_WAITOK | Z_ZERO)) == NULL) { |
d9a64523 A |
7333 | error = ENOMEM; |
7334 | goto out; | |
7335 | } | |
d9a64523 A |
7336 | |
7337 | proc_list_lock(); | |
7338 | ||
7339 | if (memorystatus_global_probabilities_table) { | |
7340 | tmp_table_old = memorystatus_global_probabilities_table; | |
7341 | tmp_table_old_size = memorystatus_global_probabilities_size; | |
7342 | } | |
7343 | ||
7344 | memorystatus_global_probabilities_table = tmp_table_new; | |
7345 | memorystatus_global_probabilities_size = tmp_table_new_size; | |
7346 | tmp_table_new = NULL; | |
7347 | ||
0a7de745 | 7348 | for (i = 0; i < entry_count; i++) { |
d9a64523 A |
7349 | /* Build the table data */ |
7350 | strlcpy(memorystatus_global_probabilities_table[i].proc_name, entries[i].proc_name, MAXCOMLEN + 1); | |
7351 | memorystatus_global_probabilities_table[i].use_probability = entries[i].use_probability; | |
7352 | } | |
7353 | ||
7354 | proc_list_unlock(); | |
0a7de745 | 7355 | |
d9a64523 A |
7356 | out: |
7357 | 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); | |
7358 | ||
7359 | if (entries) { | |
f427ee49 | 7360 | kheap_free(KHEAP_TEMP, entries, buffer_size); |
d9a64523 A |
7361 | entries = NULL; |
7362 | } | |
7363 | ||
7364 | if (tmp_table_old) { | |
7365 | kfree(tmp_table_old, tmp_table_old_size); | |
7366 | tmp_table_old = NULL; | |
7367 | } | |
7368 | ||
0a7de745 | 7369 | return error; |
d9a64523 A |
7370 | } |
7371 | ||
7372 | static int | |
7373 | memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) | |
7374 | { | |
7375 | int error = 0; | |
7376 | ||
7377 | if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) == MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) { | |
d9a64523 | 7378 | error = memorystatus_cmd_grp_set_priorities(buffer, buffer_size); |
d9a64523 | 7379 | } else if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) == MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) { |
d9a64523 | 7380 | error = memorystatus_cmd_grp_set_probabilities(buffer, buffer_size); |
d9a64523 A |
7381 | } else { |
7382 | error = EINVAL; | |
7383 | } | |
7384 | ||
7385 | return error; | |
7386 | } | |
fe8ab488 A |
7387 | |
7388 | /* | |
3e170ce0 A |
7389 | * This routine is used to update a process's jetsam priority position and stored user_data. |
7390 | * It is not used for the setting of memory limits, which is why the last 6 args to the | |
7391 | * memorystatus_update() call are 0 or FALSE. | |
cb323159 A |
7392 | * |
7393 | * Flags passed into this call are used to distinguish the motivation behind a jetsam priority | |
7394 | * transition. By default, the kernel updates the process's original requested priority when | |
7395 | * no flag is passed. But when the MEMORYSTATUS_SET_PRIORITY_ASSERTION flag is used, the kernel | |
7396 | * updates the process's assertion driven priority. | |
7397 | * | |
7398 | * The assertion flag was introduced for use by the device's assertion mediator (eg: runningboardd). | |
7399 | * When an assertion is controlling a process's jetsam priority, it may conflict with that process's | |
7400 | * dirty/clean (active/inactive) jetsam state. The kernel attempts to resolve a priority transition | |
7401 | * conflict by reviewing the process state and then choosing the maximum jetsam band at play, | |
7402 | * eg: requested priority versus assertion priority. | |
fe8ab488 | 7403 | */ |
0a7de745 | 7404 | |
39236c6e | 7405 | static int |
cb323159 | 7406 | memorystatus_cmd_set_priority_properties(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
0a7de745 | 7407 | { |
3e170ce0 | 7408 | int error = 0; |
cb323159 | 7409 | boolean_t is_assertion = FALSE; /* priority is driven by an assertion */ |
3e170ce0 A |
7410 | memorystatus_priority_properties_t mpp_entry; |
7411 | ||
39236c6e | 7412 | /* Validate inputs */ |
3e170ce0 | 7413 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_properties_t))) { |
39236c6e A |
7414 | return EINVAL; |
7415 | } | |
0a7de745 | 7416 | |
cb323159 A |
7417 | /* Validate flags */ |
7418 | if (flags == 0) { | |
7419 | /* | |
7420 | * Default. This path updates requestedpriority. | |
7421 | */ | |
7422 | } else { | |
7423 | if (flags & ~(MEMORYSTATUS_SET_PRIORITY_ASSERTION)) { | |
7424 | /* | |
7425 | * Unsupported bit set in flag. | |
7426 | */ | |
7427 | return EINVAL; | |
7428 | } else if (flags & MEMORYSTATUS_SET_PRIORITY_ASSERTION) { | |
7429 | is_assertion = TRUE; | |
7430 | } | |
7431 | } | |
7432 | ||
3e170ce0 A |
7433 | error = copyin(buffer, &mpp_entry, buffer_size); |
7434 | ||
7435 | if (error == 0) { | |
39236c6e | 7436 | proc_t p; |
0a7de745 | 7437 | |
39236c6e A |
7438 | p = proc_find(pid); |
7439 | if (!p) { | |
3e170ce0 | 7440 | return ESRCH; |
39236c6e | 7441 | } |
0a7de745 | 7442 | |
39236c6e | 7443 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
39236c6e | 7444 | proc_rele(p); |
3e170ce0 | 7445 | return EPERM; |
39236c6e | 7446 | } |
0a7de745 | 7447 | |
cb323159 A |
7448 | if (is_assertion) { |
7449 | os_log(OS_LOG_DEFAULT, "memorystatus: set assertion priority(%d) target %s:%d\n", | |
7450 | mpp_entry.priority, (*p->p_name ? p->p_name : "unknown"), p->p_pid); | |
7451 | } | |
7452 | ||
7453 | error = memorystatus_update(p, mpp_entry.priority, mpp_entry.user_data, is_assertion, FALSE, FALSE, 0, 0, FALSE, FALSE); | |
39236c6e A |
7454 | proc_rele(p); |
7455 | } | |
0a7de745 A |
7456 | |
7457 | return error; | |
3e170ce0 A |
7458 | } |
7459 | ||
7460 | static int | |
0a7de745 A |
7461 | memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
7462 | { | |
3e170ce0 A |
7463 | int error = 0; |
7464 | memorystatus_memlimit_properties_t mmp_entry; | |
7465 | ||
7466 | /* Validate inputs */ | |
7467 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_memlimit_properties_t))) { | |
7468 | return EINVAL; | |
7469 | } | |
7470 | ||
7471 | error = copyin(buffer, &mmp_entry, buffer_size); | |
7472 | ||
7473 | if (error == 0) { | |
7474 | error = memorystatus_set_memlimit_properties(pid, &mmp_entry); | |
7475 | } | |
7476 | ||
0a7de745 | 7477 | return error; |
3e170ce0 A |
7478 | } |
7479 | ||
cb323159 A |
7480 | static void |
7481 | memorystatus_get_memlimit_properties_internal(proc_t p, memorystatus_memlimit_properties_t* p_entry) | |
7482 | { | |
7483 | memset(p_entry, 0, sizeof(memorystatus_memlimit_properties_t)); | |
7484 | ||
7485 | if (p->p_memstat_memlimit_active > 0) { | |
7486 | p_entry->memlimit_active = p->p_memstat_memlimit_active; | |
7487 | } else { | |
7488 | task_convert_phys_footprint_limit(-1, &p_entry->memlimit_active); | |
7489 | } | |
7490 | ||
7491 | if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { | |
7492 | p_entry->memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
7493 | } | |
7494 | ||
7495 | /* | |
7496 | * Get the inactive limit and attributes | |
7497 | */ | |
7498 | if (p->p_memstat_memlimit_inactive <= 0) { | |
7499 | task_convert_phys_footprint_limit(-1, &p_entry->memlimit_inactive); | |
7500 | } else { | |
7501 | p_entry->memlimit_inactive = p->p_memstat_memlimit_inactive; | |
7502 | } | |
7503 | if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { | |
7504 | p_entry->memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
7505 | } | |
7506 | } | |
7507 | ||
3e170ce0 A |
7508 | /* |
7509 | * When getting the memlimit settings, we can't simply call task_get_phys_footprint_limit(). | |
7510 | * That gets the proc's cached memlimit and there is no guarantee that the active/inactive | |
7511 | * limits will be the same in the no-limit case. Instead we convert limits <= 0 using | |
7512 | * task_convert_phys_footprint_limit(). It computes the same limit value that would be written | |
7513 | * to the task's ledgers via task_set_phys_footprint_limit(). | |
7514 | */ | |
7515 | static int | |
0a7de745 A |
7516 | memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
7517 | { | |
cb323159 | 7518 | memorystatus_memlimit_properties2_t mmp_entry; |
3e170ce0 A |
7519 | |
7520 | /* Validate inputs */ | |
cb323159 A |
7521 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || |
7522 | ((buffer_size != sizeof(memorystatus_memlimit_properties_t)) && | |
7523 | (buffer_size != sizeof(memorystatus_memlimit_properties2_t)))) { | |
3e170ce0 A |
7524 | return EINVAL; |
7525 | } | |
7526 | ||
cb323159 | 7527 | memset(&mmp_entry, 0, sizeof(memorystatus_memlimit_properties2_t)); |
3e170ce0 A |
7528 | |
7529 | proc_t p = proc_find(pid); | |
7530 | if (!p) { | |
7531 | return ESRCH; | |
7532 | } | |
7533 | ||
7534 | /* | |
7535 | * Get the active limit and attributes. | |
7536 | * No locks taken since we hold a reference to the proc. | |
7537 | */ | |
7538 | ||
cb323159 | 7539 | memorystatus_get_memlimit_properties_internal(p, &mmp_entry.v1); |
3e170ce0 | 7540 | |
cb323159 A |
7541 | #if CONFIG_JETSAM |
7542 | #if DEVELOPMENT || DEBUG | |
3e170ce0 | 7543 | /* |
cb323159 | 7544 | * Get the limit increased via SPI |
3e170ce0 | 7545 | */ |
cb323159 A |
7546 | mmp_entry.memlimit_increase = roundToNearestMB(p->p_memlimit_increase); |
7547 | mmp_entry.memlimit_increase_bytes = p->p_memlimit_increase; | |
7548 | #endif /* DEVELOPMENT || DEBUG */ | |
7549 | #endif /* CONFIG_JETSAM */ | |
7550 | ||
3e170ce0 A |
7551 | proc_rele(p); |
7552 | ||
cb323159 | 7553 | int error = copyout(&mmp_entry, buffer, buffer_size); |
3e170ce0 | 7554 | |
0a7de745 | 7555 | return error; |
b0d623f7 A |
7556 | } |
7557 | ||
3e170ce0 | 7558 | |
39037602 A |
7559 | /* |
7560 | * SPI for kbd - pr24956468 | |
7561 | * This is a very simple snapshot that calculates how much a | |
7562 | * process's phys_footprint exceeds a specific memory limit. | |
7563 | * Only the inactive memory limit is supported for now. | |
7564 | * The delta is returned as bytes in excess or zero. | |
7565 | */ | |
7566 | static int | |
0a7de745 A |
7567 | memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
7568 | { | |
39037602 A |
7569 | int error = 0; |
7570 | uint64_t footprint_in_bytes = 0; | |
7571 | uint64_t delta_in_bytes = 0; | |
7572 | int32_t memlimit_mb = 0; | |
7573 | uint64_t memlimit_bytes = 0; | |
7574 | ||
7575 | /* Validate inputs */ | |
7576 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(uint64_t)) || (flags != 0)) { | |
0a7de745 | 7577 | return EINVAL; |
39037602 A |
7578 | } |
7579 | ||
7580 | proc_t p = proc_find(pid); | |
7581 | if (!p) { | |
7582 | return ESRCH; | |
7583 | } | |
7584 | ||
7585 | /* | |
7586 | * Get the inactive limit. | |
7587 | * No locks taken since we hold a reference to the proc. | |
7588 | */ | |
7589 | ||
7590 | if (p->p_memstat_memlimit_inactive <= 0) { | |
7591 | task_convert_phys_footprint_limit(-1, &memlimit_mb); | |
7592 | } else { | |
7593 | memlimit_mb = p->p_memstat_memlimit_inactive; | |
7594 | } | |
7595 | ||
7596 | footprint_in_bytes = get_task_phys_footprint(p->task); | |
7597 | ||
7598 | proc_rele(p); | |
7599 | ||
0a7de745 | 7600 | memlimit_bytes = memlimit_mb * 1024 * 1024; /* MB to bytes */ |
39037602 A |
7601 | |
7602 | /* | |
7603 | * Computed delta always returns >= 0 bytes | |
7604 | */ | |
7605 | if (footprint_in_bytes > memlimit_bytes) { | |
7606 | delta_in_bytes = footprint_in_bytes - memlimit_bytes; | |
7607 | } | |
7608 | ||
7609 | error = copyout(&delta_in_bytes, buffer, sizeof(delta_in_bytes)); | |
7610 | ||
0a7de745 | 7611 | return error; |
39037602 A |
7612 | } |
7613 | ||
7614 | ||
39236c6e | 7615 | static int |
0a7de745 A |
7616 | memorystatus_cmd_get_pressure_status(int32_t *retval) |
7617 | { | |
39236c6e | 7618 | int error; |
0a7de745 | 7619 | |
39236c6e A |
7620 | /* Need privilege for check */ |
7621 | error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0); | |
7622 | if (error) { | |
0a7de745 | 7623 | return error; |
39236c6e | 7624 | } |
0a7de745 | 7625 | |
39236c6e A |
7626 | /* Inherently racy, so it's not worth taking a lock here */ |
7627 | *retval = (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0; | |
0a7de745 | 7628 | |
39236c6e A |
7629 | return error; |
7630 | } | |
316670eb | 7631 | |
3e170ce0 | 7632 | int |
0a7de745 A |
7633 | memorystatus_get_pressure_status_kdp() |
7634 | { | |
3e170ce0 A |
7635 | return (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0; |
7636 | } | |
7637 | ||
fe8ab488 A |
7638 | /* |
7639 | * Every process, including a P_MEMSTAT_INTERNAL process (currently only pid 1), is allowed to set a HWM. | |
3e170ce0 A |
7640 | * |
7641 | * This call is inflexible -- it does not distinguish between active/inactive, fatal/non-fatal | |
7642 | * So, with 2-level HWM preserving previous behavior will map as follows. | |
7643 | * - treat the limit passed in as both an active and inactive limit. | |
7644 | * - treat the is_fatal_limit flag as though it applies to both active and inactive limits. | |
7645 | * | |
7646 | * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK | |
7647 | * - the is_fatal_limit is FALSE, meaning the active and inactive limits are non-fatal/soft | |
7648 | * - so mapping is (active/non-fatal, inactive/non-fatal) | |
7649 | * | |
7650 | * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT | |
7651 | * - the is_fatal_limit is TRUE, meaning the process's active and inactive limits are fatal/hard | |
7652 | * - so mapping is (active/fatal, inactive/fatal) | |
fe8ab488 A |
7653 | */ |
7654 | ||
5ba3f43e | 7655 | #if CONFIG_JETSAM |
b0d623f7 | 7656 | static int |
0a7de745 A |
7657 | memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit) |
7658 | { | |
39236c6e | 7659 | int error = 0; |
3e170ce0 A |
7660 | memorystatus_memlimit_properties_t entry; |
7661 | ||
7662 | entry.memlimit_active = high_water_mark; | |
7663 | entry.memlimit_active_attr = 0; | |
7664 | entry.memlimit_inactive = high_water_mark; | |
7665 | entry.memlimit_inactive_attr = 0; | |
7666 | ||
7667 | if (is_fatal_limit == TRUE) { | |
7668 | entry.memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
7669 | entry.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
7670 | } | |
7671 | ||
7672 | error = memorystatus_set_memlimit_properties(pid, &entry); | |
0a7de745 | 7673 | return error; |
3e170ce0 | 7674 | } |
5ba3f43e | 7675 | #endif /* CONFIG_JETSAM */ |
3e170ce0 A |
7676 | |
7677 | static int | |
cb323159 | 7678 | memorystatus_set_memlimit_properties_internal(proc_t p, memorystatus_memlimit_properties_t *p_entry) |
0a7de745 | 7679 | { |
cb323159 | 7680 | int error = 0; |
3e170ce0 | 7681 | |
cb323159 | 7682 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); |
3e170ce0 A |
7683 | |
7684 | /* | |
7685 | * Store the active limit variants in the proc. | |
7686 | */ | |
cb323159 | 7687 | SET_ACTIVE_LIMITS_LOCKED(p, p_entry->memlimit_active, p_entry->memlimit_active_attr); |
3e170ce0 A |
7688 | |
7689 | /* | |
7690 | * Store the inactive limit variants in the proc. | |
7691 | */ | |
cb323159 | 7692 | SET_INACTIVE_LIMITS_LOCKED(p, p_entry->memlimit_inactive, p_entry->memlimit_inactive_attr); |
3e170ce0 A |
7693 | |
7694 | /* | |
7695 | * Enforce appropriate limit variant by updating the cached values | |
7696 | * and writing the ledger. | |
7697 | * Limit choice is based on process active/inactive state. | |
7698 | */ | |
7699 | ||
7700 | if (memorystatus_highwater_enabled) { | |
813fb2f6 A |
7701 | boolean_t is_fatal; |
7702 | boolean_t use_active; | |
3e170ce0 A |
7703 | |
7704 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { | |
813fb2f6 A |
7705 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); |
7706 | use_active = TRUE; | |
fe8ab488 | 7707 | } else { |
813fb2f6 A |
7708 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
7709 | use_active = FALSE; | |
fe8ab488 | 7710 | } |
3e170ce0 A |
7711 | |
7712 | /* Enforce the limit by writing to the ledgers */ | |
813fb2f6 | 7713 | 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; |
3e170ce0 A |
7714 | |
7715 | MEMORYSTATUS_DEBUG(3, "memorystatus_set_memlimit_properties: new limit on pid %d (%dMB %s) current priority (%d) dirty_state?=0x%x %s\n", | |
0a7de745 A |
7716 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), |
7717 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, | |
7718 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
39037602 | 7719 | DTRACE_MEMORYSTATUS2(memorystatus_set_memlimit, proc_t, p, int32_t, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1)); |
fe8ab488 A |
7720 | } |
7721 | ||
39236c6e A |
7722 | return error; |
7723 | } | |
7724 | ||
d9a64523 | 7725 | static int |
cb323159 | 7726 | memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry) |
d9a64523 | 7727 | { |
cb323159 | 7728 | memorystatus_memlimit_properties_t set_entry; |
d9a64523 | 7729 | |
cb323159 | 7730 | proc_t p = proc_find(pid); |
d9a64523 A |
7731 | if (!p) { |
7732 | return ESRCH; | |
7733 | } | |
7734 | ||
cb323159 A |
7735 | /* |
7736 | * Check for valid attribute flags. | |
7737 | */ | |
7738 | const uint32_t valid_attrs = MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
7739 | if ((entry->memlimit_active_attr & (~valid_attrs)) != 0) { | |
7740 | proc_rele(p); | |
7741 | return EINVAL; | |
7742 | } | |
7743 | if ((entry->memlimit_inactive_attr & (~valid_attrs)) != 0) { | |
7744 | proc_rele(p); | |
7745 | return EINVAL; | |
7746 | } | |
d9a64523 | 7747 | |
cb323159 A |
7748 | /* |
7749 | * Setup the active memlimit properties | |
7750 | */ | |
7751 | set_entry.memlimit_active = entry->memlimit_active; | |
7752 | set_entry.memlimit_active_attr = entry->memlimit_active_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
d9a64523 | 7753 | |
cb323159 A |
7754 | /* |
7755 | * Setup the inactive memlimit properties | |
7756 | */ | |
7757 | set_entry.memlimit_inactive = entry->memlimit_inactive; | |
7758 | set_entry.memlimit_inactive_attr = entry->memlimit_inactive_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
d9a64523 | 7759 | |
cb323159 A |
7760 | /* |
7761 | * Setting a limit of <= 0 implies that the process has no | |
7762 | * high-water-mark and has no per-task-limit. That means | |
7763 | * the system_wide task limit is in place, which by the way, | |
7764 | * is always fatal. | |
7765 | */ | |
d9a64523 | 7766 | |
cb323159 A |
7767 | if (set_entry.memlimit_active <= 0) { |
7768 | /* | |
7769 | * Enforce the fatal system_wide task limit while process is active. | |
7770 | */ | |
7771 | set_entry.memlimit_active = -1; | |
7772 | set_entry.memlimit_active_attr = MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
7773 | } | |
7774 | #if CONFIG_JETSAM | |
7775 | #if DEVELOPMENT || DEBUG | |
7776 | else { | |
7777 | /* add the current increase to it, for roots */ | |
7778 | set_entry.memlimit_active += roundToNearestMB(p->p_memlimit_increase); | |
d9a64523 | 7779 | } |
cb323159 A |
7780 | #endif /* DEVELOPMENT || DEBUG */ |
7781 | #endif /* CONFIG_JETSAM */ | |
d9a64523 | 7782 | |
cb323159 A |
7783 | if (set_entry.memlimit_inactive <= 0) { |
7784 | /* | |
7785 | * Enforce the fatal system_wide task limit while process is inactive. | |
7786 | */ | |
7787 | set_entry.memlimit_inactive = -1; | |
7788 | set_entry.memlimit_inactive_attr = MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
d9a64523 | 7789 | } |
cb323159 A |
7790 | #if CONFIG_JETSAM |
7791 | #if DEVELOPMENT || DEBUG | |
7792 | else { | |
7793 | /* add the current increase to it, for roots */ | |
7794 | set_entry.memlimit_inactive += roundToNearestMB(p->p_memlimit_increase); | |
7795 | } | |
7796 | #endif /* DEVELOPMENT || DEBUG */ | |
7797 | #endif /* CONFIG_JETSAM */ | |
7798 | ||
7799 | proc_list_lock(); | |
7800 | ||
7801 | int error = memorystatus_set_memlimit_properties_internal(p, &set_entry); | |
7802 | ||
d9a64523 | 7803 | proc_list_unlock(); |
cb323159 A |
7804 | proc_rele(p); |
7805 | ||
7806 | return error; | |
7807 | } | |
d9a64523 | 7808 | |
cb323159 A |
7809 | /* |
7810 | * Returns the jetsam priority (effective or requested) of the process | |
7811 | * associated with this task. | |
7812 | */ | |
7813 | int | |
7814 | proc_get_memstat_priority(proc_t p, boolean_t effective_priority) | |
7815 | { | |
7816 | if (p) { | |
7817 | if (effective_priority) { | |
7818 | return p->p_memstat_effectivepriority; | |
7819 | } else { | |
7820 | return p->p_memstat_requestedpriority; | |
7821 | } | |
7822 | } | |
d9a64523 A |
7823 | return 0; |
7824 | } | |
7825 | ||
7826 | static int | |
cb323159 | 7827 | memorystatus_get_process_is_managed(pid_t pid, int *is_managed) |
d9a64523 | 7828 | { |
cb323159 | 7829 | proc_t p = NULL; |
d9a64523 | 7830 | |
cb323159 | 7831 | /* Validate inputs */ |
d9a64523 A |
7832 | if (pid == 0) { |
7833 | return EINVAL; | |
7834 | } | |
7835 | ||
7836 | p = proc_find(pid); | |
7837 | if (!p) { | |
7838 | return ESRCH; | |
7839 | } | |
7840 | ||
d9a64523 | 7841 | proc_list_lock(); |
cb323159 | 7842 | *is_managed = ((p->p_memstat_state & P_MEMSTAT_MANAGED) ? 1 : 0); |
d9a64523 A |
7843 | proc_rele_locked(p); |
7844 | proc_list_unlock(); | |
7845 | ||
7846 | return 0; | |
7847 | } | |
7848 | ||
7849 | static int | |
cb323159 | 7850 | memorystatus_set_process_is_managed(pid_t pid, boolean_t set_managed) |
d9a64523 | 7851 | { |
cb323159 | 7852 | proc_t p = NULL; |
d9a64523 | 7853 | |
cb323159 | 7854 | /* Validate inputs */ |
d9a64523 A |
7855 | if (pid == 0) { |
7856 | return EINVAL; | |
7857 | } | |
7858 | ||
7859 | p = proc_find(pid); | |
7860 | if (!p) { | |
7861 | return ESRCH; | |
7862 | } | |
7863 | ||
d9a64523 | 7864 | proc_list_lock(); |
cb323159 A |
7865 | if (set_managed == TRUE) { |
7866 | p->p_memstat_state |= P_MEMSTAT_MANAGED; | |
7867 | /* | |
7868 | * The P_MEMSTAT_MANAGED bit is set by assertiond for Apps. | |
7869 | * Also opt them in to being frozen (they might have started | |
7870 | * off with the P_MEMSTAT_FREEZE_DISABLED bit set.) | |
7871 | */ | |
d9a64523 | 7872 | p->p_memstat_state &= ~P_MEMSTAT_FREEZE_DISABLED; |
cb323159 A |
7873 | } else { |
7874 | p->p_memstat_state &= ~P_MEMSTAT_MANAGED; | |
d9a64523 A |
7875 | } |
7876 | proc_rele_locked(p); | |
7877 | proc_list_unlock(); | |
7878 | ||
7879 | return 0; | |
7880 | } | |
7881 | ||
39236c6e | 7882 | int |
0a7de745 A |
7883 | memorystatus_control(struct proc *p __unused, struct memorystatus_control_args *args, int *ret) |
7884 | { | |
39236c6e | 7885 | int error = EINVAL; |
d9a64523 | 7886 | boolean_t skip_auth_check = FALSE; |
39037602 | 7887 | os_reason_t jetsam_reason = OS_REASON_NULL; |
39236c6e A |
7888 | |
7889 | #if !CONFIG_JETSAM | |
cb323159 A |
7890 | #pragma unused(ret) |
7891 | #pragma unused(jetsam_reason) | |
39236c6e A |
7892 | #endif |
7893 | ||
2a1bd2d3 A |
7894 | /* We don't need entitlements if we're setting / querying the freeze preference or frozen status for a process. */ |
7895 | if (args->command == MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE || | |
7896 | args->command == MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE || | |
7897 | args->command == MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN) { | |
d9a64523 A |
7898 | skip_auth_check = TRUE; |
7899 | } | |
7900 | ||
7901 | /* Need to be root or have entitlement. */ | |
7902 | if (!kauth_cred_issuser(kauth_cred_get()) && !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT) && !skip_auth_check) { | |
39236c6e A |
7903 | error = EPERM; |
7904 | goto out; | |
b0d623f7 | 7905 | } |
39037602 A |
7906 | |
7907 | /* | |
7908 | * Sanity check. | |
7909 | * Do not enforce it for snapshots. | |
7910 | */ | |
7911 | if (args->command != MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT) { | |
7912 | if (args->buffersize > MEMORYSTATUS_BUFFERSIZE_MAX) { | |
7913 | error = EINVAL; | |
7914 | goto out; | |
7915 | } | |
39236c6e A |
7916 | } |
7917 | ||
7918 | switch (args->command) { | |
7919 | case MEMORYSTATUS_CMD_GET_PRIORITY_LIST: | |
5ba3f43e | 7920 | error = memorystatus_cmd_get_priority_list(args->pid, args->buffer, args->buffersize, ret); |
39236c6e | 7921 | break; |
39236c6e | 7922 | case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES: |
cb323159 | 7923 | error = memorystatus_cmd_set_priority_properties(args->pid, args->flags, args->buffer, args->buffersize, ret); |
39236c6e | 7924 | break; |
3e170ce0 A |
7925 | case MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES: |
7926 | error = memorystatus_cmd_set_memlimit_properties(args->pid, args->buffer, args->buffersize, ret); | |
7927 | break; | |
7928 | case MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES: | |
7929 | error = memorystatus_cmd_get_memlimit_properties(args->pid, args->buffer, args->buffersize, ret); | |
7930 | break; | |
39037602 A |
7931 | case MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS: |
7932 | error = memorystatus_cmd_get_memlimit_excess_np(args->pid, args->flags, args->buffer, args->buffersize, ret); | |
7933 | break; | |
fe8ab488 A |
7934 | case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES: |
7935 | error = memorystatus_cmd_grp_set_properties((int32_t)args->flags, args->buffer, args->buffersize, ret); | |
0a7de745 | 7936 | break; |
39236c6e | 7937 | case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT: |
3e170ce0 | 7938 | error = memorystatus_cmd_get_jetsam_snapshot((int32_t)args->flags, args->buffer, args->buffersize, ret); |
39236c6e | 7939 | break; |
f427ee49 A |
7940 | #if DEVELOPMENT || DEBUG |
7941 | case MEMORYSTATUS_CMD_SET_JETSAM_SNAPSHOT_OWNERSHIP: | |
7942 | error = memorystatus_cmd_set_jetsam_snapshot_ownership((int32_t) args->flags); | |
7943 | break; | |
7944 | #endif | |
39236c6e A |
7945 | case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS: |
7946 | error = memorystatus_cmd_get_pressure_status(ret); | |
7947 | break; | |
5ba3f43e | 7948 | #if CONFIG_JETSAM |
39236c6e | 7949 | case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK: |
3e170ce0 A |
7950 | /* |
7951 | * This call does not distinguish between active and inactive limits. | |
7952 | * Default behavior in 2-level HWM world is to set both. | |
7953 | * Non-fatal limit is also assumed for both. | |
7954 | */ | |
fe8ab488 A |
7955 | error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, FALSE); |
7956 | break; | |
7957 | case MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT: | |
3e170ce0 A |
7958 | /* |
7959 | * This call does not distinguish between active and inactive limits. | |
7960 | * Default behavior in 2-level HWM world is to set both. | |
7961 | * Fatal limit is also assumed for both. | |
7962 | */ | |
fe8ab488 | 7963 | error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, TRUE); |
39236c6e | 7964 | break; |
5ba3f43e | 7965 | #endif /* CONFIG_JETSAM */ |
0a7de745 | 7966 | /* Test commands */ |
39236c6e A |
7967 | #if DEVELOPMENT || DEBUG |
7968 | case MEMORYSTATUS_CMD_TEST_JETSAM: | |
39037602 A |
7969 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_GENERIC); |
7970 | if (jetsam_reason == OS_REASON_NULL) { | |
7971 | printf("memorystatus_control: failed to allocate jetsam reason\n"); | |
7972 | } | |
7973 | ||
7974 | error = memorystatus_kill_process_sync(args->pid, kMemorystatusKilled, jetsam_reason) ? 0 : EINVAL; | |
39236c6e | 7975 | break; |
3e170ce0 | 7976 | case MEMORYSTATUS_CMD_TEST_JETSAM_SORT: |
f427ee49 | 7977 | error = memorystatus_cmd_test_jetsam_sort(args->pid, (int32_t)args->flags, args->buffer, args->buffersize); |
3e170ce0 | 7978 | break; |
5ba3f43e | 7979 | #if CONFIG_JETSAM |
39236c6e A |
7980 | case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS: |
7981 | error = memorystatus_cmd_set_panic_bits(args->buffer, args->buffersize); | |
7982 | break; | |
5ba3f43e | 7983 | #endif /* CONFIG_JETSAM */ |
39037602 A |
7984 | #else /* DEVELOPMENT || DEBUG */ |
7985 | #pragma unused(jetsam_reason) | |
39236c6e | 7986 | #endif /* DEVELOPMENT || DEBUG */ |
490019cf A |
7987 | case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_ENABLE: |
7988 | if (memorystatus_aggressive_jetsam_lenient_allowed == FALSE) { | |
7989 | #if DEVELOPMENT || DEBUG | |
7990 | printf("Enabling Lenient Mode\n"); | |
7991 | #endif /* DEVELOPMENT || DEBUG */ | |
7992 | ||
7993 | memorystatus_aggressive_jetsam_lenient_allowed = TRUE; | |
7994 | memorystatus_aggressive_jetsam_lenient = TRUE; | |
39037602 | 7995 | error = 0; |
490019cf A |
7996 | } |
7997 | break; | |
7998 | case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE: | |
7999 | #if DEVELOPMENT || DEBUG | |
8000 | printf("Disabling Lenient mode\n"); | |
8001 | #endif /* DEVELOPMENT || DEBUG */ | |
8002 | memorystatus_aggressive_jetsam_lenient_allowed = FALSE; | |
8003 | memorystatus_aggressive_jetsam_lenient = FALSE; | |
39037602 | 8004 | error = 0; |
490019cf | 8005 | break; |
cb323159 A |
8006 | case MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE: |
8007 | *ret = (memorystatus_aggressive_jetsam_lenient ? 1 : 0); | |
8008 | error = 0; | |
8009 | break; | |
3e170ce0 A |
8010 | case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE: |
8011 | case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE: | |
8012 | error = memorystatus_low_mem_privileged_listener(args->command); | |
8013 | break; | |
39037602 | 8014 | |
39037602 A |
8015 | case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE: |
8016 | case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE: | |
d9a64523 A |
8017 | error = memorystatus_update_inactive_jetsam_priority_band(args->pid, args->command, JETSAM_PRIORITY_ELEVATED_INACTIVE, args->flags ? TRUE : FALSE); |
8018 | break; | |
8019 | case MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED: | |
8020 | error = memorystatus_set_process_is_managed(args->pid, args->flags); | |
39037602 | 8021 | break; |
39037602 | 8022 | |
d9a64523 A |
8023 | case MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED: |
8024 | error = memorystatus_get_process_is_managed(args->pid, ret); | |
8025 | break; | |
8026 | ||
cb323159 | 8027 | #if CONFIG_FREEZE |
d9a64523 A |
8028 | case MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE: |
8029 | error = memorystatus_set_process_is_freezable(args->pid, args->flags ? TRUE : FALSE); | |
8030 | break; | |
8031 | ||
8032 | case MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE: | |
8033 | error = memorystatus_get_process_is_freezable(args->pid, ret); | |
8034 | break; | |
2a1bd2d3 A |
8035 | case MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN: |
8036 | error = memorystatus_get_process_is_frozen(args->pid, ret); | |
8037 | break; | |
d9a64523 | 8038 | |
d9a64523 A |
8039 | case MEMORYSTATUS_CMD_FREEZER_CONTROL: |
8040 | error = memorystatus_freezer_control(args->flags, args->buffer, args->buffersize, ret); | |
8041 | break; | |
d9a64523 A |
8042 | #endif /* CONFIG_FREEZE */ |
8043 | ||
cb323159 A |
8044 | #if CONFIG_JETSAM |
8045 | #if DEVELOPMENT || DEBUG | |
8046 | case MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT: | |
8047 | error = memorystatus_cmd_increase_jetsam_task_limit(args->pid, args->flags); | |
8048 | break; | |
f427ee49 | 8049 | #endif /* DEVELOPMENT || DEBUG */ |
cb323159 A |
8050 | #endif /* CONFIG_JETSAM */ |
8051 | ||
39236c6e A |
8052 | default: |
8053 | break; | |
8054 | } | |
8055 | ||
8056 | out: | |
8057 | return error; | |
8058 | } | |
8059 | ||
3e170ce0 A |
8060 | /* Coalition support */ |
8061 | ||
8062 | /* sorting info for a particular priority bucket */ | |
8063 | typedef struct memstat_sort_info { | |
0a7de745 A |
8064 | coalition_t msi_coal; |
8065 | uint64_t msi_page_count; | |
8066 | pid_t msi_pid; | |
8067 | int msi_ntasks; | |
3e170ce0 A |
8068 | } memstat_sort_info_t; |
8069 | ||
0a7de745 | 8070 | /* |
3e170ce0 A |
8071 | * qsort from smallest page count to largest page count |
8072 | * | |
8073 | * return < 0 for a < b | |
8074 | * 0 for a == b | |
8075 | * > 0 for a > b | |
8076 | */ | |
0a7de745 A |
8077 | static int |
8078 | memstat_asc_cmp(const void *a, const void *b) | |
3e170ce0 | 8079 | { |
0a7de745 A |
8080 | const memstat_sort_info_t *msA = (const memstat_sort_info_t *)a; |
8081 | const memstat_sort_info_t *msB = (const memstat_sort_info_t *)b; | |
3e170ce0 | 8082 | |
0a7de745 | 8083 | return (int)((uint64_t)msA->msi_page_count - (uint64_t)msB->msi_page_count); |
3e170ce0 A |
8084 | } |
8085 | ||
8086 | /* | |
8087 | * Return the number of pids rearranged during this sort. | |
8088 | */ | |
8089 | static int | |
8090 | memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order) | |
8091 | { | |
0a7de745 A |
8092 | #define MAX_SORT_PIDS 80 |
8093 | #define MAX_COAL_LEADERS 10 | |
3e170ce0 A |
8094 | |
8095 | unsigned int b = bucket_index; | |
8096 | int nleaders = 0; | |
8097 | int ntasks = 0; | |
8098 | proc_t p = NULL; | |
8099 | coalition_t coal = COALITION_NULL; | |
8100 | int pids_moved = 0; | |
8101 | int total_pids_moved = 0; | |
8102 | int i; | |
8103 | ||
0a7de745 | 8104 | /* |
3e170ce0 A |
8105 | * The system is typically under memory pressure when in this |
8106 | * path, hence, we want to avoid dynamic memory allocation. | |
8107 | */ | |
8108 | memstat_sort_info_t leaders[MAX_COAL_LEADERS]; | |
8109 | pid_t pid_list[MAX_SORT_PIDS]; | |
8110 | ||
8111 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
0a7de745 A |
8112 | return 0; |
8113 | } | |
3e170ce0 A |
8114 | |
8115 | /* | |
8116 | * Clear the array that holds coalition leader information | |
8117 | */ | |
0a7de745 | 8118 | for (i = 0; i < MAX_COAL_LEADERS; i++) { |
3e170ce0 | 8119 | leaders[i].msi_coal = COALITION_NULL; |
0a7de745 A |
8120 | leaders[i].msi_page_count = 0; /* will hold total coalition page count */ |
8121 | leaders[i].msi_pid = 0; /* will hold coalition leader pid */ | |
8122 | leaders[i].msi_ntasks = 0; /* will hold the number of tasks in a coalition */ | |
3e170ce0 A |
8123 | } |
8124 | ||
0a7de745 A |
8125 | p = memorystatus_get_first_proc_locked(&b, FALSE); |
8126 | while (p) { | |
cb323159 A |
8127 | coal = task_get_coalition(p->task, COALITION_TYPE_JETSAM); |
8128 | if (coalition_is_leader(p->task, coal)) { | |
3e170ce0 A |
8129 | if (nleaders < MAX_COAL_LEADERS) { |
8130 | int coal_ntasks = 0; | |
8131 | uint64_t coal_page_count = coalition_get_page_count(coal, &coal_ntasks); | |
8132 | leaders[nleaders].msi_coal = coal; | |
8133 | leaders[nleaders].msi_page_count = coal_page_count; | |
0a7de745 | 8134 | leaders[nleaders].msi_pid = p->p_pid; /* the coalition leader */ |
3e170ce0 A |
8135 | leaders[nleaders].msi_ntasks = coal_ntasks; |
8136 | nleaders++; | |
8137 | } else { | |
0a7de745 | 8138 | /* |
3e170ce0 | 8139 | * We've hit MAX_COAL_LEADERS meaning we can handle no more coalitions. |
0a7de745 | 8140 | * Abandoned coalitions will linger at the tail of the priority band |
3e170ce0 A |
8141 | * when this sort session ends. |
8142 | * TODO: should this be an assert? | |
8143 | */ | |
8144 | printf("%s: WARNING: more than %d leaders in priority band [%d]\n", | |
0a7de745 | 8145 | __FUNCTION__, MAX_COAL_LEADERS, bucket_index); |
3e170ce0 A |
8146 | break; |
8147 | } | |
0a7de745 A |
8148 | } |
8149 | p = memorystatus_get_next_proc_locked(&b, p, FALSE); | |
8150 | } | |
3e170ce0 A |
8151 | |
8152 | if (nleaders == 0) { | |
8153 | /* Nothing to sort */ | |
0a7de745 | 8154 | return 0; |
3e170ce0 A |
8155 | } |
8156 | ||
0a7de745 | 8157 | /* |
3e170ce0 A |
8158 | * Sort the coalition leader array, from smallest coalition page count |
8159 | * to largest coalition page count. When inserted in the priority bucket, | |
8160 | * smallest coalition is handled first, resulting in the last to be jetsammed. | |
8161 | */ | |
8162 | if (nleaders > 1) { | |
8163 | qsort(leaders, nleaders, sizeof(memstat_sort_info_t), memstat_asc_cmp); | |
8164 | } | |
8165 | ||
8166 | #if 0 | |
8167 | for (i = 0; i < nleaders; i++) { | |
8168 | printf("%s: coal_leader[%d of %d] pid[%d] pages[%llu] ntasks[%d]\n", | |
0a7de745 A |
8169 | __FUNCTION__, i, nleaders, leaders[i].msi_pid, leaders[i].msi_page_count, |
8170 | leaders[i].msi_ntasks); | |
3e170ce0 A |
8171 | } |
8172 | #endif | |
8173 | ||
8174 | /* | |
8175 | * During coalition sorting, processes in a priority band are rearranged | |
8176 | * by being re-inserted at the head of the queue. So, when handling a | |
8177 | * list, the first process that gets moved to the head of the queue, | |
8178 | * ultimately gets pushed toward the queue tail, and hence, jetsams last. | |
8179 | * | |
8180 | * So, for example, the coalition leader is expected to jetsam last, | |
8181 | * after its coalition members. Therefore, the coalition leader is | |
8182 | * inserted at the head of the queue first. | |
8183 | * | |
8184 | * After processing a coalition, the jetsam order is as follows: | |
8185 | * undefs(jetsam first), extensions, xpc services, leader(jetsam last) | |
8186 | */ | |
8187 | ||
8188 | /* | |
8189 | * Coalition members are rearranged in the priority bucket here, | |
8190 | * based on their coalition role. | |
8191 | */ | |
8192 | total_pids_moved = 0; | |
0a7de745 | 8193 | for (i = 0; i < nleaders; i++) { |
3e170ce0 A |
8194 | /* a bit of bookkeeping */ |
8195 | pids_moved = 0; | |
8196 | ||
8197 | /* Coalition leaders are jetsammed last, so move into place first */ | |
8198 | pid_list[0] = leaders[i].msi_pid; | |
8199 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, 1); | |
8200 | ||
8201 | /* xpc services should jetsam after extensions */ | |
0a7de745 A |
8202 | ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_XPC, |
8203 | coal_sort_order, pid_list, MAX_SORT_PIDS); | |
3e170ce0 A |
8204 | |
8205 | if (ntasks > 0) { | |
0a7de745 A |
8206 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, |
8207 | (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); | |
3e170ce0 A |
8208 | } |
8209 | ||
8210 | /* extensions should jetsam after unmarked processes */ | |
0a7de745 A |
8211 | ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_EXT, |
8212 | coal_sort_order, pid_list, MAX_SORT_PIDS); | |
3e170ce0 A |
8213 | |
8214 | if (ntasks > 0) { | |
8215 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, | |
0a7de745 | 8216 | (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); |
3e170ce0 A |
8217 | } |
8218 | ||
8219 | /* undefined coalition members should be the first to jetsam */ | |
0a7de745 A |
8220 | ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_UNDEF, |
8221 | coal_sort_order, pid_list, MAX_SORT_PIDS); | |
3e170ce0 A |
8222 | |
8223 | if (ntasks > 0) { | |
0a7de745 A |
8224 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, |
8225 | (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); | |
3e170ce0 A |
8226 | } |
8227 | ||
8228 | #if 0 | |
8229 | if (pids_moved == leaders[i].msi_ntasks) { | |
8230 | /* | |
8231 | * All the pids in the coalition were found in this band. | |
8232 | */ | |
8233 | printf("%s: pids_moved[%d] equal total coalition ntasks[%d] \n", __FUNCTION__, | |
0a7de745 | 8234 | pids_moved, leaders[i].msi_ntasks); |
3e170ce0 A |
8235 | } else if (pids_moved > leaders[i].msi_ntasks) { |
8236 | /* | |
8237 | * Apparently new coalition members showed up during the sort? | |
8238 | */ | |
8239 | printf("%s: pids_moved[%d] were greater than expected coalition ntasks[%d] \n", __FUNCTION__, | |
0a7de745 | 8240 | pids_moved, leaders[i].msi_ntasks); |
3e170ce0 A |
8241 | } else { |
8242 | /* | |
8243 | * Apparently not all the pids in the coalition were found in this band? | |
8244 | */ | |
8245 | printf("%s: pids_moved[%d] were less than expected coalition ntasks[%d] \n", __FUNCTION__, | |
0a7de745 | 8246 | pids_moved, leaders[i].msi_ntasks); |
3e170ce0 A |
8247 | } |
8248 | #endif | |
8249 | ||
8250 | total_pids_moved += pids_moved; | |
3e170ce0 A |
8251 | } /* end for */ |
8252 | ||
0a7de745 | 8253 | return total_pids_moved; |
3e170ce0 A |
8254 | } |
8255 | ||
8256 | ||
8257 | /* | |
8258 | * Traverse a list of pids, searching for each within the priority band provided. | |
8259 | * If pid is found, move it to the front of the priority band. | |
8260 | * Never searches outside the priority band provided. | |
0a7de745 | 8261 | * |
3e170ce0 A |
8262 | * Input: |
8263 | * bucket_index - jetsam priority band. | |
8264 | * pid_list - pointer to a list of pids. | |
8265 | * list_sz - number of pids in the list. | |
8266 | * | |
0a7de745 | 8267 | * Pid list ordering is important in that, |
3e170ce0 A |
8268 | * pid_list[n] is expected to jetsam ahead of pid_list[n+1]. |
8269 | * The sort_order is set by the coalition default. | |
8270 | * | |
0a7de745 | 8271 | * Return: |
3e170ce0 A |
8272 | * the number of pids found and hence moved within the priority band. |
8273 | */ | |
8274 | static int | |
8275 | memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int list_sz) | |
8276 | { | |
8277 | memstat_bucket_t *current_bucket; | |
8278 | int i; | |
8279 | int found_pids = 0; | |
8280 | ||
8281 | if ((pid_list == NULL) || (list_sz <= 0)) { | |
0a7de745 | 8282 | return 0; |
3e170ce0 A |
8283 | } |
8284 | ||
8285 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
0a7de745 A |
8286 | return 0; |
8287 | } | |
3e170ce0 A |
8288 | |
8289 | current_bucket = &memstat_bucket[bucket_index]; | |
0a7de745 | 8290 | for (i = 0; i < list_sz; i++) { |
3e170ce0 A |
8291 | unsigned int b = bucket_index; |
8292 | proc_t p = NULL; | |
8293 | proc_t aProc = NULL; | |
8294 | pid_t aPid; | |
8295 | int list_index; | |
8296 | ||
8297 | list_index = ((list_sz - 1) - i); | |
0a7de745 A |
8298 | aPid = pid_list[list_index]; |
8299 | ||
8300 | /* never search beyond bucket_index provided */ | |
8301 | p = memorystatus_get_first_proc_locked(&b, FALSE); | |
8302 | while (p) { | |
8303 | if (p->p_pid == aPid) { | |
8304 | aProc = p; | |
8305 | break; | |
8306 | } | |
8307 | p = memorystatus_get_next_proc_locked(&b, p, FALSE); | |
8308 | } | |
8309 | ||
8310 | if (aProc == NULL) { | |
3e170ce0 | 8311 | /* pid not found in this band, just skip it */ |
0a7de745 A |
8312 | continue; |
8313 | } else { | |
8314 | TAILQ_REMOVE(¤t_bucket->list, aProc, p_memstat_list); | |
8315 | TAILQ_INSERT_HEAD(¤t_bucket->list, aProc, p_memstat_list); | |
3e170ce0 | 8316 | found_pids++; |
0a7de745 A |
8317 | } |
8318 | } | |
8319 | return found_pids; | |
3e170ce0 | 8320 | } |
5ba3f43e A |
8321 | |
8322 | int | |
8323 | memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index) | |
8324 | { | |
0a7de745 | 8325 | int32_t i = JETSAM_PRIORITY_IDLE; |
5ba3f43e A |
8326 | int count = 0; |
8327 | ||
8328 | if (max_bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
0a7de745 A |
8329 | return -1; |
8330 | } | |
5ba3f43e | 8331 | |
0a7de745 | 8332 | while (i <= max_bucket_index) { |
5ba3f43e A |
8333 | count += memstat_bucket[i++].count; |
8334 | } | |
8335 | ||
8336 | return count; | |
8337 | } | |
8338 | ||
8339 | int | |
8340 | memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap) | |
8341 | { | |
8342 | #if !CONFIG_JETSAM | |
d9a64523 | 8343 | if (!p || (!isApp(p)) || (p->p_memstat_state & (P_MEMSTAT_INTERNAL | P_MEMSTAT_MANAGED))) { |
5ba3f43e A |
8344 | /* |
8345 | * Ineligible processes OR system processes e.g. launchd. | |
d9a64523 A |
8346 | * |
8347 | * We also skip processes that have the P_MEMSTAT_MANAGED bit set, i.e. | |
8348 | * they're managed by assertiond. These are iOS apps that have been ported | |
8349 | * to macOS. assertiond might be in the process of modifying the app's | |
8350 | * priority / memory limit - so it might have the proc_list lock, and then try | |
8351 | * to take the task lock. Meanwhile we've entered this function with the task lock | |
8352 | * held, and we need the proc_list lock below. So we'll deadlock with assertiond. | |
8353 | * | |
8354 | * It should be fine to read the P_MEMSTAT_MANAGED bit without the proc_list | |
8355 | * lock here, since assertiond only sets this bit on process launch. | |
5ba3f43e A |
8356 | */ |
8357 | return -1; | |
8358 | } | |
8359 | ||
8360 | /* | |
8361 | * For macOS only: | |
8362 | * We would like to use memorystatus_update() here to move the processes | |
8363 | * within the bands. Unfortunately memorystatus_update() calls | |
8364 | * memorystatus_update_priority_locked() which uses any band transitions | |
8365 | * as an indication to modify ledgers. For that it needs the task lock | |
8366 | * and since we came into this function with the task lock held, we'll deadlock. | |
8367 | * | |
0a7de745 | 8368 | * Unfortunately we can't completely disable ledger updates because we still |
5ba3f43e A |
8369 | * need the ledger updates for a subset of processes i.e. daemons. |
8370 | * When all processes on all platforms support memory limits, we can simply call | |
8371 | * memorystatus_update(). | |
0a7de745 | 8372 | * |
5ba3f43e A |
8373 | * It also has some logic to deal with 'aging' which, currently, is only applicable |
8374 | * on CONFIG_JETSAM configs. So, till every platform has CONFIG_JETSAM we'll need | |
8375 | * to do this explicit band transition. | |
8376 | */ | |
8377 | ||
8378 | memstat_bucket_t *current_bucket, *new_bucket; | |
0a7de745 | 8379 | int32_t priority = 0; |
5ba3f43e A |
8380 | |
8381 | proc_list_lock(); | |
8382 | ||
8383 | if (((p->p_listflag & P_LIST_EXITED) != 0) || | |
8384 | (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED))) { | |
8385 | /* | |
8386 | * If the process is on its way out OR | |
8387 | * jetsam has alread tried and failed to kill this process, | |
8388 | * let's skip the whole jetsam band transition. | |
8389 | */ | |
8390 | proc_list_unlock(); | |
0a7de745 | 8391 | return 0; |
5ba3f43e A |
8392 | } |
8393 | ||
8394 | if (is_appnap) { | |
8395 | current_bucket = &memstat_bucket[p->p_memstat_effectivepriority]; | |
8396 | new_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
8397 | priority = JETSAM_PRIORITY_IDLE; | |
8398 | } else { | |
8399 | if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) { | |
8400 | /* | |
8401 | * It is possible that someone pulled this process | |
8402 | * out of the IDLE band without updating its app-nap | |
8403 | * parameters. | |
8404 | */ | |
8405 | proc_list_unlock(); | |
0a7de745 | 8406 | return 0; |
5ba3f43e A |
8407 | } |
8408 | ||
8409 | current_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
8410 | new_bucket = &memstat_bucket[p->p_memstat_requestedpriority]; | |
8411 | priority = p->p_memstat_requestedpriority; | |
8412 | } | |
8413 | ||
8414 | TAILQ_REMOVE(¤t_bucket->list, p, p_memstat_list); | |
8415 | current_bucket->count--; | |
cb323159 A |
8416 | if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) { |
8417 | current_bucket->relaunch_high_count--; | |
8418 | } | |
5ba3f43e A |
8419 | TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list); |
8420 | new_bucket->count++; | |
cb323159 A |
8421 | if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) { |
8422 | new_bucket->relaunch_high_count++; | |
8423 | } | |
5ba3f43e A |
8424 | /* |
8425 | * Record idle start or idle delta. | |
8426 | */ | |
8427 | if (p->p_memstat_effectivepriority == priority) { | |
0a7de745 | 8428 | /* |
5ba3f43e A |
8429 | * This process is not transitioning between |
8430 | * jetsam priority buckets. Do nothing. | |
8431 | */ | |
8432 | } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { | |
8433 | uint64_t now; | |
8434 | /* | |
8435 | * Transitioning out of the idle priority bucket. | |
8436 | * Record idle delta. | |
8437 | */ | |
8438 | assert(p->p_memstat_idle_start != 0); | |
8439 | now = mach_absolute_time(); | |
8440 | if (now > p->p_memstat_idle_start) { | |
8441 | p->p_memstat_idle_delta = now - p->p_memstat_idle_start; | |
8442 | } | |
8443 | } else if (priority == JETSAM_PRIORITY_IDLE) { | |
8444 | /* | |
8445 | * Transitioning into the idle priority bucket. | |
8446 | * Record idle start. | |
8447 | */ | |
8448 | p->p_memstat_idle_start = mach_absolute_time(); | |
8449 | } | |
8450 | ||
d9a64523 A |
8451 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CHANGE_PRIORITY), p->p_pid, priority, p->p_memstat_effectivepriority, 0, 0); |
8452 | ||
5ba3f43e A |
8453 | p->p_memstat_effectivepriority = priority; |
8454 | ||
8455 | proc_list_unlock(); | |
8456 | ||
0a7de745 | 8457 | return 0; |
5ba3f43e A |
8458 | |
8459 | #else /* !CONFIG_JETSAM */ | |
8460 | #pragma unused(p) | |
8461 | #pragma unused(is_appnap) | |
8462 | return -1; | |
8463 | #endif /* !CONFIG_JETSAM */ | |
8464 | } | |
cb323159 A |
8465 | |
8466 | uint64_t | |
f427ee49 | 8467 | memorystatus_available_memory_internal(struct proc *p) |
cb323159 A |
8468 | { |
8469 | #ifdef XNU_TARGET_OS_OSX | |
f427ee49 A |
8470 | if (p->p_memstat_memlimit <= 0) { |
8471 | return 0; | |
8472 | } | |
8473 | #endif /* XNU_TARGET_OS_OSX */ | |
cb323159 A |
8474 | const uint64_t footprint_in_bytes = get_task_phys_footprint(p->task); |
8475 | int32_t memlimit_mb; | |
8476 | int64_t memlimit_bytes; | |
8477 | int64_t rc; | |
8478 | ||
8479 | if (isApp(p) == FALSE) { | |
8480 | return 0; | |
8481 | } | |
8482 | ||
8483 | if (p->p_memstat_memlimit > 0) { | |
8484 | memlimit_mb = p->p_memstat_memlimit; | |
8485 | } else if (task_convert_phys_footprint_limit(-1, &memlimit_mb) != KERN_SUCCESS) { | |
8486 | return 0; | |
8487 | } | |
8488 | ||
8489 | if (memlimit_mb <= 0) { | |
8490 | memlimit_bytes = INT_MAX & ~((1 << 20) - 1); | |
8491 | } else { | |
8492 | memlimit_bytes = ((int64_t) memlimit_mb) << 20; | |
8493 | } | |
8494 | ||
8495 | rc = memlimit_bytes - footprint_in_bytes; | |
8496 | ||
8497 | return (rc >= 0) ? rc : 0; | |
cb323159 A |
8498 | } |
8499 | ||
8500 | int | |
8501 | memorystatus_available_memory(struct proc *p, __unused struct memorystatus_available_memory_args *args, uint64_t *ret) | |
8502 | { | |
8503 | *ret = memorystatus_available_memory_internal(p); | |
8504 | ||
8505 | return 0; | |
8506 | } | |
8507 | ||
8508 | #if CONFIG_JETSAM | |
8509 | #if DEVELOPMENT || DEBUG | |
8510 | static int | |
8511 | memorystatus_cmd_increase_jetsam_task_limit(pid_t pid, uint32_t byte_increase) | |
8512 | { | |
8513 | memorystatus_memlimit_properties_t mmp_entry; | |
8514 | ||
8515 | /* Validate inputs */ | |
8516 | if ((pid == 0) || (byte_increase == 0)) { | |
8517 | return EINVAL; | |
8518 | } | |
8519 | ||
8520 | proc_t p = proc_find(pid); | |
8521 | ||
8522 | if (!p) { | |
8523 | return ESRCH; | |
8524 | } | |
8525 | ||
8526 | const uint32_t current_memlimit_increase = roundToNearestMB(p->p_memlimit_increase); | |
f427ee49 A |
8527 | /* round to page */ |
8528 | const int32_t page_aligned_increase = (int32_t) MIN(round_page(p->p_memlimit_increase + byte_increase), INT32_MAX); | |
cb323159 A |
8529 | |
8530 | proc_list_lock(); | |
8531 | ||
8532 | memorystatus_get_memlimit_properties_internal(p, &mmp_entry); | |
8533 | ||
8534 | if (mmp_entry.memlimit_active > 0) { | |
8535 | mmp_entry.memlimit_active -= current_memlimit_increase; | |
8536 | mmp_entry.memlimit_active += roundToNearestMB(page_aligned_increase); | |
8537 | } | |
8538 | ||
8539 | if (mmp_entry.memlimit_inactive > 0) { | |
8540 | mmp_entry.memlimit_inactive -= current_memlimit_increase; | |
8541 | mmp_entry.memlimit_inactive += roundToNearestMB(page_aligned_increase); | |
8542 | } | |
8543 | ||
8544 | /* | |
8545 | * Store the updated delta limit in the proc. | |
8546 | */ | |
8547 | p->p_memlimit_increase = page_aligned_increase; | |
8548 | ||
8549 | int error = memorystatus_set_memlimit_properties_internal(p, &mmp_entry); | |
8550 | ||
8551 | proc_list_unlock(); | |
8552 | proc_rele(p); | |
8553 | ||
8554 | return error; | |
8555 | } | |
8556 | #endif /* DEVELOPMENT */ | |
8557 | #endif /* CONFIG_JETSAM */ |