@@ -199,16 +199,18 @@ namespace NActors {
199
199
bool firstEvent = true ;
200
200
bool preempted = false ;
201
201
bool wasWorking = false ;
202
- NHPTimer::STime hpnow = Ctx.HPStart ;
203
- NHPTimer::STime hpprev = TlsThreadContext->StartOfElapsingTime .exchange (hpnow, std::memory_order_acq_rel);
204
- Ctx.AddElapsedCycles (ActorSystemIndex, hpnow - hpprev);
205
- hpprev = Ctx.HPStart ;
202
+ NHPTimer::STime hpnow = Ctx.HPStart ;
203
+ NHPTimer::STime hpprev = TlsThreadContext->UpdateStartOfElapsingTime (hpnow);
204
+ if (Y_LIKELY (hpprev < hpnow)) {
205
+ Ctx.AddElapsedCycles (ActorSystemIndex, hpnow - hpprev);
206
+ }
207
+ NHPTimer::STime eventStart = Ctx.HPStart ;
206
208
207
209
for (; Ctx.ExecutedEvents < Ctx.EventsPerMailbox ; ++Ctx.ExecutedEvents ) {
208
210
if (TAutoPtr<IEventHandle> evExt = mailbox->Pop ()) {
209
211
mailbox->ProcessEvents (mailbox);
210
212
recipient = evExt->GetRecipientRewrite ();
211
- TActorContext ctx (*mailbox, *this , hpprev , recipient);
213
+ TActorContext ctx (*mailbox, *this , eventStart , recipient);
212
214
TlsActivationContext = &ctx; // ensure dtor (if any) is called within actor system
213
215
// move for destruct before ctx;
214
216
auto ev = std::move (evExt);
@@ -250,7 +252,7 @@ namespace NActors {
250
252
actor->Receive (ev);
251
253
252
254
hpnow = GetCycleCountFast ();
253
- hpprev = TlsThreadContext->StartOfElapsingTime . exchange (hpnow, std::memory_order_acq_rel );
255
+ hpprev = TlsThreadContext->UpdateStartOfElapsingTime (hpnow);
254
256
255
257
mailbox->ProcessEvents (mailbox);
256
258
actor->OnDequeueEvent ();
@@ -266,7 +268,10 @@ namespace NActors {
266
268
if (mailbox->IsEmpty ()) // was not-free and become free, we must reclaim mailbox
267
269
reclaimAsFree = true ;
268
270
269
- NHPTimer::STime elapsed = Ctx.AddEventProcessingStats (hpprev, hpnow, activityType, CurrentActorScheduledEventsCounter);
271
+ if (Y_LIKELY (hpprev < hpnow)) {
272
+ Ctx.AddEventProcessingStats (hpprev, hpnow, activityType, CurrentActorScheduledEventsCounter);
273
+ }
274
+ NHPTimer::STime elapsed = hpnow - eventStart;
270
275
if (elapsed > 1000000 ) {
271
276
LwTraceSlowEvent (ev.Get (), evTypeForTracing, actorType, Ctx.PoolId , CurrentRecipient, NHPTimer::GetSeconds (elapsed) * 1000.0 );
272
277
}
@@ -286,9 +291,12 @@ namespace NActors {
286
291
Ctx.IncrementNonDeliveredEvents ();
287
292
}
288
293
hpnow = GetCycleCountFast ();
289
- hpprev = TlsThreadContext->StartOfElapsingTime .exchange (hpnow, std::memory_order_acq_rel);
290
- Ctx.AddElapsedCycles (ActorSystemIndex, hpnow - hpprev);
294
+ hpprev = TlsThreadContext->UpdateStartOfElapsingTime (hpnow);
295
+ if (Y_LIKELY (hpprev < hpnow)) {
296
+ Ctx.AddElapsedCycles (ActorSystemIndex, hpnow - hpprev);
297
+ }
291
298
}
299
+ eventStart = hpnow;
292
300
293
301
if (TlsThreadContext->CapturedType == ESendingType::Tail) {
294
302
AtomicStore (&mailbox->ScheduleMoment , hpnow);
@@ -777,23 +785,27 @@ namespace NActors {
777
785
void TGenericExecutorThread::GetCurrentStats (TExecutorThreadStats& statsCopy) const {
778
786
NHPTimer::STime hpnow = GetCycleCountFast ();
779
787
ui64 activityType = TlsThreadCtx.ElapsingActorActivity .load (std::memory_order_acquire);
780
- NHPTimer::STime hpprev = TlsThreadCtx.StartOfElapsingTime .exchange (hpnow, std::memory_order_acq_rel);
781
- if (activityType == Max<ui64>()) {
782
- Ctx.AddParkedCycles (hpnow - hpprev);
783
- } else {
784
- Ctx.AddElapsedCycles (activityType, hpnow - hpprev);
788
+ NHPTimer::STime hpprev = TlsThreadCtx.UpdateStartOfElapsingTime (hpnow);
789
+ if (Y_LIKELY (hpprev < hpnow)) {
790
+ if (activityType == Max<ui64>()) {
791
+ Ctx.AddParkedCycles (hpnow - hpprev);
792
+ } else {
793
+ Ctx.AddElapsedCycles (activityType, hpnow - hpprev);
794
+ }
785
795
}
786
796
Ctx.GetCurrentStats (statsCopy);
787
797
}
788
798
789
799
void TGenericExecutorThread::GetSharedStats (i16 poolId, TExecutorThreadStats &statsCopy) const {
790
800
NHPTimer::STime hpnow = GetCycleCountFast ();
791
801
ui64 activityType = TlsThreadCtx.ElapsingActorActivity .load (std::memory_order_acquire);
792
- NHPTimer::STime hpprev = TlsThreadCtx.StartOfElapsingTime .exchange (hpnow, std::memory_order_acq_rel);
793
- if (activityType == Max<ui64>()) {
794
- Ctx.AddParkedCycles (hpnow - hpprev);
795
- } else {
796
- Ctx.AddElapsedCycles (activityType, hpnow - hpprev);
802
+ NHPTimer::STime hpprev = TlsThreadCtx.UpdateStartOfElapsingTime (hpnow);
803
+ if (Y_LIKELY (hpprev < hpnow)) {
804
+ if (activityType == Max<ui64>()) {
805
+ Ctx.AddParkedCycles (hpnow - hpprev);
806
+ } else {
807
+ Ctx.AddElapsedCycles (activityType, hpnow - hpprev);
808
+ }
797
809
}
798
810
statsCopy = TExecutorThreadStats ();
799
811
statsCopy.Aggregate (SharedStats[poolId]);
0 commit comments