]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
a39ff7e2 | 2 | * Copyright (c) 2006-2018 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 | ||
2d21ac55 | 43 | #include <libkern/libkern.h> |
3e170ce0 | 44 | #include <mach/coalition.h> |
316670eb | 45 | #include <mach/mach_time.h> |
b0d623f7 | 46 | #include <mach/task.h> |
316670eb | 47 | #include <mach/host_priv.h> |
39236c6e | 48 | #include <mach/mach_host.h> |
5ba3f43e | 49 | #include <os/log.h> |
39236c6e | 50 | #include <pexpert/pexpert.h> |
3e170ce0 | 51 | #include <sys/coalition.h> |
316670eb | 52 | #include <sys/kern_event.h> |
b0d623f7 | 53 | #include <sys/proc.h> |
39236c6e | 54 | #include <sys/proc_info.h> |
39037602 | 55 | #include <sys/reason.h> |
b0d623f7 A |
56 | #include <sys/signal.h> |
57 | #include <sys/signalvar.h> | |
2d21ac55 | 58 | #include <sys/sysctl.h> |
316670eb | 59 | #include <sys/sysproto.h> |
b0d623f7 | 60 | #include <sys/wait.h> |
6d2010ae | 61 | #include <sys/tree.h> |
316670eb | 62 | #include <sys/priv.h> |
39236c6e A |
63 | #include <vm/vm_pageout.h> |
64 | #include <vm/vm_protos.h> | |
6d2010ae A |
65 | |
66 | #if CONFIG_FREEZE | |
6d2010ae | 67 | #include <vm/vm_map.h> |
39236c6e | 68 | #endif /* CONFIG_FREEZE */ |
6d2010ae | 69 | |
0a7de745 | 70 | #include <sys/kern_memorystatus.h> |
6d2010ae | 71 | |
39037602 | 72 | #include <mach/machine/sdt.h> |
5ba3f43e | 73 | #include <libkern/section_keywords.h> |
d9a64523 | 74 | #include <stdatomic.h> |
39037602 | 75 | |
fe8ab488 | 76 | /* For logging clarity */ |
5ba3f43e | 77 | static const char *memorystatus_kill_cause_name[] = { |
0a7de745 A |
78 | "", /* kMemorystatusInvalid */ |
79 | "jettisoned", /* kMemorystatusKilled */ | |
80 | "highwater", /* kMemorystatusKilledHiwat */ | |
81 | "vnode-limit", /* kMemorystatusKilledVnodes */ | |
82 | "vm-pageshortage", /* kMemorystatusKilledVMPageShortage */ | |
83 | "proc-thrashing", /* kMemorystatusKilledProcThrashing */ | |
84 | "fc-thrashing", /* kMemorystatusKilledFCThrashing */ | |
85 | "per-process-limit", /* kMemorystatusKilledPerProcessLimit */ | |
86 | "disk-space-shortage", /* kMemorystatusKilledDiskSpaceShortage */ | |
87 | "idle-exit", /* kMemorystatusKilledIdleExit */ | |
88 | "zone-map-exhaustion", /* kMemorystatusKilledZoneMapExhaustion */ | |
89 | "vm-compressor-thrashing", /* kMemorystatusKilledVMCompressorThrashing */ | |
90 | "vm-compressor-space-shortage", /* kMemorystatusKilledVMCompressorSpaceShortage */ | |
fe8ab488 A |
91 | }; |
92 | ||
5ba3f43e A |
93 | static const char * |
94 | memorystatus_priority_band_name(int32_t priority) | |
95 | { | |
96 | switch (priority) { | |
97 | case JETSAM_PRIORITY_FOREGROUND: | |
98 | return "FOREGROUND"; | |
99 | case JETSAM_PRIORITY_AUDIO_AND_ACCESSORY: | |
100 | return "AUDIO_AND_ACCESSORY"; | |
101 | case JETSAM_PRIORITY_CONDUCTOR: | |
102 | return "CONDUCTOR"; | |
103 | case JETSAM_PRIORITY_HOME: | |
104 | return "HOME"; | |
105 | case JETSAM_PRIORITY_EXECUTIVE: | |
106 | return "EXECUTIVE"; | |
107 | case JETSAM_PRIORITY_IMPORTANT: | |
108 | return "IMPORTANT"; | |
109 | case JETSAM_PRIORITY_CRITICAL: | |
110 | return "CRITICAL"; | |
111 | } | |
112 | ||
0a7de745 | 113 | return "?"; |
5ba3f43e A |
114 | } |
115 | ||
fe8ab488 A |
116 | /* Does cause indicate vm or fc thrashing? */ |
117 | static boolean_t | |
5ba3f43e | 118 | is_reason_thrashing(unsigned cause) |
fe8ab488 A |
119 | { |
120 | switch (cause) { | |
fe8ab488 | 121 | case kMemorystatusKilledFCThrashing: |
d9a64523 A |
122 | case kMemorystatusKilledVMCompressorThrashing: |
123 | case kMemorystatusKilledVMCompressorSpaceShortage: | |
fe8ab488 A |
124 | return TRUE; |
125 | default: | |
126 | return FALSE; | |
127 | } | |
128 | } | |
129 | ||
5ba3f43e A |
130 | /* Is the zone map almost full? */ |
131 | static boolean_t | |
132 | is_reason_zone_map_exhaustion(unsigned cause) | |
133 | { | |
0a7de745 | 134 | if (cause == kMemorystatusKilledZoneMapExhaustion) { |
5ba3f43e | 135 | return TRUE; |
0a7de745 | 136 | } |
5ba3f43e A |
137 | return FALSE; |
138 | } | |
139 | ||
140 | /* | |
141 | * Returns the current zone map size and capacity to include in the jetsam snapshot. | |
142 | * Defined in zalloc.c | |
143 | */ | |
144 | extern void get_zone_map_size(uint64_t *current_size, uint64_t *capacity); | |
145 | ||
146 | /* | |
147 | * Returns the name of the largest zone and its size to include in the jetsam snapshot. | |
148 | * Defined in zalloc.c | |
149 | */ | |
150 | extern void get_largest_zone_info(char *zone_name, size_t zone_name_len, uint64_t *zone_size); | |
fe8ab488 | 151 | |
316670eb A |
152 | /* These are very verbose printfs(), enable with |
153 | * MEMORYSTATUS_DEBUG_LOG | |
154 | */ | |
155 | #if MEMORYSTATUS_DEBUG_LOG | |
156 | #define MEMORYSTATUS_DEBUG(cond, format, ...) \ | |
157 | do { \ | |
158 | if (cond) { printf(format, ##__VA_ARGS__); } \ | |
159 | } while(0) | |
160 | #else | |
161 | #define MEMORYSTATUS_DEBUG(cond, format, ...) | |
162 | #endif | |
6d2010ae | 163 | |
3e170ce0 A |
164 | /* |
165 | * Active / Inactive limit support | |
166 | * proc list must be locked | |
167 | * | |
168 | * The SET_*** macros are used to initialize a limit | |
169 | * for the first time. | |
170 | * | |
171 | * The CACHE_*** macros are use to cache the limit that will | |
172 | * soon be in effect down in the ledgers. | |
173 | */ | |
174 | ||
0a7de745 A |
175 | #define SET_ACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ |
176 | MACRO_BEGIN \ | |
177 | (p)->p_memstat_memlimit_active = (limit); \ | |
178 | if (is_fatal) { \ | |
179 | (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ | |
180 | } else { \ | |
181 | (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL; \ | |
182 | } \ | |
3e170ce0 A |
183 | MACRO_END |
184 | ||
0a7de745 A |
185 | #define SET_INACTIVE_LIMITS_LOCKED(p, limit, is_fatal) \ |
186 | MACRO_BEGIN \ | |
187 | (p)->p_memstat_memlimit_inactive = (limit); \ | |
188 | if (is_fatal) { \ | |
189 | (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ | |
190 | } else { \ | |
191 | (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL; \ | |
192 | } \ | |
3e170ce0 A |
193 | MACRO_END |
194 | ||
0a7de745 A |
195 | #define CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal) \ |
196 | MACRO_BEGIN \ | |
197 | (p)->p_memstat_memlimit = (p)->p_memstat_memlimit_active; \ | |
198 | if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { \ | |
199 | (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ | |
200 | is_fatal = TRUE; \ | |
201 | } else { \ | |
202 | (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ | |
203 | is_fatal = FALSE; \ | |
204 | } \ | |
3e170ce0 A |
205 | MACRO_END |
206 | ||
0a7de745 A |
207 | #define CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal) \ |
208 | MACRO_BEGIN \ | |
209 | (p)->p_memstat_memlimit = (p)->p_memstat_memlimit_inactive; \ | |
210 | if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { \ | |
211 | (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; \ | |
212 | is_fatal = TRUE; \ | |
213 | } else { \ | |
214 | (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT; \ | |
215 | is_fatal = FALSE; \ | |
216 | } \ | |
3e170ce0 A |
217 | MACRO_END |
218 | ||
219 | ||
39236c6e A |
220 | /* General tunables */ |
221 | ||
222 | unsigned long delta_percentage = 5; | |
223 | unsigned long critical_threshold_percentage = 5; | |
224 | unsigned long idle_offset_percentage = 5; | |
225 | unsigned long pressure_threshold_percentage = 15; | |
226 | unsigned long freeze_threshold_percentage = 50; | |
39037602 | 227 | unsigned long policy_more_free_offset_percentage = 5; |
39236c6e | 228 | |
316670eb | 229 | /* General memorystatus stuff */ |
6d2010ae | 230 | |
39236c6e A |
231 | struct klist memorystatus_klist; |
232 | static lck_mtx_t memorystatus_klist_mutex; | |
6d2010ae | 233 | |
39236c6e A |
234 | static void memorystatus_klist_lock(void); |
235 | static void memorystatus_klist_unlock(void); | |
6d2010ae | 236 | |
39037602 A |
237 | static uint64_t memorystatus_sysprocs_idle_delay_time = 0; |
238 | static uint64_t memorystatus_apps_idle_delay_time = 0; | |
39236c6e A |
239 | |
240 | /* | |
241 | * Memorystatus kevents | |
242 | */ | |
243 | ||
5ba3f43e | 244 | static int filt_memorystatusattach(struct knote *kn, struct kevent_internal_s *kev); |
39236c6e A |
245 | static void filt_memorystatusdetach(struct knote *kn); |
246 | static int filt_memorystatus(struct knote *kn, long hint); | |
39037602 A |
247 | static int filt_memorystatustouch(struct knote *kn, struct kevent_internal_s *kev); |
248 | static int filt_memorystatusprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev); | |
39236c6e | 249 | |
5ba3f43e | 250 | SECURITY_READ_ONLY_EARLY(struct filterops) memorystatus_filtops = { |
39236c6e A |
251 | .f_attach = filt_memorystatusattach, |
252 | .f_detach = filt_memorystatusdetach, | |
253 | .f_event = filt_memorystatus, | |
39037602 A |
254 | .f_touch = filt_memorystatustouch, |
255 | .f_process = filt_memorystatusprocess, | |
39236c6e A |
256 | }; |
257 | ||
258 | enum { | |
fe8ab488 A |
259 | kMemorystatusNoPressure = 0x1, |
260 | kMemorystatusPressure = 0x2, | |
39037602 A |
261 | kMemorystatusLowSwap = 0x4, |
262 | kMemorystatusProcLimitWarn = 0x8, | |
263 | kMemorystatusProcLimitCritical = 0x10 | |
39236c6e A |
264 | }; |
265 | ||
266 | /* Idle guard handling */ | |
267 | ||
39037602 A |
268 | static int32_t memorystatus_scheduled_idle_demotions_sysprocs = 0; |
269 | static int32_t memorystatus_scheduled_idle_demotions_apps = 0; | |
39236c6e A |
270 | |
271 | static thread_call_t memorystatus_idle_demotion_call; | |
272 | ||
273 | static void memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2); | |
274 | static void memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state); | |
275 | static void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state); | |
276 | static void memorystatus_reschedule_idle_demotion_locked(void); | |
6d2010ae | 277 | |
39037602 A |
278 | static void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check); |
279 | ||
5ba3f43e A |
280 | int memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap); |
281 | ||
39037602 | 282 | vm_pressure_level_t convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t); |
fe8ab488 A |
283 | |
284 | boolean_t is_knote_registered_modify_task_pressure_bits(struct knote*, int, task_t, vm_pressure_level_t, vm_pressure_level_t); | |
39037602 | 285 | void memorystatus_klist_reset_all_for_level(vm_pressure_level_t pressure_level_to_clear); |
fe8ab488 | 286 | void memorystatus_send_low_swap_note(void); |
39236c6e | 287 | |
39236c6e | 288 | unsigned int memorystatus_level = 0; |
6d2010ae | 289 | |
316670eb | 290 | static int memorystatus_list_count = 0; |
6d2010ae | 291 | |
d9a64523 | 292 | |
39236c6e | 293 | #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1) |
6d2010ae | 294 | |
39236c6e | 295 | typedef struct memstat_bucket { |
0a7de745 A |
296 | TAILQ_HEAD(, proc) list; |
297 | int count; | |
39236c6e | 298 | } memstat_bucket_t; |
6d2010ae | 299 | |
39236c6e A |
300 | memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT]; |
301 | ||
5ba3f43e A |
302 | int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index); |
303 | ||
39236c6e | 304 | uint64_t memstat_idle_demotion_deadline = 0; |
6d2010ae | 305 | |
39037602 A |
306 | int system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; |
307 | int applications_aging_band = JETSAM_PRIORITY_IDLE; | |
308 | ||
0a7de745 | 309 | #define isProcessInAgingBands(p) ((isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) || (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band))) |
d9a64523 A |
310 | |
311 | /* | |
312 | * Checking the p_memstat_state almost always requires the proc_list_lock | |
313 | * because the jetsam thread could be on the other core changing the state. | |
314 | * | |
315 | * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too. | |
316 | * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too. | |
317 | */ | |
0a7de745 A |
318 | #define isApp(p) ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK)) |
319 | #define isSysProc(p) ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK)) | |
39037602 | 320 | |
0a7de745 A |
321 | #define kJetsamAgingPolicyNone (0) |
322 | #define kJetsamAgingPolicyLegacy (1) | |
323 | #define kJetsamAgingPolicySysProcsReclaimedFirst (2) | |
324 | #define kJetsamAgingPolicyAppsReclaimedFirst (3) | |
325 | #define kJetsamAgingPolicyMax kJetsamAgingPolicyAppsReclaimedFirst | |
39037602 A |
326 | |
327 | unsigned int jetsam_aging_policy = kJetsamAgingPolicyLegacy; | |
328 | ||
329 | extern int corpse_for_fatal_memkill; | |
5ba3f43e | 330 | extern unsigned long total_corpses_count(void) __attribute__((pure)); |
39037602 | 331 | extern void task_purge_all_corpses(void); |
a39ff7e2 A |
332 | extern uint64_t vm_purgeable_purge_task_owned(task_t task); |
333 | boolean_t memorystatus_allowed_vm_map_fork(task_t); | |
334 | #if DEVELOPMENT || DEBUG | |
335 | void memorystatus_abort_vm_map_fork(task_t); | |
336 | #endif | |
39037602 A |
337 | |
338 | #if 0 | |
339 | ||
340 | /* Keeping around for future use if we need a utility that can do this OR an app that needs a dynamic adjustment. */ | |
341 | ||
342 | static int | |
343 | sysctl_set_jetsam_aging_policy SYSCTL_HANDLER_ARGS | |
344 | { | |
345 | #pragma unused(oidp, arg1, arg2) | |
346 | ||
347 | int error = 0, val = 0; | |
348 | memstat_bucket_t *old_bucket = 0; | |
349 | int old_system_procs_aging_band = 0, new_system_procs_aging_band = 0; | |
350 | int old_applications_aging_band = 0, new_applications_aging_band = 0; | |
351 | proc_t p = NULL, next_proc = NULL; | |
352 | ||
353 | ||
354 | error = sysctl_io_number(req, jetsam_aging_policy, sizeof(int), &val, NULL); | |
355 | if (error || !req->newptr) { | |
0a7de745 | 356 | return error; |
39037602 A |
357 | } |
358 | ||
359 | if ((val < 0) || (val > kJetsamAgingPolicyMax)) { | |
360 | printf("jetsam: ordering policy sysctl has invalid value - %d\n", val); | |
361 | return EINVAL; | |
362 | } | |
363 | ||
364 | /* | |
365 | * We need to synchronize with any potential adding/removal from aging bands | |
366 | * that might be in progress currently. We use the proc_list_lock() just for | |
367 | * consistency with all the routines dealing with 'aging' processes. We need | |
368 | * a lighterweight lock. | |
0a7de745 | 369 | */ |
39037602 A |
370 | proc_list_lock(); |
371 | ||
372 | old_system_procs_aging_band = system_procs_aging_band; | |
373 | old_applications_aging_band = applications_aging_band; | |
39037602 | 374 | |
0a7de745 A |
375 | switch (val) { |
376 | case kJetsamAgingPolicyNone: | |
377 | new_system_procs_aging_band = JETSAM_PRIORITY_IDLE; | |
378 | new_applications_aging_band = JETSAM_PRIORITY_IDLE; | |
379 | break; | |
39037602 | 380 | |
0a7de745 A |
381 | case kJetsamAgingPolicyLegacy: |
382 | /* | |
383 | * Legacy behavior where some daemons get a 10s protection once and only before the first clean->dirty->clean transition before going into IDLE band. | |
384 | */ | |
385 | new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
386 | new_applications_aging_band = JETSAM_PRIORITY_IDLE; | |
387 | break; | |
39037602 | 388 | |
0a7de745 A |
389 | case kJetsamAgingPolicySysProcsReclaimedFirst: |
390 | new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
391 | new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
392 | break; | |
39037602 | 393 | |
0a7de745 A |
394 | case kJetsamAgingPolicyAppsReclaimedFirst: |
395 | new_system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
396 | new_applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
397 | break; | |
39037602 | 398 | |
0a7de745 A |
399 | default: |
400 | break; | |
39037602 A |
401 | } |
402 | ||
403 | if (old_system_procs_aging_band && (old_system_procs_aging_band != new_system_procs_aging_band)) { | |
39037602 A |
404 | old_bucket = &memstat_bucket[old_system_procs_aging_band]; |
405 | p = TAILQ_FIRST(&old_bucket->list); | |
0a7de745 | 406 | |
39037602 | 407 | while (p) { |
39037602 A |
408 | next_proc = TAILQ_NEXT(p, p_memstat_list); |
409 | ||
410 | if (isSysProc(p)) { | |
411 | if (new_system_procs_aging_band == JETSAM_PRIORITY_IDLE) { | |
412 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
413 | } | |
414 | ||
415 | memorystatus_update_priority_locked(p, new_system_procs_aging_band, false, true); | |
416 | } | |
417 | ||
418 | p = next_proc; | |
419 | continue; | |
420 | } | |
421 | } | |
422 | ||
423 | if (old_applications_aging_band && (old_applications_aging_band != new_applications_aging_band)) { | |
39037602 A |
424 | old_bucket = &memstat_bucket[old_applications_aging_band]; |
425 | p = TAILQ_FIRST(&old_bucket->list); | |
39037602 | 426 | |
0a7de745 | 427 | while (p) { |
39037602 A |
428 | next_proc = TAILQ_NEXT(p, p_memstat_list); |
429 | ||
430 | if (isApp(p)) { | |
431 | if (new_applications_aging_band == JETSAM_PRIORITY_IDLE) { | |
432 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
433 | } | |
434 | ||
435 | memorystatus_update_priority_locked(p, new_applications_aging_band, false, true); | |
436 | } | |
437 | ||
438 | p = next_proc; | |
439 | continue; | |
440 | } | |
441 | } | |
442 | ||
443 | jetsam_aging_policy = val; | |
444 | system_procs_aging_band = new_system_procs_aging_band; | |
445 | applications_aging_band = new_applications_aging_band; | |
446 | ||
447 | proc_list_unlock(); | |
448 | ||
0a7de745 | 449 | return 0; |
39037602 A |
450 | } |
451 | ||
0a7de745 A |
452 | SYSCTL_PROC(_kern, OID_AUTO, set_jetsam_aging_policy, CTLTYPE_INT | CTLFLAG_RW, |
453 | 0, 0, sysctl_set_jetsam_aging_policy, "I", "Jetsam Aging Policy"); | |
39037602 A |
454 | #endif /*0*/ |
455 | ||
456 | static int | |
457 | sysctl_jetsam_set_sysprocs_idle_delay_time SYSCTL_HANDLER_ARGS | |
458 | { | |
459 | #pragma unused(oidp, arg1, arg2) | |
460 | ||
461 | int error = 0, val = 0, old_time_in_secs = 0; | |
462 | uint64_t old_time_in_ns = 0; | |
463 | ||
464 | absolutetime_to_nanoseconds(memorystatus_sysprocs_idle_delay_time, &old_time_in_ns); | |
465 | old_time_in_secs = old_time_in_ns / NSEC_PER_SEC; | |
466 | ||
467 | error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL); | |
468 | if (error || !req->newptr) { | |
0a7de745 | 469 | return error; |
39037602 A |
470 | } |
471 | ||
472 | if ((val < 0) || (val > INT32_MAX)) { | |
473 | printf("jetsam: new idle delay interval has invalid value.\n"); | |
474 | return EINVAL; | |
475 | } | |
476 | ||
477 | nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); | |
0a7de745 A |
478 | |
479 | return 0; | |
39037602 A |
480 | } |
481 | ||
0a7de745 A |
482 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_sysprocs_idle_delay_time, CTLTYPE_INT | CTLFLAG_RW, |
483 | 0, 0, sysctl_jetsam_set_sysprocs_idle_delay_time, "I", "Aging window for system processes"); | |
39037602 A |
484 | |
485 | ||
486 | static int | |
487 | sysctl_jetsam_set_apps_idle_delay_time SYSCTL_HANDLER_ARGS | |
488 | { | |
489 | #pragma unused(oidp, arg1, arg2) | |
490 | ||
491 | int error = 0, val = 0, old_time_in_secs = 0; | |
492 | uint64_t old_time_in_ns = 0; | |
493 | ||
494 | absolutetime_to_nanoseconds(memorystatus_apps_idle_delay_time, &old_time_in_ns); | |
495 | old_time_in_secs = old_time_in_ns / NSEC_PER_SEC; | |
496 | ||
497 | error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL); | |
498 | if (error || !req->newptr) { | |
0a7de745 | 499 | return error; |
39037602 A |
500 | } |
501 | ||
502 | if ((val < 0) || (val > INT32_MAX)) { | |
503 | printf("jetsam: new idle delay interval has invalid value.\n"); | |
504 | return EINVAL; | |
505 | } | |
506 | ||
507 | nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); | |
0a7de745 A |
508 | |
509 | return 0; | |
39037602 A |
510 | } |
511 | ||
0a7de745 A |
512 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_apps_idle_delay_time, CTLTYPE_INT | CTLFLAG_RW, |
513 | 0, 0, sysctl_jetsam_set_apps_idle_delay_time, "I", "Aging window for applications"); | |
39037602 | 514 | |
0a7de745 | 515 | SYSCTL_INT(_kern, OID_AUTO, jetsam_aging_policy, CTLTYPE_INT | CTLFLAG_RD, &jetsam_aging_policy, 0, ""); |
39037602 | 516 | |
316670eb | 517 | static unsigned int memorystatus_dirty_count = 0; |
6d2010ae | 518 | |
0a7de745 | 519 | SYSCTL_INT(_kern, OID_AUTO, max_task_pmem, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED, &max_task_footprint_mb, 0, ""); |
3e170ce0 | 520 | |
5ba3f43e A |
521 | #if CONFIG_EMBEDDED |
522 | ||
0a7de745 | 523 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_level, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_level, 0, ""); |
5ba3f43e A |
524 | |
525 | #endif /* CONFIG_EMBEDDED */ | |
39236c6e A |
526 | |
527 | int | |
528 | memorystatus_get_level(__unused struct proc *p, struct memorystatus_get_level_args *args, __unused int *ret) | |
529 | { | |
0a7de745 A |
530 | user_addr_t level = 0; |
531 | ||
39236c6e | 532 | level = args->level; |
0a7de745 | 533 | |
39236c6e A |
534 | if (copyout(&memorystatus_level, level, sizeof(memorystatus_level)) != 0) { |
535 | return EFAULT; | |
536 | } | |
0a7de745 | 537 | |
39236c6e A |
538 | return 0; |
539 | } | |
540 | ||
541 | static proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search); | |
542 | static proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search); | |
543 | ||
544 | static void memorystatus_thread(void *param __unused, wait_result_t wr __unused); | |
6d2010ae | 545 | |
39037602 A |
546 | /* Memory Limits */ |
547 | ||
548 | static int memorystatus_highwater_enabled = 1; /* Update the cached memlimit data. */ | |
549 | ||
550 | static boolean_t proc_jetsam_state_is_active_locked(proc_t); | |
551 | static boolean_t memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason); | |
552 | static boolean_t memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason); | |
553 | ||
554 | ||
3e170ce0 A |
555 | static int memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval); |
556 | ||
557 | static int memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry); | |
558 | ||
559 | static int memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval); | |
560 | ||
39037602 | 561 | 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 | 562 | |
fe8ab488 A |
563 | int proc_get_memstat_priority(proc_t, boolean_t); |
564 | ||
fe8ab488 | 565 | static boolean_t memorystatus_idle_snapshot = 0; |
39236c6e | 566 | |
316670eb A |
567 | unsigned int memorystatus_delta = 0; |
568 | ||
3e170ce0 | 569 | /* Jetsam Loop Detection */ |
0a7de745 A |
570 | static boolean_t memorystatus_jld_enabled = FALSE; /* Enable jetsam loop detection */ |
571 | static uint32_t memorystatus_jld_eval_period_msecs = 0; /* Init pass sets this based on device memory size */ | |
572 | static int memorystatus_jld_eval_aggressive_count = 3; /* Raise the priority max after 'n' aggressive loops */ | |
3e170ce0 A |
573 | static int memorystatus_jld_eval_aggressive_priority_band_max = 15; /* Kill aggressively up through this band */ |
574 | ||
490019cf A |
575 | /* |
576 | * A FG app can request that the aggressive jetsam mechanism display some leniency in the FG band. This 'lenient' mode is described as: | |
577 | * --- 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. | |
578 | * | |
579 | * RESTRICTIONS: | |
580 | * - Such a request is respected/acknowledged only once while that 'requesting' app is in the FG band i.e. if aggressive jetsam was | |
0a7de745 | 581 | * 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 |
582 | * |
583 | * - 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. | |
584 | * | |
585 | * - Also, the transition of the 'requesting' app away from the FG band will void this special behavior. | |
586 | */ | |
587 | ||
0a7de745 A |
588 | #define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD 25 |
589 | boolean_t memorystatus_aggressive_jetsam_lenient_allowed = FALSE; | |
590 | boolean_t memorystatus_aggressive_jetsam_lenient = FALSE; | |
490019cf | 591 | |
3e170ce0 | 592 | #if DEVELOPMENT || DEBUG |
0a7de745 | 593 | /* |
3e170ce0 A |
594 | * Jetsam Loop Detection tunables. |
595 | */ | |
596 | ||
0a7de745 A |
597 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_period_msecs, 0, ""); |
598 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_count, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_count, 0, ""); | |
599 | 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 |
600 | #endif /* DEVELOPMENT || DEBUG */ |
601 | ||
fe8ab488 | 602 | static uint32_t kill_under_pressure_cause = 0; |
316670eb | 603 | |
3e170ce0 A |
604 | /* |
605 | * default jetsam snapshot support | |
606 | */ | |
39236c6e | 607 | static memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot; |
d9a64523 | 608 | static memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy; |
39236c6e | 609 | #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries |
39236c6e | 610 | static unsigned int memorystatus_jetsam_snapshot_count = 0; |
d9a64523 | 611 | static unsigned int memorystatus_jetsam_snapshot_copy_count = 0; |
39236c6e | 612 | static unsigned int memorystatus_jetsam_snapshot_max = 0; |
d9a64523 | 613 | static unsigned int memorystatus_jetsam_snapshot_size = 0; |
3e170ce0 A |
614 | static uint64_t memorystatus_jetsam_snapshot_last_timestamp = 0; |
615 | static uint64_t memorystatus_jetsam_snapshot_timeout = 0; | |
616 | #define JETSAM_SNAPSHOT_TIMEOUT_SECS 30 | |
617 | ||
618 | /* | |
619 | * snapshot support for memstats collected at boot. | |
620 | */ | |
621 | static memorystatus_jetsam_snapshot_t memorystatus_at_boot_snapshot; | |
316670eb | 622 | |
39037602 A |
623 | static void memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count); |
624 | static boolean_t memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount); | |
625 | static void memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime); | |
626 | ||
39236c6e | 627 | static void memorystatus_clear_errors(void); |
d9a64523 | 628 | static void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages); |
39037602 | 629 | static void memorystatus_get_task_phys_footprint_page_counts(task_t task, |
0a7de745 A |
630 | uint64_t *internal_pages, uint64_t *internal_compressed_pages, |
631 | uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, | |
632 | uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, | |
633 | uint64_t *iokit_mapped_pages, uint64_t *page_table_pages); | |
39037602 A |
634 | |
635 | static void memorystatus_get_task_memory_region_count(task_t task, uint64_t *count); | |
636 | ||
39236c6e | 637 | static uint32_t memorystatus_build_state(proc_t p); |
fe8ab488 | 638 | //static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured); |
39236c6e | 639 | |
39037602 | 640 | static boolean_t memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, os_reason_t jetsam_reason, int32_t *priority, uint32_t *errors); |
5ba3f43e | 641 | static boolean_t memorystatus_kill_top_process_aggressive(uint32_t cause, int aggr_count, int32_t priority_max, uint32_t *errors); |
d9a64523 | 642 | static boolean_t memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, uint32_t *errors); |
a39ff7e2 | 643 | static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged); |
39236c6e A |
644 | |
645 | static boolean_t memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause); | |
316670eb | 646 | |
3e170ce0 A |
647 | /* Priority Band Sorting Routines */ |
648 | static int memorystatus_sort_bucket(unsigned int bucket_index, int sort_order); | |
649 | static int memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order); | |
650 | static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index); | |
651 | static int memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int list_sz); | |
652 | ||
653 | /* qsort routines */ | |
654 | typedef int (*cmpfunc_t)(const void *a, const void *b); | |
655 | extern void qsort(void *a, size_t n, size_t es, cmpfunc_t cmp); | |
656 | static int memstat_asc_cmp(const void *a, const void *b); | |
657 | ||
316670eb | 658 | /* VM pressure */ |
6d2010ae | 659 | |
fe8ab488 A |
660 | extern unsigned int vm_page_free_count; |
661 | extern unsigned int vm_page_active_count; | |
662 | extern unsigned int vm_page_inactive_count; | |
663 | extern unsigned int vm_page_throttled_count; | |
664 | extern unsigned int vm_page_purgeable_count; | |
665 | extern unsigned int vm_page_wire_count; | |
39037602 | 666 | #if CONFIG_SECLUDED_MEMORY |
0a7de745 | 667 | extern unsigned int vm_page_secluded_count; |
39037602 | 668 | #endif /* CONFIG_SECLUDED_MEMORY */ |
fe8ab488 | 669 | |
5ba3f43e | 670 | #if CONFIG_JETSAM |
fe8ab488 A |
671 | unsigned int memorystatus_available_pages = (unsigned int)-1; |
672 | unsigned int memorystatus_available_pages_pressure = 0; | |
673 | unsigned int memorystatus_available_pages_critical = 0; | |
5ba3f43e A |
674 | static unsigned int memorystatus_available_pages_critical_base = 0; |
675 | static unsigned int memorystatus_available_pages_critical_idle_offset = 0; | |
fe8ab488 | 676 | |
00867663 A |
677 | #if DEVELOPMENT || DEBUG |
678 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); | |
679 | #else | |
5ba3f43e | 680 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, ""); |
00867663 | 681 | #endif /* DEVELOPMENT || DEBUG */ |
5ba3f43e A |
682 | |
683 | static unsigned int memorystatus_jetsam_policy = kPolicyDefault; | |
684 | unsigned int memorystatus_policy_more_free_offset_pages = 0; | |
685 | static void memorystatus_update_levels_locked(boolean_t critical_only); | |
686 | static unsigned int memorystatus_thread_wasted_wakeup = 0; | |
687 | ||
688 | /* Callback into vm_compressor.c to signal that thrashing has been mitigated. */ | |
689 | extern void vm_thrashing_jetsam_done(void); | |
690 | 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); | |
691 | ||
692 | int32_t max_kill_priority = JETSAM_PRIORITY_MAX; | |
693 | ||
694 | #else /* CONFIG_JETSAM */ | |
695 | ||
696 | uint64_t memorystatus_available_pages = (uint64_t)-1; | |
697 | uint64_t memorystatus_available_pages_pressure = (uint64_t)-1; | |
698 | uint64_t memorystatus_available_pages_critical = (uint64_t)-1; | |
699 | ||
700 | int32_t max_kill_priority = JETSAM_PRIORITY_IDLE; | |
00867663 A |
701 | #endif /* CONFIG_JETSAM */ |
702 | ||
5ba3f43e | 703 | unsigned int memorystatus_frozen_count = 0; |
d9a64523 A |
704 | unsigned int memorystatus_frozen_processes_max = 0; |
705 | unsigned int memorystatus_frozen_shared_mb = 0; | |
706 | unsigned int memorystatus_frozen_shared_mb_max = 0; | |
707 | unsigned int memorystatus_freeze_shared_mb_per_process_max = 0; /* Max. MB allowed per process to be freezer-eligible. */ | |
708 | unsigned int memorystatus_freeze_private_shared_pages_ratio = 2; /* Ratio of private:shared pages for a process to be freezer-eligible. */ | |
5ba3f43e | 709 | unsigned int memorystatus_suspended_count = 0; |
d9a64523 A |
710 | unsigned int memorystatus_thaw_count = 0; |
711 | unsigned int memorystatus_refreeze_eligible_count = 0; /* # of processes currently thawed i.e. have state on disk & in-memory */ | |
5ba3f43e A |
712 | |
713 | #if VM_PRESSURE_EVENTS | |
714 | ||
0a7de745 | 715 | boolean_t memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused boolean_t is_fatal, boolean_t exceeded); |
5ba3f43e A |
716 | |
717 | vm_pressure_level_t memorystatus_vm_pressure_level = kVMPressureNormal; | |
718 | ||
fe8ab488 A |
719 | /* |
720 | * We use this flag to signal if we have any HWM offenders | |
721 | * on the system. This way we can reduce the number of wakeups | |
722 | * of the memorystatus_thread when the system is between the | |
723 | * "pressure" and "critical" threshold. | |
724 | * | |
725 | * The (re-)setting of this variable is done without any locks | |
726 | * or synchronization simply because it is not possible (currently) | |
727 | * to keep track of HWM offenders that drop down below their memory | |
728 | * limit and/or exit. So, we choose to burn a couple of wasted wakeups | |
729 | * by allowing the unguarded modification of this variable. | |
730 | */ | |
731 | boolean_t memorystatus_hwm_candidates = 0; | |
732 | ||
733 | static int memorystatus_send_note(int event_code, void *data, size_t data_length); | |
fe8ab488 | 734 | |
d9a64523 A |
735 | /* |
736 | * This value is the threshold that a process must meet to be considered for scavenging. | |
737 | */ | |
738 | #if CONFIG_EMBEDDED | |
0a7de745 | 739 | #define VM_PRESSURE_MINIMUM_RSIZE 6 /* MB */ |
d9a64523 | 740 | #else /* CONFIG_EMBEDDED */ |
0a7de745 | 741 | #define VM_PRESSURE_MINIMUM_RSIZE 10 /* MB */ |
d9a64523 A |
742 | #endif /* CONFIG_EMBEDDED */ |
743 | ||
744 | uint32_t vm_pressure_task_footprint_min = VM_PRESSURE_MINIMUM_RSIZE; | |
745 | ||
746 | #if DEVELOPMENT || DEBUG | |
0a7de745 | 747 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_vm_pressure_task_footprint_min, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pressure_task_footprint_min, 0, ""); |
d9a64523 A |
748 | #endif /* DEVELOPMENT || DEBUG */ |
749 | ||
316670eb A |
750 | #endif /* VM_PRESSURE_EVENTS */ |
751 | ||
39037602 A |
752 | |
753 | #if DEVELOPMENT || DEBUG | |
754 | ||
755 | lck_grp_attr_t *disconnect_page_mappings_lck_grp_attr; | |
756 | lck_grp_t *disconnect_page_mappings_lck_grp; | |
757 | static lck_mtx_t disconnect_page_mappings_mutex; | |
758 | ||
5ba3f43e A |
759 | extern boolean_t kill_on_no_paging_space; |
760 | #endif /* DEVELOPMENT || DEBUG */ | |
39037602 A |
761 | |
762 | ||
d9a64523 A |
763 | /* |
764 | * Table that expresses the probability of a process | |
765 | * being used in the next hour. | |
766 | */ | |
767 | typedef struct memorystatus_internal_probabilities { | |
768 | char proc_name[MAXCOMLEN + 1]; | |
769 | int use_probability; | |
770 | } memorystatus_internal_probabilities_t; | |
771 | ||
772 | static memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table = NULL; | |
773 | static size_t memorystatus_global_probabilities_size = 0; | |
774 | ||
316670eb A |
775 | /* Freeze */ |
776 | ||
777 | #if CONFIG_FREEZE | |
316670eb A |
778 | boolean_t memorystatus_freeze_enabled = FALSE; |
779 | int memorystatus_freeze_wakeup = 0; | |
d9a64523 | 780 | int memorystatus_freeze_jetsam_band = 0; /* the jetsam band which will contain P_MEMSTAT_FROZEN processes */ |
316670eb | 781 | |
3e170ce0 A |
782 | lck_grp_attr_t *freezer_lck_grp_attr; |
783 | lck_grp_t *freezer_lck_grp; | |
784 | static lck_mtx_t freezer_mutex; | |
785 | ||
316670eb A |
786 | static inline boolean_t memorystatus_can_freeze_processes(void); |
787 | static boolean_t memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low); | |
d9a64523 | 788 | static boolean_t memorystatus_is_process_eligible_for_freeze(proc_t p); |
316670eb | 789 | static void memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused); |
d9a64523 A |
790 | static boolean_t memorystatus_freeze_thread_should_run(void); |
791 | ||
792 | void memorystatus_disable_freeze(void); | |
316670eb A |
793 | |
794 | /* Thresholds */ | |
795 | static unsigned int memorystatus_freeze_threshold = 0; | |
796 | ||
fe8ab488 A |
797 | static unsigned int memorystatus_freeze_pages_min = 0; |
798 | static unsigned int memorystatus_freeze_pages_max = 0; | |
316670eb A |
799 | |
800 | static unsigned int memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_DEFAULT; | |
801 | ||
3e170ce0 | 802 | static unsigned int memorystatus_freeze_daily_mb_max = FREEZE_DAILY_MB_MAX_DEFAULT; |
0a7de745 | 803 | static uint64_t memorystatus_freeze_budget_pages_remaining = 0; //remaining # of pages that can be frozen to disk |
d9a64523 A |
804 | static boolean_t memorystatus_freeze_degradation = FALSE; //protected by the freezer mutex. Signals we are in a degraded freeze mode. |
805 | ||
806 | static unsigned int memorystatus_max_frozen_demotions_daily = 0; | |
807 | static unsigned int memorystatus_thaw_count_demotion_threshold = 0; | |
3e170ce0 | 808 | |
316670eb | 809 | /* Stats */ |
316670eb | 810 | static uint64_t memorystatus_freeze_pageouts = 0; |
6d2010ae A |
811 | |
812 | /* Throttling */ | |
0a7de745 A |
813 | #define DEGRADED_WINDOW_MINS (30) |
814 | #define NORMAL_WINDOW_MINS (24 * 60) | |
d9a64523 | 815 | |
316670eb | 816 | static throttle_interval_t throttle_intervals[] = { |
d9a64523 A |
817 | { DEGRADED_WINDOW_MINS, 1, 0, 0, { 0, 0 }}, |
818 | { NORMAL_WINDOW_MINS, 1, 0, 0, { 0, 0 }}, | |
6d2010ae | 819 | }; |
d9a64523 A |
820 | throttle_interval_t *degraded_throttle_window = &throttle_intervals[0]; |
821 | throttle_interval_t *normal_throttle_window = &throttle_intervals[1]; | |
6d2010ae | 822 | |
d9a64523 A |
823 | extern uint64_t vm_swap_get_free_space(void); |
824 | extern boolean_t vm_swap_max_budget(uint64_t *); | |
6d2010ae | 825 | |
d9a64523 | 826 | static void memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed); |
6d2010ae | 827 | |
d9a64523 | 828 | static uint64_t memorystatus_freezer_thread_next_run_ts = 0; |
3e170ce0 | 829 | |
0a7de745 A |
830 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_count, 0, ""); |
831 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_thaw_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_thaw_count, 0, ""); | |
832 | SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_pageouts, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_pageouts, ""); | |
833 | SYSCTL_QUAD(_kern, OID_AUTO, memorystatus_freeze_budget_pages_remaining, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_budget_pages_remaining, ""); | |
3e170ce0 | 834 | |
39236c6e | 835 | #endif /* CONFIG_FREEZE */ |
6d2010ae | 836 | |
316670eb | 837 | /* Debug */ |
6d2010ae | 838 | |
fe8ab488 A |
839 | extern struct knote *vm_find_knote_from_pid(pid_t, struct klist *); |
840 | ||
6d2010ae | 841 | #if DEVELOPMENT || DEBUG |
6d2010ae | 842 | |
39037602 | 843 | static unsigned int memorystatus_debug_dump_this_bucket = 0; |
39236c6e | 844 | |
3e170ce0 | 845 | static void |
0a7de745 | 846 | memorystatus_debug_dump_bucket_locked(unsigned int bucket_index) |
3e170ce0 A |
847 | { |
848 | proc_t p = NULL; | |
39037602 A |
849 | uint64_t bytes = 0; |
850 | int ledger_limit = 0; | |
3e170ce0 A |
851 | unsigned int b = bucket_index; |
852 | boolean_t traverse_all_buckets = FALSE; | |
853 | ||
0a7de745 | 854 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
3e170ce0 A |
855 | traverse_all_buckets = TRUE; |
856 | b = 0; | |
0a7de745 | 857 | } else { |
3e170ce0 A |
858 | traverse_all_buckets = FALSE; |
859 | b = bucket_index; | |
860 | } | |
861 | ||
862 | /* | |
39037602 A |
863 | * footprint reported in [pages / MB ] |
864 | * limits reported as: | |
865 | * L-limit proc's Ledger limit | |
866 | * C-limit proc's Cached limit, should match Ledger | |
867 | * A-limit proc's Active limit | |
868 | * IA-limit proc's Inactive limit | |
869 | * F==Fatal, NF==NonFatal | |
3e170ce0 | 870 | */ |
39037602 | 871 | |
0a7de745 | 872 | printf("memorystatus_debug_dump ***START*(PAGE_SIZE_64=%llu)**\n", PAGE_SIZE_64); |
39037602 | 873 | printf("bucket [pid] [pages / MB] [state] [EP / RP] dirty deadline [L-limit / C-limit / A-limit / IA-limit] name\n"); |
3e170ce0 A |
874 | p = memorystatus_get_first_proc_locked(&b, traverse_all_buckets); |
875 | while (p) { | |
39037602 A |
876 | bytes = get_task_phys_footprint(p->task); |
877 | task_get_phys_footprint_limit(p->task, &ledger_limit); | |
878 | printf("%2d [%5d] [%5lld /%3lldMB] 0x%-8x [%2d / %2d] 0x%-3x %10lld [%3d / %3d%s / %3d%s / %3d%s] %s\n", | |
0a7de745 A |
879 | b, p->p_pid, |
880 | (bytes / PAGE_SIZE_64), /* task's footprint converted from bytes to pages */ | |
881 | (bytes / (1024ULL * 1024ULL)), /* task's footprint converted from bytes to MB */ | |
882 | p->p_memstat_state, p->p_memstat_effectivepriority, p->p_memstat_requestedpriority, p->p_memstat_dirty, p->p_memstat_idledeadline, | |
883 | ledger_limit, | |
884 | p->p_memstat_memlimit, | |
885 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), | |
886 | p->p_memstat_memlimit_active, | |
887 | (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL ? "F " : "NF"), | |
888 | p->p_memstat_memlimit_inactive, | |
889 | (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL ? "F " : "NF"), | |
890 | (*p->p_name ? p->p_name : "unknown")); | |
3e170ce0 | 891 | p = memorystatus_get_next_proc_locked(&b, p, traverse_all_buckets); |
0a7de745 A |
892 | } |
893 | printf("memorystatus_debug_dump ***END***\n"); | |
3e170ce0 A |
894 | } |
895 | ||
896 | static int | |
897 | sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS | |
898 | { | |
899 | #pragma unused(oidp, arg2) | |
0a7de745 A |
900 | int bucket_index = 0; |
901 | int error; | |
3e170ce0 A |
902 | error = SYSCTL_OUT(req, arg1, sizeof(int)); |
903 | if (error || !req->newptr) { | |
0a7de745 A |
904 | return error; |
905 | } | |
906 | error = SYSCTL_IN(req, &bucket_index, sizeof(int)); | |
907 | if (error || !req->newptr) { | |
908 | return error; | |
3e170ce0 | 909 | } |
3e170ce0 A |
910 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
911 | /* | |
912 | * All jetsam buckets will be dumped. | |
913 | */ | |
0a7de745 | 914 | } else { |
3e170ce0 A |
915 | /* |
916 | * Only a single bucket will be dumped. | |
917 | */ | |
918 | } | |
919 | ||
920 | proc_list_lock(); | |
921 | memorystatus_debug_dump_bucket_locked(bucket_index); | |
922 | proc_list_unlock(); | |
923 | memorystatus_debug_dump_this_bucket = bucket_index; | |
0a7de745 | 924 | return error; |
3e170ce0 A |
925 | } |
926 | ||
927 | /* | |
928 | * Debug aid to look at jetsam buckets and proc jetsam fields. | |
929 | * Use this sysctl to act on a particular jetsam bucket. | |
930 | * Writing the sysctl triggers the dump. | |
0a7de745 | 931 | * Usage: sysctl kern.memorystatus_debug_dump_this_bucket=<bucket_index> |
3e170ce0 A |
932 | */ |
933 | ||
0a7de745 | 934 | 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 |
935 | |
936 | ||
39236c6e A |
937 | /* Debug aid to aid determination of limit */ |
938 | ||
939 | static int | |
940 | sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS | |
941 | { | |
942 | #pragma unused(oidp, arg2) | |
943 | proc_t p; | |
944 | unsigned int b = 0; | |
945 | int error, enable = 0; | |
0a7de745 | 946 | boolean_t use_active; /* use the active limit and active limit attributes */ |
813fb2f6 | 947 | boolean_t is_fatal; |
39236c6e A |
948 | |
949 | error = SYSCTL_OUT(req, arg1, sizeof(int)); | |
950 | if (error || !req->newptr) { | |
0a7de745 | 951 | return error; |
39236c6e A |
952 | } |
953 | ||
954 | error = SYSCTL_IN(req, &enable, sizeof(int)); | |
955 | if (error || !req->newptr) { | |
0a7de745 | 956 | return error; |
39236c6e A |
957 | } |
958 | ||
959 | if (!(enable == 0 || enable == 1)) { | |
960 | return EINVAL; | |
961 | } | |
962 | ||
963 | proc_list_lock(); | |
964 | ||
965 | p = memorystatus_get_first_proc_locked(&b, TRUE); | |
966 | while (p) { | |
813fb2f6 | 967 | use_active = proc_jetsam_state_is_active_locked(p); |
3e170ce0 | 968 | |
39236c6e | 969 | if (enable) { |
813fb2f6 A |
970 | if (use_active == TRUE) { |
971 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
39236c6e | 972 | } else { |
813fb2f6 | 973 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
39236c6e A |
974 | } |
975 | } else { | |
3e170ce0 A |
976 | /* |
977 | * Disabling limits does not touch the stored variants. | |
978 | * Set the cached limit fields to system_wide defaults. | |
979 | */ | |
980 | p->p_memstat_memlimit = -1; | |
981 | p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT; | |
813fb2f6 | 982 | is_fatal = TRUE; |
fe8ab488 | 983 | } |
3e170ce0 A |
984 | |
985 | /* | |
986 | * Enforce the cached limit by writing to the ledger. | |
987 | */ | |
813fb2f6 | 988 | task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit: -1, NULL, use_active, is_fatal); |
3e170ce0 | 989 | |
39236c6e A |
990 | p = memorystatus_get_next_proc_locked(&b, p, TRUE); |
991 | } | |
0a7de745 | 992 | |
39236c6e A |
993 | memorystatus_highwater_enabled = enable; |
994 | ||
995 | proc_list_unlock(); | |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
0a7de745 | 1000 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", ""); |
39236c6e | 1001 | |
39037602 A |
1002 | #if VM_PRESSURE_EVENTS |
1003 | ||
1004 | /* | |
813fb2f6 A |
1005 | * This routine is used for targeted notifications regardless of system memory pressure |
1006 | * and regardless of whether or not the process has already been notified. | |
1007 | * It bypasses and has no effect on the only-one-notification per soft-limit policy. | |
0a7de745 | 1008 | * |
39037602 A |
1009 | * "memnote" is the current user. |
1010 | */ | |
1011 | ||
1012 | static int | |
1013 | sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS | |
1014 | { | |
1015 | #pragma unused(arg1, arg2) | |
1016 | ||
1017 | int error = 0, pid = 0; | |
1018 | struct knote *kn = NULL; | |
1019 | boolean_t found_knote = FALSE; | |
0a7de745 | 1020 | int fflags = 0; /* filter flags for EVFILT_MEMORYSTATUS */ |
39037602 A |
1021 | uint64_t value = 0; |
1022 | ||
1023 | error = sysctl_handle_quad(oidp, &value, 0, req); | |
0a7de745 A |
1024 | if (error || !req->newptr) { |
1025 | return error; | |
1026 | } | |
39037602 A |
1027 | |
1028 | /* | |
1029 | * Find the pid in the low 32 bits of value passed in. | |
1030 | */ | |
1031 | pid = (int)(value & 0xFFFFFFFF); | |
1032 | ||
1033 | /* | |
1034 | * Find notification in the high 32 bits of the value passed in. | |
1035 | */ | |
1036 | fflags = (int)((value >> 32) & 0xFFFFFFFF); | |
1037 | ||
1038 | /* | |
1039 | * For backwards compatibility, when no notification is | |
1040 | * passed in, default to the NOTE_MEMORYSTATUS_PRESSURE_WARN | |
1041 | */ | |
1042 | if (fflags == 0) { | |
1043 | fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN; | |
1044 | // printf("memorystatus_vm_pressure_send: using default notification [0x%x]\n", fflags); | |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * See event.h ... fflags for EVFILT_MEMORYSTATUS | |
1049 | */ | |
0a7de745 A |
1050 | if (!((fflags == NOTE_MEMORYSTATUS_PRESSURE_NORMAL) || |
1051 | (fflags == NOTE_MEMORYSTATUS_PRESSURE_WARN) || | |
1052 | (fflags == NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) || | |
1053 | (fflags == NOTE_MEMORYSTATUS_LOW_SWAP) || | |
1054 | (fflags == NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) || | |
1055 | (fflags == NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) || | |
1056 | (((fflags & NOTE_MEMORYSTATUS_MSL_STATUS) != 0 && | |
1057 | ((fflags & ~NOTE_MEMORYSTATUS_MSL_STATUS) == 0))))) { | |
39037602 A |
1058 | printf("memorystatus_vm_pressure_send: notification [0x%x] not supported \n", fflags); |
1059 | error = 1; | |
0a7de745 | 1060 | return error; |
39037602 A |
1061 | } |
1062 | ||
1063 | /* | |
1064 | * Forcibly send pid a memorystatus notification. | |
1065 | */ | |
1066 | ||
1067 | memorystatus_klist_lock(); | |
1068 | ||
1069 | SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) { | |
1070 | proc_t knote_proc = knote_get_kq(kn)->kq_p; | |
1071 | pid_t knote_pid = knote_proc->p_pid; | |
1072 | ||
1073 | if (knote_pid == pid) { | |
1074 | /* | |
1075 | * Forcibly send this pid a memorystatus notification. | |
1076 | */ | |
1077 | kn->kn_fflags = fflags; | |
1078 | found_knote = TRUE; | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | if (found_knote) { | |
1083 | KNOTE(&memorystatus_klist, 0); | |
1084 | printf("memorystatus_vm_pressure_send: (value 0x%llx) notification [0x%x] sent to process [%d] \n", value, fflags, pid); | |
1085 | error = 0; | |
1086 | } else { | |
1087 | printf("memorystatus_vm_pressure_send: (value 0x%llx) notification [0x%x] not sent to process [%d] (none registered?)\n", value, fflags, pid); | |
1088 | error = 1; | |
1089 | } | |
1090 | ||
1091 | memorystatus_klist_unlock(); | |
1092 | ||
0a7de745 | 1093 | return error; |
39037602 A |
1094 | } |
1095 | ||
0a7de745 | 1096 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_send, CTLTYPE_QUAD | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
39037602 A |
1097 | 0, 0, &sysctl_memorystatus_vm_pressure_send, "Q", ""); |
1098 | ||
1099 | #endif /* VM_PRESSURE_EVENTS */ | |
1100 | ||
0a7de745 | 1101 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_idle_snapshot, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_idle_snapshot, 0, ""); |
39037602 | 1102 | |
5ba3f43e | 1103 | #if CONFIG_JETSAM |
0a7de745 A |
1104 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages_critical, 0, ""); |
1105 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_base, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_critical_base, 0, ""); | |
1106 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_idle_offset, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_critical_idle_offset, 0, ""); | |
39037602 | 1107 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_policy_more_free_offset_pages, CTLFLAG_RW, &memorystatus_policy_more_free_offset_pages, 0, ""); |
316670eb | 1108 | |
5ba3f43e A |
1109 | static unsigned int memorystatus_jetsam_panic_debug = 0; |
1110 | static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic = 0; | |
1111 | ||
316670eb | 1112 | /* Diagnostic code */ |
39236c6e | 1113 | |
316670eb | 1114 | enum { |
0a7de745 | 1115 | kJetsamDiagnosticModeNone = 0, |
316670eb A |
1116 | kJetsamDiagnosticModeAll = 1, |
1117 | kJetsamDiagnosticModeStopAtFirstActive = 2, | |
1118 | kJetsamDiagnosticModeCount | |
1119 | } jetsam_diagnostic_mode = kJetsamDiagnosticModeNone; | |
1120 | ||
1121 | static int jetsam_diagnostic_suspended_one_active_proc = 0; | |
1122 | ||
1123 | static int | |
1124 | sysctl_jetsam_diagnostic_mode SYSCTL_HANDLER_ARGS | |
1125 | { | |
1126 | #pragma unused(arg1, arg2) | |
1127 | ||
1128 | const char *diagnosticStrings[] = { | |
1129 | "jetsam: diagnostic mode: resetting critical level.", | |
1130 | "jetsam: diagnostic mode: will examine all processes", | |
0a7de745 | 1131 | "jetsam: diagnostic mode: will stop at first active process" |
316670eb | 1132 | }; |
0a7de745 | 1133 | |
316670eb A |
1134 | int error, val = jetsam_diagnostic_mode; |
1135 | boolean_t changed = FALSE; | |
1136 | ||
1137 | error = sysctl_handle_int(oidp, &val, 0, req); | |
0a7de745 A |
1138 | if (error || !req->newptr) { |
1139 | return error; | |
1140 | } | |
316670eb A |
1141 | if ((val < 0) || (val >= kJetsamDiagnosticModeCount)) { |
1142 | printf("jetsam: diagnostic mode: invalid value - %d\n", val); | |
1143 | return EINVAL; | |
1144 | } | |
0a7de745 | 1145 | |
39236c6e | 1146 | proc_list_lock(); |
0a7de745 | 1147 | |
316670eb A |
1148 | if ((unsigned int) val != jetsam_diagnostic_mode) { |
1149 | jetsam_diagnostic_mode = val; | |
1150 | ||
1151 | memorystatus_jetsam_policy &= ~kPolicyDiagnoseActive; | |
0a7de745 | 1152 | |
316670eb A |
1153 | switch (jetsam_diagnostic_mode) { |
1154 | case kJetsamDiagnosticModeNone: | |
1155 | /* Already cleared */ | |
1156 | break; | |
1157 | case kJetsamDiagnosticModeAll: | |
1158 | memorystatus_jetsam_policy |= kPolicyDiagnoseAll; | |
1159 | break; | |
1160 | case kJetsamDiagnosticModeStopAtFirstActive: | |
1161 | memorystatus_jetsam_policy |= kPolicyDiagnoseFirst; | |
1162 | break; | |
1163 | default: | |
1164 | /* Already validated */ | |
1165 | break; | |
1166 | } | |
0a7de745 | 1167 | |
39236c6e | 1168 | memorystatus_update_levels_locked(FALSE); |
316670eb A |
1169 | changed = TRUE; |
1170 | } | |
0a7de745 | 1171 | |
39236c6e | 1172 | proc_list_unlock(); |
0a7de745 | 1173 | |
316670eb A |
1174 | if (changed) { |
1175 | printf("%s\n", diagnosticStrings[val]); | |
1176 | } | |
0a7de745 A |
1177 | |
1178 | return 0; | |
316670eb A |
1179 | } |
1180 | ||
0a7de745 A |
1181 | SYSCTL_PROC(_debug, OID_AUTO, jetsam_diagnostic_mode, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY, |
1182 | &jetsam_diagnostic_mode, 0, sysctl_jetsam_diagnostic_mode, "I", "Jetsam Diagnostic Mode"); | |
316670eb | 1183 | |
0a7de745 | 1184 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jetsam_policy_offset_pages_diagnostic, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jetsam_policy_offset_pages_diagnostic, 0, ""); |
316670eb A |
1185 | |
1186 | #if VM_PRESSURE_EVENTS | |
1187 | ||
0a7de745 | 1188 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_available_pages_pressure, 0, ""); |
316670eb | 1189 | |
39037602 | 1190 | #endif /* VM_PRESSURE_EVENTS */ |
316670eb | 1191 | |
39037602 A |
1192 | #endif /* CONFIG_JETSAM */ |
1193 | ||
1194 | #if CONFIG_FREEZE | |
1195 | ||
0a7de745 A |
1196 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_jetsam_band, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_jetsam_band, 0, ""); |
1197 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_daily_mb_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_daily_mb_max, 0, ""); | |
1198 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_degraded_mode, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_freeze_degradation, 0, ""); | |
39037602 | 1199 | |
0a7de745 | 1200 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_threshold, 0, ""); |
39037602 | 1201 | |
0a7de745 A |
1202 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_min, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_pages_min, 0, ""); |
1203 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_pages_max, 0, ""); | |
39037602 | 1204 | |
0a7de745 A |
1205 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_refreeze_eligible_count, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_refreeze_eligible_count, 0, ""); |
1206 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_processes_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_frozen_processes_max, 0, ""); | |
d9a64523 A |
1207 | |
1208 | /* | |
1209 | * Max. shared-anonymous memory in MB that can be held by frozen processes in the high jetsam band. | |
1210 | * "0" means no limit. | |
1211 | * Default is 10% of system-wide task limit. | |
1212 | */ | |
1213 | ||
0a7de745 A |
1214 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_frozen_shared_mb_max, 0, ""); |
1215 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_frozen_shared_mb, 0, ""); | |
d9a64523 | 1216 | |
0a7de745 A |
1217 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_shared_mb_per_process_max, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_shared_mb_per_process_max, 0, ""); |
1218 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_private_shared_pages_ratio, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_private_shared_pages_ratio, 0, ""); | |
d9a64523 | 1219 | |
0a7de745 | 1220 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_min_processes, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_suspended_threshold, 0, ""); |
39037602 | 1221 | |
d9a64523 A |
1222 | /* |
1223 | * max. # of frozen process demotions we will allow in our daily cycle. | |
1224 | */ | |
0a7de745 | 1225 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_max_freeze_demotions_daily, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_max_frozen_demotions_daily, 0, ""); |
d9a64523 A |
1226 | /* |
1227 | * min # of thaws needed by a process to protect it from getting demoted into the IDLE band. | |
1228 | */ | |
0a7de745 | 1229 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_thaw_count_demotion_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_thaw_count_demotion_threshold, 0, ""); |
d9a64523 | 1230 | |
39037602 | 1231 | boolean_t memorystatus_freeze_throttle_enabled = TRUE; |
0a7de745 | 1232 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_throttle_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_throttle_enabled, 0, ""); |
316670eb | 1233 | |
0a7de745 A |
1234 | /* |
1235 | * When set to true, this keeps frozen processes in the compressor pool in memory, instead of swapping them out to disk. | |
1236 | * Exposed via the sysctl kern.memorystatus_freeze_to_memory. | |
1237 | */ | |
1238 | boolean_t memorystatus_freeze_to_memory = FALSE; | |
1239 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_to_memory, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_freeze_to_memory, 0, ""); | |
1240 | ||
1241 | #define VM_PAGES_FOR_ALL_PROCS (2) | |
1242 | /* | |
39037602 A |
1243 | * Manual trigger of freeze and thaw for dev / debug kernels only. |
1244 | */ | |
316670eb | 1245 | static int |
39037602 | 1246 | sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS |
316670eb A |
1247 | { |
1248 | #pragma unused(arg1, arg2) | |
39037602 A |
1249 | int error, pid = 0; |
1250 | proc_t p; | |
d9a64523 | 1251 | int freezer_error_code = 0; |
316670eb | 1252 | |
39037602 | 1253 | if (memorystatus_freeze_enabled == FALSE) { |
d9a64523 | 1254 | printf("sysctl_freeze: Freeze is DISABLED\n"); |
39037602 A |
1255 | return ENOTSUP; |
1256 | } | |
fe8ab488 | 1257 | |
316670eb | 1258 | error = sysctl_handle_int(oidp, &pid, 0, req); |
0a7de745 A |
1259 | if (error || !req->newptr) { |
1260 | return error; | |
1261 | } | |
316670eb | 1262 | |
d190cdc3 | 1263 | if (pid == VM_PAGES_FOR_ALL_PROCS) { |
3e170ce0 A |
1264 | vm_pageout_anonymous_pages(); |
1265 | ||
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | lck_mtx_lock(&freezer_mutex); | |
1270 | ||
316670eb A |
1271 | p = proc_find(pid); |
1272 | if (p != NULL) { | |
d9a64523 A |
1273 | uint32_t purgeable, wired, clean, dirty, shared; |
1274 | uint32_t max_pages = 0, state = 0; | |
39236c6e | 1275 | |
39037602 | 1276 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { |
39037602 A |
1277 | /* |
1278 | * Freezer backed by the compressor and swap file(s) | |
d9a64523 A |
1279 | * will hold compressed data. |
1280 | * | |
0a7de745 A |
1281 | * Set the sysctl kern.memorystatus_freeze_to_memory to true to keep compressed data from |
1282 | * being swapped out to disk. Note that this disables freezer swap support globally, | |
1283 | * not just for the process being frozen. | |
1284 | * | |
1285 | * | |
d9a64523 A |
1286 | * We don't care about the global freezer budget or the process's (min/max) budget here. |
1287 | * The freeze sysctl is meant to force-freeze a process. | |
1288 | * | |
1289 | * We also don't update any global or process stats on this path, so that the jetsam/ freeze | |
1290 | * logic remains unaffected. The tasks we're performing here are: freeze the process, set the | |
1291 | * P_MEMSTAT_FROZEN bit, and elevate the process to a higher band (if the freezer is active). | |
39037602 | 1292 | */ |
d9a64523 | 1293 | max_pages = memorystatus_freeze_pages_max; |
39236c6e | 1294 | } else { |
3e170ce0 A |
1295 | /* |
1296 | * We only have the compressor without any swap. | |
1297 | */ | |
39236c6e A |
1298 | max_pages = UINT32_MAX - 1; |
1299 | } | |
3e170ce0 | 1300 | |
d9a64523 A |
1301 | proc_list_lock(); |
1302 | state = p->p_memstat_state; | |
1303 | proc_list_unlock(); | |
1304 | ||
1305 | /* | |
1306 | * The jetsam path also verifies that the process is a suspended App. We don't care about that here. | |
1307 | * We simply ensure that jetsam is not already working on the process and that the process has not | |
1308 | * explicitly disabled freezing. | |
1309 | */ | |
1310 | if (state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED)) { | |
1311 | printf("sysctl_freeze: p_memstat_state check failed, process is%s%s%s\n", | |
0a7de745 A |
1312 | (state & P_MEMSTAT_TERMINATED) ? " terminated" : "", |
1313 | (state & P_MEMSTAT_LOCKED) ? " locked" : "", | |
1314 | (state & P_MEMSTAT_FREEZE_DISABLED) ? " unfreezable" : ""); | |
d9a64523 A |
1315 | |
1316 | proc_rele(p); | |
1317 | lck_mtx_unlock(&freezer_mutex); | |
1318 | return EPERM; | |
1319 | } | |
1320 | ||
1321 | error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */); | |
1322 | ||
1323 | if (error) { | |
1324 | char reason[128]; | |
1325 | if (freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) { | |
1326 | strlcpy(reason, "too much shared memory", 128); | |
1327 | } | |
1328 | ||
1329 | if (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) { | |
1330 | strlcpy(reason, "low private-shared pages ratio", 128); | |
1331 | } | |
316670eb | 1332 | |
d9a64523 A |
1333 | if (freezer_error_code == FREEZER_ERROR_NO_COMPRESSOR_SPACE) { |
1334 | strlcpy(reason, "no compressor space", 128); | |
1335 | } | |
1336 | ||
1337 | if (freezer_error_code == FREEZER_ERROR_NO_SWAP_SPACE) { | |
1338 | strlcpy(reason, "no swap space", 128); | |
1339 | } | |
0a7de745 | 1340 | |
d9a64523 A |
1341 | printf("sysctl_freeze: task_freeze failed: %s\n", reason); |
1342 | ||
1343 | if (error == KERN_NO_SPACE) { | |
1344 | /* Make it easy to distinguish between failures due to low compressor/ swap space and other failures. */ | |
1345 | error = ENOSPC; | |
1346 | } else { | |
1347 | error = EIO; | |
1348 | } | |
1349 | } else { | |
1350 | proc_list_lock(); | |
1351 | if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == 0) { | |
1352 | p->p_memstat_state |= P_MEMSTAT_FROZEN; | |
1353 | memorystatus_frozen_count++; | |
1354 | } | |
1355 | p->p_memstat_frozen_count++; | |
1356 | ||
1357 | ||
1358 | proc_list_unlock(); | |
1359 | ||
1360 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { | |
1361 | /* | |
1362 | * We elevate only if we are going to swap out the data. | |
1363 | */ | |
1364 | error = memorystatus_update_inactive_jetsam_priority_band(pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, | |
0a7de745 | 1365 | memorystatus_freeze_jetsam_band, TRUE); |
d9a64523 A |
1366 | |
1367 | if (error) { | |
1368 | printf("sysctl_freeze: Elevating frozen process to higher jetsam band failed with %d\n", error); | |
1369 | } | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | proc_rele(p); | |
3e170ce0 A |
1374 | |
1375 | lck_mtx_unlock(&freezer_mutex); | |
39236c6e | 1376 | return error; |
d9a64523 A |
1377 | } else { |
1378 | printf("sysctl_freeze: Invalid process\n"); | |
39236c6e | 1379 | } |
3e170ce0 | 1380 | |
d9a64523 | 1381 | |
3e170ce0 | 1382 | lck_mtx_unlock(&freezer_mutex); |
316670eb A |
1383 | return EINVAL; |
1384 | } | |
1385 | ||
0a7de745 | 1386 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_freeze, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
316670eb A |
1387 | 0, 0, &sysctl_memorystatus_freeze, "I", ""); |
1388 | ||
1389 | static int | |
1390 | sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS | |
1391 | { | |
1392 | #pragma unused(arg1, arg2) | |
1393 | ||
1394 | int error, pid = 0; | |
1395 | proc_t p; | |
1396 | ||
fe8ab488 A |
1397 | if (memorystatus_freeze_enabled == FALSE) { |
1398 | return ENOTSUP; | |
1399 | } | |
1400 | ||
316670eb | 1401 | error = sysctl_handle_int(oidp, &pid, 0, req); |
0a7de745 A |
1402 | if (error || !req->newptr) { |
1403 | return error; | |
1404 | } | |
316670eb | 1405 | |
d190cdc3 A |
1406 | if (pid == VM_PAGES_FOR_ALL_PROCS) { |
1407 | do_fastwake_warmup_all(); | |
1408 | return 0; | |
1409 | } else { | |
1410 | p = proc_find(pid); | |
1411 | if (p != NULL) { | |
1412 | error = task_thaw(p->task); | |
d190cdc3 | 1413 | |
d9a64523 | 1414 | if (error) { |
d190cdc3 | 1415 | error = EIO; |
d9a64523 A |
1416 | } else { |
1417 | /* | |
1418 | * task_thaw() succeeded. | |
1419 | * | |
1420 | * We increment memorystatus_frozen_count on the sysctl freeze path. | |
1421 | * And so we need the P_MEMSTAT_FROZEN to decrement the frozen count | |
1422 | * when this process exits. | |
1423 | * | |
1424 | * proc_list_lock(); | |
1425 | * p->p_memstat_state &= ~P_MEMSTAT_FROZEN; | |
1426 | * proc_list_unlock(); | |
1427 | */ | |
1428 | } | |
1429 | proc_rele(p); | |
d190cdc3 A |
1430 | return error; |
1431 | } | |
316670eb A |
1432 | } |
1433 | ||
1434 | return EINVAL; | |
1435 | } | |
1436 | ||
0a7de745 | 1437 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_thaw, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
316670eb | 1438 | 0, 0, &sysctl_memorystatus_available_pages_thaw, "I", ""); |
6d2010ae | 1439 | |
0a7de745 | 1440 | typedef struct _global_freezable_status { |
d9a64523 A |
1441 | boolean_t freeze_pages_threshold_crossed; |
1442 | boolean_t freeze_eligible_procs_available; | |
1443 | boolean_t freeze_scheduled_in_future; | |
1444 | }global_freezable_status_t; | |
1445 | ||
0a7de745 A |
1446 | typedef struct _proc_freezable_status { |
1447 | boolean_t freeze_has_memstat_state; | |
1448 | boolean_t freeze_has_pages_min; | |
1449 | int freeze_has_probability; | |
1450 | boolean_t freeze_attempted; | |
1451 | uint32_t p_memstat_state; | |
1452 | uint32_t p_pages; | |
1453 | int p_freeze_error_code; | |
1454 | int p_pid; | |
1455 | char p_name[MAXCOMLEN + 1]; | |
d9a64523 A |
1456 | }proc_freezable_status_t; |
1457 | ||
1458 | #define MAX_FREEZABLE_PROCESSES 100 | |
1459 | ||
1460 | static int | |
0a7de745 | 1461 | memorystatus_freezer_get_status(user_addr_t buffer, size_t buffer_size, int32_t *retval) |
d9a64523 | 1462 | { |
0a7de745 A |
1463 | uint32_t proc_count = 0, i = 0; |
1464 | global_freezable_status_t *list_head; | |
1465 | proc_freezable_status_t *list_entry; | |
1466 | size_t list_size = 0; | |
1467 | proc_t p; | |
1468 | memstat_bucket_t *bucket; | |
1469 | uint32_t state = 0, pages = 0, entry_count = 0; | |
1470 | boolean_t try_freeze = TRUE; | |
1471 | int error = 0, probability_of_use = 0; | |
d9a64523 A |
1472 | |
1473 | ||
1474 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE == FALSE) { | |
1475 | return ENOTSUP; | |
1476 | } | |
1477 | ||
1478 | list_size = sizeof(global_freezable_status_t) + (sizeof(proc_freezable_status_t) * MAX_FREEZABLE_PROCESSES); | |
1479 | ||
1480 | if (buffer_size < list_size) { | |
1481 | return EINVAL; | |
1482 | } | |
1483 | ||
0a7de745 | 1484 | list_head = (global_freezable_status_t*)kalloc(list_size); |
d9a64523 A |
1485 | if (list_head == NULL) { |
1486 | return ENOMEM; | |
1487 | } | |
1488 | ||
1489 | memset(list_head, 0, list_size); | |
1490 | ||
1491 | list_size = sizeof(global_freezable_status_t); | |
1492 | ||
1493 | proc_list_lock(); | |
1494 | ||
1495 | uint64_t curr_time = mach_absolute_time(); | |
1496 | ||
1497 | list_head->freeze_pages_threshold_crossed = (memorystatus_available_pages < memorystatus_freeze_threshold); | |
1498 | list_head->freeze_eligible_procs_available = ((memorystatus_suspended_count - memorystatus_frozen_count) > memorystatus_freeze_suspended_threshold); | |
1499 | list_head->freeze_scheduled_in_future = (curr_time < memorystatus_freezer_thread_next_run_ts); | |
1500 | ||
1501 | list_entry = (proc_freezable_status_t*) ((uintptr_t)list_head + sizeof(global_freezable_status_t)); | |
1502 | ||
1503 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
0a7de745 | 1504 | |
d9a64523 A |
1505 | entry_count = (memorystatus_global_probabilities_size / sizeof(memorystatus_internal_probabilities_t)); |
1506 | ||
1507 | p = memorystatus_get_first_proc_locked(&i, FALSE); | |
1508 | proc_count++; | |
1509 | ||
1510 | while ((proc_count <= MAX_FREEZABLE_PROCESSES) && | |
0a7de745 A |
1511 | (p) && |
1512 | (list_size < buffer_size)) { | |
d9a64523 A |
1513 | if (isApp(p) == FALSE) { |
1514 | p = memorystatus_get_next_proc_locked(&i, p, FALSE); | |
1515 | proc_count++; | |
1516 | continue; | |
1517 | } | |
1518 | ||
1519 | strlcpy(list_entry->p_name, p->p_name, MAXCOMLEN + 1); | |
1520 | ||
1521 | list_entry->p_pid = p->p_pid; | |
0a7de745 | 1522 | |
d9a64523 A |
1523 | state = p->p_memstat_state; |
1524 | ||
1525 | if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED | P_MEMSTAT_FREEZE_IGNORE)) || | |
0a7de745 | 1526 | !(state & P_MEMSTAT_SUSPENDED)) { |
d9a64523 A |
1527 | try_freeze = list_entry->freeze_has_memstat_state = FALSE; |
1528 | } else { | |
1529 | try_freeze = list_entry->freeze_has_memstat_state = TRUE; | |
1530 | } | |
1531 | ||
1532 | list_entry->p_memstat_state = state; | |
1533 | ||
1534 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); | |
1535 | if (pages < memorystatus_freeze_pages_min) { | |
1536 | try_freeze = list_entry->freeze_has_pages_min = FALSE; | |
1537 | } else { | |
1538 | list_entry->freeze_has_pages_min = TRUE; | |
1539 | if (try_freeze != FALSE) { | |
1540 | try_freeze = TRUE; | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | list_entry->p_pages = pages; | |
0a7de745 | 1545 | |
d9a64523 A |
1546 | if (entry_count) { |
1547 | uint32_t j = 0; | |
0a7de745 | 1548 | for (j = 0; j < entry_count; j++) { |
d9a64523 | 1549 | if (strncmp(memorystatus_global_probabilities_table[j].proc_name, |
0a7de745 A |
1550 | p->p_name, |
1551 | MAXCOMLEN + 1) == 0) { | |
d9a64523 A |
1552 | probability_of_use = memorystatus_global_probabilities_table[j].use_probability; |
1553 | break; | |
1554 | } | |
1555 | } | |
1556 | ||
1557 | list_entry->freeze_has_probability = probability_of_use; | |
1558 | ||
1559 | if (probability_of_use && try_freeze != FALSE) { | |
1560 | try_freeze = TRUE; | |
1561 | } else { | |
1562 | try_freeze = FALSE; | |
1563 | } | |
1564 | } else { | |
1565 | if (try_freeze != FALSE) { | |
1566 | try_freeze = TRUE; | |
1567 | } | |
1568 | list_entry->freeze_has_probability = -1; | |
1569 | } | |
1570 | ||
1571 | if (try_freeze) { | |
d9a64523 A |
1572 | uint32_t purgeable, wired, clean, dirty, shared; |
1573 | uint32_t max_pages = 0; | |
1574 | int freezer_error_code = 0; | |
1575 | ||
1576 | error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, TRUE /* eval only */); | |
1577 | ||
1578 | if (error) { | |
1579 | list_entry->p_freeze_error_code = freezer_error_code; | |
1580 | } | |
1581 | ||
1582 | list_entry->freeze_attempted = TRUE; | |
1583 | } | |
1584 | ||
1585 | list_entry++; | |
1586 | ||
1587 | list_size += sizeof(proc_freezable_status_t); | |
0a7de745 | 1588 | |
d9a64523 A |
1589 | p = memorystatus_get_next_proc_locked(&i, p, FALSE); |
1590 | proc_count++; | |
1591 | } | |
0a7de745 | 1592 | |
d9a64523 A |
1593 | proc_list_unlock(); |
1594 | ||
1595 | buffer_size = list_size; | |
1596 | ||
1597 | error = copyout(list_head, buffer, buffer_size); | |
1598 | if (error == 0) { | |
1599 | *retval = buffer_size; | |
1600 | } else { | |
1601 | *retval = 0; | |
1602 | } | |
1603 | ||
1604 | list_size = sizeof(global_freezable_status_t) + (sizeof(proc_freezable_status_t) * MAX_FREEZABLE_PROCESSES); | |
1605 | kfree(list_head, list_size); | |
1606 | ||
1607 | MEMORYSTATUS_DEBUG(1, "memorystatus_freezer_get_status: returning %d (%lu - size)\n", error, (unsigned long)*list_size); | |
0a7de745 | 1608 | |
d9a64523 A |
1609 | return error; |
1610 | } | |
1611 | ||
1612 | static int | |
1613 | memorystatus_freezer_control(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) | |
1614 | { | |
1615 | int err = ENOTSUP; | |
1616 | ||
1617 | if (flags == FREEZER_CONTROL_GET_STATUS) { | |
1618 | err = memorystatus_freezer_get_status(buffer, buffer_size, retval); | |
1619 | } | |
1620 | ||
1621 | return err; | |
1622 | } | |
1623 | ||
6d2010ae | 1624 | #endif /* CONFIG_FREEZE */ |
2d21ac55 | 1625 | |
fe8ab488 A |
1626 | #endif /* DEVELOPMENT || DEBUG */ |
1627 | ||
39236c6e | 1628 | extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation, |
0a7de745 A |
1629 | void *parameter, |
1630 | integer_t priority, | |
1631 | thread_t *new_thread); | |
39236c6e | 1632 | |
39037602 A |
1633 | #if DEVELOPMENT || DEBUG |
1634 | ||
1635 | static int | |
1636 | sysctl_memorystatus_disconnect_page_mappings SYSCTL_HANDLER_ARGS | |
1637 | { | |
1638 | #pragma unused(arg1, arg2) | |
0a7de745 A |
1639 | int error = 0, pid = 0; |
1640 | proc_t p; | |
39037602 A |
1641 | |
1642 | error = sysctl_handle_int(oidp, &pid, 0, req); | |
0a7de745 A |
1643 | if (error || !req->newptr) { |
1644 | return error; | |
1645 | } | |
39037602 A |
1646 | |
1647 | lck_mtx_lock(&disconnect_page_mappings_mutex); | |
1648 | ||
1649 | if (pid == -1) { | |
1650 | vm_pageout_disconnect_all_pages(); | |
1651 | } else { | |
1652 | p = proc_find(pid); | |
1653 | ||
1654 | if (p != NULL) { | |
1655 | error = task_disconnect_page_mappings(p->task); | |
1656 | ||
1657 | proc_rele(p); | |
1658 | ||
0a7de745 | 1659 | if (error) { |
39037602 | 1660 | error = EIO; |
0a7de745 A |
1661 | } |
1662 | } else { | |
39037602 | 1663 | error = EINVAL; |
0a7de745 | 1664 | } |
39037602 A |
1665 | } |
1666 | lck_mtx_unlock(&disconnect_page_mappings_mutex); | |
1667 | ||
1668 | return error; | |
1669 | } | |
1670 | ||
0a7de745 | 1671 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_disconnect_page_mappings, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
39037602 A |
1672 | 0, 0, &sysctl_memorystatus_disconnect_page_mappings, "I", ""); |
1673 | ||
1674 | #endif /* DEVELOPMENT || DEBUG */ | |
1675 | ||
1676 | ||
3e170ce0 A |
1677 | /* |
1678 | * Picks the sorting routine for a given jetsam priority band. | |
1679 | * | |
1680 | * Input: | |
1681 | * bucket_index - jetsam priority band to be sorted. | |
1682 | * sort_order - JETSAM_SORT_xxx from kern_memorystatus.h | |
1683 | * Currently sort_order is only meaningful when handling | |
1684 | * coalitions. | |
1685 | * | |
0a7de745 | 1686 | * Return: |
3e170ce0 | 1687 | * 0 on success |
0a7de745 | 1688 | * non-0 on failure |
3e170ce0 | 1689 | */ |
0a7de745 A |
1690 | static int |
1691 | memorystatus_sort_bucket(unsigned int bucket_index, int sort_order) | |
3e170ce0 A |
1692 | { |
1693 | int coal_sort_order; | |
1694 | ||
1695 | /* | |
1696 | * Verify the jetsam priority | |
1697 | */ | |
0a7de745 A |
1698 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
1699 | return EINVAL; | |
1700 | } | |
3e170ce0 A |
1701 | |
1702 | #if DEVELOPMENT || DEBUG | |
0a7de745 | 1703 | if (sort_order == JETSAM_SORT_DEFAULT) { |
3e170ce0 A |
1704 | coal_sort_order = COALITION_SORT_DEFAULT; |
1705 | } else { | |
0a7de745 | 1706 | coal_sort_order = sort_order; /* only used for testing scenarios */ |
3e170ce0 A |
1707 | } |
1708 | #else | |
1709 | /* Verify default */ | |
0a7de745 | 1710 | if (sort_order == JETSAM_SORT_DEFAULT) { |
3e170ce0 A |
1711 | coal_sort_order = COALITION_SORT_DEFAULT; |
1712 | } else { | |
0a7de745 | 1713 | return EINVAL; |
3e170ce0 A |
1714 | } |
1715 | #endif | |
1716 | ||
1717 | proc_list_lock(); | |
0a7de745 | 1718 | |
5ba3f43e A |
1719 | if (memstat_bucket[bucket_index].count == 0) { |
1720 | proc_list_unlock(); | |
0a7de745 | 1721 | return 0; |
5ba3f43e A |
1722 | } |
1723 | ||
3e170ce0 A |
1724 | switch (bucket_index) { |
1725 | case JETSAM_PRIORITY_FOREGROUND: | |
1726 | if (memorystatus_sort_by_largest_coalition_locked(bucket_index, coal_sort_order) == 0) { | |
1727 | /* | |
1728 | * Fall back to per process sorting when zero coalitions are found. | |
1729 | */ | |
1730 | memorystatus_sort_by_largest_process_locked(bucket_index); | |
1731 | } | |
1732 | break; | |
1733 | default: | |
1734 | memorystatus_sort_by_largest_process_locked(bucket_index); | |
1735 | break; | |
1736 | } | |
1737 | proc_list_unlock(); | |
0a7de745 A |
1738 | |
1739 | return 0; | |
3e170ce0 A |
1740 | } |
1741 | ||
fe8ab488 A |
1742 | /* |
1743 | * Sort processes by size for a single jetsam bucket. | |
1744 | */ | |
1745 | ||
0a7de745 A |
1746 | static void |
1747 | memorystatus_sort_by_largest_process_locked(unsigned int bucket_index) | |
fe8ab488 A |
1748 | { |
1749 | proc_t p = NULL, insert_after_proc = NULL, max_proc = NULL; | |
3e170ce0 | 1750 | proc_t next_p = NULL, prev_max_proc = NULL; |
fe8ab488 A |
1751 | uint32_t pages = 0, max_pages = 0; |
1752 | memstat_bucket_t *current_bucket; | |
0a7de745 | 1753 | |
fe8ab488 A |
1754 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { |
1755 | return; | |
1756 | } | |
0a7de745 | 1757 | |
fe8ab488 A |
1758 | current_bucket = &memstat_bucket[bucket_index]; |
1759 | ||
1760 | p = TAILQ_FIRST(¤t_bucket->list); | |
1761 | ||
3e170ce0 | 1762 | while (p) { |
d9a64523 | 1763 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); |
fe8ab488 | 1764 | max_pages = pages; |
3e170ce0 A |
1765 | max_proc = p; |
1766 | prev_max_proc = p; | |
0a7de745 | 1767 | |
3e170ce0 A |
1768 | while ((next_p = TAILQ_NEXT(p, p_memstat_list)) != NULL) { |
1769 | /* traversing list until we find next largest process */ | |
0a7de745 | 1770 | p = next_p; |
d9a64523 | 1771 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); |
fe8ab488 A |
1772 | if (pages > max_pages) { |
1773 | max_pages = pages; | |
1774 | max_proc = p; | |
1775 | } | |
fe8ab488 A |
1776 | } |
1777 | ||
3e170ce0 A |
1778 | if (prev_max_proc != max_proc) { |
1779 | /* found a larger process, place it in the list */ | |
fe8ab488 | 1780 | TAILQ_REMOVE(¤t_bucket->list, max_proc, p_memstat_list); |
fe8ab488 A |
1781 | if (insert_after_proc == NULL) { |
1782 | TAILQ_INSERT_HEAD(¤t_bucket->list, max_proc, p_memstat_list); | |
1783 | } else { | |
1784 | TAILQ_INSERT_AFTER(¤t_bucket->list, insert_after_proc, max_proc, p_memstat_list); | |
1785 | } | |
3e170ce0 A |
1786 | prev_max_proc = max_proc; |
1787 | } | |
fe8ab488 | 1788 | |
3e170ce0 | 1789 | insert_after_proc = max_proc; |
fe8ab488 | 1790 | |
3e170ce0 | 1791 | p = TAILQ_NEXT(max_proc, p_memstat_list); |
fe8ab488 A |
1792 | } |
1793 | } | |
1794 | ||
0a7de745 A |
1795 | static proc_t |
1796 | memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) | |
1797 | { | |
39236c6e A |
1798 | memstat_bucket_t *current_bucket; |
1799 | proc_t next_p; | |
1800 | ||
1801 | if ((*bucket_index) >= MEMSTAT_BUCKET_COUNT) { | |
1802 | return NULL; | |
1803 | } | |
1804 | ||
1805 | current_bucket = &memstat_bucket[*bucket_index]; | |
1806 | next_p = TAILQ_FIRST(¤t_bucket->list); | |
1807 | if (!next_p && search) { | |
1808 | while (!next_p && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) { | |
1809 | current_bucket = &memstat_bucket[*bucket_index]; | |
1810 | next_p = TAILQ_FIRST(¤t_bucket->list); | |
1811 | } | |
1812 | } | |
0a7de745 | 1813 | |
39236c6e A |
1814 | return next_p; |
1815 | } | |
1816 | ||
0a7de745 A |
1817 | static proc_t |
1818 | memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search) | |
1819 | { | |
39236c6e A |
1820 | memstat_bucket_t *current_bucket; |
1821 | proc_t next_p; | |
0a7de745 | 1822 | |
39236c6e A |
1823 | if (!p || ((*bucket_index) >= MEMSTAT_BUCKET_COUNT)) { |
1824 | return NULL; | |
1825 | } | |
1826 | ||
1827 | next_p = TAILQ_NEXT(p, p_memstat_list); | |
1828 | while (!next_p && search && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) { | |
1829 | current_bucket = &memstat_bucket[*bucket_index]; | |
1830 | next_p = TAILQ_FIRST(¤t_bucket->list); | |
1831 | } | |
1832 | ||
1833 | return next_p; | |
1834 | } | |
316670eb | 1835 | |
d9a64523 A |
1836 | /* |
1837 | * Structure to hold state for a jetsam thread. | |
1838 | * Typically there should be a single jetsam thread | |
1839 | * unless parallel jetsam is enabled. | |
1840 | */ | |
1841 | struct jetsam_thread_state { | |
0a7de745 A |
1842 | boolean_t inited; /* if the thread is initialized */ |
1843 | int memorystatus_wakeup; /* wake channel */ | |
1844 | int index; /* jetsam thread index */ | |
1845 | thread_t thread; /* jetsam thread pointer */ | |
d9a64523 A |
1846 | } *jetsam_threads; |
1847 | ||
1848 | /* Maximum number of jetsam threads allowed */ | |
1849 | #define JETSAM_THREADS_LIMIT 3 | |
1850 | ||
1851 | /* Number of active jetsam threads */ | |
1852 | _Atomic int active_jetsam_threads = 1; | |
1853 | ||
1854 | /* Number of maximum jetsam threads configured */ | |
1855 | int max_jetsam_threads = JETSAM_THREADS_LIMIT; | |
1856 | ||
1857 | /* | |
1858 | * Global switch for enabling fast jetsam. Fast jetsam is | |
1859 | * hooked up via the system_override() system call. It has the | |
1860 | * following effects: | |
1861 | * - Raise the jetsam threshold ("clear-the-deck") | |
1862 | * - Enabled parallel jetsam on eligible devices | |
1863 | */ | |
1864 | int fast_jetsam_enabled = 0; | |
1865 | ||
1866 | /* Routine to find the jetsam state structure for the current jetsam thread */ | |
1867 | static inline struct jetsam_thread_state * | |
1868 | jetsam_current_thread(void) | |
1869 | { | |
1870 | for (int thr_id = 0; thr_id < max_jetsam_threads; thr_id++) { | |
0a7de745 | 1871 | if (jetsam_threads[thr_id].thread == current_thread()) { |
d9a64523 | 1872 | return &(jetsam_threads[thr_id]); |
0a7de745 | 1873 | } |
d9a64523 A |
1874 | } |
1875 | panic("jetsam_current_thread() is being called from a non-jetsam thread\n"); | |
1876 | /* Contol should not reach here */ | |
1877 | return NULL; | |
1878 | } | |
1879 | ||
1880 | ||
316670eb A |
1881 | __private_extern__ void |
1882 | memorystatus_init(void) | |
1883 | { | |
316670eb | 1884 | kern_return_t result; |
39236c6e A |
1885 | int i; |
1886 | ||
fe8ab488 | 1887 | #if CONFIG_FREEZE |
d9a64523 A |
1888 | memorystatus_freeze_jetsam_band = JETSAM_PRIORITY_UI_SUPPORT; |
1889 | memorystatus_frozen_processes_max = FREEZE_PROCESSES_MAX; | |
1890 | memorystatus_frozen_shared_mb_max = ((MAX_FROZEN_SHARED_MB_PERCENT * max_task_footprint_mb) / 100); /* 10% of the system wide task limit */ | |
1891 | memorystatus_freeze_shared_mb_per_process_max = (memorystatus_frozen_shared_mb_max / 4); | |
fe8ab488 A |
1892 | memorystatus_freeze_pages_min = FREEZE_PAGES_MIN; |
1893 | memorystatus_freeze_pages_max = FREEZE_PAGES_MAX; | |
d9a64523 A |
1894 | memorystatus_max_frozen_demotions_daily = MAX_FROZEN_PROCESS_DEMOTIONS; |
1895 | memorystatus_thaw_count_demotion_threshold = MIN_THAW_DEMOTION_THRESHOLD; | |
fe8ab488 A |
1896 | #endif |
1897 | ||
39037602 A |
1898 | #if DEVELOPMENT || DEBUG |
1899 | disconnect_page_mappings_lck_grp_attr = lck_grp_attr_alloc_init(); | |
1900 | disconnect_page_mappings_lck_grp = lck_grp_alloc_init("disconnect_page_mappings", disconnect_page_mappings_lck_grp_attr); | |
1901 | ||
1902 | lck_mtx_init(&disconnect_page_mappings_mutex, disconnect_page_mappings_lck_grp, NULL); | |
5ba3f43e A |
1903 | |
1904 | if (kill_on_no_paging_space == TRUE) { | |
1905 | max_kill_priority = JETSAM_PRIORITY_MAX; | |
1906 | } | |
0a7de745 A |
1907 | #endif |
1908 | ||
39037602 | 1909 | |
39236c6e A |
1910 | /* Init buckets */ |
1911 | for (i = 0; i < MEMSTAT_BUCKET_COUNT; i++) { | |
1912 | TAILQ_INIT(&memstat_bucket[i].list); | |
1913 | memstat_bucket[i].count = 0; | |
1914 | } | |
39236c6e | 1915 | memorystatus_idle_demotion_call = thread_call_allocate((thread_call_func_t)memorystatus_perform_idle_demotion, NULL); |
316670eb | 1916 | |
5ba3f43e A |
1917 | #if CONFIG_JETSAM |
1918 | nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time); | |
1919 | nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time); | |
0a7de745 | 1920 | |
39236c6e A |
1921 | /* Apply overrides */ |
1922 | PE_get_default("kern.jetsam_delta", &delta_percentage, sizeof(delta_percentage)); | |
39037602 A |
1923 | if (delta_percentage == 0) { |
1924 | delta_percentage = 5; | |
1925 | } | |
39236c6e A |
1926 | assert(delta_percentage < 100); |
1927 | PE_get_default("kern.jetsam_critical_threshold", &critical_threshold_percentage, sizeof(critical_threshold_percentage)); | |
1928 | assert(critical_threshold_percentage < 100); | |
1929 | PE_get_default("kern.jetsam_idle_offset", &idle_offset_percentage, sizeof(idle_offset_percentage)); | |
1930 | assert(idle_offset_percentage < 100); | |
1931 | PE_get_default("kern.jetsam_pressure_threshold", &pressure_threshold_percentage, sizeof(pressure_threshold_percentage)); | |
1932 | assert(pressure_threshold_percentage < 100); | |
1933 | PE_get_default("kern.jetsam_freeze_threshold", &freeze_threshold_percentage, sizeof(freeze_threshold_percentage)); | |
1934 | assert(freeze_threshold_percentage < 100); | |
39037602 | 1935 | |
0a7de745 A |
1936 | if (!PE_parse_boot_argn("jetsam_aging_policy", &jetsam_aging_policy, |
1937 | sizeof(jetsam_aging_policy))) { | |
39037602 | 1938 | if (!PE_get_default("kern.jetsam_aging_policy", &jetsam_aging_policy, |
0a7de745 | 1939 | sizeof(jetsam_aging_policy))) { |
39037602 A |
1940 | jetsam_aging_policy = kJetsamAgingPolicyLegacy; |
1941 | } | |
1942 | } | |
1943 | ||
1944 | if (jetsam_aging_policy > kJetsamAgingPolicyMax) { | |
1945 | jetsam_aging_policy = kJetsamAgingPolicyLegacy; | |
1946 | } | |
1947 | ||
1948 | switch (jetsam_aging_policy) { | |
0a7de745 A |
1949 | case kJetsamAgingPolicyNone: |
1950 | system_procs_aging_band = JETSAM_PRIORITY_IDLE; | |
1951 | applications_aging_band = JETSAM_PRIORITY_IDLE; | |
1952 | break; | |
39037602 | 1953 | |
0a7de745 A |
1954 | case kJetsamAgingPolicyLegacy: |
1955 | /* | |
1956 | * Legacy behavior where some daemons get a 10s protection once | |
1957 | * AND only before the first clean->dirty->clean transition before | |
1958 | * going into IDLE band. | |
1959 | */ | |
1960 | system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
1961 | applications_aging_band = JETSAM_PRIORITY_IDLE; | |
1962 | break; | |
39037602 | 1963 | |
0a7de745 A |
1964 | case kJetsamAgingPolicySysProcsReclaimedFirst: |
1965 | system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
1966 | applications_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
1967 | break; | |
39037602 | 1968 | |
0a7de745 A |
1969 | case kJetsamAgingPolicyAppsReclaimedFirst: |
1970 | system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND2; | |
1971 | applications_aging_band = JETSAM_PRIORITY_AGING_BAND1; | |
1972 | break; | |
39037602 | 1973 | |
0a7de745 A |
1974 | default: |
1975 | break; | |
39037602 A |
1976 | } |
1977 | ||
1978 | /* | |
1979 | * The aging bands cannot overlap with the JETSAM_PRIORITY_ELEVATED_INACTIVE | |
1980 | * band and must be below it in priority. This is so that we don't have to make | |
1981 | * our 'aging' code worry about a mix of processes, some of which need to age | |
1982 | * and some others that need to stay elevated in the jetsam bands. | |
1983 | */ | |
1984 | assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > system_procs_aging_band); | |
1985 | assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > applications_aging_band); | |
1986 | ||
39037602 | 1987 | /* Take snapshots for idle-exit kills by default? First check the boot-arg... */ |
0a7de745 A |
1988 | if (!PE_parse_boot_argn("jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot))) { |
1989 | /* ...no boot-arg, so check the device tree */ | |
1990 | PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot)); | |
39037602 | 1991 | } |
3e170ce0 | 1992 | |
39236c6e | 1993 | memorystatus_delta = delta_percentage * atop_64(max_mem) / 100; |
39236c6e | 1994 | memorystatus_available_pages_critical_idle_offset = idle_offset_percentage * atop_64(max_mem) / 100; |
39236c6e | 1995 | memorystatus_available_pages_critical_base = (critical_threshold_percentage / delta_percentage) * memorystatus_delta; |
39037602 | 1996 | memorystatus_policy_more_free_offset_pages = (policy_more_free_offset_percentage / delta_percentage) * memorystatus_delta; |
0a7de745 | 1997 | |
5ba3f43e A |
1998 | /* Jetsam Loop Detection */ |
1999 | if (max_mem <= (512 * 1024 * 1024)) { | |
2000 | /* 512 MB devices */ | |
0a7de745 | 2001 | memorystatus_jld_eval_period_msecs = 8000; /* 8000 msecs == 8 second window */ |
5ba3f43e A |
2002 | } else { |
2003 | /* 1GB and larger devices */ | |
0a7de745 | 2004 | memorystatus_jld_eval_period_msecs = 6000; /* 6000 msecs == 6 second window */ |
5ba3f43e A |
2005 | } |
2006 | ||
2007 | memorystatus_jld_enabled = TRUE; | |
2008 | ||
2009 | /* No contention at this point */ | |
2010 | memorystatus_update_levels_locked(FALSE); | |
2011 | ||
2012 | #endif /* CONFIG_JETSAM */ | |
2013 | ||
39236c6e | 2014 | memorystatus_jetsam_snapshot_max = maxproc; |
d9a64523 A |
2015 | |
2016 | memorystatus_jetsam_snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + | |
0a7de745 | 2017 | (sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_max); |
d9a64523 | 2018 | |
0a7de745 A |
2019 | memorystatus_jetsam_snapshot = |
2020 | (memorystatus_jetsam_snapshot_t*)kalloc(memorystatus_jetsam_snapshot_size); | |
39236c6e A |
2021 | if (!memorystatus_jetsam_snapshot) { |
2022 | panic("Could not allocate memorystatus_jetsam_snapshot"); | |
2023 | } | |
2024 | ||
d9a64523 | 2025 | memorystatus_jetsam_snapshot_copy = |
0a7de745 | 2026 | (memorystatus_jetsam_snapshot_t*)kalloc(memorystatus_jetsam_snapshot_size); |
d9a64523 A |
2027 | if (!memorystatus_jetsam_snapshot_copy) { |
2028 | panic("Could not allocate memorystatus_jetsam_snapshot_copy"); | |
2029 | } | |
2030 | ||
3e170ce0 A |
2031 | nanoseconds_to_absolutetime((uint64_t)JETSAM_SNAPSHOT_TIMEOUT_SECS * NSEC_PER_SEC, &memorystatus_jetsam_snapshot_timeout); |
2032 | ||
2033 | memset(&memorystatus_at_boot_snapshot, 0, sizeof(memorystatus_jetsam_snapshot_t)); | |
2034 | ||
316670eb | 2035 | #if CONFIG_FREEZE |
39236c6e | 2036 | memorystatus_freeze_threshold = (freeze_threshold_percentage / delta_percentage) * memorystatus_delta; |
316670eb | 2037 | #endif |
0a7de745 | 2038 | |
d9a64523 | 2039 | /* Check the boot-arg to see if fast jetsam is allowed */ |
0a7de745 | 2040 | if (!PE_parse_boot_argn("fast_jetsam_enabled", &fast_jetsam_enabled, sizeof(fast_jetsam_enabled))) { |
d9a64523 A |
2041 | fast_jetsam_enabled = 0; |
2042 | } | |
2043 | ||
2044 | /* Check the boot-arg to configure the maximum number of jetsam threads */ | |
0a7de745 | 2045 | if (!PE_parse_boot_argn("max_jetsam_threads", &max_jetsam_threads, sizeof(max_jetsam_threads))) { |
d9a64523 A |
2046 | max_jetsam_threads = JETSAM_THREADS_LIMIT; |
2047 | } | |
2048 | ||
2049 | /* Restrict the maximum number of jetsam threads to JETSAM_THREADS_LIMIT */ | |
2050 | if (max_jetsam_threads > JETSAM_THREADS_LIMIT) { | |
2051 | max_jetsam_threads = JETSAM_THREADS_LIMIT; | |
0a7de745 | 2052 | } |
d9a64523 A |
2053 | |
2054 | /* For low CPU systems disable fast jetsam mechanism */ | |
2055 | if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) { | |
2056 | max_jetsam_threads = 1; | |
2057 | fast_jetsam_enabled = 0; | |
2058 | } | |
2059 | ||
2060 | /* Initialize the jetsam_threads state array */ | |
2061 | jetsam_threads = kalloc(sizeof(struct jetsam_thread_state) * max_jetsam_threads); | |
2062 | ||
2063 | /* Initialize all the jetsam threads */ | |
2064 | for (i = 0; i < max_jetsam_threads; i++) { | |
d9a64523 A |
2065 | result = kernel_thread_start_priority(memorystatus_thread, NULL, 95 /* MAXPRI_KERNEL */, &jetsam_threads[i].thread); |
2066 | if (result == KERN_SUCCESS) { | |
2067 | jetsam_threads[i].inited = FALSE; | |
2068 | jetsam_threads[i].index = i; | |
2069 | thread_deallocate(jetsam_threads[i].thread); | |
2070 | } else { | |
2071 | panic("Could not create memorystatus_thread %d", i); | |
2072 | } | |
316670eb | 2073 | } |
39236c6e | 2074 | } |
316670eb | 2075 | |
39236c6e A |
2076 | /* Centralised for the purposes of allowing panic-on-jetsam */ |
2077 | extern void | |
39037602 | 2078 | vm_run_compactor(void); |
316670eb | 2079 | |
fe8ab488 A |
2080 | /* |
2081 | * The jetsam no frills kill call | |
0a7de745 | 2082 | * Return: 0 on success |
fe8ab488 A |
2083 | * error code on failure (EINVAL...) |
2084 | */ | |
2085 | static int | |
0a7de745 A |
2086 | jetsam_do_kill(proc_t p, int jetsam_flags, os_reason_t jetsam_reason) |
2087 | { | |
fe8ab488 | 2088 | int error = 0; |
39037602 | 2089 | error = exit_with_reason(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags, jetsam_reason); |
0a7de745 | 2090 | return error; |
fe8ab488 A |
2091 | } |
2092 | ||
2093 | /* | |
2094 | * Wrapper for processes exiting with memorystatus details | |
2095 | */ | |
39236c6e | 2096 | static boolean_t |
0a7de745 A |
2097 | memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason) |
2098 | { | |
fe8ab488 A |
2099 | int error = 0; |
2100 | __unused pid_t victim_pid = p->p_pid; | |
2101 | ||
0a7de745 A |
2102 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_START, |
2103 | victim_pid, cause, vm_page_free_count, 0, 0); | |
39236c6e | 2104 | |
39037602 | 2105 | DTRACE_MEMORYSTATUS3(memorystatus_do_kill, proc_t, p, os_reason_t, jetsam_reason, uint32_t, cause); |
39236c6e A |
2106 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
2107 | if (memorystatus_jetsam_panic_debug & (1 << cause)) { | |
2108 | panic("memorystatus_do_kill(): jetsam debug panic (cause: %d)", cause); | |
316670eb | 2109 | } |
39236c6e A |
2110 | #else |
2111 | #pragma unused(cause) | |
316670eb | 2112 | #endif |
5ba3f43e A |
2113 | |
2114 | if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) { | |
2115 | printf("memorystatus: killing process %d [%s] in high band %s (%d) - memorystatus_available_pages: %llu\n", p->p_pid, | |
0a7de745 A |
2116 | (*p->p_name ? p->p_name : "unknown"), |
2117 | memorystatus_priority_band_name(p->p_memstat_effectivepriority), p->p_memstat_effectivepriority, | |
2118 | (uint64_t)memorystatus_available_pages); | |
5ba3f43e A |
2119 | } |
2120 | ||
d9a64523 A |
2121 | /* |
2122 | * The jetsam_reason (os_reason_t) has enough information about the kill cause. | |
2123 | * We don't really need jetsam_flags anymore, so it's okay that not all possible kill causes have been mapped. | |
2124 | */ | |
39236c6e A |
2125 | int jetsam_flags = P_LTERM_JETSAM; |
2126 | switch (cause) { | |
0a7de745 A |
2127 | case kMemorystatusKilledHiwat: jetsam_flags |= P_JETSAM_HIWAT; break; |
2128 | case kMemorystatusKilledVnodes: jetsam_flags |= P_JETSAM_VNODE; break; | |
2129 | case kMemorystatusKilledVMPageShortage: jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break; | |
2130 | case kMemorystatusKilledVMCompressorThrashing: | |
2131 | case kMemorystatusKilledVMCompressorSpaceShortage: jetsam_flags |= P_JETSAM_VMTHRASHING; break; | |
2132 | case kMemorystatusKilledFCThrashing: jetsam_flags |= P_JETSAM_FCTHRASHING; break; | |
2133 | case kMemorystatusKilledPerProcessLimit: jetsam_flags |= P_JETSAM_PID; break; | |
2134 | case kMemorystatusKilledIdleExit: jetsam_flags |= P_JETSAM_IDLEEXIT; break; | |
39236c6e | 2135 | } |
39037602 | 2136 | error = jetsam_do_kill(p, jetsam_flags, jetsam_reason); |
fe8ab488 | 2137 | |
0a7de745 A |
2138 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_END, |
2139 | victim_pid, cause, vm_page_free_count, error, 0); | |
39236c6e | 2140 | |
39037602 | 2141 | vm_run_compactor(); |
fe8ab488 | 2142 | |
0a7de745 | 2143 | return error == 0; |
316670eb A |
2144 | } |
2145 | ||
2146 | /* | |
2147 | * Node manipulation | |
2148 | */ | |
2149 | ||
2150 | static void | |
0a7de745 A |
2151 | memorystatus_check_levels_locked(void) |
2152 | { | |
39236c6e A |
2153 | #if CONFIG_JETSAM |
2154 | /* Update levels */ | |
2155 | memorystatus_update_levels_locked(TRUE); | |
5ba3f43e A |
2156 | #else /* CONFIG_JETSAM */ |
2157 | /* | |
2158 | * Nothing to do here currently since we update | |
2159 | * memorystatus_available_pages in vm_pressure_response. | |
2160 | */ | |
2161 | #endif /* CONFIG_JETSAM */ | |
39236c6e | 2162 | } |
316670eb | 2163 | |
0a7de745 | 2164 | /* |
39037602 A |
2165 | * Pin a process to a particular jetsam band when it is in the background i.e. not doing active work. |
2166 | * For an application: that means no longer in the FG band | |
2167 | * For a daemon: that means no longer in its 'requested' jetsam priority band | |
2168 | */ | |
2169 | ||
2170 | int | |
d9a64523 | 2171 | memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, int jetsam_prio, boolean_t effective_now) |
39037602 | 2172 | { |
0a7de745 | 2173 | int error = 0; |
39037602 | 2174 | boolean_t enable = FALSE; |
0a7de745 | 2175 | proc_t p = NULL; |
39037602 A |
2176 | |
2177 | if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE) { | |
2178 | enable = TRUE; | |
2179 | } else if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE) { | |
2180 | enable = FALSE; | |
2181 | } else { | |
2182 | return EINVAL; | |
2183 | } | |
2184 | ||
2185 | p = proc_find(pid); | |
2186 | if (p != NULL) { | |
39037602 A |
2187 | if ((enable && ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) == P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) || |
2188 | (!enable && ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) == 0))) { | |
2189 | /* | |
2190 | * No change in state. | |
2191 | */ | |
39037602 | 2192 | } else { |
39037602 A |
2193 | proc_list_lock(); |
2194 | ||
2195 | if (enable) { | |
2196 | p->p_memstat_state |= P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; | |
2197 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2198 | ||
2199 | if (effective_now) { | |
d9a64523 | 2200 | if (p->p_memstat_effectivepriority < jetsam_prio) { |
0a7de745 | 2201 | if (memorystatus_highwater_enabled) { |
813fb2f6 A |
2202 | /* |
2203 | * Process is about to transition from | |
2204 | * inactive --> active | |
2205 | * assign active state | |
2206 | */ | |
2207 | boolean_t is_fatal; | |
2208 | boolean_t use_active = TRUE; | |
2209 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); | |
2210 | task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, use_active, is_fatal); | |
2211 | } | |
d9a64523 | 2212 | memorystatus_update_priority_locked(p, jetsam_prio, FALSE, FALSE); |
39037602 A |
2213 | } |
2214 | } else { | |
2215 | if (isProcessInAgingBands(p)) { | |
2216 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); | |
2217 | } | |
2218 | } | |
2219 | } else { | |
39037602 A |
2220 | p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; |
2221 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2222 | ||
2223 | if (effective_now) { | |
d9a64523 | 2224 | if (p->p_memstat_effectivepriority == jetsam_prio) { |
39037602 A |
2225 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); |
2226 | } | |
2227 | } else { | |
2228 | if (isProcessInAgingBands(p)) { | |
2229 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); | |
2230 | } | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | proc_list_unlock(); | |
2235 | } | |
2236 | proc_rele(p); | |
2237 | error = 0; | |
39037602 A |
2238 | } else { |
2239 | error = ESRCH; | |
2240 | } | |
2241 | ||
2242 | return error; | |
2243 | } | |
2244 | ||
39236c6e | 2245 | static void |
0a7de745 | 2246 | memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2) |
39236c6e A |
2247 | { |
2248 | proc_t p; | |
39037602 A |
2249 | uint64_t current_time = 0, idle_delay_time = 0; |
2250 | int demote_prio_band = 0; | |
39236c6e | 2251 | memstat_bucket_t *demotion_bucket; |
0a7de745 | 2252 | |
39236c6e | 2253 | MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion()\n"); |
0a7de745 | 2254 | |
39236c6e | 2255 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_START, 0, 0, 0, 0, 0); |
0a7de745 A |
2256 | |
2257 | current_time = mach_absolute_time(); | |
2258 | ||
39236c6e | 2259 | proc_list_lock(); |
316670eb | 2260 | |
39037602 A |
2261 | demote_prio_band = JETSAM_PRIORITY_IDLE + 1; |
2262 | ||
2263 | for (; demote_prio_band < JETSAM_PRIORITY_MAX; demote_prio_band++) { | |
0a7de745 | 2264 | if (demote_prio_band != system_procs_aging_band && demote_prio_band != applications_aging_band) { |
39236c6e | 2265 | continue; |
0a7de745 | 2266 | } |
39037602 A |
2267 | |
2268 | demotion_bucket = &memstat_bucket[demote_prio_band]; | |
2269 | p = TAILQ_FIRST(&demotion_bucket->list); | |
0a7de745 | 2270 | |
39037602 A |
2271 | while (p) { |
2272 | MEMORYSTATUS_DEBUG(1, "memorystatus_perform_idle_demotion() found %d\n", p->p_pid); | |
0a7de745 | 2273 | |
39037602 A |
2274 | assert(p->p_memstat_idledeadline); |
2275 | ||
2276 | assert(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS); | |
2277 | ||
2278 | if (current_time >= p->p_memstat_idledeadline) { | |
39037602 | 2279 | if ((isSysProc(p) && |
0a7de745 A |
2280 | ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) != P_DIRTY_IDLE_EXIT_ENABLED)) || /* system proc marked dirty*/ |
2281 | task_has_assertions((struct task *)(p->task))) { /* has outstanding assertions which might indicate outstanding work too */ | |
39037602 A |
2282 | idle_delay_time = (isSysProc(p)) ? memorystatus_sysprocs_idle_delay_time : memorystatus_apps_idle_delay_time; |
2283 | ||
2284 | p->p_memstat_idledeadline += idle_delay_time; | |
2285 | p = TAILQ_NEXT(p, p_memstat_list); | |
39037602 | 2286 | } else { |
39037602 A |
2287 | proc_t next_proc = NULL; |
2288 | ||
2289 | next_proc = TAILQ_NEXT(p, p_memstat_list); | |
2290 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
2291 | ||
2292 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, false, true); | |
0a7de745 | 2293 | |
39037602 A |
2294 | p = next_proc; |
2295 | continue; | |
39037602 A |
2296 | } |
2297 | } else { | |
2298 | // No further candidates | |
2299 | break; | |
2300 | } | |
316670eb A |
2301 | } |
2302 | } | |
39037602 | 2303 | |
39236c6e | 2304 | memorystatus_reschedule_idle_demotion_locked(); |
0a7de745 | 2305 | |
39236c6e | 2306 | proc_list_unlock(); |
316670eb | 2307 | |
39236c6e | 2308 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_END, 0, 0, 0, 0, 0); |
316670eb A |
2309 | } |
2310 | ||
2311 | static void | |
0a7de745 A |
2312 | memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) |
2313 | { | |
39037602 A |
2314 | boolean_t present_in_sysprocs_aging_bucket = FALSE; |
2315 | boolean_t present_in_apps_aging_bucket = FALSE; | |
2316 | uint64_t idle_delay_time = 0; | |
2317 | ||
2318 | if (jetsam_aging_policy == kJetsamAgingPolicyNone) { | |
2319 | return; | |
2320 | } | |
2321 | ||
2322 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { | |
2323 | /* | |
2324 | * This process isn't going to be making the trip to the lower bands. | |
2325 | */ | |
2326 | return; | |
fe8ab488 A |
2327 | } |
2328 | ||
0a7de745 | 2329 | if (isProcessInAgingBands(p)) { |
39037602 A |
2330 | if (jetsam_aging_policy != kJetsamAgingPolicyLegacy) { |
2331 | assert((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) != P_DIRTY_AGING_IN_PROGRESS); | |
2332 | } | |
2333 | ||
2334 | if (isSysProc(p) && system_procs_aging_band) { | |
2335 | present_in_sysprocs_aging_bucket = TRUE; | |
39037602 A |
2336 | } else if (isApp(p) && applications_aging_band) { |
2337 | present_in_apps_aging_bucket = TRUE; | |
2338 | } | |
2339 | } | |
2340 | ||
2341 | assert(!present_in_sysprocs_aging_bucket); | |
2342 | assert(!present_in_apps_aging_bucket); | |
2343 | ||
0a7de745 | 2344 | 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", |
39037602 | 2345 | p->p_pid, p->p_memstat_dirty, set_state, (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)); |
316670eb | 2346 | |
0a7de745 | 2347 | if (isSysProc(p)) { |
39037602 A |
2348 | assert((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED); |
2349 | } | |
2350 | ||
2351 | idle_delay_time = (isSysProc(p)) ? memorystatus_sysprocs_idle_delay_time : memorystatus_apps_idle_delay_time; | |
316670eb | 2352 | |
39236c6e | 2353 | if (set_state) { |
39037602 A |
2354 | p->p_memstat_dirty |= P_DIRTY_AGING_IN_PROGRESS; |
2355 | p->p_memstat_idledeadline = mach_absolute_time() + idle_delay_time; | |
316670eb | 2356 | } |
0a7de745 | 2357 | |
fe8ab488 | 2358 | assert(p->p_memstat_idledeadline); |
39037602 | 2359 | |
0a7de745 A |
2360 | if (isSysProc(p) && present_in_sysprocs_aging_bucket == FALSE) { |
2361 | memorystatus_scheduled_idle_demotions_sysprocs++; | |
39037602 A |
2362 | } else if (isApp(p) && present_in_apps_aging_bucket == FALSE) { |
2363 | memorystatus_scheduled_idle_demotions_apps++; | |
fe8ab488 | 2364 | } |
316670eb A |
2365 | } |
2366 | ||
39236c6e | 2367 | static void |
0a7de745 | 2368 | memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) |
316670eb | 2369 | { |
39037602 A |
2370 | boolean_t present_in_sysprocs_aging_bucket = FALSE; |
2371 | boolean_t present_in_apps_aging_bucket = FALSE; | |
2372 | ||
2373 | if (!system_procs_aging_band && !applications_aging_band) { | |
2374 | return; | |
2375 | } | |
2376 | ||
2377 | if ((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) == 0) { | |
2378 | return; | |
2379 | } | |
2380 | ||
2381 | if (isProcessInAgingBands(p)) { | |
39037602 A |
2382 | if (jetsam_aging_policy != kJetsamAgingPolicyLegacy) { |
2383 | assert((p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) == P_DIRTY_AGING_IN_PROGRESS); | |
2384 | } | |
2385 | ||
2386 | if (isSysProc(p) && system_procs_aging_band) { | |
2387 | assert(p->p_memstat_effectivepriority == system_procs_aging_band); | |
2388 | assert(p->p_memstat_idledeadline); | |
2389 | present_in_sysprocs_aging_bucket = TRUE; | |
39037602 A |
2390 | } else if (isApp(p) && applications_aging_band) { |
2391 | assert(p->p_memstat_effectivepriority == applications_aging_band); | |
2392 | assert(p->p_memstat_idledeadline); | |
2393 | present_in_apps_aging_bucket = TRUE; | |
2394 | } | |
fe8ab488 A |
2395 | } |
2396 | ||
0a7de745 | 2397 | MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for pid %d (clear_state %d, demotions %d).\n", |
39037602 | 2398 | p->p_pid, clear_state, (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)); |
0a7de745 A |
2399 | |
2400 | ||
39236c6e | 2401 | if (clear_state) { |
0a7de745 A |
2402 | p->p_memstat_idledeadline = 0; |
2403 | p->p_memstat_dirty &= ~P_DIRTY_AGING_IN_PROGRESS; | |
316670eb | 2404 | } |
0a7de745 A |
2405 | |
2406 | if (isSysProc(p) && present_in_sysprocs_aging_bucket == TRUE) { | |
39037602 A |
2407 | memorystatus_scheduled_idle_demotions_sysprocs--; |
2408 | assert(memorystatus_scheduled_idle_demotions_sysprocs >= 0); | |
39037602 A |
2409 | } else if (isApp(p) && present_in_apps_aging_bucket == TRUE) { |
2410 | memorystatus_scheduled_idle_demotions_apps--; | |
2411 | assert(memorystatus_scheduled_idle_demotions_apps >= 0); | |
fe8ab488 A |
2412 | } |
2413 | ||
0a7de745 | 2414 | assert((memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps) >= 0); |
316670eb A |
2415 | } |
2416 | ||
2417 | static void | |
0a7de745 A |
2418 | memorystatus_reschedule_idle_demotion_locked(void) |
2419 | { | |
2420 | if (0 == (memorystatus_scheduled_idle_demotions_sysprocs + memorystatus_scheduled_idle_demotions_apps)) { | |
2421 | if (memstat_idle_demotion_deadline) { | |
2422 | /* Transitioned 1->0, so cancel next call */ | |
2423 | thread_call_cancel(memorystatus_idle_demotion_call); | |
2424 | memstat_idle_demotion_deadline = 0; | |
2425 | } | |
2426 | } else { | |
2427 | memstat_bucket_t *demotion_bucket; | |
2428 | proc_t p = NULL, p1 = NULL, p2 = NULL; | |
2429 | ||
2430 | if (system_procs_aging_band) { | |
39037602 A |
2431 | demotion_bucket = &memstat_bucket[system_procs_aging_band]; |
2432 | p1 = TAILQ_FIRST(&demotion_bucket->list); | |
2433 | ||
2434 | p = p1; | |
2435 | } | |
2436 | ||
0a7de745 | 2437 | if (applications_aging_band) { |
39037602 A |
2438 | demotion_bucket = &memstat_bucket[applications_aging_band]; |
2439 | p2 = TAILQ_FIRST(&demotion_bucket->list); | |
2440 | ||
2441 | if (p1 && p2) { | |
2442 | p = (p1->p_memstat_idledeadline > p2->p_memstat_idledeadline) ? p2 : p1; | |
2443 | } else { | |
2444 | p = (p1 == NULL) ? p2 : p1; | |
2445 | } | |
39037602 A |
2446 | } |
2447 | ||
2448 | assert(p); | |
2449 | ||
2450 | if (p != NULL) { | |
2451 | assert(p && p->p_memstat_idledeadline); | |
0a7de745 | 2452 | if (memstat_idle_demotion_deadline != p->p_memstat_idledeadline) { |
39037602 A |
2453 | thread_call_enter_delayed(memorystatus_idle_demotion_call, p->p_memstat_idledeadline); |
2454 | memstat_idle_demotion_deadline = p->p_memstat_idledeadline; | |
2455 | } | |
39236c6e | 2456 | } |
0a7de745 | 2457 | } |
316670eb A |
2458 | } |
2459 | ||
0a7de745 | 2460 | /* |
316670eb A |
2461 | * List manipulation |
2462 | */ | |
0a7de745 A |
2463 | |
2464 | int | |
39236c6e | 2465 | memorystatus_add(proc_t p, boolean_t locked) |
316670eb | 2466 | { |
39236c6e | 2467 | memstat_bucket_t *bucket; |
0a7de745 | 2468 | |
3e170ce0 | 2469 | MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding pid %d with priority %d.\n", p->p_pid, p->p_memstat_effectivepriority); |
39037602 | 2470 | |
39236c6e | 2471 | if (!locked) { |
0a7de745 A |
2472 | proc_list_lock(); |
2473 | } | |
39037602 A |
2474 | |
2475 | DTRACE_MEMORYSTATUS2(memorystatus_add, proc_t, p, int32_t, p->p_memstat_effectivepriority); | |
2476 | ||
39236c6e A |
2477 | /* Processes marked internal do not have priority tracked */ |
2478 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { | |
0a7de745 | 2479 | goto exit; |
39236c6e | 2480 | } |
0a7de745 | 2481 | |
39236c6e | 2482 | bucket = &memstat_bucket[p->p_memstat_effectivepriority]; |
0a7de745 | 2483 | |
39037602 A |
2484 | if (isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) { |
2485 | assert(bucket->count == memorystatus_scheduled_idle_demotions_sysprocs - 1); | |
39037602 A |
2486 | } else if (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band)) { |
2487 | assert(bucket->count == memorystatus_scheduled_idle_demotions_apps - 1); | |
39037602 A |
2488 | } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { |
2489 | /* | |
2490 | * Entering the idle band. | |
2491 | * Record idle start time. | |
2492 | */ | |
2493 | p->p_memstat_idle_start = mach_absolute_time(); | |
fe8ab488 A |
2494 | } |
2495 | ||
39236c6e A |
2496 | TAILQ_INSERT_TAIL(&bucket->list, p, p_memstat_list); |
2497 | bucket->count++; | |
316670eb | 2498 | |
39236c6e | 2499 | memorystatus_list_count++; |
316670eb | 2500 | |
39236c6e | 2501 | memorystatus_check_levels_locked(); |
0a7de745 | 2502 | |
39236c6e | 2503 | exit: |
0a7de745 A |
2504 | if (!locked) { |
2505 | proc_list_unlock(); | |
2506 | } | |
2507 | ||
39236c6e A |
2508 | return 0; |
2509 | } | |
316670eb | 2510 | |
3e170ce0 A |
2511 | /* |
2512 | * Description: | |
2513 | * Moves a process from one jetsam bucket to another. | |
2514 | * which changes the LRU position of the process. | |
2515 | * | |
2516 | * Monitors transition between buckets and if necessary | |
2517 | * will update cached memory limits accordingly. | |
39037602 A |
2518 | * |
2519 | * skip_demotion_check: | |
2520 | * - if the 'jetsam aging policy' is NOT 'legacy': | |
2521 | * When this flag is TRUE, it means we are going | |
2522 | * to age the ripe processes out of the aging bands and into the | |
2523 | * IDLE band and apply their inactive memory limits. | |
2524 | * | |
2525 | * - if the 'jetsam aging policy' is 'legacy': | |
2526 | * When this flag is TRUE, it might mean the above aging mechanism | |
2527 | * OR | |
2528 | * It might be that we have a process that has used up its 'idle deferral' | |
2529 | * stay that is given to it once per lifetime. And in this case, the process | |
2530 | * won't be going through any aging codepaths. But we still need to apply | |
2531 | * the right inactive limits and so we explicitly set this to TRUE if the | |
2532 | * new priority for the process is the IDLE band. | |
3e170ce0 | 2533 | */ |
39037602 A |
2534 | void |
2535 | memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check) | |
39236c6e A |
2536 | { |
2537 | memstat_bucket_t *old_bucket, *new_bucket; | |
0a7de745 | 2538 | |
39236c6e | 2539 | assert(priority < MEMSTAT_BUCKET_COUNT); |
0a7de745 | 2540 | |
39236c6e A |
2541 | /* Ensure that exit isn't underway, leaving the proc retained but removed from its bucket */ |
2542 | if ((p->p_listflag & P_LIST_EXITED) != 0) { | |
2543 | return; | |
316670eb | 2544 | } |
39037602 A |
2545 | |
2546 | MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting %s(%d) to priority %d, inserting at %s\n", | |
0a7de745 | 2547 | (*p->p_name ? p->p_name : "unknown"), p->p_pid, priority, head_insert ? "head" : "tail"); |
39037602 A |
2548 | |
2549 | DTRACE_MEMORYSTATUS3(memorystatus_update_priority, proc_t, p, int32_t, p->p_memstat_effectivepriority, int, priority); | |
2550 | ||
2551 | #if DEVELOPMENT || DEBUG | |
2552 | if (priority == JETSAM_PRIORITY_IDLE && /* if the process is on its way into the IDLE band */ | |
0a7de745 | 2553 | skip_demotion_check == FALSE && /* and it isn't via the path that will set the INACTIVE memlimits */ |
39037602 A |
2554 | (p->p_memstat_dirty & P_DIRTY_TRACK) && /* and it has 'DIRTY' tracking enabled */ |
2555 | ((p->p_memstat_memlimit != p->p_memstat_memlimit_inactive) || /* and we notice that the current limit isn't the right value (inactive) */ | |
0a7de745 | 2556 | ((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) */ |
39037602 | 2557 | panic("memorystatus_update_priority_locked: on %s with 0x%x, prio: %d and %d\n", p->p_name, p->p_memstat_state, priority, p->p_memstat_memlimit); /* then we must catch this */ |
0a7de745 | 2558 | } |
39037602 | 2559 | #endif /* DEVELOPMENT || DEBUG */ |
316670eb | 2560 | |
39236c6e | 2561 | old_bucket = &memstat_bucket[p->p_memstat_effectivepriority]; |
39037602 A |
2562 | |
2563 | if (skip_demotion_check == FALSE) { | |
39037602 A |
2564 | if (isSysProc(p)) { |
2565 | /* | |
2566 | * For system processes, the memorystatus_dirty_* routines take care of adding/removing | |
2567 | * the processes from the aging bands and balancing the demotion counts. | |
2568 | * We can, however, override that if the process has an 'elevated inactive jetsam band' attribute. | |
2569 | */ | |
2570 | ||
d9a64523 A |
2571 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { |
2572 | /* | |
2573 | * 2 types of processes can use the non-standard elevated inactive band: | |
2574 | * - Frozen processes that always land in memorystatus_freeze_jetsam_band | |
2575 | * OR | |
2576 | * - processes that specifically opt-in to the elevated inactive support e.g. docked processes. | |
2577 | */ | |
2578 | #if CONFIG_FREEZE | |
2579 | if (p->p_memstat_state & P_MEMSTAT_FROZEN) { | |
2580 | if (priority <= memorystatus_freeze_jetsam_band) { | |
2581 | priority = memorystatus_freeze_jetsam_band; | |
0a7de745 | 2582 | } |
d9a64523 A |
2583 | } else |
2584 | #endif /* CONFIG_FREEZE */ | |
2585 | { | |
2586 | if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) { | |
2587 | priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; | |
2588 | } | |
2589 | } | |
0a7de745 | 2590 | assert(!(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); |
39037602 A |
2591 | } |
2592 | } else if (isApp(p)) { | |
39037602 A |
2593 | /* |
2594 | * Check to see if the application is being lowered in jetsam priority. If so, and: | |
d9a64523 | 2595 | * - it has an 'elevated inactive jetsam band' attribute, then put it in the appropriate band. |
39037602 A |
2596 | * - it is a normal application, then let it age in the aging band if that policy is in effect. |
2597 | */ | |
0a7de745 | 2598 | |
d9a64523 A |
2599 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { |
2600 | #if CONFIG_FREEZE | |
2601 | if (p->p_memstat_state & P_MEMSTAT_FROZEN) { | |
2602 | if (priority <= memorystatus_freeze_jetsam_band) { | |
2603 | priority = memorystatus_freeze_jetsam_band; | |
0a7de745 A |
2604 | } |
2605 | } else | |
d9a64523 A |
2606 | #endif /* CONFIG_FREEZE */ |
2607 | { | |
2608 | if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) { | |
2609 | priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; | |
2610 | } | |
2611 | } | |
39037602 | 2612 | } else { |
39037602 | 2613 | if (applications_aging_band) { |
0a7de745 | 2614 | if (p->p_memstat_effectivepriority == applications_aging_band) { |
39037602 A |
2615 | assert(old_bucket->count == (memorystatus_scheduled_idle_demotions_apps + 1)); |
2616 | } | |
2617 | ||
2618 | if ((jetsam_aging_policy != kJetsamAgingPolicyLegacy) && (priority <= applications_aging_band)) { | |
0a7de745 | 2619 | assert(!(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS)); |
39037602 A |
2620 | priority = applications_aging_band; |
2621 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
2622 | } | |
2623 | } | |
2624 | } | |
2625 | } | |
2626 | } | |
2627 | ||
2628 | if ((system_procs_aging_band && (priority == system_procs_aging_band)) || (applications_aging_band && (priority == applications_aging_band))) { | |
2629 | assert(p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS); | |
fe8ab488 A |
2630 | } |
2631 | ||
39236c6e A |
2632 | TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list); |
2633 | old_bucket->count--; | |
39037602 | 2634 | |
0a7de745 A |
2635 | new_bucket = &memstat_bucket[priority]; |
2636 | if (head_insert) { | |
fe8ab488 | 2637 | TAILQ_INSERT_HEAD(&new_bucket->list, p, p_memstat_list); |
0a7de745 | 2638 | } else { |
fe8ab488 | 2639 | TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list); |
0a7de745 | 2640 | } |
39236c6e | 2641 | new_bucket->count++; |
3e170ce0 | 2642 | |
3e170ce0 | 2643 | if (memorystatus_highwater_enabled) { |
813fb2f6 A |
2644 | boolean_t is_fatal; |
2645 | boolean_t use_active; | |
3e170ce0 | 2646 | |
0a7de745 | 2647 | /* |
3e170ce0 A |
2648 | * If cached limit data is updated, then the limits |
2649 | * will be enforced by writing to the ledgers. | |
2650 | */ | |
2651 | boolean_t ledger_update_needed = TRUE; | |
fe8ab488 A |
2652 | |
2653 | /* | |
0a7de745 | 2654 | * Here, we must update the cached memory limit if the task |
3e170ce0 | 2655 | * is transitioning between: |
0a7de745 | 2656 | * active <--> inactive |
3e170ce0 A |
2657 | * FG <--> BG |
2658 | * but: | |
2659 | * dirty <--> clean is ignored | |
2660 | * | |
39037602 | 2661 | * We bypass non-idle processes that have opted into dirty tracking because |
3e170ce0 A |
2662 | * a move between buckets does not imply a transition between the |
2663 | * dirty <--> clean state. | |
fe8ab488 A |
2664 | */ |
2665 | ||
3e170ce0 | 2666 | if (p->p_memstat_dirty & P_DIRTY_TRACK) { |
39037602 | 2667 | if (skip_demotion_check == TRUE && priority == JETSAM_PRIORITY_IDLE) { |
813fb2f6 A |
2668 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
2669 | use_active = FALSE; | |
39037602 A |
2670 | } else { |
2671 | ledger_update_needed = FALSE; | |
2672 | } | |
3e170ce0 A |
2673 | } else if ((priority >= JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority < JETSAM_PRIORITY_FOREGROUND)) { |
2674 | /* | |
0a7de745 | 2675 | * inactive --> active |
3e170ce0 A |
2676 | * BG --> FG |
2677 | * assign active state | |
2678 | */ | |
813fb2f6 A |
2679 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); |
2680 | use_active = TRUE; | |
3e170ce0 A |
2681 | } else if ((priority < JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) { |
2682 | /* | |
0a7de745 | 2683 | * active --> inactive |
3e170ce0 A |
2684 | * FG --> BG |
2685 | * assign inactive state | |
2686 | */ | |
813fb2f6 A |
2687 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
2688 | use_active = FALSE; | |
3e170ce0 A |
2689 | } else { |
2690 | /* | |
2691 | * The transition between jetsam priority buckets apparently did | |
2692 | * not affect active/inactive state. | |
2693 | * This is not unusual... especially during startup when | |
2694 | * processes are getting established in their respective bands. | |
2695 | */ | |
2696 | ledger_update_needed = FALSE; | |
2697 | } | |
2698 | ||
2699 | /* | |
2700 | * Enforce the new limits by writing to the ledger | |
2701 | */ | |
2702 | if (ledger_update_needed) { | |
813fb2f6 | 2703 | task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, use_active, is_fatal); |
3e170ce0 A |
2704 | |
2705 | MEMORYSTATUS_DEBUG(3, "memorystatus_update_priority_locked: new limit on pid %d (%dMB %s) priority old --> new (%d --> %d) dirty?=0x%x %s\n", | |
0a7de745 A |
2706 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), |
2707 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, priority, p->p_memstat_dirty, | |
2708 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
39236c6e A |
2709 | } |
2710 | } | |
3e170ce0 | 2711 | |
39037602 A |
2712 | /* |
2713 | * Record idle start or idle delta. | |
2714 | */ | |
2715 | if (p->p_memstat_effectivepriority == priority) { | |
0a7de745 | 2716 | /* |
39037602 A |
2717 | * This process is not transitioning between |
2718 | * jetsam priority buckets. Do nothing. | |
2719 | */ | |
2720 | } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { | |
2721 | uint64_t now; | |
2722 | /* | |
2723 | * Transitioning out of the idle priority bucket. | |
2724 | * Record idle delta. | |
2725 | */ | |
2726 | assert(p->p_memstat_idle_start != 0); | |
2727 | now = mach_absolute_time(); | |
2728 | if (now > p->p_memstat_idle_start) { | |
2729 | p->p_memstat_idle_delta = now - p->p_memstat_idle_start; | |
2730 | } | |
d9a64523 A |
2731 | |
2732 | /* | |
2733 | * About to become active and so memory footprint could change. | |
2734 | * So mark it eligible for freeze-considerations next time around. | |
2735 | */ | |
2736 | if (p->p_memstat_state & P_MEMSTAT_FREEZE_IGNORE) { | |
2737 | p->p_memstat_state &= ~P_MEMSTAT_FREEZE_IGNORE; | |
2738 | } | |
39037602 A |
2739 | } else if (priority == JETSAM_PRIORITY_IDLE) { |
2740 | /* | |
2741 | * Transitioning into the idle priority bucket. | |
2742 | * Record idle start. | |
2743 | */ | |
2744 | p->p_memstat_idle_start = mach_absolute_time(); | |
2745 | } | |
2746 | ||
d9a64523 A |
2747 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CHANGE_PRIORITY), p->p_pid, priority, p->p_memstat_effectivepriority, 0, 0); |
2748 | ||
39236c6e | 2749 | p->p_memstat_effectivepriority = priority; |
39037602 A |
2750 | |
2751 | #if CONFIG_SECLUDED_MEMORY | |
2752 | if (secluded_for_apps && | |
2753 | task_could_use_secluded_mem(p->task)) { | |
2754 | task_set_can_use_secluded_mem( | |
2755 | p->task, | |
2756 | (priority >= JETSAM_PRIORITY_FOREGROUND)); | |
2757 | } | |
2758 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
0a7de745 | 2759 | |
39236c6e | 2760 | memorystatus_check_levels_locked(); |
316670eb A |
2761 | } |
2762 | ||
3e170ce0 A |
2763 | /* |
2764 | * | |
2765 | * Description: Update the jetsam priority and memory limit attributes for a given process. | |
2766 | * | |
2767 | * Parameters: | |
2768 | * p init this process's jetsam information. | |
2769 | * priority The jetsam priority band | |
2770 | * user_data user specific data, unused by the kernel | |
2771 | * effective guards against race if process's update already occurred | |
2772 | * update_memlimit When true we know this is the init step via the posix_spawn path. | |
2773 | * | |
2774 | * memlimit_active Value in megabytes; The monitored footprint level while the | |
2775 | * process is active. Exceeding it may result in termination | |
2776 | * based on it's associated fatal flag. | |
2777 | * | |
2778 | * memlimit_active_is_fatal When a process is active and exceeds its memory footprint, | |
2779 | * this describes whether or not it should be immediately fatal. | |
2780 | * | |
2781 | * memlimit_inactive Value in megabytes; The monitored footprint level while the | |
2782 | * process is inactive. Exceeding it may result in termination | |
2783 | * based on it's associated fatal flag. | |
2784 | * | |
2785 | * memlimit_inactive_is_fatal When a process is inactive and exceeds its memory footprint, | |
2786 | * this describes whether or not it should be immediatly fatal. | |
2787 | * | |
3e170ce0 A |
2788 | * Returns: 0 Success |
2789 | * non-0 Failure | |
2790 | */ | |
2791 | ||
39236c6e | 2792 | int |
3e170ce0 | 2793 | memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, boolean_t update_memlimit, |
0a7de745 A |
2794 | int32_t memlimit_active, boolean_t memlimit_active_is_fatal, |
2795 | int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal) | |
316670eb | 2796 | { |
39236c6e | 2797 | int ret; |
fe8ab488 | 2798 | boolean_t head_insert = false; |
3e170ce0 | 2799 | |
39037602 | 2800 | 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); |
316670eb | 2801 | |
39236c6e | 2802 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_START, p->p_pid, priority, user_data, effective, 0); |
0a7de745 | 2803 | |
39236c6e A |
2804 | if (priority == -1) { |
2805 | /* Use as shorthand for default priority */ | |
2806 | priority = JETSAM_PRIORITY_DEFAULT; | |
39037602 A |
2807 | } else if ((priority == system_procs_aging_band) || (priority == applications_aging_band)) { |
2808 | /* Both the aging bands are reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */ | |
0a7de745 | 2809 | priority = JETSAM_PRIORITY_IDLE; |
fe8ab488 A |
2810 | } else if (priority == JETSAM_PRIORITY_IDLE_HEAD) { |
2811 | /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */ | |
2812 | priority = JETSAM_PRIORITY_IDLE; | |
3e170ce0 | 2813 | head_insert = TRUE; |
39236c6e A |
2814 | } else if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) { |
2815 | /* Sanity check */ | |
2816 | ret = EINVAL; | |
2817 | goto out; | |
316670eb | 2818 | } |
3e170ce0 | 2819 | |
39236c6e | 2820 | proc_list_lock(); |
0a7de745 | 2821 | |
39236c6e | 2822 | assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL)); |
316670eb | 2823 | |
39236c6e A |
2824 | if (effective && (p->p_memstat_state & P_MEMSTAT_PRIORITYUPDATED)) { |
2825 | ret = EALREADY; | |
2826 | proc_list_unlock(); | |
fe8ab488 | 2827 | MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", p->p_pid); |
0a7de745 | 2828 | goto out; |
fe8ab488 A |
2829 | } |
2830 | ||
2831 | if ((p->p_memstat_state & P_MEMSTAT_TERMINATED) || ((p->p_listflag & P_LIST_EXITED) != 0)) { | |
2832 | /* | |
2833 | * This could happen when a process calling posix_spawn() is exiting on the jetsam thread. | |
2834 | */ | |
2835 | ret = EBUSY; | |
2836 | proc_list_unlock(); | |
0a7de745 | 2837 | goto out; |
316670eb A |
2838 | } |
2839 | ||
39236c6e A |
2840 | p->p_memstat_state |= P_MEMSTAT_PRIORITYUPDATED; |
2841 | p->p_memstat_userdata = user_data; | |
2842 | p->p_memstat_requestedpriority = priority; | |
39037602 | 2843 | |
39236c6e | 2844 | if (update_memlimit) { |
813fb2f6 A |
2845 | boolean_t is_fatal; |
2846 | boolean_t use_active; | |
3e170ce0 A |
2847 | |
2848 | /* | |
2849 | * Posix_spawn'd processes come through this path to instantiate ledger limits. | |
2850 | * Forked processes do not come through this path, so no ledger limits exist. | |
2851 | * (That's why forked processes can consume unlimited memory.) | |
2852 | */ | |
2853 | ||
2854 | MEMORYSTATUS_DEBUG(3, "memorystatus_update(enter): pid %d, priority %d, dirty=0x%x, Active(%dMB %s), Inactive(%dMB, %s)\n", | |
0a7de745 A |
2855 | p->p_pid, priority, p->p_memstat_dirty, |
2856 | memlimit_active, (memlimit_active_is_fatal ? "F " : "NF"), | |
2857 | memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF")); | |
3e170ce0 | 2858 | |
3e170ce0 A |
2859 | if (memlimit_active <= 0) { |
2860 | /* | |
2861 | * This process will have a system_wide task limit when active. | |
2862 | * System_wide task limit is always fatal. | |
2863 | * It's quite common to see non-fatal flag passed in here. | |
2864 | * It's not an error, we just ignore it. | |
2865 | */ | |
2866 | ||
2867 | /* | |
2868 | * For backward compatibility with some unexplained launchd behavior, | |
2869 | * we allow a zero sized limit. But we still enforce system_wide limit | |
0a7de745 | 2870 | * when written to the ledgers. |
3e170ce0 A |
2871 | */ |
2872 | ||
2873 | if (memlimit_active < 0) { | |
2874 | memlimit_active = -1; /* enforces system_wide task limit */ | |
39236c6e | 2875 | } |
3e170ce0 | 2876 | memlimit_active_is_fatal = TRUE; |
316670eb | 2877 | } |
3e170ce0 A |
2878 | |
2879 | if (memlimit_inactive <= 0) { | |
2880 | /* | |
2881 | * This process will have a system_wide task limit when inactive. | |
2882 | * System_wide task limit is always fatal. | |
2883 | */ | |
2884 | ||
2885 | memlimit_inactive = -1; | |
2886 | memlimit_inactive_is_fatal = TRUE; | |
fe8ab488 | 2887 | } |
316670eb | 2888 | |
3e170ce0 A |
2889 | /* |
2890 | * Initialize the active limit variants for this process. | |
2891 | */ | |
2892 | SET_ACTIVE_LIMITS_LOCKED(p, memlimit_active, memlimit_active_is_fatal); | |
2893 | ||
2894 | /* | |
2895 | * Initialize the inactive limit variants for this process. | |
2896 | */ | |
2897 | SET_INACTIVE_LIMITS_LOCKED(p, memlimit_inactive, memlimit_inactive_is_fatal); | |
2898 | ||
2899 | /* | |
2900 | * Initialize the cached limits for target process. | |
2901 | * When the target process is dirty tracked, it's typically | |
2902 | * in a clean state. Non dirty tracked processes are | |
2903 | * typically active (Foreground or above). | |
2904 | * But just in case, we don't make assumptions... | |
2905 | */ | |
2906 | ||
2907 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { | |
813fb2f6 A |
2908 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); |
2909 | use_active = TRUE; | |
3e170ce0 | 2910 | } else { |
813fb2f6 A |
2911 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
2912 | use_active = FALSE; | |
3e170ce0 A |
2913 | } |
2914 | ||
2915 | /* | |
2916 | * Enforce the cached limit by writing to the ledger. | |
2917 | */ | |
2918 | if (memorystatus_highwater_enabled) { | |
2919 | /* apply now */ | |
813fb2f6 | 2920 | task_set_phys_footprint_limit_internal(p->task, ((p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1), NULL, use_active, is_fatal); |
3e170ce0 A |
2921 | |
2922 | MEMORYSTATUS_DEBUG(3, "memorystatus_update: init: limit on pid %d (%dMB %s) targeting priority(%d) dirty?=0x%x %s\n", | |
0a7de745 A |
2923 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), |
2924 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), priority, p->p_memstat_dirty, | |
2925 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
3e170ce0 A |
2926 | } |
2927 | } | |
3e170ce0 A |
2928 | |
2929 | /* | |
39037602 A |
2930 | * We can't add to the aging bands buckets here. |
2931 | * But, we could be removing it from those buckets. | |
3e170ce0 A |
2932 | * Check and take appropriate steps if so. |
2933 | */ | |
0a7de745 | 2934 | |
39037602 | 2935 | if (isProcessInAgingBands(p)) { |
fe8ab488 | 2936 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
39037602 A |
2937 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, FALSE, TRUE); |
2938 | } else { | |
0a7de745 | 2939 | if (jetsam_aging_policy == kJetsamAgingPolicyLegacy && priority == JETSAM_PRIORITY_IDLE) { |
39037602 A |
2940 | /* |
2941 | * Daemons with 'inactive' limits will go through the dirty tracking codepath. | |
2942 | * This path deals with apps that may have 'inactive' limits e.g. WebContent processes. | |
2943 | * If this is the legacy aging policy we explicitly need to apply those limits. If it | |
2944 | * is any other aging policy, then we don't need to worry because all processes | |
2945 | * will go through the aging bands and then the demotion thread will take care to | |
2946 | * move them into the IDLE band and apply the required limits. | |
2947 | */ | |
2948 | memorystatus_update_priority_locked(p, priority, head_insert, TRUE); | |
2949 | } | |
fe8ab488 | 2950 | } |
39037602 A |
2951 | |
2952 | memorystatus_update_priority_locked(p, priority, head_insert, FALSE); | |
2953 | ||
39236c6e A |
2954 | proc_list_unlock(); |
2955 | ret = 0; | |
316670eb A |
2956 | |
2957 | out: | |
39236c6e A |
2958 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_END, ret, 0, 0, 0, 0); |
2959 | ||
316670eb A |
2960 | return ret; |
2961 | } | |
2962 | ||
39236c6e A |
2963 | int |
2964 | memorystatus_remove(proc_t p, boolean_t locked) | |
316670eb | 2965 | { |
39236c6e A |
2966 | int ret; |
2967 | memstat_bucket_t *bucket; | |
0a7de745 | 2968 | boolean_t reschedule = FALSE; |
316670eb | 2969 | |
3e170ce0 | 2970 | MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing pid %d\n", p->p_pid); |
316670eb | 2971 | |
0a7de745 A |
2972 | if (!locked) { |
2973 | proc_list_lock(); | |
2974 | } | |
316670eb | 2975 | |
39236c6e | 2976 | assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL)); |
0a7de745 | 2977 | |
39236c6e | 2978 | bucket = &memstat_bucket[p->p_memstat_effectivepriority]; |
39037602 A |
2979 | |
2980 | if (isSysProc(p) && system_procs_aging_band && (p->p_memstat_effectivepriority == system_procs_aging_band)) { | |
39037602 A |
2981 | assert(bucket->count == memorystatus_scheduled_idle_demotions_sysprocs); |
2982 | reschedule = TRUE; | |
39037602 | 2983 | } else if (isApp(p) && applications_aging_band && (p->p_memstat_effectivepriority == applications_aging_band)) { |
39037602 A |
2984 | assert(bucket->count == memorystatus_scheduled_idle_demotions_apps); |
2985 | reschedule = TRUE; | |
2986 | } | |
2987 | ||
2988 | /* | |
2989 | * Record idle delta | |
2990 | */ | |
2991 | ||
2992 | if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { | |
2993 | uint64_t now = mach_absolute_time(); | |
2994 | if (now > p->p_memstat_idle_start) { | |
2995 | p->p_memstat_idle_delta = now - p->p_memstat_idle_start; | |
2996 | } | |
fe8ab488 A |
2997 | } |
2998 | ||
39236c6e A |
2999 | TAILQ_REMOVE(&bucket->list, p, p_memstat_list); |
3000 | bucket->count--; | |
3001 | ||
3002 | memorystatus_list_count--; | |
316670eb | 3003 | |
39236c6e | 3004 | /* If awaiting demotion to the idle band, clean up */ |
39037602 | 3005 | if (reschedule) { |
39236c6e | 3006 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
0a7de745 | 3007 | memorystatus_reschedule_idle_demotion_locked(); |
39236c6e | 3008 | } |
316670eb | 3009 | |
39236c6e A |
3010 | memorystatus_check_levels_locked(); |
3011 | ||
0a7de745 | 3012 | #if CONFIG_FREEZE |
39236c6e | 3013 | if (p->p_memstat_state & (P_MEMSTAT_FROZEN)) { |
d9a64523 A |
3014 | if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { |
3015 | p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; | |
3016 | memorystatus_refreeze_eligible_count--; | |
3017 | } | |
3018 | ||
39236c6e | 3019 | memorystatus_frozen_count--; |
d9a64523 A |
3020 | memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages; |
3021 | p->p_memstat_freeze_sharedanon_pages = 0; | |
39236c6e | 3022 | } |
316670eb | 3023 | |
39236c6e | 3024 | if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { |
39236c6e | 3025 | memorystatus_suspended_count--; |
316670eb | 3026 | } |
39236c6e A |
3027 | #endif |
3028 | ||
0a7de745 A |
3029 | if (!locked) { |
3030 | proc_list_unlock(); | |
3031 | } | |
316670eb | 3032 | |
39236c6e | 3033 | if (p) { |
0a7de745 | 3034 | ret = 0; |
316670eb | 3035 | } else { |
39236c6e | 3036 | ret = ESRCH; |
316670eb A |
3037 | } |
3038 | ||
3039 | return ret; | |
3040 | } | |
3041 | ||
3e170ce0 A |
3042 | /* |
3043 | * Validate dirty tracking flags with process state. | |
3044 | * | |
3045 | * Return: | |
3046 | * 0 on success | |
0a7de745 | 3047 | * non-0 on failure |
39037602 A |
3048 | * |
3049 | * The proc_list_lock is held by the caller. | |
3e170ce0 A |
3050 | */ |
3051 | ||
3052 | static int | |
0a7de745 A |
3053 | memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol) |
3054 | { | |
39236c6e A |
3055 | /* See that the process isn't marked for termination */ |
3056 | if (target_p->p_memstat_dirty & P_DIRTY_TERMINATED) { | |
3e170ce0 | 3057 | return EBUSY; |
316670eb | 3058 | } |
0a7de745 | 3059 | |
39236c6e A |
3060 | /* Idle exit requires that process be tracked */ |
3061 | if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) && | |
0a7de745 | 3062 | !(pcontrol & PROC_DIRTY_TRACK)) { |
3e170ce0 | 3063 | return EINVAL; |
39236c6e A |
3064 | } |
3065 | ||
fe8ab488 A |
3066 | /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */ |
3067 | if ((pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) && | |
0a7de745 | 3068 | !(pcontrol & PROC_DIRTY_TRACK)) { |
3e170ce0 | 3069 | return EINVAL; |
fe8ab488 A |
3070 | } |
3071 | ||
d9a64523 | 3072 | /* Only one type of DEFER behavior is allowed.*/ |
0a7de745 | 3073 | if ((pcontrol & PROC_DIRTY_DEFER) && |
d9a64523 A |
3074 | (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) { |
3075 | return EINVAL; | |
3076 | } | |
3077 | ||
3078 | /* Deferral is only relevant if idle exit is specified */ | |
3079 | if (((pcontrol & PROC_DIRTY_DEFER) || | |
3080 | (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) && | |
0a7de745 | 3081 | !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) { |
3e170ce0 | 3082 | return EINVAL; |
316670eb | 3083 | } |
0a7de745 A |
3084 | |
3085 | return 0; | |
316670eb | 3086 | } |
593a1d5f | 3087 | |
39236c6e | 3088 | static void |
0a7de745 A |
3089 | memorystatus_update_idle_priority_locked(proc_t p) |
3090 | { | |
39236c6e | 3091 | int32_t priority; |
3e170ce0 | 3092 | |
39236c6e | 3093 | MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p->p_pid, p->p_memstat_dirty); |
39037602 | 3094 | |
0a7de745 | 3095 | assert(isSysProc(p)); |
39037602 | 3096 | |
0a7de745 | 3097 | if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED | P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) { |
39037602 | 3098 | priority = (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) ? system_procs_aging_band : JETSAM_PRIORITY_IDLE; |
39236c6e A |
3099 | } else { |
3100 | priority = p->p_memstat_requestedpriority; | |
3101 | } | |
39037602 | 3102 | |
0a7de745 | 3103 | if (priority != p->p_memstat_effectivepriority) { |
39037602 A |
3104 | if ((jetsam_aging_policy == kJetsamAgingPolicyLegacy) && |
3105 | (priority == JETSAM_PRIORITY_IDLE)) { | |
39037602 A |
3106 | /* |
3107 | * This process is on its way into the IDLE band. The system is | |
3108 | * using 'legacy' jetsam aging policy. That means, this process | |
3109 | * has already used up its idle-deferral aging time that is given | |
3110 | * once per its lifetime. So we need to set the INACTIVE limits | |
3111 | * explicitly because it won't be going through the demotion paths | |
3112 | * that take care to apply the limits appropriately. | |
3113 | */ | |
5ba3f43e A |
3114 | |
3115 | if (p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) { | |
5ba3f43e A |
3116 | /* |
3117 | * This process has the 'elevated inactive jetsam band' attribute. | |
3118 | * So, there will be no trip to IDLE after all. | |
3119 | * Instead, we pin the process in the elevated band, | |
3120 | * where its ACTIVE limits will apply. | |
3121 | */ | |
3122 | ||
3123 | priority = JETSAM_PRIORITY_ELEVATED_INACTIVE; | |
3124 | } | |
3125 | ||
39037602 | 3126 | memorystatus_update_priority_locked(p, priority, false, true); |
39037602 A |
3127 | } else { |
3128 | memorystatus_update_priority_locked(p, priority, false, false); | |
3129 | } | |
fe8ab488 | 3130 | } |
0a7de745 | 3131 | } |
39236c6e A |
3132 | |
3133 | /* | |
3134 | * Processes can opt to have their state tracked by the kernel, indicating when they are busy (dirty) or idle | |
3135 | * (clean). They may also indicate that they support termination when idle, with the result that they are promoted | |
3136 | * to their desired, higher, jetsam priority when dirty (and are therefore killed later), and demoted to the low | |
3137 | * priority idle band when clean (and killed earlier, protecting higher priority procesess). | |
3138 | * | |
3139 | * If the deferral flag is set, then newly tracked processes will be protected for an initial period (as determined by | |
39037602 | 3140 | * memorystatus_sysprocs_idle_delay_time); if they go clean during this time, then they will be moved to a deferred-idle band |
39236c6e A |
3141 | * with a slightly higher priority, guarding against immediate termination under memory pressure and being unable to |
3142 | * make forward progress. Finally, when the guard expires, they will be moved to the standard, lowest-priority, idle | |
3143 | * band. The deferral can be cleared early by clearing the appropriate flag. | |
3144 | * | |
3145 | * The deferral timer is active only for the duration that the process is marked as guarded and clean; if the process | |
3146 | * is marked dirty, the timer will be cancelled. Upon being subsequently marked clean, the deferment will either be | |
3147 | * re-enabled or the guard state cleared, depending on whether the guard deadline has passed. | |
3148 | */ | |
3149 | ||
3150 | int | |
0a7de745 A |
3151 | memorystatus_dirty_track(proc_t p, uint32_t pcontrol) |
3152 | { | |
39236c6e A |
3153 | unsigned int old_dirty; |
3154 | boolean_t reschedule = FALSE; | |
fe8ab488 A |
3155 | boolean_t already_deferred = FALSE; |
3156 | boolean_t defer_now = FALSE; | |
3e170ce0 | 3157 | int ret = 0; |
0a7de745 | 3158 | |
fe8ab488 | 3159 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_TRACK), |
0a7de745 A |
3160 | p->p_pid, p->p_memstat_dirty, pcontrol, 0, 0); |
3161 | ||
39236c6e | 3162 | proc_list_lock(); |
0a7de745 | 3163 | |
fe8ab488 A |
3164 | if ((p->p_listflag & P_LIST_EXITED) != 0) { |
3165 | /* | |
3166 | * Process is on its way out. | |
3167 | */ | |
3168 | ret = EBUSY; | |
3169 | goto exit; | |
3170 | } | |
3171 | ||
39236c6e A |
3172 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
3173 | ret = EPERM; | |
3174 | goto exit; | |
316670eb | 3175 | } |
0a7de745 | 3176 | |
3e170ce0 A |
3177 | if ((ret = memorystatus_validate_track_flags(p, pcontrol)) != 0) { |
3178 | /* error */ | |
39236c6e | 3179 | goto exit; |
3e170ce0 | 3180 | } |
39236c6e | 3181 | |
0a7de745 | 3182 | old_dirty = p->p_memstat_dirty; |
39236c6e A |
3183 | |
3184 | /* These bits are cumulative, as per <rdar://problem/11159924> */ | |
3185 | if (pcontrol & PROC_DIRTY_TRACK) { | |
3186 | p->p_memstat_dirty |= P_DIRTY_TRACK; | |
3187 | } | |
3188 | ||
3189 | if (pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) { | |
0a7de745 | 3190 | p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT; |
39236c6e A |
3191 | } |
3192 | ||
fe8ab488 A |
3193 | if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) { |
3194 | p->p_memstat_dirty |= P_DIRTY_LAUNCH_IN_PROGRESS; | |
3195 | } | |
3196 | ||
39037602 | 3197 | if (old_dirty & P_DIRTY_AGING_IN_PROGRESS) { |
fe8ab488 A |
3198 | already_deferred = TRUE; |
3199 | } | |
3200 | ||
39037602 | 3201 | |
39236c6e | 3202 | /* This can be set and cleared exactly once. */ |
d9a64523 | 3203 | if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) { |
d9a64523 A |
3204 | if ((pcontrol & (PROC_DIRTY_DEFER)) && |
3205 | !(old_dirty & P_DIRTY_DEFER)) { | |
fe8ab488 A |
3206 | p->p_memstat_dirty |= P_DIRTY_DEFER; |
3207 | } | |
3208 | ||
d9a64523 A |
3209 | if ((pcontrol & (PROC_DIRTY_DEFER_ALWAYS)) && |
3210 | !(old_dirty & P_DIRTY_DEFER_ALWAYS)) { | |
3211 | p->p_memstat_dirty |= P_DIRTY_DEFER_ALWAYS; | |
3212 | } | |
3213 | ||
fe8ab488 | 3214 | defer_now = TRUE; |
39236c6e A |
3215 | } |
3216 | ||
3e170ce0 | 3217 | MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for pid %d\n", |
0a7de745 A |
3218 | ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) ? "Y" : "N", |
3219 | defer_now ? "Y" : "N", | |
3220 | p->p_memstat_dirty & P_DIRTY ? "Y" : "N", | |
3221 | p->p_pid); | |
39236c6e A |
3222 | |
3223 | /* Kick off or invalidate the idle exit deferment if there's a state transition. */ | |
3224 | if (!(p->p_memstat_dirty & P_DIRTY_IS_DIRTY)) { | |
39037602 | 3225 | if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { |
39037602 | 3226 | if (defer_now && !already_deferred) { |
39037602 | 3227 | /* |
0a7de745 | 3228 | * Request to defer a clean process that's idle-exit enabled |
39037602 A |
3229 | * and not already in the jetsam deferred band. Most likely a |
3230 | * new launch. | |
3231 | */ | |
3232 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
3233 | reschedule = TRUE; | |
39037602 | 3234 | } else if (!defer_now) { |
39037602 A |
3235 | /* |
3236 | * The process isn't asking for the 'aging' facility. | |
3237 | * Could be that it is: | |
3238 | */ | |
3239 | ||
3240 | if (already_deferred) { | |
3241 | /* | |
3242 | * already in the aging bands. Traditionally, | |
3243 | * some processes have tried to use this to | |
3244 | * opt out of the 'aging' facility. | |
3245 | */ | |
0a7de745 | 3246 | |
39037602 A |
3247 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
3248 | } else { | |
3249 | /* | |
3250 | * agnostic to the 'aging' facility. In that case, | |
3251 | * we'll go ahead and opt it in because this is likely | |
3252 | * a new launch (clean process, dirty tracking enabled) | |
3253 | */ | |
0a7de745 | 3254 | |
39037602 A |
3255 | memorystatus_schedule_idle_demotion_locked(p, TRUE); |
3256 | } | |
3257 | ||
3258 | reschedule = TRUE; | |
3259 | } | |
fe8ab488 A |
3260 | } |
3261 | } else { | |
fe8ab488 A |
3262 | /* |
3263 | * We are trying to operate on a dirty process. Dirty processes have to | |
0a7de745 | 3264 | * be removed from the deferred band. The question is do we reset the |
fe8ab488 A |
3265 | * deferred state or not? |
3266 | * | |
3267 | * This could be a legal request like: | |
39037602 | 3268 | * - this process had opted into the 'aging' band |
fe8ab488 A |
3269 | * - but it's now dirty and requests to opt out. |
3270 | * In this case, we remove the process from the band and reset its | |
3271 | * state too. It'll opt back in properly when needed. | |
3272 | * | |
3273 | * OR, this request could be a user-space bug. E.g.: | |
39037602 | 3274 | * - this process had opted into the 'aging' band when clean |
fe8ab488 A |
3275 | * - and, then issues another request to again put it into the band except |
3276 | * this time the process is dirty. | |
3277 | * The process going dirty, as a transition in memorystatus_dirty_set(), will pull the process out of | |
3278 | * the deferred band with its state intact. So our request below is no-op. | |
3279 | * But we do it here anyways for coverage. | |
3280 | * | |
3281 | * memorystatus_update_idle_priority_locked() | |
39037602 | 3282 | * single-mindedly treats a dirty process as "cannot be in the aging band". |
fe8ab488 A |
3283 | */ |
3284 | ||
3285 | if (!defer_now && already_deferred) { | |
39236c6e A |
3286 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
3287 | reschedule = TRUE; | |
fe8ab488 | 3288 | } else { |
39037602 A |
3289 | boolean_t reset_state = (jetsam_aging_policy != kJetsamAgingPolicyLegacy) ? TRUE : FALSE; |
3290 | ||
3291 | memorystatus_invalidate_idle_demotion_locked(p, reset_state); | |
fe8ab488 | 3292 | reschedule = TRUE; |
316670eb A |
3293 | } |
3294 | } | |
39236c6e A |
3295 | |
3296 | memorystatus_update_idle_priority_locked(p); | |
0a7de745 | 3297 | |
39236c6e A |
3298 | if (reschedule) { |
3299 | memorystatus_reschedule_idle_demotion_locked(); | |
3300 | } | |
0a7de745 | 3301 | |
39236c6e | 3302 | ret = 0; |
0a7de745 A |
3303 | |
3304 | exit: | |
39236c6e | 3305 | proc_list_unlock(); |
0a7de745 | 3306 | |
316670eb A |
3307 | return ret; |
3308 | } | |
2d21ac55 | 3309 | |
39236c6e | 3310 | int |
0a7de745 A |
3311 | memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) |
3312 | { | |
39236c6e A |
3313 | int ret; |
3314 | boolean_t kill = false; | |
3315 | boolean_t reschedule = FALSE; | |
3316 | boolean_t was_dirty = FALSE; | |
3317 | boolean_t now_dirty = FALSE; | |
6d2010ae | 3318 | |
39236c6e | 3319 | MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self, p->p_pid, pcontrol, p->p_memstat_dirty); |
fe8ab488 | 3320 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_SET), p->p_pid, self, pcontrol, 0, 0); |
b0d623f7 | 3321 | |
39236c6e A |
3322 | proc_list_lock(); |
3323 | ||
fe8ab488 A |
3324 | if ((p->p_listflag & P_LIST_EXITED) != 0) { |
3325 | /* | |
3326 | * Process is on its way out. | |
3327 | */ | |
3328 | ret = EBUSY; | |
3329 | goto exit; | |
3330 | } | |
3331 | ||
39236c6e A |
3332 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
3333 | ret = EPERM; | |
3334 | goto exit; | |
3335 | } | |
3336 | ||
0a7de745 | 3337 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { |
39236c6e | 3338 | was_dirty = TRUE; |
0a7de745 | 3339 | } |
39236c6e A |
3340 | |
3341 | if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) { | |
3342 | /* Dirty tracking not enabled */ | |
0a7de745 | 3343 | ret = EINVAL; |
39236c6e | 3344 | } else if (pcontrol && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) { |
0a7de745 | 3345 | /* |
39236c6e A |
3346 | * Process is set to be terminated and we're attempting to mark it dirty. |
3347 | * Set for termination and marking as clean is OK - see <rdar://problem/10594349>. | |
3348 | */ | |
0a7de745 | 3349 | ret = EBUSY; |
39236c6e A |
3350 | } else { |
3351 | int flag = (self == TRUE) ? P_DIRTY : P_DIRTY_SHUTDOWN; | |
3352 | if (pcontrol && !(p->p_memstat_dirty & flag)) { | |
3353 | /* Mark the process as having been dirtied at some point */ | |
3354 | p->p_memstat_dirty |= (flag | P_DIRTY_MARKED); | |
3355 | memorystatus_dirty_count++; | |
3356 | ret = 0; | |
3357 | } else if ((pcontrol == 0) && (p->p_memstat_dirty & flag)) { | |
3e170ce0 | 3358 | if ((flag == P_DIRTY_SHUTDOWN) && (!(p->p_memstat_dirty & P_DIRTY))) { |
39236c6e A |
3359 | /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */ |
3360 | p->p_memstat_dirty |= P_DIRTY_TERMINATED; | |
3361 | kill = true; | |
3362 | } else if ((flag == P_DIRTY) && (p->p_memstat_dirty & P_DIRTY_TERMINATED)) { | |
3363 | /* Kill previously terminated processes if set clean */ | |
0a7de745 | 3364 | kill = true; |
39236c6e A |
3365 | } |
3366 | p->p_memstat_dirty &= ~flag; | |
3367 | memorystatus_dirty_count--; | |
3368 | ret = 0; | |
3369 | } else { | |
3370 | /* Already set */ | |
3371 | ret = EALREADY; | |
316670eb | 3372 | } |
39236c6e A |
3373 | } |
3374 | ||
3375 | if (ret != 0) { | |
3376 | goto exit; | |
3377 | } | |
3e170ce0 | 3378 | |
0a7de745 | 3379 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { |
39236c6e | 3380 | now_dirty = TRUE; |
0a7de745 | 3381 | } |
39236c6e A |
3382 | |
3383 | if ((was_dirty == TRUE && now_dirty == FALSE) || | |
3384 | (was_dirty == FALSE && now_dirty == TRUE)) { | |
39236c6e | 3385 | /* Manage idle exit deferral, if applied */ |
39037602 | 3386 | if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { |
fe8ab488 | 3387 | /* |
39037602 A |
3388 | * Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band OR it might be heading back |
3389 | * there once it's clean again. For the legacy case, this only applies if it has some protection window left. | |
d9a64523 A |
3390 | * P_DIRTY_DEFER: one-time protection window given at launch |
3391 | * P_DIRTY_DEFER_ALWAYS: protection window given for every dirty->clean transition. Like non-legacy mode. | |
39037602 A |
3392 | * |
3393 | * Non-Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band. It will always stop over | |
3394 | * in that band on it's way to IDLE. | |
fe8ab488 A |
3395 | */ |
3396 | ||
39236c6e | 3397 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { |
fe8ab488 A |
3398 | /* |
3399 | * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE" | |
3400 | * | |
39037602 | 3401 | * The process will move from its aging band to its higher requested |
0a7de745 | 3402 | * jetsam band. |
fe8ab488 | 3403 | */ |
39037602 A |
3404 | boolean_t reset_state = (jetsam_aging_policy != kJetsamAgingPolicyLegacy) ? TRUE : FALSE; |
3405 | ||
3406 | memorystatus_invalidate_idle_demotion_locked(p, reset_state); | |
39236c6e A |
3407 | reschedule = TRUE; |
3408 | } else { | |
fe8ab488 A |
3409 | /* |
3410 | * Process is back from "dirty" to "clean". | |
fe8ab488 A |
3411 | */ |
3412 | ||
39037602 | 3413 | if (jetsam_aging_policy == kJetsamAgingPolicyLegacy) { |
d9a64523 A |
3414 | if (((p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) == FALSE) && |
3415 | (mach_absolute_time() >= p->p_memstat_idledeadline)) { | |
39037602 | 3416 | /* |
d9a64523 A |
3417 | * The process' hasn't enrolled in the "always defer after dirty" |
3418 | * mode and its deadline has expired. It currently | |
39037602 | 3419 | * does not reside in any of the aging buckets. |
0a7de745 A |
3420 | * |
3421 | * It's on its way to the JETSAM_PRIORITY_IDLE | |
39037602 A |
3422 | * bucket via memorystatus_update_idle_priority_locked() |
3423 | * below. | |
0a7de745 | 3424 | * |
39037602 A |
3425 | * So all we need to do is reset all the state on the |
3426 | * process that's related to the aging bucket i.e. | |
3427 | * the AGING_IN_PROGRESS flag and the timer deadline. | |
3428 | */ | |
fe8ab488 | 3429 | |
39037602 A |
3430 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
3431 | reschedule = TRUE; | |
3432 | } else { | |
3433 | /* | |
d9a64523 A |
3434 | * Process enrolled in "always stop in deferral band after dirty" OR |
3435 | * it still has some protection window left and so | |
39037602 A |
3436 | * we just re-arm the timer without modifying any |
3437 | * state on the process iff it still wants into that band. | |
3438 | */ | |
3439 | ||
d9a64523 A |
3440 | if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) { |
3441 | memorystatus_schedule_idle_demotion_locked(p, TRUE); | |
3442 | reschedule = TRUE; | |
3443 | } else if (p->p_memstat_dirty & P_DIRTY_AGING_IN_PROGRESS) { | |
39037602 A |
3444 | memorystatus_schedule_idle_demotion_locked(p, FALSE); |
3445 | reschedule = TRUE; | |
3446 | } | |
3447 | } | |
39236c6e | 3448 | } else { |
39037602 | 3449 | memorystatus_schedule_idle_demotion_locked(p, TRUE); |
39236c6e A |
3450 | reschedule = TRUE; |
3451 | } | |
3452 | } | |
3453 | } | |
3e170ce0 | 3454 | |
39236c6e | 3455 | memorystatus_update_idle_priority_locked(p); |
3e170ce0 | 3456 | |
3e170ce0 | 3457 | if (memorystatus_highwater_enabled) { |
813fb2f6 A |
3458 | boolean_t ledger_update_needed = TRUE; |
3459 | boolean_t use_active; | |
3460 | boolean_t is_fatal; | |
0a7de745 A |
3461 | /* |
3462 | * We are in this path because this process transitioned between | |
3e170ce0 A |
3463 | * dirty <--> clean state. Update the cached memory limits. |
3464 | */ | |
3465 | ||
3466 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { | |
3467 | /* | |
5ba3f43e A |
3468 | * process is pinned in elevated band |
3469 | * or | |
3e170ce0 A |
3470 | * process is dirty |
3471 | */ | |
813fb2f6 A |
3472 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); |
3473 | use_active = TRUE; | |
39037602 | 3474 | ledger_update_needed = TRUE; |
3e170ce0 A |
3475 | } else { |
3476 | /* | |
39037602 A |
3477 | * process is clean...but if it has opted into pressured-exit |
3478 | * we don't apply the INACTIVE limit till the process has aged | |
3479 | * out and is entering the IDLE band. | |
3480 | * See memorystatus_update_priority_locked() for that. | |
3e170ce0 | 3481 | */ |
0a7de745 | 3482 | |
39037602 A |
3483 | if (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) { |
3484 | ledger_update_needed = FALSE; | |
3485 | } else { | |
813fb2f6 A |
3486 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
3487 | use_active = FALSE; | |
39037602 A |
3488 | ledger_update_needed = TRUE; |
3489 | } | |
3e170ce0 A |
3490 | } |
3491 | ||
3492 | /* | |
3493 | * Enforce the new limits by writing to the ledger. | |
3494 | * | |
3495 | * This is a hot path and holding the proc_list_lock while writing to the ledgers, | |
3496 | * (where the task lock is taken) is bad. So, we temporarily drop the proc_list_lock. | |
3497 | * We aren't traversing the jetsam bucket list here, so we should be safe. | |
3498 | * See rdar://21394491. | |
3499 | */ | |
3500 | ||
39037602 | 3501 | if (ledger_update_needed && proc_ref_locked(p) == p) { |
3e170ce0 A |
3502 | int ledger_limit; |
3503 | if (p->p_memstat_memlimit > 0) { | |
3504 | ledger_limit = p->p_memstat_memlimit; | |
3505 | } else { | |
3506 | ledger_limit = -1; | |
3507 | } | |
3508 | proc_list_unlock(); | |
813fb2f6 | 3509 | task_set_phys_footprint_limit_internal(p->task, ledger_limit, NULL, use_active, is_fatal); |
3e170ce0 A |
3510 | proc_list_lock(); |
3511 | proc_rele_locked(p); | |
3512 | ||
3513 | MEMORYSTATUS_DEBUG(3, "memorystatus_dirty_set: new limit on pid %d (%dMB %s) priority(%d) dirty?=0x%x %s\n", | |
0a7de745 A |
3514 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), |
3515 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, | |
3516 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
3e170ce0 | 3517 | } |
3e170ce0 | 3518 | } |
0a7de745 | 3519 | |
39236c6e A |
3520 | /* If the deferral state changed, reschedule the demotion timer */ |
3521 | if (reschedule) { | |
3522 | memorystatus_reschedule_idle_demotion_locked(); | |
3523 | } | |
3524 | } | |
3e170ce0 | 3525 | |
39236c6e | 3526 | if (kill) { |
3e170ce0 A |
3527 | if (proc_ref_locked(p) == p) { |
3528 | proc_list_unlock(); | |
3529 | psignal(p, SIGKILL); | |
3530 | proc_list_lock(); | |
3531 | proc_rele_locked(p); | |
3532 | } | |
39236c6e | 3533 | } |
0a7de745 | 3534 | |
39236c6e A |
3535 | exit: |
3536 | proc_list_unlock(); | |
3537 | ||
3538 | return ret; | |
3539 | } | |
b0d623f7 | 3540 | |
39236c6e | 3541 | int |
0a7de745 A |
3542 | memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) |
3543 | { | |
39236c6e | 3544 | int ret = 0; |
fe8ab488 A |
3545 | |
3546 | MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_clear(): %d 0x%x 0x%x\n", p->p_pid, pcontrol, p->p_memstat_dirty); | |
0a7de745 | 3547 | |
fe8ab488 A |
3548 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_CLEAR), p->p_pid, pcontrol, 0, 0, 0); |
3549 | ||
3550 | proc_list_lock(); | |
3551 | ||
3552 | if ((p->p_listflag & P_LIST_EXITED) != 0) { | |
3553 | /* | |
3554 | * Process is on its way out. | |
3555 | */ | |
3556 | ret = EBUSY; | |
3557 | goto exit; | |
3558 | } | |
3559 | ||
3560 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { | |
3561 | ret = EPERM; | |
3562 | goto exit; | |
3563 | } | |
3564 | ||
3565 | if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) { | |
3566 | /* Dirty tracking not enabled */ | |
0a7de745 | 3567 | ret = EINVAL; |
fe8ab488 | 3568 | goto exit; |
0a7de745 | 3569 | } |
fe8ab488 | 3570 | |
d9a64523 | 3571 | if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) == 0) { |
fe8ab488 A |
3572 | ret = EINVAL; |
3573 | goto exit; | |
3574 | } | |
3575 | ||
3576 | if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) { | |
3577 | p->p_memstat_dirty &= ~P_DIRTY_LAUNCH_IN_PROGRESS; | |
3578 | } | |
3579 | ||
3580 | /* This can be set and cleared exactly once. */ | |
d9a64523 | 3581 | if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) { |
d9a64523 A |
3582 | if (p->p_memstat_dirty & P_DIRTY_DEFER) { |
3583 | p->p_memstat_dirty &= ~(P_DIRTY_DEFER); | |
3584 | } | |
fe8ab488 | 3585 | |
d9a64523 A |
3586 | if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) { |
3587 | p->p_memstat_dirty &= ~(P_DIRTY_DEFER_ALWAYS); | |
3588 | } | |
fe8ab488 | 3589 | |
d9a64523 A |
3590 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
3591 | memorystatus_update_idle_priority_locked(p); | |
3592 | memorystatus_reschedule_idle_demotion_locked(); | |
fe8ab488 A |
3593 | } |
3594 | ||
3595 | ret = 0; | |
3596 | exit: | |
3597 | proc_list_unlock(); | |
3598 | ||
3599 | return ret; | |
3600 | } | |
3601 | ||
3602 | int | |
0a7de745 A |
3603 | memorystatus_dirty_get(proc_t p) |
3604 | { | |
fe8ab488 | 3605 | int ret = 0; |
0a7de745 | 3606 | |
fe8ab488 | 3607 | proc_list_lock(); |
0a7de745 | 3608 | |
fe8ab488 | 3609 | if (p->p_memstat_dirty & P_DIRTY_TRACK) { |
39236c6e A |
3610 | ret |= PROC_DIRTY_TRACKED; |
3611 | if (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) { | |
3612 | ret |= PROC_DIRTY_ALLOWS_IDLE_EXIT; | |
3613 | } | |
3614 | if (p->p_memstat_dirty & P_DIRTY) { | |
3615 | ret |= PROC_DIRTY_IS_DIRTY; | |
3616 | } | |
fe8ab488 A |
3617 | if (p->p_memstat_dirty & P_DIRTY_LAUNCH_IN_PROGRESS) { |
3618 | ret |= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS; | |
3619 | } | |
39236c6e | 3620 | } |
0a7de745 | 3621 | |
39236c6e | 3622 | proc_list_unlock(); |
0a7de745 | 3623 | |
39236c6e A |
3624 | return ret; |
3625 | } | |
b0d623f7 | 3626 | |
39236c6e | 3627 | int |
0a7de745 A |
3628 | memorystatus_on_terminate(proc_t p) |
3629 | { | |
39236c6e | 3630 | int sig; |
0a7de745 | 3631 | |
39236c6e | 3632 | proc_list_lock(); |
0a7de745 | 3633 | |
39236c6e | 3634 | p->p_memstat_dirty |= P_DIRTY_TERMINATED; |
0a7de745 A |
3635 | |
3636 | if ((p->p_memstat_dirty & (P_DIRTY_TRACK | P_DIRTY_IS_DIRTY)) == P_DIRTY_TRACK) { | |
39236c6e A |
3637 | /* Clean; mark as terminated and issue SIGKILL */ |
3638 | sig = SIGKILL; | |
3639 | } else { | |
3640 | /* Dirty, terminated, or state tracking is unsupported; issue SIGTERM to allow cleanup */ | |
3641 | sig = SIGTERM; | |
316670eb | 3642 | } |
39236c6e A |
3643 | |
3644 | proc_list_unlock(); | |
0a7de745 | 3645 | |
39236c6e | 3646 | return sig; |
316670eb | 3647 | } |
b0d623f7 | 3648 | |
316670eb | 3649 | void |
39236c6e A |
3650 | memorystatus_on_suspend(proc_t p) |
3651 | { | |
316670eb | 3652 | #if CONFIG_FREEZE |
39236c6e | 3653 | uint32_t pages; |
d9a64523 | 3654 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); |
39236c6e A |
3655 | #endif |
3656 | proc_list_lock(); | |
3657 | #if CONFIG_FREEZE | |
39236c6e | 3658 | memorystatus_suspended_count++; |
316670eb | 3659 | #endif |
39236c6e A |
3660 | p->p_memstat_state |= P_MEMSTAT_SUSPENDED; |
3661 | proc_list_unlock(); | |
3662 | } | |
6d2010ae | 3663 | |
39236c6e A |
3664 | void |
3665 | memorystatus_on_resume(proc_t p) | |
3666 | { | |
3667 | #if CONFIG_FREEZE | |
3668 | boolean_t frozen; | |
3669 | pid_t pid; | |
3670 | #endif | |
6d2010ae | 3671 | |
39236c6e | 3672 | proc_list_lock(); |
6d2010ae | 3673 | |
316670eb | 3674 | #if CONFIG_FREEZE |
39236c6e A |
3675 | frozen = (p->p_memstat_state & P_MEMSTAT_FROZEN); |
3676 | if (frozen) { | |
d9a64523 A |
3677 | /* |
3678 | * Now that we don't _thaw_ a process completely, | |
3679 | * resuming it (and having some on-demand swapins) | |
3680 | * shouldn't preclude it from being counted as frozen. | |
3681 | * | |
3682 | * memorystatus_frozen_count--; | |
3683 | * | |
3684 | * We preserve the P_MEMSTAT_FROZEN state since the process | |
3685 | * could have state on disk AND so will deserve some protection | |
3686 | * in the jetsam bands. | |
3687 | */ | |
3688 | if ((p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) == 0) { | |
3689 | p->p_memstat_state |= P_MEMSTAT_REFREEZE_ELIGIBLE; | |
3690 | memorystatus_refreeze_eligible_count++; | |
3691 | } | |
3692 | p->p_memstat_thaw_count++; | |
3693 | ||
3694 | memorystatus_thaw_count++; | |
39236c6e A |
3695 | } |
3696 | ||
39236c6e | 3697 | memorystatus_suspended_count--; |
0a7de745 | 3698 | |
39236c6e | 3699 | pid = p->p_pid; |
316670eb | 3700 | #endif |
39236c6e | 3701 | |
d9a64523 A |
3702 | /* |
3703 | * P_MEMSTAT_FROZEN will remain unchanged. This used to be: | |
3704 | * p->p_memstat_state &= ~(P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN); | |
3705 | */ | |
3706 | p->p_memstat_state &= ~P_MEMSTAT_SUSPENDED; | |
39236c6e A |
3707 | |
3708 | proc_list_unlock(); | |
0a7de745 | 3709 | |
39236c6e A |
3710 | #if CONFIG_FREEZE |
3711 | if (frozen) { | |
3712 | memorystatus_freeze_entry_t data = { pid, FALSE, 0 }; | |
3713 | memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); | |
316670eb | 3714 | } |
39236c6e | 3715 | #endif |
316670eb | 3716 | } |
6d2010ae | 3717 | |
316670eb | 3718 | void |
39236c6e | 3719 | memorystatus_on_inactivity(proc_t p) |
6d2010ae | 3720 | { |
39236c6e | 3721 | #pragma unused(p) |
316670eb A |
3722 | #if CONFIG_FREEZE |
3723 | /* Wake the freeze thread */ | |
3724 | thread_wakeup((event_t)&memorystatus_freeze_wakeup); | |
0a7de745 | 3725 | #endif |
316670eb | 3726 | } |
6d2010ae | 3727 | |
39037602 A |
3728 | /* |
3729 | * The proc_list_lock is held by the caller. | |
0a7de745 | 3730 | */ |
39236c6e | 3731 | static uint32_t |
0a7de745 A |
3732 | memorystatus_build_state(proc_t p) |
3733 | { | |
39236c6e | 3734 | uint32_t snapshot_state = 0; |
0a7de745 | 3735 | |
39236c6e A |
3736 | /* General */ |
3737 | if (p->p_memstat_state & P_MEMSTAT_SUSPENDED) { | |
3738 | snapshot_state |= kMemorystatusSuspended; | |
3739 | } | |
3740 | if (p->p_memstat_state & P_MEMSTAT_FROZEN) { | |
3741 | snapshot_state |= kMemorystatusFrozen; | |
3742 | } | |
d9a64523 | 3743 | if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { |
0a7de745 | 3744 | snapshot_state |= kMemorystatusWasThawed; |
39236c6e | 3745 | } |
0a7de745 | 3746 | |
39236c6e A |
3747 | /* Tracking */ |
3748 | if (p->p_memstat_dirty & P_DIRTY_TRACK) { | |
3749 | snapshot_state |= kMemorystatusTracked; | |
3750 | } | |
3751 | if ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) { | |
3752 | snapshot_state |= kMemorystatusSupportsIdleExit; | |
3753 | } | |
3754 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { | |
3755 | snapshot_state |= kMemorystatusDirty; | |
3756 | } | |
3757 | ||
3758 | return snapshot_state; | |
3759 | } | |
3760 | ||
39236c6e A |
3761 | static boolean_t |
3762 | kill_idle_exit_proc(void) | |
316670eb | 3763 | { |
39236c6e | 3764 | proc_t p, victim_p = PROC_NULL; |
316670eb | 3765 | uint64_t current_time; |
39236c6e A |
3766 | boolean_t killed = FALSE; |
3767 | unsigned int i = 0; | |
39037602 | 3768 | os_reason_t jetsam_reason = OS_REASON_NULL; |
316670eb | 3769 | |
39236c6e | 3770 | /* Pick next idle exit victim. */ |
316670eb | 3771 | current_time = mach_absolute_time(); |
0a7de745 | 3772 | |
39037602 A |
3773 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_IDLE_EXIT); |
3774 | if (jetsam_reason == OS_REASON_NULL) { | |
3775 | printf("kill_idle_exit_proc: failed to allocate jetsam reason\n"); | |
3776 | } | |
3777 | ||
39236c6e | 3778 | proc_list_lock(); |
0a7de745 | 3779 | |
39236c6e A |
3780 | p = memorystatus_get_first_proc_locked(&i, FALSE); |
3781 | while (p) { | |
3782 | /* No need to look beyond the idle band */ | |
3783 | if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) { | |
3784 | break; | |
3785 | } | |
0a7de745 A |
3786 | |
3787 | if ((p->p_memstat_dirty & (P_DIRTY_ALLOW_IDLE_EXIT | P_DIRTY_IS_DIRTY | P_DIRTY_TERMINATED)) == (P_DIRTY_ALLOW_IDLE_EXIT)) { | |
39236c6e A |
3788 | if (current_time >= p->p_memstat_idledeadline) { |
3789 | p->p_memstat_dirty |= P_DIRTY_TERMINATED; | |
3790 | victim_p = proc_ref_locked(p); | |
3791 | break; | |
316670eb A |
3792 | } |
3793 | } | |
0a7de745 | 3794 | |
39236c6e | 3795 | p = memorystatus_get_next_proc_locked(&i, p, FALSE); |
6d2010ae | 3796 | } |
0a7de745 | 3797 | |
39236c6e | 3798 | proc_list_unlock(); |
0a7de745 | 3799 | |
39236c6e | 3800 | if (victim_p) { |
5ba3f43e | 3801 | printf("memorystatus: killing_idle_process pid %d [%s]\n", victim_p->p_pid, (*victim_p->p_name ? victim_p->p_name : "unknown")); |
39037602 | 3802 | killed = memorystatus_do_kill(victim_p, kMemorystatusKilledIdleExit, jetsam_reason); |
39236c6e | 3803 | proc_rele(victim_p); |
39037602 A |
3804 | } else { |
3805 | os_reason_free(jetsam_reason); | |
316670eb | 3806 | } |
b0d623f7 | 3807 | |
39236c6e | 3808 | return killed; |
2d21ac55 A |
3809 | } |
3810 | ||
39236c6e | 3811 | static void |
d9a64523 | 3812 | memorystatus_thread_wake(void) |
0a7de745 | 3813 | { |
d9a64523 A |
3814 | int thr_id = 0; |
3815 | int active_thr = atomic_load(&active_jetsam_threads); | |
3816 | ||
3817 | /* Wakeup all the jetsam threads */ | |
3818 | for (thr_id = 0; thr_id < active_thr; thr_id++) { | |
0a7de745 A |
3819 | thread_wakeup((event_t)&jetsam_threads[thr_id].memorystatus_wakeup); |
3820 | } | |
3821 | } | |
3822 | ||
d9a64523 A |
3823 | #if CONFIG_JETSAM |
3824 | ||
0a7de745 | 3825 | static void |
d9a64523 A |
3826 | memorystatus_thread_pool_max() |
3827 | { | |
3828 | /* Increase the jetsam thread pool to max_jetsam_threads */ | |
3829 | int max_threads = max_jetsam_threads; | |
3830 | printf("Expanding memorystatus pool to %d!\n", max_threads); | |
3831 | atomic_store(&active_jetsam_threads, max_threads); | |
3832 | } | |
3833 | ||
3834 | static void | |
3835 | memorystatus_thread_pool_default() | |
3836 | { | |
3837 | /* Restore the jetsam thread pool to a single thread */ | |
3838 | printf("Reverting memorystatus pool back to 1\n"); | |
3839 | atomic_store(&active_jetsam_threads, 1); | |
b0d623f7 | 3840 | } |
fe8ab488 | 3841 | |
d9a64523 A |
3842 | #endif /* CONFIG_JETSAM */ |
3843 | ||
fe8ab488 | 3844 | extern void vm_pressure_response(void); |
b0d623f7 | 3845 | |
316670eb | 3846 | static int |
39236c6e A |
3847 | memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation) |
3848 | { | |
d9a64523 A |
3849 | struct jetsam_thread_state *jetsam_thread = jetsam_current_thread(); |
3850 | ||
39236c6e | 3851 | if (interval_ms) { |
d9a64523 | 3852 | assert_wait_timeout(&jetsam_thread->memorystatus_wakeup, THREAD_UNINT, interval_ms, NSEC_PER_MSEC); |
39236c6e | 3853 | } else { |
d9a64523 | 3854 | assert_wait(&jetsam_thread->memorystatus_wakeup, THREAD_UNINT); |
39236c6e | 3855 | } |
0a7de745 A |
3856 | |
3857 | return thread_block(continuation); | |
39236c6e | 3858 | } |
316670eb | 3859 | |
5ba3f43e A |
3860 | static boolean_t |
3861 | memorystatus_avail_pages_below_pressure(void) | |
3862 | { | |
3863 | #if CONFIG_EMBEDDED | |
3864 | /* | |
3865 | * Instead of CONFIG_EMBEDDED for these *avail_pages* routines, we should | |
3866 | * key off of the system having dynamic swap support. With full swap support, | |
3867 | * the system shouldn't really need to worry about various page thresholds. | |
3868 | */ | |
0a7de745 | 3869 | return memorystatus_available_pages <= memorystatus_available_pages_pressure; |
5ba3f43e A |
3870 | #else /* CONFIG_EMBEDDED */ |
3871 | return FALSE; | |
3872 | #endif /* CONFIG_EMBEDDED */ | |
3873 | } | |
3874 | ||
3875 | static boolean_t | |
3876 | memorystatus_avail_pages_below_critical(void) | |
3877 | { | |
3878 | #if CONFIG_EMBEDDED | |
0a7de745 | 3879 | return memorystatus_available_pages <= memorystatus_available_pages_critical; |
5ba3f43e A |
3880 | #else /* CONFIG_EMBEDDED */ |
3881 | return FALSE; | |
3882 | #endif /* CONFIG_EMBEDDED */ | |
3883 | } | |
3884 | ||
3885 | static boolean_t | |
3886 | memorystatus_post_snapshot(int32_t priority, uint32_t cause) | |
3887 | { | |
3888 | #if CONFIG_EMBEDDED | |
3889 | #pragma unused(cause) | |
3890 | /* | |
3891 | * Don't generate logs for steady-state idle-exit kills, | |
3892 | * unless it is overridden for debug or by the device | |
3893 | * tree. | |
3894 | */ | |
3895 | ||
0a7de745 | 3896 | return (priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot; |
5ba3f43e A |
3897 | |
3898 | #else /* CONFIG_EMBEDDED */ | |
3899 | /* | |
3900 | * Don't generate logs for steady-state idle-exit kills, | |
3901 | * unless | |
3902 | * - it is overridden for debug or by the device | |
3903 | * tree. | |
3904 | * OR | |
3905 | * - the kill causes are important i.e. not kMemorystatusKilledIdleExit | |
3906 | */ | |
3907 | ||
3908 | boolean_t snapshot_eligible_kill_cause = (is_reason_thrashing(cause) || is_reason_zone_map_exhaustion(cause)); | |
0a7de745 | 3909 | return (priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot || snapshot_eligible_kill_cause; |
5ba3f43e A |
3910 | #endif /* CONFIG_EMBEDDED */ |
3911 | } | |
3912 | ||
3913 | static boolean_t | |
3914 | memorystatus_action_needed(void) | |
3915 | { | |
3916 | #if CONFIG_EMBEDDED | |
0a7de745 A |
3917 | return is_reason_thrashing(kill_under_pressure_cause) || |
3918 | is_reason_zone_map_exhaustion(kill_under_pressure_cause) || | |
3919 | memorystatus_available_pages <= memorystatus_available_pages_pressure; | |
5ba3f43e | 3920 | #else /* CONFIG_EMBEDDED */ |
0a7de745 A |
3921 | return is_reason_thrashing(kill_under_pressure_cause) || |
3922 | is_reason_zone_map_exhaustion(kill_under_pressure_cause); | |
5ba3f43e A |
3923 | #endif /* CONFIG_EMBEDDED */ |
3924 | } | |
3925 | ||
d9a64523 | 3926 | #if CONFIG_FREEZE |
0a7de745 | 3927 | extern void vm_swap_consider_defragmenting(int); |
d9a64523 A |
3928 | |
3929 | /* | |
3930 | * This routine will _jetsam_ all frozen processes | |
3931 | * and reclaim the swap space immediately. | |
3932 | * | |
3933 | * So freeze has to be DISABLED when we call this routine. | |
3934 | */ | |
3935 | ||
3936 | void | |
3937 | memorystatus_disable_freeze(void) | |
3938 | { | |
3939 | memstat_bucket_t *bucket; | |
3940 | int bucket_count = 0, retries = 0; | |
3941 | boolean_t retval = FALSE, killed = FALSE; | |
3942 | uint32_t errors = 0, errors_over_prev_iteration = 0; | |
3943 | os_reason_t jetsam_reason = 0; | |
3944 | unsigned int band = 0; | |
3945 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
3946 | ||
3947 | assert(memorystatus_freeze_enabled == FALSE); | |
3948 | ||
3949 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE); | |
3950 | if (jetsam_reason == OS_REASON_NULL) { | |
3951 | printf("memorystatus_disable_freeze: failed to allocate jetsam reason\n"); | |
3952 | } | |
3953 | ||
3954 | /* | |
3955 | * Let's relocate all frozen processes into band 8. Demoted frozen processes | |
3956 | * are sitting in band 0 currently and it's possible to have a frozen process | |
3957 | * in the FG band being actively used. We don't reset its frozen state when | |
3958 | * it is resumed because it has state on disk. | |
3959 | * | |
3960 | * We choose to do this relocation rather than implement a new 'kill frozen' | |
3961 | * process function for these reasons: | |
3962 | * - duplication of code: too many kill functions exist and we need to rework them better. | |
3963 | * - disk-space-shortage kills are rare | |
3964 | * - not having the 'real' jetsam band at time of the this frozen kill won't preclude us | |
3965 | * from answering any imp. questions re. jetsam policy/effectiveness. | |
3966 | * | |
3967 | * This is essentially what memorystatus_update_inactive_jetsam_priority_band() does while | |
3968 | * avoiding the application of memory limits. | |
3969 | */ | |
3970 | ||
3971 | again: | |
3972 | proc_list_lock(); | |
3973 | ||
3974 | band = JETSAM_PRIORITY_IDLE; | |
3975 | p = PROC_NULL; | |
3976 | next_p = PROC_NULL; | |
3977 | ||
3978 | next_p = memorystatus_get_first_proc_locked(&band, TRUE); | |
3979 | while (next_p) { | |
d9a64523 A |
3980 | p = next_p; |
3981 | next_p = memorystatus_get_next_proc_locked(&band, p, TRUE); | |
3982 | ||
3983 | if (p->p_memstat_effectivepriority > JETSAM_PRIORITY_FOREGROUND) { | |
3984 | break; | |
3985 | } | |
3986 | ||
3987 | if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == FALSE) { | |
3988 | continue; | |
3989 | } | |
3990 | ||
3991 | if (p->p_memstat_state & P_MEMSTAT_ERROR) { | |
3992 | p->p_memstat_state &= ~P_MEMSTAT_ERROR; | |
3993 | } | |
3994 | ||
3995 | if (p->p_memstat_effectivepriority == memorystatus_freeze_jetsam_band) { | |
3996 | continue; | |
3997 | } | |
3998 | ||
3999 | /* | |
4000 | * We explicitly add this flag here so the process looks like a normal | |
4001 | * frozen process i.e. P_MEMSTAT_FROZEN and P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND. | |
4002 | * We don't bother with assigning the 'active' memory | |
4003 | * limits at this point because we are going to be killing it soon below. | |
4004 | */ | |
4005 | p->p_memstat_state |= P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; | |
4006 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
4007 | ||
4008 | memorystatus_update_priority_locked(p, memorystatus_freeze_jetsam_band, FALSE, TRUE); | |
4009 | } | |
4010 | ||
4011 | bucket = &memstat_bucket[memorystatus_freeze_jetsam_band]; | |
4012 | bucket_count = bucket->count; | |
4013 | proc_list_unlock(); | |
4014 | ||
4015 | /* | |
4016 | * Bucket count is already stale at this point. But, we don't expect | |
4017 | * freezing to continue since we have already disabled the freeze functionality. | |
4018 | * However, an existing freeze might be in progress. So we might miss that process | |
4019 | * in the first go-around. We hope to catch it in the next. | |
4020 | */ | |
4021 | ||
4022 | errors_over_prev_iteration = 0; | |
4023 | while (bucket_count) { | |
d9a64523 A |
4024 | bucket_count--; |
4025 | ||
4026 | /* | |
4027 | * memorystatus_kill_elevated_process() drops a reference, | |
4028 | * so take another one so we can continue to use this exit reason | |
4029 | * even after it returns. | |
4030 | */ | |
4031 | ||
4032 | os_reason_ref(jetsam_reason); | |
4033 | retval = memorystatus_kill_elevated_process( | |
4034 | kMemorystatusKilledDiskSpaceShortage, | |
4035 | jetsam_reason, | |
4036 | memorystatus_freeze_jetsam_band, | |
4037 | 0, /* the iteration of aggressive jetsam..ignored here */ | |
4038 | &errors); | |
4039 | ||
4040 | if (errors > 0) { | |
4041 | printf("memorystatus_disable_freeze: memorystatus_kill_elevated_process returned %d error(s)\n", errors); | |
4042 | errors_over_prev_iteration += errors; | |
4043 | errors = 0; | |
4044 | } | |
4045 | ||
4046 | if (retval == 0) { | |
4047 | /* | |
4048 | * No frozen processes left to kill. | |
4049 | */ | |
4050 | break; | |
4051 | } | |
4052 | ||
4053 | killed = TRUE; | |
4054 | } | |
4055 | ||
4056 | proc_list_lock(); | |
4057 | ||
4058 | if (memorystatus_frozen_count) { | |
4059 | /* | |
4060 | * A frozen process snuck in and so | |
4061 | * go back around to kill it. That | |
4062 | * process may have been resumed and | |
4063 | * put into the FG band too. So we | |
4064 | * have to do the relocation again. | |
4065 | */ | |
4066 | assert(memorystatus_freeze_enabled == FALSE); | |
4067 | ||
4068 | retries++; | |
4069 | if (retries < 3) { | |
4070 | proc_list_unlock(); | |
4071 | goto again; | |
4072 | } | |
4073 | #if DEVELOPMENT || DEBUG | |
4074 | panic("memorystatus_disable_freeze: Failed to kill all frozen processes, memorystatus_frozen_count = %d, errors = %d", | |
0a7de745 | 4075 | memorystatus_frozen_count, errors_over_prev_iteration); |
d9a64523 A |
4076 | #endif /* DEVELOPMENT || DEBUG */ |
4077 | } | |
4078 | proc_list_unlock(); | |
4079 | ||
4080 | os_reason_free(jetsam_reason); | |
4081 | ||
4082 | if (killed) { | |
d9a64523 A |
4083 | vm_swap_consider_defragmenting(VM_SWAP_FLAGS_FORCE_DEFRAG | VM_SWAP_FLAGS_FORCE_RECLAIM); |
4084 | ||
4085 | proc_list_lock(); | |
4086 | size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + | |
0a7de745 | 4087 | sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count); |
d9a64523 A |
4088 | uint64_t timestamp_now = mach_absolute_time(); |
4089 | memorystatus_jetsam_snapshot->notification_time = timestamp_now; | |
4090 | memorystatus_jetsam_snapshot->js_gencount++; | |
4091 | if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || | |
0a7de745 | 4092 | timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { |
d9a64523 A |
4093 | proc_list_unlock(); |
4094 | int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); | |
4095 | if (!ret) { | |
4096 | proc_list_lock(); | |
4097 | memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; | |
4098 | proc_list_unlock(); | |
4099 | } | |
4100 | } else { | |
4101 | proc_list_unlock(); | |
4102 | } | |
4103 | } | |
4104 | ||
4105 | return; | |
4106 | } | |
4107 | #endif /* CONFIG_FREEZE */ | |
4108 | ||
5ba3f43e A |
4109 | static boolean_t |
4110 | memorystatus_act_on_hiwat_processes(uint32_t *errors, uint32_t *hwm_kill, boolean_t *post_snapshot, __unused boolean_t *is_critical) | |
4111 | { | |
a39ff7e2 A |
4112 | boolean_t purged = FALSE; |
4113 | boolean_t killed = memorystatus_kill_hiwat_proc(errors, &purged); | |
5ba3f43e A |
4114 | |
4115 | if (killed) { | |
4116 | *hwm_kill = *hwm_kill + 1; | |
4117 | *post_snapshot = TRUE; | |
4118 | return TRUE; | |
4119 | } else { | |
a39ff7e2 A |
4120 | if (purged == FALSE) { |
4121 | /* couldn't purge and couldn't kill */ | |
4122 | memorystatus_hwm_candidates = FALSE; | |
4123 | } | |
5ba3f43e A |
4124 | } |
4125 | ||
4126 | #if CONFIG_JETSAM | |
4127 | /* No highwater processes to kill. Continue or stop for now? */ | |
4128 | if (!is_reason_thrashing(kill_under_pressure_cause) && | |
0a7de745 | 4129 | !is_reason_zone_map_exhaustion(kill_under_pressure_cause) && |
5ba3f43e A |
4130 | (memorystatus_available_pages > memorystatus_available_pages_critical)) { |
4131 | /* | |
4132 | * We are _not_ out of pressure but we are above the critical threshold and there's: | |
4133 | * - no compressor thrashing | |
4134 | * - enough zone memory | |
4135 | * - no more HWM processes left. | |
4136 | * For now, don't kill any other processes. | |
4137 | */ | |
0a7de745 | 4138 | |
5ba3f43e A |
4139 | if (*hwm_kill == 0) { |
4140 | memorystatus_thread_wasted_wakeup++; | |
4141 | } | |
4142 | ||
4143 | *is_critical = FALSE; | |
4144 | ||
4145 | return TRUE; | |
4146 | } | |
4147 | #endif /* CONFIG_JETSAM */ | |
4148 | ||
4149 | return FALSE; | |
4150 | } | |
4151 | ||
4152 | static boolean_t | |
4153 | memorystatus_act_aggressive(uint32_t cause, os_reason_t jetsam_reason, int *jld_idle_kills, boolean_t *corpse_list_purged, boolean_t *post_snapshot) | |
4154 | { | |
4155 | if (memorystatus_jld_enabled == TRUE) { | |
5ba3f43e A |
4156 | boolean_t killed; |
4157 | uint32_t errors = 0; | |
4158 | ||
4159 | /* Jetsam Loop Detection - locals */ | |
4160 | memstat_bucket_t *bucket; | |
0a7de745 A |
4161 | int jld_bucket_count = 0; |
4162 | struct timeval jld_now_tstamp = {0, 0}; | |
4163 | uint64_t jld_now_msecs = 0; | |
4164 | int elevated_bucket_count = 0; | |
5ba3f43e A |
4165 | |
4166 | /* Jetsam Loop Detection - statics */ | |
4167 | static uint64_t jld_timestamp_msecs = 0; | |
0a7de745 A |
4168 | static int jld_idle_kill_candidates = 0; /* Number of available processes in band 0,1 at start */ |
4169 | static int jld_eval_aggressive_count = 0; /* Bumps the max priority in aggressive loop */ | |
5ba3f43e A |
4170 | static int32_t jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; |
4171 | /* | |
4172 | * Jetsam Loop Detection: attempt to detect | |
4173 | * rapid daemon relaunches in the lower bands. | |
4174 | */ | |
0a7de745 | 4175 | |
5ba3f43e A |
4176 | microuptime(&jld_now_tstamp); |
4177 | ||
4178 | /* | |
4179 | * Ignore usecs in this calculation. | |
4180 | * msecs granularity is close enough. | |
4181 | */ | |
4182 | jld_now_msecs = (jld_now_tstamp.tv_sec * 1000); | |
4183 | ||
4184 | proc_list_lock(); | |
4185 | switch (jetsam_aging_policy) { | |
4186 | case kJetsamAgingPolicyLegacy: | |
4187 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
4188 | jld_bucket_count = bucket->count; | |
4189 | bucket = &memstat_bucket[JETSAM_PRIORITY_AGING_BAND1]; | |
4190 | jld_bucket_count += bucket->count; | |
4191 | break; | |
4192 | case kJetsamAgingPolicySysProcsReclaimedFirst: | |
4193 | case kJetsamAgingPolicyAppsReclaimedFirst: | |
4194 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
4195 | jld_bucket_count = bucket->count; | |
4196 | bucket = &memstat_bucket[system_procs_aging_band]; | |
4197 | jld_bucket_count += bucket->count; | |
4198 | bucket = &memstat_bucket[applications_aging_band]; | |
4199 | jld_bucket_count += bucket->count; | |
4200 | break; | |
4201 | case kJetsamAgingPolicyNone: | |
4202 | default: | |
4203 | bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
4204 | jld_bucket_count = bucket->count; | |
4205 | break; | |
4206 | } | |
4207 | ||
4208 | bucket = &memstat_bucket[JETSAM_PRIORITY_ELEVATED_INACTIVE]; | |
4209 | elevated_bucket_count = bucket->count; | |
4210 | ||
4211 | proc_list_unlock(); | |
4212 | ||
4213 | /* | |
4214 | * memorystatus_jld_eval_period_msecs is a tunable | |
4215 | * memorystatus_jld_eval_aggressive_count is a tunable | |
4216 | * memorystatus_jld_eval_aggressive_priority_band_max is a tunable | |
4217 | */ | |
0a7de745 A |
4218 | if ((jld_bucket_count == 0) || |
4219 | (jld_now_msecs > (jld_timestamp_msecs + memorystatus_jld_eval_period_msecs))) { | |
4220 | /* | |
4221 | * Refresh evaluation parameters | |
5ba3f43e | 4222 | */ |
0a7de745 | 4223 | jld_timestamp_msecs = jld_now_msecs; |
5ba3f43e | 4224 | jld_idle_kill_candidates = jld_bucket_count; |
0a7de745 | 4225 | *jld_idle_kills = 0; |
5ba3f43e | 4226 | jld_eval_aggressive_count = 0; |
0a7de745 | 4227 | jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT; |
5ba3f43e A |
4228 | } |
4229 | ||
4230 | if (*jld_idle_kills > jld_idle_kill_candidates) { | |
4231 | jld_eval_aggressive_count++; | |
4232 | ||
4233 | #if DEVELOPMENT || DEBUG | |
4234 | printf("memorystatus: aggressive%d: beginning of window: %lld ms, : timestamp now: %lld ms\n", | |
0a7de745 A |
4235 | jld_eval_aggressive_count, |
4236 | jld_timestamp_msecs, | |
4237 | jld_now_msecs); | |
5ba3f43e | 4238 | printf("memorystatus: aggressive%d: idle candidates: %d, idle kills: %d\n", |
0a7de745 A |
4239 | jld_eval_aggressive_count, |
4240 | jld_idle_kill_candidates, | |
4241 | *jld_idle_kills); | |
5ba3f43e A |
4242 | #endif /* DEVELOPMENT || DEBUG */ |
4243 | ||
4244 | if ((jld_eval_aggressive_count == memorystatus_jld_eval_aggressive_count) && | |
4245 | (total_corpses_count() > 0) && (*corpse_list_purged == FALSE)) { | |
4246 | /* | |
4247 | * If we reach this aggressive cycle, corpses might be causing memory pressure. | |
4248 | * So, in an effort to avoid jetsams in the FG band, we will attempt to purge | |
4249 | * corpse memory prior to this final march through JETSAM_PRIORITY_UI_SUPPORT. | |
4250 | */ | |
4251 | task_purge_all_corpses(); | |
4252 | *corpse_list_purged = TRUE; | |
0a7de745 A |
4253 | } else if (jld_eval_aggressive_count > memorystatus_jld_eval_aggressive_count) { |
4254 | /* | |
5ba3f43e A |
4255 | * Bump up the jetsam priority limit (eg: the bucket index) |
4256 | * Enforce bucket index sanity. | |
4257 | */ | |
0a7de745 | 4258 | if ((memorystatus_jld_eval_aggressive_priority_band_max < 0) || |
5ba3f43e A |
4259 | (memorystatus_jld_eval_aggressive_priority_band_max >= MEMSTAT_BUCKET_COUNT)) { |
4260 | /* | |
4261 | * Do nothing. Stick with the default level. | |
4262 | */ | |
4263 | } else { | |
4264 | jld_priority_band_max = memorystatus_jld_eval_aggressive_priority_band_max; | |
4265 | } | |
4266 | } | |
4267 | ||
4268 | /* Visit elevated processes first */ | |
4269 | while (elevated_bucket_count) { | |
5ba3f43e A |
4270 | elevated_bucket_count--; |
4271 | ||
4272 | /* | |
4273 | * memorystatus_kill_elevated_process() drops a reference, | |
4274 | * so take another one so we can continue to use this exit reason | |
4275 | * even after it returns. | |
4276 | */ | |
4277 | ||
4278 | os_reason_ref(jetsam_reason); | |
4279 | killed = memorystatus_kill_elevated_process( | |
4280 | cause, | |
4281 | jetsam_reason, | |
d9a64523 | 4282 | JETSAM_PRIORITY_ELEVATED_INACTIVE, |
5ba3f43e A |
4283 | jld_eval_aggressive_count, |
4284 | &errors); | |
4285 | ||
4286 | if (killed) { | |
4287 | *post_snapshot = TRUE; | |
4288 | if (memorystatus_avail_pages_below_pressure()) { | |
4289 | /* | |
4290 | * Still under pressure. | |
4291 | * Find another pinned processes. | |
4292 | */ | |
4293 | continue; | |
4294 | } else { | |
4295 | return TRUE; | |
4296 | } | |
4297 | } else { | |
4298 | /* | |
4299 | * No pinned processes left to kill. | |
4300 | * Abandon elevated band. | |
4301 | */ | |
4302 | break; | |
4303 | } | |
4304 | } | |
4305 | ||
4306 | /* | |
4307 | * memorystatus_kill_top_process_aggressive() allocates its own | |
d9a64523 | 4308 | * jetsam_reason so the kMemorystatusKilledProcThrashing cause |
5ba3f43e A |
4309 | * is consistent throughout the aggressive march. |
4310 | */ | |
4311 | killed = memorystatus_kill_top_process_aggressive( | |
d9a64523 | 4312 | kMemorystatusKilledProcThrashing, |
0a7de745 A |
4313 | jld_eval_aggressive_count, |
4314 | jld_priority_band_max, | |
5ba3f43e | 4315 | &errors); |
0a7de745 | 4316 | |
5ba3f43e A |
4317 | if (killed) { |
4318 | /* Always generate logs after aggressive kill */ | |
4319 | *post_snapshot = TRUE; | |
4320 | *jld_idle_kills = 0; | |
4321 | return TRUE; | |
0a7de745 | 4322 | } |
5ba3f43e A |
4323 | } |
4324 | ||
4325 | return FALSE; | |
4326 | } | |
4327 | ||
4328 | return FALSE; | |
4329 | } | |
4330 | ||
4331 | ||
39236c6e A |
4332 | static void |
4333 | memorystatus_thread(void *param __unused, wait_result_t wr __unused) | |
4334 | { | |
39236c6e A |
4335 | boolean_t post_snapshot = FALSE; |
4336 | uint32_t errors = 0; | |
fe8ab488 | 4337 | uint32_t hwm_kill = 0; |
3e170ce0 | 4338 | boolean_t sort_flag = TRUE; |
39037602 | 4339 | boolean_t corpse_list_purged = FALSE; |
0a7de745 | 4340 | int jld_idle_kills = 0; |
d9a64523 | 4341 | struct jetsam_thread_state *jetsam_thread = jetsam_current_thread(); |
316670eb | 4342 | |
d9a64523 | 4343 | if (jetsam_thread->inited == FALSE) { |
0a7de745 | 4344 | /* |
39236c6e A |
4345 | * It's the first time the thread has run, so just mark the thread as privileged and block. |
4346 | * This avoids a spurious pass with unset variables, as set out in <rdar://problem/9609402>. | |
4347 | */ | |
d9a64523 A |
4348 | |
4349 | char name[32]; | |
39236c6e | 4350 | thread_wire(host_priv_self(), current_thread(), TRUE); |
d9a64523 A |
4351 | snprintf(name, 32, "VM_memorystatus_%d", jetsam_thread->index + 1); |
4352 | ||
4353 | if (jetsam_thread->index == 0) { | |
4354 | if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) { | |
4355 | thread_vm_bind_group_add(); | |
4356 | } | |
4357 | } | |
4358 | thread_set_thread_name(current_thread(), name); | |
4359 | jetsam_thread->inited = TRUE; | |
39236c6e | 4360 | memorystatus_thread_block(0, memorystatus_thread); |
316670eb | 4361 | } |
0a7de745 | 4362 | |
39236c6e | 4363 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START, |
0a7de745 | 4364 | memorystatus_available_pages, memorystatus_jld_enabled, memorystatus_jld_eval_period_msecs, memorystatus_jld_eval_aggressive_count, 0); |
316670eb | 4365 | |
fe8ab488 A |
4366 | /* |
4367 | * Jetsam aware version. | |
4368 | * | |
4369 | * The VM pressure notification thread is working it's way through clients in parallel. | |
39236c6e | 4370 | * |
0a7de745 A |
4371 | * So, while the pressure notification thread is targeting processes in order of |
4372 | * increasing jetsam priority, we can hopefully reduce / stop it's work by killing | |
fe8ab488 | 4373 | * any processes that have exceeded their highwater mark. |
39236c6e | 4374 | * |
fe8ab488 A |
4375 | * If we run out of HWM processes and our available pages drops below the critical threshold, then, |
4376 | * we target the least recently used process in order of increasing jetsam priority (exception: the FG band). | |
39236c6e | 4377 | */ |
5ba3f43e | 4378 | while (memorystatus_action_needed()) { |
39236c6e A |
4379 | boolean_t killed; |
4380 | int32_t priority; | |
fe8ab488 | 4381 | uint32_t cause; |
39037602 A |
4382 | uint64_t jetsam_reason_code = JETSAM_REASON_INVALID; |
4383 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
fe8ab488 | 4384 | |
39037602 A |
4385 | cause = kill_under_pressure_cause; |
4386 | switch (cause) { | |
0a7de745 A |
4387 | case kMemorystatusKilledFCThrashing: |
4388 | jetsam_reason_code = JETSAM_REASON_MEMORY_FCTHRASHING; | |
4389 | break; | |
4390 | case kMemorystatusKilledVMCompressorThrashing: | |
4391 | jetsam_reason_code = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING; | |
4392 | break; | |
4393 | case kMemorystatusKilledVMCompressorSpaceShortage: | |
4394 | jetsam_reason_code = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE; | |
4395 | break; | |
4396 | case kMemorystatusKilledZoneMapExhaustion: | |
4397 | jetsam_reason_code = JETSAM_REASON_ZONE_MAP_EXHAUSTION; | |
4398 | break; | |
4399 | case kMemorystatusKilledVMPageShortage: | |
4400 | /* falls through */ | |
4401 | default: | |
4402 | jetsam_reason_code = JETSAM_REASON_MEMORY_VMPAGESHORTAGE; | |
4403 | cause = kMemorystatusKilledVMPageShortage; | |
4404 | break; | |
fe8ab488 | 4405 | } |
39236c6e | 4406 | |
39236c6e | 4407 | /* Highwater */ |
5ba3f43e A |
4408 | boolean_t is_critical = TRUE; |
4409 | if (memorystatus_act_on_hiwat_processes(&errors, &hwm_kill, &post_snapshot, &is_critical)) { | |
4410 | if (is_critical == FALSE) { | |
4411 | /* | |
4412 | * For now, don't kill any other processes. | |
4413 | */ | |
4414 | break; | |
4415 | } else { | |
4416 | goto done; | |
fe8ab488 | 4417 | } |
39236c6e | 4418 | } |
39037602 A |
4419 | |
4420 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, jetsam_reason_code); | |
4421 | if (jetsam_reason == OS_REASON_NULL) { | |
4422 | printf("memorystatus_thread: failed to allocate jetsam reason\n"); | |
4423 | } | |
4424 | ||
5ba3f43e A |
4425 | if (memorystatus_act_aggressive(cause, jetsam_reason, &jld_idle_kills, &corpse_list_purged, &post_snapshot)) { |
4426 | goto done; | |
3e170ce0 | 4427 | } |
39037602 A |
4428 | |
4429 | /* | |
4430 | * memorystatus_kill_top_process() drops a reference, | |
4431 | * so take another one so we can continue to use this exit reason | |
4432 | * even after it returns | |
4433 | */ | |
4434 | os_reason_ref(jetsam_reason); | |
4435 | ||
39236c6e | 4436 | /* LRU */ |
39037602 | 4437 | killed = memorystatus_kill_top_process(TRUE, sort_flag, cause, jetsam_reason, &priority, &errors); |
3e170ce0 A |
4438 | sort_flag = FALSE; |
4439 | ||
39236c6e | 4440 | if (killed) { |
5ba3f43e | 4441 | if (memorystatus_post_snapshot(priority, cause) == TRUE) { |
0a7de745 | 4442 | post_snapshot = TRUE; |
39236c6e | 4443 | } |
3e170ce0 A |
4444 | |
4445 | /* Jetsam Loop Detection */ | |
4446 | if (memorystatus_jld_enabled == TRUE) { | |
39037602 | 4447 | if ((priority == JETSAM_PRIORITY_IDLE) || (priority == system_procs_aging_band) || (priority == applications_aging_band)) { |
3e170ce0 A |
4448 | jld_idle_kills++; |
4449 | } else { | |
4450 | /* | |
4451 | * We've reached into bands beyond idle deferred. | |
4452 | * We make no attempt to monitor them | |
4453 | */ | |
4454 | } | |
4455 | } | |
39037602 | 4456 | |
5ba3f43e | 4457 | if ((priority >= JETSAM_PRIORITY_UI_SUPPORT) && (total_corpses_count() > 0) && (corpse_list_purged == FALSE)) { |
39037602 A |
4458 | /* |
4459 | * If we have jetsammed a process in or above JETSAM_PRIORITY_UI_SUPPORT | |
4460 | * then we attempt to relieve pressure by purging corpse memory. | |
4461 | */ | |
4462 | task_purge_all_corpses(); | |
4463 | corpse_list_purged = TRUE; | |
4464 | } | |
39236c6e A |
4465 | goto done; |
4466 | } | |
0a7de745 | 4467 | |
5ba3f43e | 4468 | if (memorystatus_avail_pages_below_critical()) { |
39037602 A |
4469 | /* |
4470 | * Still under pressure and unable to kill a process - purge corpse memory | |
4471 | */ | |
5ba3f43e | 4472 | if (total_corpses_count() > 0) { |
39037602 A |
4473 | task_purge_all_corpses(); |
4474 | corpse_list_purged = TRUE; | |
4475 | } | |
4476 | ||
5ba3f43e | 4477 | if (memorystatus_avail_pages_below_critical()) { |
39037602 A |
4478 | /* |
4479 | * Still under pressure and unable to kill a process - panic | |
4480 | */ | |
5ba3f43e | 4481 | panic("memorystatus_jetsam_thread: no victim! available pages:%llu\n", (uint64_t)memorystatus_available_pages); |
39037602 | 4482 | } |
fe8ab488 | 4483 | } |
0a7de745 A |
4484 | |
4485 | done: | |
fe8ab488 A |
4486 | |
4487 | /* | |
4488 | * We do not want to over-kill when thrashing has been detected. | |
4489 | * To avoid that, we reset the flag here and notify the | |
4490 | * compressor. | |
39236c6e | 4491 | */ |
5ba3f43e | 4492 | if (is_reason_thrashing(kill_under_pressure_cause)) { |
fe8ab488 | 4493 | kill_under_pressure_cause = 0; |
5ba3f43e | 4494 | #if CONFIG_JETSAM |
fe8ab488 | 4495 | vm_thrashing_jetsam_done(); |
5ba3f43e A |
4496 | #endif /* CONFIG_JETSAM */ |
4497 | } else if (is_reason_zone_map_exhaustion(kill_under_pressure_cause)) { | |
4498 | kill_under_pressure_cause = 0; | |
39236c6e | 4499 | } |
39037602 A |
4500 | |
4501 | os_reason_free(jetsam_reason); | |
39236c6e | 4502 | } |
fe8ab488 A |
4503 | |
4504 | kill_under_pressure_cause = 0; | |
0a7de745 | 4505 | |
39236c6e A |
4506 | if (errors) { |
4507 | memorystatus_clear_errors(); | |
4508 | } | |
4509 | ||
39236c6e | 4510 | if (post_snapshot) { |
39037602 | 4511 | proc_list_lock(); |
39236c6e | 4512 | size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + |
0a7de745 | 4513 | sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count); |
3e170ce0 A |
4514 | uint64_t timestamp_now = mach_absolute_time(); |
4515 | memorystatus_jetsam_snapshot->notification_time = timestamp_now; | |
39037602 | 4516 | memorystatus_jetsam_snapshot->js_gencount++; |
d190cdc3 | 4517 | if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || |
0a7de745 | 4518 | timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { |
39037602 | 4519 | proc_list_unlock(); |
3e170ce0 A |
4520 | int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); |
4521 | if (!ret) { | |
4522 | proc_list_lock(); | |
4523 | memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; | |
4524 | proc_list_unlock(); | |
4525 | } | |
39037602 A |
4526 | } else { |
4527 | proc_list_unlock(); | |
3e170ce0 | 4528 | } |
39236c6e | 4529 | } |
3e170ce0 | 4530 | |
39236c6e | 4531 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_END, |
0a7de745 | 4532 | memorystatus_available_pages, 0, 0, 0, 0); |
39236c6e | 4533 | |
39236c6e A |
4534 | memorystatus_thread_block(0, memorystatus_thread); |
4535 | } | |
4536 | ||
fe8ab488 A |
4537 | /* |
4538 | * Returns TRUE: | |
0a7de745 | 4539 | * when an idle-exitable proc was killed |
fe8ab488 A |
4540 | * Returns FALSE: |
4541 | * when there are no more idle-exitable procs found | |
0a7de745 | 4542 | * when the attempt to kill an idle-exitable proc failed |
fe8ab488 | 4543 | */ |
0a7de745 A |
4544 | boolean_t |
4545 | memorystatus_idle_exit_from_VM(void) | |
4546 | { | |
5ba3f43e A |
4547 | /* |
4548 | * This routine should no longer be needed since we are | |
4549 | * now using jetsam bands on all platforms and so will deal | |
4550 | * with IDLE processes within the memorystatus thread itself. | |
4551 | * | |
4552 | * But we still use it because we observed that macos systems | |
4553 | * started heavy compression/swapping with a bunch of | |
4554 | * idle-exitable processes alive and doing nothing. We decided | |
4555 | * to rather kill those processes than start swapping earlier. | |
4556 | */ | |
4557 | ||
0a7de745 | 4558 | return kill_idle_exit_proc(); |
39236c6e | 4559 | } |
39236c6e | 4560 | |
39037602 A |
4561 | /* |
4562 | * Callback invoked when allowable physical memory footprint exceeded | |
4563 | * (dirty pages + IOKit mappings) | |
4564 | * | |
4565 | * This is invoked for both advisory, non-fatal per-task high watermarks, | |
4566 | * as well as the fatal task memory limits. | |
4567 | */ | |
4568 | void | |
813fb2f6 | 4569 | memorystatus_on_ledger_footprint_exceeded(boolean_t warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal) |
39037602 A |
4570 | { |
4571 | os_reason_t jetsam_reason = OS_REASON_NULL; | |
4572 | ||
4573 | proc_t p = current_proc(); | |
4574 | ||
39236c6e A |
4575 | #if VM_PRESSURE_EVENTS |
4576 | if (warning == TRUE) { | |
39037602 A |
4577 | /* |
4578 | * This is a warning path which implies that the current process is close, but has | |
4579 | * not yet exceeded its per-process memory limit. | |
4580 | */ | |
0a7de745 | 4581 | if (memorystatus_warn_process(p->p_pid, memlimit_is_active, memlimit_is_fatal, FALSE /* not exceeded */) != TRUE) { |
39236c6e | 4582 | /* Print warning, since it's possible that task has not registered for pressure notifications */ |
5ba3f43e | 4583 | 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); |
39236c6e A |
4584 | } |
4585 | return; | |
4586 | } | |
4587 | #endif /* VM_PRESSURE_EVENTS */ | |
4588 | ||
813fb2f6 | 4589 | if (memlimit_is_fatal) { |
39236c6e | 4590 | /* |
fe8ab488 A |
4591 | * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task |
4592 | * has violated either the system-wide per-task memory limit OR its own task limit. | |
39236c6e | 4593 | */ |
39037602 A |
4594 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_PERPROCESSLIMIT); |
4595 | if (jetsam_reason == NULL) { | |
4596 | printf("task_exceeded footprint: failed to allocate jetsam reason\n"); | |
d9a64523 | 4597 | } else if (corpse_for_fatal_memkill != 0 && proc_send_synchronous_EXC_RESOURCE(p) == FALSE) { |
39037602 A |
4598 | /* Set OS_REASON_FLAG_GENERATE_CRASH_REPORT to generate corpse */ |
4599 | jetsam_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT; | |
4600 | } | |
4601 | ||
4602 | if (memorystatus_kill_process_sync(p->p_pid, kMemorystatusKilledPerProcessLimit, jetsam_reason) != TRUE) { | |
39236c6e A |
4603 | printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n"); |
4604 | } | |
fe8ab488 A |
4605 | } else { |
4606 | /* | |
4607 | * HWM offender exists. Done without locks or synchronization. | |
4608 | * See comment near its declaration for more details. | |
4609 | */ | |
4610 | memorystatus_hwm_candidates = TRUE; | |
39037602 A |
4611 | |
4612 | #if VM_PRESSURE_EVENTS | |
4613 | /* | |
4614 | * The current process is not in the warning path. | |
4615 | * This path implies the current process has exceeded a non-fatal (soft) memory limit. | |
4616 | * Failure to send note is ignored here. | |
4617 | */ | |
813fb2f6 | 4618 | (void)memorystatus_warn_process(p->p_pid, memlimit_is_active, memlimit_is_fatal, TRUE /* exceeded */); |
39037602 A |
4619 | |
4620 | #endif /* VM_PRESSURE_EVENTS */ | |
4621 | } | |
4622 | } | |
4623 | ||
813fb2f6 A |
4624 | void |
4625 | memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal) | |
4626 | { | |
4627 | proc_t p = current_proc(); | |
4628 | ||
4629 | /* | |
4630 | * The limit violation is logged here, but only once per process per limit. | |
4631 | * Soft memory limit is a non-fatal high-water-mark | |
4632 | * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit. | |
4633 | */ | |
4634 | ||
5ba3f43e | 4635 | os_log_with_startup_serial(OS_LOG_DEFAULT, "EXC_RESOURCE -> %s[%d] exceeded mem limit: %s%s %d MB (%s)\n", |
0a7de745 A |
4636 | (*p->p_name ? p->p_name : "unknown"), p->p_pid, (memlimit_is_active ? "Active" : "Inactive"), |
4637 | (memlimit_is_fatal ? "Hard" : "Soft"), max_footprint_mb, | |
4638 | (memlimit_is_fatal ? "fatal" : "non-fatal")); | |
813fb2f6 A |
4639 | |
4640 | return; | |
4641 | } | |
4642 | ||
4643 | ||
39037602 A |
4644 | /* |
4645 | * Description: | |
5ba3f43e A |
4646 | * Evaluates process state to determine which limit |
4647 | * should be applied (active vs. inactive limit). | |
4648 | * | |
4649 | * Processes that have the 'elevated inactive jetsam band' attribute | |
4650 | * are first evaluated based on their current priority band. | |
4651 | * presently elevated ==> active | |
4652 | * | |
39037602 A |
4653 | * Processes that opt into dirty tracking are evaluated |
4654 | * based on clean vs dirty state. | |
4655 | * dirty ==> active | |
4656 | * clean ==> inactive | |
4657 | * | |
4658 | * Process that do not opt into dirty tracking are | |
4659 | * evalulated based on priority level. | |
4660 | * Foreground or above ==> active | |
4661 | * Below Foreground ==> inactive | |
4662 | * | |
4663 | * Return: TRUE if active | |
4664 | * False if inactive | |
4665 | */ | |
4666 | ||
4667 | static boolean_t | |
0a7de745 A |
4668 | proc_jetsam_state_is_active_locked(proc_t p) |
4669 | { | |
5ba3f43e A |
4670 | if ((p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND) && |
4671 | (p->p_memstat_effectivepriority == JETSAM_PRIORITY_ELEVATED_INACTIVE)) { | |
4672 | /* | |
4673 | * process has the 'elevated inactive jetsam band' attribute | |
4674 | * and process is present in the elevated band | |
4675 | * implies active state | |
4676 | */ | |
4677 | return TRUE; | |
4678 | } else if (p->p_memstat_dirty & P_DIRTY_TRACK) { | |
39037602 A |
4679 | /* |
4680 | * process has opted into dirty tracking | |
4681 | * active state is based on dirty vs. clean | |
4682 | */ | |
4683 | if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) { | |
4684 | /* | |
4685 | * process is dirty | |
4686 | * implies active state | |
4687 | */ | |
4688 | return TRUE; | |
4689 | } else { | |
4690 | /* | |
4691 | * process is clean | |
4692 | * implies inactive state | |
4693 | */ | |
4694 | return FALSE; | |
4695 | } | |
4696 | } else if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) { | |
4697 | /* | |
4698 | * process is Foreground or higher | |
4699 | * implies active state | |
4700 | */ | |
4701 | return TRUE; | |
4702 | } else { | |
4703 | /* | |
4704 | * process found below Foreground | |
4705 | * implies inactive state | |
4706 | */ | |
4707 | return FALSE; | |
4708 | } | |
4709 | } | |
4710 | ||
0a7de745 A |
4711 | static boolean_t |
4712 | memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) | |
4713 | { | |
39037602 A |
4714 | boolean_t res; |
4715 | ||
39037602 A |
4716 | uint32_t errors = 0; |
4717 | ||
4718 | if (victim_pid == -1) { | |
4719 | /* No pid, so kill first process */ | |
4720 | res = memorystatus_kill_top_process(TRUE, TRUE, cause, jetsam_reason, NULL, &errors); | |
4721 | } else { | |
4722 | res = memorystatus_kill_specific_process(victim_pid, cause, jetsam_reason); | |
4723 | } | |
0a7de745 | 4724 | |
39037602 A |
4725 | if (errors) { |
4726 | memorystatus_clear_errors(); | |
4727 | } | |
4728 | ||
4729 | if (res == TRUE) { | |
4730 | /* Fire off snapshot notification */ | |
4731 | proc_list_lock(); | |
0a7de745 A |
4732 | size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + |
4733 | sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count; | |
39037602 A |
4734 | uint64_t timestamp_now = mach_absolute_time(); |
4735 | memorystatus_jetsam_snapshot->notification_time = timestamp_now; | |
d190cdc3 | 4736 | if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 || |
0a7de745 | 4737 | timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) { |
39037602 A |
4738 | proc_list_unlock(); |
4739 | int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size)); | |
4740 | if (!ret) { | |
4741 | proc_list_lock(); | |
4742 | memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; | |
4743 | proc_list_unlock(); | |
4744 | } | |
4745 | } else { | |
4746 | proc_list_unlock(); | |
4747 | } | |
4748 | } | |
39037602 | 4749 | |
39037602 A |
4750 | return res; |
4751 | } | |
4752 | ||
4753 | /* | |
4754 | * Jetsam a specific process. | |
4755 | */ | |
0a7de745 A |
4756 | static boolean_t |
4757 | memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason) | |
4758 | { | |
39037602 A |
4759 | boolean_t killed; |
4760 | proc_t p; | |
4761 | uint64_t killtime = 0; | |
0a7de745 A |
4762 | clock_sec_t tv_sec; |
4763 | clock_usec_t tv_usec; | |
4764 | uint32_t tv_msec; | |
39037602 A |
4765 | |
4766 | /* TODO - add a victim queue and push this into the main jetsam thread */ | |
4767 | ||
4768 | p = proc_find(victim_pid); | |
4769 | if (!p) { | |
4770 | os_reason_free(jetsam_reason); | |
4771 | return FALSE; | |
4772 | } | |
4773 | ||
4774 | proc_list_lock(); | |
4775 | ||
39037602 | 4776 | if (memorystatus_jetsam_snapshot_count == 0) { |
0a7de745 | 4777 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); |
fe8ab488 | 4778 | } |
39037602 A |
4779 | |
4780 | killtime = mach_absolute_time(); | |
0a7de745 A |
4781 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); |
4782 | tv_msec = tv_usec / 1000; | |
39037602 A |
4783 | |
4784 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); | |
4785 | ||
4786 | proc_list_unlock(); | |
4787 | ||
5ba3f43e | 4788 | os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: killing_specific_process pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", |
0a7de745 A |
4789 | (unsigned long)tv_sec, tv_msec, victim_pid, (*p->p_name ? p->p_name : "unknown"), |
4790 | memorystatus_kill_cause_name[cause], p->p_memstat_effectivepriority, (uint64_t)memorystatus_available_pages); | |
4791 | ||
39037602 A |
4792 | killed = memorystatus_do_kill(p, cause, jetsam_reason); |
4793 | proc_rele(p); | |
0a7de745 | 4794 | |
39037602 | 4795 | return killed; |
fe8ab488 A |
4796 | } |
4797 | ||
39037602 | 4798 | |
3e170ce0 A |
4799 | /* |
4800 | * Toggle the P_MEMSTAT_TERMINATED state. | |
4801 | * Takes the proc_list_lock. | |
4802 | */ | |
4803 | void | |
4804 | proc_memstat_terminated(proc_t p, boolean_t set) | |
4805 | { | |
4806 | #if DEVELOPMENT || DEBUG | |
4807 | if (p) { | |
4808 | proc_list_lock(); | |
4809 | if (set == TRUE) { | |
4810 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
4811 | } else { | |
4812 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
4813 | } | |
4814 | proc_list_unlock(); | |
4815 | } | |
4816 | #else | |
4817 | #pragma unused(p, set) | |
4818 | /* | |
4819 | * do nothing | |
4820 | */ | |
4821 | #endif /* DEVELOPMENT || DEBUG */ | |
4822 | return; | |
4823 | } | |
4824 | ||
39037602 A |
4825 | |
4826 | #if CONFIG_JETSAM | |
fe8ab488 A |
4827 | /* |
4828 | * This is invoked when cpulimits have been exceeded while in fatal mode. | |
4829 | * The jetsam_flags do not apply as those are for memory related kills. | |
0a7de745 | 4830 | * We call this routine so that the offending process is killed with |
fe8ab488 A |
4831 | * a non-zero exit status. |
4832 | */ | |
4833 | void | |
4834 | jetsam_on_ledger_cpulimit_exceeded(void) | |
4835 | { | |
4836 | int retval = 0; | |
4837 | int jetsam_flags = 0; /* make it obvious */ | |
4838 | proc_t p = current_proc(); | |
39037602 | 4839 | os_reason_t jetsam_reason = OS_REASON_NULL; |
fe8ab488 A |
4840 | |
4841 | printf("task_exceeded_cpulimit: killing pid %d [%s]\n", | |
0a7de745 | 4842 | p->p_pid, (*p->p_name ? p->p_name : "(unknown)")); |
fe8ab488 | 4843 | |
39037602 A |
4844 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_CPULIMIT); |
4845 | if (jetsam_reason == OS_REASON_NULL) { | |
4846 | printf("task_exceeded_cpulimit: unable to allocate memory for jetsam reason\n"); | |
4847 | } | |
4848 | ||
4849 | retval = jetsam_do_kill(p, jetsam_flags, jetsam_reason); | |
0a7de745 | 4850 | |
fe8ab488 A |
4851 | if (retval) { |
4852 | printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n"); | |
39236c6e A |
4853 | } |
4854 | } | |
4855 | ||
5ba3f43e A |
4856 | #endif /* CONFIG_JETSAM */ |
4857 | ||
39037602 A |
4858 | static void |
4859 | memorystatus_get_task_memory_region_count(task_t task, uint64_t *count) | |
4860 | { | |
4861 | assert(task); | |
4862 | assert(count); | |
4863 | ||
4864 | *count = get_task_memory_region_count(task); | |
4865 | } | |
4866 | ||
a39ff7e2 A |
4867 | |
4868 | #define MEMORYSTATUS_VM_MAP_FORK_ALLOWED 0x100000000 | |
4869 | #define MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED 0x200000000 | |
4870 | ||
5c9f4661 A |
4871 | #if DEVELOPMENT || DEBUG |
4872 | ||
4873 | /* | |
4874 | * Sysctl only used to test memorystatus_allowed_vm_map_fork() path. | |
4875 | * set a new pidwatch value | |
4876 | * or | |
4877 | * get the current pidwatch value | |
a39ff7e2 A |
4878 | * |
4879 | * The pidwatch_val starts out with a PID to watch for in the map_fork path. | |
4880 | * Its value is: | |
4881 | * - OR'd with MEMORYSTATUS_VM_MAP_FORK_ALLOWED if we allow the map_fork. | |
4882 | * - OR'd with MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED if we disallow the map_fork. | |
4883 | * - set to -1ull if the map_fork() is aborted for other reasons. | |
5c9f4661 A |
4884 | */ |
4885 | ||
4886 | uint64_t memorystatus_vm_map_fork_pidwatch_val = 0; | |
5c9f4661 A |
4887 | |
4888 | static int sysctl_memorystatus_vm_map_fork_pidwatch SYSCTL_HANDLER_ARGS { | |
4889 | #pragma unused(oidp, arg1, arg2) | |
4890 | ||
0a7de745 | 4891 | uint64_t new_value = 0; |
5c9f4661 | 4892 | uint64_t old_value = 0; |
0a7de745 | 4893 | int error = 0; |
5c9f4661 A |
4894 | |
4895 | /* | |
4896 | * The pid is held in the low 32 bits. | |
4897 | * The 'allowed' flags are in the upper 32 bits. | |
4898 | */ | |
4899 | old_value = memorystatus_vm_map_fork_pidwatch_val; | |
4900 | ||
0a7de745 | 4901 | error = sysctl_io_number(req, old_value, sizeof(old_value), &new_value, NULL); |
5c9f4661 | 4902 | |
0a7de745 | 4903 | if (error || !req->newptr) { |
5c9f4661 A |
4904 | /* |
4905 | * No new value passed in. | |
4906 | */ | |
0a7de745 | 4907 | return error; |
5c9f4661 A |
4908 | } |
4909 | ||
4910 | /* | |
4911 | * A new pid was passed in via req->newptr. | |
4912 | * Ignore any attempt to set the higher order bits. | |
4913 | */ | |
4914 | memorystatus_vm_map_fork_pidwatch_val = new_value & 0xFFFFFFFF; | |
4915 | printf("memorystatus: pidwatch old_value = 0x%llx, new_value = 0x%llx \n", old_value, new_value); | |
4916 | ||
0a7de745 | 4917 | return error; |
5c9f4661 A |
4918 | } |
4919 | ||
0a7de745 A |
4920 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_map_fork_pidwatch, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
4921 | 0, 0, sysctl_memorystatus_vm_map_fork_pidwatch, "Q", "get/set pid watched for in vm_map_fork"); | |
5c9f4661 A |
4922 | |
4923 | ||
a39ff7e2 A |
4924 | /* |
4925 | * Record if a watched process fails to qualify for a vm_map_fork(). | |
4926 | */ | |
4927 | void | |
4928 | memorystatus_abort_vm_map_fork(task_t task) | |
4929 | { | |
4930 | if (memorystatus_vm_map_fork_pidwatch_val != 0) { | |
4931 | proc_t p = get_bsdtask_info(task); | |
4932 | if (p != NULL && memorystatus_vm_map_fork_pidwatch_val == (uint64_t)p->p_pid) { | |
4933 | memorystatus_vm_map_fork_pidwatch_val = -1ull; | |
4934 | } | |
4935 | } | |
4936 | } | |
5c9f4661 | 4937 | |
a39ff7e2 A |
4938 | static void |
4939 | set_vm_map_fork_pidwatch(task_t task, uint64_t x) | |
4940 | { | |
4941 | if (memorystatus_vm_map_fork_pidwatch_val != 0) { | |
4942 | proc_t p = get_bsdtask_info(task); | |
4943 | if (p && (memorystatus_vm_map_fork_pidwatch_val == (uint64_t)p->p_pid)) { | |
4944 | memorystatus_vm_map_fork_pidwatch_val |= x; | |
4945 | } | |
4946 | } | |
4947 | } | |
5c9f4661 A |
4948 | |
4949 | #else /* DEVELOPMENT || DEBUG */ | |
4950 | ||
a39ff7e2 A |
4951 | |
4952 | static void | |
4953 | set_vm_map_fork_pidwatch(task_t task, uint64_t x) | |
4954 | { | |
4955 | #pragma unused(task) | |
4956 | #pragma unused(x) | |
4957 | } | |
5c9f4661 A |
4958 | |
4959 | #endif /* DEVELOPMENT || DEBUG */ | |
4960 | ||
5ba3f43e A |
4961 | /* |
4962 | * Called during EXC_RESOURCE handling when a process exceeds a soft | |
4963 | * memory limit. This is the corpse fork path and here we decide if | |
4964 | * vm_map_fork will be allowed when creating the corpse. | |
5c9f4661 | 4965 | * The task being considered is suspended. |
5ba3f43e A |
4966 | * |
4967 | * By default, a vm_map_fork is allowed to proceed. | |
4968 | * | |
4969 | * A few simple policy assumptions: | |
4970 | * Desktop platform is not considered in this path. | |
4971 | * The vm_map_fork is always allowed. | |
4972 | * | |
4973 | * If the device has a zero system-wide task limit, | |
4974 | * then the vm_map_fork is allowed. | |
4975 | * | |
4976 | * And if a process's memory footprint calculates less | |
4977 | * than or equal to half of the system-wide task limit, | |
4978 | * then the vm_map_fork is allowed. This calculation | |
4979 | * is based on the assumption that a process can | |
4980 | * munch memory up to the system-wide task limit. | |
4981 | */ | |
4982 | boolean_t | |
a39ff7e2 | 4983 | memorystatus_allowed_vm_map_fork(task_t task) |
5ba3f43e A |
4984 | { |
4985 | boolean_t is_allowed = TRUE; /* default */ | |
4986 | ||
4987 | #if CONFIG_EMBEDDED | |
4988 | ||
a39ff7e2 | 4989 | uint64_t footprint_in_bytes; |
a39ff7e2 | 4990 | uint64_t max_allowed_bytes; |
5ba3f43e A |
4991 | |
4992 | if (max_task_footprint_mb == 0) { | |
a39ff7e2 | 4993 | set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED); |
0a7de745 | 4994 | return is_allowed; |
5ba3f43e A |
4995 | } |
4996 | ||
5ba3f43e A |
4997 | footprint_in_bytes = get_task_phys_footprint(task); |
4998 | ||
4999 | /* | |
d9a64523 | 5000 | * Maximum is 1/4 of the system-wide task limit. |
5ba3f43e | 5001 | */ |
d9a64523 | 5002 | max_allowed_bytes = ((uint64_t)max_task_footprint_mb * 1024 * 1024) >> 2; |
5ba3f43e | 5003 | |
a39ff7e2 | 5004 | if (footprint_in_bytes > max_allowed_bytes) { |
5ba3f43e | 5005 | printf("memorystatus disallowed vm_map_fork %lld %lld\n", footprint_in_bytes, max_allowed_bytes); |
a39ff7e2 | 5006 | set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED); |
0a7de745 | 5007 | return !is_allowed; |
5ba3f43e | 5008 | } |
a39ff7e2 | 5009 | #endif /* CONFIG_EMBEDDED */ |
5ba3f43e | 5010 | |
a39ff7e2 | 5011 | set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED); |
0a7de745 | 5012 | return is_allowed; |
5ba3f43e A |
5013 | } |
5014 | ||
39236c6e | 5015 | static void |
d9a64523 | 5016 | memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages) |
39236c6e A |
5017 | { |
5018 | assert(task); | |
5019 | assert(footprint); | |
39037602 A |
5020 | |
5021 | uint64_t pages; | |
5022 | ||
5023 | pages = (get_task_phys_footprint(task) / PAGE_SIZE_64); | |
5024 | assert(((uint32_t)pages) == pages); | |
5025 | *footprint = (uint32_t)pages; | |
5026 | ||
fe8ab488 | 5027 | if (max_footprint_lifetime) { |
0a7de745 | 5028 | pages = (get_task_phys_footprint_lifetime_max(task) / PAGE_SIZE_64); |
39037602 A |
5029 | assert(((uint32_t)pages) == pages); |
5030 | *max_footprint_lifetime = (uint32_t)pages; | |
fe8ab488 A |
5031 | } |
5032 | if (purgeable_pages) { | |
39037602 A |
5033 | pages = (get_task_purgeable_size(task) / PAGE_SIZE_64); |
5034 | assert(((uint32_t)pages) == pages); | |
5035 | *purgeable_pages = (uint32_t)pages; | |
5036 | } | |
5037 | } | |
5038 | ||
5039 | static void | |
5040 | memorystatus_get_task_phys_footprint_page_counts(task_t task, | |
0a7de745 A |
5041 | uint64_t *internal_pages, uint64_t *internal_compressed_pages, |
5042 | uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages, | |
5043 | uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages, | |
5044 | uint64_t *iokit_mapped_pages, uint64_t *page_table_pages) | |
39037602 A |
5045 | { |
5046 | assert(task); | |
5047 | ||
5048 | if (internal_pages) { | |
5049 | *internal_pages = (get_task_internal(task) / PAGE_SIZE_64); | |
5050 | } | |
5051 | ||
5052 | if (internal_compressed_pages) { | |
5053 | *internal_compressed_pages = (get_task_internal_compressed(task) / PAGE_SIZE_64); | |
5054 | } | |
5055 | ||
5056 | if (purgeable_nonvolatile_pages) { | |
5057 | *purgeable_nonvolatile_pages = (get_task_purgeable_nonvolatile(task) / PAGE_SIZE_64); | |
5058 | } | |
5059 | ||
5060 | if (purgeable_nonvolatile_compressed_pages) { | |
5061 | *purgeable_nonvolatile_compressed_pages = (get_task_purgeable_nonvolatile_compressed(task) / PAGE_SIZE_64); | |
5062 | } | |
5063 | ||
5064 | if (alternate_accounting_pages) { | |
5065 | *alternate_accounting_pages = (get_task_alternate_accounting(task) / PAGE_SIZE_64); | |
5066 | } | |
5067 | ||
5068 | if (alternate_accounting_compressed_pages) { | |
5069 | *alternate_accounting_compressed_pages = (get_task_alternate_accounting_compressed(task) / PAGE_SIZE_64); | |
5070 | } | |
5071 | ||
5072 | if (iokit_mapped_pages) { | |
5073 | *iokit_mapped_pages = (get_task_iokit_mapped(task) / PAGE_SIZE_64); | |
5074 | } | |
5075 | ||
5076 | if (page_table_pages) { | |
5077 | *page_table_pages = (get_task_page_table(task) / PAGE_SIZE_64); | |
39236c6e | 5078 | } |
39236c6e A |
5079 | } |
5080 | ||
39037602 A |
5081 | /* |
5082 | * This routine only acts on the global jetsam event snapshot. | |
5083 | * Updating the process's entry can race when the memorystatus_thread | |
5084 | * has chosen to kill a process that is racing to exit on another core. | |
5085 | */ | |
39236c6e | 5086 | static void |
39037602 | 5087 | memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime) |
39236c6e | 5088 | { |
39037602 A |
5089 | memorystatus_jetsam_snapshot_entry_t *entry = NULL; |
5090 | memorystatus_jetsam_snapshot_t *snapshot = NULL; | |
5091 | memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL; | |
5092 | ||
39236c6e A |
5093 | unsigned int i; |
5094 | ||
d9a64523 A |
5095 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); |
5096 | ||
39037602 A |
5097 | if (memorystatus_jetsam_snapshot_count == 0) { |
5098 | /* | |
5099 | * No active snapshot. | |
5100 | * Nothing to do. | |
5101 | */ | |
5102 | return; | |
5103 | } | |
5104 | ||
5105 | /* | |
5106 | * Sanity check as this routine should only be called | |
5107 | * from a jetsam kill path. | |
5108 | */ | |
5109 | assert(kill_cause != 0 && killtime != 0); | |
5110 | ||
5111 | snapshot = memorystatus_jetsam_snapshot; | |
5112 | snapshot_list = memorystatus_jetsam_snapshot->entries; | |
5113 | ||
39236c6e | 5114 | for (i = 0; i < memorystatus_jetsam_snapshot_count; i++) { |
39037602 | 5115 | if (snapshot_list[i].pid == p->p_pid) { |
39037602 A |
5116 | entry = &snapshot_list[i]; |
5117 | ||
5118 | if (entry->killed || entry->jse_killtime) { | |
5119 | /* | |
5120 | * We apparently raced on the exit path | |
5121 | * for this process, as it's snapshot entry | |
5122 | * has already recorded a kill. | |
5123 | */ | |
5124 | assert(entry->killed && entry->jse_killtime); | |
5125 | break; | |
5126 | } | |
5127 | ||
5128 | /* | |
5129 | * Update the entry we just found in the snapshot. | |
5130 | */ | |
5131 | ||
5132 | entry->killed = kill_cause; | |
5133 | entry->jse_killtime = killtime; | |
5134 | entry->jse_gencount = snapshot->js_gencount; | |
5135 | entry->jse_idle_delta = p->p_memstat_idle_delta; | |
d9a64523 A |
5136 | #if CONFIG_FREEZE |
5137 | entry->jse_thaw_count = p->p_memstat_thaw_count; | |
5138 | #else /* CONFIG_FREEZE */ | |
5139 | entry->jse_thaw_count = 0; | |
5140 | #endif /* CONFIG_FREEZE */ | |
39037602 A |
5141 | |
5142 | /* | |
5143 | * If a process has moved between bands since snapshot was | |
5144 | * initialized, then likely these fields changed too. | |
5145 | */ | |
0a7de745 | 5146 | if (entry->priority != p->p_memstat_effectivepriority) { |
39037602 A |
5147 | strlcpy(entry->name, p->p_name, sizeof(entry->name)); |
5148 | entry->priority = p->p_memstat_effectivepriority; | |
5149 | entry->state = memorystatus_build_state(p); | |
5150 | entry->user_data = p->p_memstat_userdata; | |
5151 | entry->fds = p->p_fd->fd_nfiles; | |
0a7de745 | 5152 | } |
39037602 | 5153 | |
0a7de745 A |
5154 | /* |
5155 | * Always update the page counts on a kill. | |
5156 | */ | |
39037602 | 5157 | |
0a7de745 A |
5158 | uint32_t pages = 0; |
5159 | uint32_t max_pages_lifetime = 0; | |
5160 | uint32_t purgeable_pages = 0; | |
5161 | ||
5162 | memorystatus_get_task_page_counts(p->task, &pages, &max_pages_lifetime, &purgeable_pages); | |
5163 | entry->pages = (uint64_t)pages; | |
5164 | entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; | |
5165 | entry->purgeable_pages = (uint64_t)purgeable_pages; | |
5166 | ||
5167 | uint64_t internal_pages = 0; | |
5168 | uint64_t internal_compressed_pages = 0; | |
5169 | uint64_t purgeable_nonvolatile_pages = 0; | |
5170 | uint64_t purgeable_nonvolatile_compressed_pages = 0; | |
5171 | uint64_t alternate_accounting_pages = 0; | |
5172 | uint64_t alternate_accounting_compressed_pages = 0; | |
5173 | uint64_t iokit_mapped_pages = 0; | |
5174 | uint64_t page_table_pages = 0; | |
5175 | ||
5176 | memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, | |
5177 | &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, | |
5178 | &alternate_accounting_pages, &alternate_accounting_compressed_pages, | |
5179 | &iokit_mapped_pages, &page_table_pages); | |
5180 | ||
5181 | entry->jse_internal_pages = internal_pages; | |
5182 | entry->jse_internal_compressed_pages = internal_compressed_pages; | |
5183 | entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages; | |
5184 | entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages; | |
5185 | entry->jse_alternate_accounting_pages = alternate_accounting_pages; | |
5186 | entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages; | |
5187 | entry->jse_iokit_mapped_pages = iokit_mapped_pages; | |
5188 | entry->jse_page_table_pages = page_table_pages; | |
5189 | ||
5190 | uint64_t region_count = 0; | |
5191 | memorystatus_get_task_memory_region_count(p->task, ®ion_count); | |
5192 | entry->jse_memory_region_count = region_count; | |
39037602 | 5193 | |
0a7de745 | 5194 | goto exit; |
39037602 A |
5195 | } |
5196 | } | |
5197 | ||
5198 | if (entry == NULL) { | |
5199 | /* | |
5200 | * The entry was not found in the snapshot, so the process must have | |
5201 | * launched after the snapshot was initialized. | |
5202 | * Let's try to append the new entry. | |
5203 | */ | |
5204 | if (memorystatus_jetsam_snapshot_count < memorystatus_jetsam_snapshot_max) { | |
5205 | /* | |
5206 | * A populated snapshot buffer exists | |
5207 | * and there is room to init a new entry. | |
5208 | */ | |
5209 | assert(memorystatus_jetsam_snapshot_count == snapshot->entry_count); | |
5210 | ||
5211 | unsigned int next = memorystatus_jetsam_snapshot_count; | |
5212 | ||
0a7de745 | 5213 | if (memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[next], (snapshot->js_gencount)) == TRUE) { |
39037602 A |
5214 | entry = &snapshot_list[next]; |
5215 | entry->killed = kill_cause; | |
5216 | entry->jse_killtime = killtime; | |
5217 | ||
5218 | snapshot->entry_count = ++next; | |
5219 | memorystatus_jetsam_snapshot_count = next; | |
5220 | ||
5221 | if (memorystatus_jetsam_snapshot_count >= memorystatus_jetsam_snapshot_max) { | |
5222 | /* | |
5223 | * We just used the last slot in the snapshot buffer. | |
5224 | * We only want to log it once... so we do it here | |
5225 | * when we notice we've hit the max. | |
5226 | */ | |
5227 | printf("memorystatus: WARNING snapshot buffer is full, count %d\n", | |
0a7de745 | 5228 | memorystatus_jetsam_snapshot_count); |
39037602 | 5229 | } |
39236c6e | 5230 | } |
39236c6e A |
5231 | } |
5232 | } | |
39037602 A |
5233 | |
5234 | exit: | |
5235 | if (entry == NULL) { | |
5236 | /* | |
5237 | * If we reach here, the snapshot buffer could not be updated. | |
5238 | * Most likely, the buffer is full, in which case we would have | |
5239 | * logged a warning in the previous call. | |
5240 | * | |
5241 | * For now, we will stop appending snapshot entries. | |
5242 | * When the buffer is consumed, the snapshot state will reset. | |
5243 | */ | |
5244 | ||
5245 | MEMORYSTATUS_DEBUG(4, "memorystatus_update_jetsam_snapshot_entry_locked: failed to update pid %d, priority %d, count %d\n", | |
0a7de745 | 5246 | p->p_pid, p->p_memstat_effectivepriority, memorystatus_jetsam_snapshot_count); |
39037602 A |
5247 | } |
5248 | ||
5249 | return; | |
316670eb | 5250 | } |
b0d623f7 | 5251 | |
5ba3f43e | 5252 | #if CONFIG_JETSAM |
0a7de745 A |
5253 | void |
5254 | memorystatus_pages_update(unsigned int pages_avail) | |
39236c6e | 5255 | { |
fe8ab488 A |
5256 | memorystatus_available_pages = pages_avail; |
5257 | ||
5258 | #if VM_PRESSURE_EVENTS | |
5259 | /* | |
5260 | * Since memorystatus_available_pages changes, we should | |
0a7de745 | 5261 | * re-evaluate the pressure levels on the system and |
fe8ab488 A |
5262 | * check if we need to wake the pressure thread. |
5263 | * We also update memorystatus_level in that routine. | |
0a7de745 | 5264 | */ |
fe8ab488 A |
5265 | vm_pressure_response(); |
5266 | ||
5267 | if (memorystatus_available_pages <= memorystatus_available_pages_pressure) { | |
fe8ab488 A |
5268 | if (memorystatus_hwm_candidates || (memorystatus_available_pages <= memorystatus_available_pages_critical)) { |
5269 | memorystatus_thread_wake(); | |
5270 | } | |
5271 | } | |
d9a64523 A |
5272 | #if CONFIG_FREEZE |
5273 | /* | |
5274 | * We can't grab the freezer_mutex here even though that synchronization would be correct to inspect | |
5275 | * the # of frozen processes and wakeup the freezer thread. Reason being that we come here into this | |
5276 | * code with (possibly) the page-queue locks held and preemption disabled. So trying to grab a mutex here | |
5277 | * will result in the "mutex with preemption disabled" panic. | |
5278 | */ | |
5279 | ||
5280 | if (memorystatus_freeze_thread_should_run() == TRUE) { | |
5281 | /* | |
5282 | * The freezer thread is usually woken up by some user-space call i.e. pid_hibernate(any process). | |
0a7de745 | 5283 | * That trigger isn't invoked often enough and so we are enabling this explicit wakeup here. |
d9a64523 A |
5284 | */ |
5285 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { | |
5286 | thread_wakeup((event_t)&memorystatus_freeze_wakeup); | |
5287 | } | |
5288 | } | |
5289 | #endif /* CONFIG_FREEZE */ | |
5290 | ||
fe8ab488 A |
5291 | #else /* VM_PRESSURE_EVENTS */ |
5292 | ||
39236c6e | 5293 | boolean_t critical, delta; |
0a7de745 | 5294 | |
316670eb | 5295 | if (!memorystatus_delta) { |
0a7de745 | 5296 | return; |
316670eb | 5297 | } |
0a7de745 | 5298 | |
39236c6e | 5299 | critical = (pages_avail < memorystatus_available_pages_critical) ? TRUE : FALSE; |
0a7de745 A |
5300 | delta = ((pages_avail >= (memorystatus_available_pages + memorystatus_delta)) |
5301 | || (memorystatus_available_pages >= (pages_avail + memorystatus_delta))) ? TRUE : FALSE; | |
5302 | ||
39236c6e | 5303 | if (critical || delta) { |
39037602 A |
5304 | unsigned int total_pages; |
5305 | ||
5306 | total_pages = (unsigned int) atop_64(max_mem); | |
5307 | #if CONFIG_SECLUDED_MEMORY | |
5308 | total_pages -= vm_page_secluded_count; | |
5309 | #endif /* CONFIG_SECLUDED_MEMORY */ | |
5310 | memorystatus_level = memorystatus_available_pages * 100 / total_pages; | |
39236c6e | 5311 | memorystatus_thread_wake(); |
b0d623f7 | 5312 | } |
fe8ab488 | 5313 | #endif /* VM_PRESSURE_EVENTS */ |
316670eb | 5314 | } |
5ba3f43e | 5315 | #endif /* CONFIG_JETSAM */ |
316670eb A |
5316 | |
5317 | static boolean_t | |
39037602 | 5318 | memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount) |
0a7de745 | 5319 | { |
fe8ab488 A |
5320 | clock_sec_t tv_sec; |
5321 | clock_usec_t tv_usec; | |
39037602 | 5322 | uint32_t pages = 0; |
39037602 A |
5323 | uint32_t max_pages_lifetime = 0; |
5324 | uint32_t purgeable_pages = 0; | |
0a7de745 A |
5325 | uint64_t internal_pages = 0; |
5326 | uint64_t internal_compressed_pages = 0; | |
5327 | uint64_t purgeable_nonvolatile_pages = 0; | |
5328 | uint64_t purgeable_nonvolatile_compressed_pages = 0; | |
5329 | uint64_t alternate_accounting_pages = 0; | |
5330 | uint64_t alternate_accounting_compressed_pages = 0; | |
5331 | uint64_t iokit_mapped_pages = 0; | |
5332 | uint64_t page_table_pages = 0; | |
5333 | uint64_t region_count = 0; | |
39037602 | 5334 | uint64_t cids[COALITION_NUM_TYPES]; |
fe8ab488 | 5335 | |
39236c6e | 5336 | memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t)); |
39037602 | 5337 | |
316670eb | 5338 | entry->pid = p->p_pid; |
39037602 | 5339 | strlcpy(&entry->name[0], p->p_name, sizeof(entry->name)); |
39236c6e | 5340 | entry->priority = p->p_memstat_effectivepriority; |
39037602 | 5341 | |
d9a64523 | 5342 | memorystatus_get_task_page_counts(p->task, &pages, &max_pages_lifetime, &purgeable_pages); |
39037602 | 5343 | entry->pages = (uint64_t)pages; |
39037602 A |
5344 | entry->max_pages_lifetime = (uint64_t)max_pages_lifetime; |
5345 | entry->purgeable_pages = (uint64_t)purgeable_pages; | |
5346 | ||
5347 | memorystatus_get_task_phys_footprint_page_counts(p->task, &internal_pages, &internal_compressed_pages, | |
0a7de745 A |
5348 | &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages, |
5349 | &alternate_accounting_pages, &alternate_accounting_compressed_pages, | |
5350 | &iokit_mapped_pages, &page_table_pages); | |
39037602 A |
5351 | |
5352 | entry->jse_internal_pages = internal_pages; | |
5353 | entry->jse_internal_compressed_pages = internal_compressed_pages; | |
5354 | entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages; | |
5355 | entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages; | |
5356 | entry->jse_alternate_accounting_pages = alternate_accounting_pages; | |
5357 | entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages; | |
5358 | entry->jse_iokit_mapped_pages = iokit_mapped_pages; | |
5359 | entry->jse_page_table_pages = page_table_pages; | |
5360 | ||
5361 | memorystatus_get_task_memory_region_count(p->task, ®ion_count); | |
5362 | entry->jse_memory_region_count = region_count; | |
5363 | ||
5364 | entry->state = memorystatus_build_state(p); | |
39236c6e | 5365 | entry->user_data = p->p_memstat_userdata; |
316670eb | 5366 | memcpy(&entry->uuid[0], &p->p_uuid[0], sizeof(p->p_uuid)); |
39037602 | 5367 | entry->fds = p->p_fd->fd_nfiles; |
fe8ab488 A |
5368 | |
5369 | absolutetime_to_microtime(get_task_cpu_time(p->task), &tv_sec, &tv_usec); | |
d9a64523 A |
5370 | entry->cpu_time.tv_sec = (int64_t)tv_sec; |
5371 | entry->cpu_time.tv_usec = (int64_t)tv_usec; | |
316670eb | 5372 | |
39037602 | 5373 | assert(p->p_stats != NULL); |
0a7de745 A |
5374 | entry->jse_starttime = p->p_stats->ps_start; /* abstime process started */ |
5375 | entry->jse_killtime = 0; /* abstime jetsam chose to kill process */ | |
5376 | entry->killed = 0; /* the jetsam kill cause */ | |
5377 | entry->jse_gencount = gencount; /* indicates a pass through jetsam thread, when process was targeted to be killed */ | |
39037602 A |
5378 | |
5379 | entry->jse_idle_delta = p->p_memstat_idle_delta; /* Most recent timespan spent in idle-band */ | |
5380 | ||
d9a64523 A |
5381 | #if CONFIG_FREEZE |
5382 | entry->jse_thaw_count = p->p_memstat_thaw_count; | |
5383 | #else /* CONFIG_FREEZE */ | |
5384 | entry->jse_thaw_count = 0; | |
5385 | #endif /* CONFIG_FREEZE */ | |
5386 | ||
39037602 A |
5387 | proc_coalitionids(p, cids); |
5388 | entry->jse_coalition_jetsam_id = cids[COALITION_TYPE_JETSAM]; | |
5389 | ||
0a7de745 | 5390 | return TRUE; |
b0d623f7 A |
5391 | } |
5392 | ||
5393 | static void | |
3e170ce0 | 5394 | memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot) |
b0d623f7 | 5395 | { |
39236c6e | 5396 | kern_return_t kr = KERN_SUCCESS; |
0a7de745 A |
5397 | mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; |
5398 | vm_statistics64_data_t vm_stat; | |
39236c6e | 5399 | |
a39ff7e2 | 5400 | if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count)) != KERN_SUCCESS) { |
3e170ce0 A |
5401 | printf("memorystatus_init_jetsam_snapshot_stats: host_statistics64 failed with %d\n", kr); |
5402 | memset(&snapshot->stats, 0, sizeof(snapshot->stats)); | |
39236c6e | 5403 | } else { |
0a7de745 A |
5404 | snapshot->stats.free_pages = vm_stat.free_count; |
5405 | snapshot->stats.active_pages = vm_stat.active_count; | |
5406 | snapshot->stats.inactive_pages = vm_stat.inactive_count; | |
5407 | snapshot->stats.throttled_pages = vm_stat.throttled_count; | |
5408 | snapshot->stats.purgeable_pages = vm_stat.purgeable_count; | |
5409 | snapshot->stats.wired_pages = vm_stat.wire_count; | |
3e170ce0 A |
5410 | |
5411 | snapshot->stats.speculative_pages = vm_stat.speculative_count; | |
5412 | snapshot->stats.filebacked_pages = vm_stat.external_page_count; | |
5413 | snapshot->stats.anonymous_pages = vm_stat.internal_page_count; | |
5414 | snapshot->stats.compressions = vm_stat.compressions; | |
5415 | snapshot->stats.decompressions = vm_stat.decompressions; | |
5416 | snapshot->stats.compressor_pages = vm_stat.compressor_page_count; | |
5417 | snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor; | |
5418 | } | |
5ba3f43e A |
5419 | |
5420 | get_zone_map_size(&snapshot->stats.zone_map_size, &snapshot->stats.zone_map_capacity); | |
5421 | get_largest_zone_info(snapshot->stats.largest_zone_name, sizeof(snapshot->stats.largest_zone_name), | |
0a7de745 | 5422 | &snapshot->stats.largest_zone_size); |
3e170ce0 A |
5423 | } |
5424 | ||
5425 | /* | |
5426 | * Collect vm statistics at boot. | |
5427 | * Called only once (see kern_exec.c) | |
5428 | * Data can be consumed at any time. | |
5429 | */ | |
5430 | void | |
0a7de745 A |
5431 | memorystatus_init_at_boot_snapshot() |
5432 | { | |
3e170ce0 A |
5433 | memorystatus_init_snapshot_vmstats(&memorystatus_at_boot_snapshot); |
5434 | memorystatus_at_boot_snapshot.entry_count = 0; | |
5435 | memorystatus_at_boot_snapshot.notification_time = 0; /* updated when consumed */ | |
5436 | memorystatus_at_boot_snapshot.snapshot_time = mach_absolute_time(); | |
5437 | } | |
5438 | ||
5439 | static void | |
5440 | memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count ) | |
5441 | { | |
5442 | proc_t p, next_p; | |
5443 | unsigned int b = 0, i = 0; | |
5444 | ||
5445 | memorystatus_jetsam_snapshot_t *snapshot = NULL; | |
5446 | memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL; | |
5447 | unsigned int snapshot_max = 0; | |
5448 | ||
d9a64523 A |
5449 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); |
5450 | ||
3e170ce0 A |
5451 | if (od_snapshot) { |
5452 | /* | |
5453 | * This is an on_demand snapshot | |
5454 | */ | |
5455 | snapshot = od_snapshot; | |
5456 | snapshot_list = od_snapshot->entries; | |
5457 | snapshot_max = ods_list_count; | |
5458 | } else { | |
5459 | /* | |
5460 | * This is a jetsam event snapshot | |
5461 | */ | |
5462 | snapshot = memorystatus_jetsam_snapshot; | |
5463 | snapshot_list = memorystatus_jetsam_snapshot->entries; | |
5464 | snapshot_max = memorystatus_jetsam_snapshot_max; | |
39236c6e A |
5465 | } |
5466 | ||
39037602 A |
5467 | /* |
5468 | * Init the snapshot header information | |
5469 | */ | |
3e170ce0 | 5470 | memorystatus_init_snapshot_vmstats(snapshot); |
39037602 A |
5471 | snapshot->snapshot_time = mach_absolute_time(); |
5472 | snapshot->notification_time = 0; | |
5473 | snapshot->js_gencount = 0; | |
3e170ce0 | 5474 | |
39236c6e A |
5475 | next_p = memorystatus_get_first_proc_locked(&b, TRUE); |
5476 | while (next_p) { | |
5477 | p = next_p; | |
5478 | next_p = memorystatus_get_next_proc_locked(&b, p, TRUE); | |
0a7de745 | 5479 | |
39037602 | 5480 | if (FALSE == memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i], snapshot->js_gencount)) { |
316670eb A |
5481 | continue; |
5482 | } | |
0a7de745 | 5483 | |
3e170ce0 | 5484 | 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", |
0a7de745 A |
5485 | p->p_pid, |
5486 | 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], | |
5487 | 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]); | |
316670eb | 5488 | |
3e170ce0 | 5489 | if (++i == snapshot_max) { |
b0d623f7 | 5490 | break; |
0a7de745 | 5491 | } |
b0d623f7 | 5492 | } |
39236c6e | 5493 | |
3e170ce0 A |
5494 | snapshot->entry_count = i; |
5495 | ||
5496 | if (!od_snapshot) { | |
5497 | /* update the system buffer count */ | |
5498 | memorystatus_jetsam_snapshot_count = i; | |
5499 | } | |
b0d623f7 A |
5500 | } |
5501 | ||
39236c6e | 5502 | #if DEVELOPMENT || DEBUG |
b0d623f7 | 5503 | |
5ba3f43e | 5504 | #if CONFIG_JETSAM |
39236c6e | 5505 | static int |
0a7de745 A |
5506 | memorystatus_cmd_set_panic_bits(user_addr_t buffer, uint32_t buffer_size) |
5507 | { | |
39236c6e A |
5508 | int ret; |
5509 | memorystatus_jetsam_panic_options_t debug; | |
0a7de745 | 5510 | |
39236c6e A |
5511 | if (buffer_size != sizeof(memorystatus_jetsam_panic_options_t)) { |
5512 | return EINVAL; | |
b0d623f7 | 5513 | } |
39236c6e A |
5514 | |
5515 | ret = copyin(buffer, &debug, buffer_size); | |
5516 | if (ret) { | |
5517 | return ret; | |
5518 | } | |
0a7de745 | 5519 | |
39236c6e A |
5520 | /* Panic bits match kMemorystatusKilled* enum */ |
5521 | memorystatus_jetsam_panic_debug = (memorystatus_jetsam_panic_debug & ~debug.mask) | (debug.data & debug.mask); | |
0a7de745 | 5522 | |
39236c6e A |
5523 | /* Copyout new value */ |
5524 | debug.data = memorystatus_jetsam_panic_debug; | |
5525 | ret = copyout(&debug, buffer, sizeof(memorystatus_jetsam_panic_options_t)); | |
0a7de745 | 5526 | |
39236c6e | 5527 | return ret; |
b0d623f7 | 5528 | } |
5ba3f43e | 5529 | #endif /* CONFIG_JETSAM */ |
b0d623f7 | 5530 | |
3e170ce0 A |
5531 | /* |
5532 | * Triggers a sort_order on a specified jetsam priority band. | |
5533 | * This is for testing only, used to force a path through the sort | |
5534 | * function. | |
5535 | */ | |
5536 | static int | |
0a7de745 A |
5537 | memorystatus_cmd_test_jetsam_sort(int priority, int sort_order) |
5538 | { | |
3e170ce0 A |
5539 | int error = 0; |
5540 | ||
5541 | unsigned int bucket_index = 0; | |
5542 | ||
5543 | if (priority == -1) { | |
5544 | /* Use as shorthand for default priority */ | |
5545 | bucket_index = JETSAM_PRIORITY_DEFAULT; | |
5546 | } else { | |
5547 | bucket_index = (unsigned int)priority; | |
5548 | } | |
5549 | ||
5550 | error = memorystatus_sort_bucket(bucket_index, sort_order); | |
5551 | ||
0a7de745 | 5552 | return error; |
3e170ce0 A |
5553 | } |
5554 | ||
39037602 | 5555 | #endif /* DEVELOPMENT || DEBUG */ |
39236c6e | 5556 | |
a39ff7e2 A |
5557 | /* |
5558 | * Prepare the process to be killed (set state, update snapshot) and kill it. | |
5559 | */ | |
5560 | static uint64_t memorystatus_purge_before_jetsam_success = 0; | |
5561 | ||
5562 | static boolean_t | |
5563 | memorystatus_kill_proc(proc_t p, uint32_t cause, os_reason_t jetsam_reason, boolean_t *killed) | |
5564 | { | |
5565 | pid_t aPid = 0; | |
5566 | uint32_t aPid_ep = 0; | |
5567 | ||
0a7de745 A |
5568 | uint64_t killtime = 0; |
5569 | clock_sec_t tv_sec; | |
5570 | clock_usec_t tv_usec; | |
5571 | uint32_t tv_msec; | |
5572 | boolean_t retval = FALSE; | |
5573 | uint64_t num_pages_purged = 0; | |
a39ff7e2 A |
5574 | |
5575 | aPid = p->p_pid; | |
5576 | aPid_ep = p->p_memstat_effectivepriority; | |
5577 | ||
5578 | if (cause != kMemorystatusKilledVnodes && cause != kMemorystatusKilledZoneMapExhaustion) { | |
5579 | /* | |
5580 | * Genuine memory pressure and not other (vnode/zone) resource exhaustion. | |
5581 | */ | |
5582 | boolean_t success = FALSE; | |
5583 | ||
5584 | networking_memstatus_callout(p, cause); | |
5585 | num_pages_purged = vm_purgeable_purge_task_owned(p->task); | |
5586 | ||
5587 | if (num_pages_purged) { | |
5588 | /* | |
5589 | * We actually purged something and so let's | |
5590 | * check if we need to continue with the kill. | |
5591 | */ | |
5592 | if (cause == kMemorystatusKilledHiwat) { | |
5593 | uint64_t footprint_in_bytes = get_task_phys_footprint(p->task); | |
0a7de745 | 5594 | uint64_t memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ |
a39ff7e2 A |
5595 | success = (footprint_in_bytes <= memlimit_in_bytes); |
5596 | } else { | |
5597 | success = (memorystatus_avail_pages_below_pressure() == FALSE); | |
5598 | } | |
5599 | ||
5600 | if (success) { | |
a39ff7e2 A |
5601 | memorystatus_purge_before_jetsam_success++; |
5602 | ||
5603 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: purged %llu pages from pid %d [%s] and avoided %s\n", | |
0a7de745 | 5604 | num_pages_purged, aPid, (*p->p_name ? p->p_name : "unknown"), memorystatus_kill_cause_name[cause]); |
a39ff7e2 A |
5605 | |
5606 | *killed = FALSE; | |
5607 | ||
5608 | return TRUE; | |
5609 | } | |
5610 | } | |
5611 | } | |
5612 | ||
5613 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) | |
5614 | MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %lld Mb > 1 (%d Mb)\n", | |
0a7de745 A |
5615 | (memorystatus_jetsam_policy & kPolicyDiagnoseActive) ? "suspending": "killing", |
5616 | aPid, (*p->p_name ? p->p_name : "unknown"), | |
5617 | (footprint_in_bytes / (1024ULL * 1024ULL)), /* converted bytes to MB */ | |
5618 | p->p_memstat_memlimit); | |
a39ff7e2 A |
5619 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ |
5620 | ||
5621 | killtime = mach_absolute_time(); | |
5622 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
5623 | tv_msec = tv_usec / 1000; | |
5624 | ||
5625 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) | |
5626 | if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) { | |
5627 | if (cause == kMemorystatusKilledHiwat) { | |
5628 | MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] for diagnosis - memorystatus_available_pages: %d\n", | |
0a7de745 | 5629 | aPid, (*p->p_name ? p->p_name: "(unknown)"), memorystatus_available_pages); |
a39ff7e2 A |
5630 | } else { |
5631 | int activeProcess = p->p_memstat_state & P_MEMSTAT_FOREGROUND; | |
5632 | if (activeProcess) { | |
5633 | MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memorystatus_available_pages: %d\n", | |
0a7de745 | 5634 | aPid, (*p->p_name ? p->p_name: "(unknown)"), memorystatus_available_pages); |
a39ff7e2 | 5635 | |
0a7de745 A |
5636 | if (memorystatus_jetsam_policy & kPolicyDiagnoseFirst) { |
5637 | jetsam_diagnostic_suspended_one_active_proc = 1; | |
5638 | printf("jetsam: returning after suspending first active proc - %d\n", aPid); | |
5639 | } | |
a39ff7e2 A |
5640 | } |
5641 | } | |
5642 | ||
d9a64523 A |
5643 | proc_list_lock(); |
5644 | /* This diagnostic code is going away soon. Ignore the kMemorystatusInvalid cause here. */ | |
5645 | memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusInvalid, killtime); | |
5646 | proc_list_unlock(); | |
5647 | ||
a39ff7e2 A |
5648 | p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED; |
5649 | ||
5650 | if (p) { | |
5651 | task_suspend(p->task); | |
5652 | *killed = TRUE; | |
5653 | } | |
5654 | } else | |
5655 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ | |
5656 | { | |
d9a64523 | 5657 | proc_list_lock(); |
a39ff7e2 | 5658 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); |
d9a64523 | 5659 | proc_list_unlock(); |
a39ff7e2 A |
5660 | |
5661 | char kill_reason_string[128]; | |
5662 | ||
5663 | if (cause == kMemorystatusKilledHiwat) { | |
5664 | strlcpy(kill_reason_string, "killing_highwater_process", 128); | |
5665 | } else { | |
5666 | if (aPid_ep == JETSAM_PRIORITY_IDLE) { | |
5667 | strlcpy(kill_reason_string, "killing_idle_process", 128); | |
5668 | } else { | |
5669 | strlcpy(kill_reason_string, "killing_top_process", 128); | |
5670 | } | |
5671 | } | |
5672 | ||
5673 | os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: %s pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", | |
0a7de745 A |
5674 | (unsigned long)tv_sec, tv_msec, kill_reason_string, |
5675 | aPid, (*p->p_name ? p->p_name : "unknown"), | |
5676 | memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)memorystatus_available_pages); | |
a39ff7e2 A |
5677 | |
5678 | /* | |
5679 | * memorystatus_do_kill drops a reference, so take another one so we can | |
5680 | * continue to use this exit reason even after memorystatus_do_kill() | |
5681 | * returns | |
5682 | */ | |
5683 | os_reason_ref(jetsam_reason); | |
5684 | ||
5685 | retval = memorystatus_do_kill(p, cause, jetsam_reason); | |
5686 | ||
5687 | *killed = retval; | |
5688 | } | |
5689 | ||
5690 | return retval; | |
5691 | } | |
5692 | ||
39236c6e A |
5693 | /* |
5694 | * Jetsam the first process in the queue. | |
5695 | */ | |
5696 | static boolean_t | |
39037602 | 5697 | memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, os_reason_t jetsam_reason, |
0a7de745 | 5698 | int32_t *priority, uint32_t *errors) |
39236c6e A |
5699 | { |
5700 | pid_t aPid; | |
5701 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
a39ff7e2 | 5702 | boolean_t new_snapshot = FALSE, force_new_snapshot = FALSE, killed = FALSE, freed_mem = FALSE; |
39236c6e | 5703 | unsigned int i = 0; |
3e170ce0 | 5704 | uint32_t aPid_ep; |
0a7de745 | 5705 | int32_t local_max_kill_prio = JETSAM_PRIORITY_IDLE; |
b0d623f7 | 5706 | |
6d2010ae A |
5707 | #ifndef CONFIG_FREEZE |
5708 | #pragma unused(any) | |
5709 | #endif | |
0a7de745 | 5710 | |
39236c6e | 5711 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, |
0a7de745 | 5712 | memorystatus_available_pages, 0, 0, 0, 0); |
6d2010ae | 5713 | |
316670eb | 5714 | |
5ba3f43e A |
5715 | #if CONFIG_JETSAM |
5716 | if (sort_flag == TRUE) { | |
5717 | (void)memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT); | |
5718 | } | |
5719 | ||
5720 | local_max_kill_prio = max_kill_priority; | |
5721 | ||
5722 | force_new_snapshot = FALSE; | |
5723 | ||
5724 | #else /* CONFIG_JETSAM */ | |
5725 | ||
3e170ce0 | 5726 | if (sort_flag == TRUE) { |
5ba3f43e A |
5727 | (void)memorystatus_sort_bucket(JETSAM_PRIORITY_IDLE, JETSAM_SORT_DEFAULT); |
5728 | } | |
5729 | ||
5730 | /* | |
5731 | * On macos, we currently only have 2 reasons to be here: | |
5732 | * | |
5733 | * kMemorystatusKilledZoneMapExhaustion | |
5734 | * AND | |
d9a64523 | 5735 | * kMemorystatusKilledVMCompressorSpaceShortage |
5ba3f43e A |
5736 | * |
5737 | * If we are here because of kMemorystatusKilledZoneMapExhaustion, we will consider | |
5738 | * any and all processes as eligible kill candidates since we need to avoid a panic. | |
5739 | * | |
5740 | * Since this function can be called async. it is harder to toggle the max_kill_priority | |
5741 | * value before and after a call. And so we use this local variable to set the upper band | |
5742 | * on the eligible kill bands. | |
5743 | */ | |
5744 | if (cause == kMemorystatusKilledZoneMapExhaustion) { | |
5745 | local_max_kill_prio = JETSAM_PRIORITY_MAX; | |
5746 | } else { | |
5747 | local_max_kill_prio = max_kill_priority; | |
3e170ce0 A |
5748 | } |
5749 | ||
5ba3f43e A |
5750 | /* |
5751 | * And, because we are here under extreme circumstances, we force a snapshot even for | |
5752 | * IDLE kills. | |
5753 | */ | |
5754 | force_new_snapshot = TRUE; | |
5755 | ||
5756 | #endif /* CONFIG_JETSAM */ | |
5757 | ||
3e170ce0 | 5758 | proc_list_lock(); |
fe8ab488 | 5759 | |
39236c6e | 5760 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
5ba3f43e | 5761 | while (next_p && (next_p->p_memstat_effectivepriority <= local_max_kill_prio)) { |
316670eb | 5762 | #if DEVELOPMENT || DEBUG |
316670eb A |
5763 | int procSuspendedForDiagnosis; |
5764 | #endif /* DEVELOPMENT || DEBUG */ | |
0a7de745 | 5765 | |
39236c6e A |
5766 | p = next_p; |
5767 | next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
0a7de745 | 5768 | |
6d2010ae | 5769 | #if DEVELOPMENT || DEBUG |
39236c6e | 5770 | procSuspendedForDiagnosis = p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED; |
6d2010ae | 5771 | #endif /* DEVELOPMENT || DEBUG */ |
0a7de745 | 5772 | |
39236c6e | 5773 | aPid = p->p_pid; |
3e170ce0 | 5774 | aPid_ep = p->p_memstat_effectivepriority; |
316670eb | 5775 | |
39236c6e | 5776 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { |
39037602 | 5777 | continue; /* with lock held */ |
b0d623f7 | 5778 | } |
0a7de745 | 5779 | |
5ba3f43e | 5780 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
39236c6e A |
5781 | if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && procSuspendedForDiagnosis) { |
5782 | printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid); | |
5783 | continue; | |
5784 | } | |
5ba3f43e | 5785 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ |
316670eb | 5786 | |
0a7de745 | 5787 | if (cause == kMemorystatusKilledVnodes) { |
fe8ab488 A |
5788 | /* |
5789 | * If the system runs out of vnodes, we systematically jetsam | |
5790 | * processes in hopes of stumbling onto a vnode gain that helps | |
5791 | * the system recover. The process that happens to trigger | |
5ba3f43e A |
5792 | * this path has no known relationship to the vnode shortage. |
5793 | * Deadlock avoidance: attempt to safeguard the caller. | |
fe8ab488 A |
5794 | */ |
5795 | ||
5796 | if (p == current_proc()) { | |
5797 | /* do not jetsam the current process */ | |
5798 | continue; | |
5799 | } | |
5800 | } | |
5801 | ||
6d2010ae | 5802 | #if CONFIG_FREEZE |
39236c6e | 5803 | boolean_t skip; |
d9a64523 | 5804 | boolean_t reclaim_proc = !(p->p_memstat_state & P_MEMSTAT_LOCKED); |
39236c6e A |
5805 | if (any || reclaim_proc) { |
5806 | skip = FALSE; | |
5807 | } else { | |
5808 | skip = TRUE; | |
5809 | } | |
0a7de745 | 5810 | |
39236c6e A |
5811 | if (skip) { |
5812 | continue; | |
5813 | } else | |
6d2010ae | 5814 | #endif |
39236c6e | 5815 | { |
a39ff7e2 A |
5816 | if (proc_ref_locked(p) == p) { |
5817 | /* | |
5818 | * Mark as terminated so that if exit1() indicates success, but the process (for example) | |
5819 | * is blocked in task_exception_notify(), it'll be skipped if encountered again - see | |
5820 | * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the | |
5821 | * acquisition of the proc lock. | |
5822 | */ | |
5823 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
a39ff7e2 A |
5824 | } else { |
5825 | /* | |
5826 | * We need to restart the search again because | |
5827 | * proc_ref_locked _can_ drop the proc_list lock | |
5828 | * and we could have lost our stored next_p via | |
5829 | * an exit() on another core. | |
5830 | */ | |
5831 | i = 0; | |
5832 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
5833 | continue; | |
5834 | } | |
5835 | ||
0a7de745 A |
5836 | /* |
5837 | * Capture a snapshot if none exists and: | |
5ba3f43e | 5838 | * - we are forcing a new snapshot creation, either because: |
0a7de745 | 5839 | * - on a particular platform we need these snapshots every time, OR |
5ba3f43e | 5840 | * - a boot-arg/embedded device tree property has been set. |
0a7de745 A |
5841 | * - priority was not requested (this is something other than an ambient kill) |
5842 | * - the priority was requested *and* the targeted process is not at idle priority | |
5843 | */ | |
5844 | if ((memorystatus_jetsam_snapshot_count == 0) && | |
5ba3f43e | 5845 | (force_new_snapshot || memorystatus_idle_snapshot || ((!priority) || (priority && (aPid_ep != JETSAM_PRIORITY_IDLE))))) { |
0a7de745 A |
5846 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); |
5847 | new_snapshot = TRUE; | |
5848 | } | |
39037602 | 5849 | |
d9a64523 A |
5850 | proc_list_unlock(); |
5851 | ||
a39ff7e2 A |
5852 | freed_mem = memorystatus_kill_proc(p, cause, jetsam_reason, &killed); /* purged and/or killed 'p' */ |
5853 | /* Success? */ | |
5854 | if (freed_mem) { | |
5855 | if (killed) { | |
3e170ce0 A |
5856 | if (priority) { |
5857 | *priority = aPid_ep; | |
5858 | } | |
a39ff7e2 A |
5859 | } else { |
5860 | /* purged */ | |
3e170ce0 | 5861 | proc_list_lock(); |
3e170ce0 | 5862 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; |
a39ff7e2 | 5863 | proc_list_unlock(); |
6d2010ae | 5864 | } |
a39ff7e2 A |
5865 | proc_rele(p); |
5866 | goto exit; | |
6d2010ae | 5867 | } |
0a7de745 | 5868 | |
a39ff7e2 A |
5869 | /* |
5870 | * Failure - first unwind the state, | |
5871 | * then fall through to restart the search. | |
5872 | */ | |
5873 | proc_list_lock(); | |
5874 | proc_rele_locked(p); | |
5875 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
5876 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
5877 | *errors += 1; | |
5878 | ||
5879 | i = 0; | |
5880 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
b0d623f7 | 5881 | } |
b0d623f7 | 5882 | } |
0a7de745 | 5883 | |
39236c6e | 5884 | proc_list_unlock(); |
0a7de745 | 5885 | |
39236c6e | 5886 | exit: |
39037602 A |
5887 | os_reason_free(jetsam_reason); |
5888 | ||
39236c6e A |
5889 | /* Clear snapshot if freshly captured and no target was found */ |
5890 | if (new_snapshot && !killed) { | |
39037602 A |
5891 | proc_list_lock(); |
5892 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
5893 | proc_list_unlock(); | |
316670eb | 5894 | } |
0a7de745 | 5895 | |
39236c6e | 5896 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, |
0a7de745 | 5897 | memorystatus_available_pages, killed ? aPid : 0, 0, 0, 0); |
b0d623f7 | 5898 | |
39236c6e | 5899 | return killed; |
316670eb A |
5900 | } |
5901 | ||
3e170ce0 | 5902 | /* |
0a7de745 | 5903 | * Jetsam aggressively |
3e170ce0 | 5904 | */ |
39236c6e | 5905 | static boolean_t |
5ba3f43e | 5906 | memorystatus_kill_top_process_aggressive(uint32_t cause, int aggr_count, |
0a7de745 | 5907 | int32_t priority_max, uint32_t *errors) |
d1ecb069 | 5908 | { |
3e170ce0 | 5909 | pid_t aPid; |
39236c6e A |
5910 | proc_t p = PROC_NULL, next_p = PROC_NULL; |
5911 | boolean_t new_snapshot = FALSE, killed = FALSE; | |
3e170ce0 | 5912 | int kill_count = 0; |
39236c6e | 5913 | unsigned int i = 0; |
3e170ce0 | 5914 | int32_t aPid_ep = 0; |
490019cf | 5915 | unsigned int memorystatus_level_snapshot = 0; |
39037602 | 5916 | uint64_t killtime = 0; |
0a7de745 A |
5917 | clock_sec_t tv_sec; |
5918 | clock_usec_t tv_usec; | |
5919 | uint32_t tv_msec; | |
5ba3f43e | 5920 | os_reason_t jetsam_reason = OS_REASON_NULL; |
3e170ce0 A |
5921 | |
5922 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, | |
0a7de745 | 5923 | memorystatus_available_pages, priority_max, 0, 0, 0); |
3e170ce0 | 5924 | |
490019cf A |
5925 | memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT); |
5926 | ||
5ba3f43e A |
5927 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, cause); |
5928 | if (jetsam_reason == OS_REASON_NULL) { | |
5929 | printf("memorystatus_kill_top_process_aggressive: failed to allocate exit reason\n"); | |
5930 | } | |
5931 | ||
39236c6e | 5932 | proc_list_lock(); |
3e170ce0 | 5933 | |
39236c6e A |
5934 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
5935 | while (next_p) { | |
3e170ce0 A |
5936 | #if DEVELOPMENT || DEBUG |
5937 | int activeProcess; | |
5938 | int procSuspendedForDiagnosis; | |
5939 | #endif /* DEVELOPMENT || DEBUG */ | |
39236c6e | 5940 | |
5ba3f43e A |
5941 | if (((next_p->p_listflag & P_LIST_EXITED) != 0) || |
5942 | ((unsigned int)(next_p->p_memstat_effectivepriority) != i)) { | |
3e170ce0 | 5943 | /* |
5ba3f43e A |
5944 | * We have raced with next_p running on another core. |
5945 | * It may be exiting or it may have moved to a different | |
5946 | * jetsam priority band. This means we have lost our | |
5947 | * place in line while traversing the jetsam list. We | |
3e170ce0 A |
5948 | * attempt to recover by rewinding to the beginning of the band |
5949 | * we were already traversing. By doing this, we do not guarantee | |
5950 | * that no process escapes this aggressive march, but we can make | |
5951 | * skipping an entire range of processes less likely. (PR-21069019) | |
5952 | */ | |
5953 | ||
5ba3f43e | 5954 | MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: rewinding band %d, %s(%d) moved or exiting.\n", |
0a7de745 | 5955 | aggr_count, i, (*next_p->p_name ? next_p->p_name : "unknown"), next_p->p_pid); |
3e170ce0 A |
5956 | |
5957 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
5958 | continue; | |
5959 | } | |
5960 | ||
5961 | p = next_p; | |
5962 | next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
5963 | ||
5964 | if (p->p_memstat_effectivepriority > priority_max) { | |
0a7de745 | 5965 | /* |
3e170ce0 A |
5966 | * Bail out of this killing spree if we have |
5967 | * reached beyond the priority_max jetsam band. | |
0a7de745 | 5968 | * That is, we kill up to and through the |
3e170ce0 A |
5969 | * priority_max jetsam band. |
5970 | */ | |
5971 | proc_list_unlock(); | |
5972 | goto exit; | |
5973 | } | |
0a7de745 | 5974 | |
3e170ce0 A |
5975 | #if DEVELOPMENT || DEBUG |
5976 | activeProcess = p->p_memstat_state & P_MEMSTAT_FOREGROUND; | |
5977 | procSuspendedForDiagnosis = p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED; | |
5978 | #endif /* DEVELOPMENT || DEBUG */ | |
0a7de745 | 5979 | |
3e170ce0 A |
5980 | aPid = p->p_pid; |
5981 | aPid_ep = p->p_memstat_effectivepriority; | |
5982 | ||
5983 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { | |
5984 | continue; | |
5985 | } | |
0a7de745 | 5986 | |
5ba3f43e | 5987 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
3e170ce0 A |
5988 | if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && procSuspendedForDiagnosis) { |
5989 | printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid); | |
5990 | continue; | |
5991 | } | |
5ba3f43e | 5992 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ |
3e170ce0 A |
5993 | |
5994 | /* | |
5995 | * Capture a snapshot if none exists. | |
5996 | */ | |
5997 | if (memorystatus_jetsam_snapshot_count == 0) { | |
0a7de745 | 5998 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); |
3e170ce0 A |
5999 | new_snapshot = TRUE; |
6000 | } | |
0a7de745 A |
6001 | |
6002 | /* | |
3e170ce0 | 6003 | * Mark as terminated so that if exit1() indicates success, but the process (for example) |
0a7de745 A |
6004 | * is blocked in task_exception_notify(), it'll be skipped if encountered again - see |
6005 | * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the | |
3e170ce0 A |
6006 | * acquisition of the proc lock. |
6007 | */ | |
6008 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
39037602 A |
6009 | |
6010 | killtime = mach_absolute_time(); | |
6011 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
6012 | tv_msec = tv_usec / 1000; | |
0a7de745 | 6013 | |
3e170ce0 | 6014 | /* Shift queue, update stats */ |
39037602 | 6015 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); |
3e170ce0 A |
6016 | |
6017 | /* | |
6018 | * In order to kill the target process, we will drop the proc_list_lock. | |
6019 | * To guaranteee that p and next_p don't disappear out from under the lock, | |
6020 | * we must take a ref on both. | |
6021 | * If we cannot get a reference, then it's likely we've raced with | |
6022 | * that process exiting on another core. | |
6023 | */ | |
6024 | if (proc_ref_locked(p) == p) { | |
6025 | if (next_p) { | |
6026 | while (next_p && (proc_ref_locked(next_p) != next_p)) { | |
6027 | proc_t temp_p; | |
6028 | ||
0a7de745 A |
6029 | /* |
6030 | * We must have raced with next_p exiting on another core. | |
6031 | * Recover by getting the next eligible process in the band. | |
6032 | */ | |
3e170ce0 A |
6033 | |
6034 | MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: skipping %d [%s] (exiting?)\n", | |
0a7de745 | 6035 | aggr_count, next_p->p_pid, (*next_p->p_name ? next_p->p_name : "(unknown)")); |
3e170ce0 A |
6036 | |
6037 | temp_p = next_p; | |
6038 | next_p = memorystatus_get_next_proc_locked(&i, temp_p, TRUE); | |
0a7de745 | 6039 | } |
3e170ce0 A |
6040 | } |
6041 | proc_list_unlock(); | |
6042 | ||
5ba3f43e | 6043 | printf("%lu.%03d memorystatus: %s%d pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", |
0a7de745 A |
6044 | (unsigned long)tv_sec, tv_msec, |
6045 | ((aPid_ep == JETSAM_PRIORITY_IDLE) ? "killing_idle_process_aggressive" : "killing_top_process_aggressive"), | |
6046 | aggr_count, aPid, (*p->p_name ? p->p_name : "unknown"), | |
6047 | memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)memorystatus_available_pages); | |
3e170ce0 | 6048 | |
490019cf A |
6049 | memorystatus_level_snapshot = memorystatus_level; |
6050 | ||
39037602 A |
6051 | /* |
6052 | * memorystatus_do_kill() drops a reference, so take another one so we can | |
6053 | * continue to use this exit reason even after memorystatus_do_kill() | |
6054 | * returns. | |
6055 | */ | |
6056 | os_reason_ref(jetsam_reason); | |
6057 | killed = memorystatus_do_kill(p, cause, jetsam_reason); | |
6058 | ||
3e170ce0 A |
6059 | /* Success? */ |
6060 | if (killed) { | |
6061 | proc_rele(p); | |
6062 | kill_count++; | |
6063 | p = NULL; | |
6064 | killed = FALSE; | |
6065 | ||
0a7de745 | 6066 | /* |
3e170ce0 A |
6067 | * Continue the killing spree. |
6068 | */ | |
6069 | proc_list_lock(); | |
6070 | if (next_p) { | |
6071 | proc_rele_locked(next_p); | |
6072 | } | |
490019cf A |
6073 | |
6074 | if (aPid_ep == JETSAM_PRIORITY_FOREGROUND && memorystatus_aggressive_jetsam_lenient == TRUE) { | |
6075 | if (memorystatus_level > memorystatus_level_snapshot && ((memorystatus_level - memorystatus_level_snapshot) >= AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD)) { | |
6076 | #if DEVELOPMENT || DEBUG | |
6077 | printf("Disabling Lenient mode after one-time deployment.\n"); | |
6078 | #endif /* DEVELOPMENT || DEBUG */ | |
6079 | memorystatus_aggressive_jetsam_lenient = FALSE; | |
6080 | break; | |
6081 | } | |
6082 | } | |
6083 | ||
3e170ce0 A |
6084 | continue; |
6085 | } | |
0a7de745 | 6086 | |
3e170ce0 A |
6087 | /* |
6088 | * Failure - first unwind the state, | |
6089 | * then fall through to restart the search. | |
6090 | */ | |
6091 | proc_list_lock(); | |
6092 | proc_rele_locked(p); | |
6093 | if (next_p) { | |
6094 | proc_rele_locked(next_p); | |
6095 | } | |
6096 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
6097 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
6098 | *errors += 1; | |
6099 | p = NULL; | |
6100 | } | |
6101 | ||
6102 | /* | |
6103 | * Failure - restart the search at the beginning of | |
6104 | * the band we were already traversing. | |
6105 | * | |
6106 | * We might have raced with "p" exiting on another core, resulting in no | |
6107 | * ref on "p". Or, we may have failed to kill "p". | |
6108 | * | |
0a7de745 | 6109 | * Either way, we fall thru to here, leaving the proc in the |
3e170ce0 A |
6110 | * P_MEMSTAT_TERMINATED or P_MEMSTAT_ERROR state. |
6111 | * | |
6112 | * And, we hold the the proc_list_lock at this point. | |
6113 | */ | |
6114 | ||
6115 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
6116 | } | |
0a7de745 | 6117 | |
3e170ce0 | 6118 | proc_list_unlock(); |
0a7de745 | 6119 | |
3e170ce0 | 6120 | exit: |
39037602 A |
6121 | os_reason_free(jetsam_reason); |
6122 | ||
3e170ce0 A |
6123 | /* Clear snapshot if freshly captured and no target was found */ |
6124 | if (new_snapshot && (kill_count == 0)) { | |
0a7de745 A |
6125 | proc_list_lock(); |
6126 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
6127 | proc_list_unlock(); | |
3e170ce0 | 6128 | } |
0a7de745 | 6129 | |
3e170ce0 | 6130 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, |
0a7de745 | 6131 | memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); |
3e170ce0 A |
6132 | |
6133 | if (kill_count > 0) { | |
0a7de745 A |
6134 | return TRUE; |
6135 | } else { | |
6136 | return FALSE; | |
3e170ce0 A |
6137 | } |
6138 | } | |
6139 | ||
3e170ce0 | 6140 | static boolean_t |
a39ff7e2 | 6141 | memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged) |
3e170ce0 A |
6142 | { |
6143 | pid_t aPid = 0; | |
6144 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
a39ff7e2 | 6145 | boolean_t new_snapshot = FALSE, killed = FALSE, freed_mem = FALSE; |
3e170ce0 A |
6146 | unsigned int i = 0; |
6147 | uint32_t aPid_ep; | |
39037602 | 6148 | os_reason_t jetsam_reason = OS_REASON_NULL; |
3e170ce0 | 6149 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_START, |
0a7de745 A |
6150 | memorystatus_available_pages, 0, 0, 0, 0); |
6151 | ||
39037602 A |
6152 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_HIGHWATER); |
6153 | if (jetsam_reason == OS_REASON_NULL) { | |
6154 | printf("memorystatus_kill_hiwat_proc: failed to allocate exit reason\n"); | |
6155 | } | |
6156 | ||
3e170ce0 | 6157 | proc_list_lock(); |
0a7de745 | 6158 | |
3e170ce0 A |
6159 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); |
6160 | while (next_p) { | |
39037602 A |
6161 | uint64_t footprint_in_bytes = 0; |
6162 | uint64_t memlimit_in_bytes = 0; | |
6163 | boolean_t skip = 0; | |
3e170ce0 A |
6164 | |
6165 | p = next_p; | |
6166 | next_p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
0a7de745 | 6167 | |
39236c6e | 6168 | aPid = p->p_pid; |
3e170ce0 | 6169 | aPid_ep = p->p_memstat_effectivepriority; |
0a7de745 | 6170 | |
39236c6e A |
6171 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { |
6172 | continue; | |
6173 | } | |
0a7de745 | 6174 | |
39236c6e A |
6175 | /* skip if no limit set */ |
6176 | if (p->p_memstat_memlimit <= 0) { | |
6177 | continue; | |
d1ecb069 | 6178 | } |
3e170ce0 | 6179 | |
39037602 | 6180 | footprint_in_bytes = get_task_phys_footprint(p->task); |
0a7de745 | 6181 | memlimit_in_bytes = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL); /* convert MB to bytes */ |
39037602 | 6182 | skip = (footprint_in_bytes <= memlimit_in_bytes); |
3e170ce0 | 6183 | |
5ba3f43e | 6184 | #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG) |
39236c6e A |
6185 | if (!skip && (memorystatus_jetsam_policy & kPolicyDiagnoseActive)) { |
6186 | if (p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED) { | |
6187 | continue; | |
6d2010ae | 6188 | } |
39236c6e | 6189 | } |
5ba3f43e | 6190 | #endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */ |
316670eb | 6191 | |
6d2010ae | 6192 | #if CONFIG_FREEZE |
39236c6e A |
6193 | if (!skip) { |
6194 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { | |
6195 | skip = TRUE; | |
6196 | } else { | |
6197 | skip = FALSE; | |
0a7de745 | 6198 | } |
39236c6e | 6199 | } |
6d2010ae | 6200 | #endif |
316670eb | 6201 | |
39236c6e A |
6202 | if (skip) { |
6203 | continue; | |
6204 | } else { | |
39236c6e | 6205 | if (memorystatus_jetsam_snapshot_count == 0) { |
0a7de745 | 6206 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); |
a39ff7e2 A |
6207 | new_snapshot = TRUE; |
6208 | } | |
0a7de745 | 6209 | |
a39ff7e2 A |
6210 | if (proc_ref_locked(p) == p) { |
6211 | /* | |
6212 | * Mark as terminated so that if exit1() indicates success, but the process (for example) | |
6213 | * is blocked in task_exception_notify(), it'll be skipped if encountered again - see | |
6214 | * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the | |
6215 | * acquisition of the proc lock. | |
6216 | */ | |
6217 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
39037602 | 6218 | |
39236c6e | 6219 | proc_list_unlock(); |
a39ff7e2 A |
6220 | } else { |
6221 | /* | |
6222 | * We need to restart the search again because | |
6223 | * proc_ref_locked _can_ drop the proc_list lock | |
6224 | * and we could have lost our stored next_p via | |
6225 | * an exit() on another core. | |
6226 | */ | |
6227 | i = 0; | |
6228 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
6229 | continue; | |
6230 | } | |
0a7de745 | 6231 | |
a39ff7e2 | 6232 | freed_mem = memorystatus_kill_proc(p, kMemorystatusKilledHiwat, jetsam_reason, &killed); /* purged and/or killed 'p' */ |
3e170ce0 | 6233 | |
a39ff7e2 A |
6234 | /* Success? */ |
6235 | if (freed_mem) { | |
6236 | if (killed == FALSE) { | |
6237 | /* purged 'p'..don't reset HWM candidate count */ | |
6238 | *purged = TRUE; | |
3e170ce0 | 6239 | |
3e170ce0 | 6240 | proc_list_lock(); |
3e170ce0 | 6241 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; |
a39ff7e2 | 6242 | proc_list_unlock(); |
6d2010ae | 6243 | } |
a39ff7e2 A |
6244 | proc_rele(p); |
6245 | goto exit; | |
39236c6e | 6246 | } |
a39ff7e2 A |
6247 | /* |
6248 | * Failure - first unwind the state, | |
6249 | * then fall through to restart the search. | |
6250 | */ | |
6251 | proc_list_lock(); | |
6252 | proc_rele_locked(p); | |
6253 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
6254 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
6255 | *errors += 1; | |
6256 | ||
6257 | i = 0; | |
6258 | next_p = memorystatus_get_first_proc_locked(&i, TRUE); | |
6d2010ae A |
6259 | } |
6260 | } | |
0a7de745 | 6261 | |
39236c6e | 6262 | proc_list_unlock(); |
0a7de745 | 6263 | |
39236c6e | 6264 | exit: |
39037602 A |
6265 | os_reason_free(jetsam_reason); |
6266 | ||
6267 | /* Clear snapshot if freshly captured and no target was found */ | |
6268 | if (new_snapshot && !killed) { | |
6269 | proc_list_lock(); | |
6270 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; | |
6271 | proc_list_unlock(); | |
6272 | } | |
0a7de745 A |
6273 | |
6274 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END, | |
6275 | memorystatus_available_pages, killed ? aPid : 0, 0, 0, 0); | |
39037602 A |
6276 | |
6277 | return killed; | |
6278 | } | |
6279 | ||
6280 | /* | |
6281 | * Jetsam a process pinned in the elevated band. | |
6282 | * | |
6283 | * Return: true -- at least one pinned process was jetsammed | |
6284 | * false -- no pinned process was jetsammed | |
6285 | */ | |
6286 | static boolean_t | |
d9a64523 | 6287 | memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, uint32_t *errors) |
39037602 A |
6288 | { |
6289 | pid_t aPid = 0; | |
6290 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
6291 | boolean_t new_snapshot = FALSE, killed = FALSE; | |
6292 | int kill_count = 0; | |
39037602 A |
6293 | uint32_t aPid_ep; |
6294 | uint64_t killtime = 0; | |
0a7de745 A |
6295 | clock_sec_t tv_sec; |
6296 | clock_usec_t tv_usec; | |
6297 | uint32_t tv_msec; | |
39037602 A |
6298 | |
6299 | ||
6300 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START, | |
0a7de745 | 6301 | memorystatus_available_pages, 0, 0, 0, 0); |
39037602 | 6302 | |
d9a64523 A |
6303 | #if CONFIG_FREEZE |
6304 | boolean_t consider_frozen_only = FALSE; | |
6305 | ||
6306 | if (band == (unsigned int) memorystatus_freeze_jetsam_band) { | |
6307 | consider_frozen_only = TRUE; | |
6308 | } | |
6309 | #endif /* CONFIG_FREEZE */ | |
6310 | ||
39037602 A |
6311 | proc_list_lock(); |
6312 | ||
d9a64523 | 6313 | next_p = memorystatus_get_first_proc_locked(&band, FALSE); |
39037602 | 6314 | while (next_p) { |
39037602 | 6315 | p = next_p; |
d9a64523 | 6316 | next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); |
39037602 A |
6317 | |
6318 | aPid = p->p_pid; | |
6319 | aPid_ep = p->p_memstat_effectivepriority; | |
6320 | ||
6321 | /* | |
6322 | * Only pick a process pinned in this elevated band | |
6323 | */ | |
6324 | if (!(p->p_memstat_state & P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND)) { | |
6325 | continue; | |
6326 | } | |
6327 | ||
6328 | if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) { | |
6329 | continue; | |
6330 | } | |
6331 | ||
6332 | #if CONFIG_FREEZE | |
0a7de745 | 6333 | if (consider_frozen_only && !(p->p_memstat_state & P_MEMSTAT_FROZEN)) { |
d9a64523 A |
6334 | continue; |
6335 | } | |
6336 | ||
39037602 A |
6337 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { |
6338 | continue; | |
6339 | } | |
d9a64523 | 6340 | #endif /* CONFIG_FREEZE */ |
39037602 A |
6341 | |
6342 | #if DEVELOPMENT || DEBUG | |
6343 | MEMORYSTATUS_DEBUG(1, "jetsam: elevated%d process pid %d [%s] - memorystatus_available_pages: %d\n", | |
0a7de745 A |
6344 | aggr_count, |
6345 | aPid, (*p->p_name ? p->p_name : "unknown"), | |
6346 | memorystatus_available_pages); | |
39037602 A |
6347 | #endif /* DEVELOPMENT || DEBUG */ |
6348 | ||
6349 | if (memorystatus_jetsam_snapshot_count == 0) { | |
0a7de745 | 6350 | memorystatus_init_jetsam_snapshot_locked(NULL, 0); |
39037602 A |
6351 | new_snapshot = TRUE; |
6352 | } | |
6353 | ||
6354 | p->p_memstat_state |= P_MEMSTAT_TERMINATED; | |
6355 | ||
6356 | killtime = mach_absolute_time(); | |
6357 | absolutetime_to_microtime(killtime, &tv_sec, &tv_usec); | |
6358 | tv_msec = tv_usec / 1000; | |
6359 | ||
6360 | memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime); | |
6361 | ||
6362 | if (proc_ref_locked(p) == p) { | |
39037602 A |
6363 | proc_list_unlock(); |
6364 | ||
0a7de745 A |
6365 | os_log_with_startup_serial(OS_LOG_DEFAULT, "%lu.%03d memorystatus: killing_top_process_elevated%d pid %d [%s] (%s %d) - memorystatus_available_pages: %llu\n", |
6366 | (unsigned long)tv_sec, tv_msec, | |
6367 | aggr_count, | |
6368 | aPid, (*p->p_name ? p->p_name : "unknown"), | |
6369 | memorystatus_kill_cause_name[cause], aPid_ep, (uint64_t)memorystatus_available_pages); | |
39037602 A |
6370 | |
6371 | /* | |
6372 | * memorystatus_do_kill drops a reference, so take another one so we can | |
6373 | * continue to use this exit reason even after memorystatus_do_kill() | |
6374 | * returns | |
6375 | */ | |
6376 | os_reason_ref(jetsam_reason); | |
6377 | killed = memorystatus_do_kill(p, cause, jetsam_reason); | |
6378 | ||
6379 | /* Success? */ | |
6380 | if (killed) { | |
6381 | proc_rele(p); | |
6382 | kill_count++; | |
6383 | goto exit; | |
6384 | } | |
6385 | ||
6386 | /* | |
6387 | * Failure - first unwind the state, | |
6388 | * then fall through to restart the search. | |
6389 | */ | |
6390 | proc_list_lock(); | |
6391 | proc_rele_locked(p); | |
6392 | p->p_memstat_state &= ~P_MEMSTAT_TERMINATED; | |
6393 | p->p_memstat_state |= P_MEMSTAT_ERROR; | |
6394 | *errors += 1; | |
6395 | } | |
6396 | ||
6397 | /* | |
6398 | * Failure - restart the search. | |
6399 | * | |
6400 | * We might have raced with "p" exiting on another core, resulting in no | |
6401 | * ref on "p". Or, we may have failed to kill "p". | |
6402 | * | |
6403 | * Either way, we fall thru to here, leaving the proc in the | |
6404 | * P_MEMSTAT_TERMINATED state or P_MEMSTAT_ERROR state. | |
6405 | * | |
6406 | * And, we hold the the proc_list_lock at this point. | |
6407 | */ | |
6408 | ||
d9a64523 | 6409 | next_p = memorystatus_get_first_proc_locked(&band, FALSE); |
39037602 A |
6410 | } |
6411 | ||
6412 | proc_list_unlock(); | |
6413 | ||
6414 | exit: | |
6415 | os_reason_free(jetsam_reason); | |
6416 | ||
39236c6e | 6417 | /* Clear snapshot if freshly captured and no target was found */ |
39037602 A |
6418 | if (new_snapshot && (kill_count == 0)) { |
6419 | proc_list_lock(); | |
39236c6e | 6420 | memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; |
39037602 | 6421 | proc_list_unlock(); |
316670eb | 6422 | } |
39037602 A |
6423 | |
6424 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END, | |
0a7de745 | 6425 | memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0); |
6d2010ae | 6426 | |
0a7de745 | 6427 | return killed; |
316670eb | 6428 | } |
2d21ac55 | 6429 | |
0a7de745 A |
6430 | static boolean_t |
6431 | memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause) | |
6432 | { | |
39037602 A |
6433 | /* |
6434 | * TODO: allow a general async path | |
6435 | * | |
6436 | * NOTE: If a new async kill cause is added, make sure to update memorystatus_thread() to | |
6437 | * add the appropriate exit reason code mapping. | |
6438 | */ | |
d9a64523 | 6439 | if ((victim_pid != -1) || |
0a7de745 A |
6440 | (cause != kMemorystatusKilledVMPageShortage && |
6441 | cause != kMemorystatusKilledVMCompressorThrashing && | |
6442 | cause != kMemorystatusKilledVMCompressorSpaceShortage && | |
6443 | cause != kMemorystatusKilledFCThrashing && | |
6444 | cause != kMemorystatusKilledZoneMapExhaustion)) { | |
39236c6e | 6445 | return FALSE; |
316670eb | 6446 | } |
0a7de745 | 6447 | |
fe8ab488 | 6448 | kill_under_pressure_cause = cause; |
39236c6e A |
6449 | memorystatus_thread_wake(); |
6450 | return TRUE; | |
6451 | } | |
2d21ac55 | 6452 | |
5ba3f43e | 6453 | boolean_t |
0a7de745 A |
6454 | memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async) |
6455 | { | |
39236c6e | 6456 | if (async) { |
d9a64523 | 6457 | return memorystatus_kill_process_async(-1, kMemorystatusKilledVMCompressorSpaceShortage); |
39236c6e | 6458 | } else { |
d9a64523 | 6459 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE); |
39037602 | 6460 | if (jetsam_reason == OS_REASON_NULL) { |
d9a64523 | 6461 | printf("memorystatus_kill_on_VM_compressor_space_shortage -- sync: failed to allocate jetsam reason\n"); |
39037602 A |
6462 | } |
6463 | ||
d9a64523 | 6464 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMCompressorSpaceShortage, jetsam_reason); |
39236c6e A |
6465 | } |
6466 | } | |
2d21ac55 | 6467 | |
5ba3f43e | 6468 | #if CONFIG_JETSAM |
d9a64523 | 6469 | boolean_t |
0a7de745 A |
6470 | memorystatus_kill_on_VM_compressor_thrashing(boolean_t async) |
6471 | { | |
d9a64523 A |
6472 | if (async) { |
6473 | return memorystatus_kill_process_async(-1, kMemorystatusKilledVMCompressorThrashing); | |
6474 | } else { | |
6475 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING); | |
6476 | if (jetsam_reason == OS_REASON_NULL) { | |
6477 | printf("memorystatus_kill_on_VM_compressor_thrashing -- sync: failed to allocate jetsam reason\n"); | |
6478 | } | |
6479 | ||
6480 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMCompressorThrashing, jetsam_reason); | |
6481 | } | |
6482 | } | |
6483 | ||
0a7de745 A |
6484 | boolean_t |
6485 | memorystatus_kill_on_VM_page_shortage(boolean_t async) | |
6486 | { | |
39236c6e | 6487 | if (async) { |
5ba3f43e | 6488 | return memorystatus_kill_process_async(-1, kMemorystatusKilledVMPageShortage); |
39236c6e | 6489 | } else { |
5ba3f43e | 6490 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMPAGESHORTAGE); |
39037602 | 6491 | if (jetsam_reason == OS_REASON_NULL) { |
5ba3f43e | 6492 | printf("memorystatus_kill_on_VM_page_shortage -- sync: failed to allocate jetsam reason\n"); |
39037602 A |
6493 | } |
6494 | ||
5ba3f43e | 6495 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVMPageShortage, jetsam_reason); |
2d21ac55 A |
6496 | } |
6497 | } | |
b0d623f7 | 6498 | |
fe8ab488 | 6499 | boolean_t |
0a7de745 A |
6500 | memorystatus_kill_on_FC_thrashing(boolean_t async) |
6501 | { | |
fe8ab488 A |
6502 | if (async) { |
6503 | return memorystatus_kill_process_async(-1, kMemorystatusKilledFCThrashing); | |
6504 | } else { | |
39037602 A |
6505 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_FCTHRASHING); |
6506 | if (jetsam_reason == OS_REASON_NULL) { | |
6507 | printf("memorystatus_kill_on_FC_thrashing -- sync: failed to allocate jetsam reason\n"); | |
6508 | } | |
6509 | ||
6510 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledFCThrashing, jetsam_reason); | |
fe8ab488 A |
6511 | } |
6512 | } | |
6513 | ||
0a7de745 A |
6514 | boolean_t |
6515 | memorystatus_kill_on_vnode_limit(void) | |
6516 | { | |
39037602 A |
6517 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_VNODE); |
6518 | if (jetsam_reason == OS_REASON_NULL) { | |
6519 | printf("memorystatus_kill_on_vnode_limit: failed to allocate jetsam reason\n"); | |
6520 | } | |
6521 | ||
6522 | return memorystatus_kill_process_sync(-1, kMemorystatusKilledVnodes, jetsam_reason); | |
39236c6e A |
6523 | } |
6524 | ||
316670eb A |
6525 | #endif /* CONFIG_JETSAM */ |
6526 | ||
5ba3f43e | 6527 | boolean_t |
0a7de745 A |
6528 | memorystatus_kill_on_zone_map_exhaustion(pid_t pid) |
6529 | { | |
5ba3f43e A |
6530 | boolean_t res = FALSE; |
6531 | if (pid == -1) { | |
6532 | res = memorystatus_kill_process_async(-1, kMemorystatusKilledZoneMapExhaustion); | |
6533 | } else { | |
6534 | os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_ZONE_MAP_EXHAUSTION); | |
6535 | if (jetsam_reason == OS_REASON_NULL) { | |
6536 | printf("memorystatus_kill_on_zone_map_exhaustion: failed to allocate jetsam reason\n"); | |
6537 | } | |
6538 | ||
6539 | res = memorystatus_kill_process_sync(pid, kMemorystatusKilledZoneMapExhaustion, jetsam_reason); | |
6540 | } | |
6541 | return res; | |
6542 | } | |
6543 | ||
6d2010ae A |
6544 | #if CONFIG_FREEZE |
6545 | ||
6546 | __private_extern__ void | |
316670eb | 6547 | memorystatus_freeze_init(void) |
6d2010ae | 6548 | { |
316670eb A |
6549 | kern_return_t result; |
6550 | thread_t thread; | |
3e170ce0 A |
6551 | |
6552 | freezer_lck_grp_attr = lck_grp_attr_alloc_init(); | |
6553 | freezer_lck_grp = lck_grp_alloc_init("freezer", freezer_lck_grp_attr); | |
6554 | ||
6555 | lck_mtx_init(&freezer_mutex, freezer_lck_grp, NULL); | |
d9a64523 A |
6556 | |
6557 | /* | |
6558 | * This is just the default value if the underlying | |
6559 | * storage device doesn't have any specific budget. | |
6560 | * We check with the storage layer in memorystatus_freeze_update_throttle() | |
6561 | * before we start our freezing the first time. | |
6562 | */ | |
6563 | memorystatus_freeze_budget_pages_remaining = (memorystatus_freeze_daily_mb_max * 1024 * 1024) / PAGE_SIZE; | |
6564 | ||
316670eb A |
6565 | result = kernel_thread_start(memorystatus_freeze_thread, NULL, &thread); |
6566 | if (result == KERN_SUCCESS) { | |
d9a64523 A |
6567 | proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, TASK_POLICY_IO, THROTTLE_LEVEL_COMPRESSOR_TIER2); |
6568 | proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, TASK_POLICY_PASSIVE_IO, TASK_POLICY_ENABLE); | |
6569 | thread_set_thread_name(thread, "VM_freezer"); | |
6570 | ||
316670eb A |
6571 | thread_deallocate(thread); |
6572 | } else { | |
6573 | panic("Could not create memorystatus_freeze_thread"); | |
6574 | } | |
6d2010ae A |
6575 | } |
6576 | ||
d9a64523 A |
6577 | static boolean_t |
6578 | memorystatus_is_process_eligible_for_freeze(proc_t p) | |
6579 | { | |
6580 | /* | |
6581 | * Called with proc_list_lock held. | |
6582 | */ | |
6583 | ||
6584 | LCK_MTX_ASSERT(proc_list_mlock, LCK_MTX_ASSERT_OWNED); | |
6585 | ||
6586 | boolean_t should_freeze = FALSE; | |
6587 | uint32_t state = 0, entry_count = 0, pages = 0, i = 0; | |
6588 | int probability_of_use = 0; | |
6589 | ||
6590 | if (isApp(p) == FALSE) { | |
6591 | goto out; | |
6592 | } | |
6593 | ||
6594 | state = p->p_memstat_state; | |
6595 | ||
6596 | if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FREEZE_DISABLED | P_MEMSTAT_FREEZE_IGNORE)) || | |
0a7de745 | 6597 | !(state & P_MEMSTAT_SUSPENDED)) { |
d9a64523 A |
6598 | goto out; |
6599 | } | |
6600 | ||
6601 | /* Only freeze processes meeting our minimum resident page criteria */ | |
6602 | memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL); | |
6603 | if (pages < memorystatus_freeze_pages_min) { | |
6604 | goto out; | |
6605 | } | |
6606 | ||
6607 | entry_count = (memorystatus_global_probabilities_size / sizeof(memorystatus_internal_probabilities_t)); | |
6608 | ||
6609 | if (entry_count) { | |
0a7de745 | 6610 | for (i = 0; i < entry_count; i++) { |
d9a64523 | 6611 | if (strncmp(memorystatus_global_probabilities_table[i].proc_name, |
0a7de745 A |
6612 | p->p_name, |
6613 | MAXCOMLEN + 1) == 0) { | |
d9a64523 A |
6614 | probability_of_use = memorystatus_global_probabilities_table[i].use_probability; |
6615 | break; | |
6616 | } | |
6617 | } | |
6618 | ||
6619 | if (probability_of_use == 0) { | |
6620 | goto out; | |
6621 | } | |
6622 | } | |
6623 | ||
6624 | should_freeze = TRUE; | |
6625 | out: | |
6626 | return should_freeze; | |
6627 | } | |
6628 | ||
3e170ce0 A |
6629 | /* |
6630 | * Synchronously freeze the passed proc. Called with a reference to the proc held. | |
6631 | * | |
d9a64523 A |
6632 | * Doesn't deal with re-freezing because this is called on a specific process and |
6633 | * not by the freezer thread. If that changes, we'll have to teach it about | |
6634 | * refreezing a frozen process. | |
6635 | * | |
3e170ce0 A |
6636 | * Returns EINVAL or the value returned by task_freeze(). |
6637 | */ | |
6638 | int | |
6639 | memorystatus_freeze_process_sync(proc_t p) | |
6640 | { | |
6641 | int ret = EINVAL; | |
6642 | pid_t aPid = 0; | |
6643 | boolean_t memorystatus_freeze_swap_low = FALSE; | |
0a7de745 | 6644 | int freezer_error_code = 0; |
3e170ce0 A |
6645 | |
6646 | lck_mtx_lock(&freezer_mutex); | |
6647 | ||
6648 | if (p == NULL) { | |
d9a64523 | 6649 | printf("memorystatus_freeze_process_sync: Invalid process\n"); |
3e170ce0 A |
6650 | goto exit; |
6651 | } | |
6652 | ||
6653 | if (memorystatus_freeze_enabled == FALSE) { | |
d9a64523 | 6654 | printf("memorystatus_freeze_process_sync: Freezing is DISABLED\n"); |
3e170ce0 A |
6655 | goto exit; |
6656 | } | |
6657 | ||
6658 | if (!memorystatus_can_freeze(&memorystatus_freeze_swap_low)) { | |
d9a64523 | 6659 | printf("memorystatus_freeze_process_sync: Low compressor and/or low swap space...skipping freeze\n"); |
3e170ce0 A |
6660 | goto exit; |
6661 | } | |
6662 | ||
d9a64523 A |
6663 | memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining); |
6664 | if (!memorystatus_freeze_budget_pages_remaining) { | |
6665 | printf("memorystatus_freeze_process_sync: exit with NO available budget\n"); | |
3e170ce0 A |
6666 | goto exit; |
6667 | } | |
6668 | ||
6669 | proc_list_lock(); | |
6670 | ||
6671 | if (p != NULL) { | |
d9a64523 A |
6672 | uint32_t purgeable, wired, clean, dirty, shared; |
6673 | uint32_t max_pages, i; | |
3e170ce0 A |
6674 | |
6675 | aPid = p->p_pid; | |
3e170ce0 A |
6676 | |
6677 | /* Ensure the process is eligible for freezing */ | |
d9a64523 | 6678 | if (memorystatus_is_process_eligible_for_freeze(p) == FALSE) { |
3e170ce0 A |
6679 | proc_list_unlock(); |
6680 | goto exit; | |
6681 | } | |
6682 | ||
39037602 | 6683 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { |
d9a64523 | 6684 | max_pages = MIN(memorystatus_freeze_pages_max, memorystatus_freeze_budget_pages_remaining); |
3e170ce0 A |
6685 | } else { |
6686 | /* | |
6687 | * We only have the compressor without any swap. | |
6688 | */ | |
6689 | max_pages = UINT32_MAX - 1; | |
6690 | } | |
6691 | ||
6692 | /* Mark as locked temporarily to avoid kill */ | |
6693 | p->p_memstat_state |= P_MEMSTAT_LOCKED; | |
6694 | proc_list_unlock(); | |
6695 | ||
d9a64523 | 6696 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, |
0a7de745 | 6697 | memorystatus_available_pages, 0, 0, 0, 0); |
d9a64523 A |
6698 | |
6699 | ret = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */); | |
6700 | ||
6701 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, | |
0a7de745 | 6702 | memorystatus_available_pages, aPid, 0, 0, 0); |
3e170ce0 | 6703 | |
39037602 A |
6704 | DTRACE_MEMORYSTATUS6(memorystatus_freeze, proc_t, p, unsigned int, memorystatus_available_pages, boolean_t, purgeable, unsigned int, wired, uint32_t, clean, uint32_t, dirty); |
6705 | ||
3e170ce0 | 6706 | MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_process_sync: task_freeze %s for pid %d [%s] - " |
0a7de745 A |
6707 | "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n", |
6708 | (ret == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"), | |
6709 | memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared); | |
3e170ce0 A |
6710 | |
6711 | proc_list_lock(); | |
3e170ce0 A |
6712 | |
6713 | if (ret == KERN_SUCCESS) { | |
d9a64523 | 6714 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (specific) pid %d [%s]...done", |
0a7de745 | 6715 | aPid, (*p->p_name ? p->p_name : "unknown")); |
d9a64523 | 6716 | |
3e170ce0 A |
6717 | memorystatus_freeze_entry_t data = { aPid, TRUE, dirty }; |
6718 | ||
d9a64523 | 6719 | p->p_memstat_freeze_sharedanon_pages += shared; |
3e170ce0 | 6720 | |
d9a64523 | 6721 | memorystatus_frozen_shared_mb += shared; |
3e170ce0 | 6722 | |
d9a64523 A |
6723 | if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == 0) { |
6724 | p->p_memstat_state |= P_MEMSTAT_FROZEN; | |
6725 | memorystatus_frozen_count++; | |
3e170ce0 A |
6726 | } |
6727 | ||
d9a64523 | 6728 | p->p_memstat_frozen_count++; |
3e170ce0 | 6729 | |
d9a64523 A |
6730 | /* |
6731 | * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process | |
6732 | * to its higher jetsam band. | |
6733 | */ | |
3e170ce0 A |
6734 | proc_list_unlock(); |
6735 | ||
6736 | memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); | |
d9a64523 A |
6737 | |
6738 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { | |
d9a64523 | 6739 | ret = memorystatus_update_inactive_jetsam_priority_band(p->p_pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, |
0a7de745 | 6740 | memorystatus_freeze_jetsam_band, TRUE); |
d9a64523 A |
6741 | |
6742 | if (ret) { | |
6743 | printf("Elevating the frozen process failed with %d\n", ret); | |
6744 | /* not fatal */ | |
6745 | ret = 0; | |
6746 | } | |
6747 | ||
6748 | proc_list_lock(); | |
6749 | ||
6750 | /* Update stats */ | |
6751 | for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) { | |
6752 | throttle_intervals[i].pageouts += dirty; | |
6753 | } | |
6754 | } else { | |
6755 | proc_list_lock(); | |
6756 | } | |
6757 | ||
6758 | memorystatus_freeze_pageouts += dirty; | |
6759 | ||
6760 | if (memorystatus_frozen_count == (memorystatus_frozen_processes_max - 1)) { | |
6761 | /* | |
6762 | * Add some eviction logic here? At some point should we | |
6763 | * jetsam a process to get back its swap space so that we | |
6764 | * can freeze a more eligible process at this moment in time? | |
6765 | */ | |
6766 | } | |
3e170ce0 | 6767 | } else { |
d9a64523 A |
6768 | char reason[128]; |
6769 | if (freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) { | |
6770 | strlcpy(reason, "too much shared memory", 128); | |
6771 | } | |
6772 | ||
6773 | if (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) { | |
6774 | strlcpy(reason, "low private-shared pages ratio", 128); | |
6775 | } | |
6776 | ||
6777 | if (freezer_error_code == FREEZER_ERROR_NO_COMPRESSOR_SPACE) { | |
6778 | strlcpy(reason, "no compressor space", 128); | |
6779 | } | |
6780 | ||
6781 | if (freezer_error_code == FREEZER_ERROR_NO_SWAP_SPACE) { | |
6782 | strlcpy(reason, "no swap space", 128); | |
6783 | } | |
6784 | ||
6785 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (specific) pid %d [%s]...skipped (%s)", | |
0a7de745 | 6786 | aPid, (*p->p_name ? p->p_name : "unknown"), reason); |
d9a64523 | 6787 | p->p_memstat_state |= P_MEMSTAT_FREEZE_IGNORE; |
3e170ce0 | 6788 | } |
d9a64523 A |
6789 | |
6790 | p->p_memstat_state &= ~P_MEMSTAT_LOCKED; | |
6791 | proc_list_unlock(); | |
3e170ce0 A |
6792 | } |
6793 | ||
6794 | exit: | |
6795 | lck_mtx_unlock(&freezer_mutex); | |
3e170ce0 A |
6796 | |
6797 | return ret; | |
6798 | } | |
6799 | ||
316670eb | 6800 | static int |
d9a64523 | 6801 | memorystatus_freeze_top_process(void) |
6d2010ae | 6802 | { |
39236c6e A |
6803 | pid_t aPid = 0; |
6804 | int ret = -1; | |
6805 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
6806 | unsigned int i = 0; | |
d9a64523 A |
6807 | unsigned int band = JETSAM_PRIORITY_IDLE; |
6808 | boolean_t refreeze_processes = FALSE; | |
39236c6e A |
6809 | |
6810 | proc_list_lock(); | |
d9a64523 A |
6811 | |
6812 | if (memorystatus_frozen_count >= memorystatus_frozen_processes_max) { | |
6813 | /* | |
6814 | * Freezer is already full but we are here and so let's | |
6815 | * try to refreeze any processes we might have thawed | |
6816 | * in the past and push out their compressed state out. | |
6817 | */ | |
6818 | refreeze_processes = TRUE; | |
6819 | band = (unsigned int) memorystatus_freeze_jetsam_band; | |
6820 | } | |
6821 | ||
0a7de745 | 6822 | freeze_process: |
d9a64523 A |
6823 | |
6824 | next_p = memorystatus_get_first_proc_locked(&band, FALSE); | |
39236c6e A |
6825 | while (next_p) { |
6826 | kern_return_t kr; | |
d9a64523 | 6827 | uint32_t purgeable, wired, clean, dirty, shared; |
39236c6e | 6828 | uint32_t max_pages = 0; |
0a7de745 A |
6829 | int freezer_error_code = 0; |
6830 | ||
39236c6e | 6831 | p = next_p; |
d9a64523 | 6832 | next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); |
6d2010ae | 6833 | |
39236c6e | 6834 | aPid = p->p_pid; |
6d2010ae | 6835 | |
d9a64523 A |
6836 | if (p->p_memstat_effectivepriority != (int32_t) band) { |
6837 | /* | |
6838 | * We shouldn't be freezing processes outside the | |
6839 | * prescribed band. | |
6840 | */ | |
6841 | break; | |
316670eb | 6842 | } |
6d2010ae | 6843 | |
d9a64523 A |
6844 | /* Ensure the process is eligible for (re-)freezing */ |
6845 | if (refreeze_processes) { | |
6846 | /* | |
6847 | * Has to have been frozen once before. | |
6848 | */ | |
6849 | if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == FALSE) { | |
6850 | continue; | |
6851 | } | |
6852 | ||
6853 | /* | |
6854 | * Has to have been resumed once before. | |
6855 | */ | |
6856 | if ((p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) == FALSE) { | |
6857 | continue; | |
6858 | } | |
6859 | ||
6860 | /* | |
6861 | * Not currently being looked at for something. | |
6862 | */ | |
6863 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { | |
6864 | continue; | |
6865 | } | |
3e170ce0 | 6866 | |
d9a64523 A |
6867 | /* |
6868 | * We are going to try and refreeze and so re-evaluate | |
6869 | * the process. We don't want to double count the shared | |
6870 | * memory. So deduct the old snapshot here. | |
6871 | */ | |
6872 | memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages; | |
6873 | p->p_memstat_freeze_sharedanon_pages = 0; | |
3e170ce0 | 6874 | |
d9a64523 A |
6875 | p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; |
6876 | memorystatus_refreeze_eligible_count--; | |
d9a64523 A |
6877 | } else { |
6878 | if (memorystatus_is_process_eligible_for_freeze(p) == FALSE) { | |
6879 | continue; // with lock held | |
6880 | } | |
6881 | } | |
6882 | ||
6883 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { | |
39037602 A |
6884 | /* |
6885 | * Freezer backed by the compressor and swap file(s) | |
d9a64523 | 6886 | * will hold compressed data. |
39037602 | 6887 | */ |
3e170ce0 | 6888 | |
d9a64523 | 6889 | max_pages = MIN(memorystatus_freeze_pages_max, memorystatus_freeze_budget_pages_remaining); |
39236c6e | 6890 | } else { |
3e170ce0 A |
6891 | /* |
6892 | * We only have the compressor pool. | |
6893 | */ | |
39236c6e A |
6894 | max_pages = UINT32_MAX - 1; |
6895 | } | |
0a7de745 | 6896 | |
39236c6e A |
6897 | /* Mark as locked temporarily to avoid kill */ |
6898 | p->p_memstat_state |= P_MEMSTAT_LOCKED; | |
6899 | ||
6900 | p = proc_ref_locked(p); | |
39236c6e | 6901 | if (!p) { |
d9a64523 | 6902 | break; |
39236c6e | 6903 | } |
d9a64523 A |
6904 | |
6905 | proc_list_unlock(); | |
6906 | ||
6907 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START, | |
0a7de745 | 6908 | memorystatus_available_pages, 0, 0, 0, 0); |
d9a64523 A |
6909 | |
6910 | kr = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, &freezer_error_code, FALSE /* eval only */); | |
0a7de745 | 6911 | |
d9a64523 | 6912 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END, |
0a7de745 | 6913 | memorystatus_available_pages, aPid, 0, 0, 0); |
d9a64523 | 6914 | |
39236c6e | 6915 | MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_top_process: task_freeze %s for pid %d [%s] - " |
0a7de745 A |
6916 | "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, max_pages %d, shared %d\n", |
6917 | (kr == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (*p->p_name ? p->p_name : "(unknown)"), | |
6918 | memorystatus_available_pages, purgeable, wired, clean, dirty, max_pages, shared); | |
6919 | ||
39236c6e | 6920 | proc_list_lock(); |
0a7de745 | 6921 | |
39236c6e A |
6922 | /* Success? */ |
6923 | if (KERN_SUCCESS == kr) { | |
d9a64523 A |
6924 | if (refreeze_processes) { |
6925 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: Refreezing (general) pid %d [%s]...done", | |
0a7de745 | 6926 | aPid, (*p->p_name ? p->p_name : "unknown")); |
d9a64523 A |
6927 | } else { |
6928 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (general) pid %d [%s]...done", | |
0a7de745 | 6929 | aPid, (*p->p_name ? p->p_name : "unknown")); |
d9a64523 A |
6930 | } |
6931 | ||
39236c6e | 6932 | memorystatus_freeze_entry_t data = { aPid, TRUE, dirty }; |
0a7de745 | 6933 | |
d9a64523 A |
6934 | p->p_memstat_freeze_sharedanon_pages += shared; |
6935 | ||
6936 | memorystatus_frozen_shared_mb += shared; | |
6937 | ||
6938 | if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == 0) { | |
6939 | p->p_memstat_state |= P_MEMSTAT_FROZEN; | |
6940 | memorystatus_frozen_count++; | |
6941 | } | |
6942 | ||
6943 | p->p_memstat_frozen_count++; | |
6944 | ||
6945 | /* | |
6946 | * Still keeping the P_MEMSTAT_LOCKED bit till we are actually done elevating this frozen process | |
6947 | * to its higher jetsam band. | |
6948 | */ | |
6949 | proc_list_unlock(); | |
6950 | ||
6951 | memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data)); | |
39037602 A |
6952 | |
6953 | if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { | |
d9a64523 A |
6954 | ret = memorystatus_update_inactive_jetsam_priority_band(p->p_pid, MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE, memorystatus_freeze_jetsam_band, TRUE); |
6955 | ||
6956 | if (ret) { | |
6957 | printf("Elevating the frozen process failed with %d\n", ret); | |
6958 | /* not fatal */ | |
6959 | ret = 0; | |
6960 | } | |
6961 | ||
6962 | proc_list_lock(); | |
6963 | ||
3e170ce0 A |
6964 | /* Update stats */ |
6965 | for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) { | |
6966 | throttle_intervals[i].pageouts += dirty; | |
6967 | } | |
d9a64523 A |
6968 | } else { |
6969 | proc_list_lock(); | |
39236c6e | 6970 | } |
3e170ce0 | 6971 | |
39236c6e | 6972 | memorystatus_freeze_pageouts += dirty; |
6d2010ae | 6973 | |
d9a64523 A |
6974 | if (memorystatus_frozen_count == (memorystatus_frozen_processes_max - 1)) { |
6975 | /* | |
6976 | * Add some eviction logic here? At some point should we | |
6977 | * jetsam a process to get back its swap space so that we | |
6978 | * can freeze a more eligible process at this moment in time? | |
6979 | */ | |
6980 | } | |
6d2010ae | 6981 | |
d9a64523 | 6982 | /* Return KERN_SUCCESS */ |
3e170ce0 | 6983 | ret = kr; |
6d2010ae | 6984 | |
d9a64523 A |
6985 | p->p_memstat_state &= ~P_MEMSTAT_LOCKED; |
6986 | proc_rele_locked(p); | |
6987 | ||
6988 | /* | |
6989 | * We froze a process successfully. We can stop now | |
6990 | * and see if that helped. | |
6991 | */ | |
6992 | ||
6993 | break; | |
39236c6e | 6994 | } else { |
d9a64523 A |
6995 | p->p_memstat_state &= ~P_MEMSTAT_LOCKED; |
6996 | ||
6997 | if (refreeze_processes == TRUE) { | |
6998 | if ((freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) || | |
6999 | (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO)) { | |
7000 | /* | |
7001 | * Keeping this prior-frozen process in this high band when | |
7002 | * we failed to re-freeze it due to bad shared memory usage | |
7003 | * could cause excessive pressure on the lower bands. | |
7004 | * We need to demote it for now. It'll get re-evaluated next | |
7005 | * time because we don't set the P_MEMSTAT_FREEZE_IGNORE | |
7006 | * bit. | |
7007 | */ | |
7008 | ||
7009 | p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; | |
7010 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
7011 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, TRUE, TRUE); | |
7012 | } | |
7013 | } else { | |
7014 | p->p_memstat_state |= P_MEMSTAT_FREEZE_IGNORE; | |
7015 | } | |
7016 | ||
7017 | proc_rele_locked(p); | |
7018 | ||
7019 | char reason[128]; | |
7020 | if (freezer_error_code == FREEZER_ERROR_EXCESS_SHARED_MEMORY) { | |
7021 | strlcpy(reason, "too much shared memory", 128); | |
7022 | } | |
7023 | ||
7024 | if (freezer_error_code == FREEZER_ERROR_LOW_PRIVATE_SHARED_RATIO) { | |
7025 | strlcpy(reason, "low private-shared pages ratio", 128); | |
7026 | } | |
7027 | ||
7028 | if (freezer_error_code == FREEZER_ERROR_NO_COMPRESSOR_SPACE) { | |
7029 | strlcpy(reason, "no compressor space", 128); | |
7030 | } | |
7031 | ||
7032 | if (freezer_error_code == FREEZER_ERROR_NO_SWAP_SPACE) { | |
7033 | strlcpy(reason, "no swap space", 128); | |
7034 | } | |
7035 | ||
7036 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: freezing (general) pid %d [%s]...skipped (%s)", | |
0a7de745 | 7037 | aPid, (*p->p_name ? p->p_name : "unknown"), reason); |
d9a64523 A |
7038 | |
7039 | if (vm_compressor_low_on_space() || vm_swap_low_on_space()) { | |
7040 | break; | |
7041 | } | |
316670eb | 7042 | } |
d9a64523 A |
7043 | } |
7044 | ||
7045 | if ((ret == -1) && | |
7046 | (memorystatus_refreeze_eligible_count >= MIN_THAW_REFREEZE_THRESHOLD) && | |
7047 | (refreeze_processes == FALSE)) { | |
7048 | /* | |
7049 | * We failed to freeze a process from the IDLE | |
7050 | * band AND we have some thawed processes | |
7051 | * AND haven't tried refreezing as yet. | |
7052 | * Let's try and re-freeze processes in the | |
7053 | * frozen band that have been resumed in the past | |
7054 | * and so have brought in state from disk. | |
7055 | */ | |
7056 | ||
7057 | band = (unsigned int) memorystatus_freeze_jetsam_band; | |
7058 | ||
7059 | refreeze_processes = TRUE; | |
7060 | ||
7061 | goto freeze_process; | |
6d2010ae | 7062 | } |
0a7de745 | 7063 | |
39236c6e | 7064 | proc_list_unlock(); |
0a7de745 | 7065 | |
39236c6e | 7066 | return ret; |
6d2010ae A |
7067 | } |
7068 | ||
0a7de745 A |
7069 | static inline boolean_t |
7070 | memorystatus_can_freeze_processes(void) | |
6d2010ae | 7071 | { |
316670eb | 7072 | boolean_t ret; |
0a7de745 | 7073 | |
39236c6e | 7074 | proc_list_lock(); |
6d2010ae | 7075 | |
0a7de745 | 7076 | if (memorystatus_suspended_count) { |
d9a64523 | 7077 | memorystatus_freeze_suspended_threshold = MIN(memorystatus_freeze_suspended_threshold, FREEZE_SUSPENDED_THRESHOLD_DEFAULT); |
0a7de745 | 7078 | |
316670eb A |
7079 | if ((memorystatus_suspended_count - memorystatus_frozen_count) > memorystatus_freeze_suspended_threshold) { |
7080 | ret = TRUE; | |
7081 | } else { | |
7082 | ret = FALSE; | |
6d2010ae | 7083 | } |
316670eb A |
7084 | } else { |
7085 | ret = FALSE; | |
6d2010ae | 7086 | } |
0a7de745 | 7087 | |
39236c6e | 7088 | proc_list_unlock(); |
0a7de745 | 7089 | |
316670eb | 7090 | return ret; |
6d2010ae A |
7091 | } |
7092 | ||
0a7de745 | 7093 | static boolean_t |
316670eb | 7094 | memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low) |
6d2010ae | 7095 | { |
3e170ce0 A |
7096 | boolean_t can_freeze = TRUE; |
7097 | ||
316670eb | 7098 | /* Only freeze if we're sufficiently low on memory; this holds off freeze right |
0a7de745 | 7099 | * after boot, and is generally is a no-op once we've reached steady state. */ |
316670eb A |
7100 | if (memorystatus_available_pages > memorystatus_freeze_threshold) { |
7101 | return FALSE; | |
7102 | } | |
0a7de745 | 7103 | |
316670eb A |
7104 | /* Check minimum suspended process threshold. */ |
7105 | if (!memorystatus_can_freeze_processes()) { | |
7106 | return FALSE; | |
7107 | } | |
39037602 | 7108 | assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); |
6d2010ae | 7109 | |
0a7de745 | 7110 | if (!VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) { |
3e170ce0 A |
7111 | /* |
7112 | * In-core compressor used for freezing WITHOUT on-disk swap support. | |
7113 | */ | |
3e170ce0 A |
7114 | if (vm_compressor_low_on_space()) { |
7115 | if (*memorystatus_freeze_swap_low) { | |
7116 | *memorystatus_freeze_swap_low = TRUE; | |
7117 | } | |
7118 | ||
7119 | can_freeze = FALSE; | |
3e170ce0 A |
7120 | } else { |
7121 | if (*memorystatus_freeze_swap_low) { | |
7122 | *memorystatus_freeze_swap_low = FALSE; | |
7123 | } | |
7124 | ||
7125 | can_freeze = TRUE; | |
7126 | } | |
7127 | } else { | |
7128 | /* | |
7129 | * Freezing WITH on-disk swap support. | |
39037602 A |
7130 | * |
7131 | * In-core compressor fronts the swap. | |
3e170ce0 | 7132 | */ |
39037602 A |
7133 | if (vm_swap_low_on_space()) { |
7134 | if (*memorystatus_freeze_swap_low) { | |
7135 | *memorystatus_freeze_swap_low = TRUE; | |
3e170ce0 A |
7136 | } |
7137 | ||
39037602 | 7138 | can_freeze = FALSE; |
316670eb | 7139 | } |
6d2010ae | 7140 | } |
0a7de745 | 7141 | |
3e170ce0 | 7142 | return can_freeze; |
6d2010ae A |
7143 | } |
7144 | ||
d9a64523 A |
7145 | /* |
7146 | * This function evaluates if the currently frozen processes deserve | |
7147 | * to stay in the higher jetsam band. If the # of thaws of a process | |
7148 | * is below our threshold, then we will demote that process into the IDLE | |
7149 | * band and put it at the head. We don't immediately kill the process here | |
7150 | * because it already has state on disk and so it might be worth giving | |
7151 | * it another shot at getting thawed/resumed and used. | |
7152 | */ | |
6d2010ae | 7153 | static void |
d9a64523 | 7154 | memorystatus_demote_frozen_processes(void) |
6d2010ae | 7155 | { |
d9a64523 A |
7156 | unsigned int band = (unsigned int) memorystatus_freeze_jetsam_band; |
7157 | unsigned int demoted_proc_count = 0; | |
7158 | proc_t p = PROC_NULL, next_p = PROC_NULL; | |
7159 | ||
7160 | proc_list_lock(); | |
7161 | ||
7162 | if (memorystatus_freeze_enabled == FALSE) { | |
7163 | /* | |
7164 | * Freeze has been disabled likely to | |
7165 | * reclaim swap space. So don't change | |
7166 | * any state on the frozen processes. | |
7167 | */ | |
7168 | proc_list_unlock(); | |
7169 | return; | |
7170 | } | |
7171 | ||
7172 | next_p = memorystatus_get_first_proc_locked(&band, FALSE); | |
7173 | while (next_p) { | |
d9a64523 A |
7174 | p = next_p; |
7175 | next_p = memorystatus_get_next_proc_locked(&band, p, FALSE); | |
7176 | ||
7177 | if ((p->p_memstat_state & P_MEMSTAT_FROZEN) == FALSE) { | |
7178 | continue; | |
6d2010ae | 7179 | } |
d9a64523 A |
7180 | |
7181 | if (p->p_memstat_state & P_MEMSTAT_LOCKED) { | |
7182 | continue; | |
6d2010ae | 7183 | } |
316670eb | 7184 | |
d9a64523 A |
7185 | if (p->p_memstat_thaw_count < memorystatus_thaw_count_demotion_threshold) { |
7186 | p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND; | |
7187 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); | |
7188 | ||
7189 | memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, TRUE, TRUE); | |
7190 | #if DEVELOPMENT || DEBUG | |
7191 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus_demote_frozen_process pid %d [%s]", | |
0a7de745 | 7192 | p->p_pid, (*p->p_name ? p->p_name : "unknown")); |
d9a64523 A |
7193 | #endif /* DEVELOPMENT || DEBUG */ |
7194 | ||
7195 | /* | |
7196 | * The freezer thread will consider this a normal app to be frozen | |
7197 | * because it is in the IDLE band. So we don't need the | |
7198 | * P_MEMSTAT_REFREEZE_ELIGIBLE state here. Also, if it gets resumed | |
7199 | * we'll correctly count it as eligible for re-freeze again. | |
7200 | * | |
7201 | * We don't drop the frozen count because this process still has | |
7202 | * state on disk. So there's a chance it gets resumed and then it | |
7203 | * should land in the higher jetsam band. For that it needs to | |
7204 | * remain marked frozen. | |
7205 | */ | |
7206 | if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) { | |
7207 | p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE; | |
7208 | memorystatus_refreeze_eligible_count--; | |
7209 | } | |
7210 | ||
7211 | demoted_proc_count++; | |
7212 | } | |
7213 | ||
7214 | if (demoted_proc_count == memorystatus_max_frozen_demotions_daily) { | |
7215 | break; | |
7216 | } | |
7217 | } | |
7218 | ||
7219 | memorystatus_thaw_count = 0; | |
7220 | proc_list_unlock(); | |
6d2010ae A |
7221 | } |
7222 | ||
d9a64523 A |
7223 | |
7224 | /* | |
7225 | * This function will do 4 things: | |
7226 | * | |
7227 | * 1) check to see if we are currently in a degraded freezer mode, and if so: | |
7228 | * - check to see if our window has expired and we should exit this mode, OR, | |
7229 | * - return a budget based on the degraded throttle window's max. pageouts vs current pageouts. | |
7230 | * | |
7231 | * 2) check to see if we are in a NEW normal window and update the normal throttle window's params. | |
7232 | * | |
7233 | * 3) check what the current normal window allows for a budget. | |
7234 | * | |
7235 | * 4) calculate the current rate of pageouts for DEGRADED_WINDOW_MINS duration. If that rate is below | |
7236 | * what we would normally expect, then we are running low on our daily budget and need to enter | |
7237 | * degraded perf. mode. | |
7238 | */ | |
7239 | ||
7240 | static void | |
7241 | memorystatus_freeze_update_throttle(uint64_t *budget_pages_allowed) | |
6d2010ae A |
7242 | { |
7243 | clock_sec_t sec; | |
7244 | clock_nsec_t nsec; | |
7245 | mach_timespec_t ts; | |
d9a64523 A |
7246 | |
7247 | unsigned int freeze_daily_pageouts_max = 0; | |
6d2010ae A |
7248 | |
7249 | #if DEVELOPMENT || DEBUG | |
d9a64523 A |
7250 | if (!memorystatus_freeze_throttle_enabled) { |
7251 | /* | |
7252 | * No throttling...we can use the full budget everytime. | |
7253 | */ | |
7254 | *budget_pages_allowed = UINT64_MAX; | |
7255 | return; | |
7256 | } | |
6d2010ae A |
7257 | #endif |
7258 | ||
7259 | clock_get_system_nanotime(&sec, &nsec); | |
7260 | ts.tv_sec = sec; | |
7261 | ts.tv_nsec = nsec; | |
6d2010ae | 7262 | |
d9a64523 A |
7263 | struct throttle_interval_t *interval = NULL; |
7264 | ||
7265 | if (memorystatus_freeze_degradation == TRUE) { | |
d9a64523 A |
7266 | interval = degraded_throttle_window; |
7267 | ||
7268 | if (CMP_MACH_TIMESPEC(&ts, &interval->ts) >= 0) { | |
7269 | memorystatus_freeze_degradation = FALSE; | |
7270 | interval->pageouts = 0; | |
7271 | interval->max_pageouts = 0; | |
d9a64523 A |
7272 | } else { |
7273 | *budget_pages_allowed = interval->max_pageouts - interval->pageouts; | |
7274 | } | |
7275 | } | |
7276 | ||
7277 | interval = normal_throttle_window; | |
7278 | ||
7279 | if (CMP_MACH_TIMESPEC(&ts, &interval->ts) >= 0) { | |
7280 | /* | |
7281 | * New throttle window. | |
7282 | * Rollover any unused budget. | |
7283 | * Also ask the storage layer what the new budget needs to be. | |
7284 | */ | |
7285 | uint64_t freeze_daily_budget = 0; | |
7286 | unsigned int daily_budget_pageouts = 0; | |
7287 | ||
7288 | if (vm_swap_max_budget(&freeze_daily_budget)) { | |
7289 | memorystatus_freeze_daily_mb_max = (freeze_daily_budget / (1024 * 1024)); | |
7290 | os_log_with_startup_serial(OS_LOG_DEFAULT, "memorystatus: memorystatus_freeze_daily_mb_max set to %dMB\n", memorystatus_freeze_daily_mb_max); | |
7291 | } | |
7292 | ||
7293 | freeze_daily_pageouts_max = memorystatus_freeze_daily_mb_max * (1024 * 1024 / PAGE_SIZE); | |
7294 | ||
7295 | daily_budget_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * freeze_daily_pageouts_max) / NORMAL_WINDOW_MINS)); | |
7296 | interval->max_pageouts = (interval->max_pageouts - interval->pageouts) + daily_budget_pageouts; | |
7297 | ||
7298 | interval->ts.tv_sec = interval->mins * 60; | |
7299 | interval->ts.tv_nsec = 0; | |
7300 | ADD_MACH_TIMESPEC(&interval->ts, &ts); | |
7301 | /* Since we update the throttle stats pre-freeze, adjust for overshoot here */ | |
7302 | if (interval->pageouts > interval->max_pageouts) { | |
7303 | interval->pageouts -= interval->max_pageouts; | |
7304 | } else { | |
7305 | interval->pageouts = 0; | |
7306 | } | |
7307 | *budget_pages_allowed = interval->max_pageouts; | |
7308 | ||
7309 | memorystatus_demote_frozen_processes(); | |
d9a64523 A |
7310 | } else { |
7311 | /* | |
7312 | * Current throttle window. | |
7313 | * Deny freezing if we have no budget left. | |
7314 | * Try graceful degradation if we are within 25% of: | |
7315 | * - the daily budget, and | |
7316 | * - the current budget left is below our normal budget expectations. | |
7317 | */ | |
7318 | ||
7319 | #if DEVELOPMENT || DEBUG | |
7320 | /* | |
0a7de745 A |
7321 | * This can only happen in the INTERNAL configs because we allow modifying the daily budget for testing. |
7322 | */ | |
d9a64523 A |
7323 | |
7324 | if (freeze_daily_pageouts_max > interval->max_pageouts) { | |
7325 | /* | |
7326 | * We just bumped the daily budget. Re-evaluate our normal window params. | |
7327 | */ | |
7328 | interval->max_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * freeze_daily_pageouts_max) / NORMAL_WINDOW_MINS)); | |
7329 | memorystatus_freeze_degradation = FALSE; //we'll re-evaluate this below... | |
7330 | } | |
7331 | #endif /* DEVELOPMENT || DEBUG */ | |
7332 | ||
7333 | if (memorystatus_freeze_degradation == FALSE) { | |
d9a64523 | 7334 | if (interval->pageouts >= interval->max_pageouts) { |
d9a64523 | 7335 | *budget_pages_allowed = 0; |
d9a64523 | 7336 | } else { |
d9a64523 A |
7337 | int budget_left = interval->max_pageouts - interval->pageouts; |
7338 | int budget_threshold = (freeze_daily_pageouts_max * FREEZE_DEGRADATION_BUDGET_THRESHOLD) / 100; | |
7339 | ||
0a7de745 | 7340 | mach_timespec_t time_left = {0, 0}; |
d9a64523 A |
7341 | |
7342 | time_left.tv_sec = interval->ts.tv_sec; | |
7343 | time_left.tv_nsec = 0; | |
7344 | ||
7345 | SUB_MACH_TIMESPEC(&time_left, &ts); | |
7346 | ||
7347 | if (budget_left <= budget_threshold) { | |
d9a64523 A |
7348 | /* |
7349 | * For the current normal window, calculate how much we would pageout in a DEGRADED_WINDOW_MINS duration. | |
7350 | * And also calculate what we would pageout for the same DEGRADED_WINDOW_MINS duration if we had the full | |
7351 | * daily pageout budget. | |
7352 | */ | |
7353 | ||
7354 | unsigned int current_budget_rate_allowed = ((budget_left / time_left.tv_sec) / 60) * DEGRADED_WINDOW_MINS; | |
7355 | unsigned int normal_budget_rate_allowed = (freeze_daily_pageouts_max / NORMAL_WINDOW_MINS) * DEGRADED_WINDOW_MINS; | |
7356 | ||
7357 | /* | |
7358 | * The current rate of pageouts is below what we would expect for | |
7359 | * the normal rate i.e. we have below normal budget left and so... | |
7360 | */ | |
7361 | ||
7362 | if (current_budget_rate_allowed < normal_budget_rate_allowed) { | |
d9a64523 A |
7363 | memorystatus_freeze_degradation = TRUE; |
7364 | degraded_throttle_window->max_pageouts = current_budget_rate_allowed; | |
7365 | degraded_throttle_window->pageouts = 0; | |
7366 | ||
7367 | /* | |
7368 | * Switch over to the degraded throttle window so the budget | |
7369 | * doled out is based on that window. | |
7370 | */ | |
7371 | interval = degraded_throttle_window; | |
7372 | } | |
7373 | } | |
7374 | ||
7375 | *budget_pages_allowed = interval->max_pageouts - interval->pageouts; | |
7376 | } | |
7377 | } | |
7378 | } | |
7379 | ||
7380 | MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_update_throttle_interval: throttle updated - %d frozen (%d max) within %dm; %dm remaining; throttle %s\n", | |
0a7de745 A |
7381 | interval->pageouts, interval->max_pageouts, interval->mins, (interval->ts.tv_sec - ts->tv_sec) / 60, |
7382 | interval->throttle ? "on" : "off"); | |
6d2010ae A |
7383 | } |
7384 | ||
7385 | static void | |
316670eb | 7386 | memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused) |
6d2010ae | 7387 | { |
316670eb | 7388 | static boolean_t memorystatus_freeze_swap_low = FALSE; |
3e170ce0 A |
7389 | |
7390 | lck_mtx_lock(&freezer_mutex); | |
d9a64523 | 7391 | |
316670eb | 7392 | if (memorystatus_freeze_enabled) { |
d9a64523 A |
7393 | if ((memorystatus_frozen_count < memorystatus_frozen_processes_max) || |
7394 | (memorystatus_refreeze_eligible_count >= MIN_THAW_REFREEZE_THRESHOLD)) { | |
d9a64523 | 7395 | if (memorystatus_can_freeze(&memorystatus_freeze_swap_low)) { |
d9a64523 A |
7396 | /* Only freeze if we've not exceeded our pageout budgets.*/ |
7397 | memorystatus_freeze_update_throttle(&memorystatus_freeze_budget_pages_remaining); | |
7398 | ||
7399 | if (memorystatus_freeze_budget_pages_remaining) { | |
7400 | memorystatus_freeze_top_process(); | |
7401 | } | |
316670eb A |
7402 | } |
7403 | } | |
7404 | } | |
d9a64523 A |
7405 | |
7406 | /* | |
7407 | * We use memorystatus_apps_idle_delay_time because if/when we adopt aging for applications, | |
7408 | * it'll tie neatly into running the freezer once we age an application. | |
7409 | * | |
7410 | * Till then, it serves as a good interval that can be tuned via a sysctl too. | |
7411 | */ | |
7412 | memorystatus_freezer_thread_next_run_ts = mach_absolute_time() + memorystatus_apps_idle_delay_time; | |
6d2010ae | 7413 | |
316670eb | 7414 | assert_wait((event_t) &memorystatus_freeze_wakeup, THREAD_UNINT); |
d9a64523 A |
7415 | lck_mtx_unlock(&freezer_mutex); |
7416 | ||
0a7de745 | 7417 | thread_block((thread_continue_t) memorystatus_freeze_thread); |
316670eb A |
7418 | } |
7419 | ||
d9a64523 A |
7420 | static boolean_t |
7421 | memorystatus_freeze_thread_should_run(void) | |
7422 | { | |
7423 | /* | |
7424 | * No freezer_mutex held here...see why near call-site | |
7425 | * within memorystatus_pages_update(). | |
7426 | */ | |
7427 | ||
7428 | boolean_t should_run = FALSE; | |
7429 | ||
7430 | if (memorystatus_freeze_enabled == FALSE) { | |
7431 | goto out; | |
7432 | } | |
7433 | ||
7434 | if (memorystatus_available_pages > memorystatus_freeze_threshold) { | |
7435 | goto out; | |
7436 | } | |
7437 | ||
7438 | if ((memorystatus_frozen_count >= memorystatus_frozen_processes_max) && | |
7439 | (memorystatus_refreeze_eligible_count < MIN_THAW_REFREEZE_THRESHOLD)) { | |
7440 | goto out; | |
7441 | } | |
7442 | ||
7443 | if (memorystatus_frozen_shared_mb_max && (memorystatus_frozen_shared_mb >= memorystatus_frozen_shared_mb_max)) { | |
7444 | goto out; | |
7445 | } | |
7446 | ||
7447 | uint64_t curr_time = mach_absolute_time(); | |
7448 | ||
7449 | if (curr_time < memorystatus_freezer_thread_next_run_ts) { | |
7450 | goto out; | |
7451 | } | |
7452 | ||
7453 | should_run = TRUE; | |
7454 | ||
7455 | out: | |
7456 | return should_run; | |
7457 | } | |
7458 | ||
d190cdc3 A |
7459 | static int |
7460 | sysctl_memorystatus_do_fastwake_warmup_all SYSCTL_HANDLER_ARGS | |
7461 | { | |
7462 | #pragma unused(oidp, req, arg1, arg2) | |
7463 | ||
7464 | /* Need to be root or have entitlement */ | |
7465 | if (!kauth_cred_issuser(kauth_cred_get()) && !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT)) { | |
7466 | return EPERM; | |
7467 | } | |
7468 | ||
7469 | if (memorystatus_freeze_enabled == FALSE) { | |
7470 | return ENOTSUP; | |
7471 | } | |
7472 | ||
7473 | do_fastwake_warmup_all(); | |
7474 | ||
7475 | return 0; | |
7476 | } | |
7477 | ||
0a7de745 | 7478 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_do_fastwake_warmup_all, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
d190cdc3 A |
7479 | 0, 0, &sysctl_memorystatus_do_fastwake_warmup_all, "I", ""); |
7480 | ||
316670eb | 7481 | #endif /* CONFIG_FREEZE */ |
6d2010ae | 7482 | |
fe8ab488 | 7483 | #if VM_PRESSURE_EVENTS |
6d2010ae | 7484 | |
fe8ab488 | 7485 | #if CONFIG_MEMORYSTATUS |
316670eb | 7486 | |
fe8ab488 | 7487 | static int |
0a7de745 A |
7488 | memorystatus_send_note(int event_code, void *data, size_t data_length) |
7489 | { | |
fe8ab488 A |
7490 | int ret; |
7491 | struct kev_msg ev_msg; | |
39037602 | 7492 | |
fe8ab488 A |
7493 | ev_msg.vendor_code = KEV_VENDOR_APPLE; |
7494 | ev_msg.kev_class = KEV_SYSTEM_CLASS; | |
7495 | ev_msg.kev_subclass = KEV_MEMORYSTATUS_SUBCLASS; | |
7496 | ||
7497 | ev_msg.event_code = event_code; | |
7498 | ||
7499 | ev_msg.dv[0].data_length = data_length; | |
7500 | ev_msg.dv[0].data_ptr = data; | |
7501 | ev_msg.dv[1].data_length = 0; | |
7502 | ||
7503 | ret = kev_post_msg(&ev_msg); | |
7504 | if (ret) { | |
7505 | printf("%s: kev_post_msg() failed, err %d\n", __func__, ret); | |
316670eb | 7506 | } |
0a7de745 | 7507 | |
fe8ab488 | 7508 | return ret; |
316670eb A |
7509 | } |
7510 | ||
fe8ab488 | 7511 | boolean_t |
0a7de745 A |
7512 | memorystatus_warn_process(pid_t pid, __unused boolean_t is_active, __unused boolean_t is_fatal, boolean_t limit_exceeded) |
7513 | { | |
fe8ab488 | 7514 | boolean_t ret = FALSE; |
3e170ce0 | 7515 | boolean_t found_knote = FALSE; |
fe8ab488 | 7516 | struct knote *kn = NULL; |
813fb2f6 | 7517 | int send_knote_count = 0; |
316670eb | 7518 | |
fe8ab488 A |
7519 | /* |
7520 | * See comment in sysctl_memorystatus_vm_pressure_send. | |
7521 | */ | |
39236c6e | 7522 | |
fe8ab488 | 7523 | memorystatus_klist_lock(); |
3e170ce0 A |
7524 | |
7525 | SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) { | |
39037602 | 7526 | proc_t knote_proc = knote_get_kq(kn)->kq_p; |
3e170ce0 A |
7527 | pid_t knote_pid = knote_proc->p_pid; |
7528 | ||
7529 | if (knote_pid == pid) { | |
7530 | /* | |
7531 | * By setting the "fflags" here, we are forcing | |
7532 | * a process to deal with the case where it's | |
7533 | * bumping up into its memory limits. If we don't | |
7534 | * do this here, we will end up depending on the | |
7535 | * system pressure snapshot evaluation in | |
7536 | * filt_memorystatus(). | |
7537 | */ | |
39037602 | 7538 | |
5ba3f43e A |
7539 | #if CONFIG_EMBEDDED |
7540 | if (!limit_exceeded) { | |
7541 | /* | |
7542 | * Intentionally set either the unambiguous limit warning, | |
7543 | * the system-wide critical or the system-wide warning | |
7544 | * notification bit. | |
7545 | */ | |
7546 | ||
7547 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { | |
7548 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; | |
7549 | found_knote = TRUE; | |
7550 | send_knote_count++; | |
7551 | } else if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) { | |
7552 | kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; | |
7553 | found_knote = TRUE; | |
7554 | send_knote_count++; | |
7555 | } else if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) { | |
7556 | kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN; | |
7557 | found_knote = TRUE; | |
7558 | send_knote_count++; | |
7559 | } | |
7560 | } else { | |
7561 | /* | |
7562 | * Send this notification when a process has exceeded a soft limit. | |
7563 | */ | |
7564 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { | |
7565 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; | |
7566 | found_knote = TRUE; | |
7567 | send_knote_count++; | |
7568 | } | |
7569 | } | |
7570 | #else /* CONFIG_EMBEDDED */ | |
39037602 | 7571 | if (!limit_exceeded) { |
39037602 A |
7572 | /* |
7573 | * Processes on desktop are not expecting to handle a system-wide | |
7574 | * critical or system-wide warning notification from this path. | |
7575 | * Intentionally set only the unambiguous limit warning here. | |
813fb2f6 A |
7576 | * |
7577 | * If the limit is soft, however, limit this to one notification per | |
7578 | * active/inactive limit (per each registered listener). | |
39037602 A |
7579 | */ |
7580 | ||
7581 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { | |
0a7de745 | 7582 | found_knote = TRUE; |
813fb2f6 A |
7583 | if (!is_fatal) { |
7584 | /* | |
7585 | * Restrict proc_limit_warn notifications when | |
7586 | * non-fatal (soft) limit is at play. | |
7587 | */ | |
7588 | if (is_active) { | |
7589 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE) { | |
0a7de745 A |
7590 | /* |
7591 | * Mark this knote for delivery. | |
7592 | */ | |
7593 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; | |
813fb2f6 A |
7594 | /* |
7595 | * And suppress it from future notifications. | |
0a7de745 A |
7596 | */ |
7597 | kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; | |
813fb2f6 | 7598 | send_knote_count++; |
0a7de745 | 7599 | } |
813fb2f6 A |
7600 | } else { |
7601 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE) { | |
0a7de745 A |
7602 | /* |
7603 | * Mark this knote for delivery. | |
7604 | */ | |
7605 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; | |
813fb2f6 A |
7606 | /* |
7607 | * And suppress it from future notifications. | |
0a7de745 A |
7608 | */ |
7609 | kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; | |
813fb2f6 | 7610 | send_knote_count++; |
0a7de745 | 7611 | } |
813fb2f6 A |
7612 | } |
7613 | } else { | |
7614 | /* | |
7615 | * No restriction on proc_limit_warn notifications when | |
7616 | * fatal (hard) limit is at play. | |
7617 | */ | |
7618 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; | |
7619 | send_knote_count++; | |
7620 | } | |
3e170ce0 A |
7621 | } |
7622 | } else { | |
39037602 | 7623 | /* |
813fb2f6 | 7624 | * Send this notification when a process has exceeded a soft limit, |
39037602 | 7625 | */ |
813fb2f6 | 7626 | |
39037602 | 7627 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { |
39037602 | 7628 | found_knote = TRUE; |
813fb2f6 A |
7629 | if (!is_fatal) { |
7630 | /* | |
7631 | * Restrict critical notifications for soft limits. | |
7632 | */ | |
7633 | ||
7634 | if (is_active) { | |
7635 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE) { | |
7636 | /* | |
7637 | * Suppress future proc_limit_critical notifications | |
7638 | * for the active soft limit. | |
7639 | */ | |
7640 | kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE; | |
7641 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; | |
7642 | send_knote_count++; | |
813fb2f6 A |
7643 | } |
7644 | } else { | |
7645 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE) { | |
7646 | /* | |
7647 | * Suppress future proc_limit_critical_notifications | |
7648 | * for the inactive soft limit. | |
7649 | */ | |
7650 | kn->kn_sfflags &= ~NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE; | |
7651 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; | |
7652 | send_knote_count++; | |
7653 | } | |
7654 | } | |
7655 | } else { | |
7656 | /* | |
7657 | * We should never be trying to send a critical notification for | |
7658 | * a hard limit... the process would be killed before it could be | |
7659 | * received. | |
7660 | */ | |
7661 | panic("Caught sending pid %d a critical warning for a fatal limit.\n", pid); | |
7662 | } | |
3e170ce0 A |
7663 | } |
7664 | } | |
5ba3f43e | 7665 | #endif /* CONFIG_EMBEDDED */ |
39236c6e | 7666 | } |
3e170ce0 A |
7667 | } |
7668 | ||
7669 | if (found_knote) { | |
813fb2f6 A |
7670 | if (send_knote_count > 0) { |
7671 | KNOTE(&memorystatus_klist, 0); | |
7672 | } | |
3e170ce0 | 7673 | ret = TRUE; |
6d2010ae | 7674 | } |
3e170ce0 | 7675 | |
fe8ab488 | 7676 | memorystatus_klist_unlock(); |
6d2010ae | 7677 | |
fe8ab488 | 7678 | return ret; |
316670eb A |
7679 | } |
7680 | ||
3e170ce0 A |
7681 | /* |
7682 | * Can only be set by the current task on itself. | |
7683 | */ | |
7684 | int | |
7685 | memorystatus_low_mem_privileged_listener(uint32_t op_flags) | |
7686 | { | |
7687 | boolean_t set_privilege = FALSE; | |
7688 | /* | |
7689 | * Need an entitlement check here? | |
7690 | */ | |
7691 | if (op_flags == MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE) { | |
7692 | set_privilege = TRUE; | |
7693 | } else if (op_flags == MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE) { | |
7694 | set_privilege = FALSE; | |
7695 | } else { | |
7696 | return EINVAL; | |
7697 | } | |
7698 | ||
0a7de745 | 7699 | return task_low_mem_privileged_listener(current_task(), set_privilege, NULL); |
3e170ce0 A |
7700 | } |
7701 | ||
39236c6e | 7702 | int |
0a7de745 A |
7703 | memorystatus_send_pressure_note(pid_t pid) |
7704 | { | |
7705 | MEMORYSTATUS_DEBUG(1, "memorystatus_send_pressure_note(): pid %d\n", pid); | |
7706 | return memorystatus_send_note(kMemorystatusPressureNote, &pid, sizeof(pid)); | |
6d2010ae A |
7707 | } |
7708 | ||
fe8ab488 | 7709 | void |
0a7de745 A |
7710 | memorystatus_send_low_swap_note(void) |
7711 | { | |
fe8ab488 | 7712 | struct knote *kn = NULL; |
3e170ce0 | 7713 | |
fe8ab488 A |
7714 | memorystatus_klist_lock(); |
7715 | SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) { | |
3e170ce0 A |
7716 | /* We call is_knote_registered_modify_task_pressure_bits to check if the sfflags for the |
7717 | * current note contain NOTE_MEMORYSTATUS_LOW_SWAP. Once we find one note in the memorystatus_klist | |
7718 | * that has the NOTE_MEMORYSTATUS_LOW_SWAP flags in its sfflags set, we call KNOTE with | |
7719 | * kMemoryStatusLowSwap as the hint to process and update all knotes on the memorystatus_klist accordingly. */ | |
fe8ab488 | 7720 | if (is_knote_registered_modify_task_pressure_bits(kn, NOTE_MEMORYSTATUS_LOW_SWAP, NULL, 0, 0) == TRUE) { |
3e170ce0 A |
7721 | KNOTE(&memorystatus_klist, kMemorystatusLowSwap); |
7722 | break; | |
fe8ab488 A |
7723 | } |
7724 | } | |
3e170ce0 | 7725 | |
fe8ab488 A |
7726 | memorystatus_klist_unlock(); |
7727 | } | |
7728 | ||
39236c6e | 7729 | boolean_t |
0a7de745 A |
7730 | memorystatus_bg_pressure_eligible(proc_t p) |
7731 | { | |
7732 | boolean_t eligible = FALSE; | |
7733 | ||
39236c6e | 7734 | proc_list_lock(); |
0a7de745 | 7735 | |
39236c6e | 7736 | MEMORYSTATUS_DEBUG(1, "memorystatus_bg_pressure_eligible: pid %d, state 0x%x\n", p->p_pid, p->p_memstat_state); |
0a7de745 A |
7737 | |
7738 | /* Foreground processes have already been dealt with at this point, so just test for eligibility */ | |
7739 | if (!(p->p_memstat_state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN))) { | |
7740 | eligible = TRUE; | |
39236c6e | 7741 | } |
d9a64523 A |
7742 | |
7743 | if (p->p_memstat_effectivepriority < JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC) { | |
7744 | /* | |
7745 | * IDLE and IDLE_DEFERRED bands contain processes | |
7746 | * that have dropped memory to be under their inactive | |
7747 | * memory limits. And so they can't really give back | |
7748 | * anything. | |
7749 | */ | |
7750 | eligible = FALSE; | |
7751 | } | |
7752 | ||
39236c6e | 7753 | proc_list_unlock(); |
0a7de745 A |
7754 | |
7755 | return eligible; | |
39236c6e A |
7756 | } |
7757 | ||
7758 | boolean_t | |
0a7de745 A |
7759 | memorystatus_is_foreground_locked(proc_t p) |
7760 | { | |
7761 | return (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND) || | |
7762 | (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND_SUPPORT); | |
39236c6e | 7763 | } |
39037602 A |
7764 | |
7765 | /* | |
7766 | * This is meant for stackshot and kperf -- it does not take the proc_list_lock | |
7767 | * to access the p_memstat_dirty field. | |
7768 | */ | |
0a7de745 A |
7769 | void |
7770 | memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit) | |
39037602 A |
7771 | { |
7772 | if (!v) { | |
0a7de745 A |
7773 | *is_dirty = FALSE; |
7774 | *is_dirty_tracked = FALSE; | |
d9a64523 A |
7775 | *allow_idle_exit = FALSE; |
7776 | } else { | |
7777 | proc_t p = (proc_t)v; | |
7778 | *is_dirty = (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) != 0; | |
7779 | *is_dirty_tracked = (p->p_memstat_dirty & P_DIRTY_TRACK) != 0; | |
7780 | *allow_idle_exit = (p->p_memstat_dirty & P_DIRTY_ALLOW_IDLE_EXIT) != 0; | |
39037602 | 7781 | } |
39037602 A |
7782 | } |
7783 | ||
fe8ab488 | 7784 | #endif /* CONFIG_MEMORYSTATUS */ |
39236c6e A |
7785 | |
7786 | /* | |
7787 | * Trigger levels to test the mechanism. | |
7788 | * Can be used via a sysctl. | |
7789 | */ | |
0a7de745 A |
7790 | #define TEST_LOW_MEMORY_TRIGGER_ONE 1 |
7791 | #define TEST_LOW_MEMORY_TRIGGER_ALL 2 | |
7792 | #define TEST_PURGEABLE_TRIGGER_ONE 3 | |
7793 | #define TEST_PURGEABLE_TRIGGER_ALL 4 | |
7794 | #define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE 5 | |
7795 | #define TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL 6 | |
39236c6e | 7796 | |
0a7de745 A |
7797 | boolean_t memorystatus_manual_testing_on = FALSE; |
7798 | vm_pressure_level_t memorystatus_manual_testing_level = kVMPressureNormal; | |
39236c6e A |
7799 | |
7800 | extern struct knote * | |
fe8ab488 | 7801 | vm_pressure_select_optimal_candidate_to_notify(struct klist *, int, boolean_t); |
39236c6e | 7802 | |
39236c6e | 7803 | |
0a7de745 | 7804 | #define VM_PRESSURE_NOTIFY_WAIT_PERIOD 10000 /* milliseconds */ |
39236c6e | 7805 | |
39037602 A |
7806 | #if DEBUG |
7807 | #define VM_PRESSURE_DEBUG(cond, format, ...) \ | |
7808 | do { \ | |
7809 | if (cond) { printf(format, ##__VA_ARGS__); } \ | |
7810 | } while(0) | |
7811 | #else | |
7812 | #define VM_PRESSURE_DEBUG(cond, format, ...) | |
7813 | #endif | |
39236c6e | 7814 | |
0a7de745 | 7815 | #define INTER_NOTIFICATION_DELAY (250000) /* .25 second */ |
39236c6e | 7816 | |
0a7de745 A |
7817 | void |
7818 | memorystatus_on_pageout_scan_end(void) | |
7819 | { | |
39236c6e A |
7820 | /* No-op */ |
7821 | } | |
7822 | ||
7823 | /* | |
7824 | * kn_max - knote | |
7825 | * | |
7826 | * knote_pressure_level - to check if the knote is registered for this notification level. | |
7827 | * | |
7828 | * task - task whose bits we'll be modifying | |
7829 | * | |
7830 | * pressure_level_to_clear - if the task has been notified of this past level, clear that notification bit so that if/when we revert to that level, the task will be notified again. | |
7831 | * | |
7832 | * pressure_level_to_set - the task is about to be notified of this new level. Update the task's bit notification information appropriately. | |
7833 | * | |
7834 | */ | |
39236c6e | 7835 | |
39037602 A |
7836 | boolean_t |
7837 | is_knote_registered_modify_task_pressure_bits(struct knote *kn_max, int knote_pressure_level, task_t task, vm_pressure_level_t pressure_level_to_clear, vm_pressure_level_t pressure_level_to_set) | |
7838 | { | |
7839 | if (kn_max->kn_sfflags & knote_pressure_level) { | |
39037602 | 7840 | if (pressure_level_to_clear && task_has_been_notified(task, pressure_level_to_clear) == TRUE) { |
39037602 A |
7841 | task_clear_has_been_notified(task, pressure_level_to_clear); |
7842 | } | |
7843 | ||
7844 | task_mark_has_been_notified(task, pressure_level_to_set); | |
7845 | return TRUE; | |
7846 | } | |
7847 | ||
7848 | return FALSE; | |
7849 | } | |
7850 | ||
7851 | void | |
7852 | memorystatus_klist_reset_all_for_level(vm_pressure_level_t pressure_level_to_clear) | |
7853 | { | |
7854 | struct knote *kn = NULL; | |
7855 | ||
7856 | memorystatus_klist_lock(); | |
0a7de745 A |
7857 | SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) { |
7858 | proc_t p = PROC_NULL; | |
7859 | struct task* t = TASK_NULL; | |
39037602 A |
7860 | |
7861 | p = knote_get_kq(kn)->kq_p; | |
7862 | proc_list_lock(); | |
7863 | if (p != proc_ref_locked(p)) { | |
7864 | p = PROC_NULL; | |
7865 | proc_list_unlock(); | |
7866 | continue; | |
7867 | } | |
7868 | proc_list_unlock(); | |
7869 | ||
7870 | t = (struct task *)(p->task); | |
7871 | ||
7872 | task_clear_has_been_notified(t, pressure_level_to_clear); | |
7873 | ||
7874 | proc_rele(p); | |
7875 | } | |
7876 | ||
7877 | memorystatus_klist_unlock(); | |
7878 | } | |
7879 | ||
7880 | extern kern_return_t vm_pressure_notify_dispatch_vm_clients(boolean_t target_foreground_process); | |
7881 | ||
7882 | struct knote * | |
7883 | vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int level, boolean_t target_foreground_process); | |
7884 | ||
7885 | /* | |
7886 | * Used by the vm_pressure_thread which is | |
7887 | * signalled from within vm_pageout_scan(). | |
7888 | */ | |
7889 | static void vm_dispatch_memory_pressure(void); | |
7890 | void consider_vm_pressure_events(void); | |
7891 | ||
0a7de745 A |
7892 | void |
7893 | consider_vm_pressure_events(void) | |
39037602 A |
7894 | { |
7895 | vm_dispatch_memory_pressure(); | |
7896 | } | |
0a7de745 A |
7897 | static void |
7898 | vm_dispatch_memory_pressure(void) | |
39037602 A |
7899 | { |
7900 | memorystatus_update_vm_pressure(FALSE); | |
7901 | } | |
7902 | ||
7903 | extern vm_pressure_level_t | |
7904 | convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t); | |
7905 | ||
7906 | struct knote * | |
7907 | vm_pressure_select_optimal_candidate_to_notify(struct klist *candidate_list, int level, boolean_t target_foreground_process) | |
7908 | { | |
0a7de745 A |
7909 | struct knote *kn = NULL, *kn_max = NULL; |
7910 | uint64_t resident_max = 0; /* MB */ | |
7911 | struct timeval curr_tstamp = {0, 0}; | |
7912 | int elapsed_msecs = 0; | |
7913 | int selected_task_importance = 0; | |
7914 | static int pressure_snapshot = -1; | |
7915 | boolean_t pressure_increase = FALSE; | |
39037602 A |
7916 | |
7917 | if (pressure_snapshot == -1) { | |
7918 | /* | |
7919 | * Initial snapshot. | |
7920 | */ | |
7921 | pressure_snapshot = level; | |
7922 | pressure_increase = TRUE; | |
7923 | } else { | |
5ba3f43e | 7924 | if (level && (level >= pressure_snapshot)) { |
39037602 A |
7925 | pressure_increase = TRUE; |
7926 | } else { | |
7927 | pressure_increase = FALSE; | |
7928 | } | |
7929 | ||
7930 | pressure_snapshot = level; | |
7931 | } | |
7932 | ||
7933 | if (pressure_increase == TRUE) { | |
7934 | /* | |
7935 | * We'll start by considering the largest | |
7936 | * unimportant task in our list. | |
7937 | */ | |
7938 | selected_task_importance = INT_MAX; | |
7939 | } else { | |
7940 | /* | |
7941 | * We'll start by considering the largest | |
7942 | * important task in our list. | |
7943 | */ | |
7944 | selected_task_importance = 0; | |
7945 | } | |
7946 | ||
7947 | microuptime(&curr_tstamp); | |
7948 | ||
0a7de745 A |
7949 | SLIST_FOREACH(kn, candidate_list, kn_selnext) { |
7950 | uint64_t resident_size = 0; /* MB */ | |
7951 | proc_t p = PROC_NULL; | |
7952 | struct task* t = TASK_NULL; | |
7953 | int curr_task_importance = 0; | |
7954 | boolean_t consider_knote = FALSE; | |
7955 | boolean_t privileged_listener = FALSE; | |
39037602 A |
7956 | |
7957 | p = knote_get_kq(kn)->kq_p; | |
7958 | proc_list_lock(); | |
7959 | if (p != proc_ref_locked(p)) { | |
7960 | p = PROC_NULL; | |
7961 | proc_list_unlock(); | |
7962 | continue; | |
7963 | } | |
7964 | proc_list_unlock(); | |
7965 | ||
7966 | #if CONFIG_MEMORYSTATUS | |
7967 | if (target_foreground_process == TRUE && !memorystatus_is_foreground_locked(p)) { | |
7968 | /* | |
7969 | * Skip process not marked foreground. | |
7970 | */ | |
7971 | proc_rele(p); | |
7972 | continue; | |
7973 | } | |
7974 | #endif /* CONFIG_MEMORYSTATUS */ | |
7975 | ||
7976 | t = (struct task *)(p->task); | |
7977 | ||
7978 | timevalsub(&curr_tstamp, &p->vm_pressure_last_notify_tstamp); | |
7979 | elapsed_msecs = curr_tstamp.tv_sec * 1000 + curr_tstamp.tv_usec / 1000; | |
7980 | ||
7981 | vm_pressure_level_t dispatch_level = convert_internal_pressure_level_to_dispatch_level(level); | |
7982 | ||
7983 | if ((kn->kn_sfflags & dispatch_level) == 0) { | |
7984 | proc_rele(p); | |
7985 | continue; | |
7986 | } | |
7987 | ||
7988 | #if CONFIG_MEMORYSTATUS | |
7989 | if (target_foreground_process == FALSE && !memorystatus_bg_pressure_eligible(p)) { | |
7990 | VM_PRESSURE_DEBUG(1, "[vm_pressure] skipping process %d\n", p->p_pid); | |
7991 | proc_rele(p); | |
7992 | continue; | |
7993 | } | |
7994 | #endif /* CONFIG_MEMORYSTATUS */ | |
7995 | ||
5ba3f43e A |
7996 | #if CONFIG_EMBEDDED |
7997 | curr_task_importance = p->p_memstat_effectivepriority; | |
7998 | #else /* CONFIG_EMBEDDED */ | |
39037602 | 7999 | curr_task_importance = task_importance_estimate(t); |
5ba3f43e | 8000 | #endif /* CONFIG_EMBEDDED */ |
39037602 A |
8001 | |
8002 | /* | |
8003 | * Privileged listeners are only considered in the multi-level pressure scheme | |
8004 | * AND only if the pressure is increasing. | |
8005 | */ | |
8006 | if (level > 0) { | |
39037602 | 8007 | if (task_has_been_notified(t, level) == FALSE) { |
39037602 A |
8008 | /* |
8009 | * Is this a privileged listener? | |
8010 | */ | |
8011 | if (task_low_mem_privileged_listener(t, FALSE, &privileged_listener) == 0) { | |
39037602 A |
8012 | if (privileged_listener) { |
8013 | kn_max = kn; | |
8014 | proc_rele(p); | |
8015 | goto done_scanning; | |
8016 | } | |
8017 | } | |
8018 | } else { | |
8019 | proc_rele(p); | |
8020 | continue; | |
8021 | } | |
8022 | } else if (level == 0) { | |
39037602 A |
8023 | /* |
8024 | * Task wasn't notified when the pressure was increasing and so | |
8025 | * no need to notify it that the pressure is decreasing. | |
8026 | */ | |
8027 | if ((task_has_been_notified(t, kVMPressureWarning) == FALSE) && (task_has_been_notified(t, kVMPressureCritical) == FALSE)) { | |
8028 | proc_rele(p); | |
8029 | continue; | |
8030 | } | |
8031 | } | |
8032 | ||
8033 | /* | |
0a7de745 A |
8034 | * We don't want a small process to block large processes from |
8035 | * being notified again. <rdar://problem/7955532> | |
8036 | */ | |
8037 | resident_size = (get_task_phys_footprint(t)) / (1024 * 1024ULL); /* MB */ | |
39037602 | 8038 | |
0a7de745 | 8039 | if (resident_size >= vm_pressure_task_footprint_min) { |
39037602 A |
8040 | if (level > 0) { |
8041 | /* | |
8042 | * Warning or Critical Pressure. | |
8043 | */ | |
0a7de745 | 8044 | if (pressure_increase) { |
39037602 A |
8045 | if ((curr_task_importance < selected_task_importance) || |
8046 | ((curr_task_importance == selected_task_importance) && (resident_size > resident_max))) { | |
39037602 A |
8047 | /* |
8048 | * We have found a candidate process which is: | |
8049 | * a) at a lower importance than the current selected process | |
8050 | * OR | |
8051 | * b) has importance equal to that of the current selected process but is larger | |
8052 | */ | |
39236c6e | 8053 | |
39037602 A |
8054 | consider_knote = TRUE; |
8055 | } | |
8056 | } else { | |
8057 | if ((curr_task_importance > selected_task_importance) || | |
8058 | ((curr_task_importance == selected_task_importance) && (resident_size > resident_max))) { | |
39037602 A |
8059 | /* |
8060 | * We have found a candidate process which is: | |
8061 | * a) at a higher importance than the current selected process | |
8062 | * OR | |
8063 | * b) has importance equal to that of the current selected process but is larger | |
8064 | */ | |
39236c6e | 8065 | |
39037602 A |
8066 | consider_knote = TRUE; |
8067 | } | |
8068 | } | |
8069 | } else if (level == 0) { | |
8070 | /* | |
8071 | * Pressure back to normal. | |
8072 | */ | |
8073 | if ((curr_task_importance > selected_task_importance) || | |
8074 | ((curr_task_importance == selected_task_importance) && (resident_size > resident_max))) { | |
39037602 A |
8075 | consider_knote = TRUE; |
8076 | } | |
8077 | } | |
8078 | ||
8079 | if (consider_knote) { | |
8080 | resident_max = resident_size; | |
8081 | kn_max = kn; | |
8082 | selected_task_importance = curr_task_importance; | |
8083 | consider_knote = FALSE; /* reset for the next candidate */ | |
8084 | } | |
0a7de745 A |
8085 | } else { |
8086 | /* There was no candidate with enough resident memory to scavenge */ | |
8087 | VM_PRESSURE_DEBUG(0, "[vm_pressure] threshold failed for pid %d with %llu resident...\n", p->p_pid, resident_size); | |
8088 | } | |
39037602 | 8089 | proc_rele(p); |
0a7de745 | 8090 | } |
39037602 A |
8091 | |
8092 | done_scanning: | |
8093 | if (kn_max) { | |
8094 | VM_DEBUG_CONSTANT_EVENT(vm_pressure_event, VM_PRESSURE_EVENT, DBG_FUNC_NONE, knote_get_kq(kn_max)->kq_p->p_pid, resident_max, 0, 0); | |
8095 | VM_PRESSURE_DEBUG(1, "[vm_pressure] sending event to pid %d with %llu resident\n", knote_get_kq(kn_max)->kq_p->p_pid, resident_max); | |
39236c6e A |
8096 | } |
8097 | ||
39037602 | 8098 | return kn_max; |
39236c6e A |
8099 | } |
8100 | ||
0a7de745 A |
8101 | #define VM_PRESSURE_DECREASED_SMOOTHING_PERIOD 5000 /* milliseconds */ |
8102 | #define WARNING_NOTIFICATION_RESTING_PERIOD 25 /* seconds */ | |
8103 | #define CRITICAL_NOTIFICATION_RESTING_PERIOD 25 /* seconds */ | |
39037602 A |
8104 | |
8105 | uint64_t next_warning_notification_sent_at_ts = 0; | |
8106 | uint64_t next_critical_notification_sent_at_ts = 0; | |
39236c6e A |
8107 | |
8108 | kern_return_t | |
0a7de745 A |
8109 | memorystatus_update_vm_pressure(boolean_t target_foreground_process) |
8110 | { | |
8111 | struct knote *kn_max = NULL; | |
8112 | struct knote *kn_cur = NULL, *kn_temp = NULL; /* for safe list traversal */ | |
8113 | pid_t target_pid = -1; | |
8114 | struct klist dispatch_klist = { NULL }; | |
8115 | proc_t target_proc = PROC_NULL; | |
8116 | struct task *task = NULL; | |
8117 | boolean_t found_candidate = FALSE; | |
8118 | ||
8119 | static vm_pressure_level_t level_snapshot = kVMPressureNormal; | |
8120 | static vm_pressure_level_t prev_level_snapshot = kVMPressureNormal; | |
8121 | boolean_t smoothing_window_started = FALSE; | |
8122 | struct timeval smoothing_window_start_tstamp = {0, 0}; | |
8123 | struct timeval curr_tstamp = {0, 0}; | |
8124 | int elapsed_msecs = 0; | |
8125 | uint64_t curr_ts = mach_absolute_time(); | |
fe8ab488 A |
8126 | |
8127 | #if !CONFIG_JETSAM | |
0a7de745 | 8128 | #define MAX_IDLE_KILLS 100 /* limit the number of idle kills allowed */ |
fe8ab488 | 8129 | |
0a7de745 | 8130 | int idle_kill_counter = 0; |
fe8ab488 A |
8131 | |
8132 | /* | |
8133 | * On desktop we take this opportunity to free up memory pressure | |
8134 | * by immediately killing idle exitable processes. We use a delay | |
8135 | * to avoid overkill. And we impose a max counter as a fail safe | |
8136 | * in case daemons re-launch too fast. | |
8137 | */ | |
8138 | while ((memorystatus_vm_pressure_level != kVMPressureNormal) && (idle_kill_counter < MAX_IDLE_KILLS)) { | |
8139 | if (memorystatus_idle_exit_from_VM() == FALSE) { | |
8140 | /* No idle exitable processes left to kill */ | |
8141 | break; | |
8142 | } | |
8143 | idle_kill_counter++; | |
3e170ce0 A |
8144 | |
8145 | if (memorystatus_manual_testing_on == TRUE) { | |
8146 | /* | |
8147 | * Skip the delay when testing | |
8148 | * the pressure notification scheme. | |
8149 | */ | |
8150 | } else { | |
8151 | delay(1000000); /* 1 second */ | |
8152 | } | |
fe8ab488 A |
8153 | } |
8154 | #endif /* !CONFIG_JETSAM */ | |
8155 | ||
39037602 | 8156 | if (level_snapshot != kVMPressureNormal) { |
39037602 A |
8157 | /* |
8158 | * Check to see if we are still in the 'resting' period | |
8159 | * after having notified all clients interested in | |
8160 | * a particular pressure level. | |
8161 | */ | |
8162 | ||
8163 | level_snapshot = memorystatus_vm_pressure_level; | |
8164 | ||
8165 | if (level_snapshot == kVMPressureWarning || level_snapshot == kVMPressureUrgent) { | |
5ba3f43e A |
8166 | if (next_warning_notification_sent_at_ts) { |
8167 | if (curr_ts < next_warning_notification_sent_at_ts) { | |
8168 | delay(INTER_NOTIFICATION_DELAY * 4 /* 1 sec */); | |
8169 | return KERN_SUCCESS; | |
8170 | } | |
8171 | ||
8172 | next_warning_notification_sent_at_ts = 0; | |
8173 | memorystatus_klist_reset_all_for_level(kVMPressureWarning); | |
39037602 A |
8174 | } |
8175 | } else if (level_snapshot == kVMPressureCritical) { | |
5ba3f43e A |
8176 | if (next_critical_notification_sent_at_ts) { |
8177 | if (curr_ts < next_critical_notification_sent_at_ts) { | |
8178 | delay(INTER_NOTIFICATION_DELAY * 4 /* 1 sec */); | |
8179 | return KERN_SUCCESS; | |
8180 | } | |
8181 | next_critical_notification_sent_at_ts = 0; | |
8182 | memorystatus_klist_reset_all_for_level(kVMPressureCritical); | |
39037602 A |
8183 | } |
8184 | } | |
8185 | } | |
8186 | ||
39236c6e | 8187 | while (1) { |
39236c6e A |
8188 | /* |
8189 | * There is a race window here. But it's not clear | |
8190 | * how much we benefit from having extra synchronization. | |
8191 | */ | |
8192 | level_snapshot = memorystatus_vm_pressure_level; | |
8193 | ||
fe8ab488 A |
8194 | if (prev_level_snapshot > level_snapshot) { |
8195 | /* | |
8196 | * Pressure decreased? Let's take a little breather | |
8197 | * and see if this condition stays. | |
8198 | */ | |
8199 | if (smoothing_window_started == FALSE) { | |
fe8ab488 A |
8200 | smoothing_window_started = TRUE; |
8201 | microuptime(&smoothing_window_start_tstamp); | |
8202 | } | |
8203 | ||
8204 | microuptime(&curr_tstamp); | |
8205 | timevalsub(&curr_tstamp, &smoothing_window_start_tstamp); | |
8206 | elapsed_msecs = curr_tstamp.tv_sec * 1000 + curr_tstamp.tv_usec / 1000; | |
8207 | ||
8208 | if (elapsed_msecs < VM_PRESSURE_DECREASED_SMOOTHING_PERIOD) { | |
fe8ab488 A |
8209 | delay(INTER_NOTIFICATION_DELAY); |
8210 | continue; | |
8211 | } | |
8212 | } | |
8213 | ||
8214 | prev_level_snapshot = level_snapshot; | |
8215 | smoothing_window_started = FALSE; | |
8216 | ||
39236c6e | 8217 | memorystatus_klist_lock(); |
fe8ab488 | 8218 | kn_max = vm_pressure_select_optimal_candidate_to_notify(&memorystatus_klist, level_snapshot, target_foreground_process); |
39236c6e | 8219 | |
0a7de745 | 8220 | if (kn_max == NULL) { |
39236c6e A |
8221 | memorystatus_klist_unlock(); |
8222 | ||
8223 | /* | |
8224 | * No more level-based clients to notify. | |
39236c6e | 8225 | * |
39037602 | 8226 | * Start the 'resting' window within which clients will not be re-notified. |
39236c6e A |
8227 | */ |
8228 | ||
8229 | if (level_snapshot != kVMPressureNormal) { | |
39037602 A |
8230 | if (level_snapshot == kVMPressureWarning || level_snapshot == kVMPressureUrgent) { |
8231 | nanoseconds_to_absolutetime(WARNING_NOTIFICATION_RESTING_PERIOD * NSEC_PER_SEC, &curr_ts); | |
39037602 | 8232 | |
5ba3f43e A |
8233 | /* Next warning notification (if nothing changes) won't be sent before...*/ |
8234 | next_warning_notification_sent_at_ts = mach_absolute_time() + curr_ts; | |
39037602 A |
8235 | } |
8236 | ||
8237 | if (level_snapshot == kVMPressureCritical) { | |
8238 | nanoseconds_to_absolutetime(CRITICAL_NOTIFICATION_RESTING_PERIOD * NSEC_PER_SEC, &curr_ts); | |
39037602 | 8239 | |
5ba3f43e A |
8240 | /* Next critical notification (if nothing changes) won't be sent before...*/ |
8241 | next_critical_notification_sent_at_ts = mach_absolute_time() + curr_ts; | |
39037602 A |
8242 | } |
8243 | } | |
8244 | return KERN_FAILURE; | |
39236c6e | 8245 | } |
0a7de745 | 8246 | |
39037602 | 8247 | target_proc = knote_get_kq(kn_max)->kq_p; |
0a7de745 | 8248 | |
39236c6e A |
8249 | proc_list_lock(); |
8250 | if (target_proc != proc_ref_locked(target_proc)) { | |
8251 | target_proc = PROC_NULL; | |
8252 | proc_list_unlock(); | |
8253 | memorystatus_klist_unlock(); | |
8254 | continue; | |
8255 | } | |
8256 | proc_list_unlock(); | |
0a7de745 | 8257 | |
39236c6e A |
8258 | target_pid = target_proc->p_pid; |
8259 | ||
8260 | task = (struct task *)(target_proc->task); | |
39236c6e | 8261 | |
0a7de745 | 8262 | if (level_snapshot != kVMPressureNormal) { |
39236c6e | 8263 | if (level_snapshot == kVMPressureWarning || level_snapshot == kVMPressureUrgent) { |
39037602 | 8264 | if (is_knote_registered_modify_task_pressure_bits(kn_max, NOTE_MEMORYSTATUS_PRESSURE_WARN, task, 0, kVMPressureWarning) == TRUE) { |
39236c6e A |
8265 | found_candidate = TRUE; |
8266 | } | |
8267 | } else { | |
8268 | if (level_snapshot == kVMPressureCritical) { | |
39037602 | 8269 | if (is_knote_registered_modify_task_pressure_bits(kn_max, NOTE_MEMORYSTATUS_PRESSURE_CRITICAL, task, 0, kVMPressureCritical) == TRUE) { |
39236c6e A |
8270 | found_candidate = TRUE; |
8271 | } | |
8272 | } | |
8273 | } | |
8274 | } else { | |
8275 | if (kn_max->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { | |
39236c6e A |
8276 | task_clear_has_been_notified(task, kVMPressureWarning); |
8277 | task_clear_has_been_notified(task, kVMPressureCritical); | |
8278 | ||
8279 | found_candidate = TRUE; | |
6d2010ae A |
8280 | } |
8281 | } | |
39236c6e A |
8282 | |
8283 | if (found_candidate == FALSE) { | |
3e170ce0 A |
8284 | proc_rele(target_proc); |
8285 | memorystatus_klist_unlock(); | |
39236c6e A |
8286 | continue; |
8287 | } | |
8288 | ||
3e170ce0 | 8289 | SLIST_FOREACH_SAFE(kn_cur, &memorystatus_klist, kn_selnext, kn_temp) { |
39037602 A |
8290 | int knote_pressure_level = convert_internal_pressure_level_to_dispatch_level(level_snapshot); |
8291 | ||
8292 | if (is_knote_registered_modify_task_pressure_bits(kn_cur, knote_pressure_level, task, 0, level_snapshot) == TRUE) { | |
8293 | proc_t knote_proc = knote_get_kq(kn_cur)->kq_p; | |
8294 | pid_t knote_pid = knote_proc->p_pid; | |
8295 | if (knote_pid == target_pid) { | |
8296 | KNOTE_DETACH(&memorystatus_klist, kn_cur); | |
8297 | KNOTE_ATTACH(&dispatch_klist, kn_cur); | |
8298 | } | |
3e170ce0 A |
8299 | } |
8300 | } | |
39236c6e A |
8301 | |
8302 | KNOTE(&dispatch_klist, (level_snapshot != kVMPressureNormal) ? kMemorystatusPressure : kMemorystatusNoPressure); | |
8303 | ||
3e170ce0 A |
8304 | SLIST_FOREACH_SAFE(kn_cur, &dispatch_klist, kn_selnext, kn_temp) { |
8305 | KNOTE_DETACH(&dispatch_klist, kn_cur); | |
8306 | KNOTE_ATTACH(&memorystatus_klist, kn_cur); | |
8307 | } | |
8308 | ||
39236c6e A |
8309 | memorystatus_klist_unlock(); |
8310 | ||
8311 | microuptime(&target_proc->vm_pressure_last_notify_tstamp); | |
8312 | proc_rele(target_proc); | |
8313 | ||
fe8ab488 | 8314 | if (memorystatus_manual_testing_on == TRUE && target_foreground_process == TRUE) { |
39236c6e A |
8315 | break; |
8316 | } | |
8317 | ||
fe8ab488 A |
8318 | if (memorystatus_manual_testing_on == TRUE) { |
8319 | /* | |
8320 | * Testing out the pressure notification scheme. | |
8321 | * No need for delays etc. | |
8322 | */ | |
8323 | } else { | |
fe8ab488 A |
8324 | uint32_t sleep_interval = INTER_NOTIFICATION_DELAY; |
8325 | #if CONFIG_JETSAM | |
8326 | unsigned int page_delta = 0; | |
8327 | unsigned int skip_delay_page_threshold = 0; | |
8328 | ||
8329 | assert(memorystatus_available_pages_pressure >= memorystatus_available_pages_critical_base); | |
0a7de745 | 8330 | |
fe8ab488 A |
8331 | page_delta = (memorystatus_available_pages_pressure - memorystatus_available_pages_critical_base) / 2; |
8332 | skip_delay_page_threshold = memorystatus_available_pages_pressure - page_delta; | |
8333 | ||
8334 | if (memorystatus_available_pages <= skip_delay_page_threshold) { | |
8335 | /* | |
8336 | * We are nearing the critcal mark fast and can't afford to wait between | |
8337 | * notifications. | |
8338 | */ | |
8339 | sleep_interval = 0; | |
8340 | } | |
8341 | #endif /* CONFIG_JETSAM */ | |
0a7de745 | 8342 | |
fe8ab488 A |
8343 | if (sleep_interval) { |
8344 | delay(sleep_interval); | |
8345 | } | |
39236c6e | 8346 | } |
6d2010ae | 8347 | } |
39236c6e A |
8348 | |
8349 | return KERN_SUCCESS; | |
6d2010ae A |
8350 | } |
8351 | ||
39236c6e A |
8352 | vm_pressure_level_t |
8353 | convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t internal_pressure_level) | |
8354 | { | |
0a7de745 | 8355 | vm_pressure_level_t dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; |
39236c6e | 8356 | |
0a7de745 A |
8357 | switch (internal_pressure_level) { |
8358 | case kVMPressureNormal: | |
8359 | { | |
8360 | dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; | |
8361 | break; | |
8362 | } | |
39236c6e | 8363 | |
0a7de745 A |
8364 | case kVMPressureWarning: |
8365 | case kVMPressureUrgent: | |
8366 | { | |
8367 | dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_WARN; | |
8368 | break; | |
8369 | } | |
39236c6e | 8370 | |
0a7de745 A |
8371 | case kVMPressureCritical: |
8372 | { | |
8373 | dispatch_level = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; | |
8374 | break; | |
8375 | } | |
39236c6e | 8376 | |
0a7de745 A |
8377 | default: |
8378 | break; | |
39236c6e | 8379 | } |
316670eb | 8380 | |
39236c6e A |
8381 | return dispatch_level; |
8382 | } | |
6d2010ae | 8383 | |
b0d623f7 | 8384 | static int |
39236c6e | 8385 | sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS |
b0d623f7 | 8386 | { |
39236c6e | 8387 | #pragma unused(arg1, arg2, oidp) |
5ba3f43e A |
8388 | #if CONFIG_EMBEDDED |
8389 | int error = 0; | |
8390 | ||
8391 | error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0); | |
0a7de745 A |
8392 | if (error) { |
8393 | return error; | |
8394 | } | |
5ba3f43e | 8395 | |
0a7de745 | 8396 | #endif /* CONFIG_EMBEDDED */ |
39236c6e A |
8397 | vm_pressure_level_t dispatch_level = convert_internal_pressure_level_to_dispatch_level(memorystatus_vm_pressure_level); |
8398 | ||
8399 | return SYSCTL_OUT(req, &dispatch_level, sizeof(dispatch_level)); | |
8400 | } | |
8401 | ||
fe8ab488 A |
8402 | #if DEBUG || DEVELOPMENT |
8403 | ||
0a7de745 | 8404 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, |
39236c6e A |
8405 | 0, 0, &sysctl_memorystatus_vm_pressure_level, "I", ""); |
8406 | ||
fe8ab488 A |
8407 | #else /* DEBUG || DEVELOPMENT */ |
8408 | ||
0a7de745 | 8409 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
fe8ab488 A |
8410 | 0, 0, &sysctl_memorystatus_vm_pressure_level, "I", ""); |
8411 | ||
8412 | #endif /* DEBUG || DEVELOPMENT */ | |
b0d623f7 | 8413 | |
39236c6e A |
8414 | |
8415 | static int | |
8416 | sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS | |
8417 | { | |
8418 | #pragma unused(arg1, arg2) | |
b0d623f7 | 8419 | |
39236c6e A |
8420 | int level = 0; |
8421 | int error = 0; | |
8422 | int pressure_level = 0; | |
8423 | int trigger_request = 0; | |
8424 | int force_purge; | |
8425 | ||
8426 | error = sysctl_handle_int(oidp, &level, 0, req); | |
8427 | if (error || !req->newptr) { | |
0a7de745 | 8428 | return error; |
39236c6e A |
8429 | } |
8430 | ||
8431 | memorystatus_manual_testing_on = TRUE; | |
8432 | ||
8433 | trigger_request = (level >> 16) & 0xFFFF; | |
0a7de745 | 8434 | pressure_level = (level & 0xFFFF); |
39236c6e A |
8435 | |
8436 | if (trigger_request < TEST_LOW_MEMORY_TRIGGER_ONE || | |
8437 | trigger_request > TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL) { | |
8438 | return EINVAL; | |
8439 | } | |
8440 | switch (pressure_level) { | |
8441 | case NOTE_MEMORYSTATUS_PRESSURE_NORMAL: | |
8442 | case NOTE_MEMORYSTATUS_PRESSURE_WARN: | |
8443 | case NOTE_MEMORYSTATUS_PRESSURE_CRITICAL: | |
8444 | break; | |
8445 | default: | |
b0d623f7 A |
8446 | return EINVAL; |
8447 | } | |
b0d623f7 | 8448 | |
39236c6e A |
8449 | /* |
8450 | * The pressure level is being set from user-space. | |
8451 | * And user-space uses the constants in sys/event.h | |
8452 | * So we translate those events to our internal levels here. | |
8453 | */ | |
8454 | if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { | |
39236c6e A |
8455 | memorystatus_manual_testing_level = kVMPressureNormal; |
8456 | force_purge = 0; | |
39236c6e | 8457 | } else if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_WARN) { |
39236c6e | 8458 | memorystatus_manual_testing_level = kVMPressureWarning; |
d9a64523 | 8459 | force_purge = vm_pageout_state.memorystatus_purge_on_warning; |
39236c6e | 8460 | } else if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) { |
39236c6e | 8461 | memorystatus_manual_testing_level = kVMPressureCritical; |
d9a64523 | 8462 | force_purge = vm_pageout_state.memorystatus_purge_on_critical; |
b0d623f7 A |
8463 | } |
8464 | ||
39236c6e | 8465 | memorystatus_vm_pressure_level = memorystatus_manual_testing_level; |
316670eb | 8466 | |
39236c6e A |
8467 | /* purge according to the new pressure level */ |
8468 | switch (trigger_request) { | |
8469 | case TEST_PURGEABLE_TRIGGER_ONE: | |
8470 | case TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE: | |
8471 | if (force_purge == 0) { | |
8472 | /* no purging requested */ | |
8473 | break; | |
8474 | } | |
8475 | vm_purgeable_object_purge_one_unlocked(force_purge); | |
8476 | break; | |
8477 | case TEST_PURGEABLE_TRIGGER_ALL: | |
8478 | case TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL: | |
8479 | if (force_purge == 0) { | |
8480 | /* no purging requested */ | |
8481 | break; | |
8482 | } | |
0a7de745 A |
8483 | while (vm_purgeable_object_purge_one_unlocked(force_purge)) { |
8484 | ; | |
8485 | } | |
39236c6e A |
8486 | break; |
8487 | } | |
8488 | ||
8489 | if ((trigger_request == TEST_LOW_MEMORY_TRIGGER_ONE) || | |
8490 | (trigger_request == TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ONE)) { | |
39236c6e A |
8491 | memorystatus_update_vm_pressure(TRUE); |
8492 | } | |
8493 | ||
8494 | if ((trigger_request == TEST_LOW_MEMORY_TRIGGER_ALL) || | |
8495 | (trigger_request == TEST_LOW_MEMORY_PURGEABLE_TRIGGER_ALL)) { | |
39236c6e A |
8496 | while (memorystatus_update_vm_pressure(FALSE) == KERN_SUCCESS) { |
8497 | continue; | |
8498 | } | |
8499 | } | |
0a7de745 | 8500 | |
39236c6e A |
8501 | if (pressure_level == NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { |
8502 | memorystatus_manual_testing_on = FALSE; | |
39236c6e A |
8503 | } |
8504 | ||
8505 | return 0; | |
b0d623f7 A |
8506 | } |
8507 | ||
0a7de745 | 8508 | SYSCTL_PROC(_kern, OID_AUTO, memorypressure_manual_trigger, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
39236c6e A |
8509 | 0, 0, &sysctl_memorypressure_manual_trigger, "I", ""); |
8510 | ||
8511 | ||
0a7de745 A |
8512 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_warning, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.memorystatus_purge_on_warning, 0, ""); |
8513 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_urgent, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.memorystatus_purge_on_urgent, 0, ""); | |
8514 | SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_critical, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pageout_state.memorystatus_purge_on_critical, 0, ""); | |
39236c6e | 8515 | |
d9a64523 | 8516 | #if DEBUG || DEVELOPMENT |
0a7de745 | 8517 | SYSCTL_UINT(_kern, OID_AUTO, memorystatus_vm_pressure_events_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &vm_pressure_events_enabled, 0, ""); |
d9a64523 | 8518 | #endif |
39236c6e | 8519 | |
fe8ab488 | 8520 | #endif /* VM_PRESSURE_EVENTS */ |
39236c6e A |
8521 | |
8522 | /* Return both allocated and actual size, since there's a race between allocation and list compilation */ | |
b0d623f7 | 8523 | static int |
0a7de745 | 8524 | memorystatus_get_priority_list(memorystatus_priority_entry_t **list_ptr, size_t *buffer_size, size_t *list_size, boolean_t size_only) |
b0d623f7 | 8525 | { |
0a7de745 | 8526 | uint32_t list_count, i = 0; |
39236c6e A |
8527 | memorystatus_priority_entry_t *list_entry; |
8528 | proc_t p; | |
8529 | ||
0a7de745 | 8530 | list_count = memorystatus_list_count; |
39236c6e A |
8531 | *list_size = sizeof(memorystatus_priority_entry_t) * list_count; |
8532 | ||
8533 | /* Just a size check? */ | |
8534 | if (size_only) { | |
8535 | return 0; | |
8536 | } | |
0a7de745 | 8537 | |
39236c6e A |
8538 | /* Otherwise, validate the size of the buffer */ |
8539 | if (*buffer_size < *list_size) { | |
8540 | return EINVAL; | |
8541 | } | |
8542 | ||
0a7de745 | 8543 | *list_ptr = (memorystatus_priority_entry_t*)kalloc(*list_size); |
a39ff7e2 | 8544 | if (!*list_ptr) { |
316670eb A |
8545 | return ENOMEM; |
8546 | } | |
8547 | ||
39236c6e A |
8548 | memset(*list_ptr, 0, *list_size); |
8549 | ||
8550 | *buffer_size = *list_size; | |
8551 | *list_size = 0; | |
8552 | ||
8553 | list_entry = *list_ptr; | |
8554 | ||
8555 | proc_list_lock(); | |
8556 | ||
8557 | p = memorystatus_get_first_proc_locked(&i, TRUE); | |
8558 | while (p && (*list_size < *buffer_size)) { | |
8559 | list_entry->pid = p->p_pid; | |
8560 | list_entry->priority = p->p_memstat_effectivepriority; | |
8561 | list_entry->user_data = p->p_memstat_userdata; | |
3e170ce0 | 8562 | |
3e170ce0 | 8563 | if (p->p_memstat_memlimit <= 0) { |
0a7de745 A |
8564 | task_get_phys_footprint_limit(p->task, &list_entry->limit); |
8565 | } else { | |
8566 | list_entry->limit = p->p_memstat_memlimit; | |
8567 | } | |
39037602 | 8568 | |
39236c6e A |
8569 | list_entry->state = memorystatus_build_state(p); |
8570 | list_entry++; | |
8571 | ||
8572 | *list_size += sizeof(memorystatus_priority_entry_t); | |
0a7de745 | 8573 | |
39236c6e | 8574 | p = memorystatus_get_next_proc_locked(&i, p, TRUE); |
316670eb | 8575 | } |
0a7de745 | 8576 | |
39236c6e | 8577 | proc_list_unlock(); |
0a7de745 | 8578 | |
39236c6e | 8579 | MEMORYSTATUS_DEBUG(1, "memorystatus_get_priority_list: returning %lu for size\n", (unsigned long)*list_size); |
0a7de745 | 8580 | |
39236c6e A |
8581 | return 0; |
8582 | } | |
b0d623f7 | 8583 | |
39236c6e | 8584 | static int |
0a7de745 A |
8585 | memorystatus_get_priority_pid(pid_t pid, user_addr_t buffer, size_t buffer_size) |
8586 | { | |
8587 | int error = 0; | |
8588 | memorystatus_priority_entry_t mp_entry; | |
5ba3f43e | 8589 | |
0a7de745 A |
8590 | /* Validate inputs */ |
8591 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_entry_t))) { | |
8592 | return EINVAL; | |
8593 | } | |
5ba3f43e A |
8594 | |
8595 | proc_t p = proc_find(pid); | |
0a7de745 A |
8596 | if (!p) { |
8597 | return ESRCH; | |
8598 | } | |
5ba3f43e | 8599 | |
0a7de745 | 8600 | memset(&mp_entry, 0, sizeof(memorystatus_priority_entry_t)); |
5ba3f43e | 8601 | |
0a7de745 A |
8602 | mp_entry.pid = p->p_pid; |
8603 | mp_entry.priority = p->p_memstat_effectivepriority; | |
8604 | mp_entry.user_data = p->p_memstat_userdata; | |
8605 | if (p->p_memstat_memlimit <= 0) { | |
8606 | task_get_phys_footprint_limit(p->task, &mp_entry.limit); | |
8607 | } else { | |
8608 | mp_entry.limit = p->p_memstat_memlimit; | |
8609 | } | |
8610 | mp_entry.state = memorystatus_build_state(p); | |
5ba3f43e | 8611 | |
0a7de745 | 8612 | proc_rele(p); |
5ba3f43e | 8613 | |
0a7de745 | 8614 | error = copyout(&mp_entry, buffer, buffer_size); |
5ba3f43e | 8615 | |
0a7de745 | 8616 | return error; |
5ba3f43e A |
8617 | } |
8618 | ||
8619 | static int | |
0a7de745 A |
8620 | memorystatus_cmd_get_priority_list(pid_t pid, user_addr_t buffer, size_t buffer_size, int32_t *retval) |
8621 | { | |
5ba3f43e | 8622 | int error = 0; |
39236c6e | 8623 | boolean_t size_only; |
39236c6e | 8624 | size_t list_size; |
5ba3f43e A |
8625 | |
8626 | /* | |
8627 | * When a non-zero pid is provided, the 'list' has only one entry. | |
8628 | */ | |
0a7de745 | 8629 | |
39236c6e | 8630 | size_only = ((buffer == USER_ADDR_NULL) ? TRUE: FALSE); |
39236c6e | 8631 | |
5ba3f43e A |
8632 | if (pid != 0) { |
8633 | list_size = sizeof(memorystatus_priority_entry_t) * 1; | |
8634 | if (!size_only) { | |
8635 | error = memorystatus_get_priority_pid(pid, buffer, buffer_size); | |
8636 | } | |
8637 | } else { | |
8638 | memorystatus_priority_entry_t *list = NULL; | |
8639 | error = memorystatus_get_priority_list(&list, &buffer_size, &list_size, size_only); | |
8640 | ||
8641 | if (error == 0) { | |
8642 | if (!size_only) { | |
8643 | error = copyout(list, buffer, list_size); | |
8644 | } | |
8645 | } | |
8646 | ||
8647 | if (list) { | |
8648 | kfree(list, buffer_size); | |
8649 | } | |
39236c6e | 8650 | } |
5ba3f43e | 8651 | |
39236c6e A |
8652 | if (error == 0) { |
8653 | *retval = list_size; | |
8654 | } | |
39236c6e | 8655 | |
0a7de745 | 8656 | return error; |
316670eb | 8657 | } |
b0d623f7 | 8658 | |
0a7de745 | 8659 | static void |
39236c6e A |
8660 | memorystatus_clear_errors(void) |
8661 | { | |
8662 | proc_t p; | |
8663 | unsigned int i = 0; | |
8664 | ||
8665 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_START, 0, 0, 0, 0, 0); | |
0a7de745 | 8666 | |
39236c6e | 8667 | proc_list_lock(); |
0a7de745 | 8668 | |
39236c6e A |
8669 | p = memorystatus_get_first_proc_locked(&i, TRUE); |
8670 | while (p) { | |
8671 | if (p->p_memstat_state & P_MEMSTAT_ERROR) { | |
8672 | p->p_memstat_state &= ~P_MEMSTAT_ERROR; | |
8673 | } | |
8674 | p = memorystatus_get_next_proc_locked(&i, p, TRUE); | |
8675 | } | |
0a7de745 | 8676 | |
39236c6e A |
8677 | proc_list_unlock(); |
8678 | ||
8679 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_END, 0, 0, 0, 0, 0); | |
8680 | } | |
b0d623f7 | 8681 | |
5ba3f43e | 8682 | #if CONFIG_JETSAM |
316670eb | 8683 | static void |
0a7de745 A |
8684 | memorystatus_update_levels_locked(boolean_t critical_only) |
8685 | { | |
39236c6e | 8686 | memorystatus_available_pages_critical = memorystatus_available_pages_critical_base; |
fe8ab488 A |
8687 | |
8688 | /* | |
8689 | * If there's an entry in the first bucket, we have idle processes. | |
8690 | */ | |
39037602 | 8691 | |
fe8ab488 A |
8692 | memstat_bucket_t *first_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; |
8693 | if (first_bucket->count) { | |
8694 | memorystatus_available_pages_critical += memorystatus_available_pages_critical_idle_offset; | |
8695 | ||
0a7de745 A |
8696 | if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure) { |
8697 | /* | |
fe8ab488 A |
8698 | * The critical threshold must never exceed the pressure threshold |
8699 | */ | |
8700 | memorystatus_available_pages_critical = memorystatus_available_pages_pressure; | |
39236c6e A |
8701 | } |
8702 | } | |
fe8ab488 | 8703 | |
316670eb A |
8704 | #if DEBUG || DEVELOPMENT |
8705 | if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) { | |
8706 | memorystatus_available_pages_critical += memorystatus_jetsam_policy_offset_pages_diagnostic; | |
fe8ab488 | 8707 | |
0a7de745 A |
8708 | if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure) { |
8709 | /* | |
fe8ab488 A |
8710 | * The critical threshold must never exceed the pressure threshold |
8711 | */ | |
8712 | memorystatus_available_pages_critical = memorystatus_available_pages_pressure; | |
8713 | } | |
39236c6e | 8714 | } |
5ba3f43e | 8715 | #endif /* DEBUG || DEVELOPMENT */ |
39037602 A |
8716 | |
8717 | if (memorystatus_jetsam_policy & kPolicyMoreFree) { | |
8718 | memorystatus_available_pages_critical += memorystatus_policy_more_free_offset_pages; | |
8719 | } | |
8720 | ||
39236c6e A |
8721 | if (critical_only) { |
8722 | return; | |
8723 | } | |
0a7de745 | 8724 | |
316670eb | 8725 | #if VM_PRESSURE_EVENTS |
39236c6e A |
8726 | memorystatus_available_pages_pressure = (pressure_threshold_percentage / delta_percentage) * memorystatus_delta; |
8727 | #if DEBUG || DEVELOPMENT | |
8728 | if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) { | |
316670eb | 8729 | memorystatus_available_pages_pressure += memorystatus_jetsam_policy_offset_pages_diagnostic; |
316670eb A |
8730 | } |
8731 | #endif | |
39236c6e A |
8732 | #endif |
8733 | } | |
8734 | ||
d9a64523 A |
8735 | void |
8736 | memorystatus_fast_jetsam_override(boolean_t enable_override) | |
8737 | { | |
8738 | /* If fast jetsam is not enabled, simply return */ | |
0a7de745 | 8739 | if (!fast_jetsam_enabled) { |
d9a64523 | 8740 | return; |
0a7de745 | 8741 | } |
d9a64523 A |
8742 | |
8743 | if (enable_override) { | |
0a7de745 | 8744 | if ((memorystatus_jetsam_policy & kPolicyMoreFree) == kPolicyMoreFree) { |
d9a64523 | 8745 | return; |
0a7de745 | 8746 | } |
d9a64523 A |
8747 | proc_list_lock(); |
8748 | memorystatus_jetsam_policy |= kPolicyMoreFree; | |
8749 | memorystatus_thread_pool_max(); | |
8750 | memorystatus_update_levels_locked(TRUE); | |
8751 | proc_list_unlock(); | |
8752 | } else { | |
0a7de745 | 8753 | if ((memorystatus_jetsam_policy & kPolicyMoreFree) == 0) { |
d9a64523 | 8754 | return; |
0a7de745 | 8755 | } |
d9a64523 A |
8756 | proc_list_lock(); |
8757 | memorystatus_jetsam_policy &= ~kPolicyMoreFree; | |
8758 | memorystatus_thread_pool_default(); | |
8759 | memorystatus_update_levels_locked(TRUE); | |
8760 | proc_list_unlock(); | |
8761 | } | |
8762 | } | |
8763 | ||
5ba3f43e | 8764 | |
39037602 A |
8765 | static int |
8766 | sysctl_kern_memorystatus_policy_more_free SYSCTL_HANDLER_ARGS | |
8767 | { | |
8768 | #pragma unused(arg1, arg2, oidp) | |
8769 | int error = 0, more_free = 0; | |
8770 | ||
8771 | /* | |
8772 | * TODO: Enable this privilege check? | |
8773 | * | |
8774 | * error = priv_check_cred(kauth_cred_get(), PRIV_VM_JETSAM, 0); | |
8775 | * if (error) | |
8776 | * return (error); | |
8777 | */ | |
8778 | ||
8779 | error = sysctl_handle_int(oidp, &more_free, 0, req); | |
0a7de745 A |
8780 | if (error || !req->newptr) { |
8781 | return error; | |
8782 | } | |
39037602 | 8783 | |
39037602 | 8784 | if (more_free) { |
d9a64523 | 8785 | memorystatus_fast_jetsam_override(true); |
39037602 | 8786 | } else { |
d9a64523 | 8787 | memorystatus_fast_jetsam_override(false); |
39037602 A |
8788 | } |
8789 | ||
39037602 A |
8790 | return 0; |
8791 | } | |
0a7de745 | 8792 | SYSCTL_PROC(_kern, OID_AUTO, memorystatus_policy_more_free, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, |
39037602 A |
8793 | 0, 0, &sysctl_kern_memorystatus_policy_more_free, "I", ""); |
8794 | ||
5ba3f43e A |
8795 | #endif /* CONFIG_JETSAM */ |
8796 | ||
3e170ce0 A |
8797 | /* |
8798 | * Get the at_boot snapshot | |
8799 | */ | |
39236c6e | 8800 | static int |
0a7de745 A |
8801 | memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
8802 | { | |
39236c6e | 8803 | size_t input_size = *snapshot_size; |
3e170ce0 A |
8804 | |
8805 | /* | |
8806 | * The at_boot snapshot has no entry list. | |
8807 | */ | |
8808 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t); | |
8809 | ||
8810 | if (size_only) { | |
8811 | return 0; | |
8812 | } | |
8813 | ||
8814 | /* | |
8815 | * Validate the size of the snapshot buffer | |
8816 | */ | |
8817 | if (input_size < *snapshot_size) { | |
8818 | return EINVAL; | |
8819 | } | |
8820 | ||
8821 | /* | |
8822 | * Update the notification_time only | |
8823 | */ | |
8824 | memorystatus_at_boot_snapshot.notification_time = mach_absolute_time(); | |
8825 | *snapshot = &memorystatus_at_boot_snapshot; | |
8826 | ||
8827 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_at_boot_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%d)\n", | |
0a7de745 | 8828 | (long)input_size, (long)*snapshot_size, 0); |
3e170ce0 A |
8829 | return 0; |
8830 | } | |
8831 | ||
d9a64523 A |
8832 | /* |
8833 | * Get the previous fully populated snapshot | |
8834 | */ | |
8835 | static int | |
0a7de745 A |
8836 | memorystatus_get_jetsam_snapshot_copy(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
8837 | { | |
d9a64523 A |
8838 | size_t input_size = *snapshot_size; |
8839 | ||
8840 | if (memorystatus_jetsam_snapshot_copy_count > 0) { | |
8841 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_copy_count)); | |
8842 | } else { | |
8843 | *snapshot_size = 0; | |
8844 | } | |
8845 | ||
8846 | if (size_only) { | |
8847 | return 0; | |
8848 | } | |
8849 | ||
8850 | if (input_size < *snapshot_size) { | |
8851 | return EINVAL; | |
8852 | } | |
8853 | ||
8854 | *snapshot = memorystatus_jetsam_snapshot_copy; | |
8855 | ||
8856 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot_copy: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
0a7de745 | 8857 | (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_copy_count); |
d9a64523 A |
8858 | |
8859 | return 0; | |
8860 | } | |
8861 | ||
3e170ce0 | 8862 | static int |
0a7de745 A |
8863 | memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
8864 | { | |
3e170ce0 A |
8865 | size_t input_size = *snapshot_size; |
8866 | uint32_t ods_list_count = memorystatus_list_count; | |
0a7de745 | 8867 | memorystatus_jetsam_snapshot_t *ods = NULL; /* The on_demand snapshot buffer */ |
3e170ce0 A |
8868 | |
8869 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (ods_list_count)); | |
8870 | ||
8871 | if (size_only) { | |
8872 | return 0; | |
8873 | } | |
8874 | ||
8875 | /* | |
8876 | * Validate the size of the snapshot buffer. | |
8877 | * This is inherently racey. May want to revisit | |
8878 | * this error condition and trim the output when | |
8879 | * it doesn't fit. | |
8880 | */ | |
8881 | if (input_size < *snapshot_size) { | |
8882 | return EINVAL; | |
8883 | } | |
8884 | ||
8885 | /* | |
8886 | * Allocate and initialize a snapshot buffer. | |
8887 | */ | |
8888 | ods = (memorystatus_jetsam_snapshot_t *)kalloc(*snapshot_size); | |
8889 | if (!ods) { | |
0a7de745 | 8890 | return ENOMEM; |
3e170ce0 A |
8891 | } |
8892 | ||
8893 | memset(ods, 0, *snapshot_size); | |
8894 | ||
8895 | proc_list_lock(); | |
8896 | memorystatus_init_jetsam_snapshot_locked(ods, ods_list_count); | |
8897 | proc_list_unlock(); | |
8898 | ||
8899 | /* | |
8900 | * Return the kernel allocated, on_demand buffer. | |
8901 | * The caller of this routine will copy the data out | |
8902 | * to user space and then free the kernel allocated | |
8903 | * buffer. | |
8904 | */ | |
8905 | *snapshot = ods; | |
8906 | ||
8907 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_on_demand_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
0a7de745 A |
8908 | (long)input_size, (long)*snapshot_size, (long)ods_list_count); |
8909 | ||
3e170ce0 A |
8910 | return 0; |
8911 | } | |
8912 | ||
8913 | static int | |
0a7de745 A |
8914 | memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) |
8915 | { | |
3e170ce0 A |
8916 | size_t input_size = *snapshot_size; |
8917 | ||
39236c6e A |
8918 | if (memorystatus_jetsam_snapshot_count > 0) { |
8919 | *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count)); | |
8920 | } else { | |
8921 | *snapshot_size = 0; | |
8922 | } | |
8923 | ||
8924 | if (size_only) { | |
8925 | return 0; | |
316670eb | 8926 | } |
39236c6e A |
8927 | |
8928 | if (input_size < *snapshot_size) { | |
8929 | return EINVAL; | |
8930 | } | |
8931 | ||
8932 | *snapshot = memorystatus_jetsam_snapshot; | |
3e170ce0 A |
8933 | |
8934 | MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n", | |
0a7de745 | 8935 | (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_count); |
3e170ce0 | 8936 | |
39236c6e | 8937 | return 0; |
316670eb A |
8938 | } |
8939 | ||
fe8ab488 | 8940 | |
316670eb | 8941 | static int |
0a7de745 A |
8942 | memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) |
8943 | { | |
39236c6e A |
8944 | int error = EINVAL; |
8945 | boolean_t size_only; | |
3e170ce0 A |
8946 | boolean_t is_default_snapshot = FALSE; |
8947 | boolean_t is_on_demand_snapshot = FALSE; | |
8948 | boolean_t is_at_boot_snapshot = FALSE; | |
39236c6e | 8949 | memorystatus_jetsam_snapshot_t *snapshot; |
3e170ce0 | 8950 | |
39236c6e | 8951 | size_only = ((buffer == USER_ADDR_NULL) ? TRUE : FALSE); |
3e170ce0 A |
8952 | |
8953 | if (flags == 0) { | |
8954 | /* Default */ | |
8955 | is_default_snapshot = TRUE; | |
8956 | error = memorystatus_get_jetsam_snapshot(&snapshot, &buffer_size, size_only); | |
8957 | } else { | |
d9a64523 | 8958 | if (flags & ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT | MEMORYSTATUS_SNAPSHOT_COPY)) { |
3e170ce0 A |
8959 | /* |
8960 | * Unsupported bit set in flag. | |
8961 | */ | |
8962 | return EINVAL; | |
8963 | } | |
8964 | ||
d9a64523 | 8965 | if (flags & (flags - 0x1)) { |
3e170ce0 | 8966 | /* |
d9a64523 | 8967 | * Can't have multiple flags set at the same time. |
3e170ce0 A |
8968 | */ |
8969 | return EINVAL; | |
8970 | } | |
8971 | ||
8972 | if (flags & MEMORYSTATUS_SNAPSHOT_ON_DEMAND) { | |
8973 | is_on_demand_snapshot = TRUE; | |
8974 | /* | |
8975 | * When not requesting the size only, the following call will allocate | |
8976 | * an on_demand snapshot buffer, which is freed below. | |
8977 | */ | |
8978 | error = memorystatus_get_on_demand_snapshot(&snapshot, &buffer_size, size_only); | |
3e170ce0 A |
8979 | } else if (flags & MEMORYSTATUS_SNAPSHOT_AT_BOOT) { |
8980 | is_at_boot_snapshot = TRUE; | |
8981 | error = memorystatus_get_at_boot_snapshot(&snapshot, &buffer_size, size_only); | |
d9a64523 A |
8982 | } else if (flags & MEMORYSTATUS_SNAPSHOT_COPY) { |
8983 | error = memorystatus_get_jetsam_snapshot_copy(&snapshot, &buffer_size, size_only); | |
3e170ce0 A |
8984 | } else { |
8985 | /* | |
8986 | * Invalid flag setting. | |
8987 | */ | |
8988 | return EINVAL; | |
8989 | } | |
8990 | } | |
8991 | ||
39236c6e A |
8992 | if (error) { |
8993 | goto out; | |
8994 | } | |
316670eb | 8995 | |
3e170ce0 A |
8996 | /* |
8997 | * Copy the data out to user space and clear the snapshot buffer. | |
8998 | * If working with the jetsam snapshot, | |
8999 | * clearing the buffer means, reset the count. | |
9000 | * If working with an on_demand snapshot | |
9001 | * clearing the buffer means, free it. | |
9002 | * If working with the at_boot snapshot | |
9003 | * there is nothing to clear or update. | |
d9a64523 A |
9004 | * If working with a copy of the snapshot |
9005 | * there is nothing to clear or update. | |
3e170ce0 | 9006 | */ |
39236c6e A |
9007 | if (!size_only) { |
9008 | if ((error = copyout(snapshot, buffer, buffer_size)) == 0) { | |
3e170ce0 A |
9009 | if (is_default_snapshot) { |
9010 | /* | |
9011 | * The jetsam snapshot is never freed, its count is simply reset. | |
d9a64523 A |
9012 | * However, we make a copy for any parties that might be interested |
9013 | * in the previous fully populated snapshot. | |
3e170ce0 | 9014 | */ |
3e170ce0 | 9015 | proc_list_lock(); |
d9a64523 A |
9016 | memcpy(memorystatus_jetsam_snapshot_copy, memorystatus_jetsam_snapshot, memorystatus_jetsam_snapshot_size); |
9017 | memorystatus_jetsam_snapshot_copy_count = memorystatus_jetsam_snapshot_count; | |
39037602 | 9018 | snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0; |
3e170ce0 A |
9019 | memorystatus_jetsam_snapshot_last_timestamp = 0; |
9020 | proc_list_unlock(); | |
9021 | } | |
9022 | } | |
9023 | ||
9024 | if (is_on_demand_snapshot) { | |
9025 | /* | |
9026 | * The on_demand snapshot is always freed, | |
9027 | * even if the copyout failed. | |
9028 | */ | |
0a7de745 | 9029 | if (snapshot) { |
3e170ce0 A |
9030 | kfree(snapshot, buffer_size); |
9031 | } | |
39236c6e A |
9032 | } |
9033 | } | |
316670eb | 9034 | |
39236c6e A |
9035 | if (error == 0) { |
9036 | *retval = buffer_size; | |
9037 | } | |
9038 | out: | |
9039 | return error; | |
9040 | } | |
316670eb | 9041 | |
fe8ab488 | 9042 | /* |
0a7de745 | 9043 | * Routine: memorystatus_cmd_grp_set_priorities |
d9a64523 | 9044 | * Purpose: Update priorities for a group of processes. |
fe8ab488 | 9045 | * |
fe8ab488 A |
9046 | * [priority] |
9047 | * Move each process out of its effective priority | |
9048 | * band and into a new priority band. | |
9049 | * Maintains relative order from lowest to highest priority. | |
9050 | * In single band, maintains relative order from head to tail. | |
9051 | * | |
9052 | * eg: before [effectivepriority | pid] | |
9053 | * [18 | p101 ] | |
9054 | * [17 | p55, p67, p19 ] | |
9055 | * [12 | p103 p10 ] | |
9056 | * [ 7 | p25 ] | |
0a7de745 | 9057 | * [ 0 | p71, p82, ] |
fe8ab488 A |
9058 | * |
9059 | * after [ new band | pid] | |
9060 | * [ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101] | |
9061 | * | |
9062 | * Returns: 0 on success, else non-zero. | |
9063 | * | |
9064 | * Caveat: We know there is a race window regarding recycled pids. | |
9065 | * A process could be killed before the kernel can act on it here. | |
9066 | * If a pid cannot be found in any of the jetsam priority bands, | |
9067 | * then we simply ignore it. No harm. | |
9068 | * But, if the pid has been recycled then it could be an issue. | |
9069 | * In that scenario, we might move an unsuspecting process to the new | |
9070 | * priority band. It's not clear how the kernel can safeguard | |
9071 | * against this, but it would be an extremely rare case anyway. | |
9072 | * The caller of this api might avoid such race conditions by | |
9073 | * ensuring that the processes passed in the pid list are suspended. | |
9074 | */ | |
9075 | ||
9076 | ||
fe8ab488 | 9077 | static int |
d9a64523 A |
9078 | memorystatus_cmd_grp_set_priorities(user_addr_t buffer, size_t buffer_size) |
9079 | { | |
fe8ab488 A |
9080 | /* |
9081 | * We only handle setting priority | |
9082 | * per process | |
9083 | */ | |
9084 | ||
9085 | int error = 0; | |
d9a64523 | 9086 | memorystatus_properties_entry_v1_t *entries = NULL; |
fe8ab488 A |
9087 | uint32_t entry_count = 0; |
9088 | ||
9089 | /* This will be the ordered proc list */ | |
d9a64523 A |
9090 | typedef struct memorystatus_internal_properties { |
9091 | proc_t proc; | |
9092 | int32_t priority; | |
9093 | } memorystatus_internal_properties_t; | |
9094 | ||
fe8ab488 A |
9095 | memorystatus_internal_properties_t *table = NULL; |
9096 | size_t table_size = 0; | |
9097 | uint32_t table_count = 0; | |
9098 | ||
9099 | uint32_t i = 0; | |
9100 | uint32_t bucket_index = 0; | |
9101 | boolean_t head_insert; | |
9102 | int32_t new_priority; | |
0a7de745 | 9103 | |
fe8ab488 A |
9104 | proc_t p; |
9105 | ||
9106 | /* Verify inputs */ | |
d9a64523 | 9107 | if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) { |
fe8ab488 A |
9108 | error = EINVAL; |
9109 | goto out; | |
9110 | } | |
9111 | ||
d9a64523 A |
9112 | entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t)); |
9113 | if ((entries = (memorystatus_properties_entry_v1_t *)kalloc(buffer_size)) == NULL) { | |
fe8ab488 A |
9114 | error = ENOMEM; |
9115 | goto out; | |
9116 | } | |
9117 | ||
d9a64523 | 9118 | 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 |
9119 | |
9120 | if ((error = copyin(buffer, entries, buffer_size)) != 0) { | |
9121 | goto out; | |
9122 | } | |
9123 | ||
9124 | /* Verify sanity of input priorities */ | |
d9a64523 A |
9125 | if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) { |
9126 | if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) { | |
9127 | error = EINVAL; | |
9128 | goto out; | |
9129 | } | |
9130 | } else { | |
9131 | error = EINVAL; | |
9132 | goto out; | |
9133 | } | |
0a7de745 A |
9134 | |
9135 | for (i = 0; i < entry_count; i++) { | |
fe8ab488 A |
9136 | if (entries[i].priority == -1) { |
9137 | /* Use as shorthand for default priority */ | |
9138 | entries[i].priority = JETSAM_PRIORITY_DEFAULT; | |
39037602 A |
9139 | } else if ((entries[i].priority == system_procs_aging_band) || (entries[i].priority == applications_aging_band)) { |
9140 | /* Both the aging bands are reserved for internal use; | |
fe8ab488 A |
9141 | * if requested, adjust to JETSAM_PRIORITY_IDLE. */ |
9142 | entries[i].priority = JETSAM_PRIORITY_IDLE; | |
0a7de745 | 9143 | } else if (entries[i].priority == JETSAM_PRIORITY_IDLE_HEAD) { |
fe8ab488 A |
9144 | /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle |
9145 | * queue */ | |
9146 | /* Deal with this later */ | |
9147 | } else if ((entries[i].priority < 0) || (entries[i].priority >= MEMSTAT_BUCKET_COUNT)) { | |
9148 | /* Sanity check */ | |
9149 | error = EINVAL; | |
9150 | goto out; | |
9151 | } | |
9152 | } | |
9153 | ||
9154 | table_size = sizeof(memorystatus_internal_properties_t) * entry_count; | |
0a7de745 | 9155 | if ((table = (memorystatus_internal_properties_t *)kalloc(table_size)) == NULL) { |
fe8ab488 A |
9156 | error = ENOMEM; |
9157 | goto out; | |
9158 | } | |
9159 | memset(table, 0, table_size); | |
9160 | ||
9161 | ||
9162 | /* | |
9163 | * For each jetsam bucket entry, spin through the input property list. | |
9164 | * When a matching pid is found, populate an adjacent table with the | |
9165 | * appropriate proc pointer and new property values. | |
9166 | * This traversal automatically preserves order from lowest | |
9167 | * to highest priority. | |
9168 | */ | |
9169 | ||
0a7de745 A |
9170 | bucket_index = 0; |
9171 | ||
fe8ab488 A |
9172 | proc_list_lock(); |
9173 | ||
9174 | /* Create the ordered table */ | |
0a7de745 | 9175 | p = memorystatus_get_first_proc_locked(&bucket_index, TRUE); |
fe8ab488 | 9176 | while (p && (table_count < entry_count)) { |
0a7de745 | 9177 | for (i = 0; i < entry_count; i++) { |
fe8ab488 A |
9178 | if (p->p_pid == entries[i].pid) { |
9179 | /* Build the table data */ | |
9180 | table[table_count].proc = p; | |
9181 | table[table_count].priority = entries[i].priority; | |
9182 | table_count++; | |
9183 | break; | |
9184 | } | |
9185 | } | |
9186 | p = memorystatus_get_next_proc_locked(&bucket_index, p, TRUE); | |
9187 | } | |
0a7de745 | 9188 | |
fe8ab488 | 9189 | /* We now have ordered list of procs ready to move */ |
0a7de745 | 9190 | for (i = 0; i < table_count; i++) { |
fe8ab488 A |
9191 | p = table[i].proc; |
9192 | assert(p != NULL); | |
9193 | ||
9194 | /* Allow head inserts -- but relative order is now */ | |
9195 | if (table[i].priority == JETSAM_PRIORITY_IDLE_HEAD) { | |
9196 | new_priority = JETSAM_PRIORITY_IDLE; | |
9197 | head_insert = true; | |
9198 | } else { | |
9199 | new_priority = table[i].priority; | |
9200 | head_insert = false; | |
9201 | } | |
0a7de745 | 9202 | |
fe8ab488 A |
9203 | /* Not allowed */ |
9204 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { | |
9205 | continue; | |
9206 | } | |
9207 | ||
9208 | /* | |
39037602 A |
9209 | * Take appropriate steps if moving proc out of |
9210 | * either of the aging bands. | |
fe8ab488 | 9211 | */ |
39037602 | 9212 | if ((p->p_memstat_effectivepriority == system_procs_aging_band) || (p->p_memstat_effectivepriority == applications_aging_band)) { |
fe8ab488 A |
9213 | memorystatus_invalidate_idle_demotion_locked(p, TRUE); |
9214 | } | |
9215 | ||
39037602 | 9216 | memorystatus_update_priority_locked(p, new_priority, head_insert, false); |
fe8ab488 A |
9217 | } |
9218 | ||
9219 | proc_list_unlock(); | |
9220 | ||
9221 | /* | |
9222 | * if (table_count != entry_count) | |
9223 | * then some pids were not found in a jetsam band. | |
9224 | * harmless but interesting... | |
9225 | */ | |
fe8ab488 | 9226 | out: |
d9a64523 | 9227 | 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 |
9228 | |
9229 | if (entries) { | |
fe8ab488 | 9230 | kfree(entries, buffer_size); |
0a7de745 A |
9231 | } |
9232 | if (table) { | |
fe8ab488 | 9233 | kfree(table, table_size); |
0a7de745 | 9234 | } |
fe8ab488 | 9235 | |
0a7de745 | 9236 | return error; |
fe8ab488 A |
9237 | } |
9238 | ||
d9a64523 A |
9239 | static int |
9240 | memorystatus_cmd_grp_set_probabilities(user_addr_t buffer, size_t buffer_size) | |
9241 | { | |
9242 | int error = 0; | |
9243 | memorystatus_properties_entry_v1_t *entries = NULL; | |
9244 | uint32_t entry_count = 0, i = 0; | |
9245 | memorystatus_internal_probabilities_t *tmp_table_new = NULL, *tmp_table_old = NULL; | |
9246 | size_t tmp_table_new_size = 0, tmp_table_old_size = 0; | |
9247 | ||
9248 | /* Verify inputs */ | |
9249 | if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) { | |
9250 | error = EINVAL; | |
9251 | goto out; | |
9252 | } | |
9253 | ||
9254 | entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t)); | |
9255 | ||
9256 | if ((entries = (memorystatus_properties_entry_v1_t *) kalloc(buffer_size)) == NULL) { | |
9257 | error = ENOMEM; | |
9258 | goto out; | |
9259 | } | |
9260 | ||
9261 | 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); | |
9262 | ||
9263 | if ((error = copyin(buffer, entries, buffer_size)) != 0) { | |
9264 | goto out; | |
9265 | } | |
9266 | ||
9267 | if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) { | |
9268 | if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) { | |
9269 | error = EINVAL; | |
9270 | goto out; | |
9271 | } | |
9272 | } else { | |
9273 | error = EINVAL; | |
9274 | goto out; | |
9275 | } | |
0a7de745 | 9276 | |
d9a64523 | 9277 | /* Verify sanity of input priorities */ |
0a7de745 | 9278 | for (i = 0; i < entry_count; i++) { |
d9a64523 A |
9279 | /* |
9280 | * 0 - low probability of use. | |
9281 | * 1 - high probability of use. | |
9282 | * | |
0a7de745 | 9283 | * Keeping this field an int (& not a bool) to allow |
d9a64523 A |
9284 | * us to experiment with different values/approaches |
9285 | * later on. | |
9286 | */ | |
9287 | if (entries[i].use_probability > 1) { | |
9288 | error = EINVAL; | |
9289 | goto out; | |
9290 | } | |
9291 | } | |
9292 | ||
9293 | tmp_table_new_size = sizeof(memorystatus_internal_probabilities_t) * entry_count; | |
9294 | ||
0a7de745 | 9295 | if ((tmp_table_new = (memorystatus_internal_probabilities_t *) kalloc(tmp_table_new_size)) == NULL) { |
d9a64523 A |
9296 | error = ENOMEM; |
9297 | goto out; | |
9298 | } | |
9299 | memset(tmp_table_new, 0, tmp_table_new_size); | |
9300 | ||
9301 | proc_list_lock(); | |
9302 | ||
9303 | if (memorystatus_global_probabilities_table) { | |
9304 | tmp_table_old = memorystatus_global_probabilities_table; | |
9305 | tmp_table_old_size = memorystatus_global_probabilities_size; | |
9306 | } | |
9307 | ||
9308 | memorystatus_global_probabilities_table = tmp_table_new; | |
9309 | memorystatus_global_probabilities_size = tmp_table_new_size; | |
9310 | tmp_table_new = NULL; | |
9311 | ||
0a7de745 | 9312 | for (i = 0; i < entry_count; i++) { |
d9a64523 A |
9313 | /* Build the table data */ |
9314 | strlcpy(memorystatus_global_probabilities_table[i].proc_name, entries[i].proc_name, MAXCOMLEN + 1); | |
9315 | memorystatus_global_probabilities_table[i].use_probability = entries[i].use_probability; | |
9316 | } | |
9317 | ||
9318 | proc_list_unlock(); | |
0a7de745 | 9319 | |
d9a64523 A |
9320 | out: |
9321 | 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); | |
9322 | ||
9323 | if (entries) { | |
9324 | kfree(entries, buffer_size); | |
9325 | entries = NULL; | |
9326 | } | |
9327 | ||
9328 | if (tmp_table_old) { | |
9329 | kfree(tmp_table_old, tmp_table_old_size); | |
9330 | tmp_table_old = NULL; | |
9331 | } | |
9332 | ||
0a7de745 | 9333 | return error; |
d9a64523 A |
9334 | } |
9335 | ||
9336 | static int | |
9337 | memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) | |
9338 | { | |
9339 | int error = 0; | |
9340 | ||
9341 | if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) == MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) { | |
d9a64523 | 9342 | error = memorystatus_cmd_grp_set_priorities(buffer, buffer_size); |
d9a64523 | 9343 | } else if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) == MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) { |
d9a64523 | 9344 | error = memorystatus_cmd_grp_set_probabilities(buffer, buffer_size); |
d9a64523 A |
9345 | } else { |
9346 | error = EINVAL; | |
9347 | } | |
9348 | ||
9349 | return error; | |
9350 | } | |
fe8ab488 A |
9351 | |
9352 | /* | |
3e170ce0 A |
9353 | * This routine is used to update a process's jetsam priority position and stored user_data. |
9354 | * It is not used for the setting of memory limits, which is why the last 6 args to the | |
9355 | * memorystatus_update() call are 0 or FALSE. | |
fe8ab488 | 9356 | */ |
0a7de745 | 9357 | |
39236c6e | 9358 | static int |
0a7de745 A |
9359 | memorystatus_cmd_set_priority_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
9360 | { | |
3e170ce0 A |
9361 | int error = 0; |
9362 | memorystatus_priority_properties_t mpp_entry; | |
9363 | ||
39236c6e | 9364 | /* Validate inputs */ |
3e170ce0 | 9365 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_properties_t))) { |
39236c6e A |
9366 | return EINVAL; |
9367 | } | |
0a7de745 | 9368 | |
3e170ce0 A |
9369 | error = copyin(buffer, &mpp_entry, buffer_size); |
9370 | ||
9371 | if (error == 0) { | |
39236c6e | 9372 | proc_t p; |
0a7de745 | 9373 | |
39236c6e A |
9374 | p = proc_find(pid); |
9375 | if (!p) { | |
3e170ce0 | 9376 | return ESRCH; |
39236c6e | 9377 | } |
0a7de745 | 9378 | |
39236c6e | 9379 | if (p->p_memstat_state & P_MEMSTAT_INTERNAL) { |
39236c6e | 9380 | proc_rele(p); |
3e170ce0 | 9381 | return EPERM; |
39236c6e | 9382 | } |
0a7de745 | 9383 | |
5ba3f43e | 9384 | error = memorystatus_update(p, mpp_entry.priority, mpp_entry.user_data, FALSE, FALSE, 0, 0, FALSE, FALSE); |
39236c6e A |
9385 | proc_rele(p); |
9386 | } | |
0a7de745 A |
9387 | |
9388 | return error; | |
3e170ce0 A |
9389 | } |
9390 | ||
9391 | static int | |
0a7de745 A |
9392 | memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
9393 | { | |
3e170ce0 A |
9394 | int error = 0; |
9395 | memorystatus_memlimit_properties_t mmp_entry; | |
9396 | ||
9397 | /* Validate inputs */ | |
9398 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_memlimit_properties_t))) { | |
9399 | return EINVAL; | |
9400 | } | |
9401 | ||
9402 | error = copyin(buffer, &mmp_entry, buffer_size); | |
9403 | ||
9404 | if (error == 0) { | |
9405 | error = memorystatus_set_memlimit_properties(pid, &mmp_entry); | |
9406 | } | |
9407 | ||
0a7de745 | 9408 | return error; |
3e170ce0 A |
9409 | } |
9410 | ||
9411 | /* | |
9412 | * When getting the memlimit settings, we can't simply call task_get_phys_footprint_limit(). | |
9413 | * That gets the proc's cached memlimit and there is no guarantee that the active/inactive | |
9414 | * limits will be the same in the no-limit case. Instead we convert limits <= 0 using | |
9415 | * task_convert_phys_footprint_limit(). It computes the same limit value that would be written | |
9416 | * to the task's ledgers via task_set_phys_footprint_limit(). | |
9417 | */ | |
9418 | static int | |
0a7de745 A |
9419 | memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
9420 | { | |
3e170ce0 A |
9421 | int error = 0; |
9422 | memorystatus_memlimit_properties_t mmp_entry; | |
9423 | ||
9424 | /* Validate inputs */ | |
9425 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_memlimit_properties_t))) { | |
9426 | return EINVAL; | |
9427 | } | |
9428 | ||
0a7de745 | 9429 | memset(&mmp_entry, 0, sizeof(memorystatus_memlimit_properties_t)); |
3e170ce0 A |
9430 | |
9431 | proc_t p = proc_find(pid); | |
9432 | if (!p) { | |
9433 | return ESRCH; | |
9434 | } | |
9435 | ||
9436 | /* | |
9437 | * Get the active limit and attributes. | |
9438 | * No locks taken since we hold a reference to the proc. | |
9439 | */ | |
9440 | ||
0a7de745 | 9441 | if (p->p_memstat_memlimit_active > 0) { |
3e170ce0 A |
9442 | mmp_entry.memlimit_active = p->p_memstat_memlimit_active; |
9443 | } else { | |
9444 | task_convert_phys_footprint_limit(-1, &mmp_entry.memlimit_active); | |
9445 | } | |
9446 | ||
9447 | if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) { | |
9448 | mmp_entry.memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
9449 | } | |
9450 | ||
9451 | /* | |
9452 | * Get the inactive limit and attributes | |
9453 | */ | |
9454 | if (p->p_memstat_memlimit_inactive <= 0) { | |
9455 | task_convert_phys_footprint_limit(-1, &mmp_entry.memlimit_inactive); | |
9456 | } else { | |
9457 | mmp_entry.memlimit_inactive = p->p_memstat_memlimit_inactive; | |
9458 | } | |
9459 | if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) { | |
9460 | mmp_entry.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
9461 | } | |
9462 | proc_rele(p); | |
9463 | ||
9464 | error = copyout(&mmp_entry, buffer, buffer_size); | |
9465 | ||
0a7de745 | 9466 | return error; |
b0d623f7 A |
9467 | } |
9468 | ||
3e170ce0 | 9469 | |
39037602 A |
9470 | /* |
9471 | * SPI for kbd - pr24956468 | |
9472 | * This is a very simple snapshot that calculates how much a | |
9473 | * process's phys_footprint exceeds a specific memory limit. | |
9474 | * Only the inactive memory limit is supported for now. | |
9475 | * The delta is returned as bytes in excess or zero. | |
9476 | */ | |
9477 | static int | |
0a7de745 A |
9478 | memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) |
9479 | { | |
39037602 A |
9480 | int error = 0; |
9481 | uint64_t footprint_in_bytes = 0; | |
9482 | uint64_t delta_in_bytes = 0; | |
9483 | int32_t memlimit_mb = 0; | |
9484 | uint64_t memlimit_bytes = 0; | |
9485 | ||
9486 | /* Validate inputs */ | |
9487 | if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(uint64_t)) || (flags != 0)) { | |
0a7de745 | 9488 | return EINVAL; |
39037602 A |
9489 | } |
9490 | ||
9491 | proc_t p = proc_find(pid); | |
9492 | if (!p) { | |
9493 | return ESRCH; | |
9494 | } | |
9495 | ||
9496 | /* | |
9497 | * Get the inactive limit. | |
9498 | * No locks taken since we hold a reference to the proc. | |
9499 | */ | |
9500 | ||
9501 | if (p->p_memstat_memlimit_inactive <= 0) { | |
9502 | task_convert_phys_footprint_limit(-1, &memlimit_mb); | |
9503 | } else { | |
9504 | memlimit_mb = p->p_memstat_memlimit_inactive; | |
9505 | } | |
9506 | ||
9507 | footprint_in_bytes = get_task_phys_footprint(p->task); | |
9508 | ||
9509 | proc_rele(p); | |
9510 | ||
0a7de745 | 9511 | memlimit_bytes = memlimit_mb * 1024 * 1024; /* MB to bytes */ |
39037602 A |
9512 | |
9513 | /* | |
9514 | * Computed delta always returns >= 0 bytes | |
9515 | */ | |
9516 | if (footprint_in_bytes > memlimit_bytes) { | |
9517 | delta_in_bytes = footprint_in_bytes - memlimit_bytes; | |
9518 | } | |
9519 | ||
9520 | error = copyout(&delta_in_bytes, buffer, sizeof(delta_in_bytes)); | |
9521 | ||
0a7de745 | 9522 | return error; |
39037602 A |
9523 | } |
9524 | ||
9525 | ||
39236c6e | 9526 | static int |
0a7de745 A |
9527 | memorystatus_cmd_get_pressure_status(int32_t *retval) |
9528 | { | |
39236c6e | 9529 | int error; |
0a7de745 | 9530 | |
39236c6e A |
9531 | /* Need privilege for check */ |
9532 | error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0); | |
9533 | if (error) { | |
0a7de745 | 9534 | return error; |
39236c6e | 9535 | } |
0a7de745 | 9536 | |
39236c6e A |
9537 | /* Inherently racy, so it's not worth taking a lock here */ |
9538 | *retval = (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0; | |
0a7de745 | 9539 | |
39236c6e A |
9540 | return error; |
9541 | } | |
316670eb | 9542 | |
3e170ce0 | 9543 | int |
0a7de745 A |
9544 | memorystatus_get_pressure_status_kdp() |
9545 | { | |
3e170ce0 A |
9546 | return (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0; |
9547 | } | |
9548 | ||
fe8ab488 A |
9549 | /* |
9550 | * Every process, including a P_MEMSTAT_INTERNAL process (currently only pid 1), is allowed to set a HWM. | |
3e170ce0 A |
9551 | * |
9552 | * This call is inflexible -- it does not distinguish between active/inactive, fatal/non-fatal | |
9553 | * So, with 2-level HWM preserving previous behavior will map as follows. | |
9554 | * - treat the limit passed in as both an active and inactive limit. | |
9555 | * - treat the is_fatal_limit flag as though it applies to both active and inactive limits. | |
9556 | * | |
9557 | * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK | |
9558 | * - the is_fatal_limit is FALSE, meaning the active and inactive limits are non-fatal/soft | |
9559 | * - so mapping is (active/non-fatal, inactive/non-fatal) | |
9560 | * | |
9561 | * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT | |
9562 | * - the is_fatal_limit is TRUE, meaning the process's active and inactive limits are fatal/hard | |
9563 | * - so mapping is (active/fatal, inactive/fatal) | |
fe8ab488 A |
9564 | */ |
9565 | ||
5ba3f43e | 9566 | #if CONFIG_JETSAM |
b0d623f7 | 9567 | static int |
0a7de745 A |
9568 | memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit) |
9569 | { | |
39236c6e | 9570 | int error = 0; |
3e170ce0 A |
9571 | memorystatus_memlimit_properties_t entry; |
9572 | ||
9573 | entry.memlimit_active = high_water_mark; | |
9574 | entry.memlimit_active_attr = 0; | |
9575 | entry.memlimit_inactive = high_water_mark; | |
9576 | entry.memlimit_inactive_attr = 0; | |
9577 | ||
9578 | if (is_fatal_limit == TRUE) { | |
9579 | entry.memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
9580 | entry.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL; | |
9581 | } | |
9582 | ||
9583 | error = memorystatus_set_memlimit_properties(pid, &entry); | |
0a7de745 | 9584 | return error; |
3e170ce0 | 9585 | } |
5ba3f43e | 9586 | #endif /* CONFIG_JETSAM */ |
3e170ce0 A |
9587 | |
9588 | static int | |
0a7de745 A |
9589 | memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry) |
9590 | { | |
3e170ce0 A |
9591 | int32_t memlimit_active; |
9592 | boolean_t memlimit_active_is_fatal; | |
9593 | int32_t memlimit_inactive; | |
9594 | boolean_t memlimit_inactive_is_fatal; | |
9595 | uint32_t valid_attrs = 0; | |
9596 | int error = 0; | |
0a7de745 | 9597 | |
39236c6e A |
9598 | proc_t p = proc_find(pid); |
9599 | if (!p) { | |
9600 | return ESRCH; | |
9601 | } | |
3e170ce0 A |
9602 | |
9603 | /* | |
9604 | * Check for valid attribute flags. | |
9605 | */ | |
9606 | valid_attrs |= (MEMORYSTATUS_MEMLIMIT_ATTR_FATAL); | |
9607 | if ((entry->memlimit_active_attr & (~valid_attrs)) != 0) { | |
9608 | proc_rele(p); | |
9609 | return EINVAL; | |
9610 | } | |
9611 | if ((entry->memlimit_inactive_attr & (~valid_attrs)) != 0) { | |
9612 | proc_rele(p); | |
9613 | return EINVAL; | |
39236c6e | 9614 | } |
fe8ab488 | 9615 | |
3e170ce0 A |
9616 | /* |
9617 | * Setup the active memlimit properties | |
9618 | */ | |
9619 | memlimit_active = entry->memlimit_active; | |
9620 | if (entry->memlimit_active_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL) { | |
9621 | memlimit_active_is_fatal = TRUE; | |
9622 | } else { | |
9623 | memlimit_active_is_fatal = FALSE; | |
9624 | } | |
fe8ab488 | 9625 | |
3e170ce0 A |
9626 | /* |
9627 | * Setup the inactive memlimit properties | |
9628 | */ | |
9629 | memlimit_inactive = entry->memlimit_inactive; | |
9630 | if (entry->memlimit_inactive_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL) { | |
9631 | memlimit_inactive_is_fatal = TRUE; | |
9632 | } else { | |
9633 | memlimit_inactive_is_fatal = FALSE; | |
39236c6e A |
9634 | } |
9635 | ||
3e170ce0 A |
9636 | /* |
9637 | * Setting a limit of <= 0 implies that the process has no | |
9638 | * high-water-mark and has no per-task-limit. That means | |
9639 | * the system_wide task limit is in place, which by the way, | |
9640 | * is always fatal. | |
9641 | */ | |
9642 | ||
9643 | if (memlimit_active <= 0) { | |
9644 | /* | |
9645 | * Enforce the fatal system_wide task limit while process is active. | |
9646 | */ | |
9647 | memlimit_active = -1; | |
9648 | memlimit_active_is_fatal = TRUE; | |
9649 | } | |
9650 | ||
9651 | if (memlimit_inactive <= 0) { | |
9652 | /* | |
9653 | * Enforce the fatal system_wide task limit while process is inactive. | |
9654 | */ | |
9655 | memlimit_inactive = -1; | |
9656 | memlimit_inactive_is_fatal = TRUE; | |
9657 | } | |
9658 | ||
9659 | proc_list_lock(); | |
9660 | ||
9661 | /* | |
9662 | * Store the active limit variants in the proc. | |
9663 | */ | |
9664 | SET_ACTIVE_LIMITS_LOCKED(p, memlimit_active, memlimit_active_is_fatal); | |
9665 | ||
9666 | /* | |
9667 | * Store the inactive limit variants in the proc. | |
9668 | */ | |
9669 | SET_INACTIVE_LIMITS_LOCKED(p, memlimit_inactive, memlimit_inactive_is_fatal); | |
9670 | ||
9671 | /* | |
9672 | * Enforce appropriate limit variant by updating the cached values | |
9673 | * and writing the ledger. | |
9674 | * Limit choice is based on process active/inactive state. | |
9675 | */ | |
9676 | ||
9677 | if (memorystatus_highwater_enabled) { | |
813fb2f6 A |
9678 | boolean_t is_fatal; |
9679 | boolean_t use_active; | |
3e170ce0 A |
9680 | |
9681 | if (proc_jetsam_state_is_active_locked(p) == TRUE) { | |
813fb2f6 A |
9682 | CACHE_ACTIVE_LIMITS_LOCKED(p, is_fatal); |
9683 | use_active = TRUE; | |
fe8ab488 | 9684 | } else { |
813fb2f6 A |
9685 | CACHE_INACTIVE_LIMITS_LOCKED(p, is_fatal); |
9686 | use_active = FALSE; | |
fe8ab488 | 9687 | } |
3e170ce0 A |
9688 | |
9689 | /* Enforce the limit by writing to the ledgers */ | |
813fb2f6 | 9690 | 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 |
9691 | |
9692 | 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 |
9693 | p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1), |
9694 | (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty, | |
9695 | (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : "")); | |
39037602 | 9696 | DTRACE_MEMORYSTATUS2(memorystatus_set_memlimit, proc_t, p, int32_t, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1)); |
fe8ab488 A |
9697 | } |
9698 | ||
39236c6e A |
9699 | proc_list_unlock(); |
9700 | proc_rele(p); | |
0a7de745 | 9701 | |
39236c6e A |
9702 | return error; |
9703 | } | |
9704 | ||
fe8ab488 A |
9705 | /* |
9706 | * Returns the jetsam priority (effective or requested) of the process | |
9707 | * associated with this task. | |
9708 | */ | |
9709 | int | |
9710 | proc_get_memstat_priority(proc_t p, boolean_t effective_priority) | |
9711 | { | |
9712 | if (p) { | |
9713 | if (effective_priority) { | |
9714 | return p->p_memstat_effectivepriority; | |
9715 | } else { | |
9716 | return p->p_memstat_requestedpriority; | |
9717 | } | |
9718 | } | |
9719 | return 0; | |
9720 | } | |
3e170ce0 | 9721 | |
d9a64523 A |
9722 | static int |
9723 | memorystatus_get_process_is_managed(pid_t pid, int *is_managed) | |
9724 | { | |
9725 | proc_t p = NULL; | |
9726 | ||
9727 | /* Validate inputs */ | |
9728 | if (pid == 0) { | |
9729 | return EINVAL; | |
9730 | } | |
9731 | ||
9732 | p = proc_find(pid); | |
9733 | if (!p) { | |
9734 | return ESRCH; | |
9735 | } | |
9736 | ||
9737 | proc_list_lock(); | |
9738 | *is_managed = ((p->p_memstat_state & P_MEMSTAT_MANAGED) ? 1 : 0); | |
9739 | proc_rele_locked(p); | |
9740 | proc_list_unlock(); | |
9741 | ||
9742 | return 0; | |
9743 | } | |
9744 | ||
9745 | static int | |
9746 | memorystatus_set_process_is_managed(pid_t pid, boolean_t set_managed) | |
9747 | { | |
9748 | proc_t p = NULL; | |
9749 | ||
9750 | /* Validate inputs */ | |
9751 | if (pid == 0) { | |
9752 | return EINVAL; | |
9753 | } | |
9754 | ||
9755 | p = proc_find(pid); | |
9756 | if (!p) { | |
9757 | return ESRCH; | |
9758 | } | |
9759 | ||
9760 | proc_list_lock(); | |
9761 | if (set_managed == TRUE) { | |
9762 | p->p_memstat_state |= P_MEMSTAT_MANAGED; | |
9763 | } else { | |
9764 | p->p_memstat_state &= ~P_MEMSTAT_MANAGED; | |
9765 | } | |
9766 | proc_rele_locked(p); | |
9767 | proc_list_unlock(); | |
9768 | ||
9769 | return 0; | |
9770 | } | |
9771 | ||
9772 | static int | |
9773 | memorystatus_get_process_is_freezable(pid_t pid, int *is_freezable) | |
9774 | { | |
9775 | proc_t p = PROC_NULL; | |
9776 | ||
9777 | if (pid == 0) { | |
9778 | return EINVAL; | |
9779 | } | |
9780 | ||
9781 | p = proc_find(pid); | |
9782 | if (!p) { | |
9783 | return ESRCH; | |
9784 | } | |
9785 | ||
9786 | /* | |
9787 | * Only allow this on the current proc for now. | |
9788 | * We can check for privileges and allow targeting another process in the future. | |
9789 | */ | |
9790 | if (p != current_proc()) { | |
9791 | proc_rele(p); | |
9792 | return EPERM; | |
9793 | } | |
9794 | ||
9795 | proc_list_lock(); | |
9796 | *is_freezable = ((p->p_memstat_state & P_MEMSTAT_FREEZE_DISABLED) ? 0 : 1); | |
9797 | proc_rele_locked(p); | |
9798 | proc_list_unlock(); | |
9799 | ||
9800 | return 0; | |
9801 | } | |
9802 | ||
9803 | static int | |
9804 | memorystatus_set_process_is_freezable(pid_t pid, boolean_t is_freezable) | |
9805 | { | |
9806 | proc_t p = PROC_NULL; | |
9807 | ||
9808 | if (pid == 0) { | |
9809 | return EINVAL; | |
9810 | } | |
9811 | ||
9812 | p = proc_find(pid); | |
9813 | if (!p) { | |
9814 | return ESRCH; | |
9815 | } | |
9816 | ||
9817 | /* | |
9818 | * Only allow this on the current proc for now. | |
9819 | * We can check for privileges and allow targeting another process in the future. | |
9820 | */ | |
9821 | if (p != current_proc()) { | |
9822 | proc_rele(p); | |
9823 | return EPERM; | |
9824 | } | |
9825 | ||
9826 | proc_list_lock(); | |
9827 | if (is_freezable == FALSE) { | |
9828 | /* Freeze preference set to FALSE. Set the P_MEMSTAT_FREEZE_DISABLED bit. */ | |
9829 | p->p_memstat_state |= P_MEMSTAT_FREEZE_DISABLED; | |
9830 | printf("memorystatus_set_process_is_freezable: disabling freeze for pid %d [%s]\n", | |
0a7de745 | 9831 | p->p_pid, (*p->p_name ? p->p_name : "unknown")); |
d9a64523 A |
9832 | } else { |
9833 | p->p_memstat_state &= ~P_MEMSTAT_FREEZE_DISABLED; | |
9834 | printf("memorystatus_set_process_is_freezable: enabling freeze for pid %d [%s]\n", | |
0a7de745 | 9835 | p->p_pid, (*p->p_name ? p->p_name : "unknown")); |
d9a64523 A |
9836 | } |
9837 | proc_rele_locked(p); | |
9838 | proc_list_unlock(); | |
9839 | ||
9840 | return 0; | |
9841 | } | |
9842 | ||
39236c6e | 9843 | int |
0a7de745 A |
9844 | memorystatus_control(struct proc *p __unused, struct memorystatus_control_args *args, int *ret) |
9845 | { | |
39236c6e | 9846 | int error = EINVAL; |
d9a64523 | 9847 | boolean_t skip_auth_check = FALSE; |
39037602 | 9848 | os_reason_t jetsam_reason = OS_REASON_NULL; |
39236c6e A |
9849 | |
9850 | #if !CONFIG_JETSAM | |
9851 | #pragma unused(ret) | |
39037602 | 9852 | #pragma unused(jetsam_reason) |
39236c6e A |
9853 | #endif |
9854 | ||
d9a64523 A |
9855 | /* We don't need entitlements if we're setting/ querying the freeze preference for a process. Skip the check below. */ |
9856 | if (args->command == MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE || args->command == MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE) { | |
9857 | skip_auth_check = TRUE; | |
9858 | } | |
9859 | ||
9860 | /* Need to be root or have entitlement. */ | |
9861 | if (!kauth_cred_issuser(kauth_cred_get()) && !IOTaskHasEntitlement(current_task(), MEMORYSTATUS_ENTITLEMENT) && !skip_auth_check) { | |
39236c6e A |
9862 | error = EPERM; |
9863 | goto out; | |
b0d623f7 | 9864 | } |
39037602 A |
9865 | |
9866 | /* | |
9867 | * Sanity check. | |
9868 | * Do not enforce it for snapshots. | |
9869 | */ | |
9870 | if (args->command != MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT) { | |
9871 | if (args->buffersize > MEMORYSTATUS_BUFFERSIZE_MAX) { | |
9872 | error = EINVAL; | |
9873 | goto out; | |
9874 | } | |
39236c6e A |
9875 | } |
9876 | ||
9877 | switch (args->command) { | |
9878 | case MEMORYSTATUS_CMD_GET_PRIORITY_LIST: | |
5ba3f43e | 9879 | error = memorystatus_cmd_get_priority_list(args->pid, args->buffer, args->buffersize, ret); |
39236c6e | 9880 | break; |
39236c6e A |
9881 | case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES: |
9882 | error = memorystatus_cmd_set_priority_properties(args->pid, args->buffer, args->buffersize, ret); | |
9883 | break; | |
3e170ce0 A |
9884 | case MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES: |
9885 | error = memorystatus_cmd_set_memlimit_properties(args->pid, args->buffer, args->buffersize, ret); | |
9886 | break; | |
9887 | case MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES: | |
9888 | error = memorystatus_cmd_get_memlimit_properties(args->pid, args->buffer, args->buffersize, ret); | |
9889 | break; | |
39037602 A |
9890 | case MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS: |
9891 | error = memorystatus_cmd_get_memlimit_excess_np(args->pid, args->flags, args->buffer, args->buffersize, ret); | |
9892 | break; | |
fe8ab488 A |
9893 | case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES: |
9894 | error = memorystatus_cmd_grp_set_properties((int32_t)args->flags, args->buffer, args->buffersize, ret); | |
0a7de745 | 9895 | break; |
39236c6e | 9896 | case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT: |
3e170ce0 | 9897 | error = memorystatus_cmd_get_jetsam_snapshot((int32_t)args->flags, args->buffer, args->buffersize, ret); |
39236c6e A |
9898 | break; |
9899 | case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS: | |
9900 | error = memorystatus_cmd_get_pressure_status(ret); | |
9901 | break; | |
5ba3f43e | 9902 | #if CONFIG_JETSAM |
39236c6e | 9903 | case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK: |
3e170ce0 A |
9904 | /* |
9905 | * This call does not distinguish between active and inactive limits. | |
9906 | * Default behavior in 2-level HWM world is to set both. | |
9907 | * Non-fatal limit is also assumed for both. | |
9908 | */ | |
fe8ab488 A |
9909 | error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, FALSE); |
9910 | break; | |
9911 | case MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT: | |
3e170ce0 A |
9912 | /* |
9913 | * This call does not distinguish between active and inactive limits. | |
9914 | * Default behavior in 2-level HWM world is to set both. | |
9915 | * Fatal limit is also assumed for both. | |
9916 | */ | |
fe8ab488 | 9917 | error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, TRUE); |
39236c6e | 9918 | break; |
5ba3f43e | 9919 | #endif /* CONFIG_JETSAM */ |
0a7de745 | 9920 | /* Test commands */ |
39236c6e A |
9921 | #if DEVELOPMENT || DEBUG |
9922 | case MEMORYSTATUS_CMD_TEST_JETSAM: | |
39037602 A |
9923 | jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_GENERIC); |
9924 | if (jetsam_reason == OS_REASON_NULL) { | |
9925 | printf("memorystatus_control: failed to allocate jetsam reason\n"); | |
9926 | } | |
9927 | ||
9928 | error = memorystatus_kill_process_sync(args->pid, kMemorystatusKilled, jetsam_reason) ? 0 : EINVAL; | |
39236c6e | 9929 | break; |
3e170ce0 A |
9930 | case MEMORYSTATUS_CMD_TEST_JETSAM_SORT: |
9931 | error = memorystatus_cmd_test_jetsam_sort(args->pid, (int32_t)args->flags); | |
9932 | break; | |
5ba3f43e | 9933 | #if CONFIG_JETSAM |
39236c6e A |
9934 | case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS: |
9935 | error = memorystatus_cmd_set_panic_bits(args->buffer, args->buffersize); | |
9936 | break; | |
5ba3f43e | 9937 | #endif /* CONFIG_JETSAM */ |
39037602 A |
9938 | #else /* DEVELOPMENT || DEBUG */ |
9939 | #pragma unused(jetsam_reason) | |
39236c6e | 9940 | #endif /* DEVELOPMENT || DEBUG */ |
490019cf A |
9941 | case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_ENABLE: |
9942 | if (memorystatus_aggressive_jetsam_lenient_allowed == FALSE) { | |
9943 | #if DEVELOPMENT || DEBUG | |
9944 | printf("Enabling Lenient Mode\n"); | |
9945 | #endif /* DEVELOPMENT || DEBUG */ | |
9946 | ||
9947 | memorystatus_aggressive_jetsam_lenient_allowed = TRUE; | |
9948 | memorystatus_aggressive_jetsam_lenient = TRUE; | |
39037602 | 9949 | error = 0; |
490019cf A |
9950 | } |
9951 | break; | |
9952 | case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE: | |
9953 | #if DEVELOPMENT || DEBUG | |
9954 | printf("Disabling Lenient mode\n"); | |
9955 | #endif /* DEVELOPMENT || DEBUG */ | |
9956 | memorystatus_aggressive_jetsam_lenient_allowed = FALSE; | |
9957 | memorystatus_aggressive_jetsam_lenient = FALSE; | |
39037602 | 9958 | error = 0; |
490019cf | 9959 | break; |
3e170ce0 A |
9960 | case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE: |
9961 | case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE: | |
9962 | error = memorystatus_low_mem_privileged_listener(args->command); | |
9963 | break; | |
39037602 | 9964 | |
39037602 A |
9965 | case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE: |
9966 | case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE: | |
d9a64523 A |
9967 | error = memorystatus_update_inactive_jetsam_priority_band(args->pid, args->command, JETSAM_PRIORITY_ELEVATED_INACTIVE, args->flags ? TRUE : FALSE); |
9968 | break; | |
9969 | case MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED: | |
9970 | error = memorystatus_set_process_is_managed(args->pid, args->flags); | |
39037602 | 9971 | break; |
39037602 | 9972 | |
d9a64523 A |
9973 | case MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED: |
9974 | error = memorystatus_get_process_is_managed(args->pid, ret); | |
9975 | break; | |
9976 | ||
9977 | case MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE: | |
9978 | error = memorystatus_set_process_is_freezable(args->pid, args->flags ? TRUE : FALSE); | |
9979 | break; | |
9980 | ||
9981 | case MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE: | |
9982 | error = memorystatus_get_process_is_freezable(args->pid, ret); | |
9983 | break; | |
9984 | ||
9985 | #if CONFIG_FREEZE | |
9986 | #if DEVELOPMENT || DEBUG | |
9987 | case MEMORYSTATUS_CMD_FREEZER_CONTROL: | |
9988 | error = memorystatus_freezer_control(args->flags, args->buffer, args->buffersize, ret); | |
9989 | break; | |
9990 | #endif /* DEVELOPMENT || DEBUG */ | |
9991 | #endif /* CONFIG_FREEZE */ | |
9992 | ||
39236c6e A |
9993 | default: |
9994 | break; | |
9995 | } | |
9996 | ||
9997 | out: | |
9998 | return error; | |
9999 | } | |
10000 | ||
10001 | ||
10002 | static int | |
5ba3f43e | 10003 | filt_memorystatusattach(struct knote *kn, __unused struct kevent_internal_s *kev) |
0a7de745 | 10004 | { |
39037602 A |
10005 | int error; |
10006 | ||
39236c6e | 10007 | kn->kn_flags |= EV_CLEAR; |
39037602 A |
10008 | error = memorystatus_knote_register(kn); |
10009 | if (error) { | |
10010 | kn->kn_flags = EV_ERROR; | |
10011 | kn->kn_data = error; | |
10012 | } | |
10013 | return 0; | |
39236c6e A |
10014 | } |
10015 | ||
10016 | static void | |
10017 | filt_memorystatusdetach(struct knote *kn) | |
10018 | { | |
10019 | memorystatus_knote_unregister(kn); | |
10020 | } | |
10021 | ||
10022 | static int | |
10023 | filt_memorystatus(struct knote *kn __unused, long hint) | |
10024 | { | |
10025 | if (hint) { | |
10026 | switch (hint) { | |
10027 | case kMemorystatusNoPressure: | |
10028 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_NORMAL) { | |
3e170ce0 | 10029 | kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_NORMAL; |
39236c6e A |
10030 | } |
10031 | break; | |
10032 | case kMemorystatusPressure: | |
10033 | if (memorystatus_vm_pressure_level == kVMPressureWarning || memorystatus_vm_pressure_level == kVMPressureUrgent) { | |
10034 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) { | |
3e170ce0 | 10035 | kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN; |
39236c6e A |
10036 | } |
10037 | } else if (memorystatus_vm_pressure_level == kVMPressureCritical) { | |
39236c6e | 10038 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) { |
3e170ce0 | 10039 | kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL; |
39236c6e A |
10040 | } |
10041 | } | |
10042 | break; | |
fe8ab488 A |
10043 | case kMemorystatusLowSwap: |
10044 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_LOW_SWAP) { | |
3e170ce0 | 10045 | kn->kn_fflags = NOTE_MEMORYSTATUS_LOW_SWAP; |
fe8ab488 A |
10046 | } |
10047 | break; | |
39037602 A |
10048 | |
10049 | case kMemorystatusProcLimitWarn: | |
10050 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { | |
0a7de745 A |
10051 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_WARN; |
10052 | } | |
10053 | break; | |
39037602 A |
10054 | |
10055 | case kMemorystatusProcLimitCritical: | |
10056 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { | |
0a7de745 A |
10057 | kn->kn_fflags = NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL; |
10058 | } | |
10059 | break; | |
39037602 | 10060 | |
39236c6e A |
10061 | default: |
10062 | break; | |
b0d623f7 | 10063 | } |
39236c6e | 10064 | } |
813fb2f6 A |
10065 | |
10066 | #if 0 | |
10067 | if (kn->kn_fflags != 0) { | |
10068 | proc_t knote_proc = knote_get_kq(kn)->kq_p; | |
10069 | pid_t knote_pid = knote_proc->p_pid; | |
10070 | ||
10071 | printf("filt_memorystatus: sending kn 0x%lx (event 0x%x) for pid (%d)\n", | |
0a7de745 | 10072 | (unsigned long)kn, kn->kn_fflags, knote_pid); |
813fb2f6 A |
10073 | } |
10074 | #endif | |
10075 | ||
0a7de745 | 10076 | return kn->kn_fflags != 0; |
39236c6e A |
10077 | } |
10078 | ||
39037602 A |
10079 | static int |
10080 | filt_memorystatustouch(struct knote *kn, struct kevent_internal_s *kev) | |
10081 | { | |
10082 | int res; | |
813fb2f6 | 10083 | int prev_kn_sfflags = 0; |
39037602 A |
10084 | |
10085 | memorystatus_klist_lock(); | |
10086 | ||
10087 | /* | |
10088 | * copy in new kevent settings | |
10089 | * (saving the "desired" data and fflags). | |
10090 | */ | |
813fb2f6 A |
10091 | |
10092 | prev_kn_sfflags = kn->kn_sfflags; | |
10093 | kn->kn_sfflags = (kev->fflags & EVFILT_MEMORYSTATUS_ALL_MASK); | |
10094 | ||
5ba3f43e | 10095 | #if !CONFIG_EMBEDDED |
813fb2f6 A |
10096 | /* |
10097 | * Only on desktop do we restrict notifications to | |
10098 | * one per active/inactive state (soft limits only). | |
10099 | */ | |
10100 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { | |
10101 | /* | |
10102 | * Is there previous state to preserve? | |
10103 | */ | |
10104 | if (prev_kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { | |
10105 | /* | |
10106 | * This knote was previously interested in proc_limit_warn, | |
10107 | * so yes, preserve previous state. | |
10108 | */ | |
10109 | if (prev_kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE) { | |
10110 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; | |
10111 | } | |
10112 | if (prev_kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE) { | |
10113 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; | |
10114 | } | |
10115 | } else { | |
10116 | /* | |
10117 | * This knote was not previously interested in proc_limit_warn, | |
10118 | * but it is now. Set both states. | |
10119 | */ | |
10120 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; | |
10121 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; | |
10122 | } | |
10123 | } | |
10124 | ||
10125 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { | |
10126 | /* | |
10127 | * Is there previous state to preserve? | |
10128 | */ | |
10129 | if (prev_kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { | |
10130 | /* | |
10131 | * This knote was previously interested in proc_limit_critical, | |
10132 | * so yes, preserve previous state. | |
10133 | */ | |
10134 | if (prev_kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE) { | |
10135 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE; | |
10136 | } | |
10137 | if (prev_kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE) { | |
10138 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE; | |
10139 | } | |
10140 | } else { | |
10141 | /* | |
10142 | * This knote was not previously interested in proc_limit_critical, | |
10143 | * but it is now. Set both states. | |
10144 | */ | |
10145 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE; | |
10146 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE; | |
10147 | } | |
10148 | } | |
5ba3f43e | 10149 | #endif /* !CONFIG_EMBEDDED */ |
39037602 | 10150 | |
39037602 A |
10151 | /* |
10152 | * reset the output flags based on a | |
10153 | * combination of the old events and | |
10154 | * the new desired event list. | |
10155 | */ | |
10156 | //kn->kn_fflags &= kn->kn_sfflags; | |
10157 | ||
10158 | res = (kn->kn_fflags != 0); | |
10159 | ||
10160 | memorystatus_klist_unlock(); | |
10161 | ||
10162 | return res; | |
10163 | } | |
10164 | ||
10165 | static int | |
10166 | filt_memorystatusprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev) | |
10167 | { | |
10168 | #pragma unused(data) | |
10169 | int res; | |
10170 | ||
10171 | memorystatus_klist_lock(); | |
10172 | res = (kn->kn_fflags != 0); | |
10173 | if (res) { | |
10174 | *kev = kn->kn_kevent; | |
10175 | kn->kn_flags |= EV_CLEAR; /* automatic */ | |
10176 | kn->kn_fflags = 0; | |
10177 | kn->kn_data = 0; | |
10178 | } | |
10179 | memorystatus_klist_unlock(); | |
10180 | ||
10181 | return res; | |
10182 | } | |
10183 | ||
39236c6e | 10184 | static void |
0a7de745 A |
10185 | memorystatus_klist_lock(void) |
10186 | { | |
39236c6e A |
10187 | lck_mtx_lock(&memorystatus_klist_mutex); |
10188 | } | |
10189 | ||
10190 | static void | |
0a7de745 A |
10191 | memorystatus_klist_unlock(void) |
10192 | { | |
39236c6e A |
10193 | lck_mtx_unlock(&memorystatus_klist_mutex); |
10194 | } | |
10195 | ||
0a7de745 A |
10196 | void |
10197 | memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr) | |
10198 | { | |
39236c6e A |
10199 | lck_mtx_init(&memorystatus_klist_mutex, grp, attr); |
10200 | klist_init(&memorystatus_klist); | |
10201 | } | |
10202 | ||
10203 | int | |
0a7de745 A |
10204 | memorystatus_knote_register(struct knote *kn) |
10205 | { | |
39236c6e | 10206 | int error = 0; |
813fb2f6 | 10207 | |
39236c6e | 10208 | memorystatus_klist_lock(); |
813fb2f6 A |
10209 | |
10210 | /* | |
10211 | * Support only userspace visible flags. | |
10212 | */ | |
0a7de745 | 10213 | if ((kn->kn_sfflags & EVFILT_MEMORYSTATUS_ALL_MASK) == (unsigned int) kn->kn_sfflags) { |
5ba3f43e | 10214 | #if !CONFIG_EMBEDDED |
813fb2f6 A |
10215 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_WARN) { |
10216 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_ACTIVE; | |
10217 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_WARN_INACTIVE; | |
10218 | } | |
10219 | ||
10220 | if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL) { | |
10221 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_ACTIVE; | |
10222 | kn->kn_sfflags |= NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL_INACTIVE; | |
10223 | } | |
5ba3f43e | 10224 | #endif /* !CONFIG_EMBEDDED */ |
39236c6e | 10225 | |
3e170ce0 | 10226 | KNOTE_ATTACH(&memorystatus_klist, kn); |
0a7de745 | 10227 | } else { |
39236c6e | 10228 | error = ENOTSUP; |
b0d623f7 | 10229 | } |
0a7de745 | 10230 | |
39236c6e | 10231 | memorystatus_klist_unlock(); |
0a7de745 | 10232 | |
39236c6e | 10233 | return error; |
b0d623f7 A |
10234 | } |
10235 | ||
39236c6e | 10236 | void |
0a7de745 A |
10237 | memorystatus_knote_unregister(struct knote *kn __unused) |
10238 | { | |
39236c6e A |
10239 | memorystatus_klist_lock(); |
10240 | KNOTE_DETACH(&memorystatus_klist, kn); | |
10241 | memorystatus_klist_unlock(); | |
10242 | } | |
316670eb | 10243 | |
fe8ab488 A |
10244 | |
10245 | #if 0 | |
39236c6e A |
10246 | #if CONFIG_JETSAM && VM_PRESSURE_EVENTS |
10247 | static boolean_t | |
0a7de745 A |
10248 | memorystatus_issue_pressure_kevent(boolean_t pressured) |
10249 | { | |
39236c6e A |
10250 | memorystatus_klist_lock(); |
10251 | KNOTE(&memorystatus_klist, pressured ? kMemorystatusPressure : kMemorystatusNoPressure); | |
10252 | memorystatus_klist_unlock(); | |
10253 | return TRUE; | |
10254 | } | |
39236c6e | 10255 | #endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */ |
fe8ab488 | 10256 | #endif /* 0 */ |
3e170ce0 | 10257 | |
3e170ce0 A |
10258 | /* Coalition support */ |
10259 | ||
10260 | /* sorting info for a particular priority bucket */ | |
10261 | typedef struct memstat_sort_info { | |
0a7de745 A |
10262 | coalition_t msi_coal; |
10263 | uint64_t msi_page_count; | |
10264 | pid_t msi_pid; | |
10265 | int msi_ntasks; | |
3e170ce0 A |
10266 | } memstat_sort_info_t; |
10267 | ||
0a7de745 | 10268 | /* |
3e170ce0 A |
10269 | * qsort from smallest page count to largest page count |
10270 | * | |
10271 | * return < 0 for a < b | |
10272 | * 0 for a == b | |
10273 | * > 0 for a > b | |
10274 | */ | |
0a7de745 A |
10275 | static int |
10276 | memstat_asc_cmp(const void *a, const void *b) | |
3e170ce0 | 10277 | { |
0a7de745 A |
10278 | const memstat_sort_info_t *msA = (const memstat_sort_info_t *)a; |
10279 | const memstat_sort_info_t *msB = (const memstat_sort_info_t *)b; | |
3e170ce0 | 10280 | |
0a7de745 | 10281 | return (int)((uint64_t)msA->msi_page_count - (uint64_t)msB->msi_page_count); |
3e170ce0 A |
10282 | } |
10283 | ||
10284 | /* | |
10285 | * Return the number of pids rearranged during this sort. | |
10286 | */ | |
10287 | static int | |
10288 | memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order) | |
10289 | { | |
0a7de745 A |
10290 | #define MAX_SORT_PIDS 80 |
10291 | #define MAX_COAL_LEADERS 10 | |
3e170ce0 A |
10292 | |
10293 | unsigned int b = bucket_index; | |
10294 | int nleaders = 0; | |
10295 | int ntasks = 0; | |
10296 | proc_t p = NULL; | |
10297 | coalition_t coal = COALITION_NULL; | |
10298 | int pids_moved = 0; | |
10299 | int total_pids_moved = 0; | |
10300 | int i; | |
10301 | ||
0a7de745 | 10302 | /* |
3e170ce0 A |
10303 | * The system is typically under memory pressure when in this |
10304 | * path, hence, we want to avoid dynamic memory allocation. | |
10305 | */ | |
10306 | memstat_sort_info_t leaders[MAX_COAL_LEADERS]; | |
10307 | pid_t pid_list[MAX_SORT_PIDS]; | |
10308 | ||
10309 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
0a7de745 A |
10310 | return 0; |
10311 | } | |
3e170ce0 A |
10312 | |
10313 | /* | |
10314 | * Clear the array that holds coalition leader information | |
10315 | */ | |
0a7de745 | 10316 | for (i = 0; i < MAX_COAL_LEADERS; i++) { |
3e170ce0 | 10317 | leaders[i].msi_coal = COALITION_NULL; |
0a7de745 A |
10318 | leaders[i].msi_page_count = 0; /* will hold total coalition page count */ |
10319 | leaders[i].msi_pid = 0; /* will hold coalition leader pid */ | |
10320 | leaders[i].msi_ntasks = 0; /* will hold the number of tasks in a coalition */ | |
3e170ce0 A |
10321 | } |
10322 | ||
0a7de745 A |
10323 | p = memorystatus_get_first_proc_locked(&b, FALSE); |
10324 | while (p) { | |
10325 | if (coalition_is_leader(p->task, COALITION_TYPE_JETSAM, &coal)) { | |
3e170ce0 A |
10326 | if (nleaders < MAX_COAL_LEADERS) { |
10327 | int coal_ntasks = 0; | |
10328 | uint64_t coal_page_count = coalition_get_page_count(coal, &coal_ntasks); | |
10329 | leaders[nleaders].msi_coal = coal; | |
10330 | leaders[nleaders].msi_page_count = coal_page_count; | |
0a7de745 | 10331 | leaders[nleaders].msi_pid = p->p_pid; /* the coalition leader */ |
3e170ce0 A |
10332 | leaders[nleaders].msi_ntasks = coal_ntasks; |
10333 | nleaders++; | |
10334 | } else { | |
0a7de745 | 10335 | /* |
3e170ce0 | 10336 | * We've hit MAX_COAL_LEADERS meaning we can handle no more coalitions. |
0a7de745 | 10337 | * Abandoned coalitions will linger at the tail of the priority band |
3e170ce0 A |
10338 | * when this sort session ends. |
10339 | * TODO: should this be an assert? | |
10340 | */ | |
10341 | printf("%s: WARNING: more than %d leaders in priority band [%d]\n", | |
0a7de745 | 10342 | __FUNCTION__, MAX_COAL_LEADERS, bucket_index); |
3e170ce0 A |
10343 | break; |
10344 | } | |
0a7de745 A |
10345 | } |
10346 | p = memorystatus_get_next_proc_locked(&b, p, FALSE); | |
10347 | } | |
3e170ce0 A |
10348 | |
10349 | if (nleaders == 0) { | |
10350 | /* Nothing to sort */ | |
0a7de745 | 10351 | return 0; |
3e170ce0 A |
10352 | } |
10353 | ||
0a7de745 | 10354 | /* |
3e170ce0 A |
10355 | * Sort the coalition leader array, from smallest coalition page count |
10356 | * to largest coalition page count. When inserted in the priority bucket, | |
10357 | * smallest coalition is handled first, resulting in the last to be jetsammed. | |
10358 | */ | |
10359 | if (nleaders > 1) { | |
10360 | qsort(leaders, nleaders, sizeof(memstat_sort_info_t), memstat_asc_cmp); | |
10361 | } | |
10362 | ||
10363 | #if 0 | |
10364 | for (i = 0; i < nleaders; i++) { | |
10365 | printf("%s: coal_leader[%d of %d] pid[%d] pages[%llu] ntasks[%d]\n", | |
0a7de745 A |
10366 | __FUNCTION__, i, nleaders, leaders[i].msi_pid, leaders[i].msi_page_count, |
10367 | leaders[i].msi_ntasks); | |
3e170ce0 A |
10368 | } |
10369 | #endif | |
10370 | ||
10371 | /* | |
10372 | * During coalition sorting, processes in a priority band are rearranged | |
10373 | * by being re-inserted at the head of the queue. So, when handling a | |
10374 | * list, the first process that gets moved to the head of the queue, | |
10375 | * ultimately gets pushed toward the queue tail, and hence, jetsams last. | |
10376 | * | |
10377 | * So, for example, the coalition leader is expected to jetsam last, | |
10378 | * after its coalition members. Therefore, the coalition leader is | |
10379 | * inserted at the head of the queue first. | |
10380 | * | |
10381 | * After processing a coalition, the jetsam order is as follows: | |
10382 | * undefs(jetsam first), extensions, xpc services, leader(jetsam last) | |
10383 | */ | |
10384 | ||
10385 | /* | |
10386 | * Coalition members are rearranged in the priority bucket here, | |
10387 | * based on their coalition role. | |
10388 | */ | |
10389 | total_pids_moved = 0; | |
0a7de745 | 10390 | for (i = 0; i < nleaders; i++) { |
3e170ce0 A |
10391 | /* a bit of bookkeeping */ |
10392 | pids_moved = 0; | |
10393 | ||
10394 | /* Coalition leaders are jetsammed last, so move into place first */ | |
10395 | pid_list[0] = leaders[i].msi_pid; | |
10396 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, 1); | |
10397 | ||
10398 | /* xpc services should jetsam after extensions */ | |
0a7de745 A |
10399 | ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_XPC, |
10400 | coal_sort_order, pid_list, MAX_SORT_PIDS); | |
3e170ce0 A |
10401 | |
10402 | if (ntasks > 0) { | |
0a7de745 A |
10403 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, |
10404 | (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); | |
3e170ce0 A |
10405 | } |
10406 | ||
10407 | /* extensions should jetsam after unmarked processes */ | |
0a7de745 A |
10408 | ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_EXT, |
10409 | coal_sort_order, pid_list, MAX_SORT_PIDS); | |
3e170ce0 A |
10410 | |
10411 | if (ntasks > 0) { | |
10412 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, | |
0a7de745 | 10413 | (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); |
3e170ce0 A |
10414 | } |
10415 | ||
10416 | /* undefined coalition members should be the first to jetsam */ | |
0a7de745 A |
10417 | ntasks = coalition_get_pid_list(leaders[i].msi_coal, COALITION_ROLEMASK_UNDEF, |
10418 | coal_sort_order, pid_list, MAX_SORT_PIDS); | |
3e170ce0 A |
10419 | |
10420 | if (ntasks > 0) { | |
0a7de745 A |
10421 | pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, |
10422 | (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS)); | |
3e170ce0 A |
10423 | } |
10424 | ||
10425 | #if 0 | |
10426 | if (pids_moved == leaders[i].msi_ntasks) { | |
10427 | /* | |
10428 | * All the pids in the coalition were found in this band. | |
10429 | */ | |
10430 | printf("%s: pids_moved[%d] equal total coalition ntasks[%d] \n", __FUNCTION__, | |
0a7de745 | 10431 | pids_moved, leaders[i].msi_ntasks); |
3e170ce0 A |
10432 | } else if (pids_moved > leaders[i].msi_ntasks) { |
10433 | /* | |
10434 | * Apparently new coalition members showed up during the sort? | |
10435 | */ | |
10436 | printf("%s: pids_moved[%d] were greater than expected coalition ntasks[%d] \n", __FUNCTION__, | |
0a7de745 | 10437 | pids_moved, leaders[i].msi_ntasks); |
3e170ce0 A |
10438 | } else { |
10439 | /* | |
10440 | * Apparently not all the pids in the coalition were found in this band? | |
10441 | */ | |
10442 | printf("%s: pids_moved[%d] were less than expected coalition ntasks[%d] \n", __FUNCTION__, | |
0a7de745 | 10443 | pids_moved, leaders[i].msi_ntasks); |
3e170ce0 A |
10444 | } |
10445 | #endif | |
10446 | ||
10447 | total_pids_moved += pids_moved; | |
3e170ce0 A |
10448 | } /* end for */ |
10449 | ||
0a7de745 | 10450 | return total_pids_moved; |
3e170ce0 A |
10451 | } |
10452 | ||
10453 | ||
10454 | /* | |
10455 | * Traverse a list of pids, searching for each within the priority band provided. | |
10456 | * If pid is found, move it to the front of the priority band. | |
10457 | * Never searches outside the priority band provided. | |
0a7de745 | 10458 | * |
3e170ce0 A |
10459 | * Input: |
10460 | * bucket_index - jetsam priority band. | |
10461 | * pid_list - pointer to a list of pids. | |
10462 | * list_sz - number of pids in the list. | |
10463 | * | |
0a7de745 | 10464 | * Pid list ordering is important in that, |
3e170ce0 A |
10465 | * pid_list[n] is expected to jetsam ahead of pid_list[n+1]. |
10466 | * The sort_order is set by the coalition default. | |
10467 | * | |
0a7de745 | 10468 | * Return: |
3e170ce0 A |
10469 | * the number of pids found and hence moved within the priority band. |
10470 | */ | |
10471 | static int | |
10472 | memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int list_sz) | |
10473 | { | |
10474 | memstat_bucket_t *current_bucket; | |
10475 | int i; | |
10476 | int found_pids = 0; | |
10477 | ||
10478 | if ((pid_list == NULL) || (list_sz <= 0)) { | |
0a7de745 | 10479 | return 0; |
3e170ce0 A |
10480 | } |
10481 | ||
10482 | if (bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
0a7de745 A |
10483 | return 0; |
10484 | } | |
3e170ce0 A |
10485 | |
10486 | current_bucket = &memstat_bucket[bucket_index]; | |
0a7de745 | 10487 | for (i = 0; i < list_sz; i++) { |
3e170ce0 A |
10488 | unsigned int b = bucket_index; |
10489 | proc_t p = NULL; | |
10490 | proc_t aProc = NULL; | |
10491 | pid_t aPid; | |
10492 | int list_index; | |
10493 | ||
10494 | list_index = ((list_sz - 1) - i); | |
0a7de745 A |
10495 | aPid = pid_list[list_index]; |
10496 | ||
10497 | /* never search beyond bucket_index provided */ | |
10498 | p = memorystatus_get_first_proc_locked(&b, FALSE); | |
10499 | while (p) { | |
10500 | if (p->p_pid == aPid) { | |
10501 | aProc = p; | |
10502 | break; | |
10503 | } | |
10504 | p = memorystatus_get_next_proc_locked(&b, p, FALSE); | |
10505 | } | |
10506 | ||
10507 | if (aProc == NULL) { | |
3e170ce0 | 10508 | /* pid not found in this band, just skip it */ |
0a7de745 A |
10509 | continue; |
10510 | } else { | |
10511 | TAILQ_REMOVE(¤t_bucket->list, aProc, p_memstat_list); | |
10512 | TAILQ_INSERT_HEAD(¤t_bucket->list, aProc, p_memstat_list); | |
3e170ce0 | 10513 | found_pids++; |
0a7de745 A |
10514 | } |
10515 | } | |
10516 | return found_pids; | |
3e170ce0 | 10517 | } |
5ba3f43e A |
10518 | |
10519 | int | |
10520 | memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index) | |
10521 | { | |
0a7de745 | 10522 | int32_t i = JETSAM_PRIORITY_IDLE; |
5ba3f43e A |
10523 | int count = 0; |
10524 | ||
10525 | if (max_bucket_index >= MEMSTAT_BUCKET_COUNT) { | |
0a7de745 A |
10526 | return -1; |
10527 | } | |
5ba3f43e | 10528 | |
0a7de745 | 10529 | while (i <= max_bucket_index) { |
5ba3f43e A |
10530 | count += memstat_bucket[i++].count; |
10531 | } | |
10532 | ||
10533 | return count; | |
10534 | } | |
10535 | ||
10536 | int | |
10537 | memorystatus_update_priority_for_appnap(proc_t p, boolean_t is_appnap) | |
10538 | { | |
10539 | #if !CONFIG_JETSAM | |
d9a64523 | 10540 | if (!p || (!isApp(p)) || (p->p_memstat_state & (P_MEMSTAT_INTERNAL | P_MEMSTAT_MANAGED))) { |
5ba3f43e A |
10541 | /* |
10542 | * Ineligible processes OR system processes e.g. launchd. | |
d9a64523 A |
10543 | * |
10544 | * We also skip processes that have the P_MEMSTAT_MANAGED bit set, i.e. | |
10545 | * they're managed by assertiond. These are iOS apps that have been ported | |
10546 | * to macOS. assertiond might be in the process of modifying the app's | |
10547 | * priority / memory limit - so it might have the proc_list lock, and then try | |
10548 | * to take the task lock. Meanwhile we've entered this function with the task lock | |
10549 | * held, and we need the proc_list lock below. So we'll deadlock with assertiond. | |
10550 | * | |
10551 | * It should be fine to read the P_MEMSTAT_MANAGED bit without the proc_list | |
10552 | * lock here, since assertiond only sets this bit on process launch. | |
5ba3f43e A |
10553 | */ |
10554 | return -1; | |
10555 | } | |
10556 | ||
10557 | /* | |
10558 | * For macOS only: | |
10559 | * We would like to use memorystatus_update() here to move the processes | |
10560 | * within the bands. Unfortunately memorystatus_update() calls | |
10561 | * memorystatus_update_priority_locked() which uses any band transitions | |
10562 | * as an indication to modify ledgers. For that it needs the task lock | |
10563 | * and since we came into this function with the task lock held, we'll deadlock. | |
10564 | * | |
0a7de745 | 10565 | * Unfortunately we can't completely disable ledger updates because we still |
5ba3f43e A |
10566 | * need the ledger updates for a subset of processes i.e. daemons. |
10567 | * When all processes on all platforms support memory limits, we can simply call | |
10568 | * memorystatus_update(). | |
0a7de745 | 10569 | * |
5ba3f43e A |
10570 | * It also has some logic to deal with 'aging' which, currently, is only applicable |
10571 | * on CONFIG_JETSAM configs. So, till every platform has CONFIG_JETSAM we'll need | |
10572 | * to do this explicit band transition. | |
10573 | */ | |
10574 | ||
10575 | memstat_bucket_t *current_bucket, *new_bucket; | |
0a7de745 | 10576 | int32_t priority = 0; |
5ba3f43e A |
10577 | |
10578 | proc_list_lock(); | |
10579 | ||
10580 | if (((p->p_listflag & P_LIST_EXITED) != 0) || | |
10581 | (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED))) { | |
10582 | /* | |
10583 | * If the process is on its way out OR | |
10584 | * jetsam has alread tried and failed to kill this process, | |
10585 | * let's skip the whole jetsam band transition. | |
10586 | */ | |
10587 | proc_list_unlock(); | |
0a7de745 | 10588 | return 0; |
5ba3f43e A |
10589 | } |
10590 | ||
10591 | if (is_appnap) { | |
10592 | current_bucket = &memstat_bucket[p->p_memstat_effectivepriority]; | |
10593 | new_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
10594 | priority = JETSAM_PRIORITY_IDLE; | |
10595 | } else { | |
10596 | if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) { | |
10597 | /* | |
10598 | * It is possible that someone pulled this process | |
10599 | * out of the IDLE band without updating its app-nap | |
10600 | * parameters. | |
10601 | */ | |
10602 | proc_list_unlock(); | |
0a7de745 | 10603 | return 0; |
5ba3f43e A |
10604 | } |
10605 | ||
10606 | current_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE]; | |
10607 | new_bucket = &memstat_bucket[p->p_memstat_requestedpriority]; | |
10608 | priority = p->p_memstat_requestedpriority; | |
10609 | } | |
10610 | ||
10611 | TAILQ_REMOVE(¤t_bucket->list, p, p_memstat_list); | |
10612 | current_bucket->count--; | |
10613 | ||
10614 | TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list); | |
10615 | new_bucket->count++; | |
10616 | ||
10617 | /* | |
10618 | * Record idle start or idle delta. | |
10619 | */ | |
10620 | if (p->p_memstat_effectivepriority == priority) { | |
0a7de745 | 10621 | /* |
5ba3f43e A |
10622 | * This process is not transitioning between |
10623 | * jetsam priority buckets. Do nothing. | |
10624 | */ | |
10625 | } else if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) { | |
10626 | uint64_t now; | |
10627 | /* | |
10628 | * Transitioning out of the idle priority bucket. | |
10629 | * Record idle delta. | |
10630 | */ | |
10631 | assert(p->p_memstat_idle_start != 0); | |
10632 | now = mach_absolute_time(); | |
10633 | if (now > p->p_memstat_idle_start) { | |
10634 | p->p_memstat_idle_delta = now - p->p_memstat_idle_start; | |
10635 | } | |
10636 | } else if (priority == JETSAM_PRIORITY_IDLE) { | |
10637 | /* | |
10638 | * Transitioning into the idle priority bucket. | |
10639 | * Record idle start. | |
10640 | */ | |
10641 | p->p_memstat_idle_start = mach_absolute_time(); | |
10642 | } | |
10643 | ||
d9a64523 A |
10644 | KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_CHANGE_PRIORITY), p->p_pid, priority, p->p_memstat_effectivepriority, 0, 0); |
10645 | ||
5ba3f43e A |
10646 | p->p_memstat_effectivepriority = priority; |
10647 | ||
10648 | proc_list_unlock(); | |
10649 | ||
0a7de745 | 10650 | return 0; |
5ba3f43e A |
10651 | |
10652 | #else /* !CONFIG_JETSAM */ | |
10653 | #pragma unused(p) | |
10654 | #pragma unused(is_appnap) | |
10655 | return -1; | |
10656 | #endif /* !CONFIG_JETSAM */ | |
10657 | } |