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