未验证 提交 1f736c15 编写于 作者: S Sung Yoon Whang 提交者: GitHub

Fix GC fragmentation counter to not return NaN (#46938)

* Fix fragmenetation counter to not return NaN

* add regression test
上级 45be6750
......@@ -73,7 +73,7 @@ protected override void OnEventCommand(EventCommandEventArgs command)
_timerCounter ??= new PollingCounter("active-timer-count", this, () => Timer.ActiveCount) { DisplayName = "Number of Active Timers" };
_fragmentationCounter ??= new PollingCounter("gc-fragmentation", this, () => {
var gcInfo = GC.GetGCMemoryInfo();
return gcInfo.FragmentedBytes * 100d / gcInfo.HeapSizeBytes;
return gcInfo.HeapSizeBytes != 0 ? gcInfo.FragmentedBytes * 100d / gcInfo.HeapSizeBytes : 0;
}) { DisplayName = "GC Fragmentation", DisplayUnits = "%" };
#if !MONO
_exceptionCounter ??= new IncrementingPollingCounter("exception-count", this, () => Exception.GetExceptionCount()) { DisplayName = "Exception Count", DisplayRateTimeScale = new TimeSpan(0, 0, 1) };
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#if USE_MDT_EVENTSOURCE
using Microsoft.Diagnostics.Tracing;
#else
using System.Diagnostics.Tracing;
#endif
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace EventCounterRegressionTests
{
public class SimpleEventListener : EventListener
{
private readonly EventLevel _level = EventLevel.Verbose;
public bool SawNanFragmentation = false;
public SimpleEventListener()
{
}
protected override void OnEventSourceCreated(EventSource source)
{
if (source.Name.Equals("System.Runtime"))
{
Dictionary<string, string> refreshInterval = new Dictionary<string, string>();
refreshInterval.Add("EventCounterIntervalSec", "1");
EnableEvents(source, _level, (EventKeywords)(-1), refreshInterval);
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
string fragmentationReported = "";
bool isGCFragmentationCounter = false;
for (int i = 0; i < eventData.Payload.Count; i++)
{
IDictionary<string, object> eventPayload = eventData.Payload[i] as IDictionary<string, object>;
if (eventPayload != null)
{
foreach (KeyValuePair<string, object> payload in eventPayload)
{
if (payload.Key.Equals("Name") && payload.Value.ToString().Equals("gc-fragmentation"))
isGCFragmentationCounter = true;
if (payload.Key.Equals("Mean"))
{
fragmentationReported = payload.Value.ToString();
}
}
if (isGCFragmentationCounter && fragmentationReported.Equals("NaN"))
{
SawNanFragmentation = true;
}
}
}
}
}
public partial class TestEventCounter
{
public static int Main(string[] args)
{
// Create an EventListener.
using (SimpleEventListener myListener = new SimpleEventListener())
{
Thread.Sleep(3000);
if (!myListener.SawNanFragmentation)
{
Console.WriteLine("Test passed");
return 100;
}
else
{
Console.WriteLine($"Test Failed - GC fragmentation counter reported a NaN");
return 1;
}
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestKind>BuildAndRun</CLRTestKind>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<CLRTestPriority>0</CLRTestPriority>
<GCStressIncompatible>true</GCStressIncompatible>
<!-- This test is timing sensitive and JIT timing affects the results of the test -->
<JitOptimizationSensitive>true</JitOptimizationSensitive>
<!-- This test has a secondary thread with an infinite loop -->
<UnloadabilityIncompatible>true</UnloadabilityIncompatible>
</PropertyGroup>
<ItemGroup>
<Compile Include="regression-46938.cs" />
<ProjectReference Include="../common/common.csproj" />
</ItemGroup>
</Project>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册