Skip to content

Commit

Permalink
Measure peak RAM in benchmarks (#6995)
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou committed Jul 25, 2024
1 parent 43afec3 commit 7802f86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
18 changes: 14 additions & 4 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,22 +11,31 @@
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
}
} // namespace osrm::util

#endif
#endif
8 changes: 7 additions & 1 deletion src/benchmarks/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -655,6 +655,12 @@ 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)
Expand Down

0 comments on commit 7802f86

Please sign in to comment.