1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/base/paged_memory.h"
18
19 #include <algorithm>
20 #include <cmath>
21
22 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
23 #include <Windows.h>
24 #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
25 #include <sys/mman.h>
26 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
27
28 #include "perfetto/base/logging.h"
29 #include "perfetto/ext/base/container_annotations.h"
30 #include "perfetto/ext/base/utils.h"
31
32 namespace perfetto {
33 namespace base {
34
35 namespace {
36
37 #if TRACK_COMMITTED_SIZE()
38 constexpr size_t kCommitChunkSize = 4 * 1024 * 1024; // 4MB
39 #endif
40
RoundUpToSysPageSize(size_t req_size)41 size_t RoundUpToSysPageSize(size_t req_size) {
42 const size_t page_size = GetSysPageSize();
43 return (req_size + page_size - 1) & ~(page_size - 1);
44 }
45
GuardSize()46 size_t GuardSize() {
47 return GetSysPageSize();
48 }
49
50 } // namespace
51
52 // static
Allocate(size_t req_size,int flags)53 PagedMemory PagedMemory::Allocate(size_t req_size, int flags) {
54 size_t rounded_up_size = RoundUpToSysPageSize(req_size);
55 PERFETTO_CHECK(rounded_up_size >= req_size);
56 size_t outer_size = rounded_up_size + GuardSize() * 2;
57 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
58 void* ptr = VirtualAlloc(nullptr, outer_size, MEM_RESERVE, PAGE_NOACCESS);
59 if (!ptr && (flags & kMayFail))
60 return PagedMemory();
61 PERFETTO_CHECK(ptr);
62 char* usable_region = reinterpret_cast<char*>(ptr) + GuardSize();
63 #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
64 void* ptr = mmap(nullptr, outer_size, PROT_READ | PROT_WRITE,
65 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
66 if (ptr == MAP_FAILED && (flags & kMayFail))
67 return PagedMemory();
68 PERFETTO_CHECK(ptr && ptr != MAP_FAILED);
69 char* usable_region = reinterpret_cast<char*>(ptr) + GuardSize();
70 int res = mprotect(ptr, GuardSize(), PROT_NONE);
71 res |= mprotect(usable_region + rounded_up_size, GuardSize(), PROT_NONE);
72 PERFETTO_CHECK(res == 0);
73 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
74
75 auto memory = PagedMemory(usable_region, req_size);
76 #if TRACK_COMMITTED_SIZE()
77 size_t initial_commit = req_size;
78 if (flags & kDontCommit)
79 initial_commit = std::min(initial_commit, kCommitChunkSize);
80 memory.EnsureCommitted(initial_commit);
81 #endif // TRACK_COMMITTED_SIZE()
82 return memory;
83 }
84
PagedMemory()85 PagedMemory::PagedMemory() {}
86
87 // clang-format off
PagedMemory(char * p,size_t size)88 PagedMemory::PagedMemory(char* p, size_t size) : p_(p), size_(size) {
89 ANNOTATE_NEW_BUFFER(p_, size_, committed_size_)
90 }
91
PagedMemory(PagedMemory && other)92 PagedMemory::PagedMemory(PagedMemory&& other) noexcept {
93 *this = other;
94 other.p_ = nullptr;
95 }
96 // clang-format on
97
operator =(PagedMemory && other)98 PagedMemory& PagedMemory::operator=(PagedMemory&& other) {
99 this->~PagedMemory();
100 new (this) PagedMemory(std::move(other));
101 return *this;
102 }
103
~PagedMemory()104 PagedMemory::~PagedMemory() {
105 if (!p_)
106 return;
107 PERFETTO_CHECK(size_);
108 char* start = p_ - GuardSize();
109 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
110 BOOL res = VirtualFree(start, 0, MEM_RELEASE);
111 PERFETTO_CHECK(res != 0);
112 #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
113 const size_t outer_size = RoundUpToSysPageSize(size_) + GuardSize() * 2;
114 int res = munmap(start, outer_size);
115 PERFETTO_CHECK(res == 0);
116 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
117 ANNOTATE_DELETE_BUFFER(p_, size_, committed_size_)
118 }
119
AdviseDontNeed(void * p,size_t size)120 bool PagedMemory::AdviseDontNeed(void* p, size_t size) {
121 PERFETTO_DCHECK(p_);
122 PERFETTO_DCHECK(p >= p_);
123 PERFETTO_DCHECK(static_cast<char*>(p) + size <= p_ + size_);
124 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) || PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
125 // Discarding pages on Windows has more CPU cost than is justified for the
126 // possible memory savings.
127 return false;
128 #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) ||
129 // PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
130 // http://man7.org/linux/man-pages/man2/madvise.2.html
131 int res = madvise(p, size, MADV_DONTNEED);
132 PERFETTO_DCHECK(res == 0);
133 return true;
134 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) ||
135 // PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
136 }
137
138 #if TRACK_COMMITTED_SIZE()
EnsureCommitted(size_t committed_size)139 void PagedMemory::EnsureCommitted(size_t committed_size) {
140 PERFETTO_DCHECK(committed_size > 0u);
141 PERFETTO_DCHECK(committed_size <= size_);
142 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
143 if (committed_size_ >= committed_size)
144 return;
145 // Rounding up.
146 size_t delta = committed_size - committed_size_;
147 size_t num_additional_chunks =
148 (delta + kCommitChunkSize - 1) / kCommitChunkSize;
149 PERFETTO_DCHECK(num_additional_chunks * kCommitChunkSize >= delta);
150 // Don't commit more than the total size.
151 size_t commit_size = std::min(num_additional_chunks * kCommitChunkSize,
152 size_ - committed_size_);
153 void* res = VirtualAlloc(p_ + committed_size_, commit_size, MEM_COMMIT,
154 PAGE_READWRITE);
155 PERFETTO_CHECK(res);
156 ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_,
157 committed_size_ + commit_size)
158 committed_size_ += commit_size;
159 #else // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
160 // mmap commits automatically as needed, so we only track here for ASAN.
161 committed_size = std::max(committed_size_, committed_size);
162 ANNOTATE_CHANGE_SIZE(p_, size_, committed_size_, committed_size)
163 committed_size_ = committed_size;
164 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
165 }
166 #endif // TRACK_COMMITTED_SIZE()
167
168 } // namespace base
169 } // namespace perfetto
170