mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-23 21:55:03 +02:00
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:
parent
957be31cd7
commit
805c78a7b9
@ -328,3 +328,12 @@ String Checkable::StateTypeToString(StateType type)
|
||||
return type == StateTypeSoft ? "SOFT" : "HARD";
|
||||
}
|
||||
|
||||
Dictionary::Ptr Checkable::GetFrozenLocalsForApply()
|
||||
{
|
||||
if (!m_FrozenLocalsForApply) {
|
||||
m_FrozenLocalsForApply = MakeLocalsForApply();
|
||||
m_FrozenLocalsForApply->Freeze();
|
||||
}
|
||||
|
||||
return m_FrozenLocalsForApply;
|
||||
}
|
||||
|
@ -216,12 +216,18 @@ public:
|
||||
static int GetPendingChecks();
|
||||
static void AquirePendingCheckSlot(int maxPendingChecks);
|
||||
|
||||
Dictionary::Ptr GetFrozenLocalsForApply();
|
||||
|
||||
protected:
|
||||
void Start(bool runtimeCreated) override;
|
||||
void OnConfigLoaded() override;
|
||||
void OnAllConfigLoaded() override;
|
||||
|
||||
virtual Dictionary::Ptr MakeLocalsForApply() = 0;
|
||||
|
||||
private:
|
||||
Dictionary::Ptr m_FrozenLocalsForApply;
|
||||
|
||||
mutable std::mutex m_CheckableMutex;
|
||||
bool m_CheckRunning{false};
|
||||
long m_SchedulingOffset;
|
||||
|
@ -68,16 +68,20 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
|
||||
|
||||
CONTEXT("Evaluating 'apply' rule (" << di << ")");
|
||||
|
||||
Host::Ptr host;
|
||||
Service::Ptr service;
|
||||
tie(host, service) = GetHostService(checkable);
|
||||
ScriptFrame frame (false);
|
||||
|
||||
ScriptFrame frame(true);
|
||||
if (rule.GetScope())
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
frame.Locals->Set("host", host);
|
||||
if (service)
|
||||
frame.Locals->Set("service", service);
|
||||
if (rule.GetScope() || rule.GetFTerm()) {
|
||||
frame.Locals = new Dictionary();
|
||||
|
||||
if (rule.GetScope()) {
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
}
|
||||
|
||||
checkable->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
|
||||
frame.Locals->Freeze();
|
||||
} else {
|
||||
frame.Locals = checkable->GetFrozenLocalsForApply();
|
||||
}
|
||||
|
||||
bool match = false;
|
||||
|
||||
@ -101,7 +105,7 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
|
||||
for (const Value& instance : arr) {
|
||||
String name = rule.GetName();
|
||||
|
||||
frame.Locals->Set(rule.GetFKVar(), instance);
|
||||
frame.Locals->Set(rule.GetFKVar(), instance, true);
|
||||
name += instance;
|
||||
|
||||
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
|
||||
@ -115,8 +119,8 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
|
||||
ObjectLock olock (dict);
|
||||
|
||||
for (auto& kv : dict) {
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second);
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first, true);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second, true);
|
||||
|
||||
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter))
|
||||
match = true;
|
||||
|
@ -307,3 +307,8 @@ bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *res
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Dictionary::Ptr Host::MakeLocalsForApply()
|
||||
{
|
||||
return new Dictionary({{ "host", this }});
|
||||
}
|
||||
|
@ -54,6 +54,8 @@ protected:
|
||||
|
||||
void CreateChildObjects(const Type::Ptr& childType) override;
|
||||
|
||||
Dictionary::Ptr MakeLocalsForApply() override;
|
||||
|
||||
private:
|
||||
mutable std::mutex m_ServicesMutex;
|
||||
std::map<String, intrusive_ptr<Service> > m_Services;
|
||||
|
@ -67,16 +67,20 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
|
||||
|
||||
CONTEXT("Evaluating 'apply' rule (" << di << ")");
|
||||
|
||||
Host::Ptr host;
|
||||
Service::Ptr service;
|
||||
tie(host, service) = GetHostService(checkable);
|
||||
ScriptFrame frame (false);
|
||||
|
||||
ScriptFrame frame(true);
|
||||
if (rule.GetScope())
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
frame.Locals->Set("host", host);
|
||||
if (service)
|
||||
frame.Locals->Set("service", service);
|
||||
if (rule.GetScope() || rule.GetFTerm()) {
|
||||
frame.Locals = new Dictionary();
|
||||
|
||||
if (rule.GetScope()) {
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
}
|
||||
|
||||
checkable->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
|
||||
frame.Locals->Freeze();
|
||||
} else {
|
||||
frame.Locals = checkable->GetFrozenLocalsForApply();
|
||||
}
|
||||
|
||||
bool match = false;
|
||||
|
||||
@ -100,7 +104,7 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
|
||||
for (const Value& instance : arr) {
|
||||
String name = rule.GetName();
|
||||
|
||||
frame.Locals->Set(rule.GetFKVar(), instance);
|
||||
frame.Locals->Set(rule.GetFKVar(), instance, true);
|
||||
name += instance;
|
||||
|
||||
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
|
||||
@ -114,8 +118,8 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
|
||||
ObjectLock olock (dict);
|
||||
|
||||
for (auto& kv : dict) {
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second);
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first, true);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second, true);
|
||||
|
||||
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter))
|
||||
match = true;
|
||||
|
@ -66,16 +66,20 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
|
||||
|
||||
CONTEXT("Evaluating 'apply' rule (" << di << ")");
|
||||
|
||||
Host::Ptr host;
|
||||
Service::Ptr service;
|
||||
tie(host, service) = GetHostService(checkable);
|
||||
ScriptFrame frame (false);
|
||||
|
||||
ScriptFrame frame(true);
|
||||
if (rule.GetScope())
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
frame.Locals->Set("host", host);
|
||||
if (service)
|
||||
frame.Locals->Set("service", service);
|
||||
if (rule.GetScope() || rule.GetFTerm()) {
|
||||
frame.Locals = new Dictionary();
|
||||
|
||||
if (rule.GetScope()) {
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
}
|
||||
|
||||
checkable->GetFrozenLocalsForApply()->CopyTo(frame.Locals);
|
||||
frame.Locals->Freeze();
|
||||
} else {
|
||||
frame.Locals = checkable->GetFrozenLocalsForApply();
|
||||
}
|
||||
|
||||
bool match = false;
|
||||
|
||||
@ -99,7 +103,7 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
|
||||
for (const Value& instance : arr) {
|
||||
String name = rule.GetName();
|
||||
|
||||
frame.Locals->Set(rule.GetFKVar(), instance);
|
||||
frame.Locals->Set(rule.GetFKVar(), instance, true);
|
||||
name += instance;
|
||||
|
||||
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
|
||||
@ -113,8 +117,8 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
|
||||
ObjectLock olock (dict);
|
||||
|
||||
for (auto& kv : dict) {
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second);
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first, true);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second, true);
|
||||
|
||||
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule, skipFilter))
|
||||
match = true;
|
||||
|
@ -61,10 +61,20 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo
|
||||
|
||||
CONTEXT("Evaluating 'apply' rule (" << di << ")");
|
||||
|
||||
ScriptFrame frame(true);
|
||||
if (rule.GetScope())
|
||||
rule.GetScope()->CopyTo(frame.Locals);
|
||||
frame.Locals->Set("host", host);
|
||||
ScriptFrame frame (false);
|
||||
|
||||
if (rule.GetScope() || rule.GetFTerm()) {
|
||||
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;
|
||||
|
||||
@ -89,7 +99,7 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo
|
||||
String name = rule.GetName();
|
||||
|
||||
if (!rule.GetFKVar().IsEmpty()) {
|
||||
frame.Locals->Set(rule.GetFKVar(), instance);
|
||||
frame.Locals->Set(rule.GetFKVar(), instance, true);
|
||||
name += instance;
|
||||
}
|
||||
|
||||
@ -104,8 +114,8 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bo
|
||||
ObjectLock olock (dict);
|
||||
|
||||
for (auto& kv : dict) {
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second);
|
||||
frame.Locals->Set(rule.GetFKVar(), kv.first, true);
|
||||
frame.Locals->Set(rule.GetFVVar(), kv.second, true);
|
||||
|
||||
if (EvaluateApplyRuleInstance(host, rule.GetName() + kv.first, frame, rule, skipFilter))
|
||||
match = true;
|
||||
|
@ -260,3 +260,11 @@ std::pair<Host::Ptr, Service::Ptr> icinga::GetHostService(const Checkable::Ptr&
|
||||
else
|
||||
return std::make_pair(static_pointer_cast<Host>(checkable), nullptr);
|
||||
}
|
||||
|
||||
Dictionary::Ptr Service::MakeLocalsForApply()
|
||||
{
|
||||
return new Dictionary({
|
||||
{ "host", m_Host },
|
||||
{ "service", this }
|
||||
});
|
||||
}
|
||||
|
@ -48,6 +48,8 @@ public:
|
||||
protected:
|
||||
void CreateChildObjects(const Type::Ptr& childType) override;
|
||||
|
||||
Dictionary::Ptr MakeLocalsForApply() override;
|
||||
|
||||
private:
|
||||
Host::Ptr m_Host;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user