Add SystemInfo class
This commit is contained in:
106
openVulkanoCpp/Host/Linux/SystemInfo.cpp
Normal file
106
openVulkanoCpp/Host/Linux/SystemInfo.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "Host/SystemInfo.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include "Base/Utils.hpp"
|
||||
#include "Math/ByteSize.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/resource.h>
|
||||
|
||||
namespace openVulkanoCpp
|
||||
{
|
||||
namespace
|
||||
{
|
||||
size_t ReadMemInfo(std::string_view value)
|
||||
{
|
||||
std::ifstream meminfoFile("/proc/meminfo");
|
||||
if (!meminfoFile)
|
||||
{
|
||||
Logger::PERF->error("Failed to open /proc/meminfo");
|
||||
return 0;
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(meminfoFile, line))
|
||||
{
|
||||
if (Utils::StartsWith(line, value))
|
||||
{
|
||||
std::string n, t;
|
||||
size_t v;
|
||||
std::istringstream ss(line);
|
||||
ss >> n >> v >> t;
|
||||
return ByteSize(v, ByteSizeUnit::FromName(t));
|
||||
}
|
||||
}
|
||||
meminfoFile.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t ReadProcStatus(std::string_view value)
|
||||
{
|
||||
std::ifstream statusFile("/proc/self/status");
|
||||
if (statusFile)
|
||||
{
|
||||
size_t mem = 0;
|
||||
std::string line;
|
||||
while (std::getline(statusFile, line))
|
||||
{
|
||||
if (Utils::StartsWith(line, value))
|
||||
{
|
||||
std::string n, t;
|
||||
size_t v;
|
||||
std::istringstream ss(line);
|
||||
ss >> n >> v >> t;
|
||||
return ByteSize(v, ByteSizeUnit::FromName(t));
|
||||
}
|
||||
}
|
||||
statusFile.close();
|
||||
return mem;
|
||||
}
|
||||
Logger::PERF->error("Failed to open /proc/self/status");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t SystemInfo::GetSystemRam()
|
||||
{
|
||||
return ReadMemInfo("MemTotal");
|
||||
}
|
||||
|
||||
size_t SystemInfo::GetSystemRamAvailable()
|
||||
{
|
||||
return ReadMemInfo("MemAvailable");
|
||||
}
|
||||
|
||||
size_t SystemInfo::GetAppRamUsed()
|
||||
{
|
||||
return ReadProcStatus("VmSize");
|
||||
}
|
||||
|
||||
size_t SystemInfo::GetAppRamAvailable()
|
||||
{
|
||||
return std::min(GetSystemRamAvailable(), GetAppVirtualMemoryMax() - GetAppRamUsed());
|
||||
}
|
||||
|
||||
size_t SystemInfo::GetAppRamMax()
|
||||
{
|
||||
return std::min(GetSystemRam(), GetAppVirtualMemoryMax());
|
||||
}
|
||||
|
||||
size_t SystemInfo::GetAppVirtualMemoryMax()
|
||||
{
|
||||
rlimit limit;
|
||||
if (getrlimit(RLIMIT_AS, &limit) == 0)
|
||||
{
|
||||
std::cout << "Maximum memory usage: " << limit.rlim_cur << " bytes" << std::endl;
|
||||
return limit.rlim_cur;
|
||||
}
|
||||
Logger::PERF->error("Failed to query max application memory");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user