]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kern_memorystatus.h
xnu-7195.101.1.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
f427ee49
A
105#define KEV_DIRTYSTATUS_SUBCLASS 4
106
107enum {
108 kDirtyStatusChangeNote = 1
109};
110
316670eb 111typedef struct memorystatus_priority_entry {
b0d623f7 112 pid_t pid;
316670eb 113 int32_t priority;
39236c6e 114 uint64_t user_data;
0a7de745 115 int32_t limit; /* MB */
39236c6e 116 uint32_t state;
316670eb 117} memorystatus_priority_entry_t;
b0d623f7 118
d9a64523
A
119/*
120 * This should be the structure to specify different properties
121 * for processes (group or single) from user-space. Unfortunately,
122 * we can't move to it completely because the priority_entry structure
123 * above has been in use for a while now. We'll have to deprecate it.
124 *
125 * To support new fields/properties, we will add a new structure with a
0a7de745 126 * new version and a new size.
d9a64523 127 */
0a7de745 128#define MEMORYSTATUS_MPE_VERSION_1 1
d9a64523 129
0a7de745 130#define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1)
d9a64523 131
0a7de745 132typedef struct memorystatus_properties_entry_v1 {
d9a64523
A
133 int version;
134 pid_t pid;
135 int32_t priority;
136 int use_probability;
137 uint64_t user_data;
0a7de745 138 int32_t limit; /* MB */
d9a64523 139 uint32_t state;
0a7de745 140 char proc_name[MAXCOMLEN + 1];
d9a64523
A
141 char __pad1[3];
142} memorystatus_properties_entry_v1_t;
0a7de745 143
316670eb 144typedef struct memorystatus_kernel_stats {
b0d623f7
A
145 uint32_t free_pages;
146 uint32_t active_pages;
147 uint32_t inactive_pages;
316670eb 148 uint32_t throttled_pages;
b0d623f7
A
149 uint32_t purgeable_pages;
150 uint32_t wired_pages;
39236c6e
A
151 uint32_t speculative_pages;
152 uint32_t filebacked_pages;
153 uint32_t anonymous_pages;
154 uint32_t compressor_pages;
155 uint64_t compressions;
156 uint64_t decompressions;
157 uint64_t total_uncompressed_pages_in_compressor;
5ba3f43e
A
158 uint64_t zone_map_size;
159 uint64_t zone_map_capacity;
160 uint64_t largest_zone_size;
0a7de745 161 char largest_zone_name[MACH_ZONE_NAME_MAX_LEN];
316670eb 162} memorystatus_kernel_stats_t;
b0d623f7 163
2a1bd2d3
A
164typedef enum memorystatus_freeze_skip_reason {
165 kMemorystatusFreezeSkipReasonNone = 0,
166 kMemorystatusFreezeSkipReasonExcessSharedMemory = 1,
167 kMemorystatusFreezeSkipReasonLowPrivateSharedRatio = 2,
168 kMemorystatusFreezeSkipReasonNoCompressorSpace = 3,
169 kMemorystatusFreezeSkipReasonNoSwapSpace = 4,
170 kMemorystatusFreezeSkipReasonBelowMinPages = 5,
171 kMemorystatusFreezeSkipReasonLowProbOfUse = 6,
172 kMemorystatusFreezeSkipReasonOther = 7,
173 kMemorystatusFreezeSkipReasonOutOfBudget = 8,
174 kMemorystatusFreezeSkipReasonOutOfSlots = 9,
175 kMemorystatusFreezeSkipReasonDisabled = 10,
176 _kMemorystatusFreezeSkipReasonMax
177} memorystatus_freeze_skip_reason_t;
b0d623f7
A
178/*
179** This is a variable-length struct.
316670eb 180** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t *
b0d623f7
A
181*/
182
316670eb 183typedef struct jetsam_snapshot_entry {
39236c6e 184 pid_t pid;
0a7de745 185 char name[(2 * MAXCOMLEN) + 1];
39236c6e 186 int32_t priority;
39236c6e 187 uint32_t state;
39236c6e 188 uint32_t fds;
2a1bd2d3 189 memorystatus_freeze_skip_reason_t jse_freeze_skip_reason; /* why wasn't this process frozen? */
39037602
A
190 uint8_t uuid[16];
191 uint64_t user_data;
192 uint64_t killed;
193 uint64_t pages;
39037602
A
194 uint64_t max_pages_lifetime;
195 uint64_t purgeable_pages;
196 uint64_t jse_internal_pages;
197 uint64_t jse_internal_compressed_pages;
198 uint64_t jse_purgeable_nonvolatile_pages;
199 uint64_t jse_purgeable_nonvolatile_compressed_pages;
200 uint64_t jse_alternate_accounting_pages;
201 uint64_t jse_alternate_accounting_compressed_pages;
202 uint64_t jse_iokit_mapped_pages;
203 uint64_t jse_page_table_pages;
204 uint64_t jse_memory_region_count;
0a7de745
A
205 uint64_t jse_gencount; /* memorystatus_thread generation counter */
206 uint64_t jse_starttime; /* absolute time when process starts */
207 uint64_t jse_killtime; /* absolute time when jetsam chooses to kill a process */
208 uint64_t jse_idle_delta; /* time spent in idle band */
209 uint64_t jse_coalition_jetsam_id; /* we only expose coalition id for COALITION_TYPE_JETSAM */
d9a64523
A
210 struct timeval64 cpu_time;
211 uint64_t jse_thaw_count;
f427ee49 212 uint64_t jse_frozen_to_swap_pages;
316670eb
A
213} memorystatus_jetsam_snapshot_entry_t;
214
b0d623f7 215typedef struct jetsam_snapshot {
0a7de745
A
216 uint64_t snapshot_time; /* absolute time snapshot was initialized */
217 uint64_t notification_time; /* absolute time snapshot was consumed */
218 uint64_t js_gencount; /* memorystatus_thread generation counter */
219 memorystatus_kernel_stats_t stats; /* system stat when snapshot is initialized */
b0d623f7 220 size_t entry_count;
39236c6e 221 memorystatus_jetsam_snapshot_entry_t entries[];
316670eb
A
222} memorystatus_jetsam_snapshot_t;
223
f427ee49
A
224typedef enum dirty_status_change_event_type {
225 kDirtyStatusChangedDirty = 0x0,
226 kDirtyStatusChangedClean = 0x1
227} dirty_status_change_event_type_t;
228
229typedef struct dirty_status_change_event {
230 pid_t dsc_pid;
231 char dsc_process_name[(2 * MAXCOMLEN) + 1];
232 dirty_status_change_event_type_t dsc_event_type;
233 uint64_t dsc_time;
234 uint64_t dsc_pages;
235 int32_t dsc_priority;
236} dirty_status_change_event_t;
237
39236c6e
A
238/* TODO - deprecate; see <rdar://problem/12969599> */
239#define kMaxSnapshotEntries 192
316670eb 240
cb323159
A
241/*
242 * default jetsam snapshot support
243 */
244extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot;
245extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy;
f427ee49
A
246#if CONFIG_FREEZE
247extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_freezer;
248extern unsigned int memorystatus_jetsam_snapshot_freezer_max;
249extern unsigned int memorystatus_jetsam_snapshot_freezer_size;
250#endif /* CONFIG_FREEZE */
cb323159
A
251extern unsigned int memorystatus_jetsam_snapshot_count;
252extern unsigned int memorystatus_jetsam_snapshot_copy_count;
253extern unsigned int memorystatus_jetsam_snapshot_max;
254extern unsigned int memorystatus_jetsam_snapshot_size;
255extern uint64_t memorystatus_jetsam_snapshot_last_timestamp;
256extern uint64_t memorystatus_jetsam_snapshot_timeout;
257#define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries
258#define JETSAM_SNAPSHOT_TIMEOUT_SECS 30
259
260/* General memorystatus stuff */
261
262extern uint64_t memorystatus_sysprocs_idle_delay_time;
263extern uint64_t memorystatus_apps_idle_delay_time;
264
39236c6e
A
265/* State */
266#define kMemorystatusSuspended 0x01
267#define kMemorystatusFrozen 0x02
268#define kMemorystatusWasThawed 0x04
269#define kMemorystatusTracked 0x08
270#define kMemorystatusSupportsIdleExit 0x10
271#define kMemorystatusDirty 0x20
cb323159 272#define kMemorystatusAssertion 0x40
6d2010ae 273
5ba3f43e
A
274/*
275 * Jetsam exit reason definitions - related to memorystatus
276 *
277 * When adding new exit reasons also update:
278 * JETSAM_REASON_MEMORYSTATUS_MAX
279 * kMemorystatusKilled... Cause enum
280 * memorystatus_kill_cause_name[]
281 */
0a7de745
A
282#define JETSAM_REASON_INVALID 0
283#define JETSAM_REASON_GENERIC 1
284#define JETSAM_REASON_MEMORY_HIGHWATER 2
285#define JETSAM_REASON_VNODE 3
286#define JETSAM_REASON_MEMORY_VMPAGESHORTAGE 4
287#define JETSAM_REASON_MEMORY_PROCTHRASHING 5
288#define JETSAM_REASON_MEMORY_FCTHRASHING 6
289#define JETSAM_REASON_MEMORY_PERPROCESSLIMIT 7
290#define JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE 8
291#define JETSAM_REASON_MEMORY_IDLE_EXIT 9
292#define JETSAM_REASON_ZONE_MAP_EXHAUSTION 10
293#define JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 11
294#define JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE 12
cb323159
A
295#define JETSAM_REASON_LOWSWAP 13
296#define JETSAM_REASON_MEMORYSTATUS_MAX JETSAM_REASON_LOWSWAP
5ba3f43e
A
297
298/*
299 * Jetsam exit reason definitions - not related to memorystatus
300 */
0a7de745 301#define JETSAM_REASON_CPULIMIT 100
39037602 302
5ba3f43e 303/* Cause */
b0d623f7 304enum {
0a7de745
A
305 kMemorystatusInvalid = JETSAM_REASON_INVALID,
306 kMemorystatusKilled = JETSAM_REASON_GENERIC,
307 kMemorystatusKilledHiwat = JETSAM_REASON_MEMORY_HIGHWATER,
308 kMemorystatusKilledVnodes = JETSAM_REASON_VNODE,
309 kMemorystatusKilledVMPageShortage = JETSAM_REASON_MEMORY_VMPAGESHORTAGE,
310 kMemorystatusKilledProcThrashing = JETSAM_REASON_MEMORY_PROCTHRASHING,
311 kMemorystatusKilledFCThrashing = JETSAM_REASON_MEMORY_FCTHRASHING,
312 kMemorystatusKilledPerProcessLimit = JETSAM_REASON_MEMORY_PERPROCESSLIMIT,
313 kMemorystatusKilledDiskSpaceShortage = JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE,
314 kMemorystatusKilledIdleExit = JETSAM_REASON_MEMORY_IDLE_EXIT,
315 kMemorystatusKilledZoneMapExhaustion = JETSAM_REASON_ZONE_MAP_EXHAUSTION,
316 kMemorystatusKilledVMCompressorThrashing = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING,
317 kMemorystatusKilledVMCompressorSpaceShortage = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE,
cb323159 318 kMemorystatusKilledLowSwap = JETSAM_REASON_LOWSWAP,
b0d623f7 319};
b0d623f7 320
cb323159
A
321/*
322 * For backwards compatibility
323 * Keeping these around for external users (e.g. ReportCrash, Ariadne).
324 * TODO: Remove once they stop using these.
325 */
0a7de745
A
326#define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage
327#define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing
328#define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING
d9a64523 329
39236c6e
A
330/* Memorystatus control */
331#define MEMORYSTATUS_BUFFERSIZE_MAX 65536
332
333#ifndef KERNEL
334int memorystatus_get_level(user_addr_t level);
335int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize);
316670eb 336#endif
316670eb 337
39236c6e
A
338/* Commands */
339#define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1
340#define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2
341#define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3
342#define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4
3e170ce0 343#define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */
0a7de745 344#define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */
3e170ce0
A
345#define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 7 /* Set memory limits plus attributes independently */
346#define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 8 /* Get memory limits plus attributes */
347#define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE 9 /* Set the task's status as a privileged listener w.r.t memory notifications */
348#define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE 10 /* Reset the task's status as a privileged listener w.r.t memory notifications */
490019cf
A
349#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. */
350#define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12 /* Disable the 'lenient' mode for aggressive jetsam. */
39037602 351#define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */
0a7de745
A
352#define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE 14 /* Set the inactive jetsam band for a process to JETSAM_PRIORITY_ELEVATED_INACTIVE */
353#define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE 15 /* Reset the inactive jetsam band for a process to the default band (0)*/
d9a64523
A
354#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 */
355#define MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED 17 /* Return the 'managed' status of a process */
356#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 357 * if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */
d9a64523
A
358#define MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE 19 /* Return the freezable state of a process. */
359
0a7de745 360#define MEMORYSTATUS_CMD_FREEZER_CONTROL 20
39037602 361
cb323159
A
362#define MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE 21 /* Query if the lenient mode for aggressive jetsam is enabled. */
363
364#define MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT 22 /* Used by DYLD to increase the jetsam active and inactive limits, when using roots */
365
f427ee49 366#if PRIVATE
c3c9b80d 367#define MEMORYSTATUS_CMD_SET_TESTING_PID 23 /* Used by unit tests in the development kernel only. */
f427ee49
A
368#endif /* PRIVATE */
369
2a1bd2d3
A
370#define MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN 24 /* Check if the process is frozen. */
371
3e170ce0
A
372/* Commands that act on a group of processes */
373#define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100
39236c6e
A
374
375#if PRIVATE
376/* Test commands */
377
378/* Trigger forced jetsam */
0a7de745
A
379#define MEMORYSTATUS_CMD_TEST_JETSAM 1000
380#define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001
39236c6e
A
381
382/* Panic on jetsam options */
383typedef struct memorystatus_jetsam_panic_options {
384 uint32_t data;
385 uint32_t mask;
386} memorystatus_jetsam_panic_options_t;
316670eb 387
3e170ce0
A
388#define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002
389
390/* Select priority band sort order */
0a7de745
A
391#define JETSAM_SORT_NOSORT 0
392#define JETSAM_SORT_DEFAULT 1
3e170ce0 393
39236c6e
A
394#endif /* PRIVATE */
395
d9a64523
A
396/* memorystatus_control() flags */
397
0a7de745
A
398#define MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 0x1 /* A populated snapshot buffer is returned on demand */
399#define MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 0x2 /* Returns a snapshot with memstats collected at boot */
400#define MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 0x4 /* Returns the previously populated snapshot created by the system */
401#define MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY 0x8 /* Set jetsam priorities for a group of pids */
402#define MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY 0x10 /* Set probability of use for a group of processes */
d9a64523 403
f427ee49 404#if PRIVATE
c3c9b80d
A
405#define MEMORYSTATUS_FLAGS_SET_TESTING_PID 0x20 /* Only used by xnu unit tests. */
406#define MEMORYSTATUS_FLAGS_UNSET_TESTING_PID 0x40 /* Only used by xnu unit tests. */
f427ee49
A
407#endif /* PRIVATE */
408
409#define MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER 0x80 /* A snapshot buffer containing app kills since last consumption */
3e170ce0
A
410/*
411 * For use with memorystatus_control:
412 * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
413 *
414 * A jetsam snapshot is initialized when a non-idle
415 * jetsam event occurs. The data is held in the
416 * buffer until it is reaped. This is the default
417 * behavior.
418 *
419 * Flags change the default behavior:
420 * Demand mode - this is an on_demand snapshot,
421 * meaning data is populated upon request.
422 *
423 * Boot mode - this is a snapshot of
424 * memstats collected before loading the
425 * init program. Once collected, these
426 * stats do not change. In this mode,
427 * the snapshot entry_count is always 0.
428 *
d9a64523 429 * Copy mode - this returns the previous snapshot
0a7de745 430 * collected by the system. The current snaphshot
d9a64523
A
431 * might be only half populated.
432 *
3e170ce0
A
433 * Snapshots are inherently racey between request
434 * for buffer size and actual data compilation.
0a7de745 435 */
3e170ce0 436
d9a64523 437/* These definitions are required for backwards compatibility */
0a7de745
A
438#define MEMORYSTATUS_SNAPSHOT_ON_DEMAND MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND
439#define MEMORYSTATUS_SNAPSHOT_AT_BOOT MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT
440#define MEMORYSTATUS_SNAPSHOT_COPY MEMORYSTATUS_FLAGS_SNAPSHOT_COPY
3e170ce0
A
441
442/*
443 * For use with memorystatus_control:
444 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
445 */
39236c6e
A
446typedef struct memorystatus_priority_properties {
447 int32_t priority;
448 uint64_t user_data;
449} memorystatus_priority_properties_t;
450
cb323159
A
451/*
452 * Inform the kernel that setting the priority property is driven by assertions.
453 */
454#define MEMORYSTATUS_SET_PRIORITY_ASSERTION 0x1
455
3e170ce0
A
456/*
457 * For use with memorystatus_control:
458 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
459 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
460 */
461typedef struct memorystatus_memlimit_properties {
0a7de745 462 int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */
3e170ce0 463 uint32_t memlimit_active_attr;
0a7de745 464 int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */
3e170ce0
A
465 uint32_t memlimit_inactive_attr;
466} memorystatus_memlimit_properties_t;
467
cb323159
A
468typedef struct memorystatus_memlimit_properties2 {
469 memorystatus_memlimit_properties_t v1;
470 uint32_t memlimit_increase; /* jetsam memory limit increase (in MB) for active and inactive states */
471 uint32_t memlimit_increase_bytes; /* bytes used to determine the jetsam memory limit increase, for active and inactive states */
472} memorystatus_memlimit_properties2_t;
473
0a7de745 474#define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */
3e170ce0 475
39236c6e
A
476#ifdef XNU_KERNEL_PRIVATE
477
3e170ce0
A
478/*
479 * A process will be killed immediately if it crosses a memory limit marked as fatal.
480 * Fatal limit types are the
481 * - default system-wide task limit
482 * - per-task custom memory limit
483 *
484 * A process with a non-fatal memory limit can exceed that limit, but becomes an early
485 * candidate for jetsam when the device is under memory pressure.
486 * Non-fatal limit types are the
487 * - high-water-mark limit
488 *
3e170ce0
A
489 * Processes that opt into dirty tracking are evaluated
490 * based on clean vs dirty state.
491 * dirty ==> active
492 * clean ==> inactive
493 *
494 * Processes that do not opt into dirty tracking are
495 * evalulated based on priority level.
496 * Foreground or above ==> active
497 * Below Foreground ==> inactive
498 */
499
500/*
501 * p_memstat_state flag holds
502 * - in kernel process state and memlimit state
503 */
39236c6e 504
d9a64523
A
505#define P_MEMSTAT_SUSPENDED 0x00000001 /* Process is suspended and likely in the IDLE band */
506#define P_MEMSTAT_FROZEN 0x00000002 /* Process has some state on disk. It should be suspended */
507#define P_MEMSTAT_FREEZE_DISABLED 0x00000004 /* Process isn't freeze-eligible and will not be frozen */
508#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 */
509#define P_MEMSTAT_LOCKED 0x00000010 /* Process is being actively worked on behind the proc_list_lock */
510#define P_MEMSTAT_TERMINATED 0x00000020 /* Process is exiting */
511#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 */
512#define P_MEMSTAT_PRIORITYUPDATED 0x00000080 /* Process had its jetsam priority updated */
513#define P_MEMSTAT_FOREGROUND 0x00000100 /* Process is in the FG jetsam band...unused??? */
d9a64523
A
514#define P_MEMSTAT_REFREEZE_ELIGIBLE 0x00000400 /* Process was once thawed i.e. its state was brought back from disk. It is now refreeze eligible.*/
515#define P_MEMSTAT_MANAGED 0x00000800 /* Process is managed by assertiond i.e. is either application or extension */
516#define P_MEMSTAT_INTERNAL 0x00001000 /* Process is a system-critical-not-be-jetsammed process i.e. launchd */
3e170ce0
A
517#define P_MEMSTAT_FATAL_MEMLIMIT 0x00002000 /* current fatal state of the process's memlimit */
518#define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL 0x00004000 /* if set, exceeding limit is fatal when the process is active */
813fb2f6 519#define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00008000 /* if set, exceeding limit is fatal when the process is inactive */
0a7de745
A
520#define P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND 0x00010000 /* if set, the process will go into this band & stay there when in the background instead
521 * of the aging bands and/or the IDLE band. */
cb323159 522#define P_MEMSTAT_PRIORITY_ASSERTION 0x00020000 /* jetsam priority is being driven by an assertion */
f427ee49 523#define P_MEMSTAT_FREEZE_CONSIDERED 0x00040000 /* This process has been considered for the freezer. */
cb323159 524
cb323159
A
525/*
526 * p_memstat_relaunch_flags holds
527 * - relaunch behavior when jetsammed
528 */
529#define P_MEMSTAT_RELAUNCH_UNKNOWN 0x0
530#define P_MEMSTAT_RELAUNCH_LOW 0x1
531#define P_MEMSTAT_RELAUNCH_MED 0x2
532#define P_MEMSTAT_RELAUNCH_HIGH 0x4
533
534/*
535 * Checking the p_memstat_state almost always requires the proc_list_lock
536 * because the jetsam thread could be on the other core changing the state.
537 *
538 * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too.
539 * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too.
540 */
541#define isApp(p) ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK))
542#define isSysProc(p) ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK))
543
544#define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1)
545
546typedef struct memstat_bucket {
547 TAILQ_HEAD(, proc) list;
548 int count;
549 int relaunch_high_count;
550} memstat_bucket_t;
551
552extern memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT];
553
554/*
555 * Table that expresses the probability of a process
556 * being used in the next hour.
557 */
558typedef struct memorystatus_internal_probabilities {
559 char proc_name[MAXCOMLEN + 1];
560 int use_probability;
561} memorystatus_internal_probabilities_t;
562
563extern memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table;
564extern size_t memorystatus_global_probabilities_size;
565
316670eb 566
f427ee49 567extern void memorystatus_init(void);
316670eb 568
3e170ce0
A
569extern void memorystatus_init_at_boot_snapshot(void);
570
39236c6e 571extern int memorystatus_add(proc_t p, boolean_t locked);
cb323159 572extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t is_assertion, boolean_t effective,
0a7de745
A
573 boolean_t update_memlimit, int32_t memlimit_active, boolean_t memlimit_active_is_fatal,
574 int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal);
3e170ce0 575
cb323159
A
576/* Remove this process from jetsam bands for killing or freezing.
577 * The proc_list_lock is held by the caller.
578 * @param p: The process to remove.
579 * @return: 0 if successful. EAGAIN if the process can't be removed right now (because it's being frozen) or ESRCH.
580 */
581extern int memorystatus_remove(proc_t p);
39236c6e 582
d9a64523 583int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now);
cb323159 584int memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags);
39037602 585
39236c6e
A
586extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol);
587extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol);
cb323159 588extern int memorystatus_dirty_get(proc_t p, boolean_t locked);
fe8ab488 589extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol);
39236c6e
A
590
591extern int memorystatus_on_terminate(proc_t p);
592
593extern void memorystatus_on_suspend(proc_t p);
594extern void memorystatus_on_resume(proc_t p);
595extern void memorystatus_on_inactivity(proc_t p);
596
597extern void memorystatus_on_pageout_scan_end(void);
598
599/* Memorystatus kevent */
316670eb 600
39236c6e 601void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr);
316670eb 602
39236c6e
A
603int memorystatus_knote_register(struct knote *kn);
604void memorystatus_knote_unregister(struct knote *kn);
316670eb 605
39037602 606#if CONFIG_MEMORYSTATUS
813fb2f6
A
607void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
608void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
39037602 609void proc_memstat_terminated(proc_t p, boolean_t set);
d9a64523 610void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit);
cb323159
A
611
612#if __arm64__
613void memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase);
94ff46dc 614void memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p);
f427ee49 615void memorystatus_act_on_entitled_task_limit(proc_t p);
cb323159
A
616#endif /* __arm64__ */
617
39037602
A
618#endif /* CONFIG_MEMORYSTATUS */
619
3e170ce0
A
620int memorystatus_get_pressure_status_kdp(void);
621
5ba3f43e
A
622#if CONFIG_JETSAM
623
39236c6e 624typedef enum memorystatus_policy {
0a7de745 625 kPolicyDefault = 0x0,
316670eb 626 kPolicyMoreFree = 0x1,
316670eb
A
627} memorystatus_policy_t;
628
39236c6e 629boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async);
fe8ab488 630boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async);
d9a64523 631boolean_t memorystatus_kill_on_VM_compressor_thrashing(boolean_t async);
39236c6e 632boolean_t memorystatus_kill_on_vnode_limit(void);
316670eb 633
fe8ab488 634void jetsam_on_ledger_cpulimit_exceeded(void);
d9a64523 635void memorystatus_fast_jetsam_override(boolean_t enable_override);
316670eb 636
5ba3f43e 637#endif /* CONFIG_JETSAM */
316670eb 638
cb323159
A
639/* These are very verbose printfs(), enable with
640 * MEMORYSTATUS_DEBUG_LOG
641 */
642#if MEMORYSTATUS_DEBUG_LOG
643#define MEMORYSTATUS_DEBUG(cond, format, ...) \
644do { \
645if (cond) { printf(format, ##__VA_ARGS__); } \
646} while(0)
647#else
648#define MEMORYSTATUS_DEBUG(cond, format, ...)
649#endif
650
5ba3f43e 651boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid);
d9a64523 652boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async);
5ba3f43e 653void memorystatus_pages_update(unsigned int pages_avail);
39236c6e 654boolean_t memorystatus_idle_exit_from_VM(void);
cb323159
A
655proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search);
656proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search);
657void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages);
658void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state);
659void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check);
316670eb 660
39236c6e
A
661#if VM_PRESSURE_EVENTS
662
39236c6e
A
663extern kern_return_t memorystatus_update_vm_pressure(boolean_t);
664
fe8ab488 665#if CONFIG_MEMORYSTATUS
3e170ce0
A
666/* Flags */
667extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags);
39236c6e 668extern int memorystatus_send_pressure_note(int pid);
fe8ab488 669extern boolean_t memorystatus_is_foreground_locked(proc_t p);
39236c6e 670extern boolean_t memorystatus_bg_pressure_eligible(proc_t p);
fe8ab488 671#endif /* CONFIG_MEMORYSTATUS */
39236c6e
A
672
673#endif /* VM_PRESSURE_EVENTS */
674
316670eb 675#endif /* XNU_KERNEL_PRIVATE */
2d21ac55 676
316670eb 677#endif /* SYS_MEMORYSTATUS_H */