Skip to content

Commit 33692c3

Browse files
authored
fix: reset threads cpu consumption on profiling start (#89)
1 parent 916bbcd commit 33692c3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native/StackSamplerLoop.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ void StackSamplerLoop::MainLoop()
149149
_loopThreadOsId = OpSysTools::GetThreadId();
150150
_pThreadsCpuManager->Map(_loopThreadOsId, ThreadName);
151151

152+
ResetThreadsCpuConsumption();
153+
152154
while (!_shutdownRequested)
153155
{
154156
try
@@ -612,3 +614,29 @@ void StackSamplerLoop::PersistStackSnapshotResults(
612614
_pCpuTimeCollector->Add(std::move(rawCpuSample));
613615
}
614616
}
617+
618+
void StackSamplerLoop::ResetThreadsCpuConsumption()
619+
{
620+
auto t1 = OpSysTools::GetHighPrecisionTimestamp();
621+
int32_t managedThreadsCount = _pManagedThreadList->Count();
622+
623+
for (int32_t i = 0; i < managedThreadsCount ; i++)
624+
{
625+
std::shared_ptr<ManagedThreadInfo> it = _pManagedThreadList->LoopNext(_iteratorCpuTime);
626+
if (it != nullptr)
627+
{
628+
bool failure = false;
629+
uint64_t currentConsumption = 0;
630+
OsSpecificApi::IsRunning(it.get(), currentConsumption, failure);
631+
if (!failure)
632+
{
633+
it->SetCpuConsumptionMilliseconds(currentConsumption, t1);
634+
} else
635+
{
636+
Log::Debug("Failed to get CPU consumption for thread ", it->GetClrThreadId());
637+
}
638+
}
639+
}
640+
auto t2 = OpSysTools::GetHighPrecisionTimestamp();
641+
Log::Debug("ResetThreadsCpuConsumption took ", t2 - t1, " ns");
642+
}

profiler/src/ProfilerEngine/Datadog.Profiler.Native/StackSamplerLoop.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ class StackSamplerLoop : public ServiceBase
115115

116116
bool StartImpl() override;
117117
bool StopImpl() override;
118+
119+
120+
void ResetThreadsCpuConsumption();
118121
};

0 commit comments

Comments
 (0)