From 629ba9ea05613a9eb278461b3a73eeec4b066655 Mon Sep 17 00:00:00 2001 From: Lucca Faria Ferri Date: Sun, 22 Jan 2023 10:59:36 -0800 Subject: [PATCH] Simple background monitor for tracking memory, CPU usage, and server status --- src/DiIiS-NA/Extensions.cs | 92 ++++++++++++++++++++++++++++++++++++++ src/DiIiS-NA/Program.cs | 43 ++++++++++++++++-- 2 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/DiIiS-NA/Extensions.cs diff --git a/src/DiIiS-NA/Extensions.cs b/src/DiIiS-NA/Extensions.cs new file mode 100644 index 0000000..bcc26c7 --- /dev/null +++ b/src/DiIiS-NA/Extensions.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Configuration; + +namespace DiIiS_NA +{ + public static class Extensions + { + /// + /// Transforms a timespan to a readable text. + /// E.g.: + /// 1 day, 2 hours, 3 minutes and 4 seconds + /// 5 hours, 6 minutes and 7 seconds + /// + /// If over certain threshold (millennium or more) it will only return the number of days. + /// + /// The timespan to be converted + /// The readable text + public static string ToText(this TimeSpan span) + { + List parts = new(); + + // if days are divided by 365, we have years, otherwise we have months or days + if (span.Days / 365 > 0) + { + // if days are divided by 365, we have years + parts.Add($"{((double)span.Days / 365):F} year{(span.Days / 365 > 1 ? "s" : "")}"); + // get months from the remaining days + int months = span.Days % 365 / 30; + if (months > 0) + parts.Add($"{months} month{(months > 1 ? "s" : "")}"); + + // get remaining days + int days = span.Days % 365 % 30; + if (days > 0) + parts.Add($"{days} day{(days > 1 ? "s" : "")}"); + } + else if (span.Days / 30 > 0) + { + // if days are divided by 30, we have months + parts.Add($"{((double)span.Days / 30):F} month{(span.Days / 30 > 1 ? "s" : "")}"); + // get remaining days + int days = span.Days % 30; + if (days > 0) + parts.Add($"{days} day{(days > 1 ? "s" : "")}"); + } + else if (span.Days > 0) + // if days are not divided by 365 or 30, we have days + parts.Add( $"{span.Days} day{(span.Days > 1 ? "s" : "")}"); + if (span.Hours > 0) + parts.Add($"{span.Hours} hour{(span.Hours > 1 ? "s" : "")}"); + if (span.Minutes > 0) + parts.Add($"{span.Minutes} minute{(span.Minutes > 1 ? "s" : "")}"); + if (span.Seconds > 0) + parts.Add($"{span.Seconds} second{(span.Seconds > 1 ? "s" : "")}"); + + var result = parts.ToArray(); + return string.Join(", ", result[..^1]) + (result.Length > 1 ? " and " : "") + parts[^1]; + } + + public static string ToSmallText(this TimeSpan span) + { + List parts = new(); + if (span.Days / 365 > 0) + { + parts.Add($"{((double)span.Days / 365):F}y"); + int months = span.Days % 365 / 30; + if (months > 0) + parts.Add($"{months}m"); + int days = span.Days % 365 % 30; + if (days > 0) + parts.Add($"{days}d"); + } + else if (span.Days / 30 > 0) + { + parts.Add($"{((double)span.Days / 30):F}m"); + int days = span.Days % 30; + if (days > 0) + parts.Add($"{days}d"); + } + else if (span.Days > 0) + parts.Add($"{span.Days}d"); + if (span.Hours > 0) + parts.Add($"{span.Hours}h"); + if (span.Minutes > 0) + parts.Add($"{span.Minutes}m"); + if (span.Seconds > 0) + parts.Add($"{span.Seconds}s"); + return string.Join(" ", parts); + } + } +} \ No newline at end of file diff --git a/src/DiIiS-NA/Program.cs b/src/DiIiS-NA/Program.cs index 588f660..67c11da 100644 --- a/src/DiIiS-NA/Program.cs +++ b/src/DiIiS-NA/Program.cs @@ -47,6 +47,7 @@ using Npgsql; using System; //Blizzless Project 2022 using System.Data.Common; +using System.Diagnostics; //Blizzless Project 2022 using System.Globalization; //Blizzless Project 2022 @@ -99,14 +100,50 @@ namespace DiIiS_NA AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - string Name = $"Blizzless: Build {Build}, Stage: {Stage} - {TypeBuild}"; - Console.WriteLine(" " + Name); + string name = $"Blizzless: Build {Build}, Stage: {Stage} - {TypeBuild}"; + Console.WriteLine(" " + name); Console.WriteLine("Connected Module: 0x00 - Diablo 3 RoS 2.7.4.84161"); Console.WriteLine("------------------------------------------------------------------------"); Console.ResetColor(); - Console.Title = Name; + Console.Title = name; InitLoggers(); + +#pragma warning disable CS4014 + Task.Run(async () => +#pragma warning restore CS4014 + { + Logger.Info("Started Background Monitor!"); + while (true) + { + try + { + var uptime = (DateTime.Now - StartupTime).ToText(); + // get total memory from process + var totalMemory = + (double)((double)Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024 / 1024); + // get CPU time + using var proc = Process.GetCurrentProcess(); + var cpuTime = proc.TotalProcessorTime; + var text = + $"{name} | {PlayerManager.OnlinePlayers.Count()} onlines in {PlayerManager.OnlinePlayers.Count(s => s.InGameClient.Player.World != null)} worlds | Memory: {totalMemory:0.000} GB | CPU Time: {cpuTime.ToSmallText()} | Uptime: {uptime}"; + try + { + Console.Title = text; + await Task.Delay(1000); + } + catch (Exception e) + { + Logger.Info(text); + await Task.Delay(60000); + } + } + catch (Exception e) + { + } + } + }); + AchievementManager.Initialize(); Core.Storage.AccountDataBase.SessionProvider.RebuildSchema(); #if DEBUG