diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/AllocationsProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/AllocationsProvider.cpp index c733a10d29c2..39ba338bc662 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/AllocationsProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/AllocationsProvider.cpp @@ -26,14 +26,14 @@ std::vector AllocationsProvider::SampleTypeDefinitions( { - {"alloc-samples", "count"}, - {"alloc-size", "bytes"} + {"alloc-samples", "count", -1}, + {"alloc-size", "bytes", -1} } ); std::vector AllocationsProvider::FrameworkSampleTypeDefinitions( { - {"alloc-samples", "count"}, + {"alloc-samples", "count", -1}, } ); diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ContentionProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ContentionProvider.cpp index 29101e33b413..eda95b15e2a3 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ContentionProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ContentionProvider.cpp @@ -26,8 +26,8 @@ std::vector ContentionProvider::_emptyStack; std::vector ContentionProvider::SampleTypeDefinitions( { - {"lock-count", "count"}, - {"lock-time", "nanoseconds"} + {"lock-count", "count", -1}, + {"lock-time", "nanoseconds", -1} }); ContentionProvider::ContentionProvider( diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/CpuTimeProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/CpuTimeProvider.cpp index 5b80fb5a79e7..29fc68266774 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/CpuTimeProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/CpuTimeProvider.cpp @@ -13,9 +13,8 @@ std::vector CpuTimeProvider::SampleTypeDefinitions( { - {"cpu", "nanoseconds"}, - {"cpu-samples", "count"} -} + {"cpu", "nanoseconds", -1}, + {"cpu-samples", "count", -1}} ); CpuTimeProvider::CpuTimeProvider( diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ExceptionsProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ExceptionsProvider.cpp index 20b3149182d3..a2e4a53ce4b9 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ExceptionsProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/ExceptionsProvider.cpp @@ -18,7 +18,7 @@ std::vector ExceptionsProvider::SampleTypeDefinitions( { - {"exception", "count"} + {"exception", "count", -1} }); ExceptionsProvider::ExceptionsProvider( diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/GarbageCollectionProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/GarbageCollectionProvider.cpp index a9322675aed3..8019d973cdf8 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/GarbageCollectionProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/GarbageCollectionProvider.cpp @@ -15,6 +15,7 @@ GarbageCollectionProvider::GarbageCollectionProvider( : CollectorBase("GarbageCollectorProvider", valueTypeProvider.GetOrRegister(TimelineSampleType::Definitions), rawSampleTransformer, memoryResource) { + _gen0CountMetric = metricsRegistry.GetOrRegister("dotnet_gc_gen0"); _gen1CountMetric = metricsRegistry.GetOrRegister("dotnet_gc_gen1"); _gen2CountMetric = metricsRegistry.GetOrRegister("dotnet_gc_gen2"); diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.cpp index 63f102bc840f..ff6632525f77 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.cpp @@ -16,8 +16,8 @@ std::vector LiveObjectsProvider::SampleTypeDefinitions( { - {"inuse-objects", "count"}, - {"inuse-space", "bytes"} + {"inuse-objects", "count", -1}, + {"inuse-space", "bytes", -1} }); const uint32_t MAX_LIVE_OBJECTS = 1024; diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.h b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.h index 7e1b6a863be0..61a02206bc08 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.h +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/LiveObjectsProvider.h @@ -76,7 +76,8 @@ class LiveObjectsProvider : public ServiceBase, bool StopImpl() override; private: - + // used to access the CLR to create weak handles + // and get object generation ICorProfilerInfo13* _pCorProfilerInfo = nullptr; RawSampleTransformer* _rawSampleTransformer = nullptr; diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NativeThreadsCpuProviderBase.h b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NativeThreadsCpuProviderBase.h index c37e32453498..e3ea6bc6494e 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NativeThreadsCpuProviderBase.h +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NativeThreadsCpuProviderBase.h @@ -30,6 +30,7 @@ class NativeThreadsCpuProviderBase : public ISamplesProvider virtual std::vector> const& GetThreads() = 0; virtual Labels GetLabels() = 0; +private: RawSampleTransformer* _sampleTransformer; std::chrono::milliseconds _previousTotalCpuTime; std::vector _valueOffsets; diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NetworkProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NetworkProvider.cpp index b942b02a3182..7cfaabbf55e6 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NetworkProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/NetworkProvider.cpp @@ -13,7 +13,7 @@ std::vector NetworkProvider::SampleTypeDefinitions( { - {"request-time", "nanoseconds"} + {"request-time", "nanoseconds", -1} }); diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Profile.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Profile.cpp index e7ca8a064fd7..e9c62a696076 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Profile.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Profile.cpp @@ -182,9 +182,14 @@ libdatadog::profile_unique_ptr CreateProfile(std::vector const& std::vector samplesTypes; samplesTypes.reserve(valueTypes.size()); + // TODO: create a vector containing the indexes of the valueTypes + std::vector indexes; + indexes.reserve(valueTypes.size()); + for (auto const& type : valueTypes) { samplesTypes.push_back(CreateValueType(type.Name, type.Unit)); + indexes.push_back(type.Index); } struct ddog_prof_Slice_ValueType sample_types = {samplesTypes.data(), samplesTypes.size()}; diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Sample.h b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Sample.h index ae28c4870514..e5be0f46e5f7 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Sample.h +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/Sample.h @@ -20,6 +20,11 @@ struct SampleValueType { std::string Name; std::string Unit; + + // Samples belonging to the same provider will share the same index + // For libdatadog, it means that they will be stored in the same profile + // This value will be set when registering the SampleValueType with SampleValueTypeProvider + int32_t Index; // -1 means not set }; typedef std::vector Values; diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.cpp index 2720560198bd..d10ba25a1db7 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.cpp @@ -12,13 +12,16 @@ SampleValueTypeProvider::SampleValueTypeProvider() _sampleTypeDefinitions.reserve(16); } -std::vector SampleValueTypeProvider::GetOrRegister(std::vector const& valueTypes) +std::vector SampleValueTypeProvider::GetOrRegister(std::vector& valueTypes) { std::vector offsets; offsets.reserve(valueTypes.size()); - for (auto const& valueType : valueTypes) + for (auto& valueType : valueTypes) { + // set the same index for all + valueType.Index = _nextIndex; + size_t idx = GetOffset(valueType); if (idx == -1) { @@ -27,6 +30,10 @@ std::vector SampleValueTypeProvider::GetOrRegis } offsets.push_back(idx); } + + // the next set of SampleValueType will have a different index + _nextIndex++; + return offsets; } diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.h b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.h index 2e4d16864c9e..50700c7e7cf2 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.h +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/SampleValueTypeProvider.h @@ -19,11 +19,14 @@ class SampleValueTypeProvider SampleValueTypeProvider(); - std::vector GetOrRegister(std::vector const& valueType); + std::vector GetOrRegister(std::vector& valueType); std::vector const& GetValueTypes(); private: std::int8_t GetOffset(SampleValueType const& valueType); std::vector _sampleTypeDefinitions; + + // Incremented each time a new vector of SampleValueType is registered via GetOrRegister + uint32_t _nextIndex = 0; }; diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.cpp index ff47955ff5e6..379cf4f079f7 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.cpp @@ -3,5 +3,5 @@ #include "TimelineSampleType.h" -const std::vector TimelineSampleType::Definitions( - {{"timeline", "nanoseconds"}}); \ No newline at end of file +std::vector TimelineSampleType::Definitions( + {{"timeline", "nanoseconds", -1}}); \ No newline at end of file diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.h b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.h index a217873f6d00..20b282dea70c 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.h +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/TimelineSampleType.h @@ -11,5 +11,5 @@ class TimelineSampleType { public: - static const std::vector Definitions; + static std::vector Definitions; }; \ No newline at end of file diff --git a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/WallTimeProvider.cpp b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/WallTimeProvider.cpp index b954513673ba..068d93dcad4d 100644 --- a/profiler/src/ProfilerEngine/Datadog.Profiler.Native/WallTimeProvider.cpp +++ b/profiler/src/ProfilerEngine/Datadog.Profiler.Native/WallTimeProvider.cpp @@ -13,7 +13,7 @@ class SampleValueTypeProvider; std::vector WallTimeProvider::SampleTypeDefinitions( { - {"wall", "nanoseconds"} + {"wall", "nanoseconds", -1} } ); diff --git a/profiler/test/Datadog.Profiler.Native.Tests/SampleValueTypeProviderTest.cpp b/profiler/test/Datadog.Profiler.Native.Tests/SampleValueTypeProviderTest.cpp index c950e9ece05a..817df433a322 100644 --- a/profiler/test/Datadog.Profiler.Native.Tests/SampleValueTypeProviderTest.cpp +++ b/profiler/test/Datadog.Profiler.Native.Tests/SampleValueTypeProviderTest.cpp @@ -6,9 +6,9 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" -static const SampleValueType CpuValueType = {"cpu", "nanoseconds"}; -static const SampleValueType WallTimeValueType = {"walltime", "nanoseconds"}; -static const SampleValueType ExceptionValueType = {"exception", "count"}; +static SampleValueType CpuValueType = {"cpu", "nanoseconds", -1}; +static SampleValueType WallTimeValueType = {"walltime", "nanoseconds", -1}; +static SampleValueType ExceptionValueType = {"exception", "count", -1}; testing::AssertionResult AreSampleValueTypeEqual(SampleValueType const& v1, SampleValueType const& v2) { @@ -33,7 +33,7 @@ using ValueOffsets = std::vector; TEST(SampleValueTypeProvider, RegisterValueTypes) { SampleValueTypeProvider provider; - auto const valueTypes = std::vector{CpuValueType, WallTimeValueType}; + auto valueTypes = std::vector{CpuValueType, WallTimeValueType}; auto offsets = provider.GetOrRegister(valueTypes); @@ -94,4 +94,27 @@ TEST(SampleValueTypeProvider, EnsureThrowIfAddValueTypeSameNameButDifferentUnit) auto anotherValuetype = std::vector{{"cpu", "non-sense-unit"}}; EXPECT_THROW(provider.GetOrRegister(anotherValuetype), std::runtime_error); +} + +TEST(SampleValueTypeProvider, CheckSequentialIndex) +{ + std::vector AllocationSampleTypeDefinitions( + {{"alloc-samples", "count", -1}, + {"alloc-size", "bytes", -1}}); + std::vector ExceptionSampleTypeDefinitions( + {{"exception", "count", -1}}); + std::vector CpuSampleTypeDefinitions( + {{"cpu", "nanoseconds", -1}, + {"cpu-samples", "count", -1}}); + + SampleValueTypeProvider provider; + auto allocationsOffsets = provider.GetOrRegister(AllocationSampleTypeDefinitions); + auto exceptionOffsets = provider.GetOrRegister(ExceptionSampleTypeDefinitions); + auto cpuOffsets = provider.GetOrRegister(CpuSampleTypeDefinitions); + + ASSERT_EQ(AllocationSampleTypeDefinitions[0].Index, 0); + ASSERT_EQ(AllocationSampleTypeDefinitions[1].Index, 0); + ASSERT_EQ(ExceptionSampleTypeDefinitions[0].Index, 1); + ASSERT_EQ(CpuSampleTypeDefinitions[0].Index, 2); + ASSERT_EQ(CpuSampleTypeDefinitions[1].Index, 2); } \ No newline at end of file