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 #include <sys/reason.h> 36 #include <stdbool.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_ENTITLED_MAX 9 /* Entitled processes may use bands 1-9 for experimentation */ 46 #define JETSAM_PRIORITY_IDLE_DEFERRED 10 /* Keeping this around till all xnu_quick_tests can be moved away from it.*/ 47 #define JETSAM_PRIORITY_AGING_BAND1 JETSAM_PRIORITY_IDLE_DEFERRED 48 #define JETSAM_PRIORITY_AGING_BAND1_STUCK 15 /* Sysprocs stuck in JETSAM_PRIORITY_IDLE_DEFERRED will be moved to this band */ 49 #define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 20 50 #define JETSAM_PRIORITY_AGING_BAND2 JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 51 #define JETSAM_PRIORITY_BACKGROUND 30 52 /* 53 * NB: This band is no longer used by mail, but IS used by many active 54 * processes doing background work. 55 */ 56 #define JETSAM_PRIORITY_MAIL 40 57 #define JETSAM_PRIORITY_ELEVATED_INACTIVE JETSAM_PRIORITY_MAIL 58 #define JETSAM_PRIORITY_PHONE 50 59 #define JETSAM_PRIORITY_FREEZER 75 60 #define JETSAM_PRIORITY_UI_SUPPORT 80 61 #define JETSAM_PRIORITY_FOREGROUND_SUPPORT 90 62 #define JETSAM_PRIORITY_FOREGROUND 100 63 #define JETSAM_PRIORITY_AUDIO_AND_ACCESSORY 120 64 #define JETSAM_PRIORITY_CONDUCTOR 130 65 #define JETSAM_PRIORITY_DRIVER_APPLE 150 66 #define JETSAM_PRIORITY_HOME 160 67 #define JETSAM_PRIORITY_EXECUTIVE 170 68 #define JETSAM_PRIORITY_IMPORTANT 180 69 #define JETSAM_PRIORITY_CRITICAL 190 70 71 #define JETSAM_PRIORITY_MAX 210 72 73 /* 74 * Used to show that a process is so high priority it is not processed by jetsam at all. 75 * This is set on launchd and kernel_task. 76 */ 77 #define JETSAM_PRIORITY_INTERNAL 999 78 79 /* TODO - tune. This should probably be lower priority */ 80 #define JETSAM_PRIORITY_DEFAULT 180 81 #define JETSAM_PRIORITY_TELEPHONY 190 82 83 /* Compatibility */ 84 #define DEFAULT_JETSAM_PRIORITY 180 85 86 /* 87 * The deferral time used by default for apps and daemons in all aging 88 * policies except kJetsamAgingPolicySysProcsReclaimedFirst is 89 * DEFERRED_IDLE_EXIT_TIME_SECS. 90 * 91 * For kJetsamAgingPolicySysProcsReclaimedFirst, 92 * 93 * Daemons: The actual idle deferred time for the daemon is based on 94 * the relaunch behavior of the daemon. The relaunch behavior determines 95 * the scaling factor applied to DEFERRED_IDLE_EXIT_TIME_SECS. See 96 * kJetsamSysProcsIdleDelayTime* ratios defined in kern_memorystatus.c 97 * 98 * Apps: The apps are aged for DEFERRED_IDLE_EXIT_TIME_SECS factored 99 * by kJetsamAppsIdleDelayTimeRatio. 100 */ 101 #define DEFERRED_IDLE_EXIT_TIME_SECS 10 102 103 #define KEV_MEMORYSTATUS_SUBCLASS 3 104 105 enum { 106 kMemorystatusLevelNote = 1, 107 kMemorystatusSnapshotNote = 2, 108 kMemorystatusFreezeNote = 3, 109 kMemorystatusPressureNote = 4 110 }; 111 112 enum { 113 kMemorystatusLevelAny = -1, 114 kMemorystatusLevelNormal = 0, 115 kMemorystatusLevelWarning = 1, 116 kMemorystatusLevelUrgent = 2, 117 kMemorystatusLevelCritical = 3 118 }; 119 120 typedef struct memorystatus_priority_entry { 121 pid_t pid; 122 int32_t priority; 123 uint64_t user_data; 124 int32_t limit; /* MB */ 125 uint32_t state; 126 } memorystatus_priority_entry_t; 127 128 /* 129 * This should be the structure to specify different properties 130 * for processes (group or single) from user-space. Unfortunately, 131 * we can't move to it completely because the priority_entry structure 132 * above has been in use for a while now. We'll have to deprecate it. 133 * 134 * To support new fields/properties, we will add a new structure with a 135 * new version and a new size. 136 */ 137 #define MEMORYSTATUS_MPE_VERSION_1 1 138 139 #define MEMORYSTATUS_MPE_VERSION_1_SIZE sizeof(struct memorystatus_properties_entry_v1) 140 141 typedef struct memorystatus_properties_entry_v1 { 142 int version; 143 pid_t pid; 144 int32_t priority; 145 int use_probability; 146 uint64_t user_data; 147 int32_t limit; /* MB */ 148 uint32_t state; 149 char proc_name[MAXCOMLEN + 1]; 150 char __pad1[3]; 151 } memorystatus_properties_entry_v1_t; 152 153 /* 154 * Represents a freeze or demotion candidate. 155 */ 156 typedef struct memorystatus_properties_freeze_entry_v1 { 157 int version; 158 pid_t pid; 159 uint32_t priority; 160 char proc_name[(2 * MAXCOMLEN) + 1]; 161 char __pad1[3]; 162 } memorystatus_properties_freeze_entry_v1; 163 164 typedef struct memorystatus_kernel_stats { 165 uint32_t free_pages; 166 uint32_t active_pages; 167 uint32_t inactive_pages; 168 uint32_t throttled_pages; 169 uint32_t purgeable_pages; 170 uint32_t wired_pages; 171 uint32_t speculative_pages; 172 uint32_t filebacked_pages; 173 uint32_t anonymous_pages; 174 uint32_t compressor_pages; 175 uint64_t compressions; 176 uint64_t decompressions; 177 uint64_t total_uncompressed_pages_in_compressor; 178 uint64_t zone_map_size; 179 uint64_t zone_map_capacity; 180 uint64_t largest_zone_size; 181 char largest_zone_name[MACH_ZONE_NAME_MAX_LEN]; 182 } memorystatus_kernel_stats_t; 183 184 typedef enum memorystatus_freeze_skip_reason { 185 kMemorystatusFreezeSkipReasonNone = 0, 186 kMemorystatusFreezeSkipReasonExcessSharedMemory = 1, 187 kMemorystatusFreezeSkipReasonLowPrivateSharedRatio = 2, 188 kMemorystatusFreezeSkipReasonNoCompressorSpace = 3, 189 kMemorystatusFreezeSkipReasonNoSwapSpace = 4, 190 kMemorystatusFreezeSkipReasonBelowMinPages = 5, 191 kMemorystatusFreezeSkipReasonLowProbOfUse = 6, 192 kMemorystatusFreezeSkipReasonOther = 7, 193 kMemorystatusFreezeSkipReasonOutOfBudget = 8, 194 kMemorystatusFreezeSkipReasonOutOfSlots = 9, 195 kMemorystatusFreezeSkipReasonDisabled = 10, 196 kMemorystatusFreezeSkipReasonElevated = 11, 197 _kMemorystatusFreezeSkipReasonMax 198 } memorystatus_freeze_skip_reason_t; 199 /* 200 ** This is a variable-length struct. 201 ** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t * 202 */ 203 204 typedef struct jetsam_snapshot_entry { 205 pid_t pid; 206 char name[(2 * MAXCOMLEN) + 1]; 207 int32_t priority; 208 uint32_t state; 209 uint32_t fds; 210 memorystatus_freeze_skip_reason_t jse_freeze_skip_reason; /* why wasn't this process frozen? */ 211 uint8_t uuid[16]; 212 uint64_t user_data; 213 uint64_t killed; 214 uint64_t pages; 215 uint64_t max_pages_lifetime; 216 uint64_t purgeable_pages; 217 uint64_t jse_internal_pages; 218 uint64_t jse_internal_compressed_pages; 219 uint64_t jse_purgeable_nonvolatile_pages; 220 uint64_t jse_purgeable_nonvolatile_compressed_pages; 221 uint64_t jse_alternate_accounting_pages; 222 uint64_t jse_alternate_accounting_compressed_pages; 223 uint64_t jse_iokit_mapped_pages; 224 uint64_t jse_page_table_pages; 225 uint64_t jse_memory_region_count; 226 uint64_t jse_gencount; /* memorystatus_thread generation counter */ 227 uint64_t jse_starttime; /* absolute time when process starts */ 228 uint64_t jse_killtime; /* absolute time when jetsam chooses to kill a process */ 229 uint64_t jse_idle_delta; /* time spent in idle band */ 230 uint64_t jse_coalition_jetsam_id; /* we only expose coalition id for COALITION_TYPE_JETSAM */ 231 struct timeval64 cpu_time; 232 uint64_t jse_thaw_count; 233 uint64_t jse_frozen_to_swap_pages; 234 uint64_t csflags; 235 uint32_t cs_trust_level; 236 uint64_t jse_neural_nofootprint_total_pages; 237 } memorystatus_jetsam_snapshot_entry_t; 238 239 typedef struct jetsam_snapshot { 240 uint64_t snapshot_time; /* absolute time snapshot was initialized */ 241 uint64_t notification_time; /* absolute time snapshot was consumed */ 242 uint64_t js_gencount; /* memorystatus_thread generation counter */ 243 memorystatus_kernel_stats_t stats; /* system stat when snapshot is initialized */ 244 size_t entry_count; 245 memorystatus_jetsam_snapshot_entry_t entries[]; 246 } memorystatus_jetsam_snapshot_t; 247 248 /* TODO - deprecate; see <rdar://problem/12969599> */ 249 #define kMaxSnapshotEntries 192 250 251 /* 252 * default jetsam snapshot support 253 */ 254 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot; 255 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_copy; 256 #if CONFIG_FREEZE 257 extern memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_freezer; 258 extern unsigned int memorystatus_jetsam_snapshot_freezer_max; 259 extern unsigned int memorystatus_jetsam_snapshot_freezer_size; 260 #endif /* CONFIG_FREEZE */ 261 extern unsigned int memorystatus_jetsam_snapshot_count; 262 extern unsigned int memorystatus_jetsam_snapshot_copy_count; 263 extern unsigned int memorystatus_jetsam_snapshot_max; 264 extern unsigned int memorystatus_jetsam_snapshot_size; 265 extern uint64_t memorystatus_jetsam_snapshot_last_timestamp; 266 extern uint64_t memorystatus_jetsam_snapshot_timeout; 267 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries 268 #define JETSAM_SNAPSHOT_TIMEOUT_SECS 30 269 270 /* General memorystatus stuff */ 271 272 extern uint64_t memorystatus_sysprocs_idle_delay_time; 273 extern uint64_t memorystatus_apps_idle_delay_time; 274 275 /* State */ 276 #define kMemorystatusSuspended 0x01 277 #define kMemorystatusFrozen 0x02 278 #define kMemorystatusWasThawed 0x04 279 #define kMemorystatusTracked 0x08 280 #define kMemorystatusSupportsIdleExit 0x10 281 #define kMemorystatusDirty 0x20 282 #define kMemorystatusAssertion 0x40 283 284 /* 285 * Jetsam exit reason definitions - related to memorystatus 286 * 287 * When adding new exit reasons also update: 288 * JETSAM_REASON_MEMORYSTATUS_MAX 289 * kMemorystatusKilled... Cause enum 290 * memorystatus_kill_cause_name[] 291 */ 292 #define JETSAM_REASON_INVALID 0 293 #define JETSAM_REASON_GENERIC 1 294 #define JETSAM_REASON_MEMORY_HIGHWATER 2 295 #define JETSAM_REASON_VNODE 3 296 #define JETSAM_REASON_MEMORY_VMPAGESHORTAGE 4 297 #define JETSAM_REASON_MEMORY_PROCTHRASHING 5 298 #define JETSAM_REASON_MEMORY_FCTHRASHING 6 299 #define JETSAM_REASON_MEMORY_PERPROCESSLIMIT 7 300 #define JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE 8 301 #define JETSAM_REASON_MEMORY_IDLE_EXIT 9 302 #define JETSAM_REASON_ZONE_MAP_EXHAUSTION 10 303 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 11 304 #define JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE 12 305 #define JETSAM_REASON_LOWSWAP 13 306 #define JETSAM_REASON_MEMORY_SUSTAINED_PRESSURE 14 307 #define JETSAM_REASON_MEMORY_VMPAGEOUT_STARVATION 15 308 #define JETSAM_REASON_MEMORYSTATUS_MAX JETSAM_REASON_MEMORY_VMPAGEOUT_STARVATION 309 /* non-memorystatus jetsam reasons */ 310 #define JETSAM_REASON_CPULIMIT 100 311 typedef uint64_t jetsam_reason_t; 312 313 /* memorystatus kill cause */ 314 typedef enum { 315 kMemorystatusInvalid = JETSAM_REASON_INVALID, 316 kMemorystatusKilled = JETSAM_REASON_GENERIC, 317 kMemorystatusKilledHiwat = JETSAM_REASON_MEMORY_HIGHWATER, 318 kMemorystatusKilledVnodes = JETSAM_REASON_VNODE, 319 kMemorystatusKilledVMPageShortage = JETSAM_REASON_MEMORY_VMPAGESHORTAGE, 320 kMemorystatusKilledProcThrashing = JETSAM_REASON_MEMORY_PROCTHRASHING, 321 kMemorystatusKilledFCThrashing = JETSAM_REASON_MEMORY_FCTHRASHING, 322 kMemorystatusKilledPerProcessLimit = JETSAM_REASON_MEMORY_PERPROCESSLIMIT, 323 kMemorystatusKilledDiskSpaceShortage = JETSAM_REASON_MEMORY_DISK_SPACE_SHORTAGE, 324 kMemorystatusKilledIdleExit = JETSAM_REASON_MEMORY_IDLE_EXIT, 325 kMemorystatusKilledZoneMapExhaustion = JETSAM_REASON_ZONE_MAP_EXHAUSTION, 326 kMemorystatusKilledVMCompressorThrashing = JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING, 327 kMemorystatusKilledVMCompressorSpaceShortage = JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE, 328 kMemorystatusKilledLowSwap = JETSAM_REASON_LOWSWAP, 329 kMemorystatusKilledSustainedPressure = JETSAM_REASON_MEMORY_SUSTAINED_PRESSURE, 330 kMemorystatusKilledVMPageoutStarvation = JETSAM_REASON_MEMORY_VMPAGEOUT_STARVATION, 331 } memorystatus_kill_cause_t; 332 333 /* 334 * For backwards compatibility 335 * Keeping these around for external users (e.g. ReportCrash, Ariadne). 336 * TODO: Remove once they stop using these. 337 */ 338 #define kMemorystatusKilledDiagnostic kMemorystatusKilledDiskSpaceShortage 339 #define kMemorystatusKilledVMThrashing kMemorystatusKilledVMCompressorThrashing 340 #define JETSAM_REASON_MEMORY_VMTHRASHING JETSAM_REASON_MEMORY_VMCOMPRESSOR_THRASHING 341 342 /* Memorystatus control */ 343 #define MEMORYSTATUS_BUFFERSIZE_MAX 65536 344 345 #ifndef KERNEL 346 __BEGIN_DECLS 347 int memorystatus_get_level(user_addr_t level); 348 int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize); 349 __END_DECLS 350 #endif 351 352 /* Commands */ 353 #define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1 354 #define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2 355 #define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3 356 #define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4 357 #define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* Set active memory limit = inactive memory limit, both non-fatal */ 358 #define MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT 6 /* Set active memory limit = inactive memory limit, both fatal */ 359 #define MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 7 /* Set memory limits plus attributes independently */ 360 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 8 /* Get memory limits plus attributes */ 361 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE 9 /* Set the task's status as a privileged listener w.r.t memory notifications */ 362 #define MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE 10 /* Reset the task's status as a privileged listener w.r.t memory notifications */ 363 #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. */ 364 #define MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE 12 /* Disable the 'lenient' mode for aggressive jetsam. */ 365 #define MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS 13 /* Compute how much a process's phys_footprint exceeds inactive memory limit */ 366 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE 14 /* Set the inactive jetsam band for a process to JETSAM_PRIORITY_ELEVATED_INACTIVE */ 367 #define MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE 15 /* Reset the inactive jetsam band for a process to the default band (0)*/ 368 #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 */ 369 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED 17 /* Return the 'managed' status of a process */ 370 #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., 371 * if they would prefer being jetsam'ed in the idle band to being frozen in an elevated band. */ 372 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE 19 /* Return the freezable state of a process. */ 373 374 #define MEMORYSTATUS_CMD_FREEZER_CONTROL 20 375 376 #define MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE 21 /* Query if the lenient mode for aggressive jetsam is enabled. */ 377 378 #define MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT 22 /* Used by DYLD to increase the jetsam active and inactive limits, when using roots */ 379 380 #if PRIVATE 381 #define MEMORYSTATUS_CMD_SET_TESTING_PID 23 /* Used by unit tests in the development kernel only. */ 382 #endif /* PRIVATE */ 383 384 #define MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN 24 /* Check if the process is frozen. */ 385 386 #define MEMORYSTATUS_CMD_MARK_PROCESS_COALITION_SWAPPABLE 25 /* Set the coalition led by this process as swappable. This is a one-way transition. Swappable coalitions can never be made non-swappable. */ 387 #define MEMORYSTATUS_CMD_GET_PROCESS_COALITION_IS_SWAPPABLE 26 /* Get the swappable status for this process' coalition. */ 388 389 #define MEMORYSTATUS_CMD_CONVERT_MEMLIMIT_MB 28 /* Given a memlimit value (which may be 0 or -1), convert it to an actual limit in megabytes. */ 390 391 #define MEMORYSTATUS_CMD_SET_DIAG_LIMIT 30 /* Set the diagnostics memory limit */ 392 #define MEMORYSTATUS_CMD_GET_DIAG_LIMIT 31 /* Get the diagnostics memory limit */ 393 394 #define MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_NAMES 32 /* Get jetsam snapshot zprint names array */ 395 #define MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_INFO 33 /* Get jetsam snapshot zprint zone info */ 396 #define MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_MEMINFO 34 /* Get jetsam snapshot zprint wired memory info */ 397 398 /* Commands that act on a group of processes */ 399 #define MEMORYSTATUS_CMD_GRP_SET_PROPERTIES 100 400 401 #if PRIVATE 402 /* Test commands */ 403 404 /* Trigger forced jetsam */ 405 #define MEMORYSTATUS_CMD_TEST_JETSAM 1000 406 #define MEMORYSTATUS_CMD_TEST_JETSAM_SORT 1001 407 408 /* Select priority band sort order */ 409 #define JETSAM_SORT_NOSORT 0 410 #define JETSAM_SORT_DEFAULT 1 411 412 #endif /* PRIVATE */ 413 414 /* memorystatus_control() flags */ 415 416 #define MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 0x1 /* A populated snapshot buffer is returned on demand */ 417 #define MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 0x2 /* Returns a snapshot with memstats collected at boot */ 418 #define MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 0x4 /* No longer supported. Used to return the previously populated snapshot created by the system */ 419 #define MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY 0x8 /* Set jetsam priorities for a group of pids */ 420 #define MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY 0x10 /* Set probability of use for a group of processes */ 421 422 #if PRIVATE 423 #define MEMORYSTATUS_FLAGS_SET_TESTING_PID 0x20 /* Only used by xnu unit tests. */ 424 #define MEMORYSTATUS_FLAGS_UNSET_TESTING_PID 0x40 /* Only used by xnu unit tests. */ 425 #endif /* PRIVATE */ 426 427 #define MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER 0x80 /* A snapshot buffer containing app kills since last consumption */ 428 429 #define MEMORYSTATUS_FLAGS_GRP_SET_FREEZE_PRIORITY 0x100 /* Set a new ordered list of freeze candidates */ 430 #define MEMORYSTATUS_FLAGS_GRP_SET_DEMOTE_PRIORITY 0x200 /* Set a new ordered list of demote candidates */ 431 /* 432 * For use with memorystatus_control: 433 * MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 434 * 435 * A jetsam snapshot is initialized when a non-idle 436 * jetsam event occurs. The data is held in the 437 * buffer until it is reaped. This is the default 438 * behavior. 439 * 440 * Flags change the default behavior: 441 * Demand mode - this is an on_demand snapshot, 442 * meaning data is populated upon request. 443 * 444 * Boot mode - this is a snapshot of 445 * memstats collected before loading the 446 * init program. Once collected, these 447 * stats do not change. In this mode, 448 * the snapshot entry_count is always 0. 449 * 450 * Copy mode - this returns the previous snapshot 451 * collected by the system. The current snaphshot 452 * might be only half populated. 453 * 454 * Snapshots are inherently racey between request 455 * for buffer size and actual data compilation. 456 */ 457 458 /* These definitions are required for backwards compatibility */ 459 #define MEMORYSTATUS_SNAPSHOT_ON_DEMAND MEMORYSTATUS_FLAGS_SNAPSHOT_ON_DEMAND 460 #define MEMORYSTATUS_SNAPSHOT_AT_BOOT MEMORYSTATUS_FLAGS_SNAPSHOT_AT_BOOT 461 #define MEMORYSTATUS_SNAPSHOT_COPY MEMORYSTATUS_FLAGS_SNAPSHOT_COPY 462 463 /* 464 * For use with memorystatus_control: 465 * MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 466 */ 467 typedef struct memorystatus_priority_properties { 468 int32_t priority; 469 uint64_t user_data; 470 } memorystatus_priority_properties_t; 471 472 /* 473 * Inform the kernel that setting the priority property is driven by assertions. 474 */ 475 #define MEMORYSTATUS_SET_PRIORITY_ASSERTION 0x1 476 477 /* 478 * For use with memorystatus_control: 479 * MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES 480 * MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES 481 */ 482 typedef struct memorystatus_memlimit_properties { 483 int32_t memlimit_active; /* jetsam memory limit (in MB) when process is active */ 484 uint32_t memlimit_active_attr; 485 int32_t memlimit_inactive; /* jetsam memory limit (in MB) when process is inactive */ 486 uint32_t memlimit_inactive_attr; 487 } memorystatus_memlimit_properties_t; 488 489 typedef struct memorystatus_memlimit_properties2 { 490 memorystatus_memlimit_properties_t v1; 491 uint32_t memlimit_increase; /* jetsam memory limit increase (in MB) for active and inactive states */ 492 uint32_t memlimit_increase_bytes; /* bytes used to determine the jetsam memory limit increase, for active and inactive states */ 493 } memorystatus_memlimit_properties2_t; 494 495 #define MEMORYSTATUS_MEMLIMIT_ATTR_FATAL 0x1 /* if set, exceeding the memlimit is fatal */ 496 /* 497 * For use with diagnostics memorystatus_control: 498 * MEMORYSTATUS_CMD_SET_DIAG_LIMIT 499 */ 500 typedef struct memorystatus_diag_memlimit_properties { 501 uint64_t memlimit; /* max limit for memory usage before an exception is thrown expressed in bytes, but rounded internally to Mbytes */ 502 bool threshold_enabled; /* Current status of the diagnostics threshold, only for get operations, not consider in set threshold */ 503 } memorystatus_diag_memlimit_properties_t; 504 505 506 #ifdef XNU_KERNEL_PRIVATE 507 508 /* 509 * A process will be killed immediately if it crosses a memory limit marked as fatal. 510 * Fatal limit types are the 511 * - default system-wide task limit 512 * - per-task custom memory limit 513 * 514 * A process with a non-fatal memory limit can exceed that limit, but becomes an early 515 * candidate for jetsam when the device is under memory pressure. 516 * Non-fatal limit types are the 517 * - high-water-mark limit 518 * 519 * Processes that opt into dirty tracking are evaluated 520 * based on clean vs dirty state. 521 * dirty ==> active 522 * clean ==> inactive 523 * 524 * Processes that do not opt into dirty tracking are 525 * evalulated based on priority level. 526 * Foreground or above ==> active 527 * Below Foreground ==> inactive 528 */ 529 530 /* 531 * p_memstat_state flag holds 532 * - in kernel process state and memlimit state 533 */ 534 535 #define P_MEMSTAT_SUSPENDED 0x00000001 /* Process is suspended and likely in the IDLE band */ 536 #define P_MEMSTAT_FROZEN 0x00000002 /* Process has some state on disk. It should be suspended */ 537 #define P_MEMSTAT_FREEZE_DISABLED 0x00000004 /* Process isn't freeze-eligible and will not be frozen */ 538 #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 */ 539 #define P_MEMSTAT_LOCKED 0x00000010 /* Process is being actively worked on behind the proc_list_lock */ 540 #define P_MEMSTAT_TERMINATED 0x00000020 /* Process is exiting */ 541 #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 */ 542 #define P_MEMSTAT_PRIORITYUPDATED 0x00000080 /* Process had its jetsam priority updated */ 543 #define P_MEMSTAT_FOREGROUND 0x00000100 /* Process is in the FG jetsam band...unused??? */ 544 #define P_MEMSTAT_REFREEZE_ELIGIBLE 0x00000400 /* Process was once thawed i.e. its state was brought back from disk. It is now refreeze eligible.*/ 545 #define P_MEMSTAT_MANAGED 0x00000800 /* Process is managed by RunningBoard i.e. is either application or extension */ 546 #define P_MEMSTAT_INTERNAL 0x00001000 /* Process is a system-critical-not-be-jetsammed process i.e. launchd */ 547 #define P_MEMSTAT_FATAL_MEMLIMIT 0x00002000 /* current fatal state of the process's memlimit */ 548 #define P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL 0x00004000 /* if set, exceeding limit is fatal when the process is active */ 549 #define P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL 0x00008000 /* if set, exceeding limit is fatal when the process is inactive */ 550 #define P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND 0x00010000 /* if set, the process will go into this band & stay there when in the background instead 551 * of the aging bands and/or the IDLE band. */ 552 #define P_MEMSTAT_PRIORITY_ASSERTION 0x00020000 /* jetsam priority is being driven by an assertion */ 553 #define P_MEMSTAT_FREEZE_CONSIDERED 0x00040000 /* This process has been considered for the freezer. */ 554 #define P_MEMSTAT_SKIP 0x00080000 /* Process is temporarily ineligible for memory pressure kills. Used only on development & debug kernels to make corpses of buggy processes */ 555 #define P_MEMSTAT_FROZEN_XPC_SERVICE 0x00100000 /* Process is an XPC service. Only used for freezer telemetry. */ 556 #define P_MEMSTAT_FROZEN_FOCAL_THAW 0x00200000 /* Process has been thawed while focal in the current freezer interval. Only used for freezer telemetry. */ 557 558 /* 559 * p_memstat_relaunch_flags holds 560 * - relaunch behavior when jetsammed 561 */ 562 #define P_MEMSTAT_RELAUNCH_UNKNOWN 0x0 563 #define P_MEMSTAT_RELAUNCH_LOW 0x1 564 #define P_MEMSTAT_RELAUNCH_MED 0x2 565 #define P_MEMSTAT_RELAUNCH_HIGH 0x4 566 567 /* 568 * Checking the p_memstat_state almost always requires the proc_list_lock 569 * because the jetsam thread could be on the other core changing the state. 570 * 571 * App -- almost always managed by a system process. Always have dirty tracking OFF. Can include extensions too. 572 * System Processes -- not managed by anybody. Always have dirty tracking ON. Can include extensions (here) too. 573 */ 574 #define isApp(p) ((p->p_memstat_state & P_MEMSTAT_MANAGED) || ! (p->p_memstat_dirty & P_DIRTY_TRACK)) 575 #define isSysProc(p) ( ! (p->p_memstat_state & P_MEMSTAT_MANAGED) || (p->p_memstat_dirty & P_DIRTY_TRACK)) 576 577 #define MEMSTAT_BUCKET_COUNT (JETSAM_PRIORITY_MAX + 1) 578 579 typedef struct memstat_bucket { 580 TAILQ_HEAD(, proc) list; 581 uint32_t count; 582 uint32_t relaunch_high_count; 583 } memstat_bucket_t; 584 585 extern memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT]; 586 587 /* 588 * Table that expresses the probability of a process 589 * being used in the next hour. 590 */ 591 typedef struct memorystatus_internal_probabilities { 592 char proc_name[MAXCOMLEN + 1]; 593 int use_probability; 594 } memorystatus_internal_probabilities_t; 595 596 extern memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table; 597 extern size_t memorystatus_global_probabilities_size; 598 599 extern void memorystatus_post_snapshot(void); 600 601 extern void memorystatus_init(void); 602 603 extern void memorystatus_init_at_boot_snapshot(void); 604 605 extern int memorystatus_add(proc_t p, boolean_t locked); 606 607 __options_closed_decl(memstat_priority_options_t, uint8_t, { 608 MEMSTAT_PRIORITY_OPTIONS_NONE = 0x00, 609 /* Priority is driven by a RB assertion */ 610 MEMSTAT_PRIORITY_IS_ASSERTION = 0x01, 611 MEMSTAT_PRIORITY_IS_EFFECTIVE = 0x02, 612 /* Insert at the head of the corresponding band */ 613 MEMSTAT_PRIORITY_INSERT_HEAD = 0x04, 614 /* Do not consider aging the process (used to send directly to IDLE) */ 615 MEMSTAT_PRIORITY_NO_AGING = 0x08, 616 }); 617 618 extern int memorystatus_set_priority(proc_t p, int priority, uint64_t user_data, 619 memstat_priority_options_t options); 620 621 __options_decl(memlimit_options_t, uint8_t, { 622 MEMLIMIT_OPTIONS_NONE = 0x00, 623 MEMLIMIT_ACTIVE_FATAL = 0x01, 624 MEMLIMIT_INACTIVE_FATAL = 0x02, 625 }); 626 extern int memorystatus_set_memlimits(proc_t p, int32_t active_limit, int32_t inactive_limit, memlimit_options_t options); 627 628 /* Remove this process from jetsam bands for killing or freezing. 629 * The proc_list_lock is held by the caller. 630 * @param p: The process to remove. 631 * @return: 0 if successful. EAGAIN if the process can't be removed right now (because it's being frozen) or ESRCH. 632 */ 633 extern int memorystatus_remove(proc_t p); 634 635 int memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t opflags, int priority, boolean_t effective_now); 636 int memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags); 637 638 extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol); 639 extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol); 640 extern int memorystatus_dirty_get(proc_t p, boolean_t locked); 641 extern int memorystatus_dirty_clear(proc_t p, uint32_t pcontrol); 642 643 extern int memorystatus_on_terminate(proc_t p); 644 645 extern void memorystatus_on_suspend(proc_t p); 646 extern void memorystatus_on_resume(proc_t p); 647 extern void memorystatus_on_inactivity(proc_t p); 648 649 extern void memorystatus_on_pageout_scan_end(void); 650 651 /* Memorystatus kevent */ 652 653 void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr); 654 655 int memorystatus_knote_register(struct knote *kn); 656 void memorystatus_knote_unregister(struct knote *kn); 657 658 #if CONFIG_MEMORYSTATUS 659 void memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); 660 void memorystatus_log_diag_threshold_exception(const int diag_threshold_value); 661 void memorystatus_on_ledger_footprint_exceeded(int warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal); 662 void proc_memstat_skip(proc_t p, boolean_t set); 663 void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty, boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit); 664 665 #if __arm64__ 666 void memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase); 667 void memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p); 668 void memorystatus_act_on_entitled_task_limit(proc_t p); 669 void memorystatus_act_on_entitled_developer_task_limit(proc_t p); 670 #endif /* __arm64__ */ 671 672 #endif /* CONFIG_MEMORYSTATUS */ 673 674 int memorystatus_get_pressure_status_kdp(void); 675 int memorystatus_get_proccnt_upto_priority(int32_t max_bucket_index); 676 677 #if CONFIG_JETSAM 678 679 typedef enum memorystatus_policy { 680 kPolicyDefault = 0x0, 681 kPolicyMoreFree = 0x1, 682 } memorystatus_policy_t; 683 extern unsigned int memorystatus_swap_all_apps; 684 685 /* 686 * Synchronous memorystatus kill calls. 687 */ 688 689 boolean_t memorystatus_kill_on_VM_page_shortage(void); 690 boolean_t memorystatus_kill_on_vnode_limit(void); 691 boolean_t memorystatus_kill_on_sustained_pressure(void); 692 693 /* 694 * Wake up the memorystatus thread so it can do async kills. 695 * The memorystatus thread will keep killing until the system is 696 * considered healthy. 697 */ 698 void memorystatus_thread_wake(void); 699 700 /* 701 * Attempt to kill the specified pid with the given reason. 702 * Consumes a reference on the jetsam_reason. 703 */ 704 boolean_t memorystatus_kill_with_jetsam_reason_sync(pid_t pid, os_reason_t jetsam_reason); 705 706 void jetsam_on_ledger_cpulimit_exceeded(void); 707 void memorystatus_fast_jetsam_override(boolean_t enable_override); 708 /* 709 * Disable memorystatus_swap_all_apps. 710 * Used by vm_pageout at boot if the swap volume is too small to support app swap. 711 * Returns true iff app swap is now disabled. 712 * On development or debug kernels, app swap can be enabled via a boot-arg and in 713 * that case can not be disabled. 714 */ 715 bool memorystatus_disable_swap(void); 716 717 #endif /* CONFIG_JETSAM */ 718 719 boolean_t memorystatus_kill_on_zone_map_exhaustion(pid_t pid); 720 boolean_t memorystatus_kill_on_VM_compressor_space_shortage(boolean_t async); 721 void memorystatus_kill_on_vps_starvation(void); 722 void memorystatus_pages_update(unsigned int pages_avail); 723 boolean_t memorystatus_idle_exit_from_VM(void); 724 proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search); 725 proc_t memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search); 726 void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages); 727 728 bool memorystatus_task_has_increased_memory_limit_entitlement(task_t task); 729 bool memorystatus_task_has_increased_debugging_memory_limit_entitlement(task_t task); 730 bool memorystatus_task_has_legacy_footprint_entitlement(task_t task); 731 bool memorystatus_task_has_ios13extended_footprint_limit(task_t task); 732 733 #if VM_PRESSURE_EVENTS 734 735 extern kern_return_t memorystatus_update_vm_pressure(boolean_t); 736 737 #if CONFIG_MEMORYSTATUS 738 /* Flags */ 739 extern int memorystatus_low_mem_privileged_listener(uint32_t op_flags); 740 extern int memorystatus_send_pressure_note(int pid); 741 extern boolean_t memorystatus_is_foreground_locked(proc_t p); 742 extern boolean_t memorystatus_bg_pressure_eligible(proc_t p); 743 #endif /* CONFIG_MEMORYSTATUS */ 744 745 #endif /* VM_PRESSURE_EVENTS */ 746 747 #endif /* XNU_KERNEL_PRIVATE */ 748 749 #endif /* SYS_MEMORYSTATUS_H */ 750