1#include <xrpl/basics/contract.h>
2#include <xrpl/core/JobQueue.h>
3#include <xrpl/core/PerfLog.h>
16 , m_invalidJobData(
JobTypes::instance().getInvalid(), collector, logs)
17 , m_workers(*this, &perfLog,
"JobQueue", threadCount)
19 , m_collector(collector)
21 JLOG(
m_journal.
info()) <<
"Using " << threadCount <<
" threads";
38 XRPL_ASSERT(result.second ==
true,
"xrpl::JobQueue::JobQueue : jobs added");
60 XRPL_ASSERT(type !=
jtINVALID,
"xrpl::JobQueue::addRefCountedJob : valid input job type");
64 iter !=
m_jobData.
end(),
"xrpl::JobQueue::addRefCountedJob : job type found in jobs");
68 JLOG(
m_journal.
debug()) << __func__ <<
" : Adding job : " << name <<
" : " << type;
75 "xrpl::JobQueue::addRefCountedJob : threads available or job "
76 "requires no threads");
81 auto const& job = *result.first;
83 JobType const type(job.getType());
84 XRPL_ASSERT(type !=
jtINVALID,
"xrpl::JobQueue::addRefCountedJob : has valid job type");
85 XRPL_ASSERT(
m_jobSet.contains(job),
"xrpl::JobQueue::addRefCountedJob : job found");
90 if (data.waiting + data.running <
getJobLimit(type))
121 return (c ==
m_jobData.
end()) ? 0 : (c->second.waiting + c->second.running);
135 ret += x.second.waiting;
145 XRPL_ASSERT(iter !=
m_jobData.
end(),
"xrpl::JobQueue::makeLoadEvent : valid job type input");
157 LogicError(
"JobQueue::addLoadEvents() called after JobQueue stopped");
160 XRPL_ASSERT(iter !=
m_jobData.
end(),
"xrpl::JobQueue::addLoadEvents : valid job type input");
161 iter->second.load().addSamples(count, elapsed);
168 return entry.second.load().isOver();
175 using namespace std::chrono_literals;
186 XRPL_ASSERT(x.first !=
jtINVALID,
"xrpl::JobQueue::getJson : valid job type");
195 int const waiting(data.waiting);
196 int const running(data.running);
198 if ((stats.
count != 0) || (waiting != 0) || (stats.
latencyPeak != 0ms) || (running != 0))
202 pri[
"job_type"] = data.name();
205 pri[
"over_target"] =
true;
208 pri[
"waiting"] = waiting;
210 if (stats.
count != 0)
211 pri[
"per_second"] =
static_cast<int>(stats.
count);
220 pri[
"in_progress"] = running;
224 ret[
"job_types"] = priorities;
240 XRPL_ASSERT(c !=
m_jobData.
end(),
"xrpl::JobQueue::getJobTypeData : valid job type input");
254 using namespace std::chrono_literals;
264 XRPL_ASSERT(
m_processCount == 0,
"xrpl::JobQueue::stop : all processes completed");
265 XRPL_ASSERT(
m_jobSet.empty(),
"xrpl::JobQueue::stop : all jobs completed");
266 XRPL_ASSERT(
nSuspend_ == 0,
"xrpl::JobQueue::stop : no coros suspended");
280 XRPL_ASSERT(!
m_jobSet.empty(),
"xrpl::JobQueue::getNextJob : non-empty jobs");
285 JobType const type = iter->getType();
286 XRPL_ASSERT(type !=
jtINVALID,
"xrpl::JobQueue::getNextJob : valid job type");
290 data.running <=
getJobLimit(type),
"xrpl::JobQueue::getNextJob : maximum jobs running");
295 XRPL_ASSERT(data.waiting > 0,
"xrpl::JobQueue::getNextJob : positive data waiting");
302 XRPL_ASSERT(iter !=
m_jobSet.
end(),
"xrpl::JobQueue::getNextJob : found next job");
310 XRPL_ASSERT(type !=
jtINVALID,
"xrpl::JobQueue::finishJob : valid input job type");
315 if (data.deferred > 0)
319 "xrpl::JobQueue::finishJob : job limit");
348 auto const q_time = ceil<microseconds>(start_time - job.
queue_time());
356 if (x_time >= 10ms || q_time >= 10ms)
383 XRPL_ASSERT(j.
type() !=
jtINVALID,
"xrpl::JobQueue::getJobLimit : valid job type");
Value & append(Value const &value)
Append value to array at the end.
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
void notify(std::chrono::duration< Rep, Period > const &value) const
Push an event notification.
A reference to a handler for performing polled collection.
void join(char const *name, std::chrono::milliseconds wait, beast::Journal j)
Returns once all counted in-flight closures are destroyed.
int getJobCountGE(JobType t) const
All waiting jobs at or greater than this priority.
void processTask(int instance) override
Perform a task.
JobTypeData & getJobTypeData(JobType type)
int getJobCountTotal(JobType t) const
Jobs waiting plus running at this priority.
JobTypeData m_invalidJobData
beast::insight::Gauge job_count
void rendezvous()
Block until no jobs running.
JobQueue(int threadCount, beast::insight::Collector::ptr const &collector, beast::Journal journal, Logs &logs, perf::PerfLog &perfLog)
std::atomic_bool stopping_
int getJobCount(JobType t) const
Jobs waiting at this priority.
bool addRefCountedJob(JobType type, std::string const &name, JobFunction const &func)
std::atomic_bool stopped_
std::condition_variable cv_
static int getJobLimit(JobType type)
beast::insight::Hook hook
void addLoadEvents(JobType t, int count, std::chrono::milliseconds elapsed)
Add multiple load events.
std::unique_ptr< LoadEvent > makeLoadEvent(JobType t, std::string const &name)
Return a scoped LoadEvent.
beast::insight::Collector::ptr m_collector
void finishJob(JobType type)
Json::Value getJson(int c=0)
void getNextJob(Job &job)
Holds all the 'static' information about a job, which does not change.
static JobTypes const & instance()
clock_type::time_point const & queue_time() const
Returns the time when the job was queued.
Manages partitions for logging.
void addTask()
Add a task to be performed.
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
virtual void jobFinish(JobType const type, microseconds dur, int instance)=0
Log job finishing.
virtual void jobQueue(JobType const type)=0
Log queued job.
virtual void jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance)=0
Log job executing.
T forward_as_tuple(T... args)
@ arrayValue
array value (ordered list)
@ objectValue
object value (collection of name/value pairs).
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
beast::insight::Event dequeue
beast::insight::Event execute
std::chrono::milliseconds latencyPeak
std::chrono::milliseconds latencyAvg