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