Skip to content

Commit 802f520

Browse files
authored
chore: more efficient access to FiberInterface epoch field (#388)
Done to improve efficiency of fiber context switch operations. Signed-off-by: Roman Gershman <romange@gmail.com>
1 parent 6e29a5a commit 802f520

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

util/fibers/detail/fiber_interface.cc

+3-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ PMR_NS::memory_resource* default_stack_resource = nullptr;
5353
size_t default_stack_size = 64 * 1024;
5454
uint64_t g_tsc_cycles_per_ms = 0;
5555

56+
__thread FiberInterface::TL FiberInterface::tl;
57+
5658
// Per thread initialization structure.
5759
struct TL_FiberInitializer {
5860
TL_FiberInitializer* next = nullptr;
@@ -63,7 +65,6 @@ struct TL_FiberInitializer {
6365
// Per-thread scheduler instance.
6466
// Allows overriding the main dispatch loop
6567
Scheduler* sched;
66-
uint64_t epoch = 0;
6768
uint64_t switch_delay_cycles = 0; // switch delay in cycles.
6869

6970
// Tracks fiber runtimes that took longer than 1ms.
@@ -379,7 +380,7 @@ FiberInterface* FiberInterface::SwitchSetup() {
379380
// When a kernel suspends we may get a negative delta because TSC is reset.
380381
// We ignore such cases (and they are very rare).
381382
if (tsc > cpu_tsc_) {
382-
++fb_initializer.epoch;
383+
++tl.epoch;
383384
DCHECK_GE(tsc, to_suspend->cpu_tsc_);
384385
fb_initializer.switch_delay_cycles += (tsc - cpu_tsc_);
385386

@@ -437,10 +438,6 @@ void SetCustomDispatcher(DispatchPolicy* policy) {
437438
fb_init.sched->AttachCustomPolicy(policy);
438439
}
439440

440-
uint64_t FiberSwitchEpoch() noexcept {
441-
return detail::FbInitializer().epoch;
442-
}
443-
444441
uint64_t FiberSwitchDelayUsec() noexcept {
445442
// in nanoseconds, so lets convert from cycles
446443
return detail::FbInitializer().switch_delay_cycles * 1000 / detail::g_tsc_cycles_per_ms;

util/fibers/detail/fiber_interface.h

+7
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ class FiberInterface {
223223

224224
size_t GetStackMargin(const void* stack_address) const;
225225

226+
static uint64_t TL_Epoch() {
227+
return tl.epoch;
228+
}
226229
protected:
227230
static constexpr uint16_t kTerminatedBit = 0x1;
228231
static constexpr uint16_t kBusyBit = 0x2;
@@ -262,6 +265,10 @@ class FiberInterface {
262265
uint32_t stack_size_ = 0;
263266
uint8_t* stack_bottom_ = nullptr;
264267

268+
static __thread struct TL {
269+
uint64_t epoch = 0;
270+
} tl;
271+
265272
private:
266273
#ifndef NDEBUG
267274
std::function<std::string()> stacktrace_print_cb_;

util/fibers/fibers.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class Fiber {
103103
};
104104

105105
// Returns the context switch epoch number for this thread.
106-
uint64_t FiberSwitchEpoch() noexcept;
106+
inline uint64_t FiberSwitchEpoch() noexcept {
107+
return detail::FiberInterface::TL_Epoch();
108+
}
107109

108110
// Returns the aggregated delay between activation of fibers and
109111
// the time they were switched to in microseconds.

0 commit comments

Comments
 (0)