Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisnas committed Mar 7, 2025
2 parents fc44b8d + 999a3aa commit 9d59578
Show file tree
Hide file tree
Showing 69 changed files with 2,108 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,21 @@ void AllocationsProvider::OnAllocation(std::chrono::nanoseconds timestamp,

// TODO: we need to check that threads are not jumping from one AppDomain to the other too frequently
// because we might be receiving this event 1 second after it has been emitted
// It this is the case, we should simply set the AppDomainId to -1 all the time.
// It this is the case, we should simply set the AppDomainId to 0 all the time.
AppDomainID appDomainId;
if (SUCCEEDED(_pCorProfilerInfo->GetThreadAppDomain(threadInfo->GetClrThreadId(), &appDomainId)))
{
rawSample.AppDomainId = appDomainId;
}
else
{
rawSample.AppDomainId = -1;
rawSample.AppDomainId = 0;
}
}
else // create a fake IThreadInfo that wraps the OS thread id (no name, no profiler thread id)
{
rawSample.ThreadInfo = std::make_shared<FrameworkThreadInfo>(threadId);

// TODO: do we need to set to -1?
// rawSample.AppDomainId = -1;
rawSample.AppDomainId = 0;
}

// rawSample.AllocationSize = objectSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <type_traits>

#include "EnvironmentVariables.h"
#include "Log.h"
#include "OpSysTools.h"

#include "shared/src/native-src/dd_filesystem.hpp"
Expand All @@ -33,13 +34,13 @@ Configuration::Configuration()
_pprofDirectory = ExtractPprofDirectory();
_isOperationalMetricsEnabled = GetEnvironmentValue(EnvironmentVariables::OperationalMetricsEnabled, false);
_isNativeFrameEnabled = GetEnvironmentValue(EnvironmentVariables::NativeFramesEnabled, false);
_isCpuProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::CpuProfilingEnabled, true);
_isWallTimeProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::WallTimeProfilingEnabled, true);
_isExceptionProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::ExceptionProfilingEnabled, true);
_isAllocationProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::AllocationProfilingEnabled, false);
_isCpuProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::CpuProfilingEnabled, true, true);
_isWallTimeProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::WallTimeProfilingEnabled, true, true);
_isExceptionProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::ExceptionProfilingEnabled, true, true);
_isAllocationProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::AllocationProfilingEnabled, false, true);
_isContentionProfilingEnabled = GetContention();
_isGarbageCollectionProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::GCProfilingEnabled, true);
_isHeapProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::HeapProfilingEnabled, false);
_isGarbageCollectionProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::GCProfilingEnabled, true, true);
_isHeapProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::HeapProfilingEnabled, false, true);
_uploadPeriod = ExtractUploadInterval();
_userTags = ExtractUserTags();
_version = GetEnvironmentValue(EnvironmentVariables::Version, DefaultVersion);
Expand All @@ -50,7 +51,7 @@ Configuration::Configuration()
_agentPort = GetEnvironmentValue(EnvironmentVariables::AgentPort, DefaultAgentPort);
_site = ExtractSite();
_apiKey = GetEnvironmentValue(EnvironmentVariables::ApiKey, DefaultEmptyString);
_serviceName = GetEnvironmentValue(EnvironmentVariables::ServiceName, OpSysTools::GetProcessName());
_serviceName = GetEnvironmentValue(EnvironmentVariables::ServiceName, OpSysTools::GetProcessName(), true);
_isAgentLess = GetEnvironmentValue(EnvironmentVariables::Agentless, false);
_exceptionSampleLimit = GetEnvironmentValue(EnvironmentVariables::ExceptionSampleLimit, 500);
_allocationSampleLimit = GetEnvironmentValue(EnvironmentVariables::AllocationSampleLimit, 2000);
Expand Down Expand Up @@ -90,7 +91,7 @@ Configuration::Configuration()
_cpuProfilingInterval = ExtractCpuProfilingInterval(1ms);
}

