2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2015-11-17 17:30:20 +00:00
|
|
|
|
2021-09-03 10:43:33 +00:00
|
|
|
#if defined(__APPLE__)
|
2015-11-17 17:30:20 +00:00
|
|
|
|
2022-12-05 01:48:07 +00:00
|
|
|
#include "common/Darwin/DarwinMisc.h"
|
|
|
|
|
2015-11-17 17:30:20 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2022-12-05 01:48:07 +00:00
|
|
|
#include <optional>
|
2015-11-17 17:30:20 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
2022-12-04 04:52:26 +00:00
|
|
|
#include <time.h>
|
2015-11-17 17:30:20 +00:00
|
|
|
#include <mach/mach_time.h>
|
2022-05-28 04:50:43 +00:00
|
|
|
#include <IOKit/pwr_mgt/IOPMLib.h>
|
2021-09-03 10:43:33 +00:00
|
|
|
|
|
|
|
#include "common/Pcsx2Types.h"
|
2022-12-05 01:48:07 +00:00
|
|
|
#include "common/Console.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/General.h"
|
2022-12-04 04:52:26 +00:00
|
|
|
#include "common/Threading.h"
|
2022-10-02 13:05:05 +00:00
|
|
|
#include "common/WindowInfo.h"
|
2015-11-17 17:30:20 +00:00
|
|
|
|
|
|
|
// Darwin (OSX) is a bit different from Linux when requesting properties of
|
|
|
|
// the OS because of its BSD/Mach heritage. Helpfully, most of this code
|
|
|
|
// should translate pretty well to other *BSD systems. (e.g.: the sysctl(3)
|
|
|
|
// interface).
|
|
|
|
//
|
|
|
|
// For an overview of all of Darwin's sysctls, check:
|
|
|
|
// https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/sysctl.3.html
|
|
|
|
|
|
|
|
// Return the total physical memory on the machine, in bytes. Returns 0 on
|
|
|
|
// failure (not supported by the operating system).
|
|
|
|
u64 GetPhysicalMemory()
|
|
|
|
{
|
2021-09-11 06:28:39 +00:00
|
|
|
u64 getmem = 0;
|
|
|
|
size_t len = sizeof(getmem);
|
|
|
|
int mib[] = {CTL_HW, HW_MEMSIZE};
|
|
|
|
if (sysctl(mib, std::size(mib), &getmem, &len, NULL, 0) < 0)
|
|
|
|
perror("sysctl:");
|
|
|
|
return getmem;
|
2015-11-17 17:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 04:52:26 +00:00
|
|
|
static mach_timebase_info_data_t s_timebase_info;
|
2023-06-14 15:25:48 +00:00
|
|
|
static const u64 tickfreq = []() {
|
2022-12-04 04:52:26 +00:00
|
|
|
if (mach_timebase_info(&s_timebase_info) != KERN_SUCCESS)
|
2021-09-11 06:28:39 +00:00
|
|
|
abort();
|
2023-06-14 15:25:48 +00:00
|
|
|
return (u64)1e9 * (u64)s_timebase_info.denom / (u64)s_timebase_info.numer;
|
|
|
|
}();
|
2015-11-17 17:30:20 +00:00
|
|
|
|
|
|
|
// returns the performance-counter frequency: ticks per second (Hz)
|
|
|
|
//
|
|
|
|
// usage:
|
|
|
|
// u64 seconds_passed = GetCPUTicks() / GetTickFrequency();
|
|
|
|
// u64 millis_passed = (GetCPUTicks() * 1000) / GetTickFrequency();
|
|
|
|
//
|
|
|
|
// NOTE: multiply, subtract, ... your ticks before dividing by
|
|
|
|
// GetTickFrequency() to maintain good precision.
|
|
|
|
u64 GetTickFrequency()
|
|
|
|
{
|
2021-09-11 06:28:39 +00:00
|
|
|
return tickfreq;
|
2015-11-17 17:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the number of "ticks" since some arbitrary, fixed time in the
|
|
|
|
// past. On OSX x86(-64), this is actually the number of nanoseconds passed,
|
|
|
|
// because mach_timebase_info.numer == denom == 1. So "ticks" ==
|
|
|
|
// nanoseconds.
|
|
|
|
u64 GetCPUTicks()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return mach_absolute_time();
|
2015-11-17 17:30:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 06:28:39 +00:00
|
|
|
static std::string sysctl_str(int category, int name)
|
2015-11-17 17:30:20 +00:00
|
|
|
{
|
2021-09-11 06:28:39 +00:00
|
|
|
char buf[32];
|
|
|
|
size_t len = sizeof(buf);
|
|
|
|
int mib[] = {category, name};
|
|
|
|
sysctl(mib, std::size(mib), buf, &len, nullptr, 0);
|
|
|
|
return std::string(buf, len > 0 ? len - 1 : 0);
|
|
|
|
}
|
2015-11-17 17:30:20 +00:00
|
|
|
|
2022-12-05 01:48:07 +00:00
|
|
|
static std::optional<u32> sysctlbyname_u32(const char* name)
|
|
|
|
{
|
|
|
|
u32 output;
|
|
|
|
size_t output_size = sizeof(output);
|
|
|
|
if (0 != sysctlbyname(name, &output, &output_size, nullptr, 0))
|
|
|
|
return std::nullopt;
|
|
|
|
if (output_size != sizeof(output))
|
|
|
|
{
|
|
|
|
DevCon.WriteLn("(DarwinMisc) sysctl %s gave unexpected size %zd", name, output_size);
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string GetOSVersionString()
|
2021-09-11 06:28:39 +00:00
|
|
|
{
|
|
|
|
std::string type = sysctl_str(CTL_KERN, KERN_OSTYPE);
|
|
|
|
std::string release = sysctl_str(CTL_KERN, KERN_OSRELEASE);
|
|
|
|
std::string arch = sysctl_str(CTL_HW, HW_MACHINE);
|
|
|
|
return type + " " + release + " " + arch;
|
2015-11-17 17:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 04:50:43 +00:00
|
|
|
static IOPMAssertionID s_pm_assertion;
|
|
|
|
|
2022-10-02 13:05:05 +00:00
|
|
|
bool WindowInfo::InhibitScreensaver(const WindowInfo& wi, bool inhibit)
|
2015-11-17 17:30:20 +00:00
|
|
|
{
|
2022-05-28 04:50:43 +00:00
|
|
|
if (s_pm_assertion)
|
|
|
|
{
|
|
|
|
IOPMAssertionRelease(s_pm_assertion);
|
|
|
|
s_pm_assertion = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-02 13:05:05 +00:00
|
|
|
if (inhibit)
|
2022-05-28 04:50:43 +00:00
|
|
|
IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionLevelOn, CFSTR("Playing a game"), &s_pm_assertion);
|
2022-10-02 13:05:05 +00:00
|
|
|
|
|
|
|
return true;
|
2015-11-17 17:30:20 +00:00
|
|
|
}
|
2022-12-04 04:52:26 +00:00
|
|
|
|
|
|
|
void Threading::Sleep(int ms)
|
|
|
|
{
|
|
|
|
usleep(1000 * ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Threading::SleepUntil(u64 ticks)
|
|
|
|
{
|
|
|
|
// This is definitely sub-optimal, but apparently clock_nanosleep() doesn't exist.
|
|
|
|
const s64 diff = static_cast<s64>(ticks - GetCPUTicks());
|
|
|
|
if (diff <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const u64 nanos = (static_cast<u64>(diff) * static_cast<u64>(s_timebase_info.denom)) / static_cast<u64>(s_timebase_info.numer);
|
|
|
|
if (nanos == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = nanos / 1000000000ULL;
|
|
|
|
ts.tv_nsec = nanos % 1000000000ULL;
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-12-05 01:48:07 +00:00
|
|
|
std::vector<DarwinMisc::CPUClass> DarwinMisc::GetCPUClasses()
|
|
|
|
{
|
|
|
|
std::vector<CPUClass> out;
|
|
|
|
|
|
|
|
if (std::optional<u32> nperflevels = sysctlbyname_u32("hw.nperflevels"))
|
|
|
|
{
|
|
|
|
char name[64];
|
|
|
|
for (u32 i = 0; i < *nperflevels; i++)
|
|
|
|
{
|
|
|
|
snprintf(name, sizeof(name), "hw.perflevel%u.physicalcpu", i);
|
|
|
|
std::optional<u32> physicalcpu = sysctlbyname_u32(name);
|
|
|
|
snprintf(name, sizeof(name), "hw.perflevel%u.logicalcpu", i);
|
|
|
|
std::optional<u32> logicalcpu = sysctlbyname_u32(name);
|
|
|
|
|
|
|
|
char levelname[64];
|
|
|
|
size_t levelname_size = sizeof(levelname);
|
|
|
|
snprintf(name, sizeof(name), "hw.perflevel%u.name", i);
|
|
|
|
if (0 != sysctlbyname(name, levelname, &levelname_size, nullptr, 0))
|
|
|
|
strcpy(levelname, "???");
|
|
|
|
|
|
|
|
if (!physicalcpu.has_value() || !logicalcpu.has_value())
|
|
|
|
{
|
|
|
|
Console.Warning("(DarwinMisc) Perf level %u is missing data on %s cpus!",
|
|
|
|
i, !physicalcpu.has_value() ? "physical" : "logical");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.push_back({levelname, *physicalcpu, *logicalcpu});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (std::optional<u32> physcpu = sysctlbyname_u32("hw.physicalcpu"))
|
|
|
|
{
|
|
|
|
out.push_back({"Default", *physcpu, sysctlbyname_u32("hw.logicalcpu").value_or(0)});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.Warning("(DarwinMisc) Couldn't get cpu core count!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-09-03 10:43:33 +00:00
|
|
|
#endif
|