Skip to content

Commit

Permalink
move Y_LIKELY to WorkerCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
kruall committed May 15, 2024
1 parent 407562e commit af87841
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 43 deletions.
8 changes: 2 additions & 6 deletions ydb/library/actors/core/executor_pool_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,15 @@ namespace NActors {
NHPTimer::STime hpnow = GetCycleCountFast();
NHPTimer::STime hpprev = TlsThreadContext->UpdateStartOfElapsingTime(hpnow);
TlsThreadContext->ElapsingActorActivity.store(Max<ui64>(), std::memory_order_release);
if (Y_LIKELY(hpprev < hpnow)) {
wctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
}
wctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);

if (threadCtx.WaitingPad.Park())
return 0;

hpnow = GetCycleCountFast();
hpprev = TlsThreadContext->UpdateStartOfElapsingTime(hpnow);
TlsThreadContext->ElapsingActorActivity.store(ActorSystemIndex, std::memory_order_release);
if (Y_LIKELY(hpprev < hpnow)) {
wctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
}
wctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
}

while (!StopFlag.load(std::memory_order_acquire)) {
Expand Down
34 changes: 13 additions & 21 deletions ydb/library/actors/core/executor_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ namespace NActors {
bool wasWorking = false;
NHPTimer::STime hpnow = Ctx.HPStart;
NHPTimer::STime hpprev = TlsThreadContext->UpdateStartOfElapsingTime(hpnow);
if (Y_LIKELY(hpprev < hpnow)) {
Ctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
}
Ctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
NHPTimer::STime eventStart = Ctx.HPStart;

for (; Ctx.ExecutedEvents < Ctx.EventsPerMailbox; ++Ctx.ExecutedEvents) {
Expand Down Expand Up @@ -268,9 +266,9 @@ namespace NActors {
if (mailbox->IsEmpty()) // was not-free and become free, we must reclaim mailbox
reclaimAsFree = true;

if (Y_LIKELY(hpprev < hpnow)) {
Ctx.AddEventProcessingStats(hpprev, hpnow, activityType, CurrentActorScheduledEventsCounter);
}

Ctx.AddElapsedCycles(activityType, hpnow - hpprev);
Ctx.AddEventProcessingStats(eventStart, hpnow, activityType, CurrentActorScheduledEventsCounter);
NHPTimer::STime elapsed = hpnow - eventStart;
if (elapsed > 1000000) {
LwTraceSlowEvent(ev.Get(), evTypeForTracing, actorType, Ctx.PoolId, CurrentRecipient, NHPTimer::GetSeconds(elapsed) * 1000.0);
Expand All @@ -292,9 +290,7 @@ namespace NActors {
}
hpnow = GetCycleCountFast();
hpprev = TlsThreadContext->UpdateStartOfElapsingTime(hpnow);
if (Y_LIKELY(hpprev < hpnow)) {
Ctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
}
Ctx.AddElapsedCycles(ActorSystemIndex, hpnow - hpprev);
}
eventStart = hpnow;

Expand Down Expand Up @@ -786,12 +782,10 @@ namespace NActors {
NHPTimer::STime hpnow = GetCycleCountFast();
ui64 activityType = TlsThreadCtx.ElapsingActorActivity.load(std::memory_order_acquire);
NHPTimer::STime hpprev = TlsThreadCtx.UpdateStartOfElapsingTime(hpnow);
if (Y_LIKELY(hpprev < hpnow)) {
if (activityType == Max<ui64>()) {
Ctx.AddParkedCycles(hpnow - hpprev);
} else {
Ctx.AddElapsedCycles(activityType, hpnow - hpprev);
}
if (activityType == Max<ui64>()) {
Ctx.AddParkedCycles(hpnow - hpprev);
} else {
Ctx.AddElapsedCycles(activityType, hpnow - hpprev);
}
Ctx.GetCurrentStats(statsCopy);
}
Expand All @@ -800,12 +794,10 @@ namespace NActors {
NHPTimer::STime hpnow = GetCycleCountFast();
ui64 activityType = TlsThreadCtx.ElapsingActorActivity.load(std::memory_order_acquire);
NHPTimer::STime hpprev = TlsThreadCtx.UpdateStartOfElapsingTime(hpnow);
if (Y_LIKELY(hpprev < hpnow)) {
if (activityType == Max<ui64>()) {
Ctx.AddParkedCycles(hpnow - hpprev);
} else {
Ctx.AddElapsedCycles(activityType, hpnow - hpprev);
}
if (activityType == Max<ui64>()) {
Ctx.AddParkedCycles(hpnow - hpprev);
} else {
Ctx.AddElapsedCycles(activityType, hpnow - hpprev);
}
statsCopy = TExecutorThreadStats();
statsCopy.Aggregate(SharedStats[poolId]);
Expand Down
8 changes: 2 additions & 6 deletions ydb/library/actors/core/executor_thread_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,13 @@ namespace NActors {
NHPTimer::STime hpnow = GetCycleCountFast();
NHPTimer::STime hpprev = TlsThreadContext->UpdateStartOfElapsingTime(hpnow);
TlsThreadContext->ElapsingActorActivity.store(Max<ui64>(), std::memory_order_release);
if (Y_LIKELY(hpprev < hpnow)) {
TlsThreadContext->WorkerCtx->AddElapsedCycles(TlsThreadContext->ActorSystemIndex, hpnow - hpprev);
}
TlsThreadContext->WorkerCtx->AddElapsedCycles(TlsThreadContext->ActorSystemIndex, hpnow - hpprev);
do {
if (WaitingPad.Park()) // interrupted
return true;
hpnow = GetCycleCountFast();
hpprev = TlsThreadContext->UpdateStartOfElapsingTime(hpnow);
if (Y_LIKELY(hpprev < hpnow)) {
TlsThreadContext->WorkerCtx->AddParkedCycles(hpnow - hpprev);
}
TlsThreadContext->WorkerCtx->AddParkedCycles(hpnow - hpprev);
state = GetState<TWaitState>();
} while (static_cast<EThreadState>(state) == EThreadState::Sleep && !stopFlag->load(std::memory_order_relaxed));
TlsThreadContext->ElapsingActorActivity.store(TlsThreadContext->ActorSystemIndex, std::memory_order_release);
Expand Down
8 changes: 4 additions & 4 deletions ydb/library/actors/core/thread_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ namespace NActors {
bool IsCurrentRecipientAService = false;
TMPMCRingQueue<20>::EPopMode ActivationPopMode = TMPMCRingQueue<20>::EPopMode::ReallySlow;

std::atomic<ui64> StartOfElapsingTime = 0;
std::atomic<i64> StartOfElapsingTime = 0;
std::atomic<ui64> ElapsingActorActivity = 0;
TWorkerContext *WorkerCtx = nullptr;
ui32 ActorSystemIndex = 0;

ui64 UpdateStartOfElapsingTime(ui64 newValue) {
ui64 oldValue = StartOfElapsingTime.load(std::memory_order_acquire);
ui64 UpdateStartOfElapsingTime(i64 newValue) {
i64 oldValue = StartOfElapsingTime.load(std::memory_order_acquire);
for (;;) {
if (newValue <= oldValue) {
if (newValue - oldValue <= 0) {
break;
}
if (StartOfElapsingTime.compare_exchange_strong(oldValue, newValue, std::memory_order_acq_rel)) {
Expand Down
15 changes: 9 additions & 6 deletions ydb/library/actors/core/worker_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ namespace NActors {
}

void AddElapsedCycles(ui32 activityType, i64 elapsed) {
Y_DEBUG_ABORT_UNLESS(activityType < Stats->MaxActivityType());
RelaxedStore(&Stats->ElapsedTicks, RelaxedLoad(&Stats->ElapsedTicks) + elapsed);
RelaxedStore(&Stats->ElapsedTicksByActivity[activityType], RelaxedLoad(&Stats->ElapsedTicksByActivity[activityType]) + elapsed);
if (Y_LIKELY(elapsed > 0)) {
Y_DEBUG_ABORT_UNLESS(activityType < Stats->MaxActivityType());
RelaxedStore(&Stats->ElapsedTicks, RelaxedLoad(&Stats->ElapsedTicks) + elapsed);
RelaxedStore(&Stats->ElapsedTicksByActivity[activityType], RelaxedLoad(&Stats->ElapsedTicksByActivity[activityType]) + elapsed);
}
}

void AddParkedCycles(i64 elapsed) {
RelaxedStore(&Stats->ParkedTicks, RelaxedLoad(&Stats->ParkedTicks) + elapsed);
if (Y_LIKELY(elapsed > 0)) {
RelaxedStore(&Stats->ParkedTicks, RelaxedLoad(&Stats->ParkedTicks) + elapsed);
}
}

void AddBlockedCycles(i64 elapsed) {
Expand Down Expand Up @@ -136,8 +140,7 @@ namespace NActors {
RelaxedStore(&Stats->ReceivedEvents, RelaxedLoad(&Stats->ReceivedEvents) + 1);
RelaxedStore(&Stats->ReceivedEventsByActivity[activityType], RelaxedLoad(&Stats->ReceivedEventsByActivity[activityType]) + 1);
RelaxedStore(&Stats->ScheduledEventsByActivity[activityType], RelaxedLoad(&Stats->ScheduledEventsByActivity[activityType]) + scheduled);
AddElapsedCycles(activityType, elapsed);
return elapsed;
return std::max<i64>(0, elapsed);
}

void UpdateActorsStats(size_t dyingActorsCnt) {
Expand Down

0 comments on commit af87841

Please sign in to comment.