summaryrefslogtreecommitdiffstats
path: root/src/common/x64/rdtsc.cpp
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2023-06-22 06:12:46 +0200
committerGitHub <noreply@github.com>2023-06-22 06:12:46 +0200
commite3122c5b468fd59b7eded5a3a7300643d05616bc (patch)
tree3fe4c2c0bea83f3bb8849ef8839b9520873d41c2 /src/common/x64/rdtsc.cpp
parentMerge pull request #10777 from liamwhite/no-barrier (diff)
parentnvdisp: Fix SingleCore frametime reporting (diff)
downloadyuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.tar
yuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.tar.gz
yuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.tar.bz2
yuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.tar.lz
yuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.tar.xz
yuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.tar.zst
yuzu-e3122c5b468fd59b7eded5a3a7300643d05616bc.zip
Diffstat (limited to '')
-rw-r--r--src/common/x64/rdtsc.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/common/x64/rdtsc.cpp b/src/common/x64/rdtsc.cpp
new file mode 100644
index 000000000..9273274a3
--- /dev/null
+++ b/src/common/x64/rdtsc.cpp
@@ -0,0 +1,39 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <thread>
+
+#include "common/steady_clock.h"
+#include "common/uint128.h"
+#include "common/x64/rdtsc.h"
+
+namespace Common::X64 {
+
+template <u64 Nearest>
+static u64 RoundToNearest(u64 value) {
+ const auto mod = value % Nearest;
+ return mod >= (Nearest / 2) ? (value - mod + Nearest) : (value - mod);
+}
+
+u64 EstimateRDTSCFrequency() {
+ // Discard the first result measuring the rdtsc.
+ FencedRDTSC();
+ std::this_thread::sleep_for(std::chrono::milliseconds{1});
+ FencedRDTSC();
+
+ // Get the current time.
+ const auto start_time = RealTimeClock::Now();
+ const u64 tsc_start = FencedRDTSC();
+ // Wait for 100 milliseconds.
+ std::this_thread::sleep_for(std::chrono::milliseconds{100});
+ const auto end_time = RealTimeClock::Now();
+ const u64 tsc_end = FencedRDTSC();
+ // Calculate differences.
+ const u64 timer_diff = static_cast<u64>(
+ std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count());
+ const u64 tsc_diff = tsc_end - tsc_start;
+ const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff);
+ return RoundToNearest<100'000>(tsc_freq);
+}
+
+} // namespace Common::X64