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