Cache locals of script frame used for assign where eval

to avoid malloc(). This BREAKS assign where locals.x = 1.
This commit is contained in:
Alexander A. Klimov 2022-11-08 16:12:42 +01:00
parent 957be31cd7
commit 805c78a7b9
10 changed files with 97 additions and 43 deletions

View File

@ -328,3 +328,12 @@ String Checkable::StateTypeToString(StateType type)
return type == StateTypeSoft ? "SOFT" : "HARD"; return type == StateTypeSoft ? "SOFT" : "HARD";
} }
Dictionary::Ptr Checkable::GetFrozenLocalsForApply()
{
if (!m_FrozenLocalsForApply) {
m_FrozenLocalsForApply = MakeLocalsForApply();
m_FrozenLocalsForApply->Freeze();
}
return m_FrozenLocalsForApply;
}

View File

@ -216,12 +216,18 @@ public:
static int GetPendingChecks(); static int GetPendingChecks();
static void AquirePendingCheckSlot(int maxPendingChecks); static void AquirePendingCheckSlot(int maxPendingChecks);
Dictionary::Ptr GetFrozenLocalsForApply();
protected: protected:
void Start(bool runtimeCreated) override; void Start(bool runtimeCreated) override;
void OnConfigLoaded() override; void OnConfigLoaded() override;
void OnAllConfigLoaded() override; void OnAllConfigLoaded() override;
virtual Dictionary::Ptr MakeLocalsForApply() = 0;
private: private:
Dictionary::Ptr m_FrozenLocalsForApply;
mutable std::mutex m_CheckableMutex; mutable std::mutex m_CheckableMutex;
bool m_CheckRunning{false}; bool m_CheckRunning{false};
long m_SchedulingOffset; long m_SchedulingOffset;

View File

@ -68,16 +68,20 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
CONTEXT("Evaluating 'apply' rule (" << di << ")"); CONTEXT("Evaluating 'apply' rule (" << di << ")");
Host::Ptr host; ScriptFrame frame (false);
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
ScriptFrame frame(true); if (rule.GetScope() || rule.GetFTerm()) {
if (rule.GetScope()) frame.Locals = new Dictionary();
rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("host", host); if (rule.GetScope()) {
if (service) rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("service", service); }
checkable->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
frame.Locals->Freeze();
} else {
frame.Locals = checkable->GetFrozenLocalsForApply();
}
bool match = false; bool match = false;
@ -101,7 +105,7 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
for (const Value& instance : arr) { for (const Value& instance : arr) {
String name = rule.GetName(); String name = rule.GetName();
frame.Locals->Set(rule.GetFKVar(), instance); frame.Locals->Set(rule.GetFKVar(), instance, true);
name += instance; name += instance;
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
@ -115,8 +119,8 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
ObjectLock olock (dict); ObjectLock olock (dict);
for (auto& kv : dict) { for (auto& kv : dict) {
frame.Locals->Set(rule.GetFKVar(), kv.first); frame.Locals->Set(rule.GetFKVar(), kv.first, true);
frame.Locals->Set(rule.GetFVVar(), kv.second); frame.Locals->Set(rule.GetFVVar(), kv.second, true);
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter))
match = true; match = true;

View File

@ -307,3 +307,8 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
return false; return false;
} }
Dictionary::Ptr Host::MakeLocalsForApply()
{
return new Dictionary({{ "host", this }});
}

View File

@ -54,6 +54,8 @@ protected:
void CreateChildObjects(const Type::Ptr& childType) override; void CreateChildObjects(const Type::Ptr& childType) override;
Dictionary::Ptr MakeLocalsForApply() override;
private: private:
mutable std::mutex m_ServicesMutex; mutable std::mutex m_ServicesMutex;
std::map<String, intrusive_ptr<Service> > m_Services; std::map<String, intrusive_ptr<Service> > m_Services;

View File

