2 * Copyright (c) 2006-2018 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef SYS_MEMORYSTATUS_H
30 #define SYS_MEMORYSTATUS_H
33 #include <mach_debug/zone_info.h>
36 #define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus"
38 #define JETSAM_PRIORITY_REVISION 2
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
62 #define JETSAM_PRIORITY_MAX 21
64 /* TODO - tune. This should probably be lower priority */
65 #define JETSAM_PRIORITY_DEFAULT 18
66 #define JETSAM_PRIORITY_TELEPHONY 19
69 #define DEFAULT_JETSAM_PRIORITY 18
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.
76 * For kJetsamAgingPolicySysProcsReclaimedFirst,
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
83 * Apps: The apps are aged for DEFERRED_IDLE_EXIT_TIME_SECS factored
84 * by kJetsamAppsIdleDelayTimeRatio.
86 #define DEFERRED_IDLE_EXIT_TIME_SECS 10
88 #define KEV_MEMORYSTATUS_SUBCLASS 3
91 kMemorystatusLevelNote
= 1,
92 kMemorystatusSnapshotNote
= 2,
93 kMemorystatusFreezeNote
= 3,
94 kMemorystatusPressureNote
= 4
98 kMemorystatusLevelAny
= -1,
99 kMemorystatusLevelNormal
= 0,
100 kMemorystatusLevelWarning
= 1,
101 kMemorystatusLevelUrgent
= 2,
102 kMemorystatusLevelCritical
= 3
105 #define KEV_DIRTYSTATUS_SUBCLASS 4
108 kDirtyStatusChangeNote
= 1
111 typedef struct memorystatus_priority_entry
{
115 int32_t limit
; /* MB */
117 } memorystatus_priority_entry_t
;
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.
125 * To support new fields/properties, we will add a new structure with a
126 * new version and a new size.
128 #define MEMORYSTATUS_MPE_VERSION_1 1
130 #define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1)
132 typedef struct memorystatus_properties_entry_v1
{
138 int32_t limit
; /* MB */
140 char proc_name
[MAXCOMLEN
+ 1];
142 } memorystatus_properties_entry_v1_t
;
144 typedef struct memorystatus_kernel_stats
{
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
;
165 ** This is a variable-length struct.
166 ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t *
169 typedef struct jetsam_snapshot_entry
{
171 char name
[(2 * MAXCOMLEN
) + 1];
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
;
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 */
206 memorystatus_jetsam_snapshot_entry_t entries
[];
207 } memorystatus_jetsam_snapshot_t
;
209 typedef enum dirty_status_change_event_type
{
210 kDirtyStatusChangedDirty
= 0x0,
211 kDirtyStatusChangedClean
= 0x1
212 } dirty_status_change_event_type_t
;
214 typedef struct dirty_status_change_event
{
216 char dsc_process_name
[(2 * MAXCOMLEN
) + 1];
217 dirty_status_change_event_type_t dsc_event_type
;
220 int32_t dsc_priority
;
221 } dirty_status_change_event_t
;
223 /* TODO - deprecate; see <rdar://problem/12969599> */
224 #define kMaxSnapshotEntries 192
227 * default jetsam snapshot support
229 extern memorystatus_jetsam_snapshot_t
*memorystatus_jetsam_snapshot
;
230 extern memorystatus_jetsam_snapshot_t
*memorystatus_jetsam_snapshot_copy
;
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
245 /* General memorystatus stuff */
247 extern uint64_t memorystatus_sysprocs_idle_delay_time
;
248 extern uint64_t memorystatus_apps_idle_delay_time
;
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
260 * Jetsam exit reason definitions - related to memorystatus
262 * When adding new exit reasons also update:
263 * JETSAM_REASON_MEMORYSTATUS_MAX
264 * kMemorystatusKilled... Cause enum
265 * memorystatus_kill_cause_name[]
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
284 * Jetsam exit reason definitions - not related to memorystatus
286 #define JETSAM_REASON_CPULIMIT 100
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
,
307 * For backwards compatibility
308 * Keeping these around for external users (e.g. ReportCrash, Ariadne).
309 * TODO: Remove once they stop using these.
311 #define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage
312 #define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing
313 #define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING
315 /* Memorystatus control */
316 #define MEMORYSTATUS_BUFFERSIZE_MAX 65536
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
);
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. */
345 #define MEMORYSTATUS_CMD_FREEZER_CONTROL 20
347 #define MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE 21 /* Query if the lenient mode for aggressive jetsam is enabled. */
349 #define MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT 22 /* Used by DYLD to increase the jetsam active and inactive limits, when using roots */
352 #define MEMORYSTATUS_CMD_SET_JETSAM_SNAPSHOT_OWNERSHIP 23 /* Used by unit tests in the development kernel only. */
355 /* Commands that act on a group of processes */
356 #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100
361 /* Trigger forced jetsam */
362 #define MEMORYSTATUS_CMD_TEST_JETSAM 1000
363 #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001
365 /* Panic on jetsam options */
366 typedef struct memorystatus_jetsam_panic_options
{
369 } memorystatus_jetsam_panic_options_t
;
371 #define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002
373 /* Select priority band sort order */
374 #define JETSAM_SORT_NOSORT 0
375 #define JETSAM_SORT_DEFAULT 1
379 /* memorystatus_control() flags */
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 */
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. */
392 #define MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER 0x80 /* A snapshot buffer containing app kills since last consumption */
394 * For use with memorystatus_control:
395 * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
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
402 * Flags change the default behavior:
403 * Demand mode - this is an on_demand snapshot,
404 * meaning data is populated upon request.
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.
412 * Copy mode - this returns the previous snapshot
413 * collected by the system. The current snaphshot
414 * might be only half populated.
416 * Snapshots are inherently racey between request
417 * for buffer size and actual data compilation.
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
426 * For use with memorystatus_control:
427 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
429 typedef struct memorystatus_priority_properties
{
432 } memorystatus_priority_properties_t
;
435 * Inform the kernel that setting the priority property is driven by assertions.
437 #define MEMORYSTATUS_SET_PRIORITY_ASSERTION 0x1
440 * For use with memorystatus_control:
441 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
442 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
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
;
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
;
457 #define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */
459 #ifdef XNU_KERNEL_PRIVATE
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
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
472 * Processes that opt into dirty tracking are evaluated
473 * based on clean vs dirty state.
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
484 * p_memstat_state flag holds
485 * - in kernel process state and memlimit state
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. */
510 * p_memstat_relaunch_flags holds
511 * - relaunch behavior when jetsammed
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
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.
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.
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))
528 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1)
530 typedef struct memstat_bucket
{
531 TAILQ_HEAD(, proc
) list
;
533 int relaunch_high_count
;
536 extern memstat_bucket_t memstat_bucket
[MEMSTAT_BUCKET_COUNT
];
539 * Table that expresses the probability of a process
540 * being used in the next hour.
542 typedef struct memorystatus_internal_probabilities
{
543 char proc_name
[MAXCOMLEN
+ 1];
545 } memorystatus_internal_probabilities_t
;
547 extern memorystatus_internal_probabilities_t
*memorystatus_global_probabilities_table
;
548 extern size_t memorystatus_global_probabilities_size
;
551 extern void memorystatus_init(void);
553 extern void memorystatus_init_at_boot_snapshot(void);
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
);
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.
565 extern int memorystatus_remove(proc_t p
);
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
);
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
);
575 extern int memorystatus_on_terminate(proc_t p
);
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
);
581 extern void memorystatus_on_pageout_scan_end(void);
583 /* Memorystatus kevent */
585 void memorystatus_kevent_init(lck_grp_t
*grp
, lck_attr_t
*attr
);
587 int memorystatus_knote_register(struct knote
*kn
);
588 void memorystatus_knote_unregister(struct knote
*kn
);
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
);
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__ */
602 #endif /* CONFIG_MEMORYSTATUS */
604 int memorystatus_get_pressure_status_kdp(void);
608 typedef enum memorystatus_policy
{
609 kPolicyDefault
= 0x0,
610 kPolicyMoreFree
= 0x1,
611 } memorystatus_policy_t
;
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);
618 void jetsam_on_ledger_cpulimit_exceeded(void);
619 void memorystatus_fast_jetsam_override(boolean_t enable_override
);
621 #endif /* CONFIG_JETSAM */
623 /* These are very verbose printfs(), enable with
624 * MEMORYSTATUS_DEBUG_LOG
626 #if MEMORYSTATUS_DEBUG_LOG
627 #define MEMORYSTATUS_DEBUG(cond, format, ...) \
629 if (cond) { printf(format, ##__VA_ARGS__); } \
632 #define MEMORYSTATUS_DEBUG(cond, format, ...)
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
);
645 #if VM_PRESSURE_EVENTS
647 extern kern_return_t
memorystatus_update_vm_pressure(boolean_t
);
649 #if CONFIG_MEMORYSTATUS
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 */
657 #endif /* VM_PRESSURE_EVENTS */
659 #endif /* XNU_KERNEL_PRIVATE */
661 #endif /* SYS_MEMORYSTATUS_H */