Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou committed Jul 11, 2024
1 parent fb8182a commit 270f187
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
16 changes: 13 additions & 3 deletions include/util/meminfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MEMINFO_HPP

#include "util/log.hpp"
#include <cstddef>

#ifndef _WIN32
#include <sys/resource.h>
Expand All @@ -10,18 +11,27 @@
namespace osrm::util
{

inline void DumpMemoryStats()
inline size_t PeakRAMUsedInBytes()
{
#ifndef _WIN32
rusage usage;
getrusage(RUSAGE_SELF, &usage);
#ifdef __linux__
// Under linux, ru.maxrss is in kb
util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss * 1024;
return usage.ru_maxrss * 1024;
#else // __linux__
// Under BSD systems (OSX), it's in bytes
util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss;
return usage.ru_maxrss;
#endif // __linux__
#else // _WIN32
return 0;
#endif // _WIN32
}

inline void DumpMemoryStats()
{
#ifndef _WIN32
util::Log() << "RAM: peak bytes used: " << PeakRAMUsedInBytes();
#else // _WIN32
util::Log() << "RAM: peak bytes used: <not implemented on Windows>";
#endif // _WIN32
Expand Down
29 changes: 10 additions & 19 deletions include/util/pool_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
namespace osrm::util
{

#if 1

template <typename T, size_t MinItemsInBlock = 1024>
template <typename T>
class PoolAllocator;

template <typename T, size_t MinItemsInBlock = 1024>
template <typename T>
class MemoryManager
{
private:
constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024;
public:
static std::shared_ptr<MemoryManager> instance()
{
Expand Down Expand Up @@ -82,7 +82,7 @@ class MemoryManager

void allocate_block(size_t items_in_block)
{
items_in_block = std::max(items_in_block, MinItemsInBlock);
items_in_block = std::max(items_in_block, MIN_ITEMS_IN_BLOCK);

size_t block_size = items_in_block * sizeof(T);
T *block = static_cast<T *>(std::malloc(block_size));
Expand All @@ -104,21 +104,21 @@ class MemoryManager
size_t total_allocated_ = 0;
};

template <typename T, size_t MinItemsInBlock>
template <typename T>
class PoolAllocator
{
public:
using value_type = T;

PoolAllocator() noexcept : pool(MemoryManager<T, MinItemsInBlock>::instance()) {};
PoolAllocator() noexcept : pool(MemoryManager<T>::instance()) {};

template <typename U>
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager<T, MinItemsInBlock>::instance()) {}
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager<T>::instance()) {}

template <typename U>
struct rebind
{
using other = PoolAllocator<U, MinItemsInBlock>;
using other = PoolAllocator<U>;
};

T *allocate(std::size_t n)
Expand All @@ -139,13 +139,7 @@ class PoolAllocator
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;

private:
std::shared_ptr<MemoryManager<T, MinItemsInBlock>> pool;

static size_t get_next_power_of_two_exponent(size_t n)
{
BOOST_ASSERT(n > 0);
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
}
std::shared_ptr<MemoryManager<T>> pool;
};

template <typename T, typename U>
Expand All @@ -160,7 +154,4 @@ bool operator!=(const PoolAllocator<T> &, const PoolAllocator<U> &)
return false;
}

#else
template <typename T> using PoolAllocator = std::allocator<T>;
#endif
} // namespace osrm::util
8 changes: 2 additions & 6 deletions include/util/query_heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ template <typename NodeID,
typename Key,
typename Weight,
typename Data,
typename IndexStorage = ArrayStorage<NodeID, NodeID>,
bool ThreadLocal = true>
typename IndexStorage = ArrayStorage<NodeID, NodeID>>
class QueryHeap
{
private:
Expand All @@ -214,16 +213,13 @@ class QueryHeap
}
};

using AllocatorType = typename std::conditional<ThreadLocal,
PoolAllocator<HeapData>,
std::allocator<HeapData>>::type;


using HeapContainer = boost::heap::d_ary_heap<HeapData,
boost::heap::arity<4>,
boost::heap::mutable_<true>,
boost::heap::compare<std::greater<HeapData>>,
boost::heap::allocator<AllocatorType>>;
boost::heap::allocator<PoolAllocator<HeapData>>>;
using HeapHandle = typename HeapContainer::handle_type;

public:
Expand Down
4 changes: 3 additions & 1 deletion src/benchmarks/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "osrm/coordinate.hpp"
#include "osrm/engine_config.hpp"
#include "osrm/json_container.hpp"

#include "util/meminfo.hpp"
#include "osrm/osrm.hpp"
#include "osrm/status.hpp"

Expand Down Expand Up @@ -655,6 +655,8 @@ try
std::cerr << "Unknown benchmark: " << benchmarkToRun << std::endl;
return EXIT_FAILURE;
}

std::cerr << "Peak RAM: " << osrm::util::PeakRAMUsedInBytes() / (1024 * 1024) << "MB" << std::endl;
return EXIT_SUCCESS;
}
catch (const std::exception &e)
Expand Down
2 changes: 1 addition & 1 deletion test/data/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ benchmark: data $(DATA_NAME).requests
checksum:
$(MD5SUM) $(DATA_NAME).osm.pbf $(DATA_NAME).poly > data.md5sum

.PHONY: clean checksum data
.PHONY: clean checksum benchmark data

0 comments on commit 270f187

Please sign in to comment.