Skip to content

Commit 7802f86

Browse files
Measure peak RAM in benchmarks (#6995)
1 parent 43afec3 commit 7802f86

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

include/util/meminfo.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define MEMINFO_HPP
33

44
#include "util/log.hpp"
5+
#include <cstddef>
56

67
#ifndef _WIN32
78
#include <sys/resource.h>
@@ -10,22 +11,31 @@
1011
namespace osrm::util
1112
{
1213

13-
inline void DumpMemoryStats()
14+
inline size_t PeakRAMUsedInBytes()
1415
{
1516
#ifndef _WIN32
1617
rusage usage;
1718
getrusage(RUSAGE_SELF, &usage);
1819
#ifdef __linux__
1920
// Under linux, ru.maxrss is in kb
20-
util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss * 1024;
21+
return usage.ru_maxrss * 1024;
2122
#else // __linux__
2223
// Under BSD systems (OSX), it's in bytes
23-
util::Log() << "RAM: peak bytes used: " << usage.ru_maxrss;
24+
return usage.ru_maxrss;
2425
#endif // __linux__
26+
#else // _WIN32
27+
return 0;
28+
#endif // _WIN32
29+
}
30+
31+
inline void DumpMemoryStats()
32+
{
33+
#ifndef _WIN32
34+
util::Log() << "RAM: peak bytes used: " << PeakRAMUsedInBytes();
2535
#else // _WIN32
2636
util::Log() << "RAM: peak bytes used: <not implemented on Windows>";
2737
#endif // _WIN32
2838
}
2939
} // namespace osrm::util
3040

31-
#endif
41+
#endif

src/benchmarks/bench.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "osrm/osrm.hpp"
1717
#include "osrm/status.hpp"
1818

19+
#include "util/meminfo.hpp"
1920
#include <boost/assert.hpp>
20-
2121
#include <boost/optional/optional.hpp>
2222
#include <cstdlib>
2323
#include <exception>
@@ -655,6 +655,12 @@ try
655655
std::cerr << "Unknown benchmark: " << benchmarkToRun << std::endl;
656656
return EXIT_FAILURE;
657657
}
658+
659+
std::cout << "Peak RAM: " << std::setprecision(3)
660+
<< static_cast<double>(osrm::util::PeakRAMUsedInBytes()) /
661+
static_cast<double>((1024 * 1024))
662+
<< "MB" << std::endl;
663+
658664
return EXIT_SUCCESS;
659665
}
660666
catch (const std::exception &e)

0 commit comments

Comments
 (0)