Skip to content

Commit 1570b97

Browse files
committed
Align BAPP logs
1 parent 0e6a0db commit 1570b97

File tree

1 file changed

+73
-31
lines changed

1 file changed

+73
-31
lines changed

samples/cpp/benchmark_app/main.cpp

+73-31
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,71 @@
3535
#include "remote_tensors_filling.hpp"
3636
#include "statistics_report.hpp"
3737
#include "utils.hpp"
38-
// clang-format on
3938

4039
#if defined _WIN32
41-
4240
#include <windows.h>
43-
4441
#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-
5242
#else
53-
5443
#include <fstream>
5544
#include <regex>
5645
#include <sstream>
46+
#endif
47+
48+
// clang-format on
49+
50+
namespace {
51+
52+
#if defined _WIN32
53+
54+
int64_t get_peak_memory_usage() {
55+
PROCESS_MEMORY_COUNTERS mem_counters;
56+
if (!GetProcessMemoryInfo(GetCurrentProcess(), &mem_counters, sizeof(mem_counters))) {
57+
throw std::runtime_error("Can't get system memory values");
58+
}
59+
60+
// Linux tracks memory usage in pages and then converts them to kB.
61+
// Thus, there is always some room for inaccuracy as pages are not guaranteed to be fully used.
62+
// In Windows, the situation is different: the system returns the memory usage in bytes, not in pages.
63+
// To align the output between the two operating systems as closely as possible, we have two options:
64+
// 1. Use rounding to the nearest integer.
65+
// 2. Try to estimate the number of pages used in Windows. However,
66+
// this approach is likely to be inaccurate as well, so option 1 was chosen.
67+
static constexpr double bytes_in_kilobyte = 1024.0;
68+
69+
// please note then we calculate difference
70+
// to get peak memory increment value, so we return int64, not size_t
71+
return static_cast<int64_t>(std::round(mem_counters.PeakWorkingSetSize / bytes_in_kilobyte));
72+
}
73+
74+
#else
5775

58-
int64_t getPeakMemoryUsage() {
59-
size_t peakMemUsageKB = 0;
76+
int64_t get_peak_memory_usage() {
77+
size_t peak_mem_usage_kB = 0;
6078

61-
std::ifstream statusFile("/proc/self/status");
79+
std::ifstream status_file("/proc/self/status");
6280
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;
81+
std::regex vm_peak_regex("VmPeak:");
82+
std::smatch vm_match;
83+
bool mem_values_found = false;
84+
while (std::getline(status_file, line)) {
85+
if (std::regex_search(line, vm_match, vm_peak_regex)) {
86+
std::istringstream iss(vm_match.suffix());
87+
iss >> peak_mem_usage_kB;
88+
mem_values_found = true;
6989
}
7090
}
71-
return static_cast<int64_t>(peakMemUsageKB);
91+
92+
if (!mem_values_found) {
93+
throw std::runtime_error("Can't get system memory values");
94+
}
95+
96+
// please note then we calculate difference
97+
// to get peak memory increment value, so we return int64, not size_t
98+
return static_cast<int64_t>(peak_mem_usage_kB);
7299
}
73100

74101
#endif
75102

76-
namespace {
77103
bool parse_and_check_command_line(int argc, char* argv[]) {
78104
// ---------------------------Parsing and validating input
79105
// arguments--------------------------------------
@@ -599,10 +625,18 @@ int main(int argc, char* argv[]) {
599625
slog::info << "Skipping the step for loading model from file" << slog::endl;
600626
next_step();
601627
slog::info << "Skipping the step for loading model from file" << slog::endl;
628+
auto compile_model_mem_start = get_peak_memory_usage();
602629
auto startTime = Time::now();
603630
compiledModel = core.compile_model(FLAGS_m, device_name, device_config);
604631
auto duration_ms = get_duration_ms_till_now(startTime);
632+
auto compile_model_mem_end = get_peak_memory_usage();
605633
slog::info << "Compile model took " << double_to_string(duration_ms) << " ms" << slog::endl;
634+
635+
slog::info << "Start of compilation memory usage: Peak " << compile_model_mem_start << " KB" << slog::endl;
636+
slog::info << "End of compilation memory usage: Peak " << compile_model_mem_end << " KB" << slog::endl;
637+
slog::info << "Load model ram used " << compile_model_mem_end - compile_model_mem_start << " KB"
638+
<< slog::endl;
639+
606640
slog::info << "Original model I/O parameters:" << slog::endl;
607641
printInputAndOutputsInfoShort(compiledModel);
608642

@@ -773,10 +807,18 @@ int main(int argc, char* argv[]) {
773807
// ----------------- 7. Loading the model to the device
774808
// --------------------------------------------------------
775809
next_step();
810+
auto compile_model_mem_start = get_peak_memory_usage();
776811
startTime = Time::now();
777812
compiledModel = core.compile_model(model, device_name, device_config);
778813
duration_ms = get_duration_ms_till_now(startTime);
814+
auto compile_model_mem_end = get_peak_memory_usage();
779815
slog::info << "Compile model took " << double_to_string(duration_ms) << " ms" << slog::endl;
816+
817+
slog::info << "Start of compilation memory usage: Peak " << compile_model_mem_start << " KB" << slog::endl;
818+
slog::info << "End of compilation memory usage: Peak " << compile_model_mem_end << " KB" << slog::endl;
819+
slog::info << "Load model ram used " << compile_model_mem_end - compile_model_mem_start << " KB"
820+
<< slog::endl;
821+
780822
if (statistics)
781823
statistics->add_parameters(
782824
StatisticsReport::Category::EXECUTION_RESULTS,
@@ -795,26 +837,26 @@ int main(int argc, char* argv[]) {
795837
// ----------------- 7. Loading the model to the device
796838
// --------------------------------------------------------
797839
next_step();
840+
auto import_model_mem_start = get_peak_memory_usage();
798841
auto startTime = Time::now();
799842

800843
std::ifstream modelStream(FLAGS_m, std::ios_base::binary | std::ios_base::in);
801-
auto importModelMemStart = getPeakMemoryUsage();
802844
if (!modelStream.is_open()) {
803845
throw std::runtime_error("Cannot open model file " + FLAGS_m);
804846
}
805-
847+
806848
compiledModel = core.import_model(modelStream, device_name, device_config);
807849
modelStream.close();
808850

809851
auto duration_ms = get_duration_ms_till_now(startTime);
852+
auto import_model_mem_end = get_peak_memory_usage();
810853
slog::info << "Import model took " << double_to_string(duration_ms) << " ms" << slog::endl;
811-
812-
auto importModelMemEnd = getPeakMemoryUsage();
813-
814-
slog::info << "Start of compilation memory usage: Peak " << importModelMemStart << " KB" << slog::endl;
815-
slog::info << "End of compilation memory usage: Peak " << importModelMemEnd << " KB" << slog::endl;
816-
slog::info << "Peak diff " << importModelMemEnd - importModelMemStart << " KB" << slog::endl;
817-
854+
855+
slog::info << "Start of import memory usage: Peak " << import_model_mem_start << " KB" << slog::endl;
856+
slog::info << "End of import memory usage: Peak " << import_model_mem_end << " KB" << slog::endl;
857+
slog::info << "Load model ram used " << import_model_mem_end - import_model_mem_start << " KB"
858+
<< slog::endl;
859+
818860
slog::info << "Original model I/O paramteters:" << slog::endl;
819861
printInputAndOutputsInfoShort(compiledModel);
820862

0 commit comments

Comments
 (0)