feat update

This commit is contained in:
tqcq 2024-04-29 16:32:06 +08:00
parent 4a1d1c9aa1
commit a137ee1f1c
4 changed files with 365 additions and 348 deletions

View File

@ -55,11 +55,11 @@
/* Annoying stuff for windows; makes sure clients can import these functions */ /* Annoying stuff for windows; makes sure clients can import these functions */
#ifndef PERFTOOLS_DLL_DECL #ifndef PERFTOOLS_DLL_DECL
# ifdef _WIN32 #ifdef _WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport) #define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else #else
# define PERFTOOLS_DLL_DECL #define PERFTOOLS_DLL_DECL
# endif #endif
#endif #endif
/* All this code should be usable from within C apps. */ /* All this code should be usable from within C apps. */
@ -70,7 +70,7 @@ extern "C" {
/* Start profiling and arrange to write profile data to file names /* Start profiling and arrange to write profile data to file names
* of the form: "prefix.0000", "prefix.0001", ... * of the form: "prefix.0000", "prefix.0001", ...
*/ */
PERFTOOLS_DLL_DECL void HeapProfilerStart(const char* prefix); PERFTOOLS_DLL_DECL void HeapProfilerStart(const char *prefix);
/* Returns non-zero if we are currently profiling the heap. (Returns /* Returns non-zero if we are currently profiling the heap. (Returns
* an int rather than a bool so it's usable from C.) This is true * an int rather than a bool so it's usable from C.) This is true
@ -96,10 +96,10 @@ PERFTOOLS_DLL_DECL void HeapProfilerDump(const char *reason);
* The returned pointer is a '\0'-terminated string allocated using malloc() * The returned pointer is a '\0'-terminated string allocated using malloc()
* and should be free()-ed as soon as the caller does not need it anymore. * and should be free()-ed as soon as the caller does not need it anymore.
*/ */
PERFTOOLS_DLL_DECL char* GetHeapProfile(); PERFTOOLS_DLL_DECL char *GetHeapProfile();
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" }// extern "C"
#endif #endif
#endif /* BASE_HEAP_PROFILER_H_ */ #endif /* BASE_HEAP_PROFILER_H_ */

View File

@ -42,41 +42,41 @@
#endif #endif
#include <inttypes.h> #include <inttypes.h>
#ifdef HAVE_FCNTL_H #ifdef HAVE_FCNTL_H
#include <fcntl.h> // for open() #include <fcntl.h>// for open()
#endif #endif
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#include <errno.h>
#include <assert.h> #include <assert.h>
#include <sys/types.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <sys/types.h>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <gperftools/heap-profiler.h> #include <gperftools/heap-profiler.h>
#include "base/logging.h" #include "base/basictypes.h"// for PRId64, among other things
#include "base/basictypes.h" // for PRId64, among other things
#include "base/googleinit.h"
#include "base/commandlineflags.h" #include "base/commandlineflags.h"
#include "malloc_hook-inl.h" #include "base/googleinit.h"
#include "tcmalloc_guard.h" #include "base/logging.h"
#include <gperftools/malloc_hook.h>
#include <gperftools/malloc_extension.h>
#include "base/spinlock.h"
#include "base/low_level_alloc.h" #include "base/low_level_alloc.h"
#include "base/sysinfo.h" // for GetUniquePathFromEnv() #include "base/spinlock.h"
#include "base/sysinfo.h"// for GetUniquePathFromEnv()
#include "heap-profile-table.h" #include "heap-profile-table.h"
#include "malloc_hook-inl.h"
#include "memory_region_map.h" #include "memory_region_map.h"
#include "mmap_hook.h" #include "mmap_hook.h"
#include "tcmalloc_guard.h"
#include <gperftools/malloc_extension.h>
#include <gperftools/malloc_hook.h>
#ifndef PATH_MAX #ifndef PATH_MAX
#ifdef MAXPATHLEN #ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN #define PATH_MAX MAXPATHLEN
#else #else
#define PATH_MAX 4096 // seems conservative for max filename len! #define PATH_MAX 4096// seems conservative for max filename len!
#endif #endif
#endif #endif
@ -110,9 +110,7 @@ DEFINE_int64(heap_profile_time_interval,
EnvToInt64("HEAP_PROFILE_TIME_INTERVAL", 0), EnvToInt64("HEAP_PROFILE_TIME_INTERVAL", 0),
"If non-zero, dump heap profiling information once every " "If non-zero, dump heap profiling information once every "
"specified number of seconds since the last dump."); "specified number of seconds since the last dump.");
DEFINE_bool(mmap_log, DEFINE_bool(mmap_log, EnvToBool("HEAP_PROFILE_MMAP_LOG", false), "Should mmap/munmap calls be logged?");
EnvToBool("HEAP_PROFILE_MMAP_LOG", false),
"Should mmap/munmap calls be logged?");
DEFINE_bool(mmap_profile, DEFINE_bool(mmap_profile,
EnvToBool("HEAP_PROFILE_MMAP", false), EnvToBool("HEAP_PROFILE_MMAP", false),
"If heap-profiling is on, also profile mmap, mremap, and sbrk)"); "If heap-profiling is on, also profile mmap, mremap, and sbrk)");
@ -121,7 +119,6 @@ DEFINE_bool(only_mmap_profile,
"If heap-profiling is on, only profile mmap, mremap, and sbrk; " "If heap-profiling is on, only profile mmap, mremap, and sbrk; "
"do not profile malloc/new/etc"); "do not profile malloc/new/etc");
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Locking // Locking
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -140,10 +137,15 @@ static SpinLock heap_lock(SpinLock::LINKER_INITIALIZED);
static LowLevelAlloc::Arena *heap_profiler_memory; static LowLevelAlloc::Arena *heap_profiler_memory;
static void* ProfilerMalloc(size_t bytes) { static void *
ProfilerMalloc(size_t bytes)
{
return LowLevelAlloc::AllocWithArena(bytes, heap_profiler_memory); return LowLevelAlloc::AllocWithArena(bytes, heap_profiler_memory);
} }
static void ProfilerFree(void* p) {
static void
ProfilerFree(void *p)
{
LowLevelAlloc::Free(p); LowLevelAlloc::Free(p);
} }
@ -155,17 +157,16 @@ static const int kProfileBufferSize = 1 << 20;
// will be used by HeapProfileEndWriter when the application has to // will be used by HeapProfileEndWriter when the application has to
// exit due to out-of-memory. This buffer is allocated in // exit due to out-of-memory. This buffer is allocated in
// HeapProfilerStart. Access to this must be protected by heap_lock. // HeapProfilerStart. Access to this must be protected by heap_lock.
static char* global_profiler_buffer = NULL; static char *global_profiler_buffer = NULL;
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Profiling control/state data // Profiling control/state data
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Access to all of these is protected by heap_lock. // Access to all of these is protected by heap_lock.
static bool is_on = false; // If are on as a subsytem. static bool is_on = false;// If are on as a subsytem.
static bool dumping = false; // Dumping status to prevent recursion static bool dumping = false;// Dumping status to prevent recursion
static char* filename_prefix = NULL; // Prefix used for profile file names static char *filename_prefix = NULL; // Prefix used for profile file names
// (NULL if no need for dumping yet) // (NULL if no need for dumping yet)
static int dump_count = 0; // How many dumps so far static int dump_count = 0; // How many dumps so far
static int64 last_dump_alloc = 0; // alloc_size when did we last dump static int64 last_dump_alloc = 0; // alloc_size when did we last dump
@ -173,24 +174,25 @@ static int64 last_dump_free = 0; // free_size when did we last dump
static int64 high_water_mark = 0; // In-use-bytes at last high-water dump static int64 high_water_mark = 0; // In-use-bytes at last high-water dump
static int64 last_dump_time = 0; // The time of the last dump static int64 last_dump_time = 0; // The time of the last dump
static HeapProfileTable* heap_profile = NULL; // the heap profile table static HeapProfileTable *heap_profile = NULL;// the heap profile table
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Profile generation // Profile generation
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Input must be a buffer of size at least 1MB. // Input must be a buffer of size at least 1MB.
static char* DoGetHeapProfileLocked(char* buf, int buflen) { static char *
DoGetHeapProfileLocked(char *buf, int buflen)
{
// We used to be smarter about estimating the required memory and // We used to be smarter about estimating the required memory and
// then capping it to 1MB and generating the profile into that. // then capping it to 1MB and generating the profile into that.
if (buf == NULL || buflen < 1) if (buf == NULL || buflen < 1) return NULL;
return NULL;
RAW_DCHECK(heap_lock.IsHeld(), ""); RAW_DCHECK(heap_lock.IsHeld(), "");
int bytes_written = 0; int bytes_written = 0;
if (is_on) { if (is_on) {
HeapProfileTable::Stats const stats = heap_profile->total(); HeapProfileTable::Stats const stats = heap_profile->total();
(void)stats; // avoid an unused-variable warning in non-debug mode. (void) stats;// avoid an unused-variable warning in non-debug mode.
bytes_written = heap_profile->FillOrderedProfile(buf, buflen - 1); bytes_written = heap_profile->FillOrderedProfile(buf, buflen - 1);
// FillOrderedProfile should not reduce the set of active mmap-ed regions, // FillOrderedProfile should not reduce the set of active mmap-ed regions,
// hence MemoryRegionMap will let us remove everything we've added above: // hence MemoryRegionMap will let us remove everything we've added above:
@ -204,32 +206,35 @@ static char* DoGetHeapProfileLocked(char* buf, int buflen) {
return buf; return buf;
} }
extern "C" char* GetHeapProfile() { extern "C" char *
GetHeapProfile()
{
// Use normal malloc: we return the profile to the user to free it: // Use normal malloc: we return the profile to the user to free it:
char* buffer = reinterpret_cast<char*>(malloc(kProfileBufferSize)); char *buffer = reinterpret_cast<char *>(malloc(kProfileBufferSize));
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
return DoGetHeapProfileLocked(buffer, kProfileBufferSize); return DoGetHeapProfileLocked(buffer, kProfileBufferSize);
} }
// defined below // defined below
static void NewHook(const void* ptr, size_t size); static void NewHook(const void *ptr, size_t size);
static void DeleteHook(const void* ptr); static void DeleteHook(const void *ptr);
// Helper for HeapProfilerDump. // Helper for HeapProfilerDump.
static void DumpProfileLocked(const char* reason) { static void
DumpProfileLocked(const char *reason)
{
RAW_DCHECK(heap_lock.IsHeld(), ""); RAW_DCHECK(heap_lock.IsHeld(), "");
RAW_DCHECK(is_on, ""); RAW_DCHECK(is_on, "");
RAW_DCHECK(!dumping, ""); RAW_DCHECK(!dumping, "");
if (filename_prefix == NULL) return; // we do not yet need dumping if (filename_prefix == NULL) return;// we do not yet need dumping
dumping = true; dumping = true;
// Make file name // Make file name
char file_name[1000]; char file_name[1000];
dump_count++; dump_count++;
snprintf(file_name, sizeof(file_name), "%s.%04d%s", snprintf(file_name, sizeof(file_name), "%s.%04d%s", filename_prefix, dump_count, HeapProfileTable::kFileExt);
filename_prefix, dump_count, HeapProfileTable::kFileExt);
// Dump the profile // Dump the profile
RAW_VLOG(0, "Dumping heap profile to %s (%s)", file_name, reason); RAW_VLOG(0, "Dumping heap profile to %s (%s)", file_name, reason);
@ -245,12 +250,10 @@ static void DumpProfileLocked(const char* reason) {
// This case may be impossible, but it's best to be safe. // This case may be impossible, but it's best to be safe.
// It's safe to use the global buffer: we're protected by heap_lock. // It's safe to use the global buffer: we're protected by heap_lock.
if (global_profiler_buffer == NULL) { if (global_profiler_buffer == NULL) {
global_profiler_buffer = global_profiler_buffer = reinterpret_cast<char *>(ProfilerMalloc(kProfileBufferSize));
reinterpret_cast<char*>(ProfilerMalloc(kProfileBufferSize));
} }
char* profile = DoGetHeapProfileLocked(global_profiler_buffer, char *profile = DoGetHeapProfileLocked(global_profiler_buffer, kProfileBufferSize);
kProfileBufferSize);
RawWrite(fd, profile, strlen(profile)); RawWrite(fd, profile, strlen(profile));
RawClose(fd); RawClose(fd);
@ -263,39 +266,41 @@ static void DumpProfileLocked(const char* reason) {
// Dump a profile after either an allocation or deallocation, if // Dump a profile after either an allocation or deallocation, if
// the memory use has changed enough since the last dump. // the memory use has changed enough since the last dump.
static void MaybeDumpProfileLocked() { static void
MaybeDumpProfileLocked()
{
if (!dumping) { if (!dumping) {
const HeapProfileTable::Stats& total = heap_profile->total(); const HeapProfileTable::Stats &total = heap_profile->total();
const int64_t inuse_bytes = total.alloc_size - total.free_size; const int64_t inuse_bytes = total.alloc_size - total.free_size;
bool need_to_dump = false; bool need_to_dump = false;
char buf[128]; char buf[128];
if (FLAGS_heap_profile_allocation_interval > 0 && if (FLAGS_heap_profile_allocation_interval > 0
total.alloc_size >= && total.alloc_size >= last_dump_alloc + FLAGS_heap_profile_allocation_interval) {
last_dump_alloc + FLAGS_heap_profile_allocation_interval) { snprintf(buf,
snprintf(buf, sizeof(buf), ("%" PRId64 " MB allocated cumulatively, " sizeof(buf),
("%" PRId64 " MB allocated cumulatively, "
"%" PRId64 " MB currently in use"), "%" PRId64 " MB currently in use"),
total.alloc_size >> 20, inuse_bytes >> 20); total.alloc_size >> 20,
need_to_dump = true;
} else if (FLAGS_heap_profile_deallocation_interval > 0 &&
total.free_size >=
last_dump_free + FLAGS_heap_profile_deallocation_interval) {
snprintf(buf, sizeof(buf), ("%" PRId64 " MB freed cumulatively, "
"%" PRId64 " MB currently in use"),
total.free_size >> 20, inuse_bytes >> 20);
need_to_dump = true;
} else if (FLAGS_heap_profile_inuse_interval > 0 &&
inuse_bytes >
high_water_mark + FLAGS_heap_profile_inuse_interval) {
snprintf(buf, sizeof(buf), "%" PRId64 " MB currently in use",
inuse_bytes >> 20); inuse_bytes >> 20);
need_to_dump = true; need_to_dump = true;
} else if (FLAGS_heap_profile_time_interval > 0 ) { } else if (FLAGS_heap_profile_deallocation_interval > 0
&& total.free_size >= last_dump_free + FLAGS_heap_profile_deallocation_interval) {
snprintf(buf,
sizeof(buf),
("%" PRId64 " MB freed cumulatively, "
"%" PRId64 " MB currently in use"),
total.free_size >> 20,
inuse_bytes >> 20);
need_to_dump = true;
} else if (FLAGS_heap_profile_inuse_interval > 0
&& inuse_bytes > high_water_mark + FLAGS_heap_profile_inuse_interval) {
snprintf(buf, sizeof(buf), "%" PRId64 " MB currently in use", inuse_bytes >> 20);
need_to_dump = true;
} else if (FLAGS_heap_profile_time_interval > 0) {
int64 current_time = time(NULL); int64 current_time = time(NULL);
if (current_time - last_dump_time >= if (current_time - last_dump_time >= FLAGS_heap_profile_time_interval) {
FLAGS_heap_profile_time_interval) { snprintf(buf, sizeof(buf), "%" PRId64 " sec since the last dump", current_time - last_dump_time);
snprintf(buf, sizeof(buf), "%" PRId64 " sec since the last dump",
current_time - last_dump_time);
need_to_dump = true; need_to_dump = true;
last_dump_time = current_time; last_dump_time = current_time;
} }
@ -305,16 +310,17 @@ static void MaybeDumpProfileLocked() {
last_dump_alloc = total.alloc_size; last_dump_alloc = total.alloc_size;
last_dump_free = total.free_size; last_dump_free = total.free_size;
if (inuse_bytes > high_water_mark) if (inuse_bytes > high_water_mark) high_water_mark = inuse_bytes;
high_water_mark = inuse_bytes;
} }
} }
} }
// Record an allocation in the profile. // Record an allocation in the profile.
static void RecordAlloc(const void* ptr, size_t bytes, int skip_count) { static void
RecordAlloc(const void *ptr, size_t bytes, int skip_count)
{
// Take the stack trace outside the critical section. // Take the stack trace outside the critical section.
void* stack[HeapProfileTable::kMaxStackDepth]; void *stack[HeapProfileTable::kMaxStackDepth];
int depth = HeapProfileTable::GetCallerStackTrace(skip_count + 1, stack); int depth = HeapProfileTable::GetCallerStackTrace(skip_count + 1, stack);
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
if (is_on) { if (is_on) {
@ -324,7 +330,9 @@ static void RecordAlloc(const void* ptr, size_t bytes, int skip_count) {
} }
// Record a deallocation in the profile. // Record a deallocation in the profile.
static void RecordFree(const void* ptr) { static void
RecordFree(const void *ptr)
{
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
if (is_on) { if (is_on) {
heap_profile->RecordFree(ptr); heap_profile->RecordFree(ptr);
@ -337,42 +345,57 @@ static void RecordFree(const void* ptr) {
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// static // static
void NewHook(const void* ptr, size_t size) { void
NewHook(const void *ptr, size_t size)
{
if (ptr != NULL) RecordAlloc(ptr, size, 0); if (ptr != NULL) RecordAlloc(ptr, size, 0);
} }
// static // static
void DeleteHook(const void* ptr) { void
DeleteHook(const void *ptr)
{
if (ptr != NULL) RecordFree(ptr); if (ptr != NULL) RecordFree(ptr);
} }
static tcmalloc::MappingHookSpace mmap_logging_hook_space; static tcmalloc::MappingHookSpace mmap_logging_hook_space;
static void LogMappingEvent(const tcmalloc::MappingEvent& evt) { static void
if (!FLAGS_mmap_log) { LogMappingEvent(const tcmalloc::MappingEvent &evt)
return; {
} if (!FLAGS_mmap_log) { return; }
if (evt.file_valid) { if (evt.file_valid) {
// We use PRIxPTR not just '%p' to avoid deadlocks // We use PRIxPTR not just '%p' to avoid deadlocks
// in pretty-printing of NULL as "nil". // in pretty-printing of NULL as "nil".
// TODO(maxim): instead should use a safe snprintf reimplementation // TODO(maxim): instead should use a safe snprintf reimplementation
RAW_LOG(INFO, RAW_LOG(INFO,
"mmap(start=0x%" PRIxPTR ", len=%zu, prot=0x%x, flags=0x%x, " "mmap(start=0x%" PRIxPTR
", len=%zu, prot=0x%x, flags=0x%x, "
"fd=%d, offset=0x%llx) = 0x%" PRIxPTR "", "fd=%d, offset=0x%llx) = 0x%" PRIxPTR "",
(uintptr_t) evt.before_address, evt.after_length, evt.prot, (uintptr_t) evt.before_address,
evt.flags, evt.file_fd, (unsigned long long) evt.file_off, evt.after_length,
evt.prot,
evt.flags,
evt.file_fd,
(unsigned long long) evt.file_off,
(uintptr_t) evt.after_address); (uintptr_t) evt.after_address);
} else if (evt.after_valid && evt.before_valid) { } else if (evt.after_valid && evt.before_valid) {
// We use PRIxPTR not just '%p' to avoid deadlocks // We use PRIxPTR not just '%p' to avoid deadlocks
// in pretty-printing of NULL as "nil". // in pretty-printing of NULL as "nil".
// TODO(maxim): instead should use a safe snprintf reimplementation // TODO(maxim): instead should use a safe snprintf reimplementation
RAW_LOG(INFO, RAW_LOG(INFO,
"mremap(old_addr=0x%" PRIxPTR ", old_size=%zu, " "mremap(old_addr=0x%" PRIxPTR
"new_size=%zu, flags=0x%x, new_addr=0x%" PRIxPTR ") = " ", old_size=%zu, "
"new_size=%zu, flags=0x%x, new_addr=0x%" PRIxPTR
") = "
"0x%" PRIxPTR "", "0x%" PRIxPTR "",
(uintptr_t) evt.before_address, evt.before_length, evt.after_length, evt.flags, (uintptr_t) evt.before_address,
(uintptr_t) evt.after_address, (uintptr_t) evt.after_address); evt.before_length,
evt.after_length,
evt.flags,
(uintptr_t) evt.after_address,
(uintptr_t) evt.after_address);
} else if (evt.is_sbrk) { } else if (evt.is_sbrk) {
intptr_t increment; intptr_t increment;
uintptr_t result; uintptr_t result;
@ -384,14 +407,12 @@ static void LogMappingEvent(const tcmalloc::MappingEvent& evt) {
result = reinterpret_cast<uintptr_t>(evt.before_address); result = reinterpret_cast<uintptr_t>(evt.before_address);
} }
RAW_LOG(INFO, "sbrk(inc=%zd) = 0x%" PRIxPTR "", RAW_LOG(INFO, "sbrk(inc=%zd) = 0x%" PRIxPTR "", increment, (uintptr_t) result);
increment, (uintptr_t) result);
} else if (evt.before_valid) { } else if (evt.before_valid) {
// We use PRIxPTR not just '%p' to avoid deadlocks // We use PRIxPTR not just '%p' to avoid deadlocks
// in pretty-printing of NULL as "nil". // in pretty-printing of NULL as "nil".
// TODO(maxim): instead should use a safe snprintf reimplementation // TODO(maxim): instead should use a safe snprintf reimplementation
RAW_LOG(INFO, "munmap(start=0x%" PRIxPTR ", len=%zu)", RAW_LOG(INFO, "munmap(start=0x%" PRIxPTR ", len=%zu)", (uintptr_t) evt.before_address, evt.before_length);
(uintptr_t) evt.before_address, evt.before_length);
} }
} }
@ -399,7 +420,9 @@ static void LogMappingEvent(const tcmalloc::MappingEvent& evt) {
// Starting/stopping/dumping // Starting/stopping/dumping
//---------------------------------------------------------------------- //----------------------------------------------------------------------
extern "C" void HeapProfilerStart(const char* prefix) { extern "C" void
HeapProfilerStart(const char *prefix)
{
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
if (is_on) return; if (is_on) return;
@ -412,9 +435,7 @@ extern "C" void HeapProfilerStart(const char* prefix) {
// call new, and we want that to be accounted for correctly. // call new, and we want that to be accounted for correctly.
MallocExtension::Initialize(); MallocExtension::Initialize();
if (FLAGS_only_mmap_profile) { if (FLAGS_only_mmap_profile) { FLAGS_mmap_profile = true; }
FLAGS_mmap_profile = true;
}
if (FLAGS_mmap_profile) { if (FLAGS_mmap_profile) {
// Ask MemoryRegionMap to record all mmap, mremap, and sbrk // Ask MemoryRegionMap to record all mmap, mremap, and sbrk
@ -428,15 +449,13 @@ extern "C" void HeapProfilerStart(const char* prefix) {
tcmalloc::HookMMapEvents(&mmap_logging_hook_space, LogMappingEvent); tcmalloc::HookMMapEvents(&mmap_logging_hook_space, LogMappingEvent);
} }
heap_profiler_memory = heap_profiler_memory = LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena());
LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena());
// Reserve space now for the heap profiler, so we can still write a // Reserve space now for the heap profiler, so we can still write a
// heap profile even if the application runs out of memory. // heap profile even if the application runs out of memory.
global_profiler_buffer = global_profiler_buffer = reinterpret_cast<char *>(ProfilerMalloc(kProfileBufferSize));
reinterpret_cast<char*>(ProfilerMalloc(kProfileBufferSize));
heap_profile = new(ProfilerMalloc(sizeof(HeapProfileTable))) heap_profile = new (ProfilerMalloc(sizeof(HeapProfileTable)))
HeapProfileTable(ProfilerMalloc, ProfilerFree, FLAGS_mmap_profile); HeapProfileTable(ProfilerMalloc, ProfilerFree, FLAGS_mmap_profile);
last_dump_alloc = 0; last_dump_alloc = 0;
@ -457,17 +476,21 @@ extern "C" void HeapProfilerStart(const char* prefix) {
// Copy filename prefix // Copy filename prefix
RAW_DCHECK(filename_prefix == NULL, ""); RAW_DCHECK(filename_prefix == NULL, "");
const int prefix_length = strlen(prefix); const int prefix_length = strlen(prefix);
filename_prefix = reinterpret_cast<char*>(ProfilerMalloc(prefix_length + 1)); filename_prefix = reinterpret_cast<char *>(ProfilerMalloc(prefix_length + 1));
memcpy(filename_prefix, prefix, prefix_length); memcpy(filename_prefix, prefix, prefix_length);
filename_prefix[prefix_length] = '\0'; filename_prefix[prefix_length] = '\0';
} }
extern "C" int IsHeapProfilerRunning() { extern "C" int
IsHeapProfilerRunning()
{
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
return is_on ? 1 : 0; // return an int, because C code doesn't have bool return is_on ? 1 : 0;// return an int, because C code doesn't have bool
} }
extern "C" void HeapProfilerStop() { extern "C" void
HeapProfilerStop()
{
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
if (!is_on) return; if (!is_on) return;
@ -494,53 +517,47 @@ extern "C" void HeapProfilerStop() {
ProfilerFree(filename_prefix); ProfilerFree(filename_prefix);
filename_prefix = NULL; filename_prefix = NULL;
if (!LowLevelAlloc::DeleteArena(heap_profiler_memory)) { if (!LowLevelAlloc::DeleteArena(heap_profiler_memory)) { RAW_LOG(FATAL, "Memory leak in HeapProfiler:"); }
RAW_LOG(FATAL, "Memory leak in HeapProfiler:");
}
if (FLAGS_mmap_profile) { if (FLAGS_mmap_profile) { MemoryRegionMap::Shutdown(); }
MemoryRegionMap::Shutdown();
}
is_on = false; is_on = false;
} }
extern "C" void HeapProfilerDump(const char *reason) { extern "C" void
HeapProfilerDump(const char *reason)
{
SpinLockHolder l(&heap_lock); SpinLockHolder l(&heap_lock);
if (is_on && !dumping) { if (is_on && !dumping) { DumpProfileLocked(reason); }
DumpProfileLocked(reason);
}
} }
// Signal handler that is registered when a user selectable signal // Signal handler that is registered when a user selectable signal
// number is defined in the environment variable HEAPPROFILESIGNAL. // number is defined in the environment variable HEAPPROFILESIGNAL.
static void HeapProfilerDumpSignal(int signal_number) { static void
(void)signal_number; HeapProfilerDumpSignal(int signal_number)
if (!heap_lock.TryLock()) { {
return; (void) signal_number;
} if (!heap_lock.TryLock()) { return; }
if (is_on && !dumping) { if (is_on && !dumping) { DumpProfileLocked("signal"); }
DumpProfileLocked("signal");
}
heap_lock.Unlock(); heap_lock.Unlock();
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Initialization/finalization code // Initialization/finalization code
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Initialization code // Initialization code
static void HeapProfilerInit() { static void
HeapProfilerInit()
{
// Everything after this point is for setting up the profiler based on envvar // Everything after this point is for setting up the profiler based on envvar
char fname[PATH_MAX]; char fname[PATH_MAX];
if (!GetUniquePathFromEnv("HEAPPROFILE", fname)) { if (!GetUniquePathFromEnv("HEAPPROFILE", fname)) { return; }
return;
}
// We do a uid check so we don't write out files in a setuid executable. // We do a uid check so we don't write out files in a setuid executable.
#ifdef HAVE_GETEUID #ifdef HAVE_GETEUID
if (getuid() != geteuid()) { if (getuid() != geteuid()) {
RAW_LOG(WARNING, ("HeapProfiler: ignoring HEAPPROFILE because " RAW_LOG(WARNING,
("HeapProfiler: ignoring HEAPPROFILE because "
"program seems to be setuid\n")); "program seems to be setuid\n"));
return; return;
} }
@ -553,7 +570,7 @@ static void HeapProfilerInit() {
if (old_signal_handler == reinterpret_cast<intptr_t>(SIG_ERR)) { if (old_signal_handler == reinterpret_cast<intptr_t>(SIG_ERR)) {
RAW_LOG(FATAL, "Failed to set signal. Perhaps signal number %s is invalid\n", signal_number_str); RAW_LOG(FATAL, "Failed to set signal. Perhaps signal number %s is invalid\n", signal_number_str);
} else if (old_signal_handler == 0) { } else if (old_signal_handler == 0) {
RAW_LOG(INFO,"Using signal %d as heap profiling switch", signal_number); RAW_LOG(INFO, "Using signal %d as heap profiling switch", signal_number);
} else { } else {
RAW_LOG(FATAL, "Signal %d already in use\n", signal_number); RAW_LOG(FATAL, "Signal %d already in use\n", signal_number);
} }
@ -566,21 +583,19 @@ static void HeapProfilerInit() {
// class used for finalization -- dumps the heap-profile at program exit // class used for finalization -- dumps the heap-profile at program exit
struct HeapProfileEndWriter { struct HeapProfileEndWriter {
~HeapProfileEndWriter() { ~HeapProfileEndWriter()
{
char buf[128]; char buf[128];
if (heap_profile) { if (heap_profile) {
const HeapProfileTable::Stats& total = heap_profile->total(); const HeapProfileTable::Stats &total = heap_profile->total();
const int64_t inuse_bytes = total.alloc_size - total.free_size; const int64_t inuse_bytes = total.alloc_size - total.free_size;
if ((inuse_bytes >> 20) > 0) { if ((inuse_bytes >> 20) > 0) {
snprintf(buf, sizeof(buf), ("Exiting, %" PRId64 " MB in use"), snprintf(buf, sizeof(buf), ("Exiting, %" PRId64 " MB in use"), inuse_bytes >> 20);
inuse_bytes >> 20);
} else if ((inuse_bytes >> 10) > 0) { } else if ((inuse_bytes >> 10) > 0) {
snprintf(buf, sizeof(buf), ("Exiting, %" PRId64 " kB in use"), snprintf(buf, sizeof(buf), ("Exiting, %" PRId64 " kB in use"), inuse_bytes >> 10);
inuse_bytes >> 10);
} else { } else {
snprintf(buf, sizeof(buf), ("Exiting, %" PRId64 " bytes in use"), snprintf(buf, sizeof(buf), ("Exiting, %" PRId64 " bytes in use"), inuse_bytes);
inuse_bytes);
} }
} else { } else {
snprintf(buf, sizeof(buf), ("Exiting")); snprintf(buf, sizeof(buf), ("Exiting"));

View File

@ -104,7 +104,7 @@ target_sources(
target_link_libraries( target_link_libraries(
sled sled
PUBLIC rpc_core fmt marl Async++ minilua tcmalloc_and_profiler_static PUBLIC rpc_core fmt marl Async++ minilua
# protobuf::libprotoc # protobuf::libprotoc
PRIVATE dl) PRIVATE dl)
if(SLED_WITH_PROTOBUF) if(SLED_WITH_PROTOBUF)

View File

@ -108,5 +108,7 @@
// testing // testing
#include "sled/testing/test.h" #include "sled/testing/test.h"
// debugging // debugging
#endif// SLED_SLED_H #endif// SLED_SLED_H