From a7edf142c7e855ed8030d0cde0d31c38a6ac9d38 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 10 Dec 2020 01:25:41 -0800 Subject: [PATCH] ircd::m::vm::eval: Minor interface cleanup; unit reorg. --- include/ircd/m/vm/eval.h | 22 +-- include/ircd/m/vm/vm.h | 7 + matrix/vm.cc | 36 ----- matrix/vm_eval.cc | 326 ++++++++++++++++++++++----------------- 4 files changed, 202 insertions(+), 189 deletions(-) diff --git a/include/ircd/m/vm/eval.h b/include/ircd/m/vm/eval.h index 7114097a0..b0fa3d62b 100644 --- a/include/ircd/m/vm/eval.h +++ b/include/ircd/m/vm/eval.h @@ -15,13 +15,13 @@ namespace ircd::m::vm { struct eval; + const event *find_pdu(const eval &, const event::id &) noexcept; + eval *find_parent(const eval &, const ctx::ctx & = ctx::cur()) noexcept; + eval *find_root(const eval &, const ctx::ctx & = ctx::cur()) noexcept; + string_view loghead(const mutable_buffer &, const eval &); string_view loghead(const eval &); // single tls buffer - fault execute(eval &, const vector_view &); - fault execute(eval &, const json::array &); - - fault inject(eval &, json::iov &, const json::iov &); } /// Event Evaluation Device @@ -84,19 +84,21 @@ struct ircd::m::vm::eval eval &operator=(const eval &) = delete; ~eval() noexcept; - static bool for_each(const ctx::ctx *const &, const std::function &); - static eval *find_parent(const eval &, const ctx::ctx & = ctx::cur()); - static eval *find_root(const eval &, const ctx::ctx & = ctx::cur()); - static size_t count(const ctx::ctx *const &); - + // Tools for all evals static bool for_each(const std::function &); static bool for_each_pdu(const std::function &); - static const event *find_pdu(const eval &, const event::id &); + + // Tools for all evals sharing this ircd::context + static bool for_each(const ctx::ctx *const &, const std::function &); + static size_t count(const ctx::ctx *const &); + + // Event snoop interface static const event *find_pdu(const event::id &); static size_t count(const event::id &); static eval *find(const event::id &); static eval &get(const event::id &); + // Sequence related interface static bool sequnique(const uint64_t &seq); static eval *seqnext(const uint64_t &seq); static eval *seqmax(); diff --git a/include/ircd/m/vm/vm.h b/include/ircd/m/vm/vm.h index 3e501b7a9..1865ac252 100644 --- a/include/ircd/m/vm/vm.h +++ b/include/ircd/m/vm/vm.h @@ -29,6 +29,13 @@ namespace ircd::m::vm #include "eval.h" #include "seq.h" +namespace ircd::m::vm +{ + fault execute(eval &, const vector_view &); + fault execute(eval &, const json::array &); + fault inject(eval &, json::iov &, const json::iov &); +} + struct ircd::m::vm::init { init(), ~init() noexcept; diff --git a/matrix/vm.cc b/matrix/vm.cc index db6ce8ae6..f99c30dac 100644 --- a/matrix/vm.cc +++ b/matrix/vm.cc @@ -109,42 +109,6 @@ noexcept assert(retired == sequence::retired || ircd::read_only); } -// -// m/vm.h -// - -ircd::string_view -ircd::m::vm::loghead(const eval &eval) -{ - thread_local char buf[128]; - return loghead(buf, eval); -} - -ircd::string_view -ircd::m::vm::loghead(const mutable_buffer &buf, - const eval &eval) -{ - return fmt::sprintf - { - buf, "vm:%lu:%lu:%lu parent:%lu %s eval:%lu %s seq:%lu %s", - sequence::retired, - sequence::committed, - sequence::uncommitted, - eval.parent? - eval.parent->id: - 0UL, - eval.parent? - reflect(eval.parent->phase): - reflect(phase::NONE), - eval.id, - reflect(eval.phase), - sequence::get(eval), - eval.event_? - string_view{eval.event_->event_id}: - ""_sv, - }; -} - ircd::http::code ircd::m::vm::http_code(const fault &code) { diff --git a/matrix/vm_eval.cc b/matrix/vm_eval.cc index 255558ec3..6b14e8c8c 100644 --- a/matrix/vm_eval.cc +++ b/matrix/vm_eval.cc @@ -37,6 +37,172 @@ ircd::m::vm::eval::executing; decltype(ircd::m::vm::eval::injecting) ircd::m::vm::eval::injecting; +ircd::string_view +ircd::m::vm::loghead(const eval &eval) +{ + thread_local char buf[128]; + return loghead(buf, eval); +} + +ircd::string_view +ircd::m::vm::loghead(const mutable_buffer &buf, + const eval &eval) +{ + return fmt::sprintf + { + buf, "vm:%lu:%lu:%lu parent:%lu %s eval:%lu %s seq:%lu %s", + sequence::retired, + sequence::committed, + sequence::uncommitted, + eval.parent? + eval.parent->id: + 0UL, + eval.parent? + reflect(eval.parent->phase): + reflect(phase::NONE), + eval.id, + reflect(eval.phase), + sequence::get(eval), + eval.event_? + string_view{eval.event_->event_id}: + ""_sv, + }; +} + +ircd::m::vm::eval * +ircd::m::vm::find_root(const eval &a, + const ctx::ctx &c) +noexcept +{ + eval *ret {nullptr}, *parent {nullptr}; do + { + if(!(parent = find_parent(a, c))) + return ret; + + ret = parent; + } + while(1); +} + +ircd::m::vm::eval * +ircd::m::vm::find_parent(const eval &a, + const ctx::ctx &c) +noexcept +{ + eval *ret {nullptr}; + eval::for_each(&c, [&ret, &a] + (eval &eval) + { + const bool cond + { + (&eval != &a) && (!ret || eval.id > ret->id) + }; + + ret = cond? &eval : ret; + return true; + }); + + return ret; +} + +const ircd::m::event * +ircd::m::vm::find_pdu(const eval &eval, + const event::id &event_id) +noexcept +{ + const m::event *ret{nullptr}; + for(const auto &event : eval.pdus) + { + if(event.event_id != event_id) + continue; + + ret = std::addressof(event); + break; + } + + return ret; +} + +// +// eval::eval +// + +ircd::m::vm::eval::eval(const vm::opts &opts) +:opts{&opts} +,parent +{ + find_parent(*this) +} +{ + if(parent) + { + assert(!parent->child); + parent->child = this; + } +} + +ircd::m::vm::eval::eval(const vm::copts &opts) +:opts{&opts} +,copts{&opts} +,parent +{ + find_parent(*this) +} +{ + if(parent) + { + assert(!parent->child); + parent->child = this; + } +} + +ircd::m::vm::eval::eval(json::iov &event, + const json::iov &content, + const vm::copts &opts) +:eval{opts} +{ + inject(*this, event, content); +} + +ircd::m::vm::eval::eval(const event &event, + const vm::opts &opts) +:eval +{ + vector_view(&event, 1), + opts +} +{ +} + +ircd::m::vm::eval::eval(const json::array &pdus, + const vm::opts &opts) +:eval{opts} +{ + execute(*this, pdus); +} + +ircd::m::vm::eval::eval(const vector_view &events, + const vm::opts &opts) +:eval{opts} +{ + execute(*this, events); +} + +ircd::m::vm::eval::~eval() +noexcept +{ + assert(!child); + if(parent) + { + assert(parent->child == this); + parent->child = nullptr; + } +} + +// +// Tools +// + void ircd::m::vm::eval::seqsort() { @@ -194,21 +360,26 @@ ircd::m::vm::eval::find_pdu(const event::id &event_id) return ret; } -const ircd::m::event * -ircd::m::vm::eval::find_pdu(const eval &eval, - const event::id &event_id) +size_t +ircd::m::vm::eval::count(const ctx::ctx *const &c) { - const m::event *ret{nullptr}; - for(const auto &event : eval.pdus) + return std::count_if(begin(eval::list), end(eval::list), [&c] + (const eval *const &eval) { - if(event.event_id != event_id) - continue; + return eval->ctx == c; + }); +} - ret = std::addressof(event); - break; - } +bool +ircd::m::vm::eval::for_each(const ctx::ctx *const &c, + const std::function &closure) +{ + for(eval *const &eval : eval::list) + if(eval->ctx == c) + if(!closure(*eval)) + return false; - return ret; + return true; } bool @@ -231,6 +402,7 @@ ircd::m::vm::eval::for_each_pdu(const std::function &closu return true; }); } + bool ircd::m::vm::eval::for_each(const std::function &closure) { @@ -241,138 +413,6 @@ ircd::m::vm::eval::for_each(const std::function &closure) return true; } -size_t -ircd::m::vm::eval::count(const ctx::ctx *const &c) -{ - return std::count_if(begin(eval::list), end(eval::list), [&c] - (const eval *const &eval) - { - return eval->ctx == c; - }); -} - -ircd::m::vm::eval * -ircd::m::vm::eval::find_root(const eval &a, - const ctx::ctx &c) -{ - eval *ret {nullptr}, *parent {nullptr}; do - { - if(!(parent = find_parent(a, c))) - return ret; - - ret = parent; - } - while(1); -} - -ircd::m::vm::eval * -ircd::m::vm::eval::find_parent(const eval &a, - const ctx::ctx &c) -{ - eval *ret {nullptr}; - for_each(&c, [&ret, &a] - (eval &eval) - { - const bool cond - { - (&eval != &a) && (!ret || eval.id > ret->id) - }; - - ret = cond? &eval : ret; - return true; - }); - - return ret; -} - -bool -ircd::m::vm::eval::for_each(const ctx::ctx *const &c, - const std::function &closure) -{ - for(eval *const &eval : eval::list) - if(eval->ctx == c) - if(!closure(*eval)) - return false; - - return true; -} - -// -// eval::eval -// - -ircd::m::vm::eval::eval(const vm::opts &opts) -:opts{&opts} -,parent -{ - find_parent(*this) -} -{ - if(parent) - { - assert(!parent->child); - parent->child = this; - } -} - -ircd::m::vm::eval::eval(const vm::copts &opts) -:opts{&opts} -,copts{&opts} -,parent -{ - find_parent(*this) -} -{ - if(parent) - { - assert(!parent->child); - parent->child = this; - } -} - -ircd::m::vm::eval::eval(json::iov &event, - const json::iov &content, - const vm::copts &opts) -:eval{opts} -{ - inject(*this, event, content); -} - -ircd::m::vm::eval::eval(const event &event, - const vm::opts &opts) -:eval -{ - vector_view(&event, 1), - opts -} -{ -} - -ircd::m::vm::eval::eval(const json::array &pdus, - const vm::opts &opts) -:eval{opts} -{ - execute(*this, pdus); -} - -ircd::m::vm::eval::eval(const vector_view &events, - const vm::opts &opts) -:eval{opts} -{ - execute(*this, events); -} - -ircd::m::vm::eval::~eval() -noexcept -{ - assert(!child); - if(parent) - { - assert(parent->child == this); - parent->child = nullptr; - } -} - void ircd::m::vm::eval::mfetch_keys() const