Skip to content

Commit 7b57237

Browse files
committed
Use timing types instead of integer type
1 parent 5325593 commit 7b57237

38 files changed

+220
-298
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/OsSpecificApi.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -172,30 +172,29 @@ bool GetCpuInfo(pid_t tid, bool& isRunning, uint64_t& cpuTime)
172172
return true;
173173
}
174174

175-
uint64_t GetThreadCpuTime(IThreadInfo* pThreadInfo)
175+
std::chrono::milliseconds GetThreadCpuTime(IThreadInfo* pThreadInfo)
176176
{
177177
bool isRunning = false;
178178
uint64_t cpuTime = 0;
179179
if (!GetCpuInfo(pThreadInfo->GetOsThreadId(), isRunning, cpuTime))
180180
{
181-
return 0;
181+
return 0ms;
182182
}
183183

184-
return cpuTime;
184+
return std::chrono::milliseconds(cpuTime);
185185
}
186186

187-
bool IsRunning(IThreadInfo* pThreadInfo, uint64_t& cpuTime, bool& failed)
187+
// isRunning, cpu time , failed
188+
std::tuple<bool, std::chrono::milliseconds, bool> IsRunning(IThreadInfo* pThreadInfo)
188189
{
189190
bool isRunning = false;
191+
uint64_t cpuTime = 0;
190192
if (!GetCpuInfo(pThreadInfo->GetOsThreadId(), isRunning, cpuTime))
191193
{
192-
cpuTime = 0;
193-
failed = true;
194-
return false;
194+
return {false, 0ms, true};
195195
}
196196

197-
failed = false;
198-
return isRunning;
197+
return {isRunning, std::chrono::milliseconds(cpuTime), false};
199198
}
200199

201200
// from https://linux.die.net/man/3/get_nprocs

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/TimerCreateCpuProfiler.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ bool TimerCreateCpuProfiler::Collect(void* ctx)
227227

228228
std::tie(rawCpuSample.LocalRootSpanId, rawCpuSample.SpanId) = threadInfo->GetTracingContext();
229229

230-
rawCpuSample.Timestamp = OpSysTools::GetTimestampSafe();
230+
rawCpuSample.Timestamp = std::chrono::nanoseconds(OpSysTools::GetTimestampSafe());
231231
rawCpuSample.AppDomainId = threadInfo->GetAppDomainId();
232232
rawCpuSample.Stack = std::move(callstack);
233233
rawCpuSample.ThreadInfo = std::move(threadInfo);
234-
rawCpuSample.Duration = _samplingInterval.count();
234+
rawCpuSample.Duration = _samplingInterval;
235235
_pProvider->Add(std::move(rawCpuSample));
236236

237237
return true;

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Windows/OsSpecificApi.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ std::unique_ptr<StackFramesCollectorBase> CreateNewStackFramesCollectorInstance(
7777
#endif
7878
}
7979

80-
uint64_t GetThreadCpuTime(IThreadInfo* pThreadInfo)
80+
std::chrono::milliseconds GetThreadCpuTime(IThreadInfo* pThreadInfo)
8181
{
8282
FILETIME creationTime, exitTime = {}; // not used here
8383
FILETIME kernelTime = {};
@@ -87,7 +87,7 @@ uint64_t GetThreadCpuTime(IThreadInfo* pThreadInfo)
8787
if (::GetThreadTimes(pThreadInfo->GetOsThreadHandle(), &creationTime, &exitTime, &kernelTime, &userTime))
8888
{
8989
uint64_t milliseconds = GetTotalMilliseconds(userTime) + GetTotalMilliseconds(kernelTime);
90-
return milliseconds;
90+
return std::chrono::milliseconds(milliseconds);
9191
}
9292
else
9393
{
@@ -106,7 +106,7 @@ uint64_t GetThreadCpuTime(IThreadInfo* pThreadInfo)
106106
}
107107
}
108108
}
109-
return 0;
109+
return 0ms;
110110
}
111111