_isEtwEnabled = GetEnvironmentValue(EnvironmentVariables::EtwEnabled, true);
_isEtwEnabled = GetEnvironmentValue(EnvironmentVariables::EtwEnabled, true, true);
_deploymentMode = GetEnvironmentValue(EnvironmentVariables::SsiDeployed, DeploymentMode::Manual);
_isEtwLoggingEnabled = GetEnvironmentValue(EnvironmentVariables::EtwLoggingEnabled, false);
_etwReplayEndpoint = GetEnvironmentValue(EnvironmentVariables::EtwReplayEndpoint, DefaultEmptyString);
Expand Down Expand Up @@ -518,11 +519,12 @@ bool Configuration::GetContention()
// first look at the supported env var
if (IsEnvironmentValueSet(EnvironmentVariables::LockContentionProfilingEnabled, lockContentionEnabled))
{
Log::Info("Configuration: ", EnvironmentVariables::LockContentionProfilingEnabled, " env var is set - lock contention is enabled");
return lockContentionEnabled;
}

// if not there, look at the deprecated one
return GetEnvironmentValue(EnvironmentVariables::DeprecatedContentionProfilingEnabled, lockContentionEnabled);
return GetEnvironmentValue(EnvironmentVariables::DeprecatedContentionProfilingEnabled, lockContentionEnabled, true);
}

bool Configuration::GetDefaultDebugLogEnabled()
Expand Down Expand Up @@ -669,13 +671,32 @@ static bool convert_to(shared::WSTRING const& s, DeploymentMode& result)


