Skip to content

Commit

Permalink
Use an alternative mechanism to generate unique span ids.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 700828349
Change-Id: I194fc08d9c0d1887aab39b3b3197ee4156e60f15
  • Loading branch information
laramiel authored and copybara-github committed Nov 28, 2024
1 parent 63df973 commit 13998ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
23 changes: 23 additions & 0 deletions tensorstore/internal/tracing/logged_trace_span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,37 @@

#include "tensorstore/internal/tracing/logged_trace_span.h"

#include <stdint.h>

#include <atomic>
#include <ostream>
#include <string_view>

#include "absl/strings/str_format.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"

namespace tensorstore {
namespace internal_tracing {

/* static */
uint64_t LoggedTraceSpan::random_id() {
static std::atomic<int64_t> base{absl::ToUnixNanos(absl::Now())};

thread_local uint64_t id =
static_cast<uint64_t>(base.fetch_add(1, std::memory_order_relaxed));

// Apply xorshift64, which has a period of 2^64-1, to the per-thread id
// to generate the next id.
uint64_t x = id;
do {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
} while (x == 0);
return id = x;
}

void LoggedTraceSpan::BeginLog(std::ostream& stream) {
stream << absl::StreamFormat("%x: Start %s", id_, method());
}
Expand Down
15 changes: 1 addition & 14 deletions tensorstore/internal/tracing/logged_trace_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include "absl/log/log_streamer.h"
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "tensorstore/internal/source_location.h"
#include "tensorstore/internal/tracing/span_attribute.h"
#include "tensorstore/internal/tracing/trace_span.h"
Expand All @@ -39,18 +37,7 @@ namespace internal_tracing {
/// A TraceSpan which optionally includes scoped logging to ABSL INFO.
class LoggedTraceSpan : public TraceSpan {
// Generates a random ID for logging the Begin/End log messages.
inline uint64_t random_id() {
thread_local uint64_t id = absl::ToUnixNanos(absl::Now());
// Apply xorshift64, which has a period of 2^64-1, to the per-thread id
// to generate the next id.
uint64_t x = id;
do {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
} while (x == 0);
return id = x;
}
static uint64_t random_id();

public:
LoggedTraceSpan(std::string_view method, bool log,
Expand Down

0 comments on commit 13998ce

Please sign in to comment.