Skip to content

File bench_global_memory_peak.cpp

File List > benchmark > metrics > implementations > bench_global_memory_peak.cpp

Go to the documentation of this file

#include "bench_global_memory_peak.h"
#include <fstream>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <psapi.h>
#else
#include <sys/resource.h>
#include <sys/time.h>
#endif


double MemoryPeakGlobal::get_memory_peak_mo() const {
#if defined(_WIN32)
    PROCESS_MEMORY_COUNTERS_EX pmc;
    GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS *>(&pmc), sizeof(pmc));
    const SIZE_T memory = pmc.PeakWorkingSetSize;
    return memory / 1048576.0;
#else
    std::string line;
    std::ifstream status_file("/proc/self/status");
    while (std::getline(status_file, line)) {
        if (line.compare(0, 6, "VmHWM:") == 0) {
            return std::stol(line.substr(7)) / 1024.0;
        }
    }
    return 0;
#endif
}


void MemoryPeakGlobal::compute(const Mesh &mesh, const Mesh &reconstruct, benchmark::State &state) const {
    state.counters[getName()] = benchmark::Counter(get_memory_peak_mo());
}