template <typename T>
T Configuration::GetEnvironmentValue(shared::WSTRING const& name, T const& defaultValue)
T Configuration::GetEnvironmentValue(shared::WSTRING const& name, T const& defaultValue, bool shouldLog)
{
if (!shared::EnvironmentExist(name)) return defaultValue;
if (!shared::EnvironmentExist(name))
{
if (shouldLog)
{
Log::Info("Configuration: ", name, " env var is not set - '", defaultValue, "' is used as default value");
}
return defaultValue;
}

T result{};
auto r = shared::GetEnvironmentValue(name);
if (!convert_to(r, result)) return defaultValue;
if (!convert_to(r, result))
{
if (shouldLog)
{
Log::Info("Configuration: ", name, " env var is set to '", r,"' that cannot be converted - '", defaultValue, "' is used as default value");
}
return defaultValue;
}

if (shouldLog)
{
Log::Info("Configuration: ", name, " env var is set to '", r, "'");
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Configuration final : public IConfiguration
static std::chrono::seconds GetDefaultUploadInterval();
static bool GetDefaultDebugLogEnabled();
template <typename T>
static T GetEnvironmentValue(shared::WSTRING const& name, T const& defaultValue);
static T GetEnvironmentValue(shared::WSTRING const& name, T const& defaultValue, bool shouldLog = false);
template <typename T>
static bool IsEnvironmentValueSet(shared::WSTRING const& name, T& value);
static std::chrono::nanoseconds ExtractCpuWallTimeSamplingRate(int minimum = 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,13 @@ void ContentionProvider::AddContentionSample(
}
else
{
rawSample.AppDomainId = -1;
rawSample.AppDomainId = 0;
}
}
else // create a fake IThreadInfo that wraps the OS thread id (no name, no profiler thread id)
{
rawSample.ThreadInfo = std::make_shared<FrameworkThreadInfo>(threadId);

// TODO: do we need to set to -1?
//rawSample.AppDomainId = -1;
rawSample.AppDomainId = 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "shared/src/native-src/string.h"

enum class CpuProfilerType
enum CpuProfilerType : int
{
ManualCpuTime,
#ifdef LINUX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <string>

enum class DeploymentMode
enum DeploymentMode : int
{
Manual,
SingleStepInstrumentation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// <copyright file="ConfigurationTest.cs" company="Datadog">
// 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.
// </copyright>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Datadog.Profiler.IntegrationTests.Helpers;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Datadog.Profiler.IntegrationTests.Exceptions
{
public class ConfigurationTest
{
private const string Scenario1 = "--scenario 18";

private readonly ITestOutputHelper _output;

public ConfigurationTest(ITestOutputHelper output)
{
_output = output;
}

// NOTE: we don't need to validate ALL runtimes but just one
//

[TestAppFact("Samples.Computer01", new[] { "net9.0" })]
public void CheckEnvVarsInLogWithDefaultProfilers(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: Scenario1);
// EnvironmentHelper.DisableDefaultProfilers(runner);

using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);

bool cpuIsLogged = false;
bool walltimeIsLogged = false;
bool exceptionIsLogged = false;
bool allocationIsLogged = false;
bool lockIsLogged = false;
bool gcIsLogged = false;
bool heapIsLogged = false;
bool serviceIsLogged = false;
bool etwIsLogged = false;

var logFile = Directory.GetFiles(runner.Environment.LogDir)
.Single(f => Path.GetFileName(f).StartsWith("DD-DotNet-Profiler-Native-"));

foreach (var line in File.ReadLines(logFile))
{
if (line.Contains("DD_PROFILING_CPU_ENABLED"))
{
cpuIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_WALLTIME_ENABLED"))
{
walltimeIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_EXCEPTION_ENABLED"))
{
exceptionIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_ALLOCATION_ENABLED"))
{
allocationIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_CONTENTION_ENABLED"))
{
lockIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_GC_ENABLED"))
{
gcIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_HEAP_ENABLED"))
{
heapIsLogged = true;
}
else
if (line.Contains("DD_SERVICE"))
{
serviceIsLogged = true;
}
else
if (line.Contains("DD_INTERNAL_PROFILING_ETW_ENABLED"))
{
etwIsLogged = true;
}
else if (line.Contains("] Configuration: DD_"))
{
// This is the default value
Assert.Fail($"unexpected configuration log - {line}");
}
}

cpuIsLogged.Should().BeTrue();
walltimeIsLogged.Should().BeTrue();
exceptionIsLogged.Should().BeTrue();
allocationIsLogged.Should().BeTrue();
lockIsLogged.Should().BeTrue();
gcIsLogged.Should().BeTrue();
heapIsLogged.Should().BeTrue();
serviceIsLogged.Should().BeTrue();
etwIsLogged.Should().BeTrue();
}

[TestAppFact("Samples.Computer01", new[] { "net9.0" })]
public void CheckEnvVarsInLogWithDisabledProfilers(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: Scenario1);
EnvironmentHelper.DisableDefaultProfilers(runner);

using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);

bool cpuIsLogged = false;
bool walltimeIsLogged = false;
bool exceptionIsLogged = false;
bool allocationIsLogged = false;
bool lockIsLogged = false;
bool gcIsLogged = false;
bool heapIsLogged = false;
bool serviceIsLogged = false;
bool etwIsLogged = false;

var logFile = Directory.GetFiles(runner.Environment.LogDir)
.Single(f => Path.GetFileName(f).StartsWith("DD-DotNet-Profiler-Native-"));

foreach (var line in File.ReadLines(logFile))
{
if (line.Contains("DD_PROFILING_CPU_ENABLED"))
{
cpuIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_WALLTIME_ENABLED"))
{
walltimeIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_EXCEPTION_ENABLED"))
{
exceptionIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_ALLOCATION_ENABLED"))
{
allocationIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_LOCK_ENABLED"))
{
lockIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_GC_ENABLED"))
{
gcIsLogged = true;
}
else
if (line.Contains("DD_PROFILING_HEAP_ENABLED"))
{
heapIsLogged = true;
}
else
if (line.Contains("DD_SERVICE"))
{
serviceIsLogged = true;
}
else
if (line.Contains("DD_INTERNAL_PROFILING_ETW_ENABLED"))
{
etwIsLogged = true;
}
else if (line.Contains("] Configuration: DD_"))
{
// This is the default value
Assert.Fail($"unexpected configuration log - {line}");
}
}

cpuIsLogged.Should().BeTrue();
walltimeIsLogged.Should().BeTrue();
exceptionIsLogged.Should().BeTrue();
allocationIsLogged.Should().BeTrue();
lockIsLogged.Should().BeTrue();
gcIsLogged.Should().BeTrue();
heapIsLogged.Should().BeTrue();
serviceIsLogged.Should().BeTrue();
etwIsLogged.Should().BeTrue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<assembly fullname="StackExchange.Redis" />
<assembly fullname="StackExchange.Redis.StrongName" />
<assembly fullname="System" />
<assembly fullname="System.Buffers">
<type fullname="System.Buffers.ArrayPool`1" />
</assembly>
<assembly fullname="System.Collections">
<type fullname="System.Collections.BitArray" />
<type fullname="System.Collections.Generic.Comparer`1" />
Expand Down Expand Up @@ -496,6 +499,7 @@
<type fullname="System.BitConverter" />
<type fullname="System.Boolean" />
<type fullname="System.Buffer" />
<type fullname="System.Buffers.ArrayPool`1" />
<type fullname="System.Buffers.SpanAction`2" />
<type fullname="System.Byte" />
<type fullname="System.Char" />
Expand Down
Loading

0 comments on commit 9d59578

Please sign in to comment.