Merge PQ_Timer into base TimerMgr class

This commit is contained in:
Tim Wojtulewicz 2020-08-17 16:08:17 -07:00
parent 0edc331ca1
commit b77ede4bed
5 changed files with 39 additions and 58 deletions

View file

@ -193,8 +193,7 @@ void ProfileLogger::Log()
run_state::network_time, stats.matchers, stats.nfa_states, run_state::network_time, stats.matchers, stats.nfa_states,
stats.dfa_states, stats.computed, stats.mem / 1024)); stats.dfa_states, stats.computed, stats.mem / 1024));
} }
file->Write(util::fmt("%.06f Timers: current=%zu max=%zu lag=%.2fs\n", run_state::network_time,
file->Write(util::fmt("%.06f Timers: current=%d max=%d lag=%.2fs\n", run_state::network_time,
timer_mgr->Size(), timer_mgr->PeakSize(), timer_mgr->Size(), timer_mgr->PeakSize(),
run_state::network_time - timer_mgr->LastTimestamp())); run_state::network_time - timer_mgr->LastTimestamp()));

View file

@ -72,12 +72,12 @@ TimerMgr::TimerMgr()
num_expired = 0; num_expired = 0;
last_advance = last_timestamp = 0; last_advance = last_timestamp = 0;
q = std::make_unique<PriorityQueue>();
if ( iosource_mgr ) if ( iosource_mgr )
iosource_mgr->Register(this, true); iosource_mgr->Register(this, true);
} }
TimerMgr::~TimerMgr() { }
int TimerMgr::Advance(double arg_t, int max_expire) int TimerMgr::Advance(double arg_t, int max_expire)
{ {
DBG_LOG(DBG_TM, "advancing timer mgr to %.6f", arg_t); DBG_LOG(DBG_TM, "advancing timer mgr to %.6f", arg_t);
@ -113,17 +113,7 @@ void TimerMgr::InitPostScript()
iosource_mgr->Register(this, true); iosource_mgr->Register(this, true);
} }
PQ_TimerMgr::PQ_TimerMgr() : TimerMgr() void TimerMgr::Add(Timer* timer)
{
q = new PriorityQueue;
}
PQ_TimerMgr::~PQ_TimerMgr()
{
delete q;
}
void PQ_TimerMgr::Add(Timer* timer)
{ {
DBG_LOG(DBG_TM, "Adding timer %s (%p) at %.6f", timer_type_to_string(timer->Type()), timer, DBG_LOG(DBG_TM, "Adding timer %s (%p) at %.6f", timer_type_to_string(timer->Type()), timer,
timer->Time()); timer->Time());
@ -137,7 +127,7 @@ void PQ_TimerMgr::Add(Timer* timer)
++current_timers[timer->Type()]; ++current_timers[timer->Type()];
} }
void PQ_TimerMgr::Expire() void TimerMgr::Expire()
{ {
Timer* timer; Timer* timer;
while ( (timer = Remove()) ) while ( (timer = Remove()) )
@ -149,7 +139,7 @@ void PQ_TimerMgr::Expire()
} }
} }
int PQ_TimerMgr::DoAdvance(double new_t, int max_expire) int TimerMgr::DoAdvance(double new_t, int max_expire)
{ {
Timer* timer = Top(); Timer* timer = Top();
for ( num_expired = 0; (num_expired < max_expire) && timer && timer->Time() <= new_t; for ( num_expired = 0; (num_expired < max_expire) && timer && timer->Time() <= new_t;
@ -173,7 +163,7 @@ int PQ_TimerMgr::DoAdvance(double new_t, int max_expire)
return num_expired; return num_expired;
} }
void PQ_TimerMgr::Remove(Timer* timer) void TimerMgr::Remove(Timer* timer)
{ {
if ( ! q->Remove(timer) ) if ( ! q->Remove(timer) )
reporter->InternalError("asked to remove a missing timer"); reporter->InternalError("asked to remove a missing timer");
@ -182,7 +172,7 @@ void PQ_TimerMgr::Remove(Timer* timer)
delete timer; delete timer;
} }
double PQ_TimerMgr::GetNextTimeout() double TimerMgr::GetNextTimeout()
{ {
Timer* top = Top(); Timer* top = Top();
if ( top ) if ( top )
@ -191,4 +181,14 @@ double PQ_TimerMgr::GetNextTimeout()
return -1; return -1;
} }
Timer* TimerMgr::Remove()
{
return (Timer*)q->Remove();
}
Timer* TimerMgr::Top()
{
return (Timer*)q->Top();
}
} // namespace zeek::detail } // namespace zeek::detail

View file

@ -2,7 +2,8 @@
#pragma once #pragma once
#include <stdint.h> #include <cstdint>
#include <memory>
#include "zeek/PriorityQueue.h" #include "zeek/PriorityQueue.h"
#include "zeek/iosource/IOSource.h" #include "zeek/iosource/IOSource.h"
@ -75,12 +76,12 @@ protected:
TimerType type{}; TimerType type{};
}; };
class TimerMgr : public iosource::IOSource class TimerMgr final : public iosource::IOSource
{ {
public: public:
virtual ~TimerMgr(); TimerMgr();
virtual void Add(Timer* timer) = 0; void Add(Timer* timer);
/** /**
* Advance the clock to time t, expiring at most max_expire timers. * Advance the clock to time t, expiring at most max_expire timers.
@ -100,7 +101,7 @@ public:
/** /**
* Expire all timers. * Expire all timers.
*/ */
virtual void Expire() = 0; void Expire();
/** /**
* Removes a timer. Cancel() is a method separate from Remove() * Removes a timer. Cancel() is a method separate from Remove()
@ -115,9 +116,9 @@ public:
double Time() const { return t ? t : 1; } // 1 > 0 double Time() const { return t ? t : 1; } // 1 > 0
virtual int Size() const = 0; size_t Size() const { return q->Size(); }
virtual int PeakSize() const = 0; size_t PeakSize() const { return q->PeakSize(); }
virtual uint64_t CumulativeNum() const = 0; size_t CumulativeNum() const { return q->CumulativeNum(); }
double LastTimestamp() const { return last_timestamp; } double LastTimestamp() const { return last_timestamp; }
@ -129,7 +130,7 @@ public:
static unsigned int* CurrentTimers() { return current_timers; } static unsigned int* CurrentTimers() { return current_timers; }
// IOSource API methods // IOSource API methods
virtual double GetNextTimeout() override { return -1; } virtual double GetNextTimeout() override;
virtual void Process() override; virtual void Process() override;
virtual const char* Tag() override { return "TimerMgr"; } virtual const char* Tag() override { return "TimerMgr"; }
@ -139,11 +140,12 @@ public:
*/ */
void InitPostScript(); void InitPostScript();
protected: private:
TimerMgr(); int DoAdvance(double t, int max_expire);
void Remove(Timer* timer);
virtual int DoAdvance(double t, int max_expire) = 0; Timer* Remove();
virtual void Remove(Timer* timer) = 0; Timer* Top();
double t; double t;
double last_timestamp; double last_timestamp;
@ -151,31 +153,11 @@ protected:
int num_expired; int num_expired;
size_t peak_size = 0;
size_t cumulative_num = 0;
static unsigned int current_timers[NUM_TIMER_TYPES]; static unsigned int current_timers[NUM_TIMER_TYPES];
}; std::unique_ptr<PriorityQueue> q;
class PQ_TimerMgr : public TimerMgr
{
public:
PQ_TimerMgr();
~PQ_TimerMgr() override;
void Add(Timer* timer) override;
void Expire() override;
int Size() const override { return q->Size(); }
int PeakSize() const override { return q->PeakSize(); }
uint64_t CumulativeNum() const override { return q->CumulativeNum(); }
double GetNextTimeout() override;
protected:
int DoAdvance(double t, int max_expire) override;
void Remove(Timer* timer) override;
Timer* Remove() { return (Timer*)q->Remove(); }
Timer* Top() { return (Timer*)q->Top(); }
PriorityQueue* q;
}; };
extern TimerMgr* timer_mgr; extern TimerMgr* timer_mgr;

