Skip to content

Commit b6e2174

Browse files
committed
Add peak mem diff for bapp
1 parent 91a5518 commit b6e2174

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

samples/cpp/benchmark_app/main.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@
3737
#include "utils.hpp"
3838
// clang-format on
3939

40+
#if defined _WIN32
41+
42+
#include <windows.h>
43+
44+
#include <psapi.h>
45+
46+
int64_t getPeakMemoryUsage() {
47+
PROCESS_MEMORY_COUNTERS memCounters;
48+
GetProcessMemoryInfo(GetCurrentProcess(), &memCounters, sizeof(memCounters));
49+
return memCounters.PeakWorkingSetSize / 1000;
50+
}
51+
52+
#else
53+
54+
#include <fstream>
55+
#include <regex>
56+
#include <sstream>
57+
58+
int64_t getPeakMemoryUsage() {
59+
size_t peakMemUsageKB = 0;
60+
61+
std::ifstream statusFile("/proc/self/status");
62+
std::string line;
63+
std::regex vmPeakRegex("VmPeak:");
64+
std::smatch vmMatch;
65+
while (std::getline(statusFile, line)) {
66+
if (std::regex_search(line, vmMatch, vmPeakRegex)) {
67+
std::istringstream iss(vmMatch.suffix());
68+
iss >> peakMemUsageKB;
69+
}
70+
}
71+
return static_cast<int64_t>(peakMemUsageKB);
72+
}
73+
74+
#endif
75+
4076
namespace {
4177
bool parse_and_check_command_line(int argc, char* argv[]) {
4278
// ---------------------------Parsing and validating input
@@ -772,14 +808,23 @@ int main(int argc, char* argv[]) {
772808
auto startTime = Time::now();
773809

774810
std::ifstream modelStream(FLAGS_m, std::ios_base::binary | std::ios_base::in);
811+
auto importModelMemStart = getPeakMemoryUsage();
775812
if (!modelStream.is_open()) {
776813
throw std::runtime_error("Cannot open model file " + FLAGS_m);
777814
}
815+
778816
compiledModel = core.import_model(modelStream, device_name, device_config);
779817
modelStream.close();
780818

781819
auto duration_ms = get_duration_ms_till_now(startTime);
782820
slog::info << "Import model took " << double_to_string(duration_ms) << " ms" << slog::endl;
821+
822+
auto importModelMemEnd = getPeakMemoryUsage();
823+
824+
slog::info << "Start of compilation memory usage: Peak " << importModelMemStart << " KB" << slog::endl;
825+
slog::info << "End of compilation memory usage: Peak " << importModelMemEnd << " KB" << slog::endl;
826+
slog::info << "Peak diff " << importModelMemEnd - importModelMemStart << " KB" << slog::endl;
827+
783828
slog::info << "Original model I/O paramteters:" << slog::endl;
784829
printInputAndOutputsInfoShort(compiledModel);
785830

0 commit comments

Comments
 (0)