Skip to content

Commit

Permalink
Performance improvement for PerfCounters. switching to PerformanceCou…
Browse files Browse the repository at this point in the history
…nterCategory api (#1695)

* suggested change

* fix

* Update CHANGELOG.md
  • Loading branch information
TimothyMothra authored Feb 20, 2020
1 parent 58618d7 commit 77a90ef
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Adding a flag to DependencyTrackingTelemetryModule to enable/disable collection of SQL Command text.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1514)
Collecting SQL Command Text will now be opt-in, so this value will default to false. This is a change from the current behavior on .NET Core. To see how to collect SQL Command Text see here for details: https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-dependencies#advanced-sql-tracking-to-get-full-sql-query
- [change references to log4net to version 2.0.8](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1675)
- [Fix: PerformanceCounter implementation is taking large memory allocation](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1694)

## Version 2.13.0-beta2
- [Move FileDiagnosticTelemetryModule to Microsoft.ApplicationInsights assembly.](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1059)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ internal static class PerformanceCounterUtility
private const string WebSiteIsolationEnvironmentVariable = "WEBSITE_ISOLATION";
private const string WebSiteIsolationHyperV = "hyperv";

#if !NETSTANDARD1_6
private static readonly ConcurrentDictionary<string, Tuple<DateTime, PerformanceCounterCategory, InstanceDataCollectionCollection>> cache = new ConcurrentDictionary<string, Tuple<DateTime, PerformanceCounterCategory, InstanceDataCollectionCollection>>();
#endif

private static readonly ConcurrentDictionary<string, string> PlaceholderCache =
new ConcurrentDictionary<string, string>();

Expand Down Expand Up @@ -480,26 +484,54 @@ private static string ExpandInstanceName(
}

#if !NETSTANDARD1_6
private static string FindProcessInstance(
int pid,
IEnumerable<string> instances,
string categoryName,
string counterName)
private static string FindProcessInstance(int pid, IEnumerable<string> instances, string categoryName, string counterName)
{
return instances.FirstOrDefault(
i =>
Tuple<DateTime, PerformanceCounterCategory, InstanceDataCollectionCollection> cached;

DateTime utcNow = DateTime.UtcNow;

InstanceDataCollectionCollection result = null;

PerformanceCounterCategory category = null;

if (cache.TryGetValue(categoryName, out cached))
{
category = cached.Item2;

if (cached.Item1 < utcNow)
{
try
{
return pid == (int)new PerformanceCounter(categoryName, counterName, i, true).RawValue;
}
catch (Exception)
result = cached.Item3;
}
}

if (result == null)
{
if (category == null)
{
category = new PerformanceCounterCategory(categoryName);
}

result = category.ReadCategory();

cache.TryAdd(categoryName, new Tuple<DateTime, PerformanceCounterCategory, InstanceDataCollectionCollection>(utcNow.AddMinutes(1), category, result));
}

InstanceDataCollection counters = result[counterName];

if (counters != null)
{
foreach (string i in instances)
{
InstanceData instance = counters[i];

if ((instance != null) && (pid == instance.RawValue))
{
// most likely the process has terminated since we got the process list
// that process is not us, we're still running
return false;
return i;
}
});
}
}

return null;
}

private static IList<string> GetInstances(string categoryName)
Expand Down

0 comments on commit 77a90ef

Please sign in to comment.