View file

@ -288,7 +288,7 @@ function get_timer_stats%(%): TimerStats
r->Assign(n++, static_cast<uint64_t>(zeek::detail::timer_mgr->Size())); r->Assign(n++, static_cast<uint64_t>(zeek::detail::timer_mgr->Size()));
r->Assign(n++, static_cast<uint64_t>(zeek::detail::timer_mgr->PeakSize())); r->Assign(n++, static_cast<uint64_t>(zeek::detail::timer_mgr->PeakSize()));
r->Assign(n++, zeek::detail::timer_mgr->CumulativeNum()); r->Assign(n++, static_cast<uint64_t>(zeek::detail::timer_mgr->CumulativeNum()));
return r; return r;
%} %}

View file

@ -641,7 +641,7 @@ SetupResult setup(int argc, char** argv, Options* zopts)
if ( r != SQLITE_OK ) if ( r != SQLITE_OK )
reporter->Error("Failed to initialize sqlite3: %s", sqlite3_errstr(r)); reporter->Error("Failed to initialize sqlite3: %s", sqlite3_errstr(r));
timer_mgr = new PQ_TimerMgr(); timer_mgr = new TimerMgr();
auto zeekygen_cfg = options.zeekygen_config_file.value_or(""); auto zeekygen_cfg = options.zeekygen_config_file.value_or("");
zeekygen_mgr = new zeekygen::detail::Manager(zeekygen_cfg, zeek_argv[0]); zeekygen_mgr = new zeekygen::detail::Manager(zeekygen_cfg, zeek_argv[0]);