| 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 <stdint.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <sys/proc.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <mach_debug/zone_info.h> |
| 37 | |
| 38 | #define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus" |
| 39 | |
| 40 | #define JETSAM_PRIORITY_REVISION 2 |
| 41 | |
| 42 | #define JETSAM_PRIORITY_IDLE_HEAD -2 |
| 43 | /* The value -1 is an alias to JETSAM_PRIORITY_DEFAULT */ |
| 44 | #define JETSAM_PRIORITY_IDLE 0 |
| 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 |
| 47 | #define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 2 |
| 48 | #define JETSAM_PRIORITY_AGING_BAND2 JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC |
| 49 | #define JETSAM_PRIORITY_BACKGROUND 3 |
| 50 | #define JETSAM_PRIORITY_ELEVATED_INACTIVE JETSAM_PRIORITY_BACKGROUND |
| 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 |
| 62 | |
| 63 | #define JETSAM_PRIORITY_MAX 21 |
| 64 | |
| 65 | /* TODO - tune. This should probably be lower priority */ |
| 66 | #define JETSAM_PRIORITY_DEFAULT 18 |
| 67 | #define JETSAM_PRIORITY_TELEPHONY 19 |
| 68 | |
| 69 | /* Compatibility */ |
| 70 | #define DEFAULT_JETSAM_PRIORITY 18 |
| 71 | |
| 72 | #define DEFERRED_IDLE_EXIT_TIME_SECS 10 |
| 73 | |
| 74 | #define KEV_MEMORYSTATUS_SUBCLASS 3 |
| 75 | |
| 76 | enum { |
| 77 | kMemorystatusLevelNote = 1, |
| 78 | kMemorystatusSnapshotNote = 2, |
| 79 | kMemorystatusFreezeNote = 3, |
| 80 | kMemorystatusPressureNote = 4 |
| 81 | }; |
| 82 | |
| 83 | enum { |
| 84 | kMemorystatusLevelAny = -1, |
| 85 | kMemorystatusLevelNormal = 0, |
| 86 | kMemorystatusLevelWarning = 1, |
| 87 | kMemorystatusLevelUrgent = 2, |
| 88 | kMemorystatusLevelCritical = 3 |
| 89 | }; |
| 90 | |
| 91 | typedef struct memorystatus_priority_entry { |
| 92 | pid_t pid; |
| 93 | int32_t priority; |
| 94 | uint64_t user_data; |
| 95 | int32_t limit; /* MB */ |
| 96 | uint32_t state; |
| 97 | } memorystatus_priority_entry_t; |
| 98 | |
| 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 | |
| 124 | typedef struct memorystatus_kernel_stats { |
| 125 | uint32_t free_pages; |
| 126 | uint32_t active_pages; |
| 127 | uint32_t inactive_pages; |
| 128 | uint32_t throttled_pages; |
| 129 | uint32_t purgeable_pages; |
| 130 | uint32_t wired_pages; |
| 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; |
| 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]; |
| 142 | } memorystatus_kernel_stats_t; |
| 143 | |
| 144 | /* |
| 145 | ** This is a variable-length struct. |
| 146 | ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t * |
| 147 | */ |
| 148 | |
| 149 | typedef struct jetsam_snapshot_entry { |
| 150 | pid_t pid; |
| 151 | char name[(2*MAXCOMLEN)+1]; |
| 152 | int32_t priority; |
| 153 | uint32_t state; |
| 154 | uint32_t fds; |
| 155 | uint8_t uuid[16]; |
| 156 | uint64_t user_data; |
| 157 | uint64_t killed; |
| 158 | uint64_t pages; |
| 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 */ |
| 175 | struct timeval64 cpu_time; |
| 176 | uint64_t jse_thaw_count; |
| 177 | } memorystatus_jetsam_snapshot_entry_t; |
| 178 | |
| 179 | typedef struct jetsam_snapshot { |
| 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 */ |
| 184 | size_t entry_count; |
| 185 | memorystatus_jetsam_snapshot_entry_t entries[]; |
| 186 | } memorystatus_jetsam_snapshot_t; |
| 187 | |
| 188 | typedef struct memorystatus_freeze_entry { |
| 189 | int32_t pid; |
| 190 | uint32_t flags; |
| 191 | uint32_t pages; |
| 192 | } memorystatus_freeze_entry_t; |
| 193 | |
| 194 | /* TODO - deprecate; see <rdar://problem/12969599> */ |
| 195 | #define kMaxSnapshotEntries 192 |
| 196 | |
| 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 |
| 204 | |
| 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 | */ |
| 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 |
| 228 | |
| 229 | /* |
| 230 | * Jetsam exit reason definitions - not related to memorystatus |
| 231 | */ |
| 232 | #define JETSAM_REASON_CPULIMIT 100 |
| 233 | |
| 234 | /* Cause */ |
| 235 | enum { |
| 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, |
| 249 | }; |
| 250 | |
| 251 | /* For backwards compatibility */ |
| 252 | #define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage |
| 253 | #define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing |
| 254 | #define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING |
| 255 | |
| 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); |
| 262 | #endif |
| 263 | |
| 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 |
| 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 */ |
| 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. */ |
| 277 | #define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */ |
| 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 */ |
| 291 | |
| 292 | /* Commands that act on a group of processes */ |
| 293 | #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100 |
| 294 | |
| 295 | #if PRIVATE |
| 296 | /* Test commands */ |
| 297 | |
| 298 | /* Trigger forced jetsam */ |
| 299 | #define MEMORYSTATUS_CMD_TEST_JETSAM 1000 |
| 300 | #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001 |
| 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; |
| 307 | |
| 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 | |
| 314 | #endif /* PRIVATE */ |
| 315 | |
| 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 | |
| 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 | * |
| 343 | * Copy mode - this returns the previous snapshot |
| 344 | * collected by the system. The current snaphshot |
| 345 | * might be only half populated. |
| 346 | * |
| 347 | * Snapshots are inherently racey between request |
| 348 | * for buffer size and actual data compilation. |
| 349 | */ |
| 350 | |
| 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 |
| 355 | |
| 356 | /* |
| 357 | * For use with memorystatus_control: |
| 358 | * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES |
| 359 | */ |
| 360 | typedef struct memorystatus_priority_properties { |
| 361 | int32_t priority; |
| 362 | uint64_t user_data; |
| 363 | } memorystatus_priority_properties_t; |
| 364 | |
| 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 | |
| 379 | #ifdef XNU_KERNEL_PRIVATE |
| 380 | |
| 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 | * |
| 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 | */ |
| 407 | |
| 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 */ |
| 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 */ |
| 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 |
| 425 | of the aging bands and/or the IDLE band. */ |
| 426 | |
| 427 | extern void memorystatus_init(void) __attribute__((section("__TEXT, initcode" ))); |
| 428 | |
| 429 | extern void memorystatus_init_at_boot_snapshot(void); |
| 430 | |
| 431 | extern int memorystatus_add(proc_t p, boolean_t locked); |
| 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, |
| 434 | int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal); |
| 435 | |
| 436 | extern int memorystatus_remove(proc_t p, boolean_t locked); |
| 437 | |
| 438 | int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now); |
| 439 | |
| 440 | |
| 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); |
| 444 | extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol); |
| 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 */ |
| 455 | |
| 456 | void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr); |
| 457 | |
| 458 | int memorystatus_knote_register(struct knote *kn); |
| 459 | void memorystatus_knote_unregister(struct knote *kn); |
| 460 | |
| 461 | #if CONFIG_MEMORYSTATUS |
| 462 | void memorystatus_log_exception(const int , boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); |
| 463 | void (int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); |
| 464 | void proc_memstat_terminated(proc_t p, boolean_t set); |
| 465 | void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit); |
| 466 | #endif /* CONFIG_MEMORYSTATUS */ |
| 467 | |
| 468 | int memorystatus_get_pressure_status_kdp(void); |
| 469 | |
| 470 | #if CONFIG_JETSAM |
| 471 | |
| 472 | typedef enum memorystatus_policy { |
| 473 | kPolicyDefault = 0x0, |
| 474 | kPolicyMoreFree = 0x1, |
| 475 | kPolicyDiagnoseAll = 0x2, |
| 476 | kPolicyDiagnoseFirst = 0x4, |
| 477 | kPolicyDiagnoseActive = (kPolicyDiagnoseAll | kPolicyDiagnoseFirst), |
| 478 | } memorystatus_policy_t; |
| 479 | |
| 480 | boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async); |
| 481 | boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async); |
| 482 | boolean_t memorystatus_kill_on_VM_compressor_thrashing(boolean_t async); |
| 483 | boolean_t memorystatus_kill_on_vnode_limit(void); |
| 484 | |
| 485 | void jetsam_on_ledger_cpulimit_exceeded(void); |
| 486 | void memorystatus_fast_jetsam_override(boolean_t enable_override); |
| 487 | |
| 488 | #endif /* CONFIG_JETSAM */ |
| 489 | |
| 490 | boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid); |
| 491 | boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async); |
| 492 | void memorystatus_pages_update(unsigned int pages_avail); |
| 493 | |
| 494 | boolean_t memorystatus_idle_exit_from_VM(void); |
| 495 | |
| 496 | |
| 497 | #ifdef CONFIG_FREEZE |
| 498 | |
| 499 | #define FREEZE_PAGES_MIN ( 8 * 1024 * 1024 / PAGE_SIZE) |
| 500 | #define FREEZE_PAGES_MAX (32 * 1024 * 1024 / PAGE_SIZE) |
| 501 | |
| 502 | #define FREEZE_SUSPENDED_THRESHOLD_DEFAULT 4 |
| 503 | #define FREEZE_PROCESSES_MAX 20 |
| 504 | |
| 505 | #define FREEZE_DAILY_MB_MAX_DEFAULT 1024 |
| 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. */ |
| 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; |
| 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" ))); |
| 525 | extern int memorystatus_freeze_process_sync(proc_t p); |
| 526 | |
| 527 | #if DEVELOPMENT || DEBUG |
| 528 | #define FREEZER_CONTROL_GET_STATUS (1) |
| 529 | #endif /* DEVELOPMENT || DEBUG */ |
| 530 | |
| 531 | #endif /* CONFIG_FREEZE */ |
| 532 | |
| 533 | #if VM_PRESSURE_EVENTS |
| 534 | |
| 535 | extern kern_return_t memorystatus_update_vm_pressure(boolean_t); |
| 536 | |
| 537 | #if CONFIG_MEMORYSTATUS |
| 538 | /* Flags */ |
| 539 | extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags); |
| 540 | extern int memorystatus_send_pressure_note(int pid); |
| 541 | extern boolean_t memorystatus_is_foreground_locked(proc_t p); |
| 542 | extern boolean_t memorystatus_bg_pressure_eligible(proc_t p); |
| 543 | #endif /* CONFIG_MEMORYSTATUS */ |
| 544 | |
| 545 | #endif /* VM_PRESSURE_EVENTS */ |
| 546 | |
| 547 | #endif /* XNU_KERNEL_PRIVATE */ |
| 548 | |
| 549 | #endif /* SYS_MEMORYSTATUS_H */ |
| 550 | |