EventRegistry/Func: Disable events when all bodies are disabled

This is just a small optimization on top of what is there.

Add state to Func for tracking if any enabled bodies exist which
allows us to propagate it up to the EventHandler::bool() operator.
In turn, when all bodies of an event's Func have been runtime disabled,
the event itself will not be invoked anymore.

Experiments have shown that this allows runtime toggling of new_event()
without performance impact when disabled. This could enable use-cases
where new_packet() handlers are enabled for a split second once in a
while to either dump or sample raw packet data at runtime.
This commit is contained in:
Arne Welzel 2022-12-08 12:55:47 +01:00
parent 92e4c11914
commit 21cc5f9132
6 changed files with 163 additions and 7 deletions

View file

@ -85,7 +85,14 @@ public:
};
const std::vector<Body>& GetBodies() const { return bodies; }
bool HasBodies() const { return bodies.size(); }
bool HasBodies() const { return ! bodies.empty(); }
/**
* Are there bodies and is any one of them enabled?
*
* @return true if bodies exist and at least one is enabled.
*/
bool HasEnabledBodies() const { return ! bodies.empty() && has_enabled_bodies; };
/**
* Calls a Zeek function.
@ -149,8 +156,11 @@ protected:
std::string name;
private:
// EventGroup updates Func::Body.disabled
// EventGroup updates Func::Body.disabled and has_enabled_bodies.
// This is friend/private with EventGroup here so that we do not
// expose accessors in the zeek:: public interface.
friend class EventGroup;
bool has_enabled_bodies = true;
};
namespace detail