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

Side by Side Diff: src/zone/accounting-allocator.cc

Issue 2335343007: Pool implementation for zone segments (Closed)
Patch Set: Remove garbage stack 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project 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 "src/zone/accounting-allocator.h" 5 #include "src/zone/accounting-allocator.h"
6 6
7 #include <cstdlib> 7 #include <cstdlib>
8 8
9 #if V8_LIBC_BIONIC 9 #if V8_LIBC_BIONIC
10 #include <malloc.h> // NOLINT 10 #include <malloc.h> // NOLINT
11 #endif 11 #endif
12 12
13 namespace v8 { 13 namespace v8 {
14 namespace internal { 14 namespace internal {
15 15
16 AccountingAllocator::AccountingAllocator() {
17 memory_pressure_level_.SetValue(MemoryPressureLevel::kNone);
18 std::fill(unused_segments_heads_,
19 unused_segments_heads_ +
20 (1 + kMaxSegmentSizePower - kMinSegmentSizePower),
21 nullptr);
22 std::fill(
23 unused_segments_sizes,
24 unused_segments_sizes + (1 + kMaxSegmentSizePower - kMinSegmentSizePower),
25 0);
26 }
27
28 AccountingAllocator::~AccountingAllocator() {
29 ClearPool();
30 delete[] unused_segments_heads_;
31 delete[] unused_segments_sizes;
32 delete unused_segments_mutex_;
33 }
34
35 void AccountingAllocator::MemoryPressureNotification(
36 MemoryPressureLevel level) {
37 memory_pressure_level_.SetValue(level);
38
39 if (level != MemoryPressureLevel::kNone) {
40 ClearPool();
41 }
42 }
43
44 Segment* AccountingAllocator::GetSegment(size_t bytes) {
45 Segment* result = GetSegmentFromPool(bytes);
46 if (result == nullptr) {
47 result = AllocateSegment(bytes);
48 }
49
50 return result;
51 }
52
16 Segment* AccountingAllocator::AllocateSegment(size_t bytes) { 53 Segment* AccountingAllocator::AllocateSegment(size_t bytes) {
17 void* memory = malloc(bytes); 54 void* memory = malloc(bytes);
18 if (memory) { 55 if (memory) {
19 base::AtomicWord current = 56 base::AtomicWord current =
20 base::NoBarrier_AtomicIncrement(&current_memory_usage_, bytes); 57 base::NoBarrier_AtomicIncrement(&current_memory_usage_, bytes);
21 base::AtomicWord max = base::NoBarrier_Load(&max_memory_usage_); 58 base::AtomicWord max = base::NoBarrier_Load(&max_memory_usage_);
22 while (current > max) { 59 while (current > max) {
23 max = base::NoBarrier_CompareAndSwap(&max_memory_usage_, max, current); 60 max = base::NoBarrier_CompareAndSwap(&max_memory_usage_, max, current);
24 } 61 }
25 } 62 }
26 return reinterpret_cast<Segment*>(memory); 63 return reinterpret_cast<Segment*>(memory);
27 } 64 }
28 65
66 void AccountingAllocator::ReturnSegment(Segment* segment) {
67 segment->ZapContents();
68 if (memory_pressure_level_.Value() != MemoryPressureLevel::kNone) {
69 FreeSegment(segment);
70 } else if (!AddSegmentToPool(segment)) {
71 FreeSegment(segment);
72 }
73 }
74
29 void AccountingAllocator::FreeSegment(Segment* memory) { 75 void AccountingAllocator::FreeSegment(Segment* memory) {
30 base::NoBarrier_AtomicIncrement( 76 base::NoBarrier_AtomicIncrement(
31 &current_memory_usage_, -static_cast<base::AtomicWord>(memory->size())); 77 &current_memory_usage_, -static_cast<base::AtomicWord>(memory->size()));
78 memory->ZapHeader();
32 free(memory); 79 free(memory);
33 } 80 }
34 81
35 size_t AccountingAllocator::GetCurrentMemoryUsage() const { 82 size_t AccountingAllocator::GetCurrentMemoryUsage() const {
36 return base::NoBarrier_Load(&current_memory_usage_); 83 return base::NoBarrier_Load(&current_memory_usage_);
37 } 84 }
38 85
39 size_t AccountingAllocator::GetMaxMemoryUsage() const { 86 size_t AccountingAllocator::GetMaxMemoryUsage() const {
40 return base::NoBarrier_Load(&max_memory_usage_); 87 return base::NoBarrier_Load(&max_memory_usage_);
41 } 88 }
42 89
90 Segment* AccountingAllocator::GetSegmentFromPool(size_t requested_size) {
91 if (requested_size > 1 << kMaxSegmentSizePower) {
Toon Verwaest 2016/09/21 11:14:31 (1<<kMaxSeg...) to make it obvious
heimbuef 2016/09/21 11:36:09 Format check wont let that pass.
92 return nullptr;
93 }
94
95 uint8_t power = kMinSegmentSizePower;
96 while (requested_size > static_cast<size_t>(1 << power)) power++;
97
98 power -= kMinSegmentSizePower;
99
100 DCHECK_GE(power, 0);
Toon Verwaest 2016/09/21 11:14:31 LE(0, power)
101
102 unused_segments_mutex_->Lock();
103
104 Segment* segment = unused_segments_heads_[power];
105
106 if (segment) {
Toon Verwaest 2016/09/21 11:14:31 segment != nullptr
107 unused_segments_heads_[power] = segment->next();
108 segment->set_next(nullptr);
109
110 unused_segments_sizes[power]--;
111 unused_segments_size_ -= segment->size();
112 }
113
114 unused_segments_mutex_->Unlock();
115
116 if (segment) {
117 DCHECK_GE(segment->size(), requested_size);
118 // PrintF("%f; 0;-%lu\n", static_cast<double>(clock() - begin) /
Toon Verwaest 2016/09/21 11:14:31 drop debug code
119 // CLOCKS_PER_SEC, segment->size());
120 }
121 return segment;
122 }
123
124 bool AccountingAllocator::AddSegmentToPool(Segment* segment) {
125 size_t size = segment->size();
126
127 if (size >= (1 << (kMaxSegmentSizePower + 1))) {
Toon Verwaest 2016/09/21 11:14:32 I generally prefer the style if (size >= ..) retur
128 return false;
129 }
130
131 if (size < (1 << kMinSegmentSizePower)) {
132 return false;
133 }
134
135 uint8_t power = kMaxSegmentSizePower;
136
137 while (size < static_cast<size_t>(1 << power)) power--;
138
139 power -= kMinSegmentSizePower;
Toon Verwaest 2016/09/21 11:14:31 Add helper method to share with code above
heimbuef 2016/09/21 11:36:09 It's not exactly the same. There can be segments o
140
141 DCHECK_GE(power, 0);
142
143 {
144 base::LockGuard<base::Mutex> lock_guard(unused_segments_mutex_);
Toon Verwaest 2016/09/21 11:14:31 why is this locked differently?
heimbuef 2016/09/21 11:36:09 Done.
145
146 if (unused_segments_sizes[power] >= kMaxSegmentsPerBucket) {
147 return false;
148 }
149
150 segment->set_next(unused_segments_heads_[power]);
151 unused_segments_heads_[power] = segment;
152 unused_segments_size_ += size;
153 unused_segments_sizes[power]++;
154 }
155
156 // PrintF("%f; 0;+%lu\n", static_cast<double>(clock() - begin) /
Toon Verwaest 2016/09/21 11:14:31 drop debug code
157 // CLOCKS_PER_SEC, size);
158
159 return true;
160 }
161
162 void AccountingAllocator::ClearPool() {
163 base::LockGuard<base::Mutex> lock_guard(unused_segments_mutex_);
164
165 for (uint8_t power = 0; power <= kMaxSegmentSizePower - kMinSegmentSizePower;
166 power++) {
167 Segment* current = unused_segments_heads_[power];
168 while (current) {
169 Segment* next = current->next();
170
Toon Verwaest 2016/09/21 11:14:31 drop these newlines
171 FreeSegment(current);
172
173 current = next;
174 }
175 unused_segments_heads_[power] = nullptr;
176 }
177 }
178
43 } // namespace internal 179 } // namespace internal
44 } // namespace v8 180 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698