Skip to content

Commit

Permalink
Fix delayed startup timer registration/deregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
gleocadie committed Jun 17, 2024
1 parent 0a89c2e commit 588ddc2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ bool ProfilerSignalManager::SetupSignalHandler()
int32_t result = sigaction(_signalToSend, &sampleAction, &_previousAction);
if (result != 0)
{
Log::Error("ProfilerSignalManager::SetupSignalHandler: Failed to setup signal handler for ", _signalToSend, " signals. Reason: ",
Log::Error("ProfilerSignalManager::SetupSignalHandler: Failed to setup signal handler for ", strsignal(_signalToSend), " signals. Reason: ",
strerror(errno), ".");
return false;
}

Log::Info("ProfilerSignalManager::SetupSignalHandler: Successfully setup signal handler for ", _signalToSend, " signal.");
Log::Info("ProfilerSignalManager::SetupSignalHandler: Successfully setup signal handler for ", strsignal(_signalToSend), " signal.");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ TimerCreateCpuProfiler::~TimerCreateCpuProfiler()

void TimerCreateCpuProfiler::RegisterThread(std::shared_ptr<ManagedThreadInfo> threadInfo)
{
std::shared_lock(_registerLock);
std::shared_lock lock(_registerLock);

if (GetState() != SeriveBase::State::Started)
if (GetState() != ServiceBase::State::Started)
{
return;
}
Expand Down Expand Up @@ -71,16 +71,16 @@ const char* TimerCreateCpuProfiler::GetName()

bool TimerCreateCpuProfiler::StartImpl()
{
// If the signal is higjacked, what to do?
// If the signal is highjacked, what to do?
auto registered = _pSignalManager->RegisterHandler(TimerCreateCpuProfiler::CollectStackSampleSignalHandler);

if (registered)
{
std::unique_lock lock(_registerLock);
Instance = this;

// Create and start timer for all threads
_pManagedThreadsList->ForEach([this](ManagedThreadList* thread) { RegisterImpl(thread); });
// Create and start timer for all threads.
_pManagedThreadsList->ForEach([this](ManagedThreadInfo* thread) { RegisterThreadImpl(thread); });
}

return registered;
Expand All @@ -99,7 +99,7 @@ bool TimerCreateCpuProfiler::CollectStackSampleSignalHandler(int sig, siginfo_t*
auto instance = Instance;
if (instance == nullptr)
{
return;
return false;
}

return instance->Collect(ucontext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TimerCreateCpuProfiler : public ServiceBase
void RegisterThreadImpl(ManagedThreadInfo* thread);

bool StartImpl() override;
bool StopImp() override;
bool StopImpl() override;

enum class ServiceState
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ CpuProfilerType Configuration::GetCpuProfilerType() const
return _cpuProfilerType;
}

=======
std::chrono::milliseconds Configuration::GetCpuProfilingInterval() const
{
return _cpuProfilingInterval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ServiceBase.h"
#include "ManagedThreadInfo.h"

#include <functional>
#include <memory>

class IManagedThreadList : public ServiceBase
Expand All @@ -25,4 +26,5 @@ class IManagedThreadList : public ServiceBase
virtual HRESULT TryGetCurrentThreadInfo(std::shared_ptr<ManagedThreadInfo>& ppThreadInfo) = 0;
virtual std::shared_ptr<ManagedThreadInfo> GetOrCreate(ThreadID clrThreadId) = 0;
virtual bool TryGetThreadInfo(uint32_t osThreadId, std::shared_ptr<ManagedThreadInfo>& ppThreadInfo) = 0;
virtual void ForEach(std::function<void (ManagedThreadInfo*)> callback) = 0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,14 @@ bool ManagedThreadList::RegisterThread(std::shared_ptr<ManagedThreadInfo>& pThre
}

return false;
}

void ManagedThreadList::ForEach(std::function<void (ManagedThreadInfo*)> callback)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);

for (auto& thread : _threads)
{
callback(thread.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ManagedThreadList : public IManagedThreadList
HRESULT TryGetCurrentThreadInfo(std::shared_ptr<ManagedThreadInfo>& ppThreadInfo) override;
std::shared_ptr<ManagedThreadInfo> GetOrCreate(ThreadID clrThreadId) override;
bool TryGetThreadInfo(uint32_t osThreadId, std::shared_ptr<ManagedThreadInfo>& ppThreadInfo) override;
void ForEach(std::function<void (ManagedThreadInfo*)> callback) override;

private:
const char* _serviceName = "ManagedThreadList";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ StackSamplerLoop::StackSamplerLoop(
_cpuThreadsThreshold{pConfiguration->CpuThreadsThreshold()},
_codeHotspotsThreadsThreshold{pConfiguration->CodeHotspotsThreadsThreshold()},
_isWalltimeEnabled{pConfiguration->IsWallTimeProfilingEnabled()},
_isCpuEnabled{pConfiguration->IsCpuProfilingEnabled()},
_areInternalMetricsEnabled{pConfiguration->IsInternalMetricsEnabled()}
_isCpuEnabled{pConfiguration->IsCpuProfilingEnabled() && pConfiguration->GetCpuProfilerType() == CpuProfilerType::ManualCpuTime},
_areInternalMetricsEnabled{pConfiguration->IsInternalMetricsEnabled()},
_areInternalMetricsEnabled{pConfiguration->IsInternalMetricsEnabled()}
{
_nbCores = OsSpecificApi::GetProcessorCount();
Log::Info("Processor cores = ", _nbCores);
Expand Down

0 comments on commit 588ddc2

Please sign in to comment.