]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kern_memorystatus.h
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / sys / kern_memorystatus.h
CommitLineData
2d21ac55 1/*
d9a64523 2 * Copyright (c) 2006-2018 Apple Computer, 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 */
2d21ac55 28
316670eb
A
29#ifndef SYS_MEMORYSTATUS_H
30#define SYS_MEMORYSTATUS_H
b0d623f7 31
b0d623f7 32#include <sys/time.h>
5ba3f43e 33#include <mach_debug/zone_info.h>
cb323159 34#include <sys/proc.h>
316670eb 35
39037602
A
36#define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus"
37
39236c6e 38#define JETSAM_PRIORITY_REVISION 2
316670eb 39
fe8ab488
A
40#define JETSAM_PRIORITY_IDLE_HEAD -2
41/* The value -1 is an alias to JETSAM_PRIORITY_DEFAULT */
39236c6e 42#define JETSAM_PRIORITY_IDLE 0
0a7de745
A
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
39236c6e 45#define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 2
0a7de745 46#define JETSAM_PRIORITY_AGING_BAND2 JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC
39236c6e 47#define JETSAM_PRIORITY_BACKGROUND 3
0a7de745 48#define JETSAM_PRIORITY_ELEVATED_INACTIVE JETSAM_PRIORITY_BACKGROUND
39236c6e
A
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
cb323159 56#define JETSAM_PRIORITY_DRIVER_APPLE 15
39236c6e
A
57#define JETSAM_PRIORITY_HOME 16
58#define JETSAM_PRIORITY_EXECUTIVE 17
59#define JETSAM_PRIORITY_IMPORTANT 18
60#define JETSAM_PRIORITY_CRITICAL 19
316670eb 61
39236c6e 62#define JETSAM_PRIORITY_MAX 21
b0d623f7 63
39236c6e
A
64/* TODO - tune. This should probably be lower priority */
65#define JETSAM_PRIORITY_DEFAULT 18
66#define JETSAM_PRIORITY_TELEPHONY 19
2d21ac55 67
39236c6e
A
68/* Compatibility */
69#define DEFAULT_JETSAM_PRIORITY 18
70
cb323159
A
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 */
39236c6e
A
86#define DEFERRED_IDLE_EXIT_TIME_SECS 10
87
88#define KEV_MEMORYSTATUS_SUBCLASS 3
2d21ac55 89
b0d623f7 90enum {
316670eb
A
91 kMemorystatusLevelNote = 1,
92 kMemorystatusSnapshotNote = 2,
93 kMemorystatusFreezeNote = 3,
94 kMemorystatusPressureNote = 4
b0d623f7
A
95};
96
2d21ac55 97enum {
316670eb
A
98 kMemorystatusLevelAny = -1,
99 kMemorystatusLevelNormal = 0,
100 kMemorystatusLevelWarning = 1,
101 kMemorystatusLevelUrgent = 2,
102 kMemorystatusLevelCritical = 3
2d21ac55
A
103};
104
316670eb 105typedef struct memorystatus_priority_entry {
b0d623f7 106 pid_t pid;
316670eb 107 int32_t priority;
39236c6e 108 uint64_t user_data;
0a7de745 109 int32_t limit; /* MB */
39236c6e 110 uint32_t state;
316670eb 111} memorystatus_priority_entry_t;
b0d623f7 112
d9a64523
A
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
0a7de745 120 * new version and a new size.
d9a64523 121 */
0a7de745 122#define MEMORYSTATUS_MPE_VERSION_1 1
d9a64523 123
0a7de745 124#define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1)
d9a64523 125
0a7de745 126typedef struct memorystatus_properties_entry_v1 {
d9a64523
A
127 int version;
128 pid_t pid;
129 int32_t priority;
130 int use_probability;
131 uint64_t user_data;
0a7de745 132 int32_t limit; /* MB */
d9a64523 133 uint32_t state;
0a7de745 134 char proc_name[MAXCOMLEN + 1];
d9a64523
A
135 char __pad1[3];
136} memorystatus_properties_entry_v1_t;
0a7de745 137
316670eb 138typedef struct memorystatus_kernel_stats {
b0d623f7
A
139 uint32_t free_pages;
140 uint32_t active_pages;
141 uint32_t inactive_pages;
316670eb 142 uint32_t throttled_pages;
b0d623f7
A
143 uint32_t purgeable_pages;
144 uint32_t wired_pages;
39236c6e
A
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;
5ba3f43e
A
152 uint64_t zone_map_size;
153 uint64_t zone_map_capacity;
154 uint64_t largest_zone_size;
0a7de745 155 char largest_zone_name[MACH_ZONE_NAME_MAX_LEN];
316670eb 156} memorystatus_kernel_stats_t;
b0d623f7
A
157
158/*
159** This is a variable-length struct.
316670eb 160** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t *
b0d623f7
A
161*/
162
316670eb 163typedef struct jetsam_snapshot_entry {
39236c6e 164 pid_t pid;
0a7de745 165 char name[(2 * MAXCOMLEN) + 1];
39236c6e 166 int32_t priority;
39236c6e 167 uint32_t state;
39236c6e 168 uint32_t fds;
39037602
A
169 uint8_t uuid[16];
170 uint64_t user_data;
171 uint64_t killed;
172 uint64_t pages;
39037602
A
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;
0a7de745
A
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 */
d9a64523
A
189 struct timeval64 cpu_time;
190 uint64_t jse_thaw_count;
316670eb
A
191} memorystatus_jetsam_snapshot_entry_t;
192
b0d623f7 193typedef struct jetsam_snapshot {
0a7de745
A
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 */
b0d623f7 198 size_t entry_count;
39236c6e 199 memorystatus_jetsam_snapshot_entry_t entries[];
316670eb
A
200} memorystatus_jetsam_snapshot_t;
201
39236c6e
A
202/* TODO - deprecate; see <rdar://problem/12969599> */
203#define kMaxSnapshotEntries 192
316670eb 204
cb323159
A
205/*
206 * default jetsam snapshot support
207 */
208extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot;
209extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy;
210extern unsigned int memorystatus_jetsam_snapshot_count;
211extern unsigned int memorystatus_jetsam_snapshot_copy_count;
212extern unsigned int memorystatus_jetsam_snapshot_max;
213extern unsigned int memorystatus_jetsam_snapshot_size;
214extern uint64_t memorystatus_jetsam_snapshot_last_timestamp;
215extern 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
221extern uint64_t memorystatus_sysprocs_idle_delay_time;
222extern uint64_t memorystatus_apps_idle_delay_time;
223
39236c6e
A
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
cb323159 231#define kMemorystatusAssertion 0x40
6d2010ae 232
5ba3f43e
A
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 */
0a7de745
A
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
cb323159
A
254#define JETSAM_REASON_LOWSWAP 13
255#define JETSAM_REASON_MEMORYSTATUS_MAX JETSAM_REASON_LOWSWAP
5ba3f43e
A
256
257/*
258 * Jetsam exit reason definitions - not related to memorystatus
259 */
0a7de745 260#define JETSAM_REASON_CPULIMIT 100
39037602 261
5ba3f43e 262/* Cause */
b0d623f7 263enum {
0a7de745
A
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,
cb323159 277 kMemorystatusKilledLowSwap = JETSAM_REASON_LOWSWAP,
b0d623f7 278};
b0d623f7 279
cb323159
A
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 */
0a7de745
A
285#define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage
286#define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing
287#define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING
d9a64523 288
39236c6e
A
289/* Memorystatus control */
290#define MEMORYSTATUS_BUFFERSIZE_MAX 65536
291
292#ifndef KERNEL
293int memorystatus_get_level(user_addr_t level);
294int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize);
316670eb 295#endif
316670eb 296
39236c6e
A
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
3e170ce0 302#define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */
0a7de745 303#define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */
3e170ce0
A
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 */
490019cf
A
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. */
39037602 310#define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */
0a7de745
A
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)*/
d9a64523
A
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.,
0a7de745 316 * if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */
d9a64523
A
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
0a7de745 321#define MEMORYSTATUS_CMD_FREEZER_CONTROL 20
d9a64523
A
322#endif /* DEVELOPMENT || DEBUG */
323#endif /* CONFIG_FREEZE */
39037602 324
cb323159
A
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
3e170ce0
A
329/* Commands that act on a group of processes */
330#define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100
39236c6e
A
331
332#if PRIVATE
333/* Test commands */
334
335/* Trigger forced jetsam */
0a7de745
A
336#define MEMORYSTATUS_CMD_TEST_JETSAM 1000
337#define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001
39236c6e
A
338
339/* Panic on jetsam options */
340typedef struct memorystatus_jetsam_panic_options {
341 uint32_t data;
342 uint32_t mask;
343} memorystatus_jetsam_panic_options_t;
316670eb 344
3e170ce0
A
345#define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002
346
347/* Select priority band sort order */
0a7de745
A
348#define JETSAM_SORT_NOSORT 0
349#define JETSAM_SORT_DEFAULT 1
3e170ce0 350
39236c6e
A
351#endif /* PRIVATE */
352
d9a64523
A
353/* memorystatus_control() flags */
354
0a7de745
A
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 */
d9a64523 360
3e170ce0
A
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 *
d9a64523 380 * Copy mode - this returns the previous snapshot
0a7de745 381 * collected by the system. The current snaphshot
d9a64523
A
382 * might be only half populated.
383 *
3e170ce0
A
384 * Snapshots are inherently racey between request
385 * for buffer size and actual data compilation.
0a7de745 386 */
3e170ce0 387
d9a64523 388/* These definitions are required for backwards compatibility */
0a7de745
A
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
3e170ce0
A
392
393/*
394 * For use with memorystatus_control:
395 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
396 */
39236c6e
A
397typedef struct memorystatus_priority_properties {
398 int32_t priority;
399 uint64_t user_data;
400} memorystatus_priority_properties_t;
401
cb323159
A
402/*
403 * Inform the kernel that setting the priority property is driven by assertions.
404 */
405#define MEMORYSTATUS_SET_PRIORITY_ASSERTION 0x1
406
3e170ce0
A
407/*
408 * For use with memorystatus_control:
409 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
410 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
411 */
412typedef struct memorystatus_memlimit_properties {
0a7de745 413 int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */
3e170ce0 414 uint32_t memlimit_active_attr;
0a7de745 415 int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */
3e170ce0
A
416 uint32_t memlimit_inactive_attr;
417} memorystatus_memlimit_properties_t;
418
cb323159
A
419typedef 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
0a7de745 425#define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */
3e170ce0 426
39236c6e
A
427#ifdef XNU_KERNEL_PRIVATE
428
3e170ce0
A
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 *
3e170ce0
A
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 */
39236c6e 455
d9a64523
A
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??? */
d9a64523
A
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 */
3e170ce0
A
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 */
813fb2f6 470#define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00008000 /* if set, exceeding limit is fatal when the process is inactive */
0a7de745
A
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. */
cb323159
A
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
497typedef struct memstat_bucket {
498 TAILQ_HEAD(, proc) list;
499 int count;
500 int relaunch_high_count;
501} memstat_bucket_t;
502
503extern 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 */
509typedef struct memorystatus_internal_probabilities {
510 char proc_name[MAXCOMLEN + 1];
511 int use_probability;
512} memorystatus_internal_probabilities_t;
513
514extern memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table;
515extern size_t memorystatus_global_probabilities_size;
516
316670eb
A
517
518extern void memorystatus_init(void) __attribute__((section("__TEXT, initcode")));
519
3e170ce0
A
520extern void memorystatus_init_at_boot_snapshot(void);
521
39236c6e 522extern int memorystatus_add(proc_t p, boolean_t locked);
cb323159 523extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t is_assertion, boolean_t effective,
0a7de745
A
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);
3e170ce0 526
cb323159
A
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 */
532extern int memorystatus_remove(proc_t p);
39236c6e 533
d9a64523 534int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now);
cb323159 535int memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags);
39037602 536
39236c6e
A
537extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol);
538extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol);
cb323159 539extern int memorystatus_dirty_get(proc_t p, boolean_t locked);
fe8ab488 540extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol);
39236c6e
A
541
542extern int memorystatus_on_terminate(proc_t p);
543
544extern void memorystatus_on_suspend(proc_t p);
545extern void memorystatus_on_resume(proc_t p);
546extern void memorystatus_on_inactivity(proc_t p);
547
548extern void memorystatus_on_pageout_scan_end(void);
549
550/* Memorystatus kevent */
316670eb 551
39236c6e 552void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr);
316670eb 553
39236c6e
A
554int memorystatus_knote_register(struct knote *kn);
555void memorystatus_knote_unregister(struct knote *kn);
316670eb 556
39037602 557#if CONFIG_MEMORYSTATUS
813fb2f6
A
558void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
559void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
39037602 560void proc_memstat_terminated(proc_t p, boolean_t set);
d9a64523 561void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit);
cb323159
A
562
563#if __arm64__
564void memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase);
94ff46dc 565void memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p);
cb323159
A
566#endif /* __arm64__ */
567
39037602
A
568#endif /* CONFIG_MEMORYSTATUS */
569
3e170ce0
A
570int memorystatus_get_pressure_status_kdp(void);
571
5ba3f43e
A
572#if CONFIG_JETSAM
573
39236c6e 574typedef enum memorystatus_policy {
0a7de745 575 kPolicyDefault = 0x0,
316670eb 576 kPolicyMoreFree = 0x1,
316670eb
A
577} memorystatus_policy_t;
578
39236c6e 579boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async);
fe8ab488 580boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async);
d9a64523 581boolean_t memorystatus_kill_on_VM_compressor_thrashing(boolean_t async);
39236c6e 582boolean_t memorystatus_kill_on_vnode_limit(void);
316670eb 583
fe8ab488 584void jetsam_on_ledger_cpulimit_exceeded(void);
d9a64523 585void memorystatus_fast_jetsam_override(boolean_t enable_override);
316670eb 586
5ba3f43e 587#endif /* CONFIG_JETSAM */
316670eb 588
cb323159
A
589/* These are very verbose printfs(), enable with
590 * MEMORYSTATUS_DEBUG_LOG
591 */
592#if MEMORYSTATUS_DEBUG_LOG
593#define MEMORYSTATUS_DEBUG(cond, format, ...) \
594do { \
595if (cond) { printf(format, ##__VA_ARGS__); } \
596} while(0)
597#else
598#define MEMORYSTATUS_DEBUG(cond, format, ...)
599#endif
600
5ba3f43e 601boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid);
d9a64523 602boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async);
5ba3f43e 603void memorystatus_pages_update(unsigned int pages_avail);
39236c6e 604boolean_t memorystatus_idle_exit_from_VM(void);
cb323159
A
605proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search);
606proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search);
607void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages);
608void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state);
609void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check);
316670eb 610
39236c6e
A
611#if VM_PRESSURE_EVENTS
612
39236c6e
A
613extern kern_return_t memorystatus_update_vm_pressure(boolean_t);
614
fe8ab488 615#if CONFIG_MEMORYSTATUS
3e170ce0
A
616/* Flags */
617extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags);
39236c6e 618extern int memorystatus_send_pressure_note(int pid);
fe8ab488 619extern boolean_t memorystatus_is_foreground_locked(proc_t p);
39236c6e 620extern boolean_t memorystatus_bg_pressure_eligible(proc_t p);
fe8ab488 621#endif /* CONFIG_MEMORYSTATUS */
39236c6e
A
622
623#endif /* VM_PRESSURE_EVENTS */
624
316670eb 625#endif /* XNU_KERNEL_PRIVATE */
2d21ac55 626
316670eb 627#endif /* SYS_MEMORYSTATUS_H */