This commit is contained in:
Siarhei Fedartsou 2024-07-12 22:10:05 +02:00
parent 3096440505
commit 6090387c5e
3 changed files with 11 additions and 21 deletions

View File

@ -2,7 +2,6 @@
#define MEMINFO_HPP #define MEMINFO_HPP
#include "util/log.hpp" #include "util/log.hpp"
#include <cstddef>
#ifndef _WIN32 #ifndef _WIN32
#include <sys/resource.h> #include <sys/resource.h>
@ -11,31 +10,22 @@
namespace osrm::util namespace osrm::util
{ {
inline size_t PeakRAMUsedInBytes() inline void DumpMemoryStats()
{ {
#ifndef _WIN32 #ifndef _WIN32
rusage usage; rusage usage;
getrusage(RUSAGE_SELF, &usage); getrusage(RUSAGE_SELF, &usage);
#ifdef __linux__ #ifdef __linux__
// Under linux, ru.maxrss is in kb // Under linux, ru.maxrss is in kb
return usage.ru_maxrss * 1024; util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss * 1024;
#else // __linux__ #else // __linux__
// Under BSD systems (OSX), it's in bytes // Under BSD systems (OSX), it's in bytes
return usage.ru_maxrss; util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss;
#endif // __linux__ #endif // __linux__
#else // _WIN32
return 0;
#endif // _WIN32
}
inline void DumpMemoryStats()
{
#ifndef _WIN32
util::Log() << "RAM: peak bytes used: " << PeakRAMUsedInBytes();
#else // _WIN32 #else // _WIN32
util::Log() << "RAM: peak bytes used: <not implemented on Windows>"; util::Log() << "RAM: peak bytes used: <not implemented on Windows>";
#endif // _WIN32 #endif // _WIN32
} }
} // namespace osrm::util } // namespace osrm::util
#endif #endif

View File

@ -62,7 +62,7 @@ class MemoryPool
current_chunk_left_bytes_ -= block_size_in_bytes; current_chunk_left_bytes_ -= block_size_in_bytes;
current_chunk_ptr_ += block_size_in_bytes; current_chunk_ptr_ += block_size_in_bytes;
} }
auto ptr = static_cast<T *>(free_list.back()); auto ptr = reinterpret_cast<T *>(free_list.back());
free_list.pop_back(); free_list.pop_back();
return ptr; return ptr;
} }
@ -70,7 +70,8 @@ class MemoryPool
template <typename T> void deallocate(T *p, std::size_t n) noexcept template <typename T> void deallocate(T *p, std::size_t n) noexcept
{ {
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T)); size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
free_lists_[free_list_index].push_back(static_cast<void *>(p)); // NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion)
free_lists_[free_list_index].push_back(reinterpret_cast<void *>(p));
} }
~MemoryPool() ~MemoryPool()
@ -138,6 +139,8 @@ template <typename T> class PoolAllocator
PoolAllocator &operator=(PoolAllocator &&) noexcept = default; PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
private: private:
// using shared_ptr guarantees that memory pool won't be destroyed before all allocators using
// it (important if there are static instances of PoolAllocator)
std::shared_ptr<MemoryPool> pool; std::shared_ptr<MemoryPool> pool;
}; };
template <typename T, typename U> template <typename T, typename U>

View File

@ -12,9 +12,9 @@
#include "osrm/coordinate.hpp" #include "osrm/coordinate.hpp"
#include "osrm/engine_config.hpp" #include "osrm/engine_config.hpp"
#include "osrm/json_container.hpp" #include "osrm/json_container.hpp"
#include "osrm/osrm.hpp" #include "osrm/osrm.hpp"
#include "osrm/status.hpp" #include "osrm/status.hpp"
#include "util/meminfo.hpp"
#include <boost/assert.hpp> #include <boost/assert.hpp>
@ -655,13 +655,10 @@ try
std::cerr << "Unknown benchmark: " << benchmarkToRun << std::endl; std::cerr << "Unknown benchmark: " << benchmarkToRun << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
std::cout << "Peak RAM: " << osrm::util::PeakRAMUsedInBytes() / (1024 * 1024) << "MB"
<< std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }