Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ EtwEventsManager::EtwEventsManager(
_parser = std::make_unique<ClrEventsParser>(
nullptr, // to avoid duplicates with what is done in EtwEventsHandler
nullptr, // to avoid duplicates with what is done in EtwEventsHandler
pGCSuspensionsListener);
pGCSuspensionsListener,
nullptr // no GC dump for .NET Framework (TODO: how to trigger it from ETW?)
);
_logger = std::make_unique<ProfilerLogger>();
_IpcClient = nullptr;
_IpcServer = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ void ClrEventsParser::LogGcEvent(
ClrEventsParser::ClrEventsParser(
IAllocationsListener* pAllocationListener,
IContentionListener* pContentionListener,
IGCSuspensionsListener* pGCSuspensionsListener)
IGCSuspensionsListener* pGCSuspensionsListener,
IGCDumpListener* pGCDumpListener)
:
_pAllocationListener{pAllocationListener},
_pContentionListener{pContentionListener},
_pGCSuspensionsListener{pGCSuspensionsListener}
_pGCSuspensionsListener{pGCSuspensionsListener},
_pGCDumpListener{pGCDumpListener}
{
ResetGC(_gcInProgress);
ResetGC(_currentBGC);
Expand Down Expand Up @@ -242,6 +244,49 @@ ClrEventsParser::ParseGcEvent(std::chrono::nanoseconds timestamp, DWORD id, DWOR
return;
}

// GC dump related events
if (id == EVENT_GC_BULK_NODE)
{
// TODO: get the list of objects in the GC heap dump
LogGcEvent("OnGCBulkNode");

if (_pGCDumpListener != nullptr)
{
GCBulkNodePayload payload{0};
ULONG offset = 0;
if (!EventsParserHelper::Read<GCBulkNodePayload>(payload, pEventData, cbEventData, offset))
{
// TODO: log and stop the dump?
return;
}

// sanity check
_pGCDumpListener->OnBulkNodes(
payload.Index,
payload.Count,
(GCBulkNodeValue*)(pEventData + offset));
}
}
else if (id == EVENT_GC_BULK_EDGE)
{
// TODO: get the list of references between objects in the GC heap dump
LogGcEvent("OnGCBulkEdge");

if (_pGCDumpListener != nullptr)
{
// TODO: _pGCDumpListener->OnGCBulkEdge(...);
GCBulkEdgePayload payload{0};
ULONG offset = 0;
if (!EventsParserHelper::Read<GCBulkEdgePayload>(payload, pEventData, cbEventData, offset))
{
_pGCDumpListener->OnBulkEdges(
payload.Index,
payload.Count,
(GCBulkEdgeValue*)(pEventData + offset));
}
}
}

// the rest of events are related to garbage collections lifetime
// read https://medium.com/criteo-engineering/spying-on-net-garbage-collector-with-net-core-eventpipes-9f2a986d5705?source=friends_link&sk=baf9a7766fb5c7899b781f016803597f
// for more details about the state machine
Expand Down Expand Up @@ -480,11 +525,12 @@ void ClrEventsParser::NotifyGarbageCollectionEnd(
std::chrono::nanoseconds endTimestamp,
uint64_t gen2Size,
uint64_t lohSize,
uint64_t pohSize)
uint64_t pohSize,
uint32_t memPressure)
{
for (auto& pGarbageCollectionsListener : _pGarbageCollectionsListeners)
{
LogGcEvent("OnGarbageCollectionEnd: ", number, " ", generation, " ", reason, " ", type);
LogGcEvent("OnGarbageCollectionEnd: #", number, " gen", generation, " ", reason, " ", type, " ", memPressure, "%");

pGarbageCollectionsListener->OnGarbageCollectionEnd(
number,
Expand All @@ -497,7 +543,8 @@ void ClrEventsParser::NotifyGarbageCollectionEnd(
endTimestamp,
gen2Size,
lohSize,
pohSize);
pohSize,
memPressure);
}
}

