Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1252)

Unified Diff: base/metrics/persistent_memory_allocator.cc

Issue 2362713002: Handle failures allocating mapped local memory. (Closed)
Patch Set: only report failures so new histograms don't mess up tests Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/metrics/histograms/histograms.xml » ('j') | tools/metrics/histograms/histograms.xml » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/persistent_memory_allocator.cc
diff --git a/base/metrics/persistent_memory_allocator.cc b/base/metrics/persistent_memory_allocator.cc
index dfa408f44d2be244e859727515c16d402c39ad78..1ea547e356eb8d9f2f0c644c491ee62db1b9c6b6 100644
--- a/base/metrics/persistent_memory_allocator.cc
+++ b/base/metrics/persistent_memory_allocator.cc
@@ -17,6 +17,7 @@
#include "base/logging.h"
#include "base/memory/shared_memory.h"
#include "base/metrics/histogram_macros.h"
+#include "base/metrics/sparse_histogram.h"
namespace {
@@ -744,21 +745,39 @@ LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() {
// static
void* LocalPersistentMemoryAllocator::AllocateLocalMemory(size_t size) {
+ void* address;
+
#if defined(OS_WIN)
- void* address =
+ address =
::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
- DPCHECK(address);
- return address;
+ if (!address) {
+ UMA_HISTOGRAM_SPARSE_SLOWLY(
+ "UMA.LocalPersistentMemoryAllocator.Failures.Win", ::GetLastError());
+ }
#elif defined(OS_POSIX)
// 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.
- void* address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE,
- MAP_ANON | MAP_SHARED, -1, 0);
- DPCHECK(MAP_FAILED != address);
- return address;
+ address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED, -1, 0);
+ if (address == MAP_FAILED) {
+ UMA_HISTOGRAM_SPARSE_SLOWLY(
+ "UMA.LocalPersistentMemoryAllocator.Failures.Posix", errno);
+ address = nullptr;
+ }
#else
#error This architecture is not (yet) supported.
#endif
+
+ if (!address) {
+ // 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 added
+ // to the process now istead of when first accessed).
Alexei Svitkine (slow) 2016/09/22 16:57:12 Can you add a histogram to track how often this ha
bcwhite 2016/09/22 17:25:59 I tried that (see patch #1) but it means creating
+ address = ::malloc(size);
+ DPCHECK(address);
+ ::memset(address, 0, size);
Alexei Svitkine (slow) 2016/09/22 16:57:12 Nit: I don't think it's general convention to pref
bcwhite 2016/09/22 17:25:59 Done.
+ }
+ return address;
}
// static
@@ -766,10 +785,16 @@ void LocalPersistentMemoryAllocator::DeallocateLocalMemory(void* memory,
size_t size) {
#if defined(OS_WIN)
BOOL success = ::VirtualFree(memory, 0, MEM_DECOMMIT);
- DPCHECK(success);
+ if (!success) {
+ // Must have been allocated by fallback allocator.
+ ::free(memory);
+ }
#elif defined(OS_POSIX)
int result = ::munmap(memory, size);
- DPCHECK(0 == result);
+ if (result < 0) {
+ // Must have been allocated by fallback allocator.
Alexei Svitkine (slow) 2016/09/22 16:57:12 I don't think this is a safe assumption - e.g. if
bcwhite 2016/09/22 17:25:59 I'd like to but can't because there is no place to
+ ::free(memory);
+ }
#else
#error This architecture is not (yet) supported.
#endif
« no previous file with comments | « no previous file | tools/metrics/histograms/histograms.xml » ('j') | tools/metrics/histograms/histograms.xml » ('J')

Powered by Google App Engine
This is Rietveld 408576698