diff options
Diffstat (limited to 'staging_vespalib/src/vespa/vespalib/util')
3 files changed, 53 insertions, 1 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt index 2b52e9e167f..e69dd36d6f5 100644 --- a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -5,6 +5,7 @@ vespa_add_library(staging_vespalib_vespalib_util OBJECT bits.cpp clock.cpp crc.cpp + document_runnable.cpp doom.cpp foregroundtaskexecutor.cpp growablebytebuffer.cpp @@ -12,10 +13,10 @@ vespa_add_library(staging_vespalib_vespalib_util OBJECT jsonexception.cpp jsonstream.cpp jsonwriter.cpp + malloc_mmap_guard.cpp process_memory_stats.cpp programoptions.cpp programoptions_testutils.cpp - document_runnable.cpp rusage.cpp sequencedtaskexecutor.cpp sequencedtaskexecutorobserver.cpp diff --git a/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp new file mode 100644 index 00000000000..0ced160fda2 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp @@ -0,0 +1,23 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "malloc_mmap_guard.h" +#include <vespa/vespalib/util/size_literals.h> +#include <malloc.h> +#include <limits> +#include <cassert> + +namespace vespalib { + +MallocMmapGuard::MallocMmapGuard(size_t mmapLimit) : + _threadId(std::this_thread::get_id()) +{ + int limit = mmapLimit <= std::numeric_limits<int>::max() ? mmapLimit : std::numeric_limits<int>::max(); + mallopt(M_MMAP_THRESHOLD, limit); +} + +MallocMmapGuard::~MallocMmapGuard() +{ + assert(_threadId == std::this_thread::get_id()); + mallopt(M_MMAP_THRESHOLD, 1_Gi); +} + +} diff --git a/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h new file mode 100644 index 00000000000..03e6d38c03c --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h @@ -0,0 +1,28 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <thread> + +namespace vespalib { + +/** + * Provides a hint to malloc implementation that all allocations in the scope of this guard + * will use mmap directly for allocation larger than the given limit. + * NB !! Note that guards can not be nested. Intention is to use around third party libraries where + * you do not control allocation yourself. + * The effect is implementation dependent. vespamalloc applies this only for the calling thread. + **/ +class MallocMmapGuard +{ +public: + MallocMmapGuard(size_t mmapLimit); + MallocMmapGuard(const MallocMmapGuard &) = delete; + MallocMmapGuard & operator=(const MallocMmapGuard &) = delete; + MallocMmapGuard(MallocMmapGuard &&) = delete; + MallocMmapGuard & operator=(MallocMmapGuard &&) = delete; + ~MallocMmapGuard(); +private: + std::thread::id _threadId; +}; + +} // namespace vespalib |