Skip to content

Commit 22cddab

Browse files
authored
[ML] Replace CResourceMonitor::m_AllocationFailures to reduce the memory footprint (#2527)
Fixes #2524
1 parent 5cdd02b commit 22cddab

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

include/model/CResourceMonitor.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ class MODEL_EXPORT CResourceMonitor {
179179
private:
180180
using TMonitoredResourcePtrSizeUMap =
181181
boost::unordered_map<CMonitoredResource*, std::size_t>;
182-
using TTimeSizeMap = std::map<core_t::TTime, std::size_t>;
183182
using TMeanVarAccumulator =
184183
maths::common::CBasicStatistics::SSampleMeanVar<double>::TAccumulator;
185184

@@ -258,8 +257,11 @@ class MODEL_EXPORT CResourceMonitor {
258257
//! Callback function to fire when memory usage increases by 1%
259258
TMemoryUsageReporterFunc m_MemoryUsageReporter;
260259

261-
//! Keep track of times of anomaly detector allocation failures
262-
TTimeSizeMap m_AllocationFailures;
260+
//! Keep track of the number of distinct allocation failures
261+
std::size_t m_AllocationFailuresCount{0};
262+
263+
//! The time at which the last allocation failure occurred
264+
core_t::TTime m_LastAllocationFailureTime{0};
263265

264266
//! The time at which the last allocation failure was reported
265267
core_t::TTime m_LastAllocationFailureReport{0};

lib/model/CResourceMonitor.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,8 @@ bool CResourceMonitor::needToSendReport(model_t::EAssignmentMemoryBasis currentA
318318
}
319319

320320
// Have we had new allocation failures
321-
if (!m_AllocationFailures.empty()) {
322-
core_t::TTime latestAllocationError{(--m_AllocationFailures.end())->first};
323-
if (latestAllocationError > m_LastAllocationFailureReport) {
321+
if (m_AllocationFailuresCount != 0) {
322+
if (m_LastAllocationFailureTime > m_LastAllocationFailureReport) {
324323
return true;
325324
}
326325
}
@@ -369,8 +368,8 @@ void CResourceMonitor::sendMemoryUsageReport(core_t::TTime bucketStartTime,
369368
total);
370369
if (m_MemoryUsageReporter) {
371370
m_MemoryUsageReporter(this->createMemoryUsageReport(bucketStartTime));
372-
if (!m_AllocationFailures.empty()) {
373-
m_LastAllocationFailureReport = m_AllocationFailures.rbegin()->first;
371+
if (m_AllocationFailuresCount != 0) {
372+
m_LastAllocationFailureReport = m_LastAllocationFailureTime;
374373
}
375374
}
376375
m_PreviousTotal = total;
@@ -395,7 +394,7 @@ CResourceMonitor::createMemoryUsageReport(core_t::TTime bucketStartTime) {
395394
for (const auto& resource : m_Resources) {
396395
resource.first->updateModelSizeStats(res);
397396
}
398-
res.s_AllocationFailures += m_AllocationFailures.size();
397+
res.s_AllocationFailures += m_AllocationFailuresCount;
399398
res.s_OverallCategorizerStats.s_MemoryCategorizationFailures += m_CategorizerAllocationFailures;
400399
return res;
401400
}
@@ -428,7 +427,10 @@ std::size_t CResourceMonitor::persistenceMemoryIncreaseFactor() const {
428427

429428
void CResourceMonitor::acceptAllocationFailureResult(core_t::TTime time) {
430429
m_MemoryStatus = model_t::E_MemoryStatusHardLimit;
431-
++m_AllocationFailures[time];
430+
if (time > m_LastAllocationFailureTime) {
431+
m_AllocationFailuresCount += 1;
432+
m_LastAllocationFailureTime = time;
433+
}
432434
}
433435

434436
void CResourceMonitor::startPruning() {

lib/model/unittest/CResourceMonitorTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ BOOST_FIXTURE_TEST_CASE(testMonitor, CTestFixture) {
341341
mon.acceptAllocationFailureResult(14401000);
342342
mon.acceptAllocationFailureResult(14402000);
343343

344-
BOOST_REQUIRE_EQUAL(3, mon.m_AllocationFailures.size());
344+
BOOST_REQUIRE_EQUAL(3, mon.m_AllocationFailuresCount);
345345
BOOST_REQUIRE_EQUAL(core_t::TTime(0), mon.m_LastAllocationFailureReport);
346346
mon.refresh(categorizer);
347347
mon.refresh(detector1);

0 commit comments

Comments
 (0)