112112
typedef LONG KPRIORITY;
@@ -193,16 +193,14 @@ bool IsRunning(ULONG threadState)
193193
// If some callstacks show non cpu-bound frames at the top, return true only for Running state
194194
}
195195

196-
bool IsRunning(IThreadInfo* pThreadInfo, uint64_t& cpuTime, bool& failed)
196+
// isRunning, cpu time , failed
197+
std::tuple<bool, std::chrono::milliseconds, bool> IsRunning(IThreadInfo* pThreadInfo)
197198
{
198-
failed = true;
199-
cpuTime = 0;
200-
201199
if (NtQueryInformationThread == nullptr)
202200
{
203201
if (!InitializeNtQueryInformationThreadCallback())
204202
{
205-
return false;
203+
return {false, 0ms, true};
206204
}
207205
}
208206

@@ -230,14 +228,12 @@ bool IsRunning(IThreadInfo* pThreadInfo, uint64_t& cpuTime, bool& failed)
230228
}
231229
#endif
232230
// This always happens in 32 bit so uses another API to at least get the CPU consumption
233-
cpuTime = GetThreadCpuTime(pThreadInfo);
234-
return false;
231+
return {false, GetThreadCpuTime(pThreadInfo), true};
235232
}
236233

237-
failed = false;
238-
cpuTime = GetTotalMilliseconds(sti.UserTime) + GetTotalMilliseconds(sti.KernelTime);
234+
auto cpuTime = std::chrono::milliseconds(GetTotalMilliseconds(sti.UserTime) + GetTotalMilliseconds(sti.KernelTime));
239235

240-
return IsRunning(sti.ThreadState);
236+
return {IsRunning(sti.ThreadState), cpuTime, false};
241237
}
242238

243239
// https://devblogs.microsoft.com/oldnewthing/20200824-00/?p=104116

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
#include "Log.h"
44

55
#include <algorithm>
6+
#include <chrono>
67
#include <stdexcept>
78

