]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/kern_memorystatus.h
xnu-6153.41.3.tar.gz
[apple/xnu.git] / bsd / sys / kern_memorystatus.h
1 /*
2 * Copyright (c) 2006-2018 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef SYS_MEMORYSTATUS_H
30 #define SYS_MEMORYSTATUS_H
31
32 #include <sys/time.h>
33 #include <mach_debug/zone_info.h>
34 #include <sys/proc.h>
35
36 #define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus"
37
38 #define JETSAM_PRIORITY_REVISION 2
39
40 #define JETSAM_PRIORITY_IDLE_HEAD -2
41 /* The value -1 is an alias to JETSAM_PRIORITY_DEFAULT */
42 #define JETSAM_PRIORITY_IDLE 0
43 #define JETSAM_PRIORITY_IDLE_DEFERRED 1 /* Keeping this around till all xnu_quick_tests can be moved away from it.*/
44 #define JETSAM_PRIORITY_AGING_BAND1 JETSAM_PRIORITY_IDLE_DEFERRED
45 #define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 2
46 #define JETSAM_PRIORITY_AGING_BAND2 JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC
47 #define JETSAM_PRIORITY_BACKGROUND 3
48 #define JETSAM_PRIORITY_ELEVATED_INACTIVE JETSAM_PRIORITY_BACKGROUND
49 #define JETSAM_PRIORITY_MAIL 4
50 #define JETSAM_PRIORITY_PHONE 5
51 #define JETSAM_PRIORITY_UI_SUPPORT 8
52 #define JETSAM_PRIORITY_FOREGROUND_SUPPORT 9
53 #define JETSAM_PRIORITY_FOREGROUND 10
54 #define JETSAM_PRIORITY_AUDIO_AND_ACCESSORY 12
55 #define JETSAM_PRIORITY_CONDUCTOR 13
56 #define JETSAM_PRIORITY_DRIVER_APPLE 15
57 #define JETSAM_PRIORITY_HOME 16
58 #define JETSAM_PRIORITY_EXECUTIVE 17
59 #define JETSAM_PRIORITY_IMPORTANT 18
60 #define JETSAM_PRIORITY_CRITICAL 19
61
62 #define JETSAM_PRIORITY_MAX 21
63
64 /* TODO - tune. This should probably be lower priority */
65 #define JETSAM_PRIORITY_DEFAULT 18
66 #define JETSAM_PRIORITY_TELEPHONY 19
67
68 /* Compatibility */
69 #define DEFAULT_JETSAM_PRIORITY 18
70
71 /*
72 * The deferral time used by default for apps and daemons in all aging
73 * policies except kJetsamAgingPolicySysProcsReclaimedFirst is
74 * DEFERRED_IDLE_EXIT_TIME_SECS.
75 *
76 * For kJetsamAgingPolicySysProcsReclaimedFirst,
77 *
78 * Daemons: The actual idle deferred time for the daemon is based on
79 * the relaunch behavior of the daemon. The relaunch behavior determines
80 * the scaling factor applied to DEFERRED_IDLE_EXIT_TIME_SECS. See
81 * kJetsamSysProcsIdleDelayTime* ratios defined in kern_memorystatus.c
82 *
83 * Apps: The apps are aged for DEFERRED_IDLE_EXIT_TIME_SECS factored
84 * by kJetsamAppsIdleDelayTimeRatio.
85 */
86 #define DEFERRED_IDLE_EXIT_TIME_SECS 10
87
88 #define KEV_MEMORYSTATUS_SUBCLASS 3
89
90 enum {
91 kMemorystatusLevelNote = 1,
92 kMemorystatusSnapshotNote = 2,
93 kMemorystatusFreezeNote = 3,
94 kMemorystatusPressureNote = 4
95 };
96
97 enum {
98 kMemorystatusLevelAny = -1,
99 kMemorystatusLevelNormal = 0,
100 kMemorystatusLevelWarning = 1,
101 kMemorystatusLevelUrgent = 2,
102 kMemorystatusLevelCritical = 3
103 };
104
105 typedef struct memorystatus_priority_entry {
106 pid_t pid;
107 int32_t priority;
108 uint64_t user_data;
109 int32_t limit; /* MB */
110 uint32_t state;
111 } memorystatus_priority_entry_t;
112
113 /*
114 * This should be the structure to specify different properties
115 * for processes (group or single) from user-space. Unfortunately,
116 * we can't move to it completely because the priority_entry structure
117 * above has been in use for a while now. We'll have to deprecate it.
118 *
119 * To support new fields/properties, we will add a new structure with a
120 * new version and a new size.
121 */
122 #define MEMORYSTATUS_MPE_VERSION_1 1
123
124 #define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1)
125
126 typedef struct memorystatus_properties_entry_v1 {
127 int version;
128 pid_t pid;
129 int32_t priority;
130 int use_probability;
131 uint64_t user_data;
132 int32_t limit; /* MB */
133 uint32_t state;
134 char proc_name[MAXCOMLEN + 1];
135 char __pad1[3];
136 } memorystatus_properties_entry_v1_t;
137
138 typedef struct memorystatus_kernel_stats {
139 uint32_t free_pages;
140 uint32_t active_pages;
141 uint32_t inactive_pages;
142 uint32_t throttled_pages;
143 uint32_t purgeable_pages;
144 uint32_t wired_pages;
145 uint32_t speculative_pages;
146 uint32_t filebacked_pages;
147 uint32_t anonymous_pages;
148 uint32_t compressor_pages;
149 uint64_t compressions;
150 uint64_t decompressions;
151 uint64_t total_uncompressed_pages_in_compressor;
152 uint64_t zone_map_size;
153 uint64_t zone_map_capacity;
154 uint64_t largest_zone_size;
155 char largest_zone_name[MACH_ZONE_NAME_MAX_LEN];
156 } memorystatus_kernel_stats_t;
157
158 /*
159 ** This is a variable-length struct.
160 ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t *
161 */
162
163 typedef struct jetsam_snapshot_entry {
164 pid_t pid;
165 char name[(2 * MAXCOMLEN) + 1];
166 int32_t priority;
167 uint32_t state;
168 uint32_t fds;
169 uint8_t uuid[16];
170 uint64_t user_data;
171 uint64_t killed;
172 uint64_t pages;
173 uint64_t max_pages_lifetime;
174 uint64_t purgeable_pages;
175 uint64_t jse_internal_pages;
176 uint64_t jse_internal_compressed_pages;
177 uint64_t jse_purgeable_nonvolatile_pages;
178 uint64_t jse_purgeable_nonvolatile_compressed_pages;
179 uint64_t jse_alternate_accounting_pages;
180 uint64_t jse_alternate_accounting_compressed_pages;
181 uint64_t jse_iokit_mapped_pages;
182 uint64_t jse_page_table_pages;
183 uint64_t jse_memory_region_count;
184 uint64_t jse_gencount; /* memorystatus_thread generation counter */
185 uint64_t jse_starttime; /* absolute time when process starts */
186 uint64_t jse_killtime; /* absolute time when jetsam chooses to kill a process */
187 uint64_t jse_idle_delta; /* time spent in idle band */
188 uint64_t jse_coalition_jetsam_id; /* we only expose coalition id for COALITION_TYPE_JETSAM */
189 struct timeval64 cpu_time;
190 uint64_t jse_thaw_count;
191 } memorystatus_jetsam_snapshot_entry_t;
192
193 typedef struct jetsam_snapshot {
194 uint64_t snapshot_time; /* absolute time snapshot was initialized */
195 uint64_t notification_time; /* absolute time snapshot was consumed */
196 uint64_t js_gencount; /* memorystatus_thread generation counter */
197 memorystatus_kernel_stats_t stats; /* system stat when snapshot is initialized */
198 size_t entry_count;
199 memorystatus_jetsam_snapshot_entry_t entries[];
200 } memorystatus_jetsam_snapshot_t;
201
202 /* TODO - deprecate; see <rdar://problem/12969599> */
203 #define kMaxSnapshotEntries 192
204
205 /*
206 * default jetsam snapshot support
207 */
208 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot;
209 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy;
210 extern unsigned int memorystatus_jetsam_snapshot_count;
211 extern unsigned int memorystatus_jetsam_snapshot_copy_count;
212 extern unsigned int memorystatus_jetsam_snapshot_max;
213 extern unsigned int memorystatus_jetsam_snapshot_size;
214 extern uint64_t memorystatus_jetsam_snapshot_last_timestamp;
215 extern uint64_t memorystatus_jetsam_snapshot_timeout;
216 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries
217 #define JETSAM_SNAPSHOT_TIMEOUT_SECS 30
218
219 /* General memorystatus stuff */
220
221 extern uint64_t memorystatus_sysprocs_idle_delay_time;
222 extern uint64_t memorystatus_apps_idle_delay_time;
223
224 /* State */
225 #define kMemorystatusSuspended 0x01
226 #define kMemorystatusFrozen 0x02
227 #define kMemorystatusWasThawed 0x04
228 #define kMemorystatusTracked 0x08
229 #define kMemorystatusSupportsIdleExit 0x10
230 #define kMemorystatusDirty 0x20
231 #define kMemorystatusAssertion 0x40
232
233 /*
234 * Jetsam exit reason definitions - related to memorystatus
235 *
236 * When adding new exit reasons also update:
237 * JETSAM_REASON_MEMORYSTATUS_MAX
238 * kMemorystatusKilled... Cause enum
239 * memorystatus_kill_cause_name[]
240 */
241 #define JETSAM_REASON_INVALID 0
242 #define JETSAM_REASON_GENERIC 1
243 #define JETSAM_REASON_MEMORY_HIGHWATER 2
244 #define JETSAM_REASON_VNODE 3
245 #define JETSAM_REASON_MEMORY_VMPAGESHORTAGE 4
246 #define JETSAM_REASON_MEMORY_PROCTHRASHING 5
247 #define JETSAM_REASON_MEMORY_FCTHRASHING 6
248 #define JETSAM_REASON_MEMORY_PERPROCESSLIMIT 7
249 #define JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE 8
250 #define JETSAM_REASON_MEMORY_IDLE_EXIT 9
251 #define JETSAM_REASON_ZONE_MAP_EXHAUSTION 10
252 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 11
253 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE 12
254 #define JETSAM_REASON_LOWSWAP 13
255 #define JETSAM_REASON_MEMORYSTATUS_MAX JETSAM_REASON_LOWSWAP
256
257 /*
258 * Jetsam exit reason definitions - not related to memorystatus
259 */
260 #define JETSAM_REASON_CPULIMIT 100
261
262 /* Cause */
263 enum {
264 kMemorystatusInvalid = JETSAM_REASON_INVALID,
265 kMemorystatusKilled = JETSAM_REASON_GENERIC,
266 kMemorystatusKilledHiwat = JETSAM_REASON_MEMORY_HIGHWATER,
267 kMemorystatusKilledVnodes = JETSAM_REASON_VNODE,
268 kMemorystatusKilledVMPageShortage = JETSAM_REASON_MEMORY_VMPAGESHORTAGE,
269 kMemorystatusKilledProcThrashing = JETSAM_REASON_MEMORY_PROCTHRASHING,
270 kMemorystatusKilledFCThrashing = JETSAM_REASON_MEMORY_FCTHRASHING,
271 kMemorystatusKilledPerProcessLimit = JETSAM_REASON_MEMORY_PERPROCESSLIMIT,
272 kMemorystatusKilledDiskSpaceShortage = JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE,
273 kMemorystatusKilledIdleExit = JETSAM_REASON_MEMORY_IDLE_EXIT,
274 kMemorystatusKilledZoneMapExhaustion = JETSAM_REASON_ZONE_MAP_EXHAUSTION,
275 kMemorystatusKilledVMCompressorThrashing = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING,
276 kMemorystatusKilledVMCompressorSpaceShortage = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE,
277 kMemorystatusKilledLowSwap = JETSAM_REASON_LOWSWAP,
278 };
279
280 /*
281 * For backwards compatibility
282 * Keeping these around for external users (e.g. ReportCrash, Ariadne).
283 * TODO: Remove once they stop using these.
284 */
285 #define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage
286 #define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing
287 #define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING
288
289 /* Memorystatus control */
290 #define MEMORYSTATUS_BUFFERSIZE_MAX 65536
291
292 #ifndef KERNEL
293 int memorystatus_get_level(user_addr_t level);
294 int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize);
295 #endif
296
297 /* Commands */
298 #define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1
299 #define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2
300 #define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3
301 #define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4
302 #define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */
303 #define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */
304 #define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 7 /* Set memory limits plus attributes independently */
305 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 8 /* Get memory limits plus attributes */
306 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE 9 /* Set the task's status as a privileged listener w.r.t memory notifications */
307 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE 10 /* Reset the task's status as a privileged listener w.r.t memory notifications */
308 #define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_ENABLE 11 /* Enable the 'lenient' mode for aggressive jetsam. See comments in kern_memorystatus.c near the top. */
309 #define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12 /* Disable the 'lenient' mode for aggressive jetsam. */
310 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */
311 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE 14 /* Set the inactive jetsam band for a process to JETSAM_PRIORITY_ELEVATED_INACTIVE */
312 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE 15 /* Reset the inactive jetsam band for a process to the default band (0)*/
313 #define MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED 16 /* (Re-)Set state on a process that marks it as (un-)managed by a system entity e.g. assertiond */
314 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED 17 /* Return the 'managed' status of a process */
315 #define MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE 18 /* Is the process eligible for freezing? Apps and extensions can pass in FALSE to opt out of freezing, i.e.,
316 * if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */
317 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE 19 /* Return the freezable state of a process. */
318
319 #if CONFIG_FREEZE
320 #if DEVELOPMENT || DEBUG
321 #define MEMORYSTATUS_CMD_FREEZER_CONTROL 20
322 #endif /* DEVELOPMENT || DEBUG */
323 #endif /* CONFIG_FREEZE */
324
325 #define MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE 21 /* Query if the lenient mode for aggressive jetsam is enabled. */
326
327 #define MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT 22 /* Used by DYLD to increase the jetsam active and inactive limits, when using roots */
328
329 /* Commands that act on a group of processes */
330 #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100
331
332 #if PRIVATE
333 /* Test commands */
334
335 /* Trigger forced jetsam */
336 #define MEMORYSTATUS_CMD_TEST_JETSAM 1000
337 #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001
338
339 /* Panic on jetsam options */
340 typedef struct memorystatus_jetsam_panic_options {
341 uint32_t data;
342 uint32_t mask;
343 } memorystatus_jetsam_panic_options_t;
344
345 #define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002
346
347 /* Select priority band sort order */
348 #define JETSAM_SORT_NOSORT 0
349 #define JETSAM_SORT_DEFAULT 1
350
351 #endif /* PRIVATE */
352
353 /* memorystatus_control() flags */
354
355 #define MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 0x1 /* A populated snapshot buffer is returned on demand */
356 #define MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 0x2 /* Returns a snapshot with memstats collected at boot */
357 #define MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 0x4 /* Returns the previously populated snapshot created by the system */
358 #define MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY 0x8 /* Set jetsam priorities for a group of pids */
359 #define MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY 0x10 /* Set probability of use for a group of processes */
360
361 /*
362 * For use with memorystatus_control:
363 * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
364 *
365 * A jetsam snapshot is initialized when a non-idle
366 * jetsam event occurs. The data is held in the
367 * buffer until it is reaped. This is the default
368 * behavior.
369 *
370 * Flags change the default behavior:
371 * Demand mode - this is an on_demand snapshot,
372 * meaning data is populated upon request.
373 *
374 * Boot mode - this is a snapshot of
375 * memstats collected before loading the
376 * init program. Once collected, these
377 * stats do not change. In this mode,
378 * the snapshot entry_count is always 0.
379 *
380 * Copy mode - this returns the previous snapshot
381 * collected by the system. The current snaphshot
382 * might be only half populated.
383 *
384 * Snapshots are inherently racey between request
385 * for buffer size and actual data compilation.
386 */
387
388 /* These definitions are required for backwards compatibility */
389 #define MEMORYSTATUS_SNAPSHOT_ON_DEMAND MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND
390 #define MEMORYSTATUS_SNAPSHOT_AT_BOOT MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT
391 #define MEMORYSTATUS_SNAPSHOT_COPY MEMORYSTATUS_FLAGS_SNAPSHOT_COPY
392
393 /*
394 * For use with memorystatus_control:
395 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
396 */
397 typedef struct memorystatus_priority_properties {
398 int32_t priority;
399 uint64_t user_data;
400 } memorystatus_priority_properties_t;
401
402 /*
403 * Inform the kernel that setting the priority property is driven by assertions.
404 */
405 #define MEMORYSTATUS_SET_PRIORITY_ASSERTION 0x1
406
407 /*
408 * For use with memorystatus_control:
409 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
410 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
411 */
412 typedef struct memorystatus_memlimit_properties {
413 int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */
414 uint32_t memlimit_active_attr;
415 int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */
416 uint32_t memlimit_inactive_attr;
417 } memorystatus_memlimit_properties_t;
418
419 typedef struct memorystatus_memlimit_properties2 {
420 memorystatus_memlimit_properties_t v1;
421 uint32_t memlimit_increase; /* jetsam memory limit increase (in MB) for active and inactive states */
422 uint32_t memlimit_increase_bytes; /* bytes used to determine the jetsam memory limit increase, for active and inactive states */
423 } memorystatus_memlimit_properties2_t;
424
425 #define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */
426
427 #ifdef XNU_KERNEL_PRIVATE
428
429 /*
430 * A process will be killed immediately if it crosses a memory limit marked as fatal.
431 * Fatal limit types are the
432 * - default system-wide task limit
433 * - per-task custom memory limit
434 *
435 * A process with a non-fatal memory limit can exceed that limit, but becomes an early
436 * candidate for jetsam when the device is under memory pressure.
437 * Non-fatal limit types are the
438 * - high-water-mark limit
439 *
440 * Processes that opt into dirty tracking are evaluated
441 * based on clean vs dirty state.
442 * dirty ==> active
443 * clean ==> inactive
444 *
445 * Processes that do not opt into dirty tracking are
446 * evalulated based on priority level.
447 * Foreground or above ==> active
448 * Below Foreground ==> inactive
449 */
450
451 /*
452 * p_memstat_state flag holds
453 * - in kernel process state and memlimit state
454 */
455
456 #define P_MEMSTAT_SUSPENDED 0x00000001 /* Process is suspended and likely in the IDLE band */
457 #define P_MEMSTAT_FROZEN 0x00000002 /* Process has some state on disk. It should be suspended */
458 #define P_MEMSTAT_FREEZE_DISABLED 0x00000004 /* Process isn't freeze-eligible and will not be frozen */
459 #define P_MEMSTAT_ERROR 0x00000008 /* Process couldn't be jetsammed for some reason. Transient state so jetsam can skip it next time it sees it */
460 #define P_MEMSTAT_LOCKED 0x00000010 /* Process is being actively worked on behind the proc_list_lock */
461 #define P_MEMSTAT_TERMINATED 0x00000020 /* Process is exiting */
462 #define P_MEMSTAT_FREEZE_IGNORE 0x00000040 /* Process was evaluated by freezer and will be ignored till the next time it goes active and does something */
463 #define P_MEMSTAT_PRIORITYUPDATED 0x00000080 /* Process had its jetsam priority updated */
464 #define P_MEMSTAT_FOREGROUND 0x00000100 /* Process is in the FG jetsam band...unused??? */
465 #define P_MEMSTAT_REFREEZE_ELIGIBLE 0x00000400 /* Process was once thawed i.e. its state was brought back from disk. It is now refreeze eligible.*/
466 #define P_MEMSTAT_MANAGED 0x00000800 /* Process is managed by assertiond i.e. is either application or extension */
467 #define P_MEMSTAT_INTERNAL 0x00001000 /* Process is a system-critical-not-be-jetsammed process i.e. launchd */
468 #define P_MEMSTAT_FATAL_MEMLIMIT 0x00002000 /* current fatal state of the process's memlimit */
469 #define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL 0x00004000 /* if set, exceeding limit is fatal when the process is active */
470 #define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00008000 /* if set, exceeding limit is fatal when the process is inactive */
471 #define P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND 0x00010000 /* if set, the process will go into this band & stay there when in the background instead
472 * of the aging bands and/or the IDLE band. */
473 #define P_MEMSTAT_PRIORITY_ASSERTION 0x00020000 /* jetsam priority is being driven by an assertion */
474
475
476 /*
477 * p_memstat_relaunch_flags holds
478 * - relaunch behavior when jetsammed
479 */
480 #define P_MEMSTAT_RELAUNCH_UNKNOWN 0x0
481 #define P_MEMSTAT_RELAUNCH_LOW 0x1
482 #define P_MEMSTAT_RELAUNCH_MED 0x2
483 #define P_MEMSTAT_RELAUNCH_HIGH 0x4
484
485 /*
486 * Checking the p_memstat_state almost always requires the proc_list_lock
487 * because the jetsam thread could be on the other core changing the state.
488 *
489 * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too.
490 * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too.
491 */
492 #define isApp(p) ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK))
493 #define isSysProc(p) ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK))
494
495 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1)
496
497 typedef struct memstat_bucket {
498 TAILQ_HEAD(, proc) list;
499 int count;
500 int relaunch_high_count;
501 } memstat_bucket_t;
502
503 extern memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT];
504
505 /*
506 * Table that expresses the probability of a process
507 * being used in the next hour.
508 */
509 typedef struct memorystatus_internal_probabilities {
510 char proc_name[MAXCOMLEN + 1];
511 int use_probability;
512 } memorystatus_internal_probabilities_t;
513
514 extern memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table;
515 extern size_t memorystatus_global_probabilities_size;
516
517
518 extern void memorystatus_init(void) __attribute__((section("__TEXT, initcode")));
519
520 extern void memorystatus_init_at_boot_snapshot(void);
521
522 extern int memorystatus_add(proc_t p, boolean_t locked);
523 extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t is_assertion, boolean_t effective,
524 boolean_t update_memlimit, int32_t memlimit_active, boolean_t memlimit_active_is_fatal,
525 int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal);
526
527 /* Remove this process from jetsam bands for killing or freezing.
528 * The proc_list_lock is held by the caller.
529 * @param p: The process to remove.
530 * @return: 0 if successful. EAGAIN if the process can't be removed right now (because it's being frozen) or ESRCH.
531 */
532 extern int memorystatus_remove(proc_t p);
533
534 int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now);
535 int memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags);
536
537 extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol);
538 extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol);
539 extern int memorystatus_dirty_get(proc_t p, boolean_t locked);
540 extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol);
541
542 extern int memorystatus_on_terminate(proc_t p);
543
544 extern void memorystatus_on_suspend(proc_t p);
545 extern void memorystatus_on_resume(proc_t p);
546 extern void memorystatus_on_inactivity(proc_t p);
547
548 extern void memorystatus_on_pageout_scan_end(void);
549
550 /* Memorystatus kevent */
551
552 void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr);
553
554 int memorystatus_knote_register(struct knote *kn);
555 void memorystatus_knote_unregister(struct knote *kn);
556
557 #if CONFIG_MEMORYSTATUS
558 void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
559 void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
560 void proc_memstat_terminated(proc_t p, boolean_t set);
561 void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit);
562
563 #if __arm64__
564 void memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase);
565 void memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p);
566 #endif /* __arm64__ */
567
568 #endif /* CONFIG_MEMORYSTATUS */
569
570 int memorystatus_get_pressure_status_kdp(void);
571
572 #if CONFIG_JETSAM
573
574 typedef enum memorystatus_policy {
575 kPolicyDefault = 0x0,
576 kPolicyMoreFree = 0x1,
577 } memorystatus_policy_t;
578
579 boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async);
580 boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async);
581 boolean_t memorystatus_kill_on_VM_compressor_thrashing(boolean_t async);
582 boolean_t memorystatus_kill_on_vnode_limit(void);
583
584 void jetsam_on_ledger_cpulimit_exceeded(void);
585 void memorystatus_fast_jetsam_override(boolean_t enable_override);
586
587 #endif /* CONFIG_JETSAM */
588
589 /* These are very verbose printfs(), enable with
590 * MEMORYSTATUS_DEBUG_LOG
591 */
592 #if MEMORYSTATUS_DEBUG_LOG
593 #define MEMORYSTATUS_DEBUG(cond, format, ...) \
594 do { \
595 if (cond) { printf(format, ##__VA_ARGS__); } \
596 } while(0)
597 #else
598 #define MEMORYSTATUS_DEBUG(cond, format, ...)
599 #endif
600
601 boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid);
602 boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async);
603 void memorystatus_pages_update(unsigned int pages_avail);
604 boolean_t memorystatus_idle_exit_from_VM(void);
605 proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search);
606 proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search);
607 void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages);
608 void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state);
609 void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check);
610
611 #if VM_PRESSURE_EVENTS
612
613 extern kern_return_t memorystatus_update_vm_pressure(boolean_t);
614
615 #if CONFIG_MEMORYSTATUS
616 /* Flags */
617 extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags);
618 extern int memorystatus_send_pressure_note(int pid);
619 extern boolean_t memorystatus_is_foreground_locked(proc_t p);
620 extern boolean_t memorystatus_bg_pressure_eligible(proc_t p);
621 #endif /* CONFIG_MEMORYSTATUS */
622
623 #endif /* VM_PRESSURE_EVENTS */
624
625 #endif /* XNU_KERNEL_PRIVATE */
626
627 #endif /* SYS_MEMORYSTATUS_H */