Skip to content

Commit

Permalink
Merge pull request #21159 from vespa-engine/toregge/move-ownership-of…
Browse files Browse the repository at this point in the history
…-memory-allocator-to-attribute-vector

Move ownership of memory allocator to AttributeVector.
  • Loading branch information
geirst authored Feb 11, 2022
2 parents 0be7fcf + 01eef2a commit 94ac841
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
33 changes: 32 additions & 1 deletion searchlib/src/vespa/searchlib/attribute/attributevector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vespa/searchlib/util/file_settings.h>
#include <vespa/searchlib/util/logutil.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/mmap_file_allocator_factory.h>
#include <vespa/vespalib/util/size_literals.h>
#include <thread>

Expand Down Expand Up @@ -107,6 +108,34 @@ AttributeVector::ValueModifier::~ValueModifier() {
}
}

namespace {

bool
allow_paged(const search::attribute::Config& config)
{
if (!config.paged()) {
return false;
}
using Type = search::attribute::BasicType::Type;
if (config.basicType() == Type::REFERENCE || config.basicType() == Type::PREDICATE) {
return false;
}
if (config.basicType() == Type::TENSOR) {
return (!config.tensorType().is_error() && config.tensorType().is_dense());
}
return true;
}

std::unique_ptr<vespalib::alloc::MemoryAllocator>
make_memory_allocator(const vespalib::string& name, const search::attribute::Config& config)
{
if (allow_paged(config)) {
return vespalib::alloc::MmapFileAllocatorFactory::instance().make_memory_allocator(name);
}
return {};
}

}

AttributeVector::AttributeVector(vespalib::stringref baseFileName, const Config &c)
: _baseFileName(baseFileName),
Expand All @@ -124,7 +153,9 @@ AttributeVector::AttributeVector(vespalib::stringref baseFileName, const Config
_compactLidSpaceGeneration(0u),
_hasEnum(false),
_loaded(false),
_isUpdateableInMemoryOnly(attribute::isUpdateableInMemoryOnly(getName(), getConfig()))
_isUpdateableInMemoryOnly(attribute::isUpdateableInMemoryOnly(getName(), getConfig())),
_nextStatUpdateTime(),
_memory_allocator(make_memory_allocator(_baseFileName.getAttributeName(), c))
{
}

Expand Down
2 changes: 2 additions & 0 deletions searchlib/src/vespa/searchlib/attribute/attributevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class AttributeVector : public vespalib::Identifiable,
virtual vespalib::MemoryUsage getEnumStoreValuesMemoryUsage() const;
virtual void populate_address_space_usage(AddressSpaceUsage& usage) const;

const std::shared_ptr<vespalib::alloc::MemoryAllocator>& get_memory_allocator() const noexcept { return _memory_allocator; }
public:
DECLARE_IDENTIFIABLE_ABSTRACT(AttributeVector);
bool isLoaded() const { return _loaded; }
Expand Down Expand Up @@ -584,6 +585,7 @@ class AttributeVector : public vespalib::Identifiable,
bool _loaded;
bool _isUpdateableInMemoryOnly;
vespalib::steady_time _nextStatUpdateTime;
std::shared_ptr<vespalib::alloc::MemoryAllocator> _memory_allocator;

////// Locking strategy interface. only available from the Guards.
/**
Expand Down
13 changes: 1 addition & 12 deletions searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,6 @@ BlobSequenceReader::is_present() {
return true;
}



std::unique_ptr<vespalib::alloc::MemoryAllocator>
make_memory_allocator(const vespalib::string& name, bool swappable)
{
if (swappable) {
return vespalib::alloc::MmapFileAllocatorFactory::instance().make_memory_allocator(name);
}
return {};
}

}

void
Expand Down Expand Up @@ -161,7 +150,7 @@ DenseTensorAttribute::populate_address_space_usage(AddressSpaceUsage& usage) con
DenseTensorAttribute::DenseTensorAttribute(vespalib::stringref baseFileName, const Config& cfg,
const NearestNeighborIndexFactory& index_factory)
: TensorAttribute(baseFileName, cfg, _denseTensorStore),
_denseTensorStore(cfg.tensorType(), make_memory_allocator(getName(), cfg.paged())),
_denseTensorStore(cfg.tensorType(), get_memory_allocator()),
_index()
{
if (cfg.hnsw_index_params().has_value()) {
Expand Down
4 changes: 2 additions & 2 deletions searchlib/src/vespa/searchlib/tensor/dense_tensor_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ DenseTensorStore::TensorSizeCalc::TensorSizeCalc(const ValueType &type)
_aligned_size = my_align(buf_size, alignment);
}

DenseTensorStore::BufferType::BufferType(const TensorSizeCalc &tensorSizeCalc, std::unique_ptr<vespalib::alloc::MemoryAllocator> allocator)
DenseTensorStore::BufferType::BufferType(const TensorSizeCalc &tensorSizeCalc, std::shared_ptr<vespalib::alloc::MemoryAllocator> allocator)
: vespalib::datastore::BufferType<char>(tensorSizeCalc.alignedSize(), MIN_BUFFER_ARRAYS, RefType::offsetSize()),
_allocator(std::move(allocator))
{}
Expand All @@ -65,7 +65,7 @@ DenseTensorStore::BufferType::get_memory_allocator() const
return _allocator.get();
}

DenseTensorStore::DenseTensorStore(const ValueType &type, std::unique_ptr<vespalib::alloc::MemoryAllocator> allocator)
DenseTensorStore::DenseTensorStore(const ValueType &type, std::shared_ptr<vespalib::alloc::MemoryAllocator> allocator)
: TensorStore(_concreteStore),
_concreteStore(),
_tensorSizeCalc(type),
Expand Down
6 changes: 3 additions & 3 deletions searchlib/src/vespa/searchlib/tensor/dense_tensor_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class DenseTensorStore : public TensorStore
class BufferType : public vespalib::datastore::BufferType<char>
{
using CleanContext = vespalib::datastore::BufferType<char>::CleanContext;
std::unique_ptr<vespalib::alloc::MemoryAllocator> _allocator;
std::shared_ptr<vespalib::alloc::MemoryAllocator> _allocator;
public:
BufferType(const TensorSizeCalc &tensorSizeCalc, std::unique_ptr<vespalib::alloc::MemoryAllocator> allocator);
BufferType(const TensorSizeCalc &tensorSizeCalc, std::shared_ptr<vespalib::alloc::MemoryAllocator> allocator);
~BufferType() override;
void cleanHold(void *buffer, size_t offset, ElemCount numElems, CleanContext cleanCtx) override;
const vespalib::alloc::MemoryAllocator* get_memory_allocator() const override;
Expand All @@ -55,7 +55,7 @@ class DenseTensorStore : public TensorStore
TensorStore::EntryRef
setDenseTensor(const TensorType &tensor);
public:
DenseTensorStore(const ValueType &type, std::unique_ptr<vespalib::alloc::MemoryAllocator> allocator);
DenseTensorStore(const ValueType &type, std::shared_ptr<vespalib::alloc::MemoryAllocator> allocator);
~DenseTensorStore() override;

const ValueType &type() const { return _type; }
Expand Down

0 comments on commit 94ac841

Please sign in to comment.