Fix Linux peak memory stat to use VmHWM (#8022) (#8070)

This commit is contained in:
Tyrone Marhguy 2026-08-09 15:37:57 -04:00 committed by GitHub
parent 645b8cdf24
commit 86fa00416d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 8 deletions

View File

@ -316,6 +316,7 @@ Tracy Narine
Trung Nguyen
Tudor Timi
Tymoteusz Blazejczyk
Tyrone Marhguy
Udaya Raj Subedi
Udi Finkelstein
Unai Martinez-Corral

View File

@ -66,7 +66,7 @@ The information in this report is:
.. describe:: "allocated 123 MB"
Total memory used during simulation in megabytes.
Peak resident memory used during simulation in megabytes.
.. _benchmarking & optimization:

View File

@ -623,5 +623,5 @@ The information in this report is:
.. describe:: "allocated 123 MB"
Total memory used during build by Verilator executable (excludes
:vlopt:`--build` compiler's usage) in megabytes.
Peak resident memory used by the Verilator executable during build
(excludes :vlopt:`--build` compiler's usage) in megabytes.

View File

@ -169,17 +169,19 @@ void memUsageBytes(uint64_t& peakr, uint64_t& currentr) VL_MT_SAFE {
}
#else
// Highly unportable. Sorry
// Use VmHWM (peak resident), matching Windows PeakWorkingSetSize and macOS resident_size_max.
// VmHWM excludes pages swapped out before the peak; /proc has no peak-(RSS+Swap) counter.
std::ifstream is{"/proc/self/status"};
if (!is) return;
std::string line;
uint64_t vmPeak = 0;
uint64_t vmHwm = 0;
uint64_t vmRss = 0;
uint64_t vmSwap = 0;
std::string field;
while (std::getline(is, line)) {
if (line.rfind("VmPeak:", 0) == 0) {
if (line.rfind("VmHWM:", 0) == 0) {
std::stringstream ss{line};
ss >> field >> vmPeak;
ss >> field >> vmHwm;
} else if (line.rfind("VmRSS:", 0) == 0) {
std::stringstream ss{line};
ss >> field >> vmRss;
@ -188,7 +190,7 @@ void memUsageBytes(uint64_t& peakr, uint64_t& currentr) VL_MT_SAFE {
ss >> field >> vmSwap;
}
}
peakr = vmPeak * 1024;
peakr = vmHwm * 1024;
currentr = (vmRss + vmSwap) * 1024;
#endif
}

View File

@ -11,11 +11,13 @@ import vltest_bootstrap
test.scenarios('vlt')
test.compile(verilator_flags2=["--stats"])
test.compile(verilator_flags2=["--stats --verilate-jobs 2"])
memUsageMB = int(test.file_grep(test.stats, r'Peak Memory Usage \(MB\) +(\d+)')[0])
if memUsageMB > 128 and not test.have_dev_asan:
test.error("Consumed over 128MB memory")
test.file_grep(test.stats, r'Verilate jobs: 2')
test.passes()