]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kern_memorystatus.h
xnu-3248.40.184.tar.gz
[apple/xnu.git] / bsd / sys / kern_memorystatus.h
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 2006 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 */
2d21ac55 28
316670eb
A
29#ifndef SYS_MEMORYSTATUS_H
30#define SYS_MEMORYSTATUS_H
b0d623f7
A
31
32#include <stdint.h>
33#include <sys/time.h>
34#include <sys/proc.h>
316670eb
A
35#include <sys/param.h>
36
39236c6e 37#define JETSAM_PRIORITY_REVISION 2
316670eb 38
fe8ab488
A
39#define JETSAM_PRIORITY_IDLE_HEAD -2
40/* The value -1 is an alias to JETSAM_PRIORITY_DEFAULT */
39236c6e
A
41#define JETSAM_PRIORITY_IDLE 0
42#define JETSAM_PRIORITY_IDLE_DEFERRED 1
43#define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 2
44#define JETSAM_PRIORITY_BACKGROUND 3
45#define JETSAM_PRIORITY_MAIL 4
46#define JETSAM_PRIORITY_PHONE 5
47#define JETSAM_PRIORITY_UI_SUPPORT 8
48#define JETSAM_PRIORITY_FOREGROUND_SUPPORT 9
49#define JETSAM_PRIORITY_FOREGROUND 10
50#define JETSAM_PRIORITY_AUDIO_AND_ACCESSORY 12
51#define JETSAM_PRIORITY_CONDUCTOR 13
52#define JETSAM_PRIORITY_HOME 16
53#define JETSAM_PRIORITY_EXECUTIVE 17
54#define JETSAM_PRIORITY_IMPORTANT 18
55#define JETSAM_PRIORITY_CRITICAL 19
316670eb 56
39236c6e 57#define JETSAM_PRIORITY_MAX 21
b0d623f7 58
39236c6e
A
59/* TODO - tune. This should probably be lower priority */
60#define JETSAM_PRIORITY_DEFAULT 18
61#define JETSAM_PRIORITY_TELEPHONY 19
2d21ac55 62
39236c6e
A
63/* Compatibility */
64#define DEFAULT_JETSAM_PRIORITY 18
65
66#define DEFERRED_IDLE_EXIT_TIME_SECS 10
67
68#define KEV_MEMORYSTATUS_SUBCLASS 3
2d21ac55 69
b0d623f7 70enum {
316670eb
A
71 kMemorystatusLevelNote = 1,
72 kMemorystatusSnapshotNote = 2,
73 kMemorystatusFreezeNote = 3,
74 kMemorystatusPressureNote = 4
b0d623f7
A
75};
76
2d21ac55 77enum {
316670eb
A
78 kMemorystatusLevelAny = -1,
79 kMemorystatusLevelNormal = 0,
80 kMemorystatusLevelWarning = 1,
81 kMemorystatusLevelUrgent = 2,
82 kMemorystatusLevelCritical = 3
2d21ac55
A
83};
84
316670eb 85typedef struct memorystatus_priority_entry {
b0d623f7 86 pid_t pid;
316670eb 87 int32_t priority;
39236c6e
A
88 uint64_t user_data;
89 int32_t limit;
90 uint32_t state;
316670eb 91} memorystatus_priority_entry_t;
b0d623f7 92
316670eb 93typedef struct memorystatus_kernel_stats {
b0d623f7
A
94 uint32_t free_pages;
95 uint32_t active_pages;
96 uint32_t inactive_pages;
316670eb 97 uint32_t throttled_pages;
b0d623f7
A
98 uint32_t purgeable_pages;
99 uint32_t wired_pages;
39236c6e
A
100 uint32_t speculative_pages;
101 uint32_t filebacked_pages;
102 uint32_t anonymous_pages;
103 uint32_t compressor_pages;
104 uint64_t compressions;
105 uint64_t decompressions;
106 uint64_t total_uncompressed_pages_in_compressor;
316670eb 107} memorystatus_kernel_stats_t;
b0d623f7
A
108
109/*
110** This is a variable-length struct.
316670eb 111** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t *
b0d623f7
A
112*/
113
316670eb 114typedef struct jetsam_snapshot_entry {
39236c6e
A
115 pid_t pid;
116 char name[MAXCOMLEN+1];
117 int32_t priority;
316670eb 118 uint32_t pages;
39236c6e
A
119 uint32_t max_pages;
120 uint32_t state;
121 uint32_t killed;
122 uint64_t user_data;
123 uint8_t uuid[16];
124 uint32_t fds;
fe8ab488
A
125 uint32_t max_pages_lifetime;
126 uint32_t purgeable_pages;
127 struct timeval cpu_time;
316670eb
A
128} memorystatus_jetsam_snapshot_entry_t;
129
b0d623f7 130typedef struct jetsam_snapshot {
316670eb
A
131 uint64_t snapshot_time;
132 uint64_t notification_time;
133 memorystatus_kernel_stats_t stats;
b0d623f7 134 size_t entry_count;
39236c6e 135 memorystatus_jetsam_snapshot_entry_t entries[];
316670eb
A
136} memorystatus_jetsam_snapshot_t;
137
316670eb
A
138typedef struct memorystatus_freeze_entry {
139 int32_t pid;
6d2010ae
A
140 uint32_t flags;
141 uint32_t pages;
316670eb
A
142} memorystatus_freeze_entry_t;
143
39236c6e
A
144/* TODO - deprecate; see <rdar://problem/12969599> */
145#define kMaxSnapshotEntries 192
316670eb 146
39236c6e
A
147/* State */
148#define kMemorystatusSuspended 0x01
149#define kMemorystatusFrozen 0x02
150#define kMemorystatusWasThawed 0x04
151#define kMemorystatusTracked 0x08
152#define kMemorystatusSupportsIdleExit 0x10
153#define kMemorystatusDirty 0x20
6d2010ae 154
39236c6e
A
155/* Cause */
156enum {
157 kMemorystatusKilled = 1,
158 kMemorystatusKilledHiwat,
159 kMemorystatusKilledVnodes,
160 kMemorystatusKilledVMPageShortage,
161 kMemorystatusKilledVMThrashing,
fe8ab488 162 kMemorystatusKilledFCThrashing,
39236c6e
A
163 kMemorystatusKilledPerProcessLimit,
164 kMemorystatusKilledDiagnostic,
165 kMemorystatusKilledIdleExit
166};
6d2010ae 167
39236c6e
A
168/* Temporary, to prevent the need for a linked submission of ReportCrash */
169/* Remove when <rdar://problem/13210532> has been integrated */
b0d623f7 170enum {
39236c6e 171 kMemorystatusKilledVM = kMemorystatusKilledVMPageShortage
b0d623f7 172};
b0d623f7 173
39236c6e
A
174/* Memorystatus control */
175#define MEMORYSTATUS_BUFFERSIZE_MAX 65536
176
177#ifndef KERNEL
178int memorystatus_get_level(user_addr_t level);
179int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize);
316670eb 180#endif
316670eb 181
39236c6e
A
182/* Commands */
183#define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1
184#define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2
185#define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3
186#define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4
3e170ce0
A
187#define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */
188#define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */
189#define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 7 /* Set memory limits plus attributes independently */
190#define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 8 /* Get memory limits plus attributes */
191#define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE 9 /* Set the task's status as a privileged listener w.r.t memory notifications */
192#define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE 10 /* Reset the task's status as a privileged listener w.r.t memory notifications */
490019cf
A
193#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. */
194#define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12 /* Disable the 'lenient' mode for aggressive jetsam. */
3e170ce0
A
195/* Commands that act on a group of processes */
196#define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100
39236c6e
A
197
198#if PRIVATE
199/* Test commands */
200
201/* Trigger forced jetsam */
3e170ce0
A
202#define MEMORYSTATUS_CMD_TEST_JETSAM 1000
203#define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001
39236c6e
A
204
205/* Panic on jetsam options */
206typedef struct memorystatus_jetsam_panic_options {
207 uint32_t data;
208 uint32_t mask;
209} memorystatus_jetsam_panic_options_t;
316670eb 210
3e170ce0
A
211#define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002
212
213/* Select priority band sort order */
214#define JETSAM_SORT_NOSORT 0
215#define JETSAM_SORT_DEFAULT 1
216
39236c6e
A
217#endif /* PRIVATE */
218
3e170ce0
A
219/*
220 * For use with memorystatus_control:
221 * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT
222 *
223 * A jetsam snapshot is initialized when a non-idle
224 * jetsam event occurs. The data is held in the
225 * buffer until it is reaped. This is the default
226 * behavior.
227 *
228 * Flags change the default behavior:
229 * Demand mode - this is an on_demand snapshot,
230 * meaning data is populated upon request.
231 *
232 * Boot mode - this is a snapshot of
233 * memstats collected before loading the
234 * init program. Once collected, these
235 * stats do not change. In this mode,
236 * the snapshot entry_count is always 0.
237 *
238 * Snapshots are inherently racey between request
239 * for buffer size and actual data compilation.
240*/
241
242/* Flags */
243#define MEMORYSTATUS_SNAPSHOT_ON_DEMAND 0x1 /* A populated snapshot buffer is returned on demand */
244#define MEMORYSTATUS_SNAPSHOT_AT_BOOT 0x2 /* Returns a snapshot with memstats collected at boot */
245
246
247/*
248 * For use with memorystatus_control:
249 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES
250 */
39236c6e
A
251typedef struct memorystatus_priority_properties {
252 int32_t priority;
253 uint64_t user_data;
254} memorystatus_priority_properties_t;
255
3e170ce0
A
256/*
257 * For use with memorystatus_control:
258 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES
259 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES
260 */
261typedef struct memorystatus_memlimit_properties {
262 int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */
263 uint32_t memlimit_active_attr;
264 int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */
265 uint32_t memlimit_inactive_attr;
266} memorystatus_memlimit_properties_t;
267
268#define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */
269
270
39236c6e
A
271#ifdef XNU_KERNEL_PRIVATE
272
3e170ce0
A
273/*
274 * A process will be killed immediately if it crosses a memory limit marked as fatal.
275 * Fatal limit types are the
276 * - default system-wide task limit
277 * - per-task custom memory limit
278 *
279 * A process with a non-fatal memory limit can exceed that limit, but becomes an early
280 * candidate for jetsam when the device is under memory pressure.
281 * Non-fatal limit types are the
282 * - high-water-mark limit
283 *
284 * P_MEMSTAT_MEMLIMIT_BACKGROUND is translated in posix_spawn as
285 * the fatal system_wide task limit when active
286 * non-fatal inactive limit based on limit provided.
287 * This is necessary for backward compatibility until the
288 * the flag can be considered obsolete.
289 *
290 * Processes that opt into dirty tracking are evaluated
291 * based on clean vs dirty state.
292 * dirty ==> active
293 * clean ==> inactive
294 *
295 * Processes that do not opt into dirty tracking are
296 * evalulated based on priority level.
297 * Foreground or above ==> active
298 * Below Foreground ==> inactive
299 */
300
301/*
302 * p_memstat_state flag holds
303 * - in kernel process state and memlimit state
304 */
39236c6e
A
305
306#define P_MEMSTAT_SUSPENDED 0x00000001
307#define P_MEMSTAT_FROZEN 0x00000002
308#define P_MEMSTAT_NORECLAIM 0x00000004
309#define P_MEMSTAT_ERROR 0x00000008
310#define P_MEMSTAT_LOCKED 0x00000010
311#define P_MEMSTAT_TERMINATED 0x00000020
312#define P_MEMSTAT_NOTFIED 0x00000040
313#define P_MEMSTAT_PRIORITYUPDATED 0x00000080
314#define P_MEMSTAT_FOREGROUND 0x00000100
315#define P_MEMSTAT_DIAG_SUSPENDED 0x00000200
316#define P_MEMSTAT_PRIOR_THAW 0x00000400
fe8ab488 317#define P_MEMSTAT_MEMLIMIT_BACKGROUND 0x00000800 /* Task has a memory limit for when it's in the background. Used for a process' "high water mark".*/
39236c6e 318#define P_MEMSTAT_INTERNAL 0x00001000
3e170ce0
A
319#define P_MEMSTAT_FATAL_MEMLIMIT 0x00002000 /* current fatal state of the process's memlimit */
320#define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL 0x00004000 /* if set, exceeding limit is fatal when the process is active */
321#define P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED 0x00008000 /* if set, supresses high-water-mark EXC_RESOURCE, allows one hit per active limit */
322#define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00010000 /* if set, exceeding limit is fatal when the process is inactive */
323#define P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED 0x00020000 /* if set, supresses high-water-mark EXC_RESOURCE, allows one hit per inactive limit */
316670eb
A
324
325extern void memorystatus_init(void) __attribute__((section("__TEXT, initcode")));
326
3e170ce0
A
327extern void memorystatus_init_at_boot_snapshot(void);
328
39236c6e 329extern int memorystatus_add(proc_t p, boolean_t locked);
3e170ce0
A
330extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective,
331 boolean_t update_memlimit, int32_t memlimit_active, boolean_t memlimit_active_is_fatal,
332 int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal, boolean_t memlimit_background);
333
39236c6e
A
334extern int memorystatus_remove(proc_t p, boolean_t locked);
335
336extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol);
337extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol);
338extern int memorystatus_dirty_get(proc_t p);
fe8ab488 339extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol);
39236c6e
A
340
341extern int memorystatus_on_terminate(proc_t p);
342
343extern void memorystatus_on_suspend(proc_t p);
344extern void memorystatus_on_resume(proc_t p);
345extern void memorystatus_on_inactivity(proc_t p);
346
347extern void memorystatus_on_pageout_scan_end(void);
348
349/* Memorystatus kevent */
316670eb 350
39236c6e 351void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr);
316670eb 352
39236c6e
A
353int memorystatus_knote_register(struct knote *kn);
354void memorystatus_knote_unregister(struct knote *kn);
316670eb
A
355
356#if CONFIG_JETSAM
357
3e170ce0
A
358int memorystatus_get_pressure_status_kdp(void);
359
39236c6e 360typedef enum memorystatus_policy {
316670eb
A
361 kPolicyDefault = 0x0,
362 kPolicyMoreFree = 0x1,
363 kPolicyDiagnoseAll = 0x2,
364 kPolicyDiagnoseFirst = 0x4,
365 kPolicyDiagnoseActive = (kPolicyDiagnoseAll | kPolicyDiagnoseFirst),
366} memorystatus_policy_t;
367
368extern int memorystatus_jetsam_wakeup;
369extern unsigned int memorystatus_jetsam_running;
2d21ac55 370
39236c6e
A
371boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async);
372boolean_t memorystatus_kill_on_VM_thrashing(boolean_t async);
fe8ab488 373boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async);
39236c6e 374boolean_t memorystatus_kill_on_vnode_limit(void);
316670eb 375
39236c6e 376void memorystatus_on_ledger_footprint_exceeded(int warning, const int max_footprint_mb);
3e170ce0 377void proc_memstat_terminated(proc_t p, boolean_t set);
fe8ab488 378void jetsam_on_ledger_cpulimit_exceeded(void);
316670eb 379
39236c6e 380void memorystatus_pages_update(unsigned int pages_avail);
316670eb 381
39236c6e 382#else /* CONFIG_JETSAM */
316670eb 383
39236c6e 384boolean_t memorystatus_idle_exit_from_VM(void);
316670eb 385
39236c6e 386#endif /* !CONFIG_JETSAM */
6d2010ae
A
387
388#ifdef CONFIG_FREEZE
6d2010ae 389
316670eb
A
390#define FREEZE_PAGES_MIN ( 1 * 1024 * 1024 / PAGE_SIZE)
391#define FREEZE_PAGES_MAX (16 * 1024 * 1024 / PAGE_SIZE)
6d2010ae 392
316670eb
A
393#define FREEZE_SUSPENDED_THRESHOLD_LOW 2
394#define FREEZE_SUSPENDED_THRESHOLD_DEFAULT 4
395
3e170ce0 396#define FREEZE_DAILY_MB_MAX_DEFAULT 1024
316670eb
A
397
398typedef struct throttle_interval_t {
399 uint32_t mins;
400 uint32_t burst_multiple;
401 uint32_t pageouts;
402 uint32_t max_pageouts;
403 mach_timespec_t ts;
404 boolean_t throttle;
405} throttle_interval_t;
406
407extern boolean_t memorystatus_freeze_enabled;
408extern int memorystatus_freeze_wakeup;
409
410extern void memorystatus_freeze_init(void) __attribute__((section("__TEXT, initcode")));
3e170ce0 411extern int memorystatus_freeze_process_sync(proc_t p);
316670eb
A
412#endif /* CONFIG_FREEZE */
413
39236c6e
A
414#if VM_PRESSURE_EVENTS
415
39236c6e
A
416extern kern_return_t memorystatus_update_vm_pressure(boolean_t);
417
fe8ab488 418#if CONFIG_MEMORYSTATUS
3e170ce0
A
419/* Flags */
420extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags);
39236c6e 421extern int memorystatus_send_pressure_note(int pid);
fe8ab488 422extern boolean_t memorystatus_is_foreground_locked(proc_t p);
39236c6e 423extern boolean_t memorystatus_bg_pressure_eligible(proc_t p);
fe8ab488 424#endif /* CONFIG_MEMORYSTATUS */
39236c6e
A
425
426#endif /* VM_PRESSURE_EVENTS */
427
316670eb 428#endif /* XNU_KERNEL_PRIVATE */
2d21ac55 429
316670eb 430#endif /* SYS_MEMORYSTATUS_H */