Skip to content

Commit 5263608

Browse files
authored
Dependencies: remove System.Diagnostics.PerformanceCounter (#2285)
The original purpose of this was to get system-level CPU to help advise people filing issues that their machine overall was under too much load and that's why we were seeing timeouts. However, due to ecosystem changes and shifts the actual reporting of this counter has dropped off so dramatically it's not actually doing what it's supposed to be doing and giving us signal data to help. Given it's a dependency chain that also depends ultimately on some problematic packages now (e.g. System.Drawing.Common) and isn't cross-platform correctly...let's just remove it. It's not a net win anymore. Fixes #2283.
1 parent ae28ae6 commit 5263608

File tree

5 files changed

+6
-69
lines changed

5 files changed

+6
-69
lines changed

docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Current package versions:
1010

1111
- 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))
1212
- Adds: general-purpose tunnel support, with HTTP proxy "connect" support included ([#2274 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2274))
13+
- Removes: Package dependency (`System.Diagnostics.PerformanceCounter`) ([#2285 by NickCraver](https://github.com/StackExchange/StackExchange.Redis/pull/2285))
1314

1415
## 2.6.70
1516

src/StackExchange.Redis/ExceptionFactory.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ internal static Exception NoConnectionAvailable(
156156
if (multiplexer.RawConfig.IncludeDetailInExceptions)
157157
{
158158
CopyDataToException(data, ex);
159-
sb.Append("; ").Append(PerfCounterHelper.GetThreadPoolAndCPUSummary(multiplexer.RawConfig.IncludePerformanceCountersInExceptions));
159+
sb.Append("; ").Append(PerfCounterHelper.GetThreadPoolAndCPUSummary());
160160
AddExceptionDetail(ex, message, server, commandLabel);
161161
}
162162
return ex;
@@ -353,11 +353,6 @@ private static void AddCommonDetail(
353353
}
354354
data.Add(Tuple.Create("Busy-Workers", busyWorkerCount.ToString()));
355355

356-
if (multiplexer.RawConfig.IncludePerformanceCountersInExceptions)
357-
{
358-
Add(data, sb, "Local-CPU", "Local-CPU", PerfCounterHelper.GetSystemCpuPercent());
359-
}
360-
361356
Add(data, sb, "Version", "v", Utils.GetLibVersion());
362357
}
363358

src/StackExchange.Redis/PerfCounterHelper.cs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,15 @@
1-
using System;
2-
using System.Diagnostics;
3-
using System.Runtime.InteropServices;
4-
using System.Threading;
5-
#if NET5_0_OR_GREATER
6-
using System.Runtime.Versioning;
7-
#endif
1+
using System.Threading;
82

93
namespace StackExchange.Redis
104
{
115
internal static class PerfCounterHelper
126
{
13-
private static readonly object staticLock = new();
14-
private static volatile PerformanceCounter? _cpu;
15-
private static volatile bool _disabled = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
16-
17-
#if NET5_0_OR_GREATER
18-
[SupportedOSPlatform("Windows")]
19-
#endif
20-
public static bool TryGetSystemCPU(out float value)
21-
{
22-
value = -1;
23-
24-
try
25-
{
26-
if (!_disabled && _cpu == null)
27-
{
28-
lock (staticLock)
29-
{
30-
if (_cpu == null)
31-
{
32-
_cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
33-
34-
// First call always returns 0, so get that out of the way.
35-
_cpu.NextValue();
36-
}
37-
}
38-
}
39-
}
40-
catch (UnauthorizedAccessException)
41-
{
42-
// Some environments don't allow access to Performance Counters, so stop trying.
43-
_disabled = true;
44-
}
45-
catch (Exception e)
46-
{
47-
// this shouldn't happen, but just being safe...
48-
Trace.WriteLine(e);
49-
}
50-
51-
if (!_disabled && _cpu != null)
52-
{
53-
value = _cpu.NextValue();
54-
return true;
55-
}
56-
return false;
57-
}
58-
59-
internal static string GetThreadPoolAndCPUSummary(bool includePerformanceCounters)
7+
internal static string GetThreadPoolAndCPUSummary()
608
{
619
GetThreadPoolStats(out string iocp, out string worker, out string? workItems);
62-
var cpu = includePerformanceCounters ? GetSystemCpuPercent() : "n/a";
63-
return $"IOCP: {iocp}, WORKER: {worker}, POOL: {workItems ?? "n/a"}, Local-CPU: {cpu}";
10+
return $"IOCP: {iocp}, WORKER: {worker}, POOL: {workItems ?? "n/a"}";
6411
}
6512

66-
internal static string GetSystemCpuPercent() =>
67-
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && TryGetSystemCPU(out float systemCPU)
68-
? Math.Round(systemCPU, 2) + "%"
69-
: "unavailable";
70-
7113
internal static int GetThreadPoolStats(out string iocp, out string worker, out string? workItems)
7214
{
7315
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIoThreads);

src/StackExchange.Redis/ResultProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public virtual bool SetResult(PhysicalConnection connection, Message message, in
282282
if (bridge.Multiplexer.IncludeDetailInExceptions)
283283
{
284284
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. "
285-
+ PerfCounterHelper.GetThreadPoolAndCPUSummary(bridge.Multiplexer.RawConfig.IncludePerformanceCountersInExceptions);
285+
+ PerfCounterHelper.GetThreadPoolAndCPUSummary();
286286
}
287287
else
288288
{

src/StackExchange.Redis/StackExchange.Redis.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<ItemGroup>
1919
<!-- needed everywhere -->
2020
<PackageReference Include="Pipelines.Sockets.Unofficial" />
21-
<PackageReference Include="System.Diagnostics.PerformanceCounter" />
2221

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

0 commit comments

Comments
 (0)