Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Fix mem_peak_usage on macos (#29241)

Nikita Kudriavtsev634dd296bfd7
diff --git a/samples/cpp/benchmark_app/main.cpp b/samples/cpp/benchmark_app/main.cpp
index 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 on
namespace {
-#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);
+}
+
#else
int64_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.py
index 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 json
import os
+import platform
import numpy as np
import pathlib
import pytest
@@ -63,7 +64,8 @@ def verify(sample_language, device, api=None, nireq=None, shape=None, data_shape
assert '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 output
if tmp_path:
diff --git a/tools/benchmark_tool/openvino/tools/benchmark/main.py b/tools/benchmark_tool/openvino/tools/benchmark/main.py
index 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 os
import sys
+import platform
from datetime import datetime
from openvino import Dimension, properties
@@ -23,7 +24,7 @@ from openvino.tools.benchmark.utils.statistics_report import StatisticsReport, J
averageCntReport, detailedCntReport
def 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 file
return None
def log_memory_usage(logger, start_mem_usage, end_mem_usage, action_name):