@ -67,16 +67,20 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
CONTEXT("Evaluating 'apply' rule (" << di << ")"); CONTEXT("Evaluating 'apply' rule (" << di << ")");
Host::Ptr host; ScriptFrame frame (false);
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
ScriptFrame frame(true); if (rule.GetScope() || rule.GetFTerm()) {
if (rule.GetScope()) frame.Locals = new Dictionary();
rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("host", host); if (rule.GetScope()) {
if (service) rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("service", service); }
checkable->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
frame.Locals->Freeze();
} else {
frame.Locals = checkable->GetFrozenLocalsForApply();
}
bool match = false; bool match = false;
@ -100,7 +104,7 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
for (const Value& instance : arr) { for (const Value& instance : arr) {
String name = rule.GetName(); String name = rule.GetName();
frame.Locals->Set(rule.GetFKVar(), instance); frame.Locals->Set(rule.GetFKVar(), instance, true);
name += instance; name += instance;
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
@ -114,8 +118,8 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
ObjectLock olock (dict); ObjectLock olock (dict);
for (auto& kv : dict) { for (auto& kv : dict) {
frame.Locals->Set(rule.GetFKVar(), kv.first); frame.Locals->Set(rule.GetFKVar(), kv.first, true);
frame.Locals->Set(rule.GetFVVar(), kv.second); frame.Locals->Set(rule.GetFVVar(), kv.second, true);
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter))
match = true; match = true;

View File

@ -66,16 +66,20 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
CONTEXT("Evaluating 'apply' rule (" << di << ")"); CONTEXT("Evaluating 'apply' rule (" << di << ")");
Host::Ptr host; ScriptFrame frame (false);
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
ScriptFrame frame(true); if (rule.GetScope() || rule.GetFTerm()) {
if (rule.GetScope()) frame.Locals = new Dictionary();
rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("host", host); if (rule.GetScope()) {
if (service) rule.GetScope()->CopyTo(frame.Locals);
frame.Locals->Set("service", service); }
checkable->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
frame.Locals->Freeze();
} else {
frame.Locals = checkable->GetFrozenLocalsForApply();
}
bool match = false; bool match = false;
@ -99,7 +103,7 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
for (const Value& instance : arr) { for (const Value& instance : arr) {
String name = rule.GetName(); String name = rule.GetName();
frame.Locals->Set(rule.GetFKVar(), instance); frame.Locals->Set(rule.GetFKVar(), instance, true);
name += instance; name += instance;
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
@ -113,8 +117,8 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
ObjectLock olock (dict); ObjectLock olock (dict);
for (auto& kv : dict) { for (auto& kv : dict) {
frame.Locals->Set(rule.GetFKVar(), kv.first); frame.Locals->Set(rule.GetFKVar(), kv.first, true);
frame.Locals->Set(rule.GetFVVar(), kv.second); frame.Locals->Set(rule.GetFVVar(), kv.second, true);
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter))
match = true; match = true;

View File

@ -61,10 +61,20 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo
CONTEXT("Evaluating 'apply' rule (" << di << ")"); CONTEXT("Evaluating 'apply' rule (" << di << ")");
ScriptFrame frame(true); ScriptFrame frame (false);
if (rule.GetScope())
rule.GetScope()->CopyTo(frame.Locals); if (rule.GetScope() || rule.GetFTerm()) {
frame.Locals->Set("host", host); frame.Locals = new Dictionary();
if (rule.GetScope()) {
rule.GetScope()->CopyTo(frame.Locals);
}
host->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
frame.Locals->Freeze();
} else {
frame.Locals = host->GetFrozenLocalsForApply();
}
bool match = false; bool match = false;
@ -89,7 +99,7 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo
String name = rule.GetName(); String name = rule.GetName();
if (!rule.GetFKVar().IsEmpty()) { if (!rule.GetFKVar().IsEmpty()) {
frame.Locals->Set(rule.GetFKVar(), instance); frame.Locals->Set(rule.GetFKVar(), instance, true);
name += instance; name += instance;
} }
@ -104,8 +114,8 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo
ObjectLock olock (dict); ObjectLock olock (dict);
for (auto& kv : dict) { for (auto& kv : dict) {
frame.Locals->Set(rule.GetFKVar(), kv.first); frame.Locals->Set(rule.GetFKVar(), kv.first, true);
frame.Locals->Set(rule.GetFVVar(), kv.second); frame.Locals->Set(rule.GetFVVar(), kv.second, true);
if (EvaluateApplyRuleInstance(host, rule.GetName() + kv.first, frame, rule, skipFilter)) if (EvaluateApplyRuleInstance(host, rule.GetName() + kv.first, frame, rule, skipFilter))
match = true; match = true;

View File

@ -260,3 +260,11 @@ std::pair<Host::Ptr, Service::Ptr> icinga::GetHostService(const Checkable::Ptr&
else else
return std::make_pair(static_pointer_cast<Host>(checkable), nullptr); return std::make_pair(static_pointer_cast<Host>(checkable), nullptr);
} }
Dictionary::Ptr Service::MakeLocalsForApply()
{
return new Dictionary({
{ "host", m_Host },
{ "service", this }
});
}

View File

@ -48,6 +48,8 @@ public:
protected: protected:
void CreateChildObjects(const Type::Ptr& childType) override; void CreateChildObjects(const Type::Ptr& childType) override;
Dictionary::Ptr MakeLocalsForApply() override;
private: private:
Host::Ptr m_Host; Host::Ptr m_Host;