gpt4all/gpt4all-backend/sysinfo.h

62 lines
1.5 KiB
C
Raw Normal View History

2023-06-22 19:44:49 +00:00
#ifndef SYSINFO_H
#define SYSINFO_H
2023-06-22 19:44:49 +00:00
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#if defined(__linux__)
#include <unistd.h>
#elif defined(__APPLE__)
#include <sys/types.h>
#include <sys/sysctl.h>
2023-06-22 19:44:49 +00:00
#elif defined(_WIN32)
#include <windows.h>
#endif
2023-06-22 19:44:49 +00:00
static long long getSystemTotalRAMInBytes()
{
2023-06-22 19:44:49 +00:00
long long totalRAM = 0;
#if defined(__linux__)
std::ifstream file("/proc/meminfo");
std::string line;
while (std::getline(file, line)) {
if (line.find("MemTotal") != std::string::npos) {
std::string memTotalStr = line.substr(line.find(":") + 1);
memTotalStr.erase(0, memTotalStr.find_first_not_of(" "));
memTotalStr = memTotalStr.substr(0, memTotalStr.find(" "));
totalRAM = std::stoll(memTotalStr) * 1024; // Convert from KB to bytes
break;
}
}
2023-06-22 19:44:49 +00:00
file.close();
#elif defined(__APPLE__)
int mib[2] = {CTL_HW, HW_MEMSIZE};
size_t length = sizeof(totalRAM);
sysctl(mib, 2, &totalRAM, &length, NULL, 0);
2023-06-22 19:44:49 +00:00
#elif defined(_WIN32)
MEMORYSTATUSEX memoryStatus;
memoryStatus.dwLength = sizeof(memoryStatus);
GlobalMemoryStatusEx(&memoryStatus);
totalRAM = memoryStatus.ullTotalPhys;
#endif
2023-06-22 19:44:49 +00:00
return totalRAM;
}
2023-06-22 19:44:49 +00:00
static double getSystemTotalRAMInGB()
{
return static_cast<double>(getSystemTotalRAMInBytes()) / (1024 * 1024 * 1024);
}
static std::string getSystemTotalRAMInGBString()
{
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << getSystemTotalRAMInGB() << " GB";
return ss.str();
}
#endif // SYSINFO_H