mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 03:02:29 +00:00
feat(common): implement processor feature detection on Windows and MacOS, more accurate OsTime* functions on Windows
This commit is contained in:
parent
93bdaa6c9e
commit
bc34c481d3
15 changed files with 836 additions and 60 deletions
|
|
@ -4,9 +4,17 @@ file(GLOB COMMON_SOURCES
|
|||
"datastore/*.cpp"
|
||||
"mempool/*.cpp"
|
||||
"objectalloc/*.cpp"
|
||||
"processor/*.cpp"
|
||||
"processor/win/*.cpp"
|
||||
"processor/mac/*.cpp"
|
||||
"processor/linux/*.cpp"
|
||||
"ref/*.cpp"
|
||||
"string/*.cpp"
|
||||
"thread/*.cpp"
|
||||
"time/*.cpp"
|
||||
"time/win/*.cpp"
|
||||
"time/mac/*.cpp"
|
||||
"time/linux/*.cpp"
|
||||
"xml/*.cpp"
|
||||
)
|
||||
|
||||
|
|
|
|||
6
common/Processor.hpp
Normal file
6
common/Processor.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef COMMON_PROCESSOR_HPP
|
||||
#define COMMON_PROCESSOR_HPP
|
||||
|
||||
#include "common/processor/Processor.hpp"
|
||||
|
||||
#endif
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
#include "common/Time.hpp"
|
||||
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
#include <windows.h>
|
||||
#elif defined(WHOA_SYSTEM_MAC)
|
||||
#include <mach/mach_time.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(WHOA_SYSTEM_LINUX)
|
||||
#include <chrono>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
uint64_t OsGetAsyncTimeMs() {
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
return GetTickCount();
|
||||
|
||||
#elif defined(WHOA_SYSTEM_MAC)
|
||||
static mach_timebase_info_data_t timebase;
|
||||
|
||||
if (timebase.denom == 0) {
|
||||
mach_timebase_info(&timebase);
|
||||
}
|
||||
|
||||
uint64_t ticks = mach_absolute_time();
|
||||
|
||||
return ticks * (timebase.numer / timebase.denom) / 1000000;
|
||||
|
||||
#elif defined(WHOA_SYSTEM_LINUX)
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
uint64_t ticks = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
return ticks;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
// TODO QueryPerformanceCounter implementation
|
||||
return OsGetAsyncTimeMs();
|
||||
|
||||
#else
|
||||
return OsGetAsyncTimeMs();
|
||||
#endif
|
||||
}
|
||||
|
||||
void OsSleep(uint32_t duration) {
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
Sleep(duration);
|
||||
#endif
|
||||
|
||||
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
|
||||
usleep(duration);
|
||||
#endif
|
||||
}
|
||||
|
|
@ -1,12 +1,6 @@
|
|||
#ifndef COMMON_TIME_HPP
|
||||
#define COMMON_TIME_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
uint64_t OsGetAsyncTimeMs();
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise();
|
||||
|
||||
void OsSleep(uint32_t duration);
|
||||
#include "common/time/Time.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
|||
6
common/processor/Processor.cpp
Normal file
6
common/processor/Processor.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include "common/processor/Processor.hpp"
|
||||
|
||||
uint32_t OsGetProcessorFeatures() {
|
||||
int32_t vendorID;
|
||||
return OsGetProcessorFeaturesEx(vendorID);
|
||||
}
|
||||
16
common/processor/Processor.hpp
Normal file
16
common/processor/Processor.hpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef COMMON_PROCESSOR_PROCESSOR_HPP
|
||||
#define COMMON_PROCESSOR_PROCESSOR_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
uint32_t OsGetProcessorCount();
|
||||
|
||||
uint32_t OsGetProcessorFeatures();
|
||||
|
||||
uint32_t OsGetProcessorFeaturesEx(int32_t& vendorID);
|
||||
|
||||
void OsSystemEnableCpuLog();
|
||||
|
||||
uint64_t OsGetProcessorTicksPerSecond();
|
||||
|
||||
#endif
|
||||
27
common/processor/linux/Processor.cpp
Normal file
27
common/processor/linux/Processor.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#if defined(WHOA_SYSTEM_LINUX)
|
||||
|
||||
#include "common/processor/Processor.hpp"
|
||||
#include <unistd.h>
|
||||
|
||||
uint32_t OsGetProcessorCount() {
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
|
||||
uint32_t OsGetProcessorFeaturesEx(int32_t& vendorID) {
|
||||
|
||||
|
||||
// TODO
|
||||
vendorID = 1;
|
||||
return 0x80000000;
|
||||
}
|
||||
|
||||
void OsSystemEnableCpuLog() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
uint64_t OsGetProcessorTicksPerSecond() {
|
||||
// TODO
|
||||
return 1000ULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
58
common/processor/mac/Processor.cpp
Normal file
58
common/processor/mac/Processor.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#if defined(WHOA_SYSTEM_MAC)
|
||||
|
||||
#include "common/processor/Processor.hpp"
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
uint32_t OsGetProcessorCount() {
|
||||
uint32_t count = 1;
|
||||
uint32_t logicalcpu;
|
||||
int32_t length = sizeof(logicalcpu);
|
||||
if (sysctlbyname("hw.logicalcpu", &logicalcpu, &length, nullptr, 0) == 0) {
|
||||
count = logicalcpu;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
uint32_t OsGetProcessorFeaturesEx(int32_t& vendorID) {
|
||||
// https://en.wikipedia.org/wiki/Mac_transition_to_Intel_processors
|
||||
vendorID = 1;
|
||||
|
||||
static uint32_t features = 0;
|
||||
|
||||
if (!features) {
|
||||
features = 0x80000000;
|
||||
|
||||
int32_t feature;
|
||||
int32_t length;
|
||||
|
||||
length = sizeof(feature);
|
||||
if (sysctlbyname("hw.optional.mmx", &feature, &length, nullptr, 0) == 0 && feature) {
|
||||
features |= 0x2;
|
||||
}
|
||||
length = sizeof(feature);
|
||||
if (sysctlbyname("hw.optional.sse", &feature, &length, nullptr, 0) == 0 && feature) {
|
||||
features |= 0x4;
|
||||
}
|
||||
length = sizeof(feature);
|
||||
if (sysctlbyname("hw.optional.sse2", &feature, &length, nullptr, 0) == 0 && feature) {
|
||||
features |= 0x10;
|
||||
}
|
||||
length = sizeof(feature);
|
||||
if (sysctlbyname("hw.optional.altivec", &feature, &length, nullptr, 0) == 0 && feature) {
|
||||
features |= 0x20;
|
||||
}
|
||||
}
|
||||
|
||||
return features;
|
||||
}
|
||||
|
||||
void OsSystemEnableCpuLog() {
|
||||
}
|
||||
|
||||
uint64_t OsGetProcessorTicksPerSecond() {
|
||||
// TODO
|
||||
return 1000ULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
440
common/processor/win/Processor.cpp
Normal file
440
common/processor/win/Processor.cpp
Normal file
|
|
@ -0,0 +1,440 @@
|
|||
#if defined(WHOA_SYSTEM_WIN)
|
||||
|
||||
#include "common/processor/Processor.hpp"
|
||||
#include "common/time/Time.hpp"
|
||||
#include <cstring>
|
||||
#include <storm/Log.hpp>
|
||||
#include <bc/os/File.hpp>
|
||||
#include <windows.h>
|
||||
|
||||
uint32_t OsGetProcessorCount() {
|
||||
SYSTEM_INFO systeminfo;
|
||||
memset(&systeminfo, 0, sizeof(systeminfo));
|
||||
GetSystemInfo(&systeminfo);
|
||||
return systeminfo.dwNumberOfProcessors ? systeminfo.dwNumberOfProcessors : 1;
|
||||
}
|
||||
|
||||
// NOTE: this is is an x86-only structure
|
||||
struct ProcessorFeatures {
|
||||
// vendor ID string
|
||||
uint32_t std_0b; // 00
|
||||
uint32_t std_0d; // 04
|
||||
uint32_t std_0c; // 08
|
||||
// standard
|
||||
uint32_t std_0a; // 0C
|
||||
|
||||
uint32_t std_1b; // 10
|
||||
uint32_t std_1d; // 14
|
||||
uint32_t std_4a; // 18
|
||||
// extended
|
||||
uint32_t ext_0a; // 1C
|
||||
|
||||
uint32_t ext_1c; // 20
|
||||
uint32_t ext_1d; // 24
|
||||
uint32_t ext_8c; // 28
|
||||
|
||||
// processor brand string
|
||||
uint32_t ext_2a; // 2C
|
||||
uint32_t ext_2b; // 30
|
||||
uint32_t ext_2c; // 34
|
||||
uint32_t ext_2d; // 38
|
||||
|
||||
uint32_t ext_3a; // 3C
|
||||
uint32_t ext_3b; // 40
|
||||
uint32_t ext_3c; // 44
|
||||
uint32_t ext_3d; // 48
|
||||
|
||||
uint32_t ext_4a; // 4c
|
||||
uint32_t ext_4b; // 50
|
||||
uint32_t ext_4c; // 54
|
||||
uint32_t ext_4d; // 58
|
||||
};
|
||||
|
||||
// MinGW implementation
|
||||
|
||||
#if defined(__MINGW32__) && (defined(__x86_64__) || defined(__i386__))
|
||||
|
||||
#include <cpuid.h>
|
||||
|
||||
#define COMMON_GET_PROCESSOR_FEATURES_IMPL 1
|
||||
|
||||
int32_t IOsGetProcessorFeatures(ProcessorFeatures& features) {
|
||||
memset(&features, 0, sizeof(ProcessorFeatures));
|
||||
|
||||
uint32_t eax;
|
||||
uint32_t ebx;
|
||||
uint32_t ecx;
|
||||
uint32_t edx;
|
||||
|
||||
int32_t result = 0;
|
||||
|
||||
if (__get_cpuid(0, &eax, &ebx, &ecx, &edx)) {
|
||||
result = 1;
|
||||
features.std_0a = eax;
|
||||
features.std_0b = ebx;
|
||||
features.std_0d = edx;
|
||||
features.std_0c = ecx;
|
||||
|
||||
if (features.std_0a >= 4) {
|
||||
__get_cpuid(4, &eax, &ebx, &ecx, &edx);
|
||||
features.std_4a = eax;
|
||||
}
|
||||
|
||||
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
|
||||
features.std_1d = edx;
|
||||
features.std_1b = ebx;
|
||||
|
||||
__get_cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
|
||||
if (eax > 0x80000000) {
|
||||
result = 2;
|
||||
features.ext_0a = eax;
|
||||
if (features.ext_0a >= 0x80000008) {
|
||||
__get_cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
|
||||
features.ext_8c = ecx;
|
||||
}
|
||||
if (features.ext_0a >= 0x80000002) {
|
||||
__get_cpuid(0x80000002, &eax, &ebx, &ecx, &edx);
|
||||
features.ext_2a = eax;
|
||||
features.ext_2b = ebx;
|
||||
features.ext_2c = ecx;
|
||||
features.ext_2d = edx;
|
||||
}
|
||||
if (features.ext_0a >= 0x80000003) {
|
||||
__get_cpuid(0x80000003, &eax, &ebx, &ecx, &edx);
|
||||
features.ext_3a = eax;
|
||||
features.ext_3b = ebx;
|
||||
features.ext_3c = ecx;
|
||||
features.ext_3d = edx;
|
||||
}
|
||||
if (features.ext_0a >= 0x80000004) {
|
||||
__get_cpuid(0x80000004, &eax, &ebx, &ecx, &edx);
|
||||
features.ext_4a = eax;
|
||||
features.ext_4b = ebx;
|
||||
features.ext_4c = ecx;
|
||||
features.ext_4d = edx;
|
||||
}
|
||||
__get_cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
|
||||
features.ext_1d = edx;
|
||||
features.ext_1c = ecx;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
|
||||
|
||||
// MSVC implementation
|
||||
|
||||
#include <intrin.h>
|
||||
|
||||
int32_t IOsGetProcessorFeatures(ProcessorFeatures& features) {
|
||||
memset(&features, 0, sizeof(ProcessorFeatures));
|
||||
|
||||
int cpuinfo[4];
|
||||
|
||||
int32_t result = 0;
|
||||
|
||||
__cpuid(0, cpuinfo);
|
||||
if (cpuinfo[0]) {
|
||||
result = 1;
|
||||
features.std_0a = cpuinfo[0];
|
||||
features.std_0b = cpuinfo[1];
|
||||
features.std_0d = cpuinfo[3];
|
||||
features.std_0c = cpuinfo[2];
|
||||
|
||||
if (features.std_0a >= 4) {
|
||||
__cpuid(4, cpuinfo);
|
||||
features.std_4a = cpuinfo[0];
|
||||
}
|
||||
|
||||
__cpuid(1, cpuinfo);
|
||||
features.std_1d = cpuinfo[3];
|
||||
features.std_1b = cpuinfo[0];
|
||||
|
||||
__cpuid(0x80000000, cpuinfo);
|
||||
if (cpuinfo[0] > 0x80000000) {
|
||||
result = 2;
|
||||
features.ext_0a = cpuinfo[0];
|
||||
if (features.ext_0a >= 0x80000008) {
|
||||
__cpuid(0x80000008, cpuinfo);
|
||||
features.ext_8c = cpuinfo[2];
|
||||
}
|
||||
if (features.ext_0a >= 0x80000002) {
|
||||
__cpuid(0x80000002, cpuinfo);
|
||||
features.ext_2a = cpuinfo[0];
|
||||
features.ext_2b = cpuinfo[1];
|
||||
features.ext_2c = cpuinfo[2];
|
||||
features.ext_2d = cpuinfo[3];
|
||||
}
|
||||
if (features.ext_0a >= 0x80000003) {
|
||||
__cpuid(0x80000003, cpuinfo);
|
||||
features.ext_3a = cpuinfo[0];
|
||||
features.ext_3b = cpuinfo[1];
|
||||
features.ext_3c = cpuinfo[2];
|
||||
features.ext_3d = cpuinfo[3];
|
||||
}
|
||||
if (features.ext_0a >= 0x80000004) {
|
||||
__cpuid(0x80000004, cpuinfo);
|
||||
features.ext_4a = cpuinfo[0];
|
||||
features.ext_4b = cpuinfo[1];
|
||||
features.ext_4c = cpuinfo[2];
|
||||
features.ext_4d = cpuinfo[3];
|
||||
}
|
||||
__cpuid(0x80000001, cpuinfo);
|
||||
features.ext_1d = cpuinfo[3];
|
||||
features.ext_1c = cpuinfo[2];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#define COMMON_GET_PROCESSOR_FEATURES_IMPL 1
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(COMMON_GET_PROCESSOR_FEATURES_IMPL)
|
||||
|
||||
// placeholder implementation
|
||||
|
||||
int32_t IOsGetProcessorFeatures(ProcessorFeatures& features) {
|
||||
memset(&features, 0, sizeof(ProcessorFeatures));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static uint64_t s_cpuTicksPerSecond;
|
||||
|
||||
static int32_t s_cpuLogEnabled;
|
||||
|
||||
static int32_t s_haveProcessorFeatures;
|
||||
static uint32_t s_processorFeatures;
|
||||
static uint32_t s_processorCount;
|
||||
static uint32_t s_processorCores;
|
||||
static uint32_t s_processorVendor;
|
||||
static uint32_t s_processorSockets;
|
||||
|
||||
static const char s_vendorIDs[] = {
|
||||
'G', 'e', 'n', 'u', 'i', 'n', 'e', 'I', 'n', 't', 'e', 'l',
|
||||
'A', 'u', 't', 'h', 'e', 'n', 't', 'i', 'c', 'A', 'M', 'D',
|
||||
'C', 'y', 'r', 'i', 'x', 'I', 'n', 's', 't', 'e', 'a', 'd',
|
||||
'C', 'e', 'n', 't', 'a', 'u', 'r', 'H', 'a', 'l', 'l', 's'
|
||||
};
|
||||
|
||||
void OsSystemEnableCpuLog() {
|
||||
s_cpuLogEnabled = 1;
|
||||
}
|
||||
|
||||
void IOsSystemCpuLog(ProcessorFeatures& features) {
|
||||
if (s_cpuLogEnabled) {
|
||||
OsCreateDirectory("Logs", 0);
|
||||
HSLOG log;
|
||||
SLogCreate("Logs\\cpu.log", 0, &log);
|
||||
if (s_processorVendor == 4) {
|
||||
SLogWrite(log, "UNABLE TO IDENTIFY CPU");
|
||||
SLogClose(log);
|
||||
return;
|
||||
}
|
||||
SLogWrite(log, "vendor: %d", s_processorVendor);
|
||||
SLogWrite(log, "features: %08X", s_processorFeatures & 0x7fffffff);
|
||||
SLogWrite(log, "sockets: %d", s_processorSockets);
|
||||
SLogWrite(log, "cores: %d", s_processorCores);
|
||||
SLogWrite(log, "processors: %d", s_processorCount);
|
||||
|
||||
char vendorID[13];
|
||||
strncpy(vendorID, reinterpret_cast<char*>(&features), 12);
|
||||
vendorID[12] = '\0';
|
||||
SLogWrite(log, "vendor id string= %s", vendorID);
|
||||
|
||||
SLogWrite(log, "standard (%d): 1b=%08X 1d=%08x 4a=%08X", features.std_0a, features.std_1b, features.std_1d, features.std_4a);
|
||||
SLogWrite(log, "extended (%d): 1c=%08X 1d=%08x 8c=%08X", features.ext_0a & 0x7fffffff, features.ext_1c, features.ext_1d, features.ext_8c);
|
||||
|
||||
auto brand = reinterpret_cast<const char*>(&features.ext_2a);
|
||||
while (*brand && *brand == ' ') {
|
||||
brand++;
|
||||
}
|
||||
SLogWrite(log, "processor brand string= %s", brand);
|
||||
SLogClose(log);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t IOsParseProcessorFrequency(const char* str) {
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto length = strlen(str);
|
||||
// z
|
||||
auto z = &str[length - 1];
|
||||
if (z <= str) {
|
||||
return 0;
|
||||
}
|
||||
if (*z != 'z') {
|
||||
return 0;
|
||||
}
|
||||
// Hz
|
||||
auto H = &str[length - 2];
|
||||
if (H <= str) {
|
||||
return 0;
|
||||
}
|
||||
if (*H != 'H') {
|
||||
return 0;
|
||||
}
|
||||
// GHz, MHz, THz (!)
|
||||
auto s = &str[length - 3];
|
||||
if (s <= str) {
|
||||
return 0;
|
||||
}
|
||||
double scale;
|
||||
if (*s == 'G') {
|
||||
scale = 1000000000.0;
|
||||
} else if (*s == 'M') {
|
||||
scale = 1000000.0;
|
||||
} else if (*s == 'T') {
|
||||
scale = 1000000000000.0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// quantity (floating-point)
|
||||
auto q = &str[length - 4];
|
||||
char quantitystr[48];
|
||||
memset(quantitystr, 0, sizeof(quantitystr));
|
||||
quantitystr[47] = '\0';
|
||||
for (auto p = quantitystr + 46; q > str; p--) {
|
||||
if (!*q || (*q < '0' || *q > '9') && *q != '.') {
|
||||
break;
|
||||
}
|
||||
*p = *q;
|
||||
q--;
|
||||
}
|
||||
auto quantity = atof(quantitystr);
|
||||
if (quantity == 0.0) {
|
||||
return 0;
|
||||
}
|
||||
s_cpuTicksPerSecond = static_cast<uint64_t>(quantity * scale);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t IOsGetPowerProfFrequency() {
|
||||
auto library = LoadLibrary(TEXT("powrprof.dll"));
|
||||
if (!library) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t result = 0;
|
||||
|
||||
typedef NTSTATUS (*LPCALLNTPOWERINFORMATIONFUNC)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
|
||||
auto CallNtPowerInformation = reinterpret_cast<LPCALLNTPOWERINFORMATIONFUNC>(GetProcAddress(library, "CallNtPowerInformation"));
|
||||
if (CallNtPowerInformation) {
|
||||
struct PROCESSOR_POWER_INFORMATION {
|
||||
ULONG Number;
|
||||
ULONG MaxMhz;
|
||||
ULONG CurrentMhz;
|
||||
ULONG MhzLimit;
|
||||
ULONG MaxIdleState;
|
||||
ULONG CurrentIdleState;
|
||||
};
|
||||
PROCESSOR_POWER_INFORMATION info[4];
|
||||
if (!CallNtPowerInformation(ProcessorInformation, nullptr, 0, &info, sizeof(info))) {
|
||||
s_cpuTicksPerSecond = 1000000LL * static_cast<int64_t>(info[0].MaxMhz);
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
FreeLibrary(library);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t OsGetProcessorFeaturesEx(int32_t& vendorID) {
|
||||
if (s_haveProcessorFeatures) {
|
||||
vendorID = s_processorVendor;
|
||||
return s_processorFeatures;
|
||||
}
|
||||
|
||||
s_haveProcessorFeatures = 1;
|
||||
|
||||
s_processorCores = 1;
|
||||
s_processorSockets = 1;
|
||||
|
||||
s_processorFeatures = 0x80000000;
|
||||
|
||||
ProcessorFeatures features;
|
||||
if (!IOsGetProcessorFeatures(features)) {
|
||||
s_processorVendor = 4;
|
||||
} else {
|
||||
if ((features.std_1d & 0x800000) != 0) {
|
||||
s_processorFeatures |= 0x2;
|
||||
}
|
||||
if ((features.std_1d & 0x2000000) != 0) {
|
||||
s_processorFeatures |= 0x4;
|
||||
}
|
||||
if ((features.ext_1d & 0x80000000) != 0) {
|
||||
s_processorFeatures |= 0x8;
|
||||
}
|
||||
if ((features.std_1d & 0x4000000) != 0) {
|
||||
s_processorFeatures |= 0x10;
|
||||
}
|
||||
|
||||
auto vendorstring = reinterpret_cast<char*>(&features);
|
||||
|
||||
// Intel
|
||||
if (!strncmp(vendorstring, s_vendorIDs, 12)) {
|
||||
s_processorVendor = 1;
|
||||
auto cores = (features.std_4a >> 0x1A);
|
||||
if (features.std_0a >= 4 && cores) {
|
||||
s_processorFeatures |= 0x40;
|
||||
s_processorCores = cores + 1;
|
||||
}
|
||||
// AMD
|
||||
} else if (!strncmp(vendorstring, s_vendorIDs + 12, 12)) {
|
||||
s_processorVendor = 2;
|
||||
auto cores = static_cast<uint8_t>(features.ext_8c);
|
||||
if (cores) {
|
||||
s_processorFeatures |= 0x40;
|
||||
s_processorCores = cores + 1;
|
||||
}
|
||||
// Cyrix
|
||||
} else if (!strncmp(vendorstring, s_vendorIDs + 24, 12)) {
|
||||
// ???
|
||||
// Centaur
|
||||
} else if (!strncmp(vendorstring, s_vendorIDs + 36, 12)) {
|
||||
// ???
|
||||
}
|
||||
|
||||
IOsSystemCpuLog(features);
|
||||
}
|
||||
|
||||
if (!IOsParseProcessorFrequency(reinterpret_cast<char*>(&features) + 44)) {
|
||||
static int32_t s_checkedPowerProfInfo = 0;
|
||||
static int32_t s_gotPowerProfFrequency = 0;
|
||||
|
||||
if (!s_checkedPowerProfInfo) {
|
||||
s_gotPowerProfFrequency = IOsGetPowerProfFrequency();
|
||||
s_checkedPowerProfInfo = 1;
|
||||
}
|
||||
|
||||
if (!s_gotPowerProfFrequency) {
|
||||
s_cpuTicksPerSecond = OsGetAsyncClocksPerSecond();
|
||||
}
|
||||
}
|
||||
|
||||
vendorID = s_processorVendor;
|
||||
return s_processorFeatures;
|
||||
}
|
||||
|
||||
uint64_t OsGetProcessorTicksPerSecond() {
|
||||
if (!s_cpuTicksPerSecond) {
|
||||
int32_t vendorID;
|
||||
OsGetProcessorFeaturesEx(vendorID);
|
||||
}
|
||||
|
||||
return s_cpuTicksPerSecond;
|
||||
}
|
||||
|
||||
#endif
|
||||
27
common/time/Time.hpp
Normal file
27
common/time/Time.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef COMMON_TIME_TIME_HPP
|
||||
#define COMMON_TIME_TIME_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum TimingMethod {
|
||||
NotSet = -1,
|
||||
BestAvailable = 0,
|
||||
// GetTickCount (Windows), mach_absolute_time (MacOS)
|
||||
SystemMethod1 = 1,
|
||||
// QueryPerformanceCounter (Windows), Carbon Microseconds (MacOS)
|
||||
SystemMethod2 = 2
|
||||
};
|
||||
|
||||
void OsTimeStartup(TimingMethod timingMethod);
|
||||
|
||||
void OsTimeShutdown();
|
||||
|
||||
uint64_t OsGetAsyncTimeMs();
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise();
|
||||
|
||||
int64_t OsGetAsyncClocksPerSecond();
|
||||
|
||||
void OsSleep(uint32_t duration);
|
||||
|
||||
#endif
|
||||
30
common/time/linux/Time.cpp
Normal file
30
common/time/linux/Time.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#if defined(WHOA_SYSTEM_LINUX)
|
||||
|
||||
#include "common/time/Time.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <unistd.h>
|
||||
|
||||
void OsTimeStartup(TimingMethod timingMethod) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void OsTimeShutdown() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
uint64_t ticks = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
return ticks;
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMs() {
|
||||
return OsGetAsyncTimeMsPrecise();
|
||||
}
|
||||
|
||||
void OsSleep(uint32_t duration) {
|
||||
usleep(duration);
|
||||
}
|
||||
|
||||
#endif
|
||||
36
common/time/mac/Time.cpp
Normal file
36
common/time/mac/Time.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#if defined(WHOA_SYSTEM_MAC)
|
||||
|
||||
#include "common/time/Time.hpp"
|
||||
#include "common/time/GetAsyncTimeMs.hpp"
|
||||
#include <mach/mach_time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void OsTimeStartup(TimingMethod timingMethod) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void OsTimeShutdown() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||
static mach_timebase_info_data_t timebase;
|
||||
|
||||
if (timebase.denom == 0) {
|
||||
mach_timebase_info(&timebase);
|
||||
}
|
||||
|
||||
uint64_t ticks = mach_absolute_time();
|
||||
|
||||
return ticks * (timebase.numer / timebase.denom) / 100000;
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMs() {
|
||||
return OsGetAsyncTimeMsPrecise();
|
||||
}
|
||||
|
||||
void OsSleep(uint32_t duration) {
|
||||
usleep(duration);
|
||||
}
|
||||
|
||||
#endif
|
||||
52
common/time/win/Time.cpp
Normal file
52
common/time/win/Time.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#if defined(WHOA_SYSTEM_WIN)
|
||||
|
||||
#include "common/time/Time.hpp"
|
||||
#include "common/time/win/TimeManager.hpp"
|
||||
#include <storm/Memory.hpp>
|
||||
#include <windows.h>
|
||||
|
||||
static OsTimeManager* s_OsTimeMgr;
|
||||
|
||||
void OsTimeManagerCreate(TimingMethod timingMethod) {
|
||||
auto m = SMemAlloc(sizeof(OsTimeManager), __FILE__, __LINE__, 0x8);
|
||||
if (m) {
|
||||
s_OsTimeMgr = new (m) OsTimeManager(timingMethod);
|
||||
}
|
||||
}
|
||||
|
||||
void OsTimeManagerDestroy() {
|
||||
if (s_OsTimeMgr) {
|
||||
SMemFree(s_OsTimeMgr, "delete", -1, 0x0);
|
||||
s_OsTimeMgr = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void OsTimeStartup(TimingMethod timingMethod) {
|
||||
OsTimeManagerCreate(timingMethod);
|
||||
}
|
||||
|
||||
void OsTimeShutdown() {
|
||||
OsTimeManagerDestroy();
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||
return s_OsTimeMgr->Snapshot();
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMs() {
|
||||
return s_OsTimeMgr->Snapshot();
|
||||
}
|
||||
|
||||
int64_t OsGetAsyncClocksPerSecond() {
|
||||
if (s_OsTimeMgr->timingMethod == SystemMethod2) {
|
||||
return s_OsTimeMgr->performanceFrequency;
|
||||
} else {
|
||||
return 1000LL;
|
||||
}
|
||||
}
|
||||
|
||||
void OsSleep(uint32_t duration) {
|
||||
Sleep(duration);
|
||||
}
|
||||
|
||||
#endif
|
||||
110
common/time/win/TimeManager.cpp
Normal file
110
common/time/win/TimeManager.cpp
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
#if defined(WHOA_SYSTEM_WIN)
|
||||
|
||||
#include "common/time/win/TimeManager.hpp"
|
||||
#include "common/time/Time.hpp"
|
||||
#include "common/processor/Processor.hpp"
|
||||
#include <windows.h>
|
||||
|
||||
OsTimeManager::OsTimeManager(TimingMethod tm) {
|
||||
this->timingMethod = NotSet;
|
||||
|
||||
auto method = this->Calibrate();
|
||||
|
||||
if (tm != BestAvailable && method != timingMethod) {
|
||||
if (tm == SystemMethod2 && method == SystemMethod1) {
|
||||
this->timingTestError = 5;
|
||||
}
|
||||
method = tm;
|
||||
}
|
||||
|
||||
this->timingMethod = method;
|
||||
auto freq = method == SystemMethod2 ? this->performanceFrequency : static_cast<int64_t>(1000LL);
|
||||
this->scaleToMs = 1000.0 / static_cast<double>(freq);
|
||||
this->timeBegin = 0;
|
||||
}
|
||||
|
||||
TimingMethod OsTimeManager::Calibrate() {
|
||||
if (!QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&this->performanceFrequency))) {
|
||||
this->timingTestError = 1;
|
||||
return SystemMethod1;
|
||||
}
|
||||
|
||||
if (this->performanceFrequency == 0) {
|
||||
this->timingTestError = 2;
|
||||
return SystemMethod1;
|
||||
}
|
||||
|
||||
auto process = GetCurrentProcess();
|
||||
auto thread = GetCurrentThread();
|
||||
auto priorityClass = GetPriorityClass(process);
|
||||
auto threadPriority = GetThreadPriority(thread);
|
||||
SetPriorityClass(process, HIGH_PRIORITY_CLASS);
|
||||
SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL);
|
||||
OsSleep(0);
|
||||
|
||||
this->timingTestError = 0;
|
||||
|
||||
DWORD tc1 = GetTickCount();
|
||||
DWORD tc2 = tc1;
|
||||
while (tc1 == tc2) {
|
||||
tc2 = GetTickCount();
|
||||
}
|
||||
|
||||
int64_t pc1;
|
||||
int64_t pc2;
|
||||
|
||||
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&pc1));
|
||||
|
||||
auto nproc = OsGetProcessorCount();
|
||||
if (nproc > 1) {
|
||||
DWORD_PTR processAffinityMask;
|
||||
DWORD_PTR systemAffinityMask;
|
||||
GetProcessAffinityMask(process, &processAffinityMask, &systemAffinityMask);
|
||||
|
||||
for (uint32_t i = 0; i < 512; i++) {
|
||||
SetThreadAffinityMask(thread, 1 << static_cast<DWORD_PTR>(static_cast<uint8_t>(i % nproc) & 0x1F));
|
||||
OsSleep(0);
|
||||
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&pc2));
|
||||
if (pc2 <= pc1) {
|
||||
this->timingTestError = 4;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SetThreadAffinityMask(thread, processAffinityMask);
|
||||
OsSleep(0);
|
||||
}
|
||||
|
||||
if (this->timingTestError == 0) {
|
||||
auto tc3 = GetTickCount();
|
||||
while ((tc3 - tc2) < 0xFA) {
|
||||
tc3 = GetTickCount();
|
||||
}
|
||||
auto tc4 = GetTickCount();
|
||||
while (tc3 == tc4) {
|
||||
tc4 = GetTickCount();
|
||||
}
|
||||
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&pc2));
|
||||
|
||||
if (std::abs(tc4 - static_cast<int64_t>(static_cast<double>(pc2 - pc1) / this->performanceFrequency * 1000.0) - tc2) >= 5) {
|
||||
this->timingTestError = 3;
|
||||
}
|
||||
}
|
||||
|
||||
SetPriorityClass(process, priorityClass);
|
||||
SetThreadPriority(thread, threadPriority);
|
||||
OsSleep(0);
|
||||
return this->timingMethod == BestAvailable ? SystemMethod2 : SystemMethod1;
|
||||
}
|
||||
|
||||
uint64_t OsTimeManager::Snapshot() {
|
||||
if (this->timingMethod != SystemMethod2) {
|
||||
return static_cast<uint64_t>((static_cast<double>(GetTickCount()) * this->scaleToMs) + this->timeBegin);
|
||||
}
|
||||
|
||||
int64_t performanceCount;
|
||||
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&performanceCount));
|
||||
return static_cast<uint64_t>((static_cast<double>(performanceCount) * this->scaleToMs) + this->timeBegin);
|
||||
}
|
||||
|
||||
#endif
|
||||
19
common/time/win/TimeManager.hpp
Normal file
19
common/time/win/TimeManager.hpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef COMMON_TIME_WIN_TIME_MANAGER_HPP
|
||||
#define COMMON_TIME_WIN_TIME_MANAGER_HPP
|
||||
|
||||
#include "common/time/Time.hpp"
|
||||
|
||||
class OsTimeManager {
|
||||
public:
|
||||
double scaleToMs;
|
||||
TimingMethod timingMethod;
|
||||
uint32_t timingTestError;
|
||||
int64_t performanceFrequency;
|
||||
double timeBegin;
|
||||
|
||||
OsTimeManager(TimingMethod timingMethod);
|
||||
TimingMethod Calibrate();
|
||||
uint64_t Snapshot();
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue