-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathStackSamplerLoop.h
More file actions
118 lines (102 loc) · 3.92 KB
/
Copy pathStackSamplerLoop.h
File metadata and controls
118 lines (102 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
#pragma once
#include <thread>
#ifdef _WINDOWS
#include <atlcomcli.h>
#endif
#include <atomic>
#include <future>
#include <memory>
#include <unordered_map>
// from dotnet coreclr includes
#include "cor.h"
#include "corprof.h"
// end
#include "ManagedThreadInfo.h"
#include "ICollector.h"
#include "ServiceBase.h"
#include "RawCpuSample.h"
#include "RawWallTimeSample.h"
#include "MetricsRegistry.h"
#include "MeanMaxMetric.h"
#include "shared/src/native-src/string.h"
// forward declarations
class StackFramesCollectorBase;
class StackSnapshotResultBuffer;
class StackSamplerLoopManager;
class IThreadsCpuManager;
class IManagedThreadList;
class IConfiguration;
typedef enum
{
WallTime,
CpuTime
} PROFILING_TYPE;
class StackSamplerLoop : public ServiceBase
{
friend StackSamplerLoopManager;
public:
StackSamplerLoop(
ICorProfilerInfo4* pCorProfilerInfo,
IConfiguration* pConfiguration,
StackFramesCollectorBase* pStackFramesCollector,
StackSamplerLoopManager* pManager,
IThreadsCpuManager* pThreadsCpuManager,
IManagedThreadList* pManagedThreadList,
IManagedThreadList* pCodeHotspotThreadList,
ICollector<RawWallTimeSample>* pWallTimeCollector,
ICollector<RawCpuSample>* pCpuTimeCollector,
MetricsRegistry& metricsRegistry
);
~StackSamplerLoop();
StackSamplerLoop(StackSamplerLoop const&) = delete;
StackSamplerLoop& operator=(StackSamplerLoop const&) = delete;
// Inherited via IService
const char* GetName() override;
private:
ICorProfilerInfo4* _pCorProfilerInfo;
StackFramesCollectorBase* _pStackFramesCollector;
StackSamplerLoopManager* _pManager;
IConfiguration* _pConfiguration;
IThreadsCpuManager* _pThreadsCpuManager;
IManagedThreadList* _pManagedThreadList;
IManagedThreadList* _pCodeHotspotsThreadList;
ICollector<RawWallTimeSample>* _pWallTimeCollector;
ICollector<RawCpuSample>* _pCpuTimeCollector;
std::unique_ptr<std::thread> _pLoopThread;
DWORD _loopThreadOsId;
std::atomic<bool> _shutdownRequested = false;
std::shared_ptr<ManagedThreadInfo> _targetThread;
uint32_t _iteratorWallTime;
uint32_t _iteratorCpuTime;
uint32_t _iteratorCodeHotspot;
int32_t _walltimeThreadsThreshold;
int32_t _cpuThreadsThreshold;
int32_t _codeHotspotsThreadsThreshold;
private:
std::chrono::nanoseconds _samplingPeriod;
uint32_t _nbCores;
bool _isWalltimeEnabled;
bool _isCpuEnabled;
bool _areInternalMetricsEnabled;
std::shared_ptr<MeanMaxMetric> _walltimeDurationMetric;
std::shared_ptr<MeanMaxMetric> _cpuDurationMetric;
private:
void MainLoop(std::promise<void> loopReadyPromise);
void MainLoopIteration();
void CpuProfilingIteration();
void WalltimeProfilingIteration();
void CodeHotspotIteration();
void CollectOneThreadStackSample(std::shared_ptr<ManagedThreadInfo>& pThreadInfo,
std::chrono::nanoseconds thisSampleTimestampNanosecs,
std::chrono::nanoseconds duration,
PROFILING_TYPE profilingType);
std::chrono::nanoseconds ComputeWallTime(std::chrono::nanoseconds currentTimestampNs, std::chrono::nanoseconds prevTimestampNs);
static void UpdateSnapshotInfos(StackSnapshotResultBuffer* pStackSnapshotResult, std::chrono::nanoseconds representedDuration, std::chrono::nanoseconds currentUnixTimestamp);
void PersistStackSnapshotResults(StackSnapshotResultBuffer* pSnapshotResult,
std::shared_ptr<ManagedThreadInfo>& pThreadInfo,
PROFILING_TYPE profilingType);
bool StartImpl() override;
bool StopImpl() override;
};