Commit 3a46229c by Abseil Team Committed by Copybara-Service

Name anonymous memory allocations on Linux.

Use Linux's prctl(PR_SET_VMA) system call to name memory arenas being
allocated using mmap(MAP_ANONYMOUS).

This change allows Abseil's memory arena(s) to be distinguished from
other uses of anonymous memory within a process, which in turn helps
investigations into the memory usage of applications.

The change adds a new prctl() system call to the code paths that call
mmap(). This is not expected to add significant overhead to applications.

The call to prctl(PR_SET_VMA, ...) can fail if the Linux kernel in use was
not configured with the CONFIG_ANON_VMA_NAME kernel option.  This should
be OK since the naming memory regions is primarily a debugging aid.

PiperOrigin-RevId: 523687348
Change-Id: Ie404e5eeef0a6da53330b3a56149c4f3bc6bf5c7
parent 5ad663b7
...@@ -42,6 +42,10 @@ ...@@ -42,6 +42,10 @@
#include <windows.h> #include <windows.h>
#endif #endif
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <string.h> #include <string.h>
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
...@@ -570,6 +574,18 @@ static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) { ...@@ -570,6 +574,18 @@ static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) {
ABSL_RAW_LOG(FATAL, "mmap error: %d", errno); ABSL_RAW_LOG(FATAL, "mmap error: %d", errno);
} }
#ifdef __linux__
#if defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
// Attempt to name the allocated address range in /proc/$PID/smaps on
// Linux.
//
// This invocation of prctl() may fail if the Linux kernel was not
// configured with the CONFIG_ANON_VMA_NAME option. This is OK since
// the naming of arenas is primarily a debugging aid.
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new_pages, new_pages_size,
"absl");
#endif
#endif // __linux__
#endif // _WIN32 #endif // _WIN32
arena->mu.Lock(); arena->mu.Lock();
s = reinterpret_cast<AllocList *>(new_pages); s = reinterpret_cast<AllocList *>(new_pages);
......
...@@ -33,6 +33,10 @@ ...@@ -33,6 +33,10 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
#include <cerrno> #include <cerrno>
...@@ -172,6 +176,20 @@ static bool SetupAlternateStackOnce() { ...@@ -172,6 +176,20 @@ static bool SetupAlternateStackOnce() {
if (sigaltstack(&sigstk, nullptr) != 0) { if (sigaltstack(&sigstk, nullptr) != 0) {
ABSL_RAW_LOG(FATAL, "sigaltstack() failed with errno=%d", errno); ABSL_RAW_LOG(FATAL, "sigaltstack() failed with errno=%d", errno);
} }
#ifdef __linux__
#if defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
// Make a best-effort attempt to name the allocated region in
// /proc/$PID/smaps.
//
// The call to prctl() may fail if the kernel was not configured with the
// CONFIG_ANON_VMA_NAME kernel option. This is OK since the call is
// primarily a debugging aid.
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, sigstk.ss_sp, sigstk.ss_size,
"absl-signalstack");
#endif
#endif // __linux__
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment