aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/bufferstate.cpp
blob: 704fabffe2550da908080af14f263c5caf1a3939 (plain) (blame)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "bufferstate.h"
#include <vespa/vespalib/util/memory_allocator.h>
#include <limits>
#include <cassert>

using vespalib::alloc::Alloc;
using vespalib::alloc::MemoryAllocator;

namespace vespalib::datastore {

BufferState::FreeListList::~FreeListList()
{
    assert(_head == nullptr);  // Owner should have disabled free lists
}

BufferState::BufferState()
    : _usedElems(0),
      _allocElems(0),
      _deadElems(0u),
      _holdElems(0u),
      _extraUsedBytes(0),
      _extraHoldBytes(0),
      _freeList(),
      _freeListList(nullptr),
      _nextHasFree(nullptr),
      _prevHasFree(nullptr),
      _typeHandler(nullptr),
      _buffer(Alloc::alloc(0, MemoryAllocator::HUGEPAGE_SIZE)),
      _arraySize(0),
      _typeId(0),
      _state(FREE),
      _disableElemHoldList(false),
      _compacting(false)
{
}

BufferState::~BufferState()
{
    assert(_state == FREE);
    assert(_freeListList == nullptr);
    assert(_nextHasFree == nullptr);
    assert(_prevHasFree == nullptr);
    assert(_holdElems == 0);
    assert(isFreeListEmpty());
}

void
BufferState::decHoldElems(size_t value) {
    assert(_holdElems >= value);
    _holdElems -= value;
}

namespace {

struct AllocResult {
    size_t elements;
    size_t bytes;
    AllocResult(size_t elements_, size_t bytes_) : elements(elements_), bytes(bytes_) {}
};

size_t
roundUpToMatchAllocator(size_t sz)
{
    if (sz == 0) {
        return 0;
    }
    // We round up the wanted number of bytes to allocate to match
    // the underlying allocator to ensure little to no waste of allocated memory.
    if (sz < MemoryAllocator::HUGEPAGE_SIZE) {
        // Match heap allocator in vespamalloc.
        return vespalib::roundUp2inN(sz);
    } else {
        // Match mmap allocator.
        return MemoryAllocator::roundUpToHugePages(sz);
    }
}

AllocResult
calcAllocation(uint32_t bufferId,
               BufferTypeBase &typeHandler,
               size_t elementsNeeded,
               bool resizing)
{
    size_t allocArrays = typeHandler.calcArraysToAlloc(bufferId, elementsNeeded, resizing);
    size_t allocElements = allocArrays * typeHandler.getArraySize();
    size_t allocBytes = roundUpToMatchAllocator(allocElements * typeHandler.elementSize());
    size_t maxAllocBytes = typeHandler.getMaxArrays() * typeHandler.getArraySize() * typeHandler.elementSize();
    if (allocBytes > maxAllocBytes) {
        // Ensure that allocated bytes does not exceed the maximum handled by this type.
        allocBytes = maxAllocBytes;
    }
    size_t adjustedAllocElements = (allocBytes / typeHandler.elementSize());
    return AllocResult(adjustedAllocElements, allocBytes);
}

}

void
BufferState::onActive(uint32_t bufferId, uint32_t typeId,
                      BufferTypeBase *typeHandler,
                      size_t elementsNeeded,
                      void *&buffer)
{
    assert(buffer == nullptr);
    assert(_buffer.get() == nullptr);
    assert(_state == FREE);
    assert(_typeHandler == nullptr);
    assert(_allocElems == 0);
    assert(_usedElems == 0);
    assert(_deadElems == 0u);
    assert(_holdElems == 0);
    assert(_extraUsedBytes == 0);
    assert(_extraHoldBytes == 0);
    assert(isFreeListEmpty());
    assert(_nextHasFree == nullptr);
    assert(_prevHasFree == nullptr);
    assert(_freeListList == nullptr || _freeListList->_head != this);

    size_t reservedElements = typeHandler->getReservedElements(bufferId);
    (void) reservedElements;
    AllocResult alloc = calcAllocation(bufferId, *typeHandler, elementsNeeded, false);
    assert(alloc.elements >= reservedElements + elementsNeeded);
    auto allocator = typeHandler->get_memory_allocator();
    _buffer = (allocator != nullptr) ? Alloc::alloc_with_allocator(allocator) : Alloc::alloc(0, MemoryAllocator::HUGEPAGE_SIZE);
    _buffer.create(alloc.bytes).swap(_buffer);
    buffer = _buffer.get();
    assert(buffer != nullptr || alloc.elements == 0u);
    _allocElems = alloc.elements;
    _state = ACTIVE;
    _typeHandler = typeHandler;
    assert(typeId <= std::numeric_limits<uint16_t>::max());
    _typeId = typeId;
    _arraySize = _typeHandler->getArraySize();
    typeHandler->onActive(bufferId, &_usedElems, &_deadElems, buffer);
}


void
BufferState::onHold(uint32_t buffer_id)
{
    assert(_state == ACTIVE);
    assert(_typeHandler != nullptr);
    _state = HOLD;
    _compacting = false;
    assert(_deadElems <= _usedElems);
    assert(_holdElems <= (_usedElems - _deadElems));
    _deadElems = 0;
    _holdElems = _usedElems; // Put everyting on hold
    _typeHandler->onHold(buffer_id, &_usedElems, &_deadElems);
    if ( ! isFreeListEmpty()) {
        removeFromFreeListList();
        FreeList().swap(_freeList);
    }
    assert(_nextHasFree == nullptr);
    assert(_prevHasFree == nullptr);
    assert(_freeListList == nullptr || _freeListList->_head != this);
    setFreeListList(nullptr);
}


void
BufferState::onFree(void *&buffer)
{
    assert(buffer == _buffer.get());
    assert(_state == HOLD);
    assert(_typeHandler != nullptr);
    assert(_deadElems <= _usedElems);
    assert(_holdElems == _usedElems - _deadElems);
    _typeHandler->destroyElements(buffer, _usedElems);
    Alloc::alloc().swap(_buffer);
    _typeHandler->onFree(_usedElems);
    buffer = nullptr;
    _usedElems = 0;
    _allocElems = 0;
    _deadElems = 0u;
    _holdElems = 0u;
    _extraUsedBytes = 0;
    _extraHoldBytes = 0;
    _state = FREE;
    _typeHandler = nullptr;
    _arraySize = 0;
    assert(isFreeListEmpty());
    assert(_nextHasFree == nullptr);
    assert(_prevHasFree == nullptr);
    assert(_freeListList == nullptr || _freeListList->_head != this);
    setFreeListList(nullptr);
    _disableElemHoldList = false;
}


void
BufferState::dropBuffer(uint32_t buffer_id, void *&buffer)
{
    if (_state == FREE) {
        assert(buffer == nullptr);
        return;
    }
    assert(buffer != nullptr || _allocElems == 0);
    if (_state == ACTIVE) {
        onHold(buffer_id);
    }
    if (_state == HOLD) {
        onFree(buffer);
    }
    assert(_state == FREE);
    assert(buffer == nullptr);
}


void
BufferState::setFreeListList(FreeListList *freeListList)
{
    if (_state == FREE && freeListList != nullptr) {
        return;
    }
    if (freeListList == _freeListList) {
        return; // No change
    }
    if (_freeListList != nullptr && ! isFreeListEmpty()) {
        removeFromFreeListList(); // Remove from old free list
    }
    _freeListList = freeListList;
    if ( ! isFreeListEmpty() ) {
        if (freeListList != nullptr) {
            addToFreeListList(); // Changed free list list
        } else {
            FreeList().swap(_freeList);; // Free lists have been disabled
        }
    }
}


void
BufferState::addToFreeListList()
{
    assert(_freeListList != nullptr && _freeListList->_head != this);
    assert(_nextHasFree == nullptr);
    assert(_prevHasFree == nullptr);
    if (_freeListList->_head != nullptr) {
        _nextHasFree = _freeListList->_head;
        _prevHasFree = _nextHasFree->_prevHasFree;
        _nextHasFree->_prevHasFree = this;
        _prevHasFree->_nextHasFree = this;
    } else {
        _nextHasFree = this;
        _prevHasFree = this;
    }
    _freeListList->_head = this;
}


void
BufferState::removeFromFreeListList()
{
    assert(_freeListList != nullptr);
    assert(_nextHasFree != nullptr);
    assert(_prevHasFree != nullptr);
    if (_nextHasFree == this) {
        assert(_prevHasFree == this);
        assert(_freeListList->_head == this);
        _freeListList->_head = nullptr;
    } else {
        assert(_prevHasFree != this);
        _freeListList->_head = _nextHasFree;
        _nextHasFree->_prevHasFree = _prevHasFree;
        _prevHasFree->_nextHasFree = _nextHasFree;
    }
    _nextHasFree = nullptr;
    _prevHasFree = nullptr;
}


void
BufferState::disableElemHoldList()
{
    _disableElemHoldList = true;
}


void
BufferState::fallbackResize(uint32_t bufferId,
                            size_t elementsNeeded,
                            void *&buffer,
                            Alloc &holdBuffer)
{
    assert(_state == ACTIVE);
    assert(_typeHandler != nullptr);
    assert(holdBuffer.get() == nullptr);
    AllocResult alloc = calcAllocation(bufferId, *_typeHandler, elementsNeeded, true);
    assert(alloc.elements >= _usedElems + elementsNeeded);
    assert(alloc.elements > _allocElems);
    Alloc newBuffer = _buffer.create(alloc.bytes);
    _typeHandler->fallbackCopy(newBuffer.get(), buffer, _usedElems);
    holdBuffer.swap(_buffer);
    std::atomic_thread_fence(std::memory_order_release);
    _buffer = std::move(newBuffer);
    buffer = _buffer.get();
    _allocElems = alloc.elements;
    std::atomic_thread_fence(std::memory_order_release);
}

void
BufferState::resume_primary_buffer(uint32_t buffer_id)
{
    _typeHandler->resume_primary_buffer(buffer_id, &_usedElems, &_deadElems);
}

}