diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 6a50605..aef77c8 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -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" ) diff --git a/common/Processor.hpp b/common/Processor.hpp new file mode 100644 index 0000000..136e41b --- /dev/null +++ b/common/Processor.hpp @@ -0,0 +1,6 @@ +#ifndef COMMON_PROCESSOR_HPP +#define COMMON_PROCESSOR_HPP + +#include "common/processor/Processor.hpp" + +#endif \ No newline at end of file diff --git a/common/Time.cpp b/common/Time.cpp deleted file mode 100644 index e31738d..0000000 --- a/common/Time.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "common/Time.hpp" - -#if defined(WHOA_SYSTEM_WIN) -#include -#elif defined(WHOA_SYSTEM_MAC) -#include -#include -#elif defined(WHOA_SYSTEM_LINUX) -#include -#include -#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(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 -} diff --git a/common/Time.hpp b/common/Time.hpp index eadf02e..bc73e14 100644 --- a/common/Time.hpp +++ b/common/Time.hpp @@ -1,12 +1,6 @@ #ifndef COMMON_TIME_HPP #define COMMON_TIME_HPP -#include - -uint64_t OsGetAsyncTimeMs(); - -uint64_t OsGetAsyncTimeMsPrecise(); - -void OsSleep(uint32_t duration); +#include "common/time/Time.hpp" #endif diff --git a/common/processor/Processor.cpp b/common/processor/Processor.cpp new file mode 100644 index 0000000..aa30637 --- /dev/null +++ b/common/processor/Processor.cpp @@ -0,0 +1,6 @@ +#include "common/processor/Processor.hpp" + +uint32_t OsGetProcessorFeatures() { + int32_t vendorID; + return OsGetProcessorFeaturesEx(vendorID); +} \ No newline at end of file diff --git a/common/processor/Processor.hpp b/common/processor/Processor.hpp new file mode 100644 index 0000000..f45118d --- /dev/null +++ b/common/processor/Processor.hpp @@ -0,0 +1,16 @@ +#ifndef COMMON_PROCESSOR_PROCESSOR_HPP +#define COMMON_PROCESSOR_PROCESSOR_HPP + +#include + +uint32_t OsGetProcessorCount(); + +uint32_t OsGetProcessorFeatures(); + +uint32_t OsGetProcessorFeaturesEx(int32_t& vendorID); + +void OsSystemEnableCpuLog(); + +uint64_t OsGetProcessorTicksPerSecond(); + +#endif \ No newline at end of file diff --git a/common/processor/linux/Processor.cpp b/common/processor/linux/Processor.cpp new file mode 100644 index 0000000..7bd4d9d --- /dev/null +++ b/common/processor/linux/Processor.cpp @@ -0,0 +1,27 @@ +#if defined(WHOA_SYSTEM_LINUX) + +#include "common/processor/Processor.hpp" +#include + +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 \ No newline at end of file diff --git a/common/processor/mac/Processor.cpp b/common/processor/mac/Processor.cpp new file mode 100644 index 0000000..7836fb3 --- /dev/null +++ b/common/processor/mac/Processor.cpp @@ -0,0 +1,58 @@ +#if defined(WHOA_SYSTEM_MAC) + +#include "common/processor/Processor.hpp" +#include +#include + +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 \ No newline at end of file diff --git a/common/processor/win/Processor.cpp b/common/processor/win/Processor.cpp new file mode 100644 index 0000000..87acff2 --- /dev/null +++ b/common/processor/win/Processor.cpp @@ -0,0 +1,440 @@ +#if defined(WHOA_SYSTEM_WIN) + +#include "common/processor/Processor.hpp" +#include "common/time/Time.hpp" +#include +#include +#include +#include + +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 + +#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 + +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(&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(&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(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(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(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(&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(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(&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 \ No newline at end of file diff --git a/common/time/Time.hpp b/common/time/Time.hpp new file mode 100644 index 0000000..a3e08d8 --- /dev/null +++ b/common/time/Time.hpp @@ -0,0 +1,27 @@ +#ifndef COMMON_TIME_TIME_HPP +#define COMMON_TIME_TIME_HPP + +#include + +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 diff --git a/common/time/linux/Time.cpp b/common/time/linux/Time.cpp new file mode 100644 index 0000000..d4a1ea5 --- /dev/null +++ b/common/time/linux/Time.cpp @@ -0,0 +1,30 @@ +#if defined(WHOA_SYSTEM_LINUX) + +#include "common/time/Time.hpp" + +#include +#include + +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(now.time_since_epoch()).count(); + return ticks; +} + +uint64_t OsGetAsyncTimeMs() { + return OsGetAsyncTimeMsPrecise(); +} + +void OsSleep(uint32_t duration) { + usleep(duration); +} + +#endif \ No newline at end of file diff --git a/common/time/mac/Time.cpp b/common/time/mac/Time.cpp new file mode 100644 index 0000000..29cd765 --- /dev/null +++ b/common/time/mac/Time.cpp @@ -0,0 +1,36 @@ +#if defined(WHOA_SYSTEM_MAC) + +#include "common/time/Time.hpp" +#include "common/time/GetAsyncTimeMs.hpp" +#include +#include + +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 \ No newline at end of file diff --git a/common/time/win/Time.cpp b/common/time/win/Time.cpp new file mode 100644 index 0000000..6b2b2a3 --- /dev/null +++ b/common/time/win/Time.cpp @@ -0,0 +1,52 @@ +#if defined(WHOA_SYSTEM_WIN) + +#include "common/time/Time.hpp" +#include "common/time/win/TimeManager.hpp" +#include +#include + +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 \ No newline at end of file diff --git a/common/time/win/TimeManager.cpp b/common/time/win/TimeManager.cpp new file mode 100644 index 0000000..16cd1fb --- /dev/null +++ b/common/time/win/TimeManager.cpp @@ -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 + +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(1000LL); + this->scaleToMs = 1000.0 / static_cast(freq); + this->timeBegin = 0; +} + +TimingMethod OsTimeManager::Calibrate() { + if (!QueryPerformanceFrequency(reinterpret_cast(&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(&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(static_cast(i % nproc) & 0x1F)); + OsSleep(0); + QueryPerformanceCounter(reinterpret_cast(&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(&pc2)); + + if (std::abs(tc4 - static_cast(static_cast(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((static_cast(GetTickCount()) * this->scaleToMs) + this->timeBegin); + } + + int64_t performanceCount; + QueryPerformanceCounter(reinterpret_cast(&performanceCount)); + return static_cast((static_cast(performanceCount) * this->scaleToMs) + this->timeBegin); +} + +#endif \ No newline at end of file diff --git a/common/time/win/TimeManager.hpp b/common/time/win/TimeManager.hpp new file mode 100644 index 0000000..db98391 --- /dev/null +++ b/common/time/win/TimeManager.hpp @@ -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 \ No newline at end of file