-
Notifications
You must be signed in to change notification settings - Fork 22
Improve time handling #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7794118
time: use rdtsc and vcounter and adjust WRT
nullequal 8a35426
services/acc: use system_clock time
nullequal f7f8258
use a proper wall clock
SamoZ256 61c8744
common: fix compilation on X86
SamoZ256 c19b66f
Merge pull request #1 from SamoZ256/better-time
nullequal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,58 @@ | ||
| #pragma once | ||
|
|
||
| // TODO: support cross-platform time functions | ||
|
|
||
| #include <chrono> | ||
| #include <mach/mach_time.h> | ||
|
|
||
| #if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) | ||
| #include <x86intrin.h> | ||
| #endif | ||
|
|
||
| #include "common/types.hpp" | ||
| #include "core/hw/tegra_x1/cpu/const.hpp" | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| namespace hydra { | ||
|
|
||
| inline u64 get_absolute_time() { return mach_absolute_time(); } | ||
| #if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) | ||
|
|
||
| inline u64 GetSystemTick() { | ||
| _mm_lfence(); | ||
| u64 res = __rdtsc(); | ||
| _mm_lfence(); | ||
| return res; | ||
| } | ||
|
|
||
| inline u64 GetSystemFrequency() { | ||
| auto nsc_start = std::chrono::steady_clock::now().time_since_epoch(); | ||
| u64 tsc_start = GetSystemTick(); | ||
| // More sleep, more precision. | ||
| std::this_thread::sleep_for(10ms); | ||
| auto nsc_end = std::chrono::steady_clock::now().time_since_epoch(); | ||
| u64 tsc_end = GetSystemTick(); | ||
| u64 ns_diff = | ||
| static_cast<u64>(std::chrono::duration_cast<std::chrono::nanoseconds>( | ||
| nsc_end - nsc_start) | ||
| .count()); | ||
| u64 res = (tsc_end - tsc_start) * 1000000000ULL / (ns_diff); | ||
| res = res + 100'000 / 2; | ||
| res -= res % 100'000; | ||
| return res; | ||
| } | ||
|
|
||
| #elif defined(_M_ARM64) || defined(__aarch64__) | ||
|
|
||
| inline u64 GetSystemTick() { | ||
| u64 res; | ||
| __asm__ __volatile__("mrs %0, cntvct_el0; " : "=r"(res)::"memory"); | ||
| return res; | ||
| } | ||
|
|
||
| inline u64 GetSystemFrequency() { | ||
| u64 res; | ||
| __asm__ __volatile__("mrs %0, cntfrq_el0; isb; " : "=r"(res)::"memory"); | ||
| return res; | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| } // namespace hydra | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include "core/hw/wall_clock.hpp" | ||
|
|
||
| namespace hydra::hw { | ||
|
|
||
| namespace { | ||
|
|
||
| u128 GetFactor(u64 num, u64 den) { | ||
| return (static_cast<u128>(num) << 64) / den; | ||
| } | ||
|
|
||
| u64 MultiplyByFactor(u64 num, u128 factor) { return (num * factor) >> 64; } | ||
|
|
||
| } // namespace | ||
|
|
||
| SINGLETON_DEFINE_GET_INSTANCE(WallClock, Other) | ||
|
|
||
| WallClock::WallClock() { | ||
| SINGLETON_SET_INSTANCE(WallClock, Other); | ||
|
|
||
| const auto host_freq = GetSystemFrequency(); | ||
| guest_factor = GetFactor(GUEST_CNTFRQ, host_freq); | ||
| gpu_tick_factor = GetFactor(GPU_TICK_FREQ, host_freq); | ||
| } | ||
|
|
||
| WallClock::~WallClock() { SINGLETON_UNSET_INSTANCE(); } | ||
|
|
||
| u64 WallClock::GetCntpct() const { | ||
| return MultiplyByFactor(GetSystemTick(), guest_factor); | ||
| } | ||
|
|
||
| u64 WallClock::GetGpuTick() const { | ||
| return MultiplyByFactor(GetSystemTick(), gpu_tick_factor); | ||
| } | ||
|
|
||
| } // namespace hydra::hw |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #pragma once | ||
|
|
||
| namespace hydra::hw { | ||
|
|
||
| constexpr u32 GUEST_CNTFRQ = 19'200'000; | ||
| constexpr u64 GPU_TICK_FREQ = 614'400'000; | ||
|
|
||
| class WallClock { | ||
| public: | ||
| static WallClock& GetInstance(); | ||
|
|
||
| WallClock(); | ||
| ~WallClock(); | ||
|
|
||
| u64 GetCntpct() const; | ||
| u64 GetGpuTick() const; | ||
|
|
||
| private: | ||
| u128 guest_factor; | ||
| u128 gpu_tick_factor; | ||
| }; | ||
|
|
||
| } // namespace hydra::hw |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.