kern_memorystatus.h
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 <sys/time.h> 33 #include <mach_debug/zone_info.h> 34 #include <sys/proc.h> 35 36 #define MEMORYSTATUS_ENTITLEMENT "com.apple.private.memorystatus" 37 38 #define JETSAM_PRIORITY_REVISION 2 39 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 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 /* 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. 75 * 76 * For kJetsamAgingPolicySysProcsReclaimedFirst, 77 * 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 82 * 83 * Apps: The apps are aged for DEFERRED_IDLE_EXIT_TIME_SECS factored 84 * by kJetsamAppsIdleDelayTimeRatio. 85 */ 86 #define DEFERRED_IDLE_EXIT_TIME_SECS 10 87 88 #define KEV_MEMORYSTATUS_SUBCLASS 3 89 90 enum { 91 kMemorystatusLevelNote = 1, 92 kMemorystatusSnapshotNote = 2, 93 kMemorystatusFreezeNote = 3, 94 kMemorystatusPressureNote = 4 95 }; 96 97 enum { 98 kMemorystatusLevelAny = -1, 99 kMemorystatusLevelNormal = 0, 100 kMemorystatusLevelWarning = 1, 101 kMemorystatusLevelUrgent = 2, 102 kMemorystatusLevelCritical = 3 103 }; 104 105 #define KEV_DIRTYSTATUS_SUBCLASS 4 106 107 enum { 108 kDirtyStatusChangeNote = 1 109 }; 110 111 typedef struct memorystatus_priority_entry { 112 pid_t pid; 113 int32_t priority; 114 uint64_t user_data; 115 int32_t limit; /* MB */ 116 uint32_t state; 117 } memorystatus_priority_entry_t; 118 119 /* 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. 124 * 125 * To support new fields/properties, we will add a new structure with a 126 * new version and a new size. 127 */ 128 #define MEMORYSTATUS_MPE_VERSION_1 1 129 130 #define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1) 131 132 typedef struct memorystatus_properties_entry_v1 { 133 int version; 134 pid_t pid; 135 int32_t priority; 136 int use_probability; 137 uint64_t user_data; 138 int32_t limit; /* MB */ 139 uint32_t state; 140 char proc_name[MAXCOMLEN + 1]; 141 char __pad1[3]; 142 } memorystatus_properties_entry_v1_t; 143 144 typedef struct memorystatus_kernel_stats { 145 uint32_t free_pages; 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; 163 164 typedef enum memorystatus_freeze_skip_reason { 165 kMemorystatusFreezeSkipReasonNone = 0, 166 kMemorystatusFreezeSkipReasonExcessSharedMemory = 1, 167 kMemorystatusFreezeSkipReasonLowPrivateSharedRatio = 2, 168 kMemorystatusFreezeSkipReasonNoCompressorSpace = 3, 169 kMemorystatusFreezeSkipReasonNoSwapSpace = 4, 170 kMemorystatusFreezeSkipReasonBelowMinPages = 5, 171 kMemorystatusFreezeSkipReasonLowProbOfUse = 6, 172 kMemorystatusFreezeSkipReasonOther = 7, 173 kMemorystatusFreezeSkipReasonOutOfBudget = 8, 174 kMemorystatusFreezeSkipReasonOutOfSlots = 9, 175 kMemorystatusFreezeSkipReasonDisabled = 10, 176 _kMemorystatusFreezeSkipReasonMax 177 } memorystatus_freeze_skip_reason_t; 178 /* 179 ** This is a variable-length struct. 180 ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t * 181 */ 182 183 typedef struct jetsam_snapshot_entry { 184 pid_t pid; 185 char name[(2 * MAXCOMLEN) + 1]; 186 int32_t priority; 187 uint32_t state; 188 uint32_t fds; 189 memorystatus_freeze_skip_reason_t jse_freeze_skip_reason; /* why wasn't this process frozen? */ 190 uint8_t uuid[16]; 191 uint64_t user_data; 192 uint64_t killed; 193 uint64_t pages; 194 uint64_t max_pages_lifetime; 195 uint64_t purgeable_pages; 196 uint64_t jse_internal_pages; 197 uint64_t jse_internal_compressed_pages; 198 uint64_t jse_purgeable_nonvolatile_pages; 199 uint64_t jse_purgeable_nonvolatile_compressed_pages; 200 uint64_t jse_alternate_accounting_pages; 201 uint64_t jse_alternate_accounting_compressed_pages; 202 uint64_t jse_iokit_mapped_pages; 203 uint64_t jse_page_table_pages; 204 uint64_t jse_memory_region_count; 205 uint64_t jse_gencount; /* memorystatus_thread generation counter */ 206 uint64_t jse_starttime; /* absolute time when process starts */ 207 uint64_t jse_killtime; /* absolute time when jetsam chooses to kill a process */ 208 uint64_t jse_idle_delta; /* time spent in idle band */ 209 uint64_t jse_coalition_jetsam_id; /* we only expose coalition id for COALITION_TYPE_JETSAM */ 210 struct timeval64 cpu_time; 211 uint64_t jse_thaw_count; 212 uint64_t jse_frozen_to_swap_pages; 213 } memorystatus_jetsam_snapshot_entry_t; 214 215 typedef struct jetsam_snapshot { 216 uint64_t snapshot_time; /* absolute time snapshot was initialized */ 217 uint64_t notification_time; /* absolute time snapshot was consumed */ 218 uint64_t js_gencount; /* memorystatus_thread generation counter */ 219 memorystatus_kernel_stats_t stats; /* system stat when snapshot is initialized */ 220 size_t entry_count; 221 memorystatus_jetsam_snapshot_entry_t entries[]; 222 } memorystatus_jetsam_snapshot_t; 223 224 typedef enum dirty_status_change_event_type { 225 kDirtyStatusChangedDirty = 0x0, 226 kDirtyStatusChangedClean = 0x1 227 } dirty_status_change_event_type_t; 228 229 typedef struct dirty_status_change_event { 230 pid_t dsc_pid; 231 char dsc_process_name[(2 * MAXCOMLEN) + 1]; 232 dirty_status_change_event_type_t dsc_event_type; 233 uint64_t dsc_time; 234 uint64_t dsc_pages; 235 int32_t dsc_priority; 236 } dirty_status_change_event_t; 237 238 /* TODO - deprecate; see <rdar://problem/12969599> */ 239 #define kMaxSnapshotEntries 192 240 241 /* 242 * default jetsam snapshot support 243 */ 244 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot; 245 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy; 246 #if CONFIG_FREEZE 247 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_freezer; 248 extern unsigned int memorystatus_jetsam_snapshot_freezer_max; 249 extern unsigned int memorystatus_jetsam_snapshot_freezer_size; 250 #endif /* CONFIG_FREEZE */ 251 extern unsigned int memorystatus_jetsam_snapshot_count; 252 extern unsigned int memorystatus_jetsam_snapshot_copy_count; 253 extern unsigned int memorystatus_jetsam_snapshot_max; 254 extern unsigned int memorystatus_jetsam_snapshot_size; 255 extern uint64_t memorystatus_jetsam_snapshot_last_timestamp; 256 extern uint64_t memorystatus_jetsam_snapshot_timeout; 257 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries 258 #define JETSAM_SNAPSHOT_TIMEOUT_SECS 30 259 260 /* General memorystatus stuff */ 261 262 extern uint64_t memorystatus_sysprocs_idle_delay_time; 263 extern uint64_t memorystatus_apps_idle_delay_time; 264 265 /* State */ 266 #define kMemorystatusSuspended 0x01 267 #define kMemorystatusFrozen 0x02 268 #define kMemorystatusWasThawed 0x04 269 #define kMemorystatusTracked 0x08 270 #define kMemorystatusSupportsIdleExit 0x10 271 #define kMemorystatusDirty 0x20 272 #define kMemorystatusAssertion 0x40 273 274 /* 275 * Jetsam exit reason definitions - related to memorystatus 276 * 277 * When adding new exit reasons also update: 278 * JETSAM_REASON_MEMORYSTATUS_MAX 279 * kMemorystatusKilled... Cause enum 280 * memorystatus_kill_cause_name[] 281 */ 282 #define JETSAM_REASON_INVALID 0 283 #define JETSAM_REASON_GENERIC 1 284 #define JETSAM_REASON_MEMORY_HIGHWATER 2 285 #define JETSAM_REASON_VNODE 3 286 #define JETSAM_REASON_MEMORY_VMPAGESHORTAGE 4 287 #define JETSAM_REASON_MEMORY_PROCTHRASHING 5 288 #define JETSAM_REASON_MEMORY_FCTHRASHING 6 289 #define JETSAM_REASON_MEMORY_PERPROCESSLIMIT 7 290 #define JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE 8 291 #define JETSAM_REASON_MEMORY_IDLE_EXIT 9 292 #define JETSAM_REASON_ZONE_MAP_EXHAUSTION 10 293 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 11 294 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE 12 295 #define JETSAM_REASON_LOWSWAP 13 296 #define JETSAM_REASON_MEMORYSTATUS_MAX JETSAM_REASON_LOWSWAP 297 298 /* 299 * Jetsam exit reason definitions - not related to memorystatus 300 */ 301 #define JETSAM_REASON_CPULIMIT 100 302 303 /* Cause */ 304 enum { 305 kMemorystatusInvalid = JETSAM_REASON_INVALID, 306 kMemorystatusKilled = JETSAM_REASON_GENERIC, 307 kMemorystatusKilledHiwat = JETSAM_REASON_MEMORY_HIGHWATER, 308 kMemorystatusKilledVnodes = JETSAM_REASON_VNODE, 309 kMemorystatusKilledVMPageShortage = JETSAM_REASON_MEMORY_VMPAGESHORTAGE, 310 kMemorystatusKilledProcThrashing = JETSAM_REASON_MEMORY_PROCTHRASHING, 311 kMemorystatusKilledFCThrashing = JETSAM_REASON_MEMORY_FCTHRASHING, 312 kMemorystatusKilledPerProcessLimit = JETSAM_REASON_MEMORY_PERPROCESSLIMIT, 313 kMemorystatusKilledDiskSpaceShortage = JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE, 314 kMemorystatusKilledIdleExit = JETSAM_REASON_MEMORY_IDLE_EXIT, 315 kMemorystatusKilledZoneMapExhaustion = JETSAM_REASON_ZONE_MAP_EXHAUSTION, 316 kMemorystatusKilledVMCompressorThrashing = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING, 317 kMemorystatusKilledVMCompressorSpaceShortage = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE, 318 kMemorystatusKilledLowSwap = JETSAM_REASON_LOWSWAP, 319 }; 320 321 /* 322 * For backwards compatibility 323 * Keeping these around for external users (e.g. ReportCrash, Ariadne). 324 * TODO: Remove once they stop using these. 325 */ 326 #define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage 327 #define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing 328 #define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 329 330 /* Memorystatus control */ 331 #define MEMORYSTATUS_BUFFERSIZE_MAX 65536 332 333 #ifndef KERNEL 334 int memorystatus_get_level(user_addr_t level); 335 int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize); 336 #endif 337 338 /* Commands */ 339 #define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1 340 #define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2 341 #define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3 342 #define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4 343 #define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */ 344 #define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */ 345 #define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 7 /* Set memory limits plus attributes independently */ 346 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 8 /* Get memory limits plus attributes */ 347 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE 9 /* Set the task's status as a privileged listener w.r.t memory notifications */ 348 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE 10 /* Reset the task's status as a privileged listener w.r.t memory notifications */ 349 #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. */ 350 #define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12 /* Disable the 'lenient' mode for aggressive jetsam. */ 351 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */ 352 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE 14 /* Set the inactive jetsam band for a process to JETSAM_PRIORITY_ELEVATED_INACTIVE */ 353 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE 15 /* Reset the inactive jetsam band for a process to the default band (0)*/ 354 #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 */ 355 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED 17 /* Return the 'managed' status of a process */ 356 #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., 357 * if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */ 358 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE 19 /* Return the freezable state of a process. */ 359 360 #define MEMORYSTATUS_CMD_FREEZER_CONTROL 20 361 362 #define MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE 21 /* Query if the lenient mode for aggressive jetsam is enabled. */ 363 364 #define MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT 22 /* Used by DYLD to increase the jetsam active and inactive limits, when using roots */ 365 366 #if PRIVATE 367 #define MEMORYSTATUS_CMD_SET_TESTING_PID 23 /* Used by unit tests in the development kernel only. */ 368 #endif /* PRIVATE */ 369 370 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN 24 /* Check if the process is frozen. */ 371 372 /* Commands that act on a group of processes */ 373 #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100 374 375 #if PRIVATE 376 /* Test commands */ 377 378 /* Trigger forced jetsam */ 379 #define MEMORYSTATUS_CMD_TEST_JETSAM 1000 380 #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001 381 382 /* Panic on jetsam options */ 383 typedef struct memorystatus_jetsam_panic_options { 384 uint32_t data; 385 uint32_t mask; 386 } memorystatus_jetsam_panic_options_t; 387 388 #define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1002 389 390 /* Select priority band sort order */ 391 #define JETSAM_SORT_NOSORT 0 392 #define JETSAM_SORT_DEFAULT 1 393 394 #endif /* PRIVATE */ 395 396 /* memorystatus_control() flags */ 397 398 #define MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 0x1 /* A populated snapshot buffer is returned on demand */ 399 #define MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 0x2 /* Returns a snapshot with memstats collected at boot */ 400 #define MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 0x4 /* Returns the previously populated snapshot created by the system */ 401 #define MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY 0x8 /* Set jetsam priorities for a group of pids */ 402 #define MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY 0x10 /* Set probability of use for a group of processes */ 403 404 #if PRIVATE 405 #define MEMORYSTATUS_FLAGS_SET_TESTING_PID 0x20 /* Only used by xnu unit tests. */ 406 #define MEMORYSTATUS_FLAGS_UNSET_TESTING_PID 0x40 /* Only used by xnu unit tests. */ 407 #endif /* PRIVATE */ 408 409 #define MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER 0x80 /* A snapshot buffer containing app kills since last consumption */ 410 /* 411 * For use with memorystatus_control: 412 * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 413 * 414 * A jetsam snapshot is initialized when a non-idle 415 * jetsam event occurs. The data is held in the 416 * buffer until it is reaped. This is the default 417 * behavior. 418 * 419 * Flags change the default behavior: 420 * Demand mode - this is an on_demand snapshot, 421 * meaning data is populated upon request. 422 * 423 * Boot mode - this is a snapshot of 424 * memstats collected before loading the 425 * init program. Once collected, these 426 * stats do not change. In this mode, 427 * the snapshot entry_count is always 0. 428 * 429 * Copy mode - this returns the previous snapshot 430 * collected by the system. The current snaphshot 431 * might be only half populated. 432 * 433 * Snapshots are inherently racey between request 434 * for buffer size and actual data compilation. 435 */ 436 437 /* These definitions are required for backwards compatibility */ 438 #define MEMORYSTATUS_SNAPSHOT_ON_DEMAND MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 439 #define MEMORYSTATUS_SNAPSHOT_AT_BOOT MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 440 #define MEMORYSTATUS_SNAPSHOT_COPY MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 441 442 /* 443 * For use with memorystatus_control: 444 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 445 */ 446 typedef struct memorystatus_priority_properties { 447 int32_t priority; 448 uint64_t user_data; 449 } memorystatus_priority_properties_t; 450 451 /* 452 * Inform the kernel that setting the priority property is driven by assertions. 453 */ 454 #define MEMORYSTATUS_SET_PRIORITY_ASSERTION 0x1 455 456 /* 457 * For use with memorystatus_control: 458 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 459 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 460 */ 461 typedef struct memorystatus_memlimit_properties { 462 int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */ 463 uint32_t memlimit_active_attr; 464 int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */ 465 uint32_t memlimit_inactive_attr; 466 } memorystatus_memlimit_properties_t; 467 468 typedef struct memorystatus_memlimit_properties2 { 469 memorystatus_memlimit_properties_t v1; 470 uint32_t memlimit_increase; /* jetsam memory limit increase (in MB) for active and inactive states */ 471 uint32_t memlimit_increase_bytes; /* bytes used to determine the jetsam memory limit increase, for active and inactive states */ 472 } memorystatus_memlimit_properties2_t; 473 474 #define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */ 475 476 #ifdef XNU_KERNEL_PRIVATE 477 478 /* 479 * A process will be killed immediately if it crosses a memory limit marked as fatal. 480 * Fatal limit types are the 481 * - default system-wide task limit 482 * - per-task custom memory limit 483 * 484 * A process with a non-fatal memory limit can exceed that limit, but becomes an early 485 * candidate for jetsam when the device is under memory pressure. 486 * Non-fatal limit types are the 487 * - high-water-mark limit 488 * 489 * Processes that opt into dirty tracking are evaluated 490 * based on clean vs dirty state. 491 * dirty ==> active 492 * clean ==> inactive 493 * 494 * Processes that do not opt into dirty tracking are 495 * evalulated based on priority level. 496 * Foreground or above ==> active 497 * Below Foreground ==> inactive 498 */ 499 500 /* 501 * p_memstat_state flag holds 502 * - in kernel process state and memlimit state 503 */ 504 505 #define P_MEMSTAT_SUSPENDED 0x00000001 /* Process is suspended and likely in the IDLE band */ 506 #define P_MEMSTAT_FROZEN 0x00000002 /* Process has some state on disk. It should be suspended */ 507 #define P_MEMSTAT_FREEZE_DISABLED 0x00000004 /* Process isn't freeze-eligible and will not be frozen */ 508 #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 */ 509 #define P_MEMSTAT_LOCKED 0x00000010 /* Process is being actively worked on behind the proc_list_lock */ 510 #define P_MEMSTAT_TERMINATED 0x00000020 /* Process is exiting */ 511 #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 */ 512 #define P_MEMSTAT_PRIORITYUPDATED 0x00000080 /* Process had its jetsam priority updated */ 513 #define P_MEMSTAT_FOREGROUND 0x00000100 /* Process is in the FG jetsam band...unused??? */ 514 #define P_MEMSTAT_REFREEZE_ELIGIBLE 0x00000400 /* Process was once thawed i.e. its state was brought back from disk. It is now refreeze eligible.*/ 515 #define P_MEMSTAT_MANAGED 0x00000800 /* Process is managed by assertiond i.e. is either application or extension */ 516 #define P_MEMSTAT_INTERNAL 0x00001000 /* Process is a system-critical-not-be-jetsammed process i.e. launchd */ 517 #define P_MEMSTAT_FATAL_MEMLIMIT 0x00002000 /* current fatal state of the process's memlimit */ 518 #define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL 0x00004000 /* if set, exceeding limit is fatal when the process is active */ 519 #define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00008000 /* if set, exceeding limit is fatal when the process is inactive */ 520 #define P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND 0x00010000 /* if set, the process will go into this band & stay there when in the background instead 521 * of the aging bands and/or the IDLE band. */ 522 #define P_MEMSTAT_PRIORITY_ASSERTION 0x00020000 /* jetsam priority is being driven by an assertion */ 523 #define P_MEMSTAT_FREEZE_CONSIDERED 0x00040000 /* This process has been considered for the freezer. */ 524 525 /* 526 * p_memstat_relaunch_flags holds 527 * - relaunch behavior when jetsammed 528 */ 529 #define P_MEMSTAT_RELAUNCH_UNKNOWN 0x0 530 #define P_MEMSTAT_RELAUNCH_LOW 0x1 531 #define P_MEMSTAT_RELAUNCH_MED 0x2 532 #define P_MEMSTAT_RELAUNCH_HIGH 0x4 533 534 /* 535 * Checking the p_memstat_state almost always requires the proc_list_lock 536 * because the jetsam thread could be on the other core changing the state. 537 * 538 * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too. 539 * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too. 540 */ 541 #define isApp(p) ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK)) 542 #define isSysProc(p) ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK)) 543 544 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1) 545 546 typedef struct memstat_bucket { 547 TAILQ_HEAD(, proc) list; 548 int count; 549 int relaunch_high_count; 550 } memstat_bucket_t; 551 552 extern memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT]; 553 554 /* 555 * Table that expresses the probability of a process 556 * being used in the next hour. 557 */ 558 typedef struct memorystatus_internal_probabilities { 559 char proc_name[MAXCOMLEN + 1]; 560 int use_probability; 561 } memorystatus_internal_probabilities_t; 562 563 extern memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table; 564 extern size_t memorystatus_global_probabilities_size; 565 566 567 extern void memorystatus_init(void); 568 569 extern void memorystatus_init_at_boot_snapshot(void); 570 571 extern int memorystatus_add(proc_t p, boolean_t locked); 572 extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t is_assertion, boolean_t effective, 573 boolean_t update_memlimit, int32_t memlimit_active, boolean_t memlimit_active_is_fatal, 574 int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal); 575 576 /* Remove this process from jetsam bands for killing or freezing. 577 * The proc_list_lock is held by the caller. 578 * @param p: The process to remove. 579 * @return: 0 if successful. EAGAIN if the process can't be removed right now (because it's being frozen) or ESRCH. 580 */ 581 extern int memorystatus_remove(proc_t p); 582 583 int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now); 584 int memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags); 585 586 extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol); 587 extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol); 588 extern int memorystatus_dirty_get(proc_t p, boolean_t locked); 589 extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol); 590 591 extern int memorystatus_on_terminate(proc_t p); 592 593 extern void memorystatus_on_suspend(proc_t p); 594 extern void memorystatus_on_resume(proc_t p); 595 extern void memorystatus_on_inactivity(proc_t p); 596 597 extern void memorystatus_on_pageout_scan_end(void); 598 599 /* Memorystatus kevent */ 600 601 void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr); 602 603 int memorystatus_knote_register(struct knote *kn); 604 void memorystatus_knote_unregister(struct knote *kn); 605 606 #if CONFIG_MEMORYSTATUS 607 void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); 608 void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); 609 void proc_memstat_terminated(proc_t p, boolean_t set); 610 void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit); 611 612 #if __arm64__ 613 void memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase); 614 void memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p); 615 void memorystatus_act_on_entitled_task_limit(proc_t p); 616 #endif /* __arm64__ */ 617 618 #endif /* CONFIG_MEMORYSTATUS */ 619 620 int memorystatus_get_pressure_status_kdp(void); 621 622 #if CONFIG_JETSAM 623 624 typedef enum memorystatus_policy { 625 kPolicyDefault = 0x0, 626 kPolicyMoreFree = 0x1, 627 } memorystatus_policy_t; 628 629 boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async); 630 boolean_t memorystatus_kill_on_FC_thrashing(boolean_t async); 631 boolean_t memorystatus_kill_on_VM_compressor_thrashing(boolean_t async); 632 boolean_t memorystatus_kill_on_vnode_limit(void); 633 634 void jetsam_on_ledger_cpulimit_exceeded(void); 635 void memorystatus_fast_jetsam_override(boolean_t enable_override); 636 637 #endif /* CONFIG_JETSAM */ 638 639 /* These are very verbose printfs(), enable with 640 * MEMORYSTATUS_DEBUG_LOG 641 */ 642 #if MEMORYSTATUS_DEBUG_LOG 643 #define MEMORYSTATUS_DEBUG(cond, format, ...) \ 644 do { \ 645 if (cond) { printf(format, ##__VA_ARGS__); } \ 646 } while(0) 647 #else 648 #define MEMORYSTATUS_DEBUG(cond, format, ...) 649 #endif 650 651 boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid); 652 boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async); 653 void memorystatus_pages_update(unsigned int pages_avail); 654 boolean_t memorystatus_idle_exit_from_VM(void); 655 proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search); 656 proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search); 657 void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages); 658 void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state); 659 void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert, boolean_t skip_demotion_check); 660 661 #if VM_PRESSURE_EVENTS 662 663 extern kern_return_t memorystatus_update_vm_pressure(boolean_t); 664 665 #if CONFIG_MEMORYSTATUS 666 /* Flags */ 667 extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags); 668 extern int memorystatus_send_pressure_note(int pid); 669 extern boolean_t memorystatus_is_foreground_locked(proc_t p); 670 extern boolean_t memorystatus_bg_pressure_eligible(proc_t p); 671 #endif /* CONFIG_MEMORYSTATUS */ 672 673 #endif /* VM_PRESSURE_EVENTS */ 674 675 #endif /* XNU_KERNEL_PRIVATE */ 676 677 #endif /* SYS_MEMORYSTATUS_H */