Expand Down Expand Up @@ -623,7 +670,8 @@ void ClrEventsParser::OnGCRestartEEEnd(std::chrono::nanoseconds timestamp)
timestamp,
gc.gen2Size,
gc.lohSize,
gc.pohSize);
gc.pohSize,
gc.memPressure);
ResetGC(gc);
}
}
Expand All @@ -641,7 +689,6 @@ void ClrEventsParser::OnGCHeapStats(std::chrono::nanoseconds timestamp, uint64_t
gc.gen2Size = gen2Size;
gc.lohSize = lohSize;
gc.pohSize = pohSize;

if (gc.HasGlobalHeapHistoryBeenReceived && (gc.Generation == 2) && (gc.Type == GCType::BackgroundGC))
{
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(timestamp - gc.StartTimestamp).count();
Expand All @@ -658,7 +705,8 @@ void ClrEventsParser::OnGCHeapStats(std::chrono::nanoseconds timestamp, uint64_t
timestamp,
gc.gen2Size,
gc.lohSize,
gc.pohSize);
gc.pohSize,
gc.memPressure);
ResetGC(gc);
}
}
Expand Down Expand Up @@ -694,7 +742,8 @@ void ClrEventsParser::OnGCGlobalHeapHistory(std::chrono::nanoseconds timestamp,
timestamp,
gc.gen2Size,
gc.lohSize,
gc.pohSize);
gc.pohSize,
payload.MemPressure);
ResetGC(gc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "IAllocationsListener.h"
#include "IGarbageCollectionsListener.h"
#include "IGCSuspensionsListener.h"
#include "IGCDumpListener.h"

#include "../../../../shared/src/native-src/string.h"
#include "assert.h"
Expand Down Expand Up @@ -50,6 +51,9 @@ const int EVENT_GC_PINOBJECTATGCTIME = 33;

const int EVENT_SW_STACK = 82;

// events sent during heap dumps
const int EVENT_GC_BULK_NODE = 18;
const int EVENT_GC_BULK_EDGE = 19;


#define LONG_LENGTH 1024
Expand Down Expand Up @@ -233,6 +237,8 @@ struct GCGlobalHeapPayload
uint32_t Gen0ReductionCount;
uint32_t Reason;
uint32_t GlobalMechanisms;
uint32_t PauseMode;
uint32_t MemPressure;
};

struct WaitHandleWaitStartPayload // for .NET 9+
Expand All @@ -246,8 +252,40 @@ struct WaitHandleWaitStopPayload // for .NET 9+
{
uint16_t ClrInstanceId; // Unique ID for the instance of CLR.
};

//struct GCBulkNodeValue
//{
// uintptr_t Address;
// uint64_t Size;
// uint64_t TypeID;
// uint64_t EdgeCount;
//};
//struct GCBulkNodePayload
//{
// uint32_t Index;
// uint32_t Count;
// uint16_t ClrInstanceID;
//
// // this is followed by an array of Count GCBulkNodeValue structures
//};
//
//struct GCBulkEdgeValue
//{
// uintptr_t Value;
// uint32_t ReferencingFieldID;
//};
//struct GCBulkEdgePayload
//{
// uint32_t Index;
// uint32_t Count;
// uint16_t ClrInstanceID;
//
// // this is followed by an array of Count GCBulkEdgeValue structures
//};

#pragma pack()


class IContentionListener;


Expand All @@ -264,6 +302,7 @@ struct GCDetails
uint64_t gen2Size;
uint64_t lohSize;
uint64_t pohSize;
uint32_t memPressure;

// GlobalHeapHistory and HeapStats events are not received in the same order
// between Framework and CoreCLR. So we need to keep track of what has been received
Expand All @@ -283,10 +322,10 @@ class ClrEventsParser
ClrEventsParser(
IAllocationsListener* pAllocationListener,
IContentionListener* pContentionListener,
IGCSuspensionsListener* pGCSuspensionsListener
IGCSuspensionsListener* pGCSuspensionsListener,
IGCDumpListener* pGCDumpListener
);


