From ad87a2ce339a6037d5385df2dbb9aebd05a0b11c Mon Sep 17 00:00:00 2001 From: Mykhailo Lohvynenko Date: Mon, 3 Aug 2026 10:22:40 +0300 Subject: [PATCH] common: monitoring: decrease memory usage of monitoring Signed-off-by: Mykhailo Lohvynenko Reviewed-by: Mykola Kobets Reviewed-by: Oleksandr Grytsov --- src/core/common/monitoring/alertprocessor.cpp | 80 ++++++++++--------- src/core/common/monitoring/alertprocessor.hpp | 12 +-- src/core/common/monitoring/monitoring.cpp | 61 ++------------ src/core/common/monitoring/monitoring.hpp | 1 - .../monitoring/tests/alertprocessor.cpp | 9 +-- 5 files changed, 60 insertions(+), 103 deletions(-) diff --git a/src/core/common/monitoring/alertprocessor.cpp b/src/core/common/monitoring/alertprocessor.cpp index d37021350..4f69e9871 100644 --- a/src/core/common/monitoring/alertprocessor.cpp +++ b/src/core/common/monitoring/alertprocessor.cpp @@ -16,55 +16,55 @@ namespace { * Static **********************************************************************************************************************/ -class CreateAlertVisitor : public StaticVisitor { +class CreateAlertVisitor : public StaticVisitor { public: - CreateAlertVisitor(uint64_t currentValue, const Time& currentTime, const QuotaAlertState& state) - : mCurrentVal(currentValue) + CreateAlertVisitor( + const ResourceIdentifier& id, uint64_t currentValue, const Time& currentTime, const QuotaAlertState& state) + : mID(id) + , mCurrentVal(currentValue) , mCurrentTime(currentTime) , mState(state) { } - Res Visit(const SystemQuotaAlert& val) const + Res Visit(SystemQuotaAlert& val) const { - auto systemQuotaAlert = val; - - systemQuotaAlert.mTimestamp = mCurrentTime; - systemQuotaAlert.mValue = mCurrentVal; - systemQuotaAlert.mState = mState; - - Res result; - result.SetValue(systemQuotaAlert); - - return result; + val.mNodeID = mID.mNodeID; + val.mParameter = GetParameterName(mID); + val.mTimestamp = mCurrentTime; + val.mValue = mCurrentVal; + val.mState = mState; } - Res Visit(const InstanceQuotaAlert& val) const + Res Visit(InstanceQuotaAlert& val) const { - auto instanceQuotaAlert = val; - - instanceQuotaAlert.mTimestamp = mCurrentTime; - instanceQuotaAlert.mValue = mCurrentVal; - instanceQuotaAlert.mState = mState; - - Res result; - result.SetValue(instanceQuotaAlert); - - return result; + val.mParameter = GetParameterName(mID); + static_cast(val) = mID.mInstanceIdent.GetValue(); + val.mTimestamp = mCurrentTime; + val.mValue = mCurrentVal; + val.mState = mState; } template Res Visit(const T&) const { assert(false); - - return {}; } private: - uint64_t mCurrentVal {}; - Time mCurrentTime; - QuotaAlertState mState; + String GetParameterName(const ResourceIdentifier& id) const + { + if (id.mPartitionName.HasValue()) { + return id.mPartitionName.GetValue(); // NOSONAR cpp:S5912 - String is used as a string view. + } + + return id.mType.ToString(); // NOSONAR cpp:S5912 - String is used as a string view. + } + + const ResourceIdentifier& mID; + uint64_t mCurrentVal {}; + Time mCurrentTime; + QuotaAlertState mState; }; } // namespace @@ -73,8 +73,7 @@ class CreateAlertVisitor : public StaticVisitor { * Public **********************************************************************************************************************/ -Error AlertProcessor::Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender, - const AlertVariant& alertTemplate) +Error AlertProcessor::Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender) { mID = id; mMinTimeout = rule.mMinTimeout; @@ -84,8 +83,7 @@ Error AlertProcessor::Init(const ResourceIdentifier& id, const AlertRulePoints& LOG_DBG() << "Create alert processor" << Log::Field("id", mID) << Log::Field("minThreshold", mMinThreshold) << Log::Field("maxThreshold", mMaxThreshold) << Log::Field("minTimeout", mMinTimeout); - mAlertSender = &sender; - mAlertTemplate = alertTemplate; + mAlertSender = &sender; return ErrorEnum::eNone; } @@ -187,9 +185,19 @@ Error AlertProcessor::HandleMinThreshold(uint64_t currentValue, const Time& curr Error AlertProcessor::SendAlert(uint64_t currentValue, const Time& currentTime, const QuotaAlertState& state) { - CreateAlertVisitor visitor(currentValue, currentTime, state); + AlertVariant alert; + + if (mID.mLevel == ResourceLevelEnum::eSystem) { + alert.SetValue(); + } else if (mID.mLevel == ResourceLevelEnum::eInstance) { + alert.SetValue(); + } else { + return Error(ErrorEnum::eInvalidArgument); + } + + const CreateAlertVisitor visitor(mID, currentValue, currentTime, state); - auto alert = mAlertTemplate.ApplyVisitor(visitor); + alert.ApplyVisitor(visitor); if (auto err = mAlertSender->SendAlert(alert); !err.IsNone()) { LOG_ERR() << "Failed to send alert" << Log::Field(err); diff --git a/src/core/common/monitoring/alertprocessor.hpp b/src/core/common/monitoring/alertprocessor.hpp index a4a006b96..2ac386353 100644 --- a/src/core/common/monitoring/alertprocessor.hpp +++ b/src/core/common/monitoring/alertprocessor.hpp @@ -83,16 +83,18 @@ struct ResourceIdentifier { * @param partitionName partition name. * @param instanceIdent instance identifier. */ - ResourceIdentifier(ResourceLevel level, ResourceType type, + ResourceIdentifier(const String& nodeId, ResourceLevel level, ResourceType type, const Optional>& partitionName = {}, const Optional& instanceIdent = {}) - : mLevel(level) + : mNodeID(nodeId) + , mLevel(level) , mType(type) , mPartitionName(partitionName) , mInstanceIdent(instanceIdent) { } + StaticString mNodeID; ResourceLevel mLevel; ResourceType mType; Optional> mPartitionName; @@ -108,7 +110,7 @@ struct ResourceIdentifier { */ friend Log& operator<<(Log& log, const ResourceIdentifier& identifier) { - log << "{" << identifier.mLevel << ":" << identifier.mType; + log << "{" << identifier.mNodeID << ":" << identifier.mLevel << ":" << identifier.mType; if (identifier.mPartitionName.HasValue()) { log << ":" << identifier.mPartitionName.GetValue(); @@ -138,8 +140,7 @@ class AlertProcessor { * @param alertTemplate alert template. * @return Error. */ - Error Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender, - const AlertVariant& alertTemplate); + Error Init(const ResourceIdentifier& id, const AlertRulePoints& rule, alerts::SenderItf& sender); /** * Checks alert detection. If alert condition is true, sends alert. @@ -164,7 +165,6 @@ class AlertProcessor { ResourceIdentifier mID {}; alerts::SenderItf* mAlertSender {}; - AlertVariant mAlertTemplate; Duration mMinTimeout {}; uint64_t mMinThreshold {}; diff --git a/src/core/common/monitoring/monitoring.cpp b/src/core/common/monitoring/monitoring.cpp index 75aae24a4..ad3826d0f 100644 --- a/src/core/common/monitoring/monitoring.cpp +++ b/src/core/common/monitoring/monitoring.cpp @@ -36,15 +36,6 @@ Optional ToPoints(const Optional& percents, return ToPoints(*percents, totalValue); } -String GetParameterName(const ResourceIdentifier& id) -{ - if (id.mPartitionName.HasValue()) { - return id.mPartitionName.GetValue(); - } - - return id.mType.ToString(); -} - RetWithError GetCurrentUsage(const ResourceIdentifier& id, const MonitoringData& monitoringData) { switch (id.mType.GetValue()) { @@ -432,39 +423,6 @@ void Monitoring::ProcessMonitoring() } } -Error Monitoring::CreateAlertTemplate(const ResourceIdentifier& resourceIdentifier, AlertVariant& alert) const -{ - switch (resourceIdentifier.mLevel.GetValue()) { - case ResourceLevelEnum::eSystem: { - SystemQuotaAlert quotaAlert {}; - - quotaAlert.mNodeID = mNodeInfo.mNodeID; - quotaAlert.mParameter = GetParameterName(resourceIdentifier); - - alert.SetValue(quotaAlert); - - return ErrorEnum::eNone; - } - - case ResourceLevelEnum::eInstance: { - if (!resourceIdentifier.mInstanceIdent.HasValue()) { - return AOS_ERROR_WRAP(ErrorEnum::eInvalidArgument); - } - - InstanceQuotaAlert quotaAlert {}; - - static_cast(quotaAlert) = *resourceIdentifier.mInstanceIdent; - quotaAlert.mParameter = GetParameterName(resourceIdentifier); - - alert.SetValue(quotaAlert); - - return ErrorEnum::eNone; - } - } - - return AOS_ERROR_WRAP(ErrorEnum::eNotSupported); -} - Error Monitoring::AddAlertProcessor( const AlertRulePoints& rule, const ResourceIdentifier& identifier, Array& processors) { @@ -474,13 +432,7 @@ Error Monitoring::AddAlertProcessor( auto& alertProcessor = processors.Back(); - AlertVariant alertTemplate; - - if (auto err = CreateAlertTemplate(identifier, alertTemplate); !err.IsNone()) { - return AOS_ERROR_WRAP(err); - } - - if (auto err = alertProcessor.Init(identifier, rule, *mAlertSender, alertTemplate); !err.IsNone()) { + if (auto err = alertProcessor.Init(identifier, rule, *mAlertSender); !err.IsNone()) { return AOS_ERROR_WRAP(err); } @@ -522,7 +474,7 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc const Optional& instanceIdent, Array& processors) { if (auto cpu = ToPoints(alertRules.mCPU, mNodeInfo.mMaxDMIPS); cpu.HasValue()) { - auto id = ResourceIdentifier(level, ResourceTypeEnum::eCPU, {}, instanceIdent); + auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eCPU, {}, instanceIdent); if (auto err = AddAlertProcessor(*cpu, id, processors); !err.IsNone()) { return AOS_ERROR_WRAP(err); @@ -530,7 +482,7 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc } if (auto ram = ToPoints(alertRules.mRAM, mNodeInfo.mTotalRAM); ram.HasValue()) { - auto id = ResourceIdentifier(level, ResourceTypeEnum::eRAM, {}, instanceIdent); + auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eRAM, {}, instanceIdent); if (auto err = AddAlertProcessor(*ram, id, processors); !err.IsNone()) { return AOS_ERROR_WRAP(err); @@ -538,7 +490,7 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc } if (alertRules.mDownload.HasValue()) { - auto id = ResourceIdentifier(level, ResourceTypeEnum::eDownload, {}, instanceIdent); + auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eDownload, {}, instanceIdent); if (auto err = AddAlertProcessor(*alertRules.mDownload, id, processors); !err.IsNone()) { return AOS_ERROR_WRAP(err); @@ -546,7 +498,7 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc } if (alertRules.mUpload.HasValue()) { - auto id = ResourceIdentifier(level, ResourceTypeEnum::eUpload, {}, instanceIdent); + auto id = ResourceIdentifier(mNodeInfo.mNodeID, level, ResourceTypeEnum::eUpload, {}, instanceIdent); if (auto err = AddAlertProcessor(*alertRules.mUpload, id, processors); !err.IsNone()) { return AOS_ERROR_WRAP(err); @@ -560,7 +512,8 @@ Error Monitoring::SetAlertProcessors(const AlertRules& alertRules, const Resourc continue; } - auto id = ResourceIdentifier(level, ResourceTypeEnum::ePartition, partition.mName, instanceIdent); + auto id = ResourceIdentifier( + mNodeInfo.mNodeID, level, ResourceTypeEnum::ePartition, partition.mName, instanceIdent); if (auto err = AddAlertProcessor(ToPoints(partition, it->mTotalSize), id, processors); !err.IsNone()) { return AOS_ERROR_WRAP(err); diff --git a/src/core/common/monitoring/monitoring.hpp b/src/core/common/monitoring/monitoring.hpp index e5d4a1b0b..aef811a21 100644 --- a/src/core/common/monitoring/monitoring.hpp +++ b/src/core/common/monitoring/monitoring.hpp @@ -92,7 +92,6 @@ class Monitoring : public MonitoringItf, void GetInstanceMonitoringData(Array& instanceMonitoringData); void ProcessAlerts(NodeMonitoringData& monitoringData); void ProcessAlerts(MonitoringData& monitoringData, AlertProcessorArray& alertProcessors); - Error CreateAlertTemplate(const ResourceIdentifier& resourceIdentifier, AlertVariant& alert) const; Error AddAlertProcessor( const AlertRulePoints& rule, const ResourceIdentifier& identifier, Array& processors); Error SetNodeAlertProcessors(const Optional& alertRules); diff --git a/src/core/common/monitoring/tests/alertprocessor.cpp b/src/core/common/monitoring/tests/alertprocessor.cpp index 5b2ae433b..2749a2442 100644 --- a/src/core/common/monitoring/tests/alertprocessor.cpp +++ b/src/core/common/monitoring/tests/alertprocessor.cpp @@ -83,15 +83,12 @@ TEST_F(AlertProcessorTest, CheckRulePointAlertDetection) { const ResourceType resourceType = ResourceTypeEnum::eDownload; const AlertRulePoints rulePoints = {Time::cSeconds, 90, 95}; - const ResourceIdentifier id = {ResourceLevelEnum::eSystem, resourceType.GetValue(), {}, {}}; + const String nodeID = "node-id"; + const ResourceIdentifier id = {nodeID, ResourceLevelEnum::eSystem, resourceType.GetValue(), {}}; AlertProcessor alertProcessor; - { - AlertVariant alertTemplate; - alertTemplate.SetValue(CreateSystemQuotaAlert("node-id", resourceType.ToString(), 0)); - ASSERT_TRUE(alertProcessor.Init(id, rulePoints, mAlertSender, alertTemplate).IsNone()); - } + ASSERT_TRUE(alertProcessor.Init(id, rulePoints, mAlertSender).IsNone()); Time currentTime = Time::Now();