OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Fix mem_peak_usage on macos (#29241)
Nikita Kudriavtsev634dd296bfd7
diff --git a/samples/cpp/benchmark_app/main.cpp b/samples/cpp/benchmark_app/main.cppindex 68b484a37fd..33db90c6449 100644--- a/samples/cpp/benchmark_app/main.cpp+++ b/samples/cpp/benchmark_app/main.cpp@@ -36,20 +36,24 @@#include "statistics_report.hpp"#include "utils.hpp"-#if defined _WIN32+#if defined(_WIN32)#include <windows.h>#include <psapi.h>-#else+#elif defined(__APPLE__)+#include <sys/resource.h>+#elif defined(__linux__)#include <fstream>#include <regex>#include <sstream>+#else+#error "unsupported OS"#endif// clang-format onnamespace {-#if defined _WIN32+#if defined(_WIN32)int64_t get_peak_memory_usage() {PROCESS_MEMORY_COUNTERS mem_counters;@@ -71,6 +75,22 @@ int64_t get_peak_memory_usage() {return static_cast<int64_t>(std::round(mem_counters.PeakWorkingSetSize / bytes_in_kilobyte));}+#elif defined(__APPLE__)++int64_t get_peak_memory_usage() {+ struct rusage usage;+ // There is no VmPeak on macOS, so the only way is to use ru_maxrss.+ // Please note, there's a difference between ru_maxrss and VmPeak:+ // ru_maxrss is the maximum amount of physical memory (RAM) occupied by the process+ // which, does not include memory-mapped files, pages reserved but not used, etc.+ if (getrusage(RUSAGE_SELF, &usage) != 0) {+ throw std::runtime_error("Can't get system memory values");+ }++ // in kilobytes+ return static_cast<int64_t>(usage.ru_maxrss);+}+#elseint64_t get_peak_memory_usage() {diff --git a/tests/samples_tests/smoke_tests/test_benchmark_app.py b/tests/samples_tests/smoke_tests/test_benchmark_app.pyindex 7bead37a230..83f8ab2870b 100755--- a/tests/samples_tests/smoke_tests/test_benchmark_app.py+++ b/tests/samples_tests/smoke_tests/test_benchmark_app.py@@ -12,6 +12,7 @@"""import jsonimport os+import platformimport numpy as npimport pathlibimport pytest@@ -63,7 +64,8 @@ def verify(sample_language, device, api=None, nireq=None, shape=None, data_shapeassert 'FPS' in output# No Windows support due to the lack of the ‘psutil’ module in the CI infrastructure- if os.name == "posix":+ # No Macos support due to no /proc/self/status file+ if platform.system() == "Linux":assert 'Compile model ram used' in outputif tmp_path:diff --git a/tools/benchmark_tool/openvino/tools/benchmark/main.py b/tools/benchmark_tool/openvino/tools/benchmark/main.pyindex 1a4187ce73b..6fbe88791ab 100755--- a/tools/benchmark_tool/openvino/tools/benchmark/main.py+++ b/tools/benchmark_tool/openvino/tools/benchmark/main.py@@ -3,6 +3,7 @@import osimport sys+import platformfrom datetime import datetimefrom openvino import Dimension, properties@@ -23,7 +24,7 @@ from openvino.tools.benchmark.utils.statistics_report import StatisticsReport, JaverageCntReport, detailedCntReportdef get_peak_memory_usage():- if os.name == "posix":+ if platform.system() == "Linux":with open("/proc/self/status", "r") as f:for line in f:if line.startswith("VmPeak:"):@@ -31,6 +32,7 @@ def get_peak_memory_usage():raise RuntimeError("VmPeak attribute not found. Unable to determine peak memory usage.")# No Windows support due to the lack of the ‘psutil’ module in the CI infrastructure+ # No Macos support due to no /proc/self/status filereturn Nonedef log_memory_usage(logger, start_mem_usage, end_mem_usage, action_name):