something something performance
This commit is contained in:
134
MareSynchronos/Services/PlayerPerformanceService.cs
Normal file
134
MareSynchronos/Services/PlayerPerformanceService.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.API.Data.Extensions;
|
||||
using MareSynchronos.FileCache;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
using MareSynchronos.PlayerData.Handlers;
|
||||
using MareSynchronos.PlayerData.Pairs;
|
||||
using MareSynchronos.Services.Mediator;
|
||||
using MareSynchronos.UI;
|
||||
using MareSynchronos.Utils;
|
||||
using MareSynchronos.WebAPI;
|
||||
using MareSynchronos.WebAPI.Files.Models;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MareSynchronos.Services;
|
||||
|
||||
public class PlayerPerformanceService : IHostedService
|
||||
{
|
||||
private readonly ILogger<PlayerPerformanceService> _logger;
|
||||
private readonly MareMediator _mediator;
|
||||
private readonly PlayerPerformanceConfigService _playerPerformanceConfigService;
|
||||
private readonly FileCacheManager _fileCacheManager;
|
||||
private readonly PairManager _pairManager;
|
||||
private readonly ApiController _apiController;
|
||||
|
||||
public PlayerPerformanceService(ILogger<PlayerPerformanceService> logger, MareMediator mediator,
|
||||
PlayerPerformanceConfigService playerPerformanceConfigService, FileCacheManager fileCacheManager,
|
||||
PairManager pairManager, ApiController apiController)
|
||||
{
|
||||
_logger = logger;
|
||||
_mediator = mediator;
|
||||
_playerPerformanceConfigService = playerPerformanceConfigService;
|
||||
_fileCacheManager = fileCacheManager;
|
||||
_pairManager = pairManager;
|
||||
_apiController = apiController;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public bool TryCalculateVRAMUsage(PairHandler pairHandler, CharacterData charaData, List<DownloadFileTransfer> toDownloadFiles, out long vramUsage)
|
||||
{
|
||||
var config = _playerPerformanceConfigService.Current;
|
||||
var pair = _pairManager.GetOnlineUserPairs().First(p => string.Equals(p.UserData.UID, pairHandler.OnlineUser.User.UID, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
vramUsage = 0;
|
||||
|
||||
if (!charaData.FileReplacements.TryGetValue(API.Data.Enum.ObjectKind.Player, out List<FileReplacementData>? playerReplacements))
|
||||
return true;
|
||||
|
||||
var moddedTextureHashes = playerReplacements.Where(p => string.IsNullOrEmpty(p.FileSwapPath) && p.GamePaths.Any(g => g.EndsWith("tex", StringComparison.OrdinalIgnoreCase)))
|
||||
.Select(p => p.Hash)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
|
||||
foreach (var hash in moddedTextureHashes.ToList())
|
||||
{
|
||||
long fileSize = 0;
|
||||
|
||||
var download = toDownloadFiles.Find(f => string.Equals(hash, f.Hash, StringComparison.OrdinalIgnoreCase));
|
||||
if (download != null)
|
||||
{
|
||||
fileSize = download.Total;
|
||||
}
|
||||
else
|
||||
{
|
||||
var fileEntry = _fileCacheManager.GetFileCacheByHash(hash);
|
||||
if (fileEntry == null) continue;
|
||||
|
||||
if (fileEntry.Size == null)
|
||||
{
|
||||
fileEntry.Size = new FileInfo(fileEntry.ResolvedFilepath).Length;
|
||||
_fileCacheManager.UpdateHashedFile(fileEntry, computeProperties: true);
|
||||
}
|
||||
|
||||
fileSize = fileEntry.Size.Value;
|
||||
}
|
||||
|
||||
vramUsage += fileSize;
|
||||
}
|
||||
|
||||
_logger.LogDebug("Calculated VRAM usage for {p}", pairHandler);
|
||||
|
||||
// no warning of any kind on ignored pairs
|
||||
if (config.UIDsToIgnore
|
||||
.Exists(uid => string.Equals(uid, pair.UserData.Alias, StringComparison.Ordinal) || string.Equals(uid, pair.UserData.UID, StringComparison.Ordinal)))
|
||||
return true;
|
||||
|
||||
bool isPrefPerm = pair.UserPair.OwnPermissions.HasFlag(API.Data.Enum.UserPermissions.Sticky);
|
||||
|
||||
// now check auto pause
|
||||
if (CheckForThreshold(config.AutoPausePlayersExceedingThresholds, config.VRAMSizeAutoPauseThresholdMiB * 1024 * 1024,
|
||||
vramUsage, config.AutoPausePlayersWithPreferredPermissionsExceedingThresholds, isPrefPerm))
|
||||
{
|
||||
_mediator.Publish(new NotificationMessage($"{pair.PlayerName} ({pair.UserData.AliasOrUID}) automatically paused",
|
||||
$"Player {pair.PlayerName} exceeded your configured VRAM auto pause threshold (" +
|
||||
$"{UiSharedService.ByteToString(vramUsage, addSuffix: true)}/{config.VRAMSizeAutoPauseThresholdMiB}MiB)" +
|
||||
$" and has been automatically paused.",
|
||||
MareConfiguration.Models.NotificationType.Warning));
|
||||
|
||||
// pause
|
||||
var perm = pair.UserPair.OwnPermissions.DeepClone();
|
||||
perm.SetPaused(paused: true);
|
||||
_ = _apiController.SetBulkPermissions(new(
|
||||
new(StringComparer.Ordinal) { { pair.UserData.UID, perm } },
|
||||
new(StringComparer.Ordinal)));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// and fucking warnings
|
||||
if (CheckForThreshold(config.WarnOnExceedingThresholds, config.VRAMSizeWarningThresholdMiB * 1024 * 1024,
|
||||
vramUsage, config.WarnOnPreferredPermissionsExceedingThresholds, isPrefPerm))
|
||||
{
|
||||
_mediator.Publish(new NotificationMessage($"{pair.PlayerName} ({pair.UserData.AliasOrUID}) exceeds performance threshold",
|
||||
$"Player {pair.PlayerName} exceeds your configured VRAM warning threshold (" +
|
||||
$"{UiSharedService.ByteToString(vramUsage, true)}/{config.VRAMSizeWarningThresholdMiB}MiB).",
|
||||
MareConfiguration.Models.NotificationType.Warning));
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool CheckForThreshold(bool thresholdEnabled, long threshold, long value, bool checkForPrefPerm, bool isPrefPerm) =>
|
||||
thresholdEnabled && threshold > 0 && threshold < value && ((checkForPrefPerm && isPrefPerm) || !isPrefPerm);
|
||||
}
|
||||
@@ -169,7 +169,7 @@ public class ServerConfigurationManager
|
||||
Save();
|
||||
}
|
||||
|
||||
internal void AddCurrentCharacterToServer(int serverSelectionIndex = -1, bool addLastSecretKey = false)
|
||||
internal void AddCurrentCharacterToServer(int serverSelectionIndex = -1)
|
||||
{
|
||||
if (serverSelectionIndex == -1) serverSelectionIndex = CurrentServerIndex;
|
||||
var server = GetServerByIndex(serverSelectionIndex);
|
||||
@@ -181,7 +181,7 @@ public class ServerConfigurationManager
|
||||
{
|
||||
CharacterName = _dalamudUtil.GetPlayerNameAsync().GetAwaiter().GetResult(),
|
||||
WorldId = _dalamudUtil.GetHomeWorldIdAsync().GetAwaiter().GetResult(),
|
||||
SecretKeyIdx = addLastSecretKey ? server.SecretKeys.Last().Key : -1,
|
||||
SecretKeyIdx = server.SecretKeys.Last().Key,
|
||||
});
|
||||
Save();
|
||||
}
|
||||
@@ -189,7 +189,10 @@ public class ServerConfigurationManager
|
||||
internal void AddEmptyCharacterToServer(int serverSelectionIndex)
|
||||
{
|
||||
var server = GetServerByIndex(serverSelectionIndex);
|
||||
server.Authentications.Add(new Authentication());
|
||||
server.Authentications.Add(new Authentication()
|
||||
{
|
||||
SecretKeyIdx = server.SecretKeys.Any() ? server.SecretKeys.First().Key : -1,
|
||||
});
|
||||
Save();
|
||||
}
|
||||
|
||||
|
||||
@@ -177,7 +177,7 @@ public sealed class XivDataAnalyzer
|
||||
|
||||
public Task<long> GetTrianglesByHash(string hash)
|
||||
{
|
||||
if (_configService.Current.TriangleDictionary.TryGetValue(hash, out var cachedTris))
|
||||
if (_configService.Current.TriangleDictionary.TryGetValue(hash, out var cachedTris) && cachedTris > 0)
|
||||
return Task.FromResult(cachedTris);
|
||||
|
||||
var path = _fileCacheManager.GetFileCacheByHash(hash);
|
||||
@@ -192,13 +192,29 @@ public sealed class XivDataAnalyzer
|
||||
var file = _luminaGameData.GetFileFromDisk<MdlFile>(filePath);
|
||||
if (file.FileHeader.LodCount <= 0)
|
||||
return Task.FromResult((long)0);
|
||||
var meshIdx = file.Lods[0].MeshIndex;
|
||||
var meshCnt = file.Lods[0].MeshCount;
|
||||
var tris = file.Meshes.Skip(meshIdx).Take(meshCnt).Sum(p => p.IndexCount) / 3;
|
||||
long tris = 0;
|
||||
for (int i = 0; i < file.FileHeader.LodCount; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var meshIdx = file.Lods[i].MeshIndex;
|
||||
var meshCnt = file.Lods[i].MeshCount;
|
||||
tris = file.Meshes.Skip(meshIdx).Take(meshCnt).Sum(p => p.IndexCount) / 3;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Could not load lod mesh {mesh} from {path}", i, filePath);
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.LogDebug("{filePath} => {tris} triangles", filePath, tris);
|
||||
_configService.Current.TriangleDictionary[hash] = tris;
|
||||
_configService.Save();
|
||||
if (tris > 0)
|
||||
{
|
||||
_logger.LogDebug("{filePath} => {tris} triangles", filePath, tris);
|
||||
_configService.Current.TriangleDictionary[hash] = tris;
|
||||
_configService.Save();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Task.FromResult(tris);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
Reference in New Issue
Block a user