Skip to content

Commit 2c6eb2f

Browse files
committed
Fix mem_peak_usage on macos
1 parent 63fc0ab commit 2c6eb2f

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

samples/cpp/benchmark_app/main.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,24 @@
3636
#include "statistics_report.hpp"
3737
#include "utils.hpp"
3838

39-
#if defined _WIN32
39+
#if defined(_WIN32)
4040
#include <windows.h>
4141
#include <psapi.h>
42-
#else
42+
#elif defined(__APPLE__) && defined(__MACH__)
43+
#include <sys/resource.h>
44+
#elif defined(__linux__)
4345
#include <fstream>
4446
#include <regex>
4547
#include <sstream>
48+
#else
49+
#error "unsupported OS"
4650
#endif
4751

4852
// clang-format on
4953

5054
namespace {
5155

52-
#if defined _WIN32
56+
#if defined(_WIN32)
5357

5458
int64_t get_peak_memory_usage() {
5559
PROCESS_MEMORY_COUNTERS mem_counters;
@@ -71,6 +75,22 @@ int64_t get_peak_memory_usage() {
7175
return static_cast<int64_t>(std::round(mem_counters.PeakWorkingSetSize / bytes_in_kilobyte));
7276
}
7377

78+
#elif defined( __APPLE__ ) && defined( __MACH__ )
79+
80+
int64_t get_peak_memory_usage() {
81+
struct rusage usage;
82+
// There is no VmPeak on macOS, so the only way is to use ru_maxrss.
83+
// Please note, there's a difference between ru_maxrss and VmPeak:
84+
// ru_maxrss is the maximum amount of physical memory (RAM) occupied by the process
85+
// which, does not include memory-mapped files, pages reserved but not used, etc.
86+
if (getrusage(RUSAGE_SELF, &usage) != 0) {
87+
throw std::runtime_error("Can't get system memory values");
88+
}
89+
90+
// in kilobytes
91+
return static_cast<int64_t>(usage.ru_maxrss);
92+
}
93+
7494
#else
7595

7696
int64_t get_peak_memory_usage() {

tests/samples_tests/smoke_tests/test_benchmark_app.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313
import json
1414
import os
15+
import platform
1516
import numpy as np
1617
import pathlib
1718
import pytest
@@ -63,7 +64,8 @@ def verify(sample_language, device, api=None, nireq=None, shape=None, data_shape
6364
assert 'FPS' in output
6465

6566
# No Windows support due to the lack of the ‘psutil’ module in the CI infrastructure
66-
if os.name == "posix":
67+
# No Macos support due to no /proc/self/status file
68+
if platform.system() == "Linux":
6769
assert 'Compile model ram used' in output
6870

6971
if tmp_path:

tools/benchmark_tool/openvino/tools/benchmark/main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import sys
6+
import platform
67
from datetime import datetime
78

89
from openvino import Dimension, properties
@@ -23,14 +24,15 @@
2324
averageCntReport, detailedCntReport
2425

2526
def get_peak_memory_usage():
26-
if os.name == "posix":
27+
if platform.system() == "Linux":
2728
with open("/proc/self/status", "r") as f:
2829
for line in f:
2930
if line.startswith("VmPeak:"):
3031
return int(line.split()[1]) # The value in KB
3132
raise RuntimeError("VmPeak attribute not found. Unable to determine peak memory usage.")
3233

3334
# No Windows support due to the lack of the ‘psutil’ module in the CI infrastructure
35+
# No Macos support due to no /proc/self/status file
3436
return None
3537

3638
def log_memory_usage(logger, start_mem_usage, end_mem_usage, action_name):

0 commit comments

Comments
 (0)