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