diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/page_table.h | 12 | ||||
| -rw-r--r-- | src/common/settings.cpp | 3 | ||||
| -rw-r--r-- | src/common/time_zone.cpp | 47 | ||||
| -rw-r--r-- | src/common/wall_clock.cpp | 4 |
4 files changed, 29 insertions, 37 deletions
diff --git a/src/common/page_table.h b/src/common/page_table.h index fec8378f3..e653d52ad 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h | |||
| @@ -51,7 +51,7 @@ struct PageTable { | |||
| 51 | class PageInfo { | 51 | class PageInfo { |
| 52 | public: | 52 | public: |
| 53 | /// Returns the page pointer | 53 | /// Returns the page pointer |
| 54 | [[nodiscard]] u8* Pointer() const noexcept { | 54 | [[nodiscard]] uintptr_t Pointer() const noexcept { |
| 55 | return ExtractPointer(raw.load(std::memory_order_relaxed)); | 55 | return ExtractPointer(raw.load(std::memory_order_relaxed)); |
| 56 | } | 56 | } |
| 57 | 57 | ||
| @@ -61,7 +61,7 @@ struct PageTable { | |||
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | /// Returns the page pointer and attribute pair, extracted from the same atomic read | 63 | /// Returns the page pointer and attribute pair, extracted from the same atomic read |
| 64 | [[nodiscard]] std::pair<u8*, PageType> PointerType() const noexcept { | 64 | [[nodiscard]] std::pair<uintptr_t, PageType> PointerType() const noexcept { |
| 65 | const uintptr_t non_atomic_raw = raw.load(std::memory_order_relaxed); | 65 | const uintptr_t non_atomic_raw = raw.load(std::memory_order_relaxed); |
| 66 | return {ExtractPointer(non_atomic_raw), ExtractType(non_atomic_raw)}; | 66 | return {ExtractPointer(non_atomic_raw), ExtractType(non_atomic_raw)}; |
| 67 | } | 67 | } |
| @@ -73,13 +73,13 @@ struct PageTable { | |||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | /// Write a page pointer and type pair atomically | 75 | /// Write a page pointer and type pair atomically |
| 76 | void Store(u8* pointer, PageType type) noexcept { | 76 | void Store(uintptr_t pointer, PageType type) noexcept { |
| 77 | raw.store(reinterpret_cast<uintptr_t>(pointer) | static_cast<uintptr_t>(type)); | 77 | raw.store(pointer | static_cast<uintptr_t>(type)); |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | /// Unpack a pointer from a page info raw representation | 80 | /// Unpack a pointer from a page info raw representation |
| 81 | [[nodiscard]] static u8* ExtractPointer(uintptr_t raw) noexcept { | 81 | [[nodiscard]] static uintptr_t ExtractPointer(uintptr_t raw) noexcept { |
| 82 | return reinterpret_cast<u8*>(raw & (~uintptr_t{0} << ATTRIBUTE_BITS)); | 82 | return raw & (~uintptr_t{0} << ATTRIBUTE_BITS); |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | /// Unpack a page type from a page info raw representation | 85 | /// Unpack a page type from a page info raw representation |
diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 491adc30e..15fd2e222 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp | |||
| @@ -72,7 +72,8 @@ std::string GetTimeZoneString(TimeZone time_zone) { | |||
| 72 | 72 | ||
| 73 | std::string location_name; | 73 | std::string location_name; |
| 74 | if (time_zone_index == 0) { // Auto | 74 | if (time_zone_index == 0) { // Auto |
| 75 | #if __cpp_lib_chrono >= 201907L | 75 | #if __cpp_lib_chrono >= 201907L && !defined(MINGW) |
| 76 | // Disabled for MinGW -- tzdb always returns Etc/UTC | ||
| 76 | try { | 77 | try { |
| 77 | const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); | 78 | const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); |
| 78 | const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); | 79 | const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); |
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index d8d7896c6..69e728a9d 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp | |||
| @@ -4,13 +4,13 @@ | |||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <exception> | 5 | #include <exception> |
| 6 | #include <iomanip> | 6 | #include <iomanip> |
| 7 | #include <map> | ||
| 7 | #include <sstream> | 8 | #include <sstream> |
| 8 | #include <stdexcept> | 9 | #include <stdexcept> |
| 9 | #include <fmt/chrono.h> | 10 | #include <fmt/chrono.h> |
| 10 | #include <fmt/core.h> | 11 | #include <fmt/core.h> |
| 11 | 12 | ||
| 12 | #include "common/logging/log.h" | 13 | #include "common/logging/log.h" |
| 13 | #include "common/settings.h" | ||
| 14 | #include "common/time_zone.h" | 14 | #include "common/time_zone.h" |
| 15 | 15 | ||
| 16 | namespace Common::TimeZone { | 16 | namespace Common::TimeZone { |
| @@ -33,32 +33,29 @@ std::string GetDefaultTimeZone() { | |||
| 33 | return "GMT"; | 33 | return "GMT"; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | static std::string GetOsTimeZoneOffset() { | 36 | // Results are not comparable to seconds since Epoch |
| 37 | const std::time_t t{std::time(nullptr)}; | 37 | static std::time_t TmSpecToSeconds(const struct std::tm& spec) { |
| 38 | const std::tm tm{*std::localtime(&t)}; | 38 | const int year = spec.tm_year - 1; // Years up to now |
| 39 | 39 | const int leap_years = year / 4 - year / 100; | |
| 40 | return fmt::format("{:%z}", tm); | 40 | std::time_t cumulative = spec.tm_year; |
| 41 | } | 41 | cumulative = cumulative * 365 + leap_years + spec.tm_yday; // Years to days |
| 42 | 42 | cumulative = cumulative * 24 + spec.tm_hour; // Days to hours | |
| 43 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { | 43 | cumulative = cumulative * 60 + spec.tm_min; // Hours to minutes |
| 44 | try { | 44 | cumulative = cumulative * 60 + spec.tm_sec; // Minutes to seconds |
| 45 | return std::stoi(timezone); | 45 | return cumulative; |
| 46 | } catch (const std::invalid_argument&) { | ||
| 47 | LOG_CRITICAL(Common, "invalid_argument with {}!", timezone); | ||
| 48 | return 0; | ||
| 49 | } catch (const std::out_of_range&) { | ||
| 50 | LOG_CRITICAL(Common, "out_of_range with {}!", timezone); | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | } | 46 | } |
| 54 | 47 | ||
| 55 | std::chrono::seconds GetCurrentOffsetSeconds() { | 48 | std::chrono::seconds GetCurrentOffsetSeconds() { |
| 56 | const int offset{ConvertOsTimeZoneOffsetToInt(GetOsTimeZoneOffset())}; | 49 | const std::time_t t{std::time(nullptr)}; |
| 50 | const std::tm local{*std::localtime(&t)}; | ||
| 51 | const std::tm gmt{*std::gmtime(&t)}; | ||
| 57 | 52 | ||
| 58 | int seconds{(offset / 100) * 60 * 60}; // Convert hour component to seconds | 53 | // gmt_seconds is a different offset than time(nullptr) |
| 59 | seconds += (offset % 100) * 60; // Convert minute component to seconds | 54 | const auto gmt_seconds = TmSpecToSeconds(gmt); |
| 55 | const auto local_seconds = TmSpecToSeconds(local); | ||
| 56 | const auto seconds_offset = local_seconds - gmt_seconds; | ||
| 60 | 57 | ||
| 61 | return std::chrono::seconds{seconds}; | 58 | return std::chrono::seconds{seconds_offset}; |
| 62 | } | 59 | } |
| 63 | 60 | ||
| 64 | // Key is [Hours * 100 + Minutes], multiplied by 100 if DST | 61 | // Key is [Hours * 100 + Minutes], multiplied by 100 if DST |
| @@ -71,11 +68,6 @@ const static std::map<s64, const char*> off_timezones = { | |||
| 71 | }; | 68 | }; |
| 72 | 69 | ||
| 73 | std::string FindSystemTimeZone() { | 70 | std::string FindSystemTimeZone() { |
| 74 | #if defined(MINGW) | ||
| 75 | // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/ | ||
| 76 | // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" | ||
| 77 | return timezones[0]; | ||
| 78 | #else | ||
| 79 | const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count()); | 71 | const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count()); |
| 80 | 72 | ||
| 81 | const s64 minutes = seconds / 60; | 73 | const s64 minutes = seconds / 60; |
| @@ -97,7 +89,6 @@ std::string FindSystemTimeZone() { | |||
| 97 | } | 89 | } |
| 98 | } | 90 | } |
| 99 | return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours)); | 91 | return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours)); |
| 100 | #endif | ||
| 101 | } | 92 | } |
| 102 | 93 | ||
| 103 | } // namespace Common::TimeZone | 94 | } // namespace Common::TimeZone |
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index dc0dcbd68..71e15ab4c 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp | |||
| @@ -56,12 +56,12 @@ std::unique_ptr<WallClock> CreateOptimalClock() { | |||
| 56 | #ifdef ARCHITECTURE_x86_64 | 56 | #ifdef ARCHITECTURE_x86_64 |
| 57 | const auto& caps = GetCPUCaps(); | 57 | const auto& caps = GetCPUCaps(); |
| 58 | 58 | ||
| 59 | if (caps.invariant_tsc && caps.tsc_frequency >= WallClock::GPUTickFreq) { | 59 | if (caps.invariant_tsc && caps.tsc_frequency >= std::nano::den) { |
| 60 | return std::make_unique<X64::NativeClock>(caps.tsc_frequency); | 60 | return std::make_unique<X64::NativeClock>(caps.tsc_frequency); |
| 61 | } else { | 61 | } else { |
| 62 | // Fallback to StandardWallClock if the hardware TSC | 62 | // Fallback to StandardWallClock if the hardware TSC |
| 63 | // - Is not invariant | 63 | // - Is not invariant |
| 64 | // - Is not more precise than GPUTickFreq | 64 | // - Is not more precise than 1 GHz (1ns resolution) |
| 65 | return std::make_unique<StandardWallClock>(); | 65 | return std::make_unique<StandardWallClock>(); |
| 66 | } | 66 | } |
| 67 | #else | 67 | #else |