Commit 1f2bdb85 authored by Brian White's avatar Brian White Committed by Commit Bot

Handle inability to allocate persistent memory during Install.

If the program is unable to allocate a memory (1MiB) for histogram
recording, just disable persistent histograms. No metrics will ever
be passed to Chrome for upload during this session but it's better
that just crashing during program initialization.

Bug: 946577
Change-Id: I45c5fe7de5de63e7b1395e301e496775fb65346f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1622767
Commit-Queue: Brian White <bcwhite@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#663128}
parent efee28e6
......@@ -9,15 +9,54 @@
#include "base/logging.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/metrics/persistent_memory_allocator.h"
#include "base/process/memory.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
// Dummy line to stop `git cl format` from reordering these includes.
#include <memoryapi.h>
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
#include <sys/mman.h>
#endif
namespace {
constexpr size_t kAllocSize = 1 << 20; // 1 MiB
void* AllocateLocalMemory(size_t size) {
void* address;
#if defined(OS_WIN)
address =
::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (address)
return address;
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
// MAP_ANON is deprecated on Linux but MAP_ANONYMOUS is not universal on Mac.
// MAP_SHARED is not available on Linux <2.4 but required on Mac.
address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
-1, 0);
if (address != MAP_FAILED)
return address;
#else
#error This architecture is not (yet) supported.
#endif
// As a last resort, just allocate the memory from the heap. This will
// achieve the same basic result but the acquired memory has to be
// explicitly zeroed and thus realized immediately (i.e. all pages are
// added to the process now instead of only when first accessed).
if (!base::UncheckedMalloc(size, &address))
return nullptr;
DCHECK(address);
memset(address, 0, size);
return address;
}
} // namespace
namespace base {
......@@ -29,14 +68,25 @@ PersistentHistogramStorage::PersistentHistogramStorage(
DCHECK(!allocator_name.empty());
DCHECK(IsStringASCII(allocator_name));
GlobalHistogramAllocator::CreateWithLocalMemory(kAllocSize,
0, // No identifier.
allocator_name);
// This code may be executed before crash handling and/or OOM handling has
// been initialized for the process. Silently ignore a failed allocation
// (no metric persistence) rather that generating a crash that won't be
// caught/reported.
void* memory = AllocateLocalMemory(kAllocSize);
if (!memory)
return;
GlobalHistogramAllocator::CreateWithPersistentMemory(memory, kAllocSize, 0,
0, // No identifier.
allocator_name);
GlobalHistogramAllocator::Get()->CreateTrackingHistograms(allocator_name);
}
PersistentHistogramStorage::~PersistentHistogramStorage() {
PersistentHistogramAllocator* allocator = GlobalHistogramAllocator::Get();
if (!allocator)
return;
allocator->UpdateTrackingHistograms();
if (disabled_)
......
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