-
Notifications
You must be signed in to change notification settings - Fork 479
/
page_allocator.cc
207 lines (188 loc) · 7.33 KB
/
page_allocator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2019 The TCMalloc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tcmalloc/page_allocator.h"
#include <cstddef>
#include <limits>
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "tcmalloc/common.h"
#include "tcmalloc/huge_page_aware_allocator.h"
#include "tcmalloc/internal/config.h"
#include "tcmalloc/internal/environment.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/pages.h"
#include "tcmalloc/parameters.h"
#include "tcmalloc/selsan/selsan.h"
#include "tcmalloc/static_vars.h"
#include "tcmalloc/stats.h"
GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc {
namespace tcmalloc_internal {
using huge_page_allocator_internal::HugePageAwareAllocatorOptions;
PageAllocator::PageAllocator() {
has_cold_impl_ = ColdFeatureActive();
size_t part = 0;
normal_impl_[0] = new (&choices_[part++].hpaa)
HugePageAwareAllocator(HugePageAwareAllocatorOptions{MemoryTag::kNormal});
if (tc_globals.numa_topology().numa_aware()) {
normal_impl_[1] = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kNormalP1});
}
sampled_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kSampled});
if (selsan::IsEnabled()) {
selsan_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kSelSan});
}
if (has_cold_impl_) {
cold_impl_ = new (&choices_[part++].hpaa)
HugePageAwareAllocator(HugePageAwareAllocatorOptions{MemoryTag::kCold});
} else {
cold_impl_ = normal_impl_[0];
}
alg_ = HPAA;
TC_CHECK_LE(part, ABSL_ARRAYSIZE(choices_));
}
void PageAllocator::ShrinkToUsageLimit(Length n) {
BackingStats s = stats();
const size_t backed =
s.system_bytes - s.unmapped_bytes + tc_globals.metadata_bytes();
// New high water marks should be rare.
if (ABSL_PREDICT_FALSE(backed > peak_backed_bytes_)) {
peak_backed_bytes_ = backed;
// This estimate may skew slightly low (and overestimate realized
// fragmentation), as we allocate successfully from the page heap before
// updating the sampled object list.
//
// TODO(ckennelly): The correction for n overestimates for many-object
// spans from the CentralFreeList, but those are typically a single page so
// the error in absolute terms is minimal.
peak_sampled_application_bytes_ =
tc_globals.sampled_objects_size_.value() + n.in_bytes();
}
// TODO(ckennelly): Consider updating peak_sampled_application_bytes_ if
// backed == peak_backed_bytes_ but application usage has gone up. This can
// occur if we allocate space for many objects preemptively and only later
// sample them (incrementing sampled_objects_size_).
if (limits_[kSoft] == std::numeric_limits<size_t>::max()) {
// Limits are not set.
return;
}
if (backed <= limits_[kSoft]) {
// We're already fine.
return;
}
++limit_hits_[kSoft];
if (limits_[kHard] < backed) ++limit_hits_[kHard];
const size_t overage = backed - limits_[kSoft];
const Length pages = LengthFromBytes(overage + kPageSize - 1);
if (ShrinkHardBy(pages, kSoft)) {
++successful_shrinks_after_limit_hit_[kSoft];
return;
}
// We're still not below limit.
if (limits_[kHard] < std::numeric_limits<size_t>::max()) {
// Recompute how many pages we still need to release.
BackingStats s = stats();
const size_t backed =
s.system_bytes - s.unmapped_bytes + tc_globals.metadata_bytes();
if (backed <= limits_[kHard]) {
// We're already fine in terms of hard limit.
return;
}
const size_t overage = backed - limits_[kHard];
const Length pages = LengthFromBytes(overage + kPageSize - 1);
if (ShrinkHardBy(pages, kHard)) {
++successful_shrinks_after_limit_hit_[kHard];
TC_ASSERT_EQ(successful_shrinks_after_limit_hit_[kHard],
limit_hits_[kHard]);
return;
}
const size_t hard_limit = limits_[kHard];
limits_[kHard] = std::numeric_limits<size_t>::max();
TC_BUG(
"Hit hard tcmalloc heap limit of %v "
"(e.g. --tcmalloc_heap_size_hard_limit). "
"Aborting.\nIt was most likely set to catch "
"allocations that would crash the process anyway. "
,
hard_limit);
}
// Print logs once.
static bool warned = false;
if (warned) return;
warned = true;
TC_LOG("Couldn't respect usage limit of %v and OOM is likely to follow.",
limits_[kSoft]);
}
bool PageAllocator::ShrinkHardBy(Length pages, LimitKind limit_kind) {
const PageReleaseReason release_reason =
limit_kind == kHard ? PageReleaseReason::kHardLimitExceeded
: PageReleaseReason::kSoftLimitExceeded;
Length ret = ReleaseAtLeastNPages(pages, release_reason);
if (alg_ == HPAA) {
if (pages <= ret) {
// We released target amount.
return true;
}
// At this point, we have no choice but to break up hugepages.
// However, if the client has turned off subrelease, and is using hard
// limits, then respect desire to do no subrelease ever.
if (limit_kind == kHard && !Parameters::hpaa_subrelease()) return false;
static bool warned_hugepages = false;
if (!warned_hugepages) {
const size_t limit = limits_[limit_kind];
TC_LOG(
"Couldn't respect usage limit of %v without breaking hugepages - "
"performance will drop",
limit);
warned_hugepages = true;
}
if (has_cold_impl_) {
ret += static_cast<HugePageAwareAllocator*>(cold_impl_)
->ReleaseAtLeastNPagesBreakingHugepages(pages - ret,
release_reason);
if (ret >= pages) {
return true;
}
}
if (selsan_impl_) {
ret += static_cast<HugePageAwareAllocator*>(selsan_impl_)
->ReleaseAtLeastNPagesBreakingHugepages(pages - ret,
release_reason);
if (ret >= pages) {
return true;
}
}
for (int partition = 0; partition < active_numa_partitions(); partition++) {
ret += static_cast<HugePageAwareAllocator*>(normal_impl_[partition])
->ReleaseAtLeastNPagesBreakingHugepages(pages - ret,
release_reason);
if (ret >= pages) {
return true;
}
}
ret += static_cast<HugePageAwareAllocator*>(sampled_impl_)
->ReleaseAtLeastNPagesBreakingHugepages(pages - ret,
release_reason);
}
// Return "true", if we got back under the limit.
return (pages <= ret);
}
size_t PageAllocator::active_numa_partitions() const {
return tc_globals.numa_topology().active_partitions();
}
} // namespace tcmalloc_internal
} // namespace tcmalloc
GOOGLE_MALLOC_SECTION_END