Copilot commented on code in PR #3756:
URL: https://github.com/apache/brpc/pull/3756#discussion_r4609923436


##########
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:
   When `__SIZEOF_INT128__` is unavailable, this fallback cannot represent a 
fractional nanoseconds-per-cycle ratio: for example, `freq = 2.4e9` produces 
`mult = 1, shift = 0`, so `cycles_to_ns(cycles, mult, shift)` returns `cycles` 
instead of approximately `cycles * 1e9 / 2.4e9`. The new helper's documented 
contract is therefore wrong on no-`__int128__` targets (and the later 
non-guarded AArch64 comparison test will fail there); keep a division-based 
fallback in `cycles_to_ns` or implement a 64-bit fixed-point multiply using 
narrower limbs.



##########
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:
   `ASSERT_GE` returns from the test body on a failure, but the producer is 
still joinable; its destructor then calls `std::terminate` instead of reporting 
the assertion cleanly. Use a non-fatal expectation (or an RAII/join guard) so 
the producer is always joined before the test exits.



-- 
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]

Reply via email to