Skip to content

Commit

Permalink
Add PerformanceStatistics for Linux
Browse files Browse the repository at this point in the history
Remove warnings from build
  • Loading branch information
NetDwarf authored Jul 14, 2022
2 parents 5bbdf99 + 913ed8f commit 0ab2120
Show file tree
Hide file tree
Showing 16 changed files with 507 additions and 339 deletions.
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;
}
}
}
52 changes: 52 additions & 0 deletions GameServer/PerformanceStatistics/DiskTransferStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace DOL.PerformanceStatistics
{
public class DiskTransfersPerSecondStatistic : IPerformanceStatistic
{
IPerformanceStatistic performanceStatistic;

public DiskTransfersPerSecondStatistic()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
performanceStatistic = new PerformanceCounterStatistic("PhysicalDisk", "Disk Transfers/sec", "_Total");
}
else
{
performanceStatistic = new PerSecondStatistic(new LinuxTotalDiskTransfers());
}
}

public float GetNextValue() => performanceStatistic.GetNextValue();
}

#if NET
[UnsupportedOSPlatform("Windows")]
#endif
internal class LinuxTotalDiskTransfers : IPerformanceStatistic
{
public float GetNextValue()
{
var diskstats = File.ReadAllText("/proc/diskstats");
var transferCount = 0L;
foreach (var line in diskstats.Split('\n'))
{
var columns = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (columns.Length < 14) continue;
var deviceName = columns[2];
if (char.IsDigit(deviceName[deviceName.Length - 1])) continue;

var readIO = Convert.ToInt64(columns[3]);
var writeIO = Convert.ToInt64(columns[7]);
var discardIO = 0L;
if (columns.Length >= 18) discardIO = Convert.ToInt64(columns[14]);
transferCount += (readIO + writeIO + discardIO);
}
return transferCount;
}
}
}
7 changes: 7 additions & 0 deletions GameServer/PerformanceStatistics/IPerformanceStatistic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DOL.PerformanceStatistics
{
public interface IPerformanceStatistic
{
float GetNextValue();
}
}
52 changes: 52 additions & 0 deletions GameServer/PerformanceStatistics/PageFaultStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace DOL.PerformanceStatistics
{
public class PageFaultsPerSecondStatistic : IPerformanceStatistic
{
IPerformanceStatistic performanceStatistic;

public PageFaultsPerSecondStatistic()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
performanceStatistic = new PerformanceCounterStatistic("Memory", "Pages/sec", null);
}
else
{
performanceStatistic = new LinuxPageFaultsPerSecondStatistic();
}
}

public float GetNextValue() => performanceStatistic.GetNextValue();
}

#if NET
[UnsupportedOSPlatform("Windows")]
#endif
internal class LinuxPageFaultsPerSecondStatistic : IPerformanceStatistic
{
private IPerformanceStatistic memoryFaultsPerSecondStatistic;

public LinuxPageFaultsPerSecondStatistic()
{
memoryFaultsPerSecondStatistic = new PerSecondStatistic(new LinuxTotalPageFaults());
}

public float GetNextValue() => memoryFaultsPerSecondStatistic.GetNextValue();
}

internal class LinuxTotalPageFaults : IPerformanceStatistic
{
public float GetNextValue()
{
return Convert.ToInt64(File.ReadAllText("/proc/vmstat").Split('\n')
.Where(s => s.StartsWith("pgfault"))
.First().Split(' ')[1]);
}
}
}
40 changes: 40 additions & 0 deletions GameServer/PerformanceStatistics/PerSecondStatistic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Reflection;
using log4net;

namespace DOL.PerformanceStatistics
{
internal class PerSecondStatistic : IPerformanceStatistic
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

DateTime lastMeasurementTime;
float lastTotal;
IPerformanceStatistic totalValueStatistic;
float cachedLastStatisticValue = -1;

public PerSecondStatistic(IPerformanceStatistic totalValueStatistic)
{
lastMeasurementTime = DateTime.UtcNow;
this.totalValueStatistic = totalValueStatistic;
lastTotal = totalValueStatistic.GetNextValue();
}

public float GetNextValue()
{
if (totalValueStatistic == null) return -1;
var currentTime = DateTime.UtcNow;
var secondsPassed = (currentTime - lastMeasurementTime).TotalSeconds;
if (secondsPassed < 1) return cachedLastStatisticValue;

var currentTotal = totalValueStatistic.GetNextValue();
var valuePerSecond = (currentTotal - lastTotal) / secondsPassed;

lastMeasurementTime = currentTime;
lastTotal = currentTotal;
cachedLastStatisticValue = (float)valuePerSecond;
return cachedLastStatisticValue;
}
}

}
21 changes: 21 additions & 0 deletions GameServer/PerformanceStatistics/PerformaceCounterStatistics.cs
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();
}
}
77 changes: 77 additions & 0 deletions GameServer/PerformanceStatistics/SystemCpuUsageStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace DOL.PerformanceStatistics
{
public class SystemCpuUsagePercent : IPerformanceStatistic
{
private IPerformanceStatistic processorTimeRatioStatistic;

public SystemCpuUsagePercent()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
processorTimeRatioStatistic = new PerformanceCounterStatistic("Processor", "% processor time", "_total");
}
else
{
processorTimeRatioStatistic = new LinuxSystemCpuUsagePercent();
}
}

