]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
d9a64523 | 2 | * Copyright (c) 2006-2018 Apple Computer, Inc. All rights reserved. |
2d21ac55 A |
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 | 35 | #include <sys/param.h> |
5ba3f43e | 36 | #include <mach_debug/zone_info.h> |
316670eb | 37 | |
39037602 A |
38 | #define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus" |
39 | ||
39236c6e | 40 | #define JETSAM_PRIORITY_REVISION 2 |
316670eb | 41 | |
fe8ab488 A |
42 | #define JETSAM_PRIORITY_IDLE_HEAD -2 |
43 | /* The value -1 is an alias to JETSAM_PRIORITY_DEFAULT */ | |
39236c6e | 44 | #define JETSAM_PRIORITY_IDLE 0 |
39037602 A |
45 | #define JETSAM_PRIORITY_IDLE_DEFERRED 1 /* Keeping this around till all xnu_quick_tests can be moved away from it.*/ |
46 | #define JETSAM_PRIORITY_AGING_BAND1 JETSAM_PRIORITY_IDLE_DEFERRED | |
39236c6e | 47 | #define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 2 |
39037602 | 48 | #define JETSAM_PRIORITY_AGING_BAND2 JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC |
39236c6e | 49 | #define JETSAM_PRIORITY_BACKGROUND 3 |
39037602 | 50 | #define JETSAM_PRIORITY_ELEVATED_INACTIVE JETSAM_PRIORITY_BACKGROUND |
39236c6e A |
51 | #define JETSAM_PRIORITY_MAIL 4 |
52 | #define JETSAM_PRIORITY_PHONE 5 | |
53 | #define JETSAM_PRIORITY_UI_SUPPORT 8 | |
54 | #define JETSAM_PRIORITY_FOREGROUND_SUPPORT 9 | |
55 | #define JETSAM_PRIORITY_FOREGROUND 10 | |
56 | #define JETSAM_PRIORITY_AUDIO_AND_ACCESSORY 12 | |
57 | #define JETSAM_PRIORITY_CONDUCTOR 13 | |
58 | #define JETSAM_PRIORITY_HOME 16 | |
59 | #define JETSAM_PRIORITY_EXECUTIVE 17 | |
60 | #define JETSAM_PRIORITY_IMPORTANT 18 | |
61 | #define JETSAM_PRIORITY_CRITICAL 19 | |
316670eb | 62 | |
39236c6e | 63 | #define JETSAM_PRIORITY_MAX 21 |
b0d623f7 | 64 | |
39236c6e A |
65 | /* TODO - tune. This should probably be lower priority */ |
66 | #define JETSAM_PRIORITY_DEFAULT 18 | |
67 | #define JETSAM_PRIORITY_TELEPHONY 19 | |
2d21ac55 | 68 | |
39236c6e A |
69 | /* Compatibility */ |
70 | #define DEFAULT_JETSAM_PRIORITY 18 | |
71 | ||
72 | #define DEFERRED_IDLE_EXIT_TIME_SECS 10 | |
73 | ||
74 | #define KEV_MEMORYSTATUS_SUBCLASS 3 | |
2d21ac55 | 75 | |
b0d623f7 | 76 | enum { |
316670eb A |
77 | kMemorystatusLevelNote = 1, |
78 | kMemorystatusSnapshotNote = 2, | |
79 | kMemorystatusFreezeNote = 3, | |
80 | kMemorystatusPressureNote = 4 | |
b0d623f7 A |
81 | }; |
82 | ||
2d21ac55 | 83 | enum { |
316670eb A |
84 | kMemorystatusLevelAny = -1, |
85 | kMemorystatusLevelNormal = 0, | |
86 | kMemorystatusLevelWarning = 1, | |
87 | kMemorystatusLevelUrgent = 2, | |
88 | kMemorystatusLevelCritical = 3 | |
2d21ac55 A |
89 | }; |
90 | ||
316670eb | 91 | typedef struct memorystatus_priority_entry { |
b0d623f7 | 92 | pid_t pid; |
316670eb | 93 | int32_t priority; |
39236c6e | 94 | uint64_t user_data; |
39037602 | 95 | int32_t limit; /* MB */ |
39236c6e | 96 | uint32_t state; |
316670eb | 97 | } memorystatus_priority_entry_t; |
b0d623f7 | 98 | |
d9a64523 A |
99 | /* |
100 | * This should be the structure to specify different properties | |
101 | * for processes (group or single) from user-space. Unfortunately, | |
102 | * we can't move to it completely because the priority_entry structure | |
103 | * above has been in use for a while now. We'll have to deprecate it. | |
104 | * | |
105 | * To support new fields/properties, we will add a new structure with a | |
106 | * new version and a new size. | |
107 | */ | |
108 | #define MEMORYSTATUS_MPE_VERSION_1 1 | |
109 | ||
110 | #define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1) | |
111 | ||
112 | typedef struct memorystatus_properties_entry_v1 { | |
113 | int version; | |
114 | pid_t pid; | |
115 | int32_t priority; | |
116 | int use_probability; | |
117 | uint64_t user_data; | |
118 | int32_t limit; /* MB */ | |
119 | uint32_t state; | |
120 | char proc_name[MAXCOMLEN+1]; | |
121 | char __pad1[3]; | |
122 | } memorystatus_properties_entry_v1_t; | |
123 | ||
316670eb | 124 | typedef struct memorystatus_kernel_stats { |
b0d623f7 A |
125 | uint32_t free_pages; |
126 | uint32_t active_pages; | |
127 | uint32_t inactive_pages; | |
316670eb | 128 | uint32_t throttled_pages; |
b0d623f7 A |
129 | uint32_t purgeable_pages; |
130 | uint32_t wired_pages; | |
39236c6e A |
131 | uint32_t speculative_pages; |
132 | uint32_t filebacked_pages; | |
133 | uint32_t anonymous_pages; | |
134 | uint32_t compressor_pages; | |
135 | uint64_t compressions; | |
136 | uint64_t decompressions; | |
137 | uint64_t total_uncompressed_pages_in_compressor; | |
5ba3f43e A |
138 | uint64_t zone_map_size; |
139 | uint64_t zone_map_capacity; | |
140 | uint64_t largest_zone_size; | |
141 | char largest_zone_name[MACH_ZONE_NAME_MAX_LEN]; | |
316670eb | 142 | } memorystatus_kernel_stats_t; |
b0d623f7 A |
143 | |
144 | /* | |
145 | ** This is a variable-length struct. | |
316670eb | 146 | ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t * |
b0d623f7 A |
147 | */ |
148 | ||
316670eb | 149 | typedef struct jetsam_snapshot_entry { |
39236c6e | 150 | pid_t pid; |
39037602 | 151 | char name[(2*MAXCOMLEN)+1]; |
39236c6e | 152 | int32_t priority; |
39236c6e | 153 | uint32_t state; |
39236c6e | 154 | uint32_t fds; |
39037602 A |
155 | uint8_t uuid[16]; |
156 | uint64_t user_data; | |
157 | uint64_t killed; | |
158 | uint64_t pages; | |
39037602 A |
159 | uint64_t max_pages_lifetime; |
160 | uint64_t purgeable_pages; | |
161 | uint64_t jse_internal_pages; | |
162 | uint64_t jse_internal_compressed_pages; | |
163 | uint64_t jse_purgeable_nonvolatile_pages; | |
164 | uint64_t jse_purgeable_nonvolatile_compressed_pages; | |
165 | uint64_t jse_alternate_accounting_pages; | |
166 | uint64_t jse_alternate_accounting_compressed_pages; | |
167 | uint64_t jse_iokit_mapped_pages; | |
168 | uint64_t jse_page_table_pages; | |
169 | uint64_t jse_memory_region_count; | |
170 | uint64_t jse_gencount; /* memorystatus_thread generation counter */ | |
171 | uint64_t jse_starttime; /* absolute time when process starts */ | |
172 | uint64_t jse_killtime; /* absolute time when jetsam chooses to kill a process */ | |
173 | uint64_t jse_idle_delta; /* time spent in idle band */ | |
174 | uint64_t jse_coalition_jetsam_id; /* we only expose coalition id for COALITION_TYPE_JETSAM */ | |
d9a64523 A |
175 | struct timeval64 cpu_time; |
176 | uint64_t jse_thaw_count; | |
316670eb A |
177 | } memorystatus_jetsam_snapshot_entry_t; |
178 | ||
b0d623f7 | 179 | typedef struct jetsam_snapshot { |
39037602 A |
180 | uint64_t snapshot_time; /* absolute time snapshot was initialized */ |
181 | uint64_t notification_time; /* absolute time snapshot was consumed */ | |
182 | uint64_t js_gencount; /* memorystatus_thread generation counter */ | |
183 | memorystatus_kernel_stats_t stats; /* system stat when snapshot is initialized */ | |
b0d623f7 | 184 | size_t entry_count; |
39236c6e | 185 | memorystatus_jetsam_snapshot_entry_t entries[]; |
316670eb A |
186 | } memorystatus_jetsam_snapshot_t; |
187 | ||
316670eb A |
188 | typedef struct memorystatus_freeze_entry { |
189 | int32_t pid; | |
6d2010ae A |
190 | uint32_t flags; |
191 | uint32_t pages; | |
316670eb A |
192 | } memorystatus_freeze_entry_t; |
193 | ||
39236c6e A |
194 | /* TODO - deprecate; see <rdar://problem/12969599> */ |
195 | #define kMaxSnapshotEntries 192 | |
316670eb | 196 | |
39236c6e A |
197 | /* State */ |
198 | #define kMemorystatusSuspended 0x01 | |
199 | #define kMemorystatusFrozen 0x02 | |
200 | #define kMemorystatusWasThawed 0x04 | |
201 | #define kMemorystatusTracked 0x08 | |
202 | #define kMemorystatusSupportsIdleExit 0x10 | |
203 | #define kMemorystatusDirty 0x20 | |
6d2010ae | 204 | |
5ba3f43e A |
205 | /* |
206 | * Jetsam exit reason definitions - related to memorystatus | |
207 | * | |
208 | * When adding new exit reasons also update: | |
209 | * JETSAM_REASON_MEMORYSTATUS_MAX | |
210 | * kMemorystatusKilled... Cause enum | |
211 | * memorystatus_kill_cause_name[] | |
212 | */ | |
d9a64523 A |
213 | #define JETSAM_REASON_INVALID 0 |
214 | #define JETSAM_REASON_GENERIC 1 | |
215 | #define JETSAM_REASON_MEMORY_HIGHWATER 2 | |
216 | #define JETSAM_REASON_VNODE 3 | |
217 | #define JETSAM_REASON_MEMORY_VMPAGESHORTAGE 4 | |
218 | #define JETSAM_REASON_MEMORY_PROCTHRASHING 5 | |
219 | #define JETSAM_REASON_MEMORY_FCTHRASHING 6 | |
220 | #define JETSAM_REASON_MEMORY_PERPROCESSLIMIT 7 | |
221 | #define JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE 8 | |
222 | #define JETSAM_REASON_MEMORY_IDLE_EXIT 9 | |
223 | #define JETSAM_REASON_ZONE_MAP_EXHAUSTION 10 | |
224 | #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 11 | |
225 | #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE 12 | |
226 | ||
227 | #define JETSAM_REASON_MEMORYSTATUS_MAX JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE | |
5ba3f43e A |
228 | |
229 | /* | |
230 | * Jetsam exit reason definitions - not related to memorystatus | |
231 | */ | |
232 | #define JETSAM_REASON_CPULIMIT 100 | |
39037602 | 233 | |
5ba3f43e | 234 | /* Cause */ |
b0d623f7 | 235 | enum { |
d9a64523 A |
236 | kMemorystatusInvalid = JETSAM_REASON_INVALID, |
237 | kMemorystatusKilled = JETSAM_REASON_GENERIC, | |
238 | kMemorystatusKilledHiwat = JETSAM_REASON_MEMORY_HIGHWATER, | |
239 | kMemorystatusKilledVnodes = JETSAM_REASON_VNODE, | |
240 | kMemorystatusKilledVMPageShortage = JETSAM_REASON_MEMORY_VMPAGESHORTAGE, | |
241 | kMemorystatusKilledProcThrashing = JETSAM_REASON_MEMORY_PROCTHRASHING, | |
242 | kMemorystatusKilledFCThrashing = JETSAM_REASON_MEMORY_FCTHRASHING, | |
243 | kMemorystatusKilledPerProcessLimit = JETSAM_REASON_MEMORY_PERPROCESSLIMIT, | |
244 | kMemorystatusKilledDiskSpaceShortage = JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE, | |
245 | kMemorystatusKilledIdleExit = JETSAM_REASON_MEMORY_IDLE_EXIT, | |
246 | kMemorystatusKilledZoneMapExhaustion = JETSAM_REASON_ZONE_MAP_EXHAUSTION, | |
247 | kMemorystatusKilledVMCompressorThrashing = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING, | |
248 | kMemorystatusKilledVMCompressorSpaceShortage = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE, | |
b0d623f7 | 249 | }; |
b0d623f7 | 250 | |
d9a64523 A |
251 | /* For backwards compatibility */ |
252 | #define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage | |
253 | #define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing | |
254 | #define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING | |
255 | ||
39236c6e A |
256 | /* Memorystatus control */ |
257 | #define MEMORYSTATUS_BUFFERSIZE_MAX 65536 | |
258 | ||
259 | #ifndef KERNEL | |
260 | int memorystatus_get_level(user_addr_t level); | |
261 | int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize); | |
316670eb | 262 | #endif |
316670eb | 263 | |
39236c6e A |
264 | /* Commands */ |
265 | #define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1 | |
266 | #define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2 | |
267 | #define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3 | |
268 | #define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4 | |
3e170ce0 A |
269 | #define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */ |
270 | #define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */ | |
271 | #define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 7 /* Set memory limits plus attributes independently */ | |
272 | #define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 8 /* Get memory limits plus attributes */ | |
273 | #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE 9 /* Set the task's status as a privileged listener w.r.t memory notifications */ | |
274 | #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE 10 /* Reset the task's status as a privileged listener w.r.t memory notifications */ | |
490019cf A |
275 | #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. */ |
276 | #define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12 /* Disable the 'lenient' mode for aggressive jetsam. */ | |
39037602 | 277 | #define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */ |
d9a64523 A |
278 | #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE 14 /* Set the inactive jetsam band for a process to JETSAM_PRIORITY_ELEVATED_INACTIVE */ |
279 | #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE 15 /* Reset the inactive jetsam band for a process to the default band (0)*/ | |
280 | #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 */ | |
281 | #define MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED 17 /* Return the 'managed' status of a process */ | |
282 | #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., | |
283 | if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */ | |
284 | #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE 19 /* Return the freezable state of a process. */ | |
285 | ||
286 | #if CONFIG_FREEZE | |
287 | #if DEVELOPMENT || DEBUG | |
288 | #define MEMORYSTATUS_CMD_FREEZER_CONTROL 20 | |
289 | #endif /* DEVELOPMENT || DEBUG */ | |
290 | #endif /* CONFIG_FREEZE */ | |
39037602 | 291 | |
3e170ce0 A |
292 | /* Commands that act on a group of processes */ |
293 | #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100 | |
39236c6e A |
294 | |
295 | #if PRIVATE | |
296 | /* Test commands */ | |
297 | ||
298 | /* Trigger forced jetsam */ | |
3e170ce0 A |
299 | #define MEMORYSTATUS_CMD_TEST_JETSAM 1000 |
300 | #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001 | |
39236c6e A |
301 | |
302 | /* Panic on jetsam options */ | |
303 | typedef struct memorystatus_jetsam_panic_options { | |
304 | uint32_t data; | |
305 | uint32_t mask; | |
306 | } memorystatus_jetsam_panic_options_t; | |
316670eb | 307 | |
3e170ce0 A |
308 | #define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002 |
309 | ||
310 | /* Select priority band sort order */ | |
311 | #define JETSAM_SORT_NOSORT 0 | |
312 | #define JETSAM_SORT_DEFAULT 1 | |
313 | ||
39236c6e A |
314 | #endif /* PRIVATE */ |
315 | ||
d9a64523 A |
316 | /* memorystatus_control() flags */ |
317 | ||
318 | #define MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 0x1 /* A populated snapshot buffer is returned on demand */ | |
319 | #define MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 0x2 /* Returns a snapshot with memstats collected at boot */ | |
320 | #define MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 0x4 /* Returns the previously populated snapshot created by the system */ | |
321 | #define MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY 0x8 /* Set jetsam priorities for a group of pids */ | |
322 | #define MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY 0x10 /* Set probability of use for a group of processes */ | |
323 | ||
3e170ce0 A |
324 | /* |
325 | * For use with memorystatus_control: | |
326 | * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT | |
327 | * | |
328 | * A jetsam snapshot is initialized when a non-idle | |
329 | * jetsam event occurs. The data is held in the | |
330 | * buffer until it is reaped. This is the default | |
331 | * behavior. | |
332 | * | |
333 | * Flags change the default behavior: | |
334 | * Demand mode - this is an on_demand snapshot, | |
335 | * meaning data is populated upon request. | |
336 | * | |
337 | * Boot mode - this is a snapshot of | |
338 | * memstats collected before loading the | |
339 | * init program. Once collected, these | |
340 | * stats do not change. In this mode, | |
341 | * the snapshot entry_count is always 0. | |
342 | * | |
d9a64523 A |
343 | * Copy mode - this returns the previous snapshot |
344 | * collected by the system. The current snaphshot | |
345 | * might be only half populated. | |
346 | * | |
3e170ce0 A |
347 | * Snapshots are inherently racey between request |
348 | * for buffer size and actual data compilation. | |
349 | */ | |
350 | ||
d9a64523 A |
351 | /* These definitions are required for backwards compatibility */ |
352 | #define MEMORYSTATUS_SNAPSHOT_ON_DEMAND MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND | |
353 | #define MEMORYSTATUS_SNAPSHOT_AT_BOOT MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT | |
354 | #define MEMORYSTATUS_SNAPSHOT_COPY MEMORYSTATUS_FLAGS_SNAPSHOT_COPY | |
3e170ce0 A |
355 | |
356 | /* | |
357 | * For use with memorystatus_control: | |
358 | * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES | |
359 | */ | |
39236c6e A |
360 | typedef struct memorystatus_priority_properties { |
361 | int32_t priority; | |
362 | uint64_t user_data; | |
363 | } memorystatus_priority_properties_t; | |
364 | ||
3e170ce0 A |
365 | /* |
366 | * For use with memorystatus_control: | |
367 | * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES | |
368 | * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES | |
369 | */ | |
370 | typedef struct memorystatus_memlimit_properties { | |
371 | int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */ | |
372 | uint32_t memlimit_active_attr; | |
373 | int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */ | |
374 | uint32_t memlimit_inactive_attr; | |
375 | } memorystatus_memlimit_properties_t; | |
376 | ||
377 | #define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */ | |
378 | ||
39236c6e A |
379 | #ifdef XNU_KERNEL_PRIVATE |
380 | ||
3e170ce0 A |
381 | /* |
382 | * A process will be killed immediately if it crosses a memory limit marked as fatal. | |
383 | * Fatal limit types are the | |
384 | * - default system-wide task limit | |
385 | * - per-task custom memory limit | |
386 | * | |
387 | * A process with a non-fatal memory limit can exceed that limit, but becomes an early | |
388 | * candidate for jetsam when the device is under memory pressure. | |
389 | * Non-fatal limit types are the | |
390 | * - high-water-mark limit | |
391 | * | |
3e170ce0 A |
392 | * Processes that opt into dirty tracking are evaluated |
393 | * based on clean vs dirty state. | |
394 | * dirty ==> active | |
395 | * clean ==> inactive | |
396 | * | |
397 | * Processes that do not opt into dirty tracking are | |
398 | * evalulated based on priority level. | |
399 | * Foreground or above ==> active | |
400 | * Below Foreground ==> inactive | |
401 | */ | |
402 | ||
403 | /* | |
404 | * p_memstat_state flag holds | |
405 | * - in kernel process state and memlimit state | |
406 | */ | |
39236c6e | 407 | |
d9a64523 A |
408 | #define P_MEMSTAT_SUSPENDED 0x00000001 /* Process is suspended and likely in the IDLE band */ |
409 | #define P_MEMSTAT_FROZEN 0x00000002 /* Process has some state on disk. It should be suspended */ | |
410 | #define P_MEMSTAT_FREEZE_DISABLED 0x00000004 /* Process isn't freeze-eligible and will not be frozen */ | |
411 | #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 */ | |
412 | #define P_MEMSTAT_LOCKED 0x00000010 /* Process is being actively worked on behind the proc_list_lock */ | |
413 | #define P_MEMSTAT_TERMINATED 0x00000020 /* Process is exiting */ | |
414 | #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 */ | |
415 | #define P_MEMSTAT_PRIORITYUPDATED 0x00000080 /* Process had its jetsam priority updated */ | |
416 | #define P_MEMSTAT_FOREGROUND 0x00000100 /* Process is in the FG jetsam band...unused??? */ | |
417 | #define P_MEMSTAT_DIAG_SUSPENDED 0x00000200 /* ...unused??? */ | |
418 | #define P_MEMSTAT_REFREEZE_ELIGIBLE 0x00000400 /* Process was once thawed i.e. its state was brought back from disk. It is now refreeze eligible.*/ | |
419 | #define P_MEMSTAT_MANAGED 0x00000800 /* Process is managed by assertiond i.e. is either application or extension */ | |
420 | #define P_MEMSTAT_INTERNAL 0x00001000 /* Process is a system-critical-not-be-jetsammed process i.e. launchd */ | |
3e170ce0 A |
421 | #define P_MEMSTAT_FATAL_MEMLIMIT 0x00002000 /* current fatal state of the process's memlimit */ |
422 | #define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL 0x00004000 /* if set, exceeding limit is fatal when the process is active */ | |
813fb2f6 A |
423 | #define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00008000 /* if set, exceeding limit is fatal when the process is inactive */ |
424 | #define P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND 0x00010000 /* if set, the process will go into this band & stay there when in the background instead | |
39037602 | 425 | of the aging bands and/or the IDLE band. */ |
316670eb A |
426 | |
427 | extern void memorystatus_init(void) __attribute__((section("__TEXT, initcode"))); | |
428 | ||
3e170ce0 A |
429 | extern void memorystatus_init_at_boot_snapshot(void); |
430 | ||
39236c6e | 431 | extern int memorystatus_add(proc_t p, boolean_t locked); |
3e170ce0 A |
432 | extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, |
433 | boolean_t update_memlimit, int32_t memlimit_active, boolean_t memlimit_active_is_fatal, | |
5ba3f43e | 434 | int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal); |
3e170ce0 | 435 | |
39236c6e A |
436 | extern int memorystatus_remove(proc_t p, boolean_t locked); |
437 | ||
d9a64523 | 438 | int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now); |
39037602 A |
439 | |
440 | ||
39236c6e A |
441 | extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol); |
442 | extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol); | |
443 | extern int memorystatus_dirty_get(proc_t p); | |
fe8ab488 | 444 | extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol); |
39236c6e A |
445 | |
446 | extern int memorystatus_on_terminate(proc_t p); | |
447 | ||
448 | extern void memorystatus_on_suspend(proc_t p); | |
449 | extern void memorystatus_on_resume(proc_t p); | |
450 | extern void memorystatus_on_inactivity(proc_t p); | |
451 | ||
452 | extern void memorystatus_on_pageout_scan_end(void); | |
453 | ||
454 | /* Memorystatus kevent */ | |
316670eb | 455 | |
39236c6e | 456 | void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr); |
316670eb | 457 | |
39236c6e A |
458 | int memorystatus_knote_register(struct knote *kn); |
459 | void memorystatus_knote_unregister(struct knote *kn); | |
316670eb | 460 | |
39037602 | 461 | #if CONFIG_MEMORYSTATUS |
813fb2f6 A |
462 | void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); |
463 | void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); | |
39037602 | 464 | void proc_memstat_terminated(proc_t p, boolean_t set); |
d9a64523 | 465 | void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit); |
39037602 A |
466 | #endif /* CONFIG_MEMORYSTATUS */ |
467 | ||
3e170ce0 A |
468 | int memorystatus_get_pressure_status_kdp(void); |
469 | ||
5ba3f43e A |
470 | #if CONFIG_JETSAM |
471 | ||
39236c6e | 472 | typedef enum memorystatus_policy { |
316670eb A |
473 | kPolicyDefault = 0x0, |
474 | kPolicyMoreFree = 0x1, | |
475 | kPolicyDiagnoseAll = 0x2, | |
476 | kPolicyDiagnoseFirst = 0x4, | |
477 | kPolicyDiagnoseActive = (kPolicyDiagnoseAll | kPolicyDiagnoseFirst), | |
478 | } memorystatus_policy_t; | |
479 | ||
39236c6e | 480 | boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async); |
fe8ab488 | 481 | boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async); |
d9a64523 | 482 | boolean_t memorystatus_kill_on_VM_compressor_thrashing(boolean_t async); |
39236c6e | 483 | boolean_t memorystatus_kill_on_vnode_limit(void); |
316670eb | 484 | |
fe8ab488 | 485 | void jetsam_on_ledger_cpulimit_exceeded(void); |
d9a64523 | 486 | void memorystatus_fast_jetsam_override(boolean_t enable_override); |
316670eb | 487 | |
5ba3f43e | 488 | #endif /* CONFIG_JETSAM */ |
316670eb | 489 | |
5ba3f43e | 490 | boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid); |
d9a64523 | 491 | boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async); |
5ba3f43e | 492 | void memorystatus_pages_update(unsigned int pages_avail); |
316670eb | 493 | |
39236c6e | 494 | boolean_t memorystatus_idle_exit_from_VM(void); |
316670eb | 495 | |
6d2010ae A |
496 | |
497 | #ifdef CONFIG_FREEZE | |
6d2010ae | 498 | |
d9a64523 A |
499 | #define FREEZE_PAGES_MIN ( 8 * 1024 * 1024 / PAGE_SIZE) |
500 | #define FREEZE_PAGES_MAX (32 * 1024 * 1024 / PAGE_SIZE) | |
6d2010ae | 501 | |
316670eb | 502 | #define FREEZE_SUSPENDED_THRESHOLD_DEFAULT 4 |
d9a64523 | 503 | #define FREEZE_PROCESSES_MAX 20 |
316670eb | 504 | |
3e170ce0 | 505 | #define FREEZE_DAILY_MB_MAX_DEFAULT 1024 |
d9a64523 A |
506 | #define FREEZE_DEGRADATION_BUDGET_THRESHOLD 25 //degraded perf. when the daily budget left falls below this threshold percentage |
507 | ||
508 | #define MAX_FROZEN_SHARED_MB_PERCENT 10 /* max shared MB calculated as percent of system task limit. */ | |
509 | #define MAX_FROZEN_PROCESS_DEMOTIONS 2 /* max demotions of frozen processes into IDLE band done daily. */ | |
510 | #define MIN_THAW_DEMOTION_THRESHOLD 5 /* min # of thaws required for a process to be safe from demotion. */ | |
511 | #define MIN_THAW_REFREEZE_THRESHOLD 3 /* min # of global thaws needed for us to consider refreezing these processes. */ | |
316670eb A |
512 | |
513 | typedef struct throttle_interval_t { | |
514 | uint32_t mins; | |
515 | uint32_t burst_multiple; | |
516 | uint32_t pageouts; | |
517 | uint32_t max_pageouts; | |
518 | mach_timespec_t ts; | |
316670eb A |
519 | } throttle_interval_t; |
520 | ||
521 | extern boolean_t memorystatus_freeze_enabled; | |
522 | extern int memorystatus_freeze_wakeup; | |
523 | ||
524 | extern void memorystatus_freeze_init(void) __attribute__((section("__TEXT, initcode"))); | |
3e170ce0 | 525 | extern int memorystatus_freeze_process_sync(proc_t p); |
d9a64523 A |
526 | |
527 | #if DEVELOPMENT || DEBUG | |
528 | #define FREEZER_CONTROL_GET_STATUS (1) | |
529 | #endif /* DEVELOPMENT || DEBUG */ | |
530 | ||
316670eb A |
531 | #endif /* CONFIG_FREEZE */ |
532 | ||
39236c6e A |
533 | #if VM_PRESSURE_EVENTS |
534 | ||
39236c6e A |
535 | extern kern_return_t memorystatus_update_vm_pressure(boolean_t); |
536 | ||
fe8ab488 | 537 | #if CONFIG_MEMORYSTATUS |
3e170ce0 A |
538 | /* Flags */ |
539 | extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags); | |
39236c6e | 540 | extern int memorystatus_send_pressure_note(int pid); |
fe8ab488 | 541 | extern boolean_t memorystatus_is_foreground_locked(proc_t p); |
39236c6e | 542 | extern boolean_t memorystatus_bg_pressure_eligible(proc_t p); |
fe8ab488 | 543 | #endif /* CONFIG_MEMORYSTATUS */ |
39236c6e A |
544 | |
545 | #endif /* VM_PRESSURE_EVENTS */ | |
546 | ||
316670eb | 547 | #endif /* XNU_KERNEL_PRIVATE */ |
2d21ac55 | 548 | |
316670eb | 549 | #endif /* SYS_MEMORYSTATUS_H */ |