1/* 2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29#ifndef SYS_MEMORYSTATUS_H 30#define SYS_MEMORYSTATUS_H 31 32#include <stdint.h> 33#include <sys/time.h> 34#include <sys/proc.h> 35#include <sys/param.h> 36 37#define JETSAM_PRIORITY_REVISION 2 38 39#define JETSAM_PRIORITY_IDLE 0 40#define JETSAM_PRIORITY_IDLE_DEFERRED 1 41#define JETSAM_PRIORITY_BACKGROUND_OPPORTUNISTIC 2 42#define JETSAM_PRIORITY_BACKGROUND 3 43#define JETSAM_PRIORITY_MAIL 4 44#define JETSAM_PRIORITY_PHONE 5 45#define JETSAM_PRIORITY_UI_SUPPORT 8 46#define JETSAM_PRIORITY_FOREGROUND_SUPPORT 9 47#define JETSAM_PRIORITY_FOREGROUND 10 48#define JETSAM_PRIORITY_AUDIO_AND_ACCESSORY 12 49#define JETSAM_PRIORITY_CONDUCTOR 13 50#define JETSAM_PRIORITY_HOME 16 51#define JETSAM_PRIORITY_EXECUTIVE 17 52#define JETSAM_PRIORITY_IMPORTANT 18 53#define JETSAM_PRIORITY_CRITICAL 19 54 55#define JETSAM_PRIORITY_MAX 21 56 57/* TODO - tune. This should probably be lower priority */ 58#define JETSAM_PRIORITY_DEFAULT 18 59#define JETSAM_PRIORITY_TELEPHONY 19 60 61/* Compatibility */ 62#define DEFAULT_JETSAM_PRIORITY 18 63 64#define DEFERRED_IDLE_EXIT_TIME_SECS 10 65 66#define KEV_MEMORYSTATUS_SUBCLASS 3 67 68enum { 69 kMemorystatusLevelNote = 1, 70 kMemorystatusSnapshotNote = 2, 71 kMemorystatusFreezeNote = 3, 72 kMemorystatusPressureNote = 4 73}; 74 75enum { 76 kMemorystatusLevelAny = -1, 77 kMemorystatusLevelNormal = 0, 78 kMemorystatusLevelWarning = 1, 79 kMemorystatusLevelUrgent = 2, 80 kMemorystatusLevelCritical = 3 81}; 82 83typedef struct memorystatus_priority_entry { 84 pid_t pid; 85 int32_t priority; 86 uint64_t user_data; 87 int32_t limit; 88 uint32_t state; 89} memorystatus_priority_entry_t; 90 91typedef struct memorystatus_kernel_stats { 92 uint32_t free_pages; 93 uint32_t active_pages; 94 uint32_t inactive_pages; 95 uint32_t throttled_pages; 96 uint32_t purgeable_pages; 97 uint32_t wired_pages; 98 uint32_t speculative_pages; 99 uint32_t filebacked_pages; 100 uint32_t anonymous_pages; 101 uint32_t compressor_pages; 102 uint64_t compressions; 103 uint64_t decompressions; 104 uint64_t total_uncompressed_pages_in_compressor; 105} memorystatus_kernel_stats_t; 106 107/* 108** This is a variable-length struct. 109** Allocate a buffer of the size returned by the sysctl, cast to a memorystatus_snapshot_t * 110*/ 111 112typedef struct jetsam_snapshot_entry { 113 pid_t pid; 114 char name[MAXCOMLEN+1]; 115 int32_t priority; 116 uint32_t pages; 117 uint32_t max_pages; 118 uint32_t state; 119 uint32_t killed; 120 uint64_t user_data; 121 uint8_t uuid[16]; 122 uint32_t fds; 123} memorystatus_jetsam_snapshot_entry_t; 124 125typedef struct jetsam_snapshot { 126 uint64_t snapshot_time; 127 uint64_t notification_time; 128 memorystatus_kernel_stats_t stats; 129 size_t entry_count; 130 memorystatus_jetsam_snapshot_entry_t entries[]; 131} memorystatus_jetsam_snapshot_t; 132 133typedef struct memorystatus_freeze_entry { 134 int32_t pid; 135 uint32_t flags; 136 uint32_t pages; 137} memorystatus_freeze_entry_t; 138 139/* TODO - deprecate; see <rdar://problem/12969599> */ 140#define kMaxSnapshotEntries 192 141 142/* State */ 143#define kMemorystatusSuspended 0x01 144#define kMemorystatusFrozen 0x02 145#define kMemorystatusWasThawed 0x04 146#define kMemorystatusTracked 0x08 147#define kMemorystatusSupportsIdleExit 0x10 148#define kMemorystatusDirty 0x20 149 150/* Cause */ 151enum { 152 kMemorystatusKilled = 1, 153 kMemorystatusKilledHiwat, 154 kMemorystatusKilledVnodes, 155 kMemorystatusKilledVMPageShortage, 156 kMemorystatusKilledVMThrashing, 157 kMemorystatusKilledPerProcessLimit, 158 kMemorystatusKilledDiagnostic, 159 kMemorystatusKilledIdleExit 160}; 161 162/* Temporary, to prevent the need for a linked submission of ReportCrash */ 163/* Remove when <rdar://problem/13210532> has been integrated */ 164enum { 165 kMemorystatusKilledVM = kMemorystatusKilledVMPageShortage 166}; 167 168/* Memorystatus control */ 169#define MEMORYSTATUS_BUFFERSIZE_MAX 65536 170 171#ifndef KERNEL 172int memorystatus_get_level(user_addr_t level); 173int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *buffer, size_t buffersize); 174#endif 175 176/* Commands */ 177#define MEMORYSTATUS_CMD_GET_PRIORITY_LIST 1 178#define MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES 2 179#define MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT 3 180#define MEMORYSTATUS_CMD_GET_PRESSURE_STATUS 4 181#define MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK 5 /* TODO: deprecate */ 182 183#if PRIVATE 184/* Test commands */ 185 186/* Trigger forced jetsam */ 187#define MEMORYSTATUS_CMD_TEST_JETSAM 1000 188 189/* Panic on jetsam options */ 190typedef struct memorystatus_jetsam_panic_options { 191 uint32_t data; 192 uint32_t mask; 193} memorystatus_jetsam_panic_options_t; 194 195#define MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS 1001 196#endif /* PRIVATE */ 197 198typedef struct memorystatus_priority_properties { 199 int32_t priority; 200 uint64_t user_data; 201} memorystatus_priority_properties_t; 202 203#ifdef XNU_KERNEL_PRIVATE 204 205/* p_memstat_state flags */ 206 207#define P_MEMSTAT_SUSPENDED 0x00000001 208#define P_MEMSTAT_FROZEN 0x00000002 209#define P_MEMSTAT_NORECLAIM 0x00000004 210#define P_MEMSTAT_ERROR 0x00000008 211#define P_MEMSTAT_LOCKED 0x00000010 212#define P_MEMSTAT_TERMINATED 0x00000020 213#define P_MEMSTAT_NOTFIED 0x00000040 214#define P_MEMSTAT_PRIORITYUPDATED 0x00000080 215#define P_MEMSTAT_FOREGROUND 0x00000100 216#define P_MEMSTAT_DIAG_SUSPENDED 0x00000200 217#define P_MEMSTAT_PRIOR_THAW 0x00000400 218#define P_MEMSTAT_MEMLIMIT_BACKGROUND 0x00000800 219#define P_MEMSTAT_INTERNAL 0x00001000 220 221extern void memorystatus_init(void) __attribute__((section("__TEXT, initcode"))); 222 223extern int memorystatus_add(proc_t p, boolean_t locked); 224extern int memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, boolean_t update_memlimit, int32_t memlimit, boolean_t memlimit_background); 225extern int memorystatus_remove(proc_t p, boolean_t locked); 226 227extern int memorystatus_dirty_track(proc_t p, uint32_t pcontrol); 228extern int memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol); 229extern int memorystatus_dirty_get(proc_t p); 230 231extern int memorystatus_on_terminate(proc_t p); 232 233extern void memorystatus_on_suspend(proc_t p); 234extern void memorystatus_on_resume(proc_t p); 235extern void memorystatus_on_inactivity(proc_t p); 236 237extern void memorystatus_on_pageout_scan_end(void); 238 239/* Memorystatus kevent */ 240 241void memorystatus_kevent_init(lck_grp_t *grp, lck_attr_t *attr); 242 243int memorystatus_knote_register(struct knote *kn); 244void memorystatus_knote_unregister(struct knote *kn); 245 246#if CONFIG_JETSAM 247 248typedef enum memorystatus_policy { 249 kPolicyDefault = 0x0, 250 kPolicyMoreFree = 0x1, 251 kPolicyDiagnoseAll = 0x2, 252 kPolicyDiagnoseFirst = 0x4, 253 kPolicyDiagnoseActive = (kPolicyDiagnoseAll | kPolicyDiagnoseFirst), 254} memorystatus_policy_t; 255 256extern int memorystatus_jetsam_wakeup; 257extern unsigned int memorystatus_jetsam_running; 258 259boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async); 260boolean_t memorystatus_kill_on_VM_thrashing(boolean_t async); 261boolean_t memorystatus_kill_on_vnode_limit(void); 262 263void memorystatus_on_ledger_footprint_exceeded(int warning, const int max_footprint_mb); 264 265void memorystatus_pages_update(unsigned int pages_avail); 266 267extern boolean_t memorystatus_is_foreground_locked(proc_t p); 268 269#else /* CONFIG_JETSAM */ 270 271boolean_t memorystatus_idle_exit_from_VM(void); 272 273#endif /* !CONFIG_JETSAM */ 274 275#ifdef CONFIG_FREEZE 276 277#define FREEZE_PAGES_MIN ( 1 * 1024 * 1024 / PAGE_SIZE) 278#define FREEZE_PAGES_MAX (16 * 1024 * 1024 / PAGE_SIZE) 279 280#define FREEZE_SUSPENDED_THRESHOLD_LOW 2 281#define FREEZE_SUSPENDED_THRESHOLD_DEFAULT 4 282 283#define FREEZE_DAILY_MB_MAX 1024 284#define FREEZE_DAILY_PAGEOUTS_MAX (FREEZE_DAILY_MB_MAX * (1024 * 1024 / PAGE_SIZE)) 285 286typedef struct throttle_interval_t { 287 uint32_t mins; 288 uint32_t burst_multiple; 289 uint32_t pageouts; 290 uint32_t max_pageouts; 291 mach_timespec_t ts; 292 boolean_t throttle; 293} throttle_interval_t; 294 295extern boolean_t memorystatus_freeze_enabled; 296extern int memorystatus_freeze_wakeup; 297 298extern void memorystatus_freeze_init(void) __attribute__((section("__TEXT, initcode"))); 299 300#endif /* CONFIG_FREEZE */ 301 302#if VM_PRESSURE_EVENTS 303 304#define MEMORYSTATUS_SUSPENDED_THRESHOLD 4 305 306extern kern_return_t memorystatus_update_vm_pressure(boolean_t); 307 308#if CONFIG_JETSAM 309extern int memorystatus_send_pressure_note(int pid); 310extern boolean_t memorystatus_bg_pressure_eligible(proc_t p); 311#endif 312 313#endif /* VM_PRESSURE_EVENTS */ 314 315#endif /* XNU_KERNEL_PRIVATE */ 316 317#endif /* SYS_MEMORYSTATUS_H */ 318