// SPDX-License-Identifier: GPL-3.0-or-later

#include "windows_plugin.h"
#include "windows-internals.h"

#define _COMMON_PLUGIN_NAME "windows.plugin"
#define _COMMON_PLUGIN_MODULE_NAME "GetSystemRam"
#include "../common-contexts/common-contexts.h"

int do_GetSystemRAM(int update_every, usec_t dt __maybe_unused)
{
    MEMORYSTATUSEX memStat = {0};
    memStat.dwLength = sizeof(memStat);

    if (!GlobalMemoryStatusEx(&memStat)) {
        nd_log(NDLS_COLLECTORS, NDLP_ERR, "GlobalMemoryStatusEx() failed (error: %lu)", GetLastError());
        return 1;
    }

    {
        ULONGLONG total_bytes = memStat.ullTotalPhys;
        ULONGLONG free_bytes = memStat.ullAvailPhys;
        ULONGLONG used_bytes = total_bytes - free_bytes;
        common_system_ram(free_bytes, used_bytes, update_every);
    }

    {
        ULONGLONG total_bytes = memStat.ullTotalPageFile;
        ULONGLONG free_bytes = memStat.ullAvailPageFile;
        ULONGLONG used_bytes = total_bytes - free_bytes;
        common_mem_swap(free_bytes, used_bytes, update_every);
    }

    return 0;
}
