Re-add performance thresholds and add whitelist/blacklist options
This commit is contained in:
@@ -1,25 +1,40 @@
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.FileCache;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
using MareSynchronos.PlayerData.Handlers;
|
||||
using MareSynchronos.Services.Events;
|
||||
using MareSynchronos.Services.Mediator;
|
||||
using MareSynchronos.Services.ServerConfiguration;
|
||||
using MareSynchronos.UI;
|
||||
using MareSynchronos.WebAPI.Files.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronos.Services;
|
||||
|
||||
public class PlayerPerformanceService
|
||||
public class PlayerPerformanceService : DisposableMediatorSubscriberBase
|
||||
{
|
||||
// Limits that will still be enforced when no limits are enabled
|
||||
public const int MaxVRAMUsageThreshold = 2000; // 2GB
|
||||
public const int MaxTriUsageThreshold = 2000000; // 2 million triangles
|
||||
|
||||
private readonly FileCacheManager _fileCacheManager;
|
||||
private readonly XivDataAnalyzer _xivDataAnalyzer;
|
||||
private readonly ILogger<PlayerPerformanceService> _logger;
|
||||
private readonly MareMediator _mediator;
|
||||
private readonly ServerConfigurationManager _serverConfigurationManager;
|
||||
private readonly PlayerPerformanceConfigService _playerPerformanceConfigService;
|
||||
private readonly Dictionary<string, bool> _warnedForPlayers = new(StringComparer.Ordinal);
|
||||
|
||||
public PlayerPerformanceService(ILogger<PlayerPerformanceService> logger, MareMediator mediator,
|
||||
FileCacheManager fileCacheManager,
|
||||
ServerConfigurationManager serverConfigurationManager,
|
||||
PlayerPerformanceConfigService playerPerformanceConfigService, FileCacheManager fileCacheManager,
|
||||
XivDataAnalyzer xivDataAnalyzer)
|
||||
: base(logger, mediator)
|
||||
{
|
||||
_logger = logger;
|
||||
_mediator = mediator;
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
_playerPerformanceConfigService = playerPerformanceConfigService;
|
||||
_fileCacheManager = fileCacheManager;
|
||||
_xivDataAnalyzer = xivDataAnalyzer;
|
||||
}
|
||||
@@ -36,6 +51,7 @@ public class PlayerPerformanceService
|
||||
|
||||
public async Task<bool> CheckTriangleUsageThresholds(PairHandler pairHandler, CharacterData charaData)
|
||||
{
|
||||
var config = _playerPerformanceConfigService.Current;
|
||||
var pair = pairHandler.Pair;
|
||||
|
||||
long triUsage = 0;
|
||||
@@ -58,13 +74,42 @@ public class PlayerPerformanceService
|
||||
|
||||
pair.LastAppliedDataTris = triUsage;
|
||||
|
||||
_logger.LogDebug("Calculated VRAM usage for {p}", pairHandler);
|
||||
_logger.LogDebug("Calculated Triangle usage for {p}", pairHandler);
|
||||
|
||||
long triUsageThreshold = config.TrisAutoPauseThresholdThousands * 1000;
|
||||
bool isDirect = pair.UserPair != null;
|
||||
bool autoPause = config.AutoPausePlayersExceedingThresholds;
|
||||
bool notify = isDirect ? config.NotifyAutoPauseDirectPairs : config.NotifyAutoPauseGroupPairs;
|
||||
|
||||
if (autoPause && isDirect && config.IgnoreDirectPairs)
|
||||
autoPause = false;
|
||||
|
||||
if (!autoPause || _serverConfigurationManager.IsUidWhitelisted(pair.UserData.UID))
|
||||
triUsageThreshold = MaxTriUsageThreshold;
|
||||
|
||||
if (triUsage > triUsageThreshold)
|
||||
{
|
||||
if (notify && !pair.IsApplicationBlocked)
|
||||
{
|
||||
_mediator.Publish(new NotificationMessage($"{pair.PlayerName} ({pair.UserData.AliasOrUID}) automatically blocked",
|
||||
$"Player {pair.PlayerName} ({pair.UserData.AliasOrUID}) exceeded your configured triangle auto block threshold (" +
|
||||
$"{triUsage}/{triUsageThreshold} triangles)" +
|
||||
$" and has been automatically blocked.",
|
||||
MareConfiguration.Models.NotificationType.Warning));
|
||||
}
|
||||
|
||||
_mediator.Publish(new EventMessage(new Event(pair.PlayerName, pair.UserData, nameof(PlayerPerformanceService), EventSeverity.Warning,
|
||||
$"Exceeds triangle threshold: ({triUsage}/{triUsageThreshold} triangles)")));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ComputeAndAutoPauseOnVRAMUsageThresholds(PairHandler pairHandler, CharacterData charaData, List<DownloadFileTransfer> toDownloadFiles)
|
||||
{
|
||||
var config = _playerPerformanceConfigService.Current;
|
||||
var pair = pairHandler.Pair;
|
||||
|
||||
long vramUsage = 0;
|
||||
@@ -110,6 +155,34 @@ public class PlayerPerformanceService
|
||||
|
||||
_logger.LogDebug("Calculated VRAM usage for {p}", pairHandler);
|
||||
|
||||
long vramUsageThreshold = config.VRAMSizeAutoPauseThresholdMiB;
|
||||
bool isDirect = pair.UserPair != null;
|
||||
bool autoPause = config.AutoPausePlayersExceedingThresholds;
|
||||
bool notify = isDirect ? config.NotifyAutoPauseDirectPairs : config.NotifyAutoPauseGroupPairs;
|
||||
|
||||
if (autoPause && isDirect && config.IgnoreDirectPairs)
|
||||
autoPause = false;
|
||||
|
||||
if (!autoPause || _serverConfigurationManager.IsUidWhitelisted(pair.UserData.UID))
|
||||
vramUsageThreshold = MaxVRAMUsageThreshold;
|
||||
|
||||
if (vramUsage > vramUsageThreshold * 1024 * 1024)
|
||||
{
|
||||
if (notify && !pair.IsApplicationBlocked)
|
||||
{
|
||||
_mediator.Publish(new NotificationMessage($"{pair.PlayerName} ({pair.UserData.AliasOrUID}) automatically blocked",
|
||||
$"Player {pair.PlayerName} ({pair.UserData.AliasOrUID}) exceeded your configured VRAM auto block threshold (" +
|
||||
$"{UiSharedService.ByteToString(vramUsage, addSuffix: true)}/{vramUsageThreshold}MiB)" +
|
||||
$" and has been automatically blocked.",
|
||||
MareConfiguration.Models.NotificationType.Warning));
|
||||
}
|
||||
|
||||
_mediator.Publish(new EventMessage(new Event(pair.PlayerName, pair.UserData, nameof(PlayerPerformanceService), EventSeverity.Warning,
|
||||
$"Exceeds VRAM threshold: ({UiSharedService.ByteToString(vramUsage, addSuffix: true)}/{vramUsageThreshold} MiB)")));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user