summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@broadpark.no>2021-02-11 15:52:12 +0100
committerTor Egge <Tor.Egge@broadpark.no>2021-02-11 15:52:12 +0100
commit2aaad6e900c1761000f5a2c075577bcbad8cfa4f (patch)
tree6f553b83d851fa8329836679a2fe02b17a2b7df5 /vespalib
parent4cb8c98d334e993f6c9187909324768e658e9f76 (diff)
Factor out function for rounding up to page size.
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/vespa/vespalib/util/CMakeLists.txt1
-rw-r--r--vespalib/src/vespa/vespalib/util/alloc.cpp12
-rw-r--r--vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp13
-rw-r--r--vespalib/src/vespa/vespalib/util/round_up_to_page_size.cpp19
-rw-r--r--vespalib/src/vespa/vespalib/util/round_up_to_page_size.h14
5 files changed, 39 insertions, 20 deletions
diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt
index bee95ad22dc..fcac7cde412 100644
--- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt
+++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt
@@ -40,6 +40,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT
reusable_set.cpp
reusable_set_handle.cpp
reusable_set_pool.cpp
+ round_up_to_page_size.cpp
runnable.cpp
runnable_pair.cpp
sequence.cpp
diff --git a/vespalib/src/vespa/vespalib/util/alloc.cpp b/vespalib/src/vespa/vespalib/util/alloc.cpp
index ecd0bcfd5e6..4defbf6df8d 100644
--- a/vespalib/src/vespa/vespalib/util/alloc.cpp
+++ b/vespalib/src/vespa/vespalib/util/alloc.cpp
@@ -1,5 +1,6 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "alloc.h"
+#include "round_up_to_page_size.h"
#include <sys/mman.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/exceptions.h>
@@ -10,7 +11,6 @@
#include <cassert>
#include <mutex>
#include <vespa/fastos/file.h>
-#include <unistd.h>
#include <vespa/log/log.h>
LOG_SETUP(".vespalib.alloc");
@@ -22,17 +22,11 @@ namespace {
volatile bool _G_hasHugePageFailureJustHappened(false);
bool _G_SilenceCoreOnOOM(false);
int _G_HugeFlags = 0;
-const size_t _G_pageSize = getpagesize();
size_t _G_MMapLogLimit = std::numeric_limits<size_t>::max();
size_t _G_MMapNoCoreLimit = std::numeric_limits<size_t>::max();
std::mutex _G_lock;
std::atomic<size_t> _G_mmapCount(0);
-size_t
-roundUp2PageSize(size_t sz) {
- return (sz + (_G_pageSize - 1)) & ~(_G_pageSize - 1);
-}
-
struct MMapInfo {
MMapInfo() :
_id(0ul),
@@ -319,7 +313,7 @@ MemoryAllocator::PtrAndSize
MMapAllocator::salloc(size_t sz, void * wantedAddress)
{
void * buf(nullptr);
- sz = roundUp2PageSize(sz);
+ sz = round_up_to_page_size(sz);
if (sz > 0) {
const int flags(MAP_ANON | MAP_PRIVATE);
const int prot(PROT_READ | PROT_WRITE);
@@ -372,7 +366,7 @@ MMapAllocator::salloc(size_t sz, void * wantedAddress)
size_t
MMapAllocator::sresize_inplace(PtrAndSize current, size_t newSize) {
- newSize = roundUp2PageSize(newSize);
+ newSize = round_up_to_page_size(newSize);
if (newSize > current.second) {
return extend_inplace(current, newSize);
} else if (newSize < current.second) {
diff --git a/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp b/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp
index f410a1f6362..3d70d0f5e19 100644
--- a/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp
+++ b/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp
@@ -1,6 +1,7 @@
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "mmap_file_allocator.h"
+#include "round_up_to_page_size.h"
#include <vespa/vespalib/stllike/hash_map.hpp>
#include <fcntl.h>
#include <sys/mman.h>
@@ -8,16 +9,6 @@
namespace vespalib::alloc {
-namespace {
-
-const size_t mmap_alignment = getpagesize();
-
-size_t round_to_mmap_alignment(size_t size) {
- return ((size + (mmap_alignment - 1)) & ~(mmap_alignment - 1));
-}
-
-}
-
MmapFileAllocator::MmapFileAllocator(const vespalib::string& dir_name)
: _dir_name(dir_name),
_file(_dir_name + "/swapfile"),
@@ -42,7 +33,7 @@ MmapFileAllocator::alloc(size_t sz) const
return PtrAndSize(nullptr, 0); // empty allocation
}
uint64_t offset = _end_offset;
- sz = round_to_mmap_alignment(sz);
+ sz = round_up_to_page_size(sz);
_end_offset += sz;
_file.resize(_end_offset);
void *buf = mmap(nullptr, sz,
diff --git a/vespalib/src/vespa/vespalib/util/round_up_to_page_size.cpp b/vespalib/src/vespa/vespalib/util/round_up_to_page_size.cpp
new file mode 100644
index 00000000000..a2d25decfcb
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/round_up_to_page_size.cpp
@@ -0,0 +1,19 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "round_up_to_page_size.h"
+#include <unistd.h>
+
+namespace vespalib {
+
+namespace {
+
+const size_t page_size = getpagesize();
+
+}
+
+size_t round_up_to_page_size(size_t size)
+{
+ return ((size + (page_size - 1)) & ~(page_size - 1));
+}
+
+}
diff --git a/vespalib/src/vespa/vespalib/util/round_up_to_page_size.h b/vespalib/src/vespa/vespalib/util/round_up_to_page_size.h
new file mode 100644
index 00000000000..103d3a35f9e
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/round_up_to_page_size.h
@@ -0,0 +1,14 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#pragma once
+
+#include <cstddef>
+
+namespace vespalib {
+
+/*
+ * Return sz rounded up to a multiple of page size.
+ */
+size_t round_up_to_page_size(size_t sz);
+
+}