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