xref: /xnu-12377.1.9/bsd/sys/kern_memorystatus.h (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
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/param.h>
35 #if BSD_KERNEL_PRIVATE
36 #include <sys/proc.h>
37 #endif /* BSD_KERNEL_PRIVATE */
38 #include <sys/reason.h>
39 #include <stdbool.h>
40 
41 #define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus"
42 
43 #define JETSAM_PRIORITY_REVISION                  2
44 
45 #define JETSAM_PRIORITY_IDLE_HEAD                -2
46 /* The value -1 is an alias to JETSAM_PRIORITY_DEFAULT */
47 #define JETSAM_PRIORITY_IDLE                      0
48 #define JETSAM_PRIORITY_ENTITLED_MAX              9 /* Entitled processes may use bands 1-9 for experimentation */
49 #define JETSAM_PRIORITY_IDLE_DEFERRED             10 /* Keeping this around till all xnu_quick_tests can be moved away from it.*/
50 #define JETSAM_PRIORITY_AGING_BAND1               JETSAM_PRIORITY_IDLE_DEFERRED
51 #define JETSAM_PRIORITY_AGING_BAND1_STUCK         15 /* Sysprocs stuck in JETSAM_PRIORITY_IDLE_DEFERRED will be moved to this band */
52 #define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC  20
53 #define JETSAM_PRIORITY_AGING_BAND2               JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC
54 #define JETSAM_PRIORITY_BACKGROUND                30
55 /*
56  * NB: This band is no longer used by mail, but IS used by many active
57  * processes doing background work.
58  */
59 #define JETSAM_PRIORITY_MAIL                      40
60 #define JETSAM_PRIORITY_ELEVATED_INACTIVE         JETSAM_PRIORITY_MAIL
61 #define JETSAM_PRIORITY_PHONE                     50
62 #define JETSAM_PRIORITY_FREEZER                   75
63 #define JETSAM_PRIORITY_UI_SUPPORT                80
64 #define JETSAM_PRIORITY_FOREGROUND_SUPPORT        90
65 #define JETSAM_PRIORITY_FOREGROUND               100
66 #define JETSAM_PRIORITY_AUDIO_AND_ACCESSORY      120
67 #define JETSAM_PRIORITY_CONDUCTOR                130
68 #define JETSAM_PRIORITY_DRIVER_APPLE             150
69 #define JETSAM_PRIORITY_HOME                     160
70 #define JETSAM_PRIORITY_EXECUTIVE                170
71 #define JETSAM_PRIORITY_IMPORTANT                180
72 #define JETSAM_PRIORITY_CRITICAL                 190
73 
74 #define JETSAM_PRIORITY_MAX                      210
75 
76 /*
77  * Used to show that a process is so high priority it is not processed by jetsam at all.
78  * This is set on launchd and kernel_task.
79  */
80 #define JETSAM_PRIORITY_INTERNAL                 999
81 
82 /* TODO - tune. This should probably be lower priority */
83 #define JETSAM_PRIORITY_DEFAULT                  180
84 #define JETSAM_PRIORITY_TELEPHONY                190
85 
86 /* Compatibility */
87 #define DEFAULT_JETSAM_PRIORITY                  180
88 
89 #define KEV_MEMORYSTATUS_SUBCLASS                 3
90 
91 enum {
92 	kMemorystatusLevelNote = 1,
93 	kMemorystatusSnapshotNote = 2,
94 	kMemorystatusFreezeNote = 3,
95 	kMemorystatusPressureNote = 4
96 };
97 
98 enum {
99 	kMemorystatusLevelAny = -1,
100 	kMemorystatusLevelNormal = 0,
101 	kMemorystatusLevelWarning = 1,
102 	kMemorystatusLevelUrgent = 2,
103 	kMemorystatusLevelCritical = 3
104 };
105 
106 typedef uint32_t memorystatus_proc_state_t;
107 
108 typedef struct memorystatus_priority_entry {
109 	pid_t pid;
110 	int32_t priority;
111 	uint64_t user_data;
112 	int32_t limit;  /* MB */
113 	memorystatus_proc_state_t state;
114 } memorystatus_priority_entry_t;
115 
116 typedef struct memorystatus_priority_entry_v2 {
117 	pid_t    pid;
118 	int32_t  priority;
119 	uint64_t user_data;
120 	int32_t  limit; /* MB */
121 	memorystatus_proc_state_t state;
122 	uint64_t priority_start_mtime;
123 
124 	uint8_t  _reserved[96];
125 } memorystatus_priority_entry_v2_t;
126 
127 /*
128  * This should be the structure to specify different properties
129  * for processes (group or single) from user-space. Unfortunately,
130  * we can't move to it completely because the priority_entry structure
131  * above has been in use for a while now. We'll have to deprecate it.
132  *
133  * To support new fields/properties, we will add a new structure with a
134  * new version and a new size.
135  */
136 #define MEMORYSTATUS_MPE_VERSION_1              1
137 
138 #define MEMORYSTATUS_MPE_VERSION_1_SIZE         sizeof(struct memorystatus_properties_entry_v1)
139 
140 typedef struct memorystatus_properties_entry_v1 {
141 	int version;
142 	pid_t pid;
143 	int32_t priority;
144 	int use_probability;
145 	uint64_t user_data;
146 	int32_t limit;  /* MB */
147 	memorystatus_proc_state_t state;
148 	char proc_name[MAXCOMLEN + 1];
149 	char __pad1[3];
150 } memorystatus_properties_entry_v1_t;
151 
152 /*
153  * Represents a freeze or demotion candidate.
154  */
155 typedef struct memorystatus_properties_freeze_entry_v1 {
156 	int version;
157 	pid_t pid;
158 	uint32_t priority;
159 	char proc_name[(2 * MAXCOMLEN) + 1];
160 	char __pad1[3];
161 } memorystatus_properties_freeze_entry_v1;
162 
163 typedef struct memorystatus_kernel_stats {
164 	uint32_t free_pages;
165 	uint32_t active_pages;
166 	uint32_t inactive_pages;
167 	uint32_t throttled_pages;
168 	uint32_t purgeable_pages;
169 	uint32_t wired_pages;
170 	uint32_t speculative_pages;
171 	uint32_t filebacked_pages;
172 	uint32_t anonymous_pages;
173 	uint32_t compressor_pages;
174 	uint64_t compressions;
175 	uint64_t decompressions;
176 	uint64_t total_uncompressed_pages_in_compressor;
177 	uint64_t zone_map_size;
178 	uint64_t zone_map_capacity;
179 	uint64_t largest_zone_size;
180 	char     largest_zone_name[MACH_ZONE_NAME_MAX_LEN];
181 } memorystatus_kernel_stats_t;
182 
183 typedef enum memorystatus_freeze_skip_reason {
184 	kMemorystatusFreezeSkipReasonNone = 0,
185 	kMemorystatusFreezeSkipReasonExcessSharedMemory = 1,
186 	kMemorystatusFreezeSkipReasonLowPrivateSharedRatio = 2,
187 	kMemorystatusFreezeSkipReasonNoCompressorSpace = 3,
188 	kMemorystatusFreezeSkipReasonNoSwapSpace = 4,
189 	kMemorystatusFreezeSkipReasonBelowMinPages = 5,
190 	kMemorystatusFreezeSkipReasonLowProbOfUse = 6,
191 	kMemorystatusFreezeSkipReasonOther = 7,
192 	kMemorystatusFreezeSkipReasonOutOfBudget = 8,
193 	kMemorystatusFreezeSkipReasonOutOfSlots = 9,
194 	kMemorystatusFreezeSkipReasonDisabled = 10,
195 	kMemorystatusFreezeSkipReasonElevated = 11,
196 	_kMemorystatusFreezeSkipReasonMax
197 } memorystatus_freeze_skip_reason_t;
198 /*
199 ** This is a variable-length struct.
200 ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t *
201 */
202 
203 typedef struct jetsam_snapshot_entry {
204 	pid_t    pid;
205 	char     name[(2 * MAXCOMLEN) + 1];
206 	int32_t  priority;
207 	memorystatus_proc_state_t state;
208 	uint32_t fds;
209 	memorystatus_freeze_skip_reason_t jse_freeze_skip_reason; /* why wasn't this process frozen? */
210 	uint8_t  uuid[16];
211 	uint64_t user_data;
212 	uint64_t killed;
213 	uint64_t pages;
214 	uint64_t max_pages_lifetime;
215 	uint64_t purgeable_pages;
216 	uint64_t jse_internal_pages;
217 	uint64_t jse_internal_compressed_pages;
218 	uint64_t jse_purgeable_nonvolatile_pages;
219 	uint64_t jse_purgeable_nonvolatile_compressed_pages;
220 	uint64_t jse_alternate_accounting_pages;
221 	uint64_t jse_alternate_accounting_compressed_pages;
222 	uint64_t jse_iokit_mapped_pages;
223 	uint64_t jse_page_table_pages;
224 	uint64_t jse_memory_region_count;
225 	uint64_t jse_gencount;                  /* memorystatus_thread generation counter */
226 	uint64_t jse_starttime;                 /* absolute time when process starts */
227 	uint64_t jse_killtime;                  /* absolute time when jetsam chooses to kill a process */
228 	uint64_t jse_idle_delta;                /* time spent in idle band */
229 	uint64_t jse_coalition_jetsam_id;       /* we only expose coalition id for COALITION_TYPE_JETSAM */
230 	struct timeval64 cpu_time;
231 	uint64_t jse_thaw_count;
232 	uint64_t jse_frozen_to_swap_pages;
233 	uint64_t csflags;
234 	uint32_t cs_trust_level;
235 	uint64_t jse_neural_nofootprint_total_pages;
236 	uint64_t jse_prio_start;                /* absolute time process moved to current priority */
237 } memorystatus_jetsam_snapshot_entry_t;
238 
239 typedef struct jetsam_snapshot {
240 	uint64_t snapshot_time;                 /* absolute time snapshot was initialized */
241 	uint64_t notification_time;             /* absolute time snapshot was consumed */
242 	uint64_t js_gencount;                   /* memorystatus_thread generation counter */
243 	memorystatus_kernel_stats_t stats;      /* system stat when snapshot is initialized */
244 	size_t entry_count;
245 	memorystatus_jetsam_snapshot_entry_t entries[];
246 } memorystatus_jetsam_snapshot_t;
247 
248 /* TODO - deprecate; see <rdar://problem/12969599> */
249 #define kMaxSnapshotEntries 192
250 
251 /* State */
252 #define kMemorystatusSuspended        0x001
253 #define kMemorystatusFrozen           0x002
254 #define kMemorystatusWasThawed        0x004
255 #define kMemorystatusTracked          0x008
256 #define kMemorystatusSupportsIdleExit 0x010
257 #define kMemorystatusDirty            0x020
258 #define kMemorystatusAssertion        0x040
259 #define kMemorystatusActive           0x080
260 #define kMemorystatusRelaunchLow      0x100
261 #define kMemorystatusRelaunchMed      0x200
262 #define kMemorystatusRelaunchHigh     0x400
263 
264 /*
265  * Jetsam exit reason definitions - related to memorystatus
266  *
267  * When adding new exit reasons also update:
268  *	JETSAM_REASON_MEMORYSTATUS_MAX
269  *	kMemorystatusKilled... Cause enum
270  *	memorystatus_kill_cause_name[]
271  */
272 #define JETSAM_REASON_INVALID                            0
273 #define JETSAM_REASON_GENERIC                            1
274 #define JETSAM_REASON_MEMORY_HIGHWATER                   2
275 #define JETSAM_REASON_VNODE                              3
276 #define JETSAM_REASON_MEMORY_VMPAGESHORTAGE              4
277 #define JETSAM_REASON_MEMORY_PROCTHRASHING               5
278 #define JETSAM_REASON_MEMORY_FCTHRASHING                 6
279 #define JETSAM_REASON_MEMORY_PERPROCESSLIMIT             7
280 #define JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE         8
281 #define JETSAM_REASON_MEMORY_IDLE_EXIT                   9
282 #define JETSAM_REASON_ZONE_MAP_EXHAUSTION                10
283 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING      11
284 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE 12
285 #define JETSAM_REASON_LOWSWAP                            13
286 #define JETSAM_REASON_MEMORY_SUSTAINED_PRESSURE          14
287 #define JETSAM_REASON_MEMORY_VMPAGEOUT_STARVATION        15
288 #define JETSAM_REASON_MEMORY_CONCLAVELIMIT               16
289 #define JETSAM_REASON_MEMORY_LONGIDLE_EXIT               17
290 #define JETSAM_REASON_MEMORYSTATUS_MAX  JETSAM_REASON_MEMORY_LONGIDLE_EXIT
291 
292 /* non-memorystatus jetsam reasons */
293 #define JETSAM_REASON_CPULIMIT                           100
294 typedef uint64_t jetsam_reason_t;
295 
296 /* memorystatus kill cause */
297 typedef enum {
298 	kMemorystatusInvalid = JETSAM_REASON_INVALID,
299 	kMemorystatusKilled = JETSAM_REASON_GENERIC,
300 	kMemorystatusKilledHiwat = JETSAM_REASON_MEMORY_HIGHWATER,
301 	kMemorystatusKilledVnodes = JETSAM_REASON_VNODE,
302 	kMemorystatusKilledVMPageShortage = JETSAM_REASON_MEMORY_VMPAGESHORTAGE,
303 	kMemorystatusKilledProcThrashing = JETSAM_REASON_MEMORY_PROCTHRASHING,
304 	kMemorystatusKilledFCThrashing = JETSAM_REASON_MEMORY_FCTHRASHING,
305 	kMemorystatusKilledPerProcessLimit = JETSAM_REASON_MEMORY_PERPROCESSLIMIT,
306 	kMemorystatusKilledDiskSpaceShortage = JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE,
307 	kMemorystatusKilledIdleExit = JETSAM_REASON_MEMORY_IDLE_EXIT,
308 	kMemorystatusKilledLongIdleExit = JETSAM_REASON_MEMORY_LONGIDLE_EXIT,
309 	kMemorystatusKilledZoneMapExhaustion = JETSAM_REASON_ZONE_MAP_EXHAUSTION,
310 	kMemorystatusKilledVMCompressorThrashing = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING,
311 	kMemorystatusKilledVMCompressorSpaceShortage = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE,
312 	kMemorystatusKilledLowSwap = JETSAM_REASON_LOWSWAP,
313 	kMemorystatusKilledSustainedPressure = JETSAM_REASON_MEMORY_SUSTAINED_PRESSURE,
314 	kMemorystatusKilledVMPageoutStarvation = JETSAM_REASON_MEMORY_VMPAGEOUT_STARVATION,
315 	kMemorystatusKilledConclaveLimit = JETSAM_REASON_MEMORY_CONCLAVELIMIT,
316 } memorystatus_kill_cause_t;
317 
318 /*
319  * For backwards compatibility
320  * Keeping these around for external users (e.g. ReportCrash, Ariadne).
321  * TODO: Remove once they stop using these.
322  */
323 #define kMemorystatusKilledDiagnostic           kMemorystatusKilledDiskSpaceShortage
324 #define kMemorystatusKilledVMThrashing          kMemorystatusKilledVMCompressorThrashing
325 #define JETSAM_REASON_MEMORY_VMTHRASHING        JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING
326 
327 /* Memorystatus control */
328 #define MEMORYSTATUS_BUFFERSIZE_MAX 65536
329 
330 #if !KERNEL
331 __BEGIN_DECLS
332 int memorystatus_get_level(user_addr_t level);
333 int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize);
334 __END_DECLS
335 #endif
336 
337 /* Commands */
338 #define MEMORYSTATUS_CMD_GET_PRIORITY_LIST            1
339 #define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES      2
340 #define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT          3
341 #define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS          4
342 #define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK   5    /* Set active memory limit = inactive memory limit, both non-fatal	*/
343 #define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT        6    /* Set active memory limit = inactive memory limit, both fatal	*/
344 #define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES      7    /* Set memory limits plus attributes independently			*/
345 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES      8    /* Get memory limits plus attributes					*/
346 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE   9    /* Set the task's status as a privileged listener w.r.t memory notifications  */
347 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE  10   /* Reset the task's status as a privileged listener w.r.t memory notifications  */
348 #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. */
349 #define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12   /* Disable the 'lenient' mode for aggressive jetsam. */
350 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS          13   /* Compute how much a process's phys_footprint exceeds inactive memory limit */
351 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE         14 /* Set the inactive jetsam band for a process to JETSAM_PRIORITY_ELEVATED_INACTIVE */
352 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE        15 /* Reset the inactive jetsam band for a process to the default band (0)*/
353 #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 */
354 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED       17   /* Return the 'managed' status of a process */
355 #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.,
356 	                                                    *  if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */
357 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE     19   /* Return the freezable state of a process. */
358 
359 #define MEMORYSTATUS_CMD_FREEZER_CONTROL              20
360 
361 #define MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE      21   /* Query if the lenient mode for aggressive jetsam is enabled. */
362 
363 #define MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT   22   /* Used by DYLD to increase the jetsam active and inactive limits, when using roots */
364 
365 #if PRIVATE
366 #define MEMORYSTATUS_CMD_SET_TESTING_PID 23 /* Used by unit tests in the development kernel only. */
367 #endif /* PRIVATE */
368 
369 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN 24 /* Check if the process is frozen. */
370 
371 #define MEMORYSTATUS_CMD_MARK_PROCESS_COALITION_SWAPPABLE 25 /* Set the coalition led by this process as swappable. This is a one-way transition. Swappable coalitions can never be made non-swappable. */
372 #define MEMORYSTATUS_CMD_GET_PROCESS_COALITION_IS_SWAPPABLE 26 /* Get the swappable status for this process' coalition. */
373 
374 #define MEMORYSTATUS_CMD_CONVERT_MEMLIMIT_MB 28 /* Given a memlimit value (which may be 0 or -1), convert it to an actual limit in megabytes. */
375 
376 #define MEMORYSTATUS_CMD_SET_DIAG_LIMIT               30 /* Set the diagnostics memory limit */
377 #define MEMORYSTATUS_CMD_GET_DIAG_LIMIT               31 /* Get the diagnostics memory limit */
378 
379 #define MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_NAMES      32   /* Get jetsam snapshot zprint names array */
380 #define MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_INFO       33   /* Get jetsam snapshot zprint zone info */
381 #define MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_MEMINFO    34   /* Get jetsam snapshot zprint wired memory info */
382 #define MEMORYSTATUS_CMD_REARM_MEMLIMIT               36 /* Re-arm memory limit (EXC_RESOURCE) */
383 
384 #define MEMORYSTATUS_CMD_GET_PRIORITY_LIST_V2         35 /* Get priority list with v2 struct */
385 
386 #define MEMORYSTATUS_CMD_GET_KILL_COUNTS              37 /* Get kill counts */
387 
388 #define MEMORYSTATUS_CMD_GET_CONCLAVE_LIMIT           38 /* Get the conclave memory limit */
389 
390 /* Commands that act on a group of processes */
391 #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES           100
392 
393 #if PRIVATE
394 /* Test commands */
395 
396 /* Trigger forced jetsam */
397 #define MEMORYSTATUS_CMD_TEST_JETSAM            1000
398 #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT       1001
399 
400 /* Select priority band sort order */
401 __enum_decl(memorystatus_jetsam_sort_order_t, int, {
402 	JETSAM_SORT_NONE             = 0x0, /* No sort */
403 	JETSAM_SORT_LRU              = 0x1, /* Sort by LRU by coalition leader */
404 	JETSAM_SORT_FOOTPRINT        = 0x2, /* Sort by footprint by coalition leader */
405 	JETSAM_SORT_FOOTPRINT_NOCOAL = 0x3, /* Sort by footprint ignoring coalitions */
406 });
407 
408 #endif /* PRIVATE */
409 
410 /* memorystatus_control() flags */
411 
412 #define MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND           0x1     /* A populated snapshot buffer is returned on demand */
413 #define MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT             0x2     /* Returns a snapshot with memstats collected at boot */
414 #define MEMORYSTATUS_FLAGS_SNAPSHOT_COPY                0x4     /* No longer supported. Used to return the previously populated snapshot created by the system */
415 #define MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY             0x8     /* Set jetsam priorities for a group of pids */
416 #define MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY          0x10    /* Set probability of use for a group of processes */
417 
418 #if PRIVATE
419 #define MEMORYSTATUS_FLAGS_SET_TESTING_PID     0x20 /* Only used by xnu unit tests. */
420 #define MEMORYSTATUS_FLAGS_UNSET_TESTING_PID   0x40 /* Only used by xnu unit tests. */
421 #define MEMORYSTATUS_FLAGS_SET_IMP_TESTING_PID 0x80 /* Only used by xnu tests. */
422 #endif /* PRIVATE */
423 
424 #define MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER             0x80    /* A snapshot buffer containing app kills since last consumption */
425 
426 #define MEMORYSTATUS_FLAGS_GRP_SET_FREEZE_PRIORITY      0x100   /* Set a new ordered list of freeze candidates */
427 #define MEMORYSTATUS_FLAGS_GRP_SET_DEMOTE_PRIORITY      0x200   /* Set a new ordered list of demote candidates */
428 
429 #define MEMORYSTATUS_FLAGS_REARM_ACTIVE   0x400 /* Re-arm active limit */
430 #define MEMORYSTATUS_FLAGS_REARM_INACTIVE 0x800 /* Re-arm inactive limit */
431 /*
432  * For use with memorystatus_control:
433  * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
434  *
435  * A jetsam snapshot is initialized when a non-idle
436  * jetsam event occurs.  The data is held in the
437  * buffer until it is reaped. This is the default
438  * behavior.
439  *
440  * Flags change the default behavior:
441  *	Demand mode - this is an on_demand snapshot,
442  *	meaning data is populated upon request.
443  *
444  *	Boot mode - this is a snapshot of
445  *	memstats collected before loading the
446  *	init program.  Once collected, these
447  *	stats do not change.  In this mode,
448  *	the snapshot entry_count is always 0.
449  *
450  *	Copy mode - this returns the previous snapshot
451  *      collected by the system. The current snaphshot
452  *	might be only half populated.
453  *
454  * Snapshots are inherently racey between request
455  * for buffer size and actual data compilation.
456  */
457 
458 /* These definitions are required for backwards compatibility */
459 #define MEMORYSTATUS_SNAPSHOT_ON_DEMAND         MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND
460 #define MEMORYSTATUS_SNAPSHOT_AT_BOOT           MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT
461 #define MEMORYSTATUS_SNAPSHOT_COPY              MEMORYSTATUS_FLAGS_SNAPSHOT_COPY
462 
463 /*
464  * For use with memorystatus_control:
465  * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
466  */
467 typedef struct memorystatus_priority_properties {
468 	int32_t  priority;
469 	uint64_t user_data;
470 } memorystatus_priority_properties_t;
471 
472 /*
473  * Inform the kernel that setting the priority property is driven by assertions.
474  */
475 #define MEMORYSTATUS_SET_PRIORITY_ASSERTION     0x1
476 
477 /*
478  * For use with memorystatus_control:
479  * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
480  * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
481  */
482 typedef struct memorystatus_memlimit_properties {
483 	int32_t memlimit_active;                /* jetsam memory limit (in MB) when process is active */
484 	uint32_t memlimit_active_attr;
485 	int32_t memlimit_inactive;              /* jetsam memory limit (in MB) when process is inactive */
486 	uint32_t memlimit_inactive_attr;
487 } memorystatus_memlimit_properties_t;
488 
489 typedef struct memorystatus_memlimit_properties2 {
490 	memorystatus_memlimit_properties_t v1;
491 	uint32_t memlimit_increase;             /* jetsam memory limit increase (in MB) for active and inactive states */
492 	uint32_t memlimit_increase_bytes;       /* bytes used to determine the jetsam memory limit increase, for active and inactive states */
493 } memorystatus_memlimit_properties2_t;
494 
495 #define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL        0x1     /* if set, exceeding the memlimit is fatal */
496 /*
497  * For use with diagnostics memorystatus_control:
498  * MEMORYSTATUS_CMD_SET_DIAG_LIMIT
499  */
500 typedef struct memorystatus_diag_memlimit_properties {
501 	uint64_t memlimit;                       /* max limit for memory usage before an exception is thrown expressed in bytes, but rounded internally to Mbytes */
502 	bool     threshold_enabled;              /* Current status of the diagnostics threshold, only for get operations, not consider in set threshold */
503 } memorystatus_diag_memlimit_properties_t;
504 
505 
506 /*
507  * For use with MEMORYSTATUS_CMD_GET_KILL_COUNTS
508  */
509 
510 __options_closed_decl(memstat_get_kill_counts_options_t, int, {
511 	MEMORYSTATUS_GET_KILL_COUNTS_CLEAR = 0x1 /* Flag to clear kill counts list */
512 });
513 
514 #ifdef BSD_KERNEL_PRIVATE
515 
516 /*
517  * A process will be killed immediately if it crosses a memory limit marked as fatal.
518  * Fatal limit types are the
519  *	- default system-wide task limit
520  *	- per-task custom memory limit
521  *
522  * A process with a non-fatal memory limit can exceed that limit, but becomes an early
523  * candidate for jetsam when the device is under memory pressure.
524  * Non-fatal limit types are the
525  *	- high-water-mark limit
526  *
527  * Processes that opt into dirty tracking are evaluated
528  * based on clean vs dirty state.
529  *      dirty ==> active
530  *      clean ==> inactive
531  *
532  * Processes that do not opt into dirty tracking are
533  * evalulated based on priority level.
534  *      Foreground or above ==> active
535  *      Below Foreground    ==> inactive
536  */
537 
538 /*
539  * p_memstat_state flag holds
540  *	- in kernel process state and memlimit state
541  */
542 
543 #define P_MEMSTAT_SUSPENDED            0x00000001 /* Process is suspended and likely in the IDLE band */
544 #define P_MEMSTAT_FROZEN               0x00000002 /* Process has some state on disk. It should be suspended */
545 #define P_MEMSTAT_FREEZE_DISABLED      0x00000004 /* Process isn't freeze-eligible and will not be frozen */
546 #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 */
547 #define P_MEMSTAT_LOCKED               0x00000010 /* Process is being actively worked on behind the proc_list_lock */
548 #define P_MEMSTAT_TERMINATED           0x00000020 /* Process is exiting */
549 #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 */
550 #define P_MEMSTAT_PRIORITYUPDATED      0x00000080 /* Process had its jetsam priority updated */
551 #define P_MEMSTAT_FOREGROUND           0x00000100 /* Process is in the FG jetsam band...unused??? */
552 #define P_MEMSTAT_REFREEZE_ELIGIBLE    0x00000400 /* Process was once thawed i.e. its state was brought back from disk. It is now refreeze eligible.*/
553 #define P_MEMSTAT_MANAGED              0x00000800 /* Process is managed by RunningBoard i.e. is either application or extension */
554 #define P_MEMSTAT_INTERNAL             0x00001000 /* Process is a system-critical-not-be-jetsammed process i.e. launchd */
555 #define P_MEMSTAT_FATAL_MEMLIMIT                  0x00002000   /* current fatal state of the process's memlimit */
556 #define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL           0x00004000   /* if set, exceeding limit is fatal when the process is active   */
557 #define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL         0x00008000   /* if set, exceeding limit is fatal when the process is inactive */
558 #define P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND      0x00010000   /* if set, the process will go into this band & stay there when in the background instead
559 	                                                        *  of the aging bands and/or the IDLE band. */
560 #define P_MEMSTAT_PRIORITY_ASSERTION              0x00020000   /* jetsam priority is being driven by an assertion */
561 #define P_MEMSTAT_FREEZE_CONSIDERED               0x00040000   /* This process has been considered for the freezer. */
562 #define P_MEMSTAT_SKIP                            0x00080000   /* Process is temporarily ineligible for memory pressure kills. Used only on development & debug kernels to make corpses of buggy processes */
563 #define P_MEMSTAT_FROZEN_XPC_SERVICE              0x00100000   /* Process is an XPC service. Only used for freezer telemetry. */
564 #define P_MEMSTAT_FROZEN_FOCAL_THAW               0x00200000 /* Process has been thawed while focal in the current freezer interval. Only used for freezer telemetry. */
565 #define P_MEMSTAT_TEST_IMP_ASSERTION              0x00400000 /* Used for testing to pretend a process has an importance assertion. */
566 
567 /*
568  * p_memstat_relaunch_flags holds
569  *      - relaunch behavior when jetsammed
570  */
571 #define P_MEMSTAT_RELAUNCH_UNKNOWN      0x0
572 #define P_MEMSTAT_RELAUNCH_LOW          0x1
573 #define P_MEMSTAT_RELAUNCH_MED          0x2
574 #define P_MEMSTAT_RELAUNCH_HIGH         0x4
575 
576 /*
577  * Checking the p_memstat_state almost always requires the proc_list_lock
578  * because the jetsam thread could be on the other core changing the state.
579  *
580  * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too.
581  * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too.
582  */
583 #define isApp(p)            ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK))
584 #define isSysProc(p)            ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK))
585 
586 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1)
587 
588 typedef struct memstat_bucket {
589 	TAILQ_HEAD(, proc) list;
590 	uint32_t count;
591 	uint32_t relaunch_high_count;
592 } memstat_bucket_t;
593 
594 extern memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT];
595 
596 /*
597  * Table that expresses the probability of a process
598  * being used in the next hour.
599  */
600 typedef struct memorystatus_internal_probabilities {
601 	char proc_name[MAXCOMLEN + 1];
602 	int use_probability;
603 } memorystatus_internal_probabilities_t;
604 
605 extern memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table;
606 extern size_t memorystatus_global_probabilities_size;
607 
608 extern void memorystatus_post_snapshot(void);
609 
610 extern void memorystatus_init(void);
611 
612 extern void memorystatus_init_at_boot_snapshot(void);
613 
614 extern int memorystatus_add(proc_t p, boolean_t locked);
615 
616 __options_closed_decl(memstat_priority_options_t, uint8_t, {
617 	MEMSTAT_PRIORITY_OPTIONS_NONE   = 0x00,
618 	/* Priority is driven by a RB assertion */
619 	MEMSTAT_PRIORITY_IS_ASSERTION   = 0x01,
620 	MEMSTAT_PRIORITY_IS_EFFECTIVE   = 0x02,
621 	/* Insert at the head of the corresponding band */
622 	MEMSTAT_PRIORITY_INSERT_HEAD    = 0x04,
623 	/* Do not consider aging the process (used to send directly to IDLE) */
624 	MEMSTAT_PRIORITY_NO_AGING = 0x08,
625 });
626 
627 extern int memorystatus_set_priority(proc_t p, int priority, uint64_t user_data,
628     memstat_priority_options_t options);
629 
630 __options_decl(memlimit_options_t, uint8_t, {
631 	MEMLIMIT_OPTIONS_NONE = 0x00,
632 	MEMLIMIT_ACTIVE_FATAL = 0x01,
633 	MEMLIMIT_INACTIVE_FATAL = 0x02,
634 });
635 extern int memorystatus_set_memlimits(proc_t p, int32_t active_limit, int32_t inactive_limit, memlimit_options_t options);
636 
637 /* Remove this process from jetsam bands for killing or freezing.
638  * The proc_list_lock is held by the caller.
639  * @param p The process to remove.
640  * @return: 0 if successful. EAGAIN if the process can't be removed right now (because it's being frozen) or ESRCH.
641  */
642 extern int memorystatus_remove(proc_t p);
643 
644 int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now);
645 int memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags);
646 
647 extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol);
648 extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol);
649 extern int memorystatus_dirty_get(proc_t p, boolean_t locked);
650 extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol);
651 
652 extern int memorystatus_on_terminate(proc_t p);
653 
654 extern void memorystatus_on_suspend(proc_t p);
655 extern void memorystatus_on_resume(proc_t p);
656 extern void memorystatus_on_inactivity(proc_t p);
657 
658 extern void memorystatus_on_pageout_scan_end(void);
659 
660 /* Memorystatus kevent */
661 
662 void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr);
663 
664 int memorystatus_knote_register(struct knote *kn);
665 void memorystatus_knote_unregister(struct knote *kn);
666 
667 #if CONFIG_MEMORYSTATUS
668 void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
669 void memorystatus_log_diag_threshold_exception(const int diag_threshold_value);
670 void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal);
671 void memorystatus_on_conclave_limit_exceeded(const int max_footprint_mb);
672 void proc_memstat_skip(proc_t p, boolean_t set);
673 void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit, boolean_t *is_active, boolean_t *is_managed, boolean_t *has_assertion);
674 
675 #if __arm64__
676 void memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase);
677 void memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p);
678 void memorystatus_act_on_entitled_task_limit(proc_t p);
679 void memorystatus_act_on_entitled_developer_task_limit(proc_t p);
680 #endif /* __arm64__ */
681 
682 #endif /* CONFIG_MEMORYSTATUS */
683 
684 int memorystatus_get_pressure_status_kdp(void);
685 
686 #if CONFIG_JETSAM
687 
688 extern unsigned int memorystatus_swap_all_apps;
689 
690 void jetsam_on_ledger_cpulimit_exceeded(void);
691 
692 /*
693  * Disable memorystatus_swap_all_apps.
694  * Used by vm_pageout at boot if the swap volume is too small to support app swap.
695  * Returns true iff app swap is now disabled.
696  * On development or debug kernels, app swap can be enabled via a boot-arg and in
697  * that case can not be disabled.
698  */
699 bool memorystatus_disable_swap(void);
700 
701 #endif /* CONFIG_JETSAM */
702 
703 proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search);
704 proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search);
705 void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages);
706 
707 bool memorystatus_task_has_increased_memory_limit_entitlement(task_t task);
708 bool memorystatus_task_has_increased_debugging_memory_limit_entitlement(task_t task);
709 bool memorystatus_task_has_legacy_footprint_entitlement(task_t task);
710 bool memorystatus_task_has_ios13extended_footprint_limit(task_t task);
711 
712 #if VM_PRESSURE_EVENTS
713 
714 extern kern_return_t memorystatus_update_vm_pressure(boolean_t);
715 
716 #if CONFIG_MEMORYSTATUS
717 /* Flags */
718 extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags);
719 extern int memorystatus_send_pressure_note(int pid);
720 extern boolean_t memorystatus_is_foreground_locked(proc_t p);
721 extern boolean_t memorystatus_bg_pressure_eligible(proc_t p);
722 #endif /* CONFIG_MEMORYSTATUS */
723 #endif /* VM_PRESSURE_EVENTS */
724 #endif /* BSD_KERNEL_PRIVATE */
725 #endif /* SYS_MEMORYSTATUS_H */
726