Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependencies: remove System.Diagnostics.PerformanceCounter #2285

Merged
merged 2 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ReleaseNotes.md
Expand Up @@ -10,6 +10,7 @@ Current package versions:

- Adds: `last-in` and `cur-in` (bytes) to timeout exceptions to help identify timeouts that were just-behind another large payload off the wire ([#2276 by NickCraver](https://github.com/StackExchange/StackExchange.Redis/pull/2276))
- Adds: general-purpose tunnel support, with HTTP proxy "connect" support included ([#2274 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2274))
- Removes: Package dependency (`System.Diagnostics.PerformanceCounter`) ([#2285 by NickCraver](https://github.com/StackExchange/StackExchange.Redis/pull/2285))

## 2.6.70

Expand Down
7 changes: 1 addition & 6 deletions src/StackExchange.Redis/ExceptionFactory.cs
Expand Up @@ -156,7 +156,7 @@ internal static string GetInnerMostExceptionMessage(Exception? e)
if (multiplexer.RawConfig.IncludeDetailInExceptions)
{
CopyDataToException(data, ex);
sb.Append("; ").Append(PerfCounterHelper.GetThreadPoolAndCPUSummary(multiplexer.RawConfig.IncludePerformanceCountersInExceptions));
sb.Append("; ").Append(PerfCounterHelper.GetThreadPoolAndCPUSummary());
AddExceptionDetail(ex, message, server, commandLabel);
}
return ex;
Expand Down Expand Up @@ -353,11 +353,6 @@ private static void CopyDataToException(List<Tuple<string, string>>? data, Excep
}
data.Add(Tuple.Create("Busy-Workers", busyWorkerCount.ToString()));

if (multiplexer.RawConfig.IncludePerformanceCountersInExceptions)
{
Add(data, sb, "Local-CPU", "Local-CPU", PerfCounterHelper.GetSystemCpuPercent());
}

Add(data, sb, "Version", "v", Utils.GetLibVersion());
}

Expand Down
64 changes: 3 additions & 61 deletions src/StackExchange.Redis/PerfCounterHelper.cs
@@ -1,73 +1,15 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
#if NET5_0_OR_GREATER
using System.Runtime.Versioning;
#endif
using System.Threading;

namespace StackExchange.Redis
{
internal static class PerfCounterHelper
{
private static readonly object staticLock = new();
private static volatile PerformanceCounter? _cpu;
private static volatile bool _disabled = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

#if NET5_0_OR_GREATER
[SupportedOSPlatform("Windows")]
#endif
public static bool TryGetSystemCPU(out float value)
{
value = -1;

try
{
if (!_disabled && _cpu == null)
{
lock (staticLock)
{
if (_cpu == null)
{
_cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");

// First call always returns 0, so get that out of the way.
_cpu.NextValue();
}
}
}
}
catch (UnauthorizedAccessException)
{
// Some environments don't allow access to Performance Counters, so stop trying.
_disabled = true;
}
catch (Exception e)
{
// this shouldn't happen, but just being safe...
Trace.WriteLine(e);
}

if (!_disabled && _cpu != null)
{
value = _cpu.NextValue();
return true;
}
return false;
}

internal static string GetThreadPoolAndCPUSummary(bool includePerformanceCounters)
internal static string GetThreadPoolAndCPUSummary()
{
GetThreadPoolStats(out string iocp, out string worker, out string? workItems);
var cpu = includePerformanceCounters ? GetSystemCpuPercent() : "n/a";
return $"IOCP: {iocp}, WORKER: {worker}, POOL: {workItems ?? "n/a"}, Local-CPU: {cpu}";
return $"IOCP: {iocp}, WORKER: {worker}, POOL: {workItems ?? "n/a"}";
}

internal static string GetSystemCpuPercent() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && TryGetSystemCPU(out float systemCPU)
? Math.Round(systemCPU, 2) + "%"
: "unavailable";

internal static int GetThreadPoolStats(out string iocp, out string worker, out string? workItems)
{
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIoThreads);
Expand Down
2 changes: 1 addition & 1 deletion src/StackExchange.Redis/ResultProcessor.cs
Expand Up @@ -282,7 +282,7 @@ public virtual bool SetResult(PhysicalConnection connection, Message message, in
if (bridge.Multiplexer.IncludeDetailInExceptions)
{
err = $"Endpoint {endpoint} serving hashslot {hashSlot} is not reachable at this point of time. Please check connectTimeout value. If it is low, try increasing it to give the ConnectionMultiplexer a chance to recover from the network disconnect. "
+ PerfCounterHelper.GetThreadPoolAndCPUSummary(bridge.Multiplexer.RawConfig.IncludePerformanceCountersInExceptions);
+ PerfCounterHelper.GetThreadPoolAndCPUSummary();
}
else
{
Expand Down
1 change: 0 additions & 1 deletion src/StackExchange.Redis/StackExchange.Redis.csproj
Expand Up @@ -18,7 +18,6 @@
<ItemGroup>
<!-- needed everywhere -->
<PackageReference Include="Pipelines.Sockets.Unofficial" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" />

<!-- built into .NET core now -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Condition="'$(TargetFramework)' == 'net472' or '$(TargetFramework)' == 'net461' or '$(TargetFramework)' == 'netstandard2.0'" />
Expand Down