| 1 |
/** |
| 2 |
* Contains OS-level routines needed by the garbage collector. |
| 3 |
* |
| 4 |
* Copyright: D Language Foundation 2005 - 2021. |
| 5 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 6 |
* Authors: Walter Bright, David Friedman, Sean Kelly, Leandro Lucarella |
| 7 |
*/ |
| 8 |
module core.internal.gc.os; |
| 9 |
|
| 10 |
|
| 11 |
version (Windows) |
| 12 |
{ |
| 13 |
import core.sys.windows.winbase : GetCurrentThreadId, VirtualAlloc, VirtualFree; |
| 14 |
import core.sys.windows.winnt : MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE; |
| 15 |
|
| 16 |
alias int pthread_t; |
| 17 |
|
| 18 |
pthread_t pthread_self() nothrow |
| 19 |
{ |
| 20 |
return cast(pthread_t) GetCurrentThreadId(); |
| 21 |
} |
| 22 |
|
| 23 |
//version = GC_Use_Alloc_Win32; |
| 24 |
} |
| 25 |
else version (Posix) |
| 26 |
{ |
| 27 |
version (OSX) |
| 28 |
version = Darwin; |
| 29 |
else version (iOS) |
| 30 |
version = Darwin; |
| 31 |
else version (TVOS) |
| 32 |
version = Darwin; |
| 33 |
else version (WatchOS) |
| 34 |
version = Darwin; |
| 35 |
|
| 36 |
public import core.sys.posix.unistd : fork, pid_t; |
| 37 |
import core.stdc.errno : ECHILD, EINTR, errno; |
| 38 |
import core.sys.posix.sys.mman : MAP_ANON, MAP_FAILED, MAP_PRIVATE, MAP_SHARED, mmap, munmap, PROT_READ, PROT_WRITE; |
| 39 |
import core.sys.posix.sys.wait : waitpid, WNOHANG; |
| 40 |
|
| 41 |
|
| 42 |
/// Possible results for the wait_pid() function. |
| 43 |
enum ChildStatus |
| 44 |
{ |
| 45 |
done, /// The process has finished successfully |
| 46 |
running, /// The process is still running |
| 47 |
error /// There was an error waiting for the process |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Wait for a process with PID pid to finish. |
| 52 |
* |
| 53 |
* If block is false, this function will not block, and return ChildStatus.running if |
| 54 |
* the process is still running. Otherwise it will return always ChildStatus.done |
| 55 |
* (unless there is an error, in which case ChildStatus.error is returned). |
| 56 |
*/ |
| 57 |
ChildStatus wait_pid(pid_t pid, bool block = true) nothrow @nogc |
| 58 |
{ |
| 59 |
import core.exception : onForkError; |
| 60 |
|
| 61 |
int status = void; |
| 62 |
pid_t waited_pid = void; |
| 63 |
// In the case where we are blocking, we need to consider signals |
| 64 |
// arriving while we wait, and resume the waiting if EINTR is returned |
| 65 |
do { |
| 66 |
errno = 0; |
| 67 |
waited_pid = waitpid(pid, &status, block ? 0 : WNOHANG); |
| 68 |
} |
| 69 |
while (waited_pid == -1 && errno == EINTR); |
| 70 |
if (waited_pid == 0) |
| 71 |
return ChildStatus.running; |
| 72 |
if (errno == ECHILD) |
| 73 |
return ChildStatus.done; // someone called posix.syswait |
| 74 |
if (waited_pid != pid || status != 0) |
| 75 |
onForkError(); |
| 76 |
return ChildStatus.done; |
| 77 |
} |
| 78 |
|
| 79 |
//version = GC_Use_Alloc_MMap; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
import core.stdc.stdlib : free, malloc; |
| 84 |
|
| 85 |
//version = GC_Use_Alloc_Malloc; |
| 86 |
} |
| 87 |
|
| 88 |
/+ |
| 89 |
static if (is(typeof(VirtualAlloc))) |
| 90 |
version = GC_Use_Alloc_Win32; |
| 91 |
else static if (is(typeof(mmap))) |
| 92 |
version = GC_Use_Alloc_MMap; |
| 93 |
else static if (is(typeof(valloc))) |
| 94 |
version = GC_Use_Alloc_Valloc; |
| 95 |
else static if (is(typeof(malloc))) |
| 96 |
version = GC_Use_Alloc_Malloc; |
| 97 |
else static assert(false, "No supported allocation methods available."); |
| 98 |
+/ |
| 99 |
|
| 100 |
static if (is(typeof(VirtualAlloc))) // version (GC_Use_Alloc_Win32) |
| 101 |
{ |
| 102 |
/** |
| 103 |
* Indicates if an implementation supports fork(). |
| 104 |
* |
| 105 |
* The value shown here is just demostrative, the real value is defined based |
| 106 |
* on the OS it's being compiled in. |
| 107 |
* enum HaveFork = true; |
| 108 |
*/ |
| 109 |
enum HaveFork = false; |
| 110 |
|
| 111 |
/** |
| 112 |
* Map memory. |
| 113 |
*/ |
| 114 |
void *os_mem_map(size_t nbytes) nothrow @nogc |
| 115 |
{ |
| 116 |
return VirtualAlloc(null, nbytes, MEM_RESERVE | MEM_COMMIT, |
| 117 |
PAGE_READWRITE); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Unmap memory allocated with os_mem_map(). |
| 123 |
* Returns: |
| 124 |
* 0 success |
| 125 |
* !=0 failure |
| 126 |
*/ |
| 127 |
int os_mem_unmap(void *base, size_t nbytes) nothrow @nogc |
| 128 |
{ |
| 129 |
return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0); |
| 130 |
} |
| 131 |
} |
| 132 |
else static if (is(typeof(mmap))) // else version (GC_Use_Alloc_MMap) |
| 133 |
{ |
| 134 |
enum HaveFork = true; |
| 135 |
|
| 136 |
void *os_mem_map(size_t nbytes, bool share = false) nothrow @nogc |
| 137 |
{ void *p; |
| 138 |
|
| 139 |
auto map_f = share ? MAP_SHARED : MAP_PRIVATE; |
| 140 |
p = mmap(null, nbytes, PROT_READ | PROT_WRITE, map_f | MAP_ANON, -1, 0); |
| 141 |
return (p == MAP_FAILED) ? null : p; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
int os_mem_unmap(void *base, size_t nbytes) nothrow @nogc |
| 146 |
{ |
| 147 |
return munmap(base, nbytes); |
| 148 |
} |
| 149 |
} |
| 150 |
else static if (is(typeof(valloc))) // else version (GC_Use_Alloc_Valloc) |
| 151 |
{ |
| 152 |
enum HaveFork = false; |
| 153 |
|
| 154 |
void *os_mem_map(size_t nbytes) nothrow @nogc |
| 155 |
{ |
| 156 |
return valloc(nbytes); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
int os_mem_unmap(void *base, size_t nbytes) nothrow @nogc |
| 161 |
{ |
| 162 |
free(base); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
} |
| 166 |
else static if (is(typeof(malloc))) // else version (GC_Use_Alloc_Malloc) |
| 167 |
{ |
| 168 |
// NOTE: This assumes malloc granularity is at least (void*).sizeof. If |
| 169 |
// (req_size + PAGESIZE) is allocated, and the pointer is rounded up |
| 170 |
// to PAGESIZE alignment, there will be space for a void* at the end |
| 171 |
// after PAGESIZE bytes used by the GC. |
| 172 |
|
| 173 |
enum HaveFork = false; |
| 174 |
|
| 175 |
import core.internal.gc.impl.conservative.gc; |
| 176 |
|
| 177 |
|
| 178 |
const size_t PAGE_MASK = PAGESIZE - 1; |
| 179 |
|
| 180 |
|
| 181 |
void *os_mem_map(size_t nbytes) nothrow @nogc |
| 182 |
{ byte *p, q; |
| 183 |
p = cast(byte *) malloc(nbytes + PAGESIZE); |
| 184 |
if (!p) |
| 185 |
return null; |
| 186 |
q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK); |
| 187 |
* cast(void**)(q + nbytes) = p; |
| 188 |
return q; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
int os_mem_unmap(void *base, size_t nbytes) nothrow @nogc |
| 193 |
{ |
| 194 |
free( *cast(void**)( cast(byte*) base + nbytes ) ); |
| 195 |
return 0; |
| 196 |
} |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
static assert(false, "No supported allocation methods available."); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
Check for any kind of memory pressure. |
| 205 |
|
| 206 |
Params: |
| 207 |
mapped = the amount of memory mapped by the GC in bytes |
| 208 |
Returns: |
| 209 |
true if memory is scarce |
| 210 |
*/ |
| 211 |
// TODO: get virtual mem sizes and current usage from OS |
| 212 |
// TODO: compare current RSS and avail. physical memory |
| 213 |
bool isLowOnMem(size_t mapped) nothrow @nogc |
| 214 |
{ |
| 215 |
version (Windows) |
| 216 |
{ |
| 217 |
import core.sys.windows.winbase : GlobalMemoryStatusEx, MEMORYSTATUSEX; |
| 218 |
|
| 219 |
MEMORYSTATUSEX stat; |
| 220 |
stat.dwLength = stat.sizeof; |
| 221 |
const success = GlobalMemoryStatusEx(&stat) != 0; |
| 222 |
assert(success, "GlobalMemoryStatusEx() failed"); |
| 223 |
if (!success) |
| 224 |
return false; |
| 225 |
|
| 226 |
// dwMemoryLoad is the 'approximate percentage of physical memory that is in use' |
| 227 |
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex |
| 228 |
const percentPhysicalRAM = stat.ullTotalPhys / 100; |
| 229 |
return (stat.dwMemoryLoad >= 95 && mapped > percentPhysicalRAM) |
| 230 |
|| (stat.dwMemoryLoad >= 90 && mapped > 10 * percentPhysicalRAM); |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
enum GB = 2 ^^ 30; |
| 235 |
version (D_LP64) |
| 236 |
return false; |
| 237 |
else version (Darwin) |
| 238 |
{ |
| 239 |
// 80 % of available 4GB is used for GC (excluding malloc and mmap) |
| 240 |
enum size_t limit = 4UL * GB * 8 / 10; |
| 241 |
return mapped > limit; |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
// be conservative and assume 3GB |
| 246 |
enum size_t limit = 3UL * GB * 8 / 10; |
| 247 |
return mapped > limit; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
Get the size of available physical memory |
| 254 |
|
| 255 |
Params: |
| 256 |
avail = if supported on the current platform, return the currently unused memory |
| 257 |
rather than the installed physical memory |
| 258 |
Returns: |
| 259 |
size of installed physical RAM |
| 260 |
*/ |
| 261 |
version (Windows) |
| 262 |
{ |
| 263 |
ulong os_physical_mem(bool avail) nothrow @nogc |
| 264 |
{ |
| 265 |
import core.sys.windows.winbase : GlobalMemoryStatus, MEMORYSTATUS; |
| 266 |
MEMORYSTATUS stat; |
| 267 |
GlobalMemoryStatus(&stat); |
| 268 |
return avail ? stat.dwAvailPhys : stat.dwTotalPhys; // limited to 4GB for Win32 |
| 269 |
} |
| 270 |
} |
| 271 |
else version (Darwin) |
| 272 |
{ |
| 273 |
extern (C) int sysctl(const int* name, uint namelen, void* oldp, size_t* oldlenp, const void* newp, size_t newlen) @nogc nothrow; |
| 274 |
ulong os_physical_mem(bool avail) nothrow @nogc |
| 275 |
{ |
| 276 |
enum |
| 277 |
{ |
| 278 |
CTL_HW = 6, |
| 279 |
HW_MEMSIZE = 24, |
| 280 |
} |
| 281 |
int[2] mib = [ CTL_HW, HW_MEMSIZE ]; |
| 282 |
ulong system_memory_bytes; |
| 283 |
size_t len = system_memory_bytes.sizeof; |
| 284 |
if (sysctl(mib.ptr, 2, &system_memory_bytes, &len, null, 0) != 0) |
| 285 |
return 0; |
| 286 |
return system_memory_bytes; |
| 287 |
} |
| 288 |
} |
| 289 |
else version (Posix) |
| 290 |
{ |
| 291 |
ulong os_physical_mem(bool avail) nothrow @nogc |
| 292 |
{ |
| 293 |
static import core.sys.posix.unistd; |
| 294 |
import core.sys.posix.unistd : _SC_PAGESIZE, _SC_PHYS_PAGES, sysconf; |
| 295 |
const pageSize = sysconf(_SC_PAGESIZE); |
| 296 |
static if (__traits(compiles, core.sys.posix.unistd._SC_AVPHYS_PAGES)) // not available on all platforms |
| 297 |
{ |
| 298 |
import core.sys.posix.unistd : _SC_AVPHYS_PAGES; |
| 299 |
const sc = avail ? _SC_AVPHYS_PAGES : _SC_PHYS_PAGES; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
const sc = _SC_PHYS_PAGES; |
| 304 |
} |
| 305 |
const pages = sysconf(sc); |
| 306 |
return pageSize * pages; |
| 307 |
} |
| 308 |
} |