// the parser is used both for synchronous (ICorProfilerCallback) and
// asynchronous (.NET Framework via the Agent) cases. The timestamp parameter
// is only valid (different from 0) in the asynchronous scenario.
Expand Down Expand Up @@ -335,7 +374,8 @@ class ClrEventsParser
std::chrono::nanoseconds endTimestamp,
uint64_t gen2Size,
uint64_t lohSize,
uint64_t pohSize
uint64_t pohSize,
uint32_t memPressure
);
GCDetails& GetCurrentGC();
void InitializeGC(std::chrono::nanoseconds timestamp, GCDetails& gc, GCStartPayload& payload);
Expand All @@ -346,6 +386,7 @@ class ClrEventsParser
IContentionListener* _pContentionListener = nullptr;
IGCSuspensionsListener* _pGCSuspensionsListener = nullptr;
std::vector<IGarbageCollectionsListener*> _pGarbageCollectionsListeners;
IGCDumpListener* _pGCDumpListener = nullptr;

template <typename... Args>
void LogGcEvent(Args const&... args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ Configuration::Configuration()
_forceHttpSampling = GetEnvironmentValue(EnvironmentVariables::ForceHttpSampling, false);
_cpuProfilerType = GetEnvironmentValue(EnvironmentVariables::CpuProfilerType, DefaultCpuProfilerType);
_isWaitHandleProfilingEnabled = GetEnvironmentValue(EnvironmentVariables::WaitHandleProfilingEnabled, false);
_isHeapSnapshotEnabled = GetEnvironmentValue(EnvironmentVariables::HeapSnapshotEnabled, false);
_heapSnapshotInterval = ExtractHeapSnapshotInterval();
_heapSnapshotUsedMemoryThreshold = GetEnvironmentValue(EnvironmentVariables::HeapSnapshotUsedMemoryThreshold, 85);
}

fs::path Configuration::ExtractLogDirectory()
Expand Down Expand Up @@ -797,7 +800,6 @@ void Configuration::SetEnablementStatus(EnablementStatus status)
_enablementStatus = status;
}


std::chrono::milliseconds Configuration::ExtractHttpRequestDurationThreshold() const
{
auto const defaultValue = 50ms;
Expand All @@ -812,3 +814,30 @@ std::chrono::milliseconds Configuration::GetHttpRequestDurationThreshold() const
{
return _httpRequestDurationThreshold;
}

bool Configuration::IsHeapSnapshotEnabled() const
{
return _isHeapSnapshotEnabled;
}

std::chrono::minutes Configuration::ExtractHeapSnapshotInterval() const
{
auto r = shared::GetEnvironmentValue(EnvironmentVariables::HeapSnapshotInterval);
int32_t interval;
if (TryParse(r, interval))
{
return std::chrono::minutes(interval);
}

return 5min;
}

std::chrono::minutes Configuration::GetHeapSnapshotInterval() const
{
return _heapSnapshotInterval;
}

int32_t Configuration::GetHeapSnapshotUsedMemoryThreshold() const
{
return _heapSnapshotUsedMemoryThreshold;
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class Configuration final : public IConfiguration
bool IsWaitHandleProfilingEnabled() const override;
bool IsManagedActivationEnabled() const override;
void SetEnablementStatus(EnablementStatus status) override;
bool IsHeapSnapshotEnabled() const override;
std::chrono::minutes GetHeapSnapshotInterval() const override;
int32_t GetHeapSnapshotUsedMemoryThreshold() const override;


private:
static tags ExtractUserTags();
Expand All @@ -110,6 +114,7 @@ class Configuration final : public IConfiguration
EnablementStatus ExtractEnablementStatus();
std::chrono::milliseconds ExtractSsiLongLivedThreshold() const;
std::chrono::milliseconds ExtractHttpRequestDurationThreshold() const;
std::chrono::minutes ExtractHeapSnapshotInterval() const;

private:
static std::string const DefaultProdSite;
Expand Down Expand Up @@ -187,4 +192,8 @@ class Configuration final : public IConfiguration
CpuProfilerType _cpuProfilerType;
std::chrono::milliseconds _cpuProfilingInterval;
bool _isWaitHandleProfilingEnabled;

bool _isHeapSnapshotEnabled;
std::chrono::minutes _heapSnapshotInterval;
int32_t _heapSnapshotUsedMemoryThreshold; // in % of used memory
};
Loading
Loading