This commit is contained in:
Siarhei Fedartsou
2024-07-11 22:32:41 +02:00
parent fb8182a10e
commit 270f187e2a
5 changed files with 29 additions and 30 deletions
+13 -3
View File
@@ -2,6 +2,7 @@
#define MEMINFO_HPP
#include "util/log.hpp"
#include <cstddef>
#ifndef _WIN32
#include <sys/resource.h>
@@ -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
+10 -19
View File
@@ -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()
{
@@ -82,7 +82,7 @@ private:
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));
@@ -104,21 +104,21 @@ private:
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)
@@ -139,13 +139,7 @@ public:
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>
@@ -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
+2 -6
View File
@@ -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:
@@ -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: