1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Lower priority of POSIX_MADV_DONTNEED. (#6671)

This commit is contained in:
Samuel Williams 2022-11-04 20:13:48 +13:00 committed by GitHub
parent ccf32a5ca4
commit 2bb89b7f11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2022-11-04 07:14:07 +00:00
Merged-By: ioquatix <samuel@codeotaku.com>

21
cont.c
View file

@ -693,21 +693,36 @@ fiber_pool_stack_free(struct fiber_pool_stack * stack)
if (DEBUG) fprintf(stderr, "fiber_pool_stack_free: %p+%"PRIuSIZE" [base=%p, size=%"PRIuSIZE"]\n", base, size, stack->base, stack->size); if (DEBUG) fprintf(stderr, "fiber_pool_stack_free: %p+%"PRIuSIZE" [base=%p, size=%"PRIuSIZE"]\n", base, size, stack->base, stack->size);
#if VM_CHECK_MODE > 0 && defined(MADV_DONTNEED) // The pages being used by the stack can be returned back to the system.
// That doesn't change the page mapping, but it does allow the system to
// reclaim the physical memory.
// Since we no longer care about the data itself, we don't need to page
// out to disk, since that is costly. Not all systems support that, so
// we try our best to select the most efficient implementation.
// In addition, it's actually slightly desirable to not do anything here,
// but that results in higher memory usage.
#ifdef __wasi__
// WebAssembly doesn't support madvise, so we just don't do anything.
#elif VM_CHECK_MODE > 0 && defined(MADV_DONTNEED)
// This immediately discards the pages and the memory is reset to zero. // This immediately discards the pages and the memory is reset to zero.
madvise(base, size, MADV_DONTNEED); madvise(base, size, MADV_DONTNEED);
#elif defined(POSIX_MADV_DONTNEED)
posix_madvise(base, size, POSIX_MADV_DONTNEED);
#elif defined(MADV_FREE_REUSABLE) #elif defined(MADV_FREE_REUSABLE)
// Darwin / macOS / iOS.
// Acknowledge the kernel down to the task info api we make this // Acknowledge the kernel down to the task info api we make this
// page reusable for future use. // page reusable for future use.
// As for MADV_FREE_REUSE below we ensure in the rare occasions the task was not // As for MADV_FREE_REUSE below we ensure in the rare occasions the task was not
// completed at the time of the call to re-iterate. // completed at the time of the call to re-iterate.
while (madvise(base, size, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN); while (madvise(base, size, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN);
#elif defined(MADV_FREE) #elif defined(MADV_FREE)
// Recent Linux.
madvise(base, size, MADV_FREE); madvise(base, size, MADV_FREE);
#elif defined(MADV_DONTNEED) #elif defined(MADV_DONTNEED)
// Old Linux.
madvise(base, size, MADV_DONTNEED); madvise(base, size, MADV_DONTNEED);
#elif defined(POSIX_MADV_DONTNEED)
// Solaris?
posix_madvise(base, size, POSIX_MADV_DONTNEED);
#elif defined(_WIN32) #elif defined(_WIN32)
VirtualAlloc(base, size, MEM_RESET, PAGE_READWRITE); VirtualAlloc(base, size, MEM_RESET, PAGE_READWRITE);
// Not available in all versions of Windows. // Not available in all versions of Windows.