System: Check host page size on startup

This commit is contained in:
Stenzek
2024-05-16 17:24:42 +10:00
parent a5b1ee4f04
commit 35bdbf2a55
8 changed files with 182 additions and 25 deletions

View File

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "window_info.h"
@ -9,6 +9,12 @@ namespace PlatformMisc {
void SuspendScreensaver();
void ResumeScreensaver();
/// Returns the size of pages for the current host.
size_t GetRuntimePageSize();
/// Returns the size of a cache line for the current host.
size_t GetRuntimeCacheLineSize();
/// Abstracts platform-specific code for asynchronously playing a sound.
/// On Windows, this will use PlaySound(). On Linux, it will shell out to aplay. On MacOS, it uses NSSound.
bool PlaySoundAsync(const char* path);

View File

@ -1,17 +1,19 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "metal_layer.h"
#include "platform_misc.h"
#include "window_info.h"
#include "metal_layer.h"
#include "common/log.h"
#include "common/small_string.h"
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <Cocoa/Cocoa.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <QuartzCore/QuartzCore.h>
#include <cinttypes>
#include <optional>
#include <sys/sysctl.h>
#include <vector>
Log_SetChannel(PlatformMisc);
@ -50,11 +52,11 @@ void PlatformMisc::SuspendScreensaver()
{
if (s_screensaver_suspended)
if (!SetScreensaverInhibitMacOS(true))
{
Log_ErrorPrintf("Failed to suspend screensaver.");
return;
}
if (!SetScreensaverInhibitMacOS(true))
{
Log_ErrorPrintf("Failed to suspend screensaver.");
return;
}
s_screensaver_suspended = true;
}
@ -70,6 +72,27 @@ void PlatformMisc::ResumeScreensaver()
s_screensaver_suspended = false;
}
template<typename T>
static std::optional<T> sysctlbyname(const char* name)
{
T output = 0;
size_t output_size = sizeof(output);
if (sysctlbyname(name, &output, &output_size, nullptr, 0) != 0)
return std::nullopt;
return output;
}
size_t PlatformMisc::GetRuntimePageSize()
{
return sysctlbyname<u32>("hw.pagesize").value_or(0);
}
size_t PlatformMisc::GetRuntimeCacheLineSize()
{
return static_cast<size_t>(std::max<s64>(sysctlbyname<s64>("hw.cachelinesize").value_or(0), 0));
}
bool PlatformMisc::PlaySoundAsync(const char* path)
{
NSString* nspath = [[NSString alloc] initWithUTF8String:path];
@ -80,46 +103,44 @@ bool PlatformMisc::PlaySoundAsync(const char* path)
return result;
}
bool CocoaTools::CreateMetalLayer(WindowInfo *wi)
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
{
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
bool ret;
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]() {
ret = CreateMetalLayer(wi);
});
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]() { ret = CreateMetalLayer(wi); });
return ret;
}
CAMetalLayer* layer = [CAMetalLayer layer];
if (layer == nil)
{
Log_ErrorPrint("Failed to create CAMetalLayer");
return false;
}
NSView* view = (__bridge NSView*)wi->window_handle;
[view setWantsLayer:TRUE];
[view setLayer:layer];
[layer setContentsScale:[[[view window] screen] backingScaleFactor]];
wi->surface_handle = (__bridge void*)layer;
return true;
}
void CocoaTools::DestroyMetalLayer(WindowInfo *wi)
void CocoaTools::DestroyMetalLayer(WindowInfo* wi)
{
if (!wi->surface_handle)
return;
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
dispatch_sync(dispatch_get_main_queue(), [wi]() { DestroyMetalLayer(wi); });
return;
}
NSView* view = (__bridge NSView*)wi->window_handle;
CAMetalLayer* layer = (__bridge CAMetalLayer*)wi->surface_handle;
[view setLayer:nil];

View File

@ -121,6 +121,34 @@ void PlatformMisc::ResumeScreensaver()
s_screensaver_suspended = false;
}
size_t PlatformMisc::GetRuntimePageSize()
{
int res = sysconf(_SC_PAGESIZE);
return (res > 0) ? static_cast<size_t>(res) : 0;
}
size_t PlatformMisc::GetRuntimeCacheLineSize()
{
int l1i = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
int l1d = sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
int res = (l1i > l1d) ? l1i : l1d;
for (int index = 0; index < 16; index++)
{
char buf[128];
snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu0/cache/index%d/coherency_line_size", index);
std::FILE* fp = std::fopen(buf, "rb");
if (!fp)
break;
std::fread(buf, sizeof(buf), 1, fp);
std::fclose(fp);
int val = std::atoi(buf);
res = (val > res) ? val : res;
}
return (res > 0) ? static_cast<size_t>(res) : 0;
}
bool PlatformMisc::PlaySoundAsync(const char* path)
{
#ifdef __linux__

View File

@ -8,7 +8,9 @@
#include "common/small_string.h"
#include "common/string_util.h"
#include <algorithm>
#include <cinttypes>
#include <memory>
#include "common/windows_headers.h"
#include <mmsystem.h>
@ -53,6 +55,35 @@ void PlatformMisc::ResumeScreensaver()
s_screensaver_suspended = false;
}
size_t PlatformMisc::GetRuntimePageSize()
{
SYSTEM_INFO si = {};
GetSystemInfo(&si);
return si.dwPageSize;
}
size_t PlatformMisc::GetRuntimeCacheLineSize()
{
DWORD size = 0;
if (!GetLogicalProcessorInformation(nullptr, &size) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return 0;
std::unique_ptr<SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]> lpi =
std::make_unique<SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]>(
(size + (sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) - 1)) / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
if (!GetLogicalProcessorInformation(lpi.get(), &size))
return 0;
u32 max_line_size = 0;
for (u32 i = 0; i < size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++)
{
if (lpi[i].Relationship == RelationCache)
max_line_size = std::max<u32>(max_line_size, lpi[i].Cache.LineSize);
}
return max_line_size;
}
bool PlatformMisc::PlaySoundAsync(const char* path)
{
const std::wstring wpath(FileSystem::GetWin32Path(path));