-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert Windows to PerformanceCounter metrics Change calculation for Linux CPU usage
- Loading branch information
Showing
8 changed files
with
155 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
GameServer/PerformanceStatistics/CurrentProcessCpuUsageStatistics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Versioning; | ||
|
||
namespace DOL.PerformanceStatistics | ||
{ | ||
public class CurrentProcessCpuUsagePercentStatistic : IPerformanceStatistic | ||
{ | ||
private IPerformanceStatistic processorTimeRatioStatistic; | ||
|
||
public CurrentProcessCpuUsagePercentStatistic() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
processorTimeRatioStatistic = new PerformanceCounterStatistic("Process", "% processor time", Process.GetCurrentProcess().ProcessName); | ||
} | ||
else | ||
{ | ||
processorTimeRatioStatistic = new LinuxCurrentProcessUsagePercentStatistic(); | ||
} | ||
} | ||
|
||
public float GetNextValue() | ||
=> processorTimeRatioStatistic.GetNextValue(); | ||
} | ||
|
||
#if NET | ||
[UnsupportedOSPlatform("Windows")] | ||
#endif | ||
internal class LinuxCurrentProcessUsagePercentStatistic : IPerformanceStatistic | ||
{ | ||
private IPerformanceStatistic idleProcessorTimeStatistic = new PerSecondStatistic(new LinuxSystemIdleProcessorTimeInSeconds()); | ||
private IPerformanceStatistic totalProcessorTimeStatistic = new PerSecondStatistic(new LinuxTotalProcessorTimeInSeconds()); | ||
private IPerformanceStatistic currentProcessProcessorTimeStatistic = new PerSecondStatistic(new LinuxCurrentProcessProcessorTimeInSeconds()); | ||
|
||
public float GetNextValue() | ||
{ | ||
var idleTime = idleProcessorTimeStatistic.GetNextValue(); | ||
var totalTime = totalProcessorTimeStatistic.GetNextValue(); | ||
var processTime = currentProcessProcessorTimeStatistic.GetNextValue(); | ||
return processTime / totalTime * 100 * Environment.ProcessorCount; | ||
} | ||
} | ||
|
||
#if NET | ||
[UnsupportedOSPlatform("Windows")] | ||
#endif | ||
internal class LinuxCurrentProcessProcessorTimeInSeconds : IPerformanceStatistic | ||
{ | ||
public float GetNextValue() | ||
{ | ||
var pid = Process.GetCurrentProcess().Id; | ||
var statArray = File.ReadAllText($"/proc/{pid}/stat").Split(' '); | ||
var processorTime = Convert.ToInt64(statArray[13]) + Convert.ToInt64(statArray[14]); | ||
return processorTime * 0.001f; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
GameServer/PerformanceStatistics/PerformaceCounterStatistics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Diagnostics; | ||
using System.Runtime.Versioning; | ||
|
||
namespace DOL.PerformanceStatistics | ||
{ | ||
#if NET | ||
[SupportedOSPlatform("Windows")] | ||
#endif | ||
public class PerformanceCounterStatistic : IPerformanceStatistic | ||
{ | ||
private PerformanceCounter performanceCounter; | ||
|
||
public PerformanceCounterStatistic(string categoryName, string counterName, string instanceName) | ||
{ | ||
performanceCounter = new PerformanceCounter(categoryName, counterName, instanceName); | ||
performanceCounter.NextValue(); | ||
} | ||
|
||
public float GetNextValue() => performanceCounter.NextValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters