• Tobias Klauser's avatar
    runtime: use MADV_FREE on Linux if available · 77f9b272
    Tobias Klauser authored
    On Linux, sysUnused currently uses madvise(MADV_DONTNEED) to signal the
    kernel that a range of allocated memory contains unneeded data. After a
    successful call, the range (but not the data it contained before the
    call to madvise) is still available but the first access to that range
    will unconditionally incur a page fault (needed to 0-fill the range).
    
    A faster alternative is MADV_FREE, available since Linux 4.5. The
    mechanism is very similar, but the page fault will only be incurred if
    the kernel, between the call to madvise and the first access, decides to
    reuse that memory for something else.
    
    In sysUnused, test whether MADV_FREE is supported and fall back to
    MADV_DONTNEED in case it isn't. This requires making the return value of
    the madvise syscall available to the caller, so change runtime.madvise
    to return it.
    
    Fixes #23687
    
    Change-Id: I962c3429000dd9f4a00846461ad128b71201bb04
    Reviewed-on: https://go-review.googlesource.com/135395
    Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
    77f9b272