9+
using namespace std::chrono_literals;
10+
811
void AdaptiveSampler::Counts::AddTest()
912
{
1013
_testCount.fetch_add(1);
@@ -83,7 +86,7 @@ AdaptiveSampler::AdaptiveSampler(
8386
_rng = std::mt19937(rd());
8487
_distribution = std::uniform_real_distribution<>(0.0, 1.0);
8588

86-
if (windowDuration != std::chrono::milliseconds::zero())
89+
if (windowDuration != 0ms)
8790
{
8891
_timer.Start();
8992
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void AllocationsProvider::OnAllocation(uint64_t timestamp,
201201

202202
// We know that we don't have any span ID nor end point details
203203

204-
rawSample.Timestamp = timestamp;
204+
rawSample.Timestamp = std::chrono::nanoseconds(timestamp);
205205
auto cs = _callstackProvider.Get();
206206
const auto nbFrames = std::min(stack.size(), static_cast<std::size_t>(cs.Capacity()));
207207
auto end_stack = stack.begin() + nbFrames;

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

-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ void ClrEventsParser::ParseEvent(
7474
}
7575
}
7676

77-
uint64_t ClrEventsParser::GetCurrentTimestamp()
78-
{
79-
return OpSysTools::GetHighPrecisionTimestamp();
80-
}
81-
8277
// TL;DR Deactivate the alignment check in the Undefined Behavior Sanitizers for the ParseGcEvent function
8378
// because events fields are not aligned in the bitstream sent by the CLR.
8479
//

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

-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ class ClrEventsParser
291291
GCDetails& GetCurrentGC();
292292
void InitializeGC(uint64_t timestamp, GCDetails& gc, GCStartPayload& payload);
293293
static void ResetGC(GCDetails& gc);
294-
static uint64_t GetCurrentTimestamp();
295-
296294

297295
public:
298296
// Points to the UTF16, null terminated string from the given event data buffer

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class CollectorBase
114114
// with std::shared_ptr<Sample> as out parameter (avoid alloc/dealloc and add up overhead)
115115
std::shared_ptr<Sample> TransformRawSample(const TRawSample& rawSample)
116116
{
117-
auto sample = std::make_shared<Sample>(0, std::string_view(), rawSample.Stack.Size());
117+
auto sample = std::make_shared<Sample>(0ms, std::string_view(), rawSample.Stack.Size());
118118

119119
TransformRawSample(rawSample, sample);
120120

@@ -127,7 +127,7 @@ class CollectorBase
127127
}
128128

129129
protected:
130-
uint64_t GetCurrentTimestamp()
130+
std::chrono::nanoseconds GetCurrentTimestamp()
131131
{
132132
return OpSysTools::GetHighPrecisionTimestamp();
133133
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void ContentionProvider::AddContentionSample(uint64_t timestamp, uint32_t thread
166166

167167
// We know that we don't have any span ID nor end point details
168168

169-
rawSample.Timestamp = timestamp;
169+
rawSample.Timestamp = std::chrono::nanoseconds(timestamp);
170170
auto cs = _callstackProvider.Get();
171171
const auto nbFrames = std::min(stack.size(), static_cast<std::size_t>(cs.Capacity()));
172172
auto end_stack = stack.begin() + nbFrames;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void EventPipeEventsManager::ParseEvent(EVENTPIPE_PROVIDER provider,
5353
}
5454

5555
// the events are expected to be processed synchronously so the current time is used as timestamp
56-
_parser->ParseEvent(OpSysTools::GetHighPrecisionTimestamp(), version, keywords, id, cbEventData, eventData);
56+
_parser->ParseEvent(OpSysTools::GetHighPrecisionTimestamp().count(), version, keywords, id, cbEventData, eventData);
5757
}
5858

5959
bool EventPipeEventsManager::TryGetEventInfo(LPCBYTE pMetadata, ULONG cbMetadata, WCHAR*& name, DWORD& id, INT64& keywords, DWORD& version)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ std::vector<FrameInfoView> GCThreadsCpuProvider::GetFrames()
7373
};
7474
}
7575

76-
void GCThreadsCpuProvider::OnCpuDuration(std::uint64_t cpuTime)
76+
void GCThreadsCpuProvider::OnCpuDuration(std::chrono::milliseconds cpuTime)
7777
{
78-
_cpuDurationMetric->Add(static_cast<double>(cpuTime));
78+
_cpuDurationMetric->Add(static_cast<double>(cpuTime.count()));
7979
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GCThreadsCpuProvider : public NativeThreadsCpuProviderBase
1919
const char* GetName() override;
2020

2121
protected:
22-
void OnCpuDuration(std::uint64_t cpuTime) override;
22+
void OnCpuDuration(std::chrono::milliseconds cpuTime) override;
2323

2424
private:
2525
bool IsGcThread(std::shared_ptr<IThreadInfo> const& thread);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void GarbageCollectionProvider::OnGarbageCollectionEnd(
100100
_pohSize = pohSize;
101101

102102
RawGarbageCollectionSample rawSample;
103-
rawSample.Timestamp = endTimestamp;
103+
rawSample.Timestamp = std::chrono::nanoseconds(endTimestamp);
104104
rawSample.LocalRootSpanId = 0;
105105
rawSample.SpanId = 0;
106106
rawSample.AppDomainId = (AppDomainID) nullptr;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
std::atomic<uint64_t> LiveObjectInfo::s_nextObjectId = 1;
77

88

9-
LiveObjectInfo::LiveObjectInfo(std::shared_ptr<Sample> sample, uintptr_t address, int64_t timestamp)
9+
LiveObjectInfo::LiveObjectInfo(std::shared_ptr<Sample> sample, uintptr_t address, std::chrono::nanoseconds timestamp)
1010
:
1111
_address(address),
1212
_weakHandle(nullptr),

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55

66
#include <atomic>
7+
#include <chrono>
78
#include <memory>
89

910
#include "cor.h"
@@ -14,7 +15,7 @@
1415
class LiveObjectInfo
1516
{
1617
public:
17-
LiveObjectInfo(std::shared_ptr<Sample> sample, uintptr_t address, int64_t timestamp);
18+
LiveObjectInfo(std::shared_ptr<Sample> sample, uintptr_t address, std::chrono::nanoseconds timestamp);
1819

1920
// accessors
2021
void SetHandle(ObjectHandleID handle);
@@ -28,9 +29,9 @@ class LiveObjectInfo
2829
std::shared_ptr<Sample> _sample;
2930
uintptr_t _address;
3031
ObjectHandleID _weakHandle;
31-
// TODO This field is not yet used because the current implementation is incomple.
32+
// TODO This field is not yet used because the current implementation is incomplete.
3233
// Just keep to remind us to finish the implementation.
33-
int64_t _timestamp;
34+
std::chrono::nanoseconds _timestamp;
3435
uint64_t _gcCount;
3536

3637
static std::atomic<uint64_t> s_nextObjectId;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ std::unique_ptr<SamplesEnumerator> LiveObjectsProvider::GetSamples()
139139
{
140140
std::lock_guard<std::mutex> lock(_liveObjectsLock);
141141

142-
int64_t currentTimestamp = OpSysTools::GetHighPrecisionTimestamp();
142+
auto currentTimestamp = OpSysTools::GetHighPrecisionTimestamp();
143143
std::size_t nbSamples = 0;
144144

145145
// OPTIM maybe use an allocator
@@ -152,7 +152,7 @@ std::unique_ptr<SamplesEnumerator> LiveObjectsProvider::GetSamples()
152152
auto sample = info.GetSample();
153153

154154
// update samples lifetime
155-
sample->ReplaceLabel(Label{Sample::ObjectLifetimeLabel, std::to_string(sample->GetTimeStamp() - currentTimestamp)});
155+
sample->ReplaceLabel(Label{Sample::ObjectLifetimeLabel, std::to_string((sample->GetTimeStamp() - currentTimestamp).count())});
156156
sample->ReplaceLabel(Label{Sample::ObjectGenerationLabel, info.IsGen2() ? Gen2 : Gen1});
157157

158158
samples->Add(sample);

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include "ManagedThreadInfo.h"
55
#include "shared/src/native-src/string.h"
66

7+
#include <chrono>
8+
9+
using namespace std::chrono_literals;
10+
711
std::atomic<std::uint32_t> ManagedThreadInfo::s_nextProfilerThreadInfoId{1};
812
thread_local std::shared_ptr<ManagedThreadInfo> ManagedThreadInfo::CurrentThreadInfo{nullptr};
913

@@ -31,13 +35,9 @@ ManagedThreadInfo::ManagedThreadInfo(ThreadID clrThreadId, ICorProfilerInfo4* pC
3135
_osThreadId(osThreadId),
3236
_osThreadHandle(osThreadHandle),
3337
_pThreadName(std::move(pThreadName)),
34-
_lastSampleHighPrecisionTimestampNanoseconds{0},
35-
_cpuConsumptionMilliseconds{0},
36-
_timestamp{0},
37-
_lastKnownSampleUnixTimeUtc{0},
38-
_highPrecisionNanosecsAtLastUnixTimeUpdate{0},
39-
_snapshotsPerformedSuccessCount{0},
40-
_snapshotsPerformedFailureCount{0},
38+
_lastSampleHighPrecisionTimestamp{0ns},
39+
_cpuConsumption{0ms},
40+
_timestamp{0ns},
4141
_deadlockTotalCount{0},
4242
_deadlockInPeriodCount{0},
4343
_deadlockDetectionPeriod{0},

0 commit comments

Comments
 (0)