Track peak RAM in benchmarks

This commit is contained in:
Siarhei Fedartsou 2024-07-12 20:18:32 +02:00
parent 8ae9abaa63
commit 2dcd6af43f
2 changed files with 25 additions and 7 deletions

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

View File

@ -16,8 +16,8 @@
#include "osrm/osrm.hpp"
#include "osrm/status.hpp"
#include "util/meminfo.hpp"
#include <boost/assert.hpp>
#include <boost/optional/optional.hpp>
#include <cstdlib>
#include <exception>
@ -655,10 +655,18 @@ try
std::cerr << "Unknown benchmark: " << benchmarkToRun << std::endl;
return EXIT_FAILURE;
}
std::cout
<< "Peak RAM: "
<< std::setprecision(3)
<< static_cast<double>(osrm::util::PeakRAMUsedInBytes()) / static_cast<double>((1024 * 1024))
<< "MB"
<< std::endl;
return EXIT_SUCCESS;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}