Skip to content

Commit

Permalink
FO 3.2.14/CO 2.2.8 RTW
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Torre committed Jan 31, 2024
1 parent 4a2f7a8 commit 6d008e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
4 changes: 2 additions & 2 deletions FabricObserver.Extensibility/ObserverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ public bool UseCircularBuffer

public TimeSpan CpuMonitorDuration
{
get => ConfigurationSettings?.CpuMonitorDuration ?? TimeSpan.FromSeconds(4);
get => ConfigurationSettings?.CpuMonitorDuration ?? TimeSpan.FromSeconds(3);
set
{
if (ConfigurationSettings != null)
{
ConfigurationSettings.CpuMonitorDuration = value >= TimeSpan.FromSeconds(1) ? value : TimeSpan.FromSeconds(1);
ConfigurationSettings.CpuMonitorDuration = value;
}
}
}
Expand Down
41 changes: 19 additions & 22 deletions FabricObserver/Observers/AppObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3130,61 +3130,58 @@ private void ComputeResourceUsage(

// CPU \\

SafeProcessHandle procHandle = null;
ICpuUsage cpuUsage;

if (IsWindows)
{
procHandle = NativeMethods.GetSafeProcessHandle(procId);

if (procHandle == null || procHandle.IsClosed || procHandle.IsInvalid)
{
return;
}

cpuUsage = new CpuUsageWin32();
}
else
{
cpuUsage = new CpuUsageProcess();
}

Stopwatch timer = Stopwatch.StartNew();
SafeProcessHandle procHandle = null;

// CpuMonitorDuration can't be set to greater than 10s.
TimeSpan monitorDuration = CpuMonitorDuration <= TimeSpan.FromSeconds(10) ? CpuMonitorDuration : TimeSpan.FromSeconds(10);
TimeSpan cpuMonitorDuration = CpuMonitorDuration <= TimeSpan.FromSeconds(10) ? CpuMonitorDuration : TimeSpan.FromSeconds(10);

// CpuMonitorLoopSleepDuration can't be set to less than 500 milliseconds.
TimeSpan monitorLoopSleepTime = CpuMonitorLoopSleepDuration;
TimeSpan cpuMonitorLoopSleepTime = CpuMonitorLoopSleepDuration;

// At least one value is needed to compute CPU Time % (in fact, more than one is best on Windows). If the user misconfigures sleep time to be greater than monitor duration,
// then we'll just set it to 1000 ms.
if (monitorLoopSleepTime > monitorDuration)
if (cpuMonitorLoopSleepTime > cpuMonitorDuration)
{
monitorLoopSleepTime = TimeSpan.FromMilliseconds(1000);
cpuMonitorLoopSleepTime = TimeSpan.FromMilliseconds(1000);
}

// Limit high potential CPU usage by FO by limiting the duration of the CPU monitoring loop.
// Limit potential for high CPU usage by throttling max duration when monitoring CPU usage with multiple threads.
if (EnableConcurrentMonitoring)
{
if (monitorDuration >= TimeSpan.FromSeconds(5))
if (cpuMonitorDuration >= TimeSpan.FromSeconds(5))
{
monitorDuration = TimeSpan.FromSeconds(5);
cpuMonitorDuration = TimeSpan.FromSeconds(5);

// Always force 1s sleep time for concurrent monitoring when duration is >= 5s.
monitorLoopSleepTime = TimeSpan.FromSeconds(1000);
cpuMonitorLoopSleepTime = TimeSpan.FromSeconds(1000);
}
}

if (IsWindows)
{
procHandle = NativeMethods.GetSafeProcessHandle(procId);

if (procHandle == null || procHandle.IsClosed || procHandle.IsInvalid)
{
return;
}
}
Stopwatch timer = Stopwatch.StartNew();

try
{
#if DEBUG
ObserverLogger.LogInfo($"ComputeResourceUsage: Entering CPU monitor while loop. MonitorDuration = {CpuMonitorDuration}. CpuMonitorLoopSleepDuration = {CpuMonitorLoopSleepDuration}.");
#endif
while (timer.Elapsed <= monitorDuration)
while (timer.Elapsed <= cpuMonitorDuration)
{
if (token.IsCancellationRequested)
{
Expand Down Expand Up @@ -3250,7 +3247,7 @@ private void ComputeResourceUsage(
}
}

Thread.Sleep(monitorLoopSleepTime);
Thread.Sleep(cpuMonitorLoopSleepTime);
}
#if DEBUG
ObserverLogger.LogInfo($"ComputeResourceUsage: Exiting CPU monitoring while loop. Ran for {timer.Elapsed}.");
Expand Down
18 changes: 7 additions & 11 deletions FabricObserver/Observers/FabricSystemObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,30 +1043,26 @@ private async Task GetProcessInfoAsync(string procName, CancellationToken token)
if (IsWindows)
{
cpuUsage = new CpuUsageWin32();
procHandle = NativeMethods.GetSafeProcessHandle(procId);
}
else
{
cpuUsage = new CpuUsageProcess();
}

Stopwatch timer = Stopwatch.StartNew();
TimeSpan monitorDuration = CpuMonitorDuration;
TimeSpan monitorLoopSleepTime = CpuMonitorLoopSleepDuration;
TimeSpan cpuMonitorDuration = CpuMonitorDuration;
TimeSpan cpuMonitorLoopSleepTime = CpuMonitorLoopSleepDuration;

// At least one value is needed to compute CPU Time % (in fact, more than one is best on Windows). If the user misconfigures sleep time to be greater than monitor duration,
// then we'll just set it to 1000 ms.
if (monitorLoopSleepTime > monitorDuration)
if (cpuMonitorLoopSleepTime > cpuMonitorDuration)
{
// CpuMonitorDuration can't be set to less than 1 second.
monitorLoopSleepTime = TimeSpan.FromMilliseconds(1000);
}

if (IsWindows)
{
procHandle = NativeMethods.GetSafeProcessHandle(procId);
cpuMonitorLoopSleepTime = TimeSpan.FromMilliseconds(1000);
}

while (timer.Elapsed <= monitorDuration)
while (timer.Elapsed <= cpuMonitorDuration)
{
token.ThrowIfCancellationRequested();

Expand All @@ -1083,7 +1079,7 @@ private async Task GetProcessInfoAsync(string procName, CancellationToken token)
}
}

await Task.Delay(monitorLoopSleepTime, token);
await Task.Delay(cpuMonitorLoopSleepTime, token);
}
catch (Exception e) when (e is not (OperationCanceledException or TaskCanceledException))
{
Expand Down

0 comments on commit 6d008e0

Please sign in to comment.