Skip to content

File bench_memory_peak.cpp

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

Go to the documentation of this file

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


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


double MemoryPeak::get_memory_usage_mo() {
#if defined(_WIN32)
    PROCESS_MEMORY_COUNTERS_EX pmc;
    GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS *>(&pmc), sizeof(pmc));
    const SIZE_T memory = pmc.WorkingSetSize;
    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, "VmRSS:") == 0) {
            return std::stol(line.substr(7)) / 1024.0;
        }
    }
    return 0;
#endif
}

void MemoryPeak::compute(const Mesh& mesh, const Mesh& reconstructed, benchmark::State& state) const {
    state.counters[getName()] = benchmark::Counter(peak);
}

void MemoryPeak::run() {
    while (isRunning) {
        peak = std::max(peak, get_memory_usage_mo());
        std::this_thread::sleep_for(std::chrono::milliseconds(500)); // Attente de 5ms
    }
}

void MemoryPeak::start() {
    peak = 0;
    isRunning = true;
    thread = std::thread(&MemoryPeak::run, this);
}

void MemoryPeak::stop() {
    isRunning = false;
    thread.join();
}