StatsManager: Updated DoMaintenance() to unload not required minute stats from cached hourly stats data for objects older than 1 hour to free memory.

This commit is contained in:
Shreyas Zare
2022-02-20 17:54:39 +05:30
parent 042a88918f
commit e6b2615693

View File

@@ -1,6 +1,6 @@
/*
Technitium DNS Server
Copyright (C) 2021 Shreyas Zare (shreyas@technitium.com)
Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -58,7 +58,7 @@ namespace DnsServerCore.Dns
readonly Timer _maintenanceTimer;
const int MAINTENANCE_TIMER_INITIAL_INTERVAL = 10000;
const int MAINTENANCE_TIMER_INTERVAL = 10000;
const int MAINTENANCE_TIMER_PERIODIC_INTERVAL = 10000;
readonly BlockingCollection<StatsQueueItem> _queue = new BlockingCollection<StatsQueueItem>();
readonly Thread _consumerThread;
@@ -99,7 +99,7 @@ namespace DnsServerCore.Dns
if (log != null)
log.Write(ex);
}
}, null, MAINTENANCE_TIMER_INITIAL_INTERVAL, MAINTENANCE_TIMER_INTERVAL);
}, null, MAINTENANCE_TIMER_INITIAL_INTERVAL, MAINTENANCE_TIMER_PERIODIC_INTERVAL);
//stats consumer thread
_consumerThread = new Thread(delegate ()
@@ -350,6 +350,18 @@ namespace DnsServerCore.Dns
_hourlyStatsCache.TryRemove(key, out _);
}
//unload minute stats data from hourly stats cache for data older than last hour
{
DateTime lastHourThreshold = DateTime.UtcNow.AddHours(-1);
lastHourThreshold = new DateTime(lastHourThreshold.Year, lastHourThreshold.Month, lastHourThreshold.Day, lastHourThreshold.Hour, 0, 0, DateTimeKind.Utc);
foreach (KeyValuePair<DateTime, HourlyStats> item in _hourlyStatsCache)
{
if (item.Key < lastHourThreshold)
item.Value.UnloadMinuteStats();
}
}
//remove old data from daily stats cache
{
DateTime threshold = DateTime.UtcNow.AddMonths(-12);
@@ -1159,7 +1171,7 @@ namespace DnsServerCore.Dns
#region variables
readonly StatCounter _hourStat; //calculated value
readonly StatCounter[] _minuteStats = new StatCounter[60];
StatCounter[] _minuteStats = new StatCounter[60];
#endregion
@@ -1209,6 +1221,11 @@ namespace DnsServerCore.Dns
_minuteStats[dateTime.Minute] = minuteStat;
}
public void UnloadMinuteStats()
{
_minuteStats = null;
}
public void WriteTo(BinaryWriter bW)
{
bW.Write(Encoding.ASCII.GetBytes("HS")); //format