jimmystewpot commented on code in PR #3756:
URL: https://github.com/apache/brpc/pull/3756#discussion_r4618839464
##########
src/butil/time.h:
##########
@@ -266,12 +270,49 @@ inline uint64_t clock_cycles() {
#endif
}
+// Precompute fixed-point multiplier and right-shift so that
+// cycles_to_ns(cycles, mult, shift) == (cycles * 10^9) / freq without runtime
+// 64-bit integer divisions.
+inline void compute_cpu_frequency_scale(int64_t freq, uint64_t* mult,
uint32_t* shift) {
+ if (freq <= 0) {
+ *mult = 0;
+ *shift = 0;
+ return;
+ }
+ const uint64_t ufreq = static_cast<uint64_t>(freq);
+#if defined(__SIZEOF_INT128__)
+ uint32_t s = 63;
+ while (s > 0) {
+ __uint128_t num = (static_cast<__uint128_t>(1000000000ULL) << s) +
(ufreq - 1);
+ __uint128_t m = num / ufreq;
+ if (m <= UINT64_MAX) {
+ *mult = static_cast<uint64_t>(m);
+ *shift = s;
+ return;
+ }
+ --s;
+ }
+#endif
+ *mult = (1000000000ULL + ufreq - 1) / ufreq;
+ *shift = 0;
+}
+
+inline int64_t cycles_to_ns(uint64_t cycles, uint64_t mult, uint32_t shift) {
+#if defined(__SIZEOF_INT128__)
+ return static_cast<int64_t>((static_cast<__uint128_t>(cycles) * mult) >>
shift);
+#else
+ return static_cast<int64_t>((cycles * mult) >> shift);
Review Comment:
Fixed in e263b108. When `__SIZEOF_INT128__` is unavailable,
`compute_cpu_frequency_scale()` stores `ufreq` in `*mult` (`*shift = 0`) and
`cycles_to_ns()` uses the division-based `(cycles % mult) * 1000000000ULL /
mult + (cycles / mult) * 1000000000ULL` fallback, preserving exact fractional
conversion on non-`__int128__` targets and unifying `cpuwide_time_ns()`.
##########
test/baidu_time_unittest.cpp:
##########
@@ -211,4 +213,207 @@ TEST(BaiduTimeTest, timer_auto_start) {
printf("Cost %" PRId64 "us\n", t.u_elapsed());
}
+TEST(BaiduTimeTest, clock_cycles_monotonic_and_serialized) {
+#if defined(__aarch64__) || defined(__x86_64__) || defined(__amd64__) ||
defined(__loongarch64)
+ uint64_t prev = butil::detail::clock_cycles();
+ ASSERT_GT(prev, static_cast<uint64_t>(0));
+ for (int i = 0; i < 10000; ++i) {
+ const uint64_t cur = butil::detail::clock_cycles();
+ ASSERT_GE(cur, prev) << "Non-monotonic clock_cycles at iteration " <<
i;
+ prev = cur;
+ }
+#endif
+#if defined(__aarch64__)
+ // Verify cross-thread acquire/release ordering of clock_cycles() on
AArch64:
+ // because clock_cycles() executes isb before mrs cntvct_el0, the reader
thread
+ // cannot speculatively hoist the counter read ahead of the acquire load.
+ uint64_t published_tsc = 0;
+ std::atomic<int> turn{0}; // 0: producer's turn, 1: consumer's turn
+ const int kHandoffs = 2000;
+ std::thread producer([&]() {
+ for (int i = 0; i < kHandoffs; ++i) {
+ while (turn.load(std::memory_order_acquire) != 0) {
+ }
+ published_tsc = butil::detail::clock_cycles();
+ turn.store(1, std::memory_order_release);
+ }
+ });
+ for (int i = 0; i < kHandoffs; ++i) {
+ while (turn.load(std::memory_order_acquire) != 1) {
+ }
+ const uint64_t after_acquire_tsc = butil::detail::clock_cycles();
+ const uint64_t before_release_tsc = published_tsc;
+ turn.store(0, std::memory_order_release);
+ ASSERT_GE(after_acquire_tsc, before_release_tsc) << "handoff=" << i;
Review Comment:
Fixed in e263b108. Replaced `ASSERT_GE` inside the handoff loop with
`EXPECT_GE`, signalling `turn = -1` and breaking so `producer.join()` is always
reached cleanly on failure.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]