Add SystemInfo class

This commit is contained in:
2021-08-01 17:20:35 +02:00
parent 87dad42c79
commit 2829c02762
5 changed files with 279 additions and 1 deletions

View 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;
}
}

View File

@@ -0,0 +1,21 @@
/*
* 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 <cstddef>
namespace openVulkanoCpp
{
class SystemInfo
{
public:
static size_t GetSystemRam();
static size_t GetSystemRamAvailable();
static size_t GetAppRamMax();
static size_t GetAppVirtualMemoryMax();
static size_t GetAppRamAvailable();
static size_t GetAppRamUsed();
};
}

View File

@@ -0,0 +1,82 @@
/*
* 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 <iostream>
#include <Windows.h>
#include <Psapi.h>
namespace openVulkanoCpp
{
namespace
{
enum class SYS_MEM_TYPE { TOTAL_PHYS, AVAIL_PHYS };
size_t ReadSystemMemInfo(SYS_MEM_TYPE type)
{
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
if (GlobalMemoryStatusEx(&status))
{
switch (type)
{
case SYS_MEM_TYPE::TOTAL_PHYS: return status.ullTotalPhys;
case SYS_MEM_TYPE::AVAIL_PHYS: return status.ullAvailPhys;
}
}
Logger::PERF->error("Failed to get system RAM info");
return 0;
}
enum class APP_MEM_TYPE { VM_MAX, USED };
size_t ReadAppMemInfo(APP_MEM_TYPE type)
{
PROCESS_MEMORY_COUNTERS_EX counters;
if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&counters), sizeof(counters)))
{
switch(type)
{
case APP_MEM_TYPE::VM_MAX return counters.PeakWorkingSetSize;
case APP_MEM_TYPE::USED: return counters.PrivateUsage;
}
}
Logger::PERF->error("Failed to get process memory info");
return 0;
}
}
size_t SystemInfo::GetSystemRam()
{
return ReadSystemMemInfo(MEM_TYPE::TOTAL_PHYS);
}
size_t SystemInfo::GetSystemRamAvailable()
{
return ReadSystemMemInfo(MEM_TYPE::AVAIL_PHYS);
}
size_t SystemInfo::GetAppRamMax()
{
return std::min(GetSystemRam(), GetAppVirtualMemoryMax());
}
size_t SystemInfo::GetAppRamAvailable()
{
return std::min(GetSystemRamAvailable(), GetAppVirtualMemoryMax() - GetAppRamUsed());
}
size_t SystemInfo::GetAppRamUsed()
{
return ReadAppMemInfo(APP_MEM_TYPE::USED);
}
size_t SystemInfo::GetAppVirtualMemoryMax()
{
return ReadAppMemInfo(APP_MEM_TYPE::VM_MAX);
}
}

View File

@@ -0,0 +1,53 @@
/*
* 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 <mach/mach.h>
#include <os/proc.h>
#import <Foundation/NSProcessInfo.h>
namespace openVulkanoCpp
{
size_t SystemInfo::GetSystemRam()
{
return [NSProcessInfo processInfo].physicalMemory;
}
size_t SystemInfo::GetSystemRamAvailable()
{
return os_proc_available_memory();
}
size_t SystemInfo::GetAppRamMax()
{
return GetAppRamAvailable() + GetAppRamUsed();
}
size_t SystemInfo::GetAppVirtualMemoryMax()
{
return INT64_MAX;
}
size_t SystemInfo::GetAppRamAvailable()
{
return os_proc_available_memory();
}
size_t SystemInfo::GetAppRamUsed()
{
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
if( kerr == KERN_SUCCESS )
{
return info.resident_size;
}
Logger::PERF->error("Failed to read memory consumption: {}", mach_error_string(kerr));
return 0;
}
}