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