OLD | NEW |
---|---|
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
11 #include "winbase.h" | 11 #include "winbase.h" |
12 #elif defined(OS_POSIX) | 12 #elif defined(OS_POSIX) |
13 #include <sys/mman.h> | 13 #include <sys/mman.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include "base/files/memory_mapped_file.h" | 16 #include "base/files/memory_mapped_file.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/memory/shared_memory.h" | 18 #include "base/memory/shared_memory.h" |
19 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
20 #include "base/metrics/sparse_histogram.h" | |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Limit of memory segment size. It has to fit in an unsigned 32-bit number | 24 // Limit of memory segment size. It has to fit in an unsigned 32-bit number |
24 // and should be a power of 2 in order to accomodate almost any page size. | 25 // and should be a power of 2 in order to accomodate almost any page size. |
25 const uint32_t kSegmentMaxSize = 1 << 30; // 1 GiB | 26 const uint32_t kSegmentMaxSize = 1 << 30; // 1 GiB |
26 | 27 |
27 // A constant (random) value placed in the shared metadata to identify | 28 // A constant (random) value placed in the shared metadata to identify |
28 // an already initialized memory segment. | 29 // an already initialized memory segment. |
29 const uint32_t kGlobalCookie = 0x408305DC; | 30 const uint32_t kGlobalCookie = 0x408305DC; |
(...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
737 base::StringPiece name) | 738 base::StringPiece name) |
738 : PersistentMemoryAllocator(AllocateLocalMemory(size), | 739 : PersistentMemoryAllocator(AllocateLocalMemory(size), |
739 size, 0, id, name, false) {} | 740 size, 0, id, name, false) {} |
740 | 741 |
741 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { | 742 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { |
742 DeallocateLocalMemory(const_cast<char*>(mem_base_), mem_size_); | 743 DeallocateLocalMemory(const_cast<char*>(mem_base_), mem_size_); |
743 } | 744 } |
744 | 745 |
745 // static | 746 // static |
746 void* LocalPersistentMemoryAllocator::AllocateLocalMemory(size_t size) { | 747 void* LocalPersistentMemoryAllocator::AllocateLocalMemory(size_t size) { |
748 void* address; | |
749 | |
747 #if defined(OS_WIN) | 750 #if defined(OS_WIN) |
748 void* address = | 751 address = |
749 ::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | 752 ::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
750 DPCHECK(address); | 753 if (!address) { |
751 return address; | 754 UMA_HISTOGRAM_SPARSE_SLOWLY( |
755 "UMA.LocalPersistentMemoryAllocator.Failures.Win", ::GetLastError()); | |
756 } | |
752 #elif defined(OS_POSIX) | 757 #elif defined(OS_POSIX) |
753 // MAP_ANON is deprecated on Linux but MAP_ANONYMOUS is not universal on Mac. | 758 // MAP_ANON is deprecated on Linux but MAP_ANONYMOUS is not universal on Mac. |
754 // MAP_SHARED is not available on Linux <2.4 but required on Mac. | 759 // MAP_SHARED is not available on Linux <2.4 but required on Mac. |
755 void* address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, | 760 address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, |
756 MAP_ANON | MAP_SHARED, -1, 0); | 761 MAP_ANON | MAP_SHARED, -1, 0); |
757 DPCHECK(MAP_FAILED != address); | 762 if (address == MAP_FAILED) { |
758 return address; | 763 UMA_HISTOGRAM_SPARSE_SLOWLY( |
764 "UMA.LocalPersistentMemoryAllocator.Failures.Posix", errno); | |
765 address = nullptr; | |
766 } | |
759 #else | 767 #else |
760 #error This architecture is not (yet) supported. | 768 #error This architecture is not (yet) supported. |
761 #endif | 769 #endif |
770 | |
771 if (!address) { | |
772 // As a last resort, just allocate the memory from the heap. This will | |
773 // achieve the same basic result but the acquired memory has to be | |
774 // explicitly zeroed and thus realized immediately (i.e. all pages added | |
775 // 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
| |
776 address = ::malloc(size); | |
777 DPCHECK(address); | |
778 ::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.
| |
779 } | |
780 return address; | |
762 } | 781 } |
763 | 782 |
764 // static | 783 // static |
765 void LocalPersistentMemoryAllocator::DeallocateLocalMemory(void* memory, | 784 void LocalPersistentMemoryAllocator::DeallocateLocalMemory(void* memory, |
766 size_t size) { | 785 size_t size) { |
767 #if defined(OS_WIN) | 786 #if defined(OS_WIN) |
768 BOOL success = ::VirtualFree(memory, 0, MEM_DECOMMIT); | 787 BOOL success = ::VirtualFree(memory, 0, MEM_DECOMMIT); |
769 DPCHECK(success); | 788 if (!success) { |
789 // Must have been allocated by fallback allocator. | |
790 ::free(memory); | |
791 } | |
770 #elif defined(OS_POSIX) | 792 #elif defined(OS_POSIX) |
771 int result = ::munmap(memory, size); | 793 int result = ::munmap(memory, size); |
772 DPCHECK(0 == result); | 794 if (result < 0) { |
795 // 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
| |
796 ::free(memory); | |
797 } | |
773 #else | 798 #else |
774 #error This architecture is not (yet) supported. | 799 #error This architecture is not (yet) supported. |
775 #endif | 800 #endif |
776 } | 801 } |
777 | 802 |
778 | 803 |
779 //----- SharedPersistentMemoryAllocator ---------------------------------------- | 804 //----- SharedPersistentMemoryAllocator ---------------------------------------- |
780 | 805 |
781 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( | 806 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( |
782 std::unique_ptr<SharedMemory> memory, | 807 std::unique_ptr<SharedMemory> memory, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
821 | 846 |
822 // static | 847 // static |
823 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 848 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
824 const MemoryMappedFile& file, | 849 const MemoryMappedFile& file, |
825 bool read_only) { | 850 bool read_only) { |
826 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); | 851 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); |
827 } | 852 } |
828 #endif // !defined(OS_NACL) | 853 #endif // !defined(OS_NACL) |
829 | 854 |
830 } // namespace base | 855 } // namespace base |
OLD | NEW |