@@ -47,65 +47,36 @@ MallocAllocator::~MallocAllocator() {
47
47
}
48
48
49
49
bool MallocAllocator::allocateNonContiguousWithoutRetry (
50
- MachinePageCount numPages,
51
- Allocation& out,
52
- ReservationCallback reservationCB,
53
- MachinePageCount minSizeClass) {
54
- const uint64_t freedBytes = freeNonContiguous (out);
55
- if (numPages == 0 ) {
56
- if (freedBytes != 0 && reservationCB != nullptr ) {
57
- reservationCB (freedBytes, false );
58
- }
50
+ const SizeMix& sizeMix,
51
+ Allocation& out) {
52
+ freeNonContiguous (out);
53
+ if (sizeMix.totalPages == 0 ) {
59
54
return true ;
60
55
}
61
- const SizeMix mix = allocationSize (numPages, minSizeClass);
62
- const auto totalBytes = AllocationTraits::pageBytes (mix.totalPages );
56
+ const auto totalBytes = AllocationTraits::pageBytes (sizeMix.totalPages );
63
57
if (testingHasInjectedFailure (InjectedFailure::kCap ) ||
64
58
!incrementUsage (totalBytes)) {
65
- if (freedBytes != 0 && reservationCB != nullptr ) {
66
- reservationCB (freedBytes, false );
67
- }
68
59
const auto errorMsg = fmt::format (
69
- " Exceeded memory allocator limit when allocating {} new pages for "
70
- " total allocation of {} pages, the memory allocator capacity is {}" ,
71
- mix.totalPages ,
72
- numPages,
60
+ " Exceeded memory allocator limit when allocating {} new pages"
61
+ " , the memory allocator capacity is {}" ,
62
+ sizeMix.totalPages ,
73
63
succinctBytes (capacity_));
74
64
VELOX_MEM_LOG_EVERY_MS (WARNING, 1000 ) << errorMsg;
75
65
setAllocatorFailureMessage (errorMsg);
76
66
return false ;
77
67
}
78
68
79
- uint64_t bytesToAllocate = 0 ;
80
- if (reservationCB != nullptr ) {
81
- bytesToAllocate = AllocationTraits::pageBytes (mix.totalPages ) - freedBytes;
82
- try {
83
- reservationCB (bytesToAllocate, true );
84
- } catch (std::exception &) {
85
- VELOX_MEM_LOG (WARNING)
86
- << " Failed to reserve " << succinctBytes (bytesToAllocate)
87
- << " for non-contiguous allocation of " << numPages
88
- << " pages, then release " << succinctBytes (freedBytes)
89
- << " from the old allocation" ;
90
- // If the new memory reservation fails, we need to release the memory
91
- // reservation of the freed memory of previously allocation.
92
- reservationCB (freedBytes, false );
93
- decrementUsage (totalBytes);
94
- std::rethrow_exception (std::current_exception ());
95
- }
96
- }
97
-
98
69
std::vector<void *> buffers;
99
- buffers.reserve (mix .numSizes );
100
- for (int32_t i = 0 ; i < mix .numSizes ; ++i) {
70
+ buffers.reserve (sizeMix .numSizes );
71
+ for (int32_t i = 0 ; i < sizeMix .numSizes ; ++i) {
101
72
MachinePageCount numSizeClassPages =
102
- mix .sizeCounts [i] * sizeClassSizes_[mix .sizeIndices [i]];
73
+ sizeMix .sizeCounts [i] * sizeClassSizes_[sizeMix .sizeIndices [i]];
103
74
void * ptr = nullptr ;
104
75
// Trigger allocation failure by skipping malloc
105
76
if (!testingHasInjectedFailure (InjectedFailure::kAllocate )) {
106
77
stats_.recordAllocate (
107
- AllocationTraits::pageBytes (sizeClassSizes_[mix .sizeIndices [i]]),
108
- mix .sizeCounts [i],
78
+ AllocationTraits::pageBytes (sizeClassSizes_[sizeMix .sizeIndices [i]]),
79
+ sizeMix .sizeCounts [i],
109
80
[&]() {
110
81
ptr = ::malloc (
111
82
AllocationTraits::pageBytes (numSizeClassPages)); // NOLINT
@@ -117,7 +88,7 @@ bool MallocAllocator::allocateNonContiguousWithoutRetry(
117
88
" Malloc failed to allocate {} of memory while allocating for "
118
89
" non-contiguous allocation of {} pages" ,
119
90
succinctBytes (AllocationTraits::pageBytes (numSizeClassPages)),
120
- numPages );
91
+ sizeMix. totalPages );
121
92
VELOX_MEM_LOG (WARNING) << errorMsg;
122
93
setAllocatorFailureMessage (errorMsg);
123
94
break ;
@@ -126,40 +97,33 @@ bool MallocAllocator::allocateNonContiguousWithoutRetry(
126
97
out.append (reinterpret_cast <uint8_t *>(ptr), numSizeClassPages); // NOLINT
127
98
}
128
99
129
- if (buffers.size () != mix .numSizes ) {
100
+ if (buffers.size () != sizeMix .numSizes ) {
130
101
// Failed to allocate memory using malloc. Free any malloced pages and
131
102
// return false.
132
103
for (auto * buffer : buffers) {
133
104
::free (buffer);
134
105
}
135
106
out.clear ();
136
- if (reservationCB != nullptr ) {
137
- VELOX_MEM_LOG (WARNING)
138
- << " Failed to allocate memory for non-contiguous allocation of "
139
- << numPages << " pages, then release "
140
- << succinctBytes (bytesToAllocate + freedBytes)
141
- << " of memory reservation including the old allocation" ;
142
- reservationCB (bytesToAllocate + freedBytes, false );
143
- }
107
+ VELOX_MEM_LOG (WARNING)
108
+ << " Failed to allocate memory for non-contiguous allocation of "
109
+ << sizeMix.totalPages << " pages" ;
144
110
decrementUsage (totalBytes);
145
111
return false ;
146
112
}
147
113
148
114
// Successfully allocated all pages.
149
- numAllocated_.fetch_add (mix .totalPages );
115
+ numAllocated_.fetch_add (sizeMix .totalPages );
150
116
return true ;
151
117
}
152
118
153
119
bool MallocAllocator::allocateContiguousWithoutRetry (
154
120
MachinePageCount numPages,
155
121
Allocation* collateral,
156
122
ContiguousAllocation& allocation,
157
- ReservationCallback reservationCB,
158
123
MachinePageCount maxPages) {
159
124
bool result;
160
125
stats_.recordAllocate (AllocationTraits::pageBytes (numPages), 1 , [&]() {
161
- result = allocateContiguousImpl (
162
- numPages, collateral, allocation, reservationCB, maxPages);
126
+ result = allocateContiguousImpl (numPages, collateral, allocation, maxPages);
163
127
});
164
128
return result;
165
129
}
@@ -168,7 +132,6 @@ bool MallocAllocator::allocateContiguousImpl(
168
132
MachinePageCount numPages,
169
133
Allocation* collateral,
170
134
ContiguousAllocation& allocation,
171
- ReservationCallback reservationCB,
172
135
MachinePageCount maxPages) {
173
136
if (maxPages == 0 ) {
174
137
maxPages = numPages;
@@ -192,23 +155,13 @@ bool MallocAllocator::allocateContiguousImpl(
192
155
decrementUsage (AllocationTraits::pageBytes (numContiguousCollateralPages));
193
156
allocation.clear ();
194
157
}
195
- const auto totalCollateralPages =
196
- numCollateralPages + numContiguousCollateralPages;
197
- const auto totalCollateralBytes =
198
- AllocationTraits::pageBytes (totalCollateralPages);
199
158
if (numPages == 0 ) {
200
- if (totalCollateralBytes != 0 && reservationCB != nullptr ) {
201
- reservationCB (totalCollateralBytes, false );
202
- }
203
159
return true ;
204
160
}
205
161
206
162
const auto totalBytes = AllocationTraits::pageBytes (numPages);
207
163
if (testingHasInjectedFailure (InjectedFailure::kCap ) ||
208
164
!incrementUsage (totalBytes)) {
209
- if (totalCollateralBytes != 0 && reservationCB != nullptr ) {
210
- reservationCB (totalCollateralBytes, false );
211
- }
212
165
const auto errorMsg = fmt::format (
213
166
" Exceeded memory allocator limit when allocating {} new pages, the "
214
167
" memory allocator capacity is {}" ,
@@ -218,23 +171,6 @@ bool MallocAllocator::allocateContiguousImpl(
218
171
VELOX_MEM_LOG_EVERY_MS (WARNING, 1000 ) << errorMsg;
219
172
return false ;
220
173
}
221
- const int64_t numNeededPages = numPages - totalCollateralPages;
222
- if (reservationCB != nullptr ) {
223
- try {
224
- reservationCB (AllocationTraits::pageBytes (numNeededPages), true );
225
- } catch (std::exception &) {
226
- // If the new memory reservation fails, we need to release the memory
227
- // reservation of the freed contiguous and non-contiguous memory.
228
- VELOX_MEM_LOG (WARNING)
229
- << " Failed to reserve " << AllocationTraits::pageBytes (numNeededPages)
230
- << " bytes for contiguous allocation of " << numPages
231
- << " pages, then release " << succinctBytes (totalCollateralBytes)
232
- << " from the old allocations" ;
233
- reservationCB (totalCollateralBytes, false );
234
- decrementUsage (totalBytes);
235
- std::rethrow_exception (std::current_exception ());
236
- }
237
- }
238
174
numAllocated_.fetch_add (numPages);
239
175
numMapped_.fetch_add (numPages);
240
176
void * data = ::mmap (
@@ -300,15 +236,7 @@ void MallocAllocator::freeContiguousImpl(ContiguousAllocation& allocation) {
300
236
301
237
bool MallocAllocator::growContiguousWithoutRetry (
302
238
MachinePageCount increment,
303
- ContiguousAllocation& allocation,
304
- ReservationCallback reservationCB) {
305
- VELOX_CHECK_LE (
306
- allocation.size () + increment * AllocationTraits::kPageSize ,
307
- allocation.maxSize ());
308
- if (reservationCB != nullptr ) {
309
- // May throw. If does, there is nothing to revert.
310
- reservationCB (AllocationTraits::pageBytes (increment), true );
311
- }
239
+ ContiguousAllocation& allocation) {
312
240
if (!incrementUsage (AllocationTraits::pageBytes (increment))) {
313
241
const auto errorMsg = fmt::format (
314
242
" Exceeded memory allocator limit when allocating {} new pages for "
@@ -318,9 +246,6 @@ bool MallocAllocator::growContiguousWithoutRetry(
318
246
succinctBytes (capacity_));
319
247
setAllocatorFailureMessage (errorMsg);
320
248
VELOX_MEM_LOG_EVERY_MS (WARNING, 1000 ) << errorMsg;
321
- if (reservationCB != nullptr ) {
322
- reservationCB (AllocationTraits::pageBytes (increment), false );
323
- }
324
249
return false ;
325
250
}
326
251
numAllocated_ += increment;
0 commit comments