public float GetNextValue()
=> processorTimeRatioStatistic.GetNextValue();
}

#if NET
[UnsupportedOSPlatform("Windows")]
#endif
internal class LinuxSystemCpuUsagePercent : IPerformanceStatistic
{
private IPerformanceStatistic processorTimeStatistic;
private IPerformanceStatistic idleTimeStatistic;

public LinuxSystemCpuUsagePercent()
{
processorTimeStatistic = new PerSecondStatistic(new LinuxTotalProcessorTimeInSeconds());
idleTimeStatistic = new PerSecondStatistic(new LinuxSystemIdleProcessorTimeInSeconds());
}

public float GetNextValue()
{
var cpuUsage = (1 - (idleTimeStatistic.GetNextValue() / processorTimeStatistic.GetNextValue()));
return cpuUsage * 100;
}
}

#if NET
[UnsupportedOSPlatform("Windows")]
#endif
internal class LinuxTotalProcessorTimeInSeconds : IPerformanceStatistic
{
public float GetNextValue()
{
var cpuTimeInSeconds = File.ReadAllText("/proc/stat").Split('\n')
.First().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1).Select(c => Convert.ToInt64(c)).Aggregate(0L, (a, b) => a + b) * 0.001f;
return cpuTimeInSeconds;
}
}

#if NET
[UnsupportedOSPlatform("Windows")]
#endif
internal class LinuxSystemIdleProcessorTimeInSeconds : IPerformanceStatistic
{
public float GetNextValue()
{
var cpuIdleTimeString = File.ReadAllText("/proc/stat").Split('\n')
.First().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[4];
var cpuIdleTimeInSeconds = Convert.ToInt64(cpuIdleTimeString) * 0.001f;
return cpuIdleTimeInSeconds;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GameInventorySiegeBallista(InventoryItem item)
/// </summary>
/// <param name="player"></param>
/// <returns>true if item use is handled here</returns>
public bool Use(GamePlayer player)
public override bool Use(GamePlayer player)
{
// Create the siege ballista
GameSiegeBallista bal = new GameSiegeBallista();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GameInventorySiegeCatapult(InventoryItem item)
/// </summary>
/// <param name="player"></param>
/// <returns>true if item use is handled here</returns>
public bool Use(GamePlayer player)
public override bool Use(GamePlayer player)
{
// Create the siege catapult
GameSiegeCatapult cat = new GameSiegeCatapult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GameInventorySiegeRam(InventoryItem item)
/// </summary>
/// <param name="player"></param>
/// <returns>true if item use is handled here</returns>
public bool Use(GamePlayer player)
public override bool Use(GamePlayer player)
{
// Create the siege ram
GameSiegeRam ram = new GameSiegeRam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GameInventorySiegeTrebuchet(InventoryItem item)
/// </summary>
/// <param name="player"></param>
/// <returns>true if item use is handled here</returns>
public bool Use(GamePlayer player)
public override bool Use(GamePlayer player)
{
// Create the siege trebuchet
GameSiegeBallista bal = new GameSiegeBallista();
Expand Down
10 changes: 0 additions & 10 deletions GameServer/gameutils/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,6 @@ public DateTime GuildBannerLostTime
{
get
{
if (m_DBguild.GuildBannerLostTime == null)
{
return new DateTime(2010, 1, 1);
}

return m_DBguild.GuildBannerLostTime;
}
set
Expand Down Expand Up @@ -916,11 +911,6 @@ public DateTime BonusStartTime
{
get
{
if (m_DBguild.BonusStartTime == null)
{
return new DateTime(2010, 1, 1);
}

return this.m_DBguild.BonusStartTime;
}
set
Expand Down
2 changes: 1 addition & 1 deletion GameServer/gameutils/Hashtable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public object this[object key] {
log.Warn("internalArraySize="+internalArray.Length);
log.Warn("checking "+i);
log.Error(e);
throw e;
throw;
}
return null;
}
Expand Down
Loading

0 comments on commit 0ab2120

Please sign in to comment.