4#include "Stroika/Frameworks/StroikaPreComp.h"
6#include "Stroika/Foundation/Containers/SortedMapping.h"
7#include "Stroika/Foundation/Debug/Main.h"
16using namespace Stroika::Frameworks;
17using namespace Stroika::Frameworks::SystemPerformance;
25 : pMostRecentMeasurements{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]] const auto* property) ->
MeasurementSet {
26 const
Capturer* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &
Capturer::pMostRecentMeasurements);
27 return thisObj->fCurrentMeasurementSet_.load ();
29 , measurementsCallbacks{
31 const Capturer* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &Capturer::measurementsCallbacks);
32 return thisObj->fCallbacks_.load ();
34 [qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
auto* property,
const auto& callbacks) {
35 Capturer* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &Capturer::measurementsCallbacks);
36 thisObj->fCallbacks_ = callbacks;
38 , captureSets{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) ->
Collection<CaptureSet> {
39 const Capturer* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &Capturer::captureSets);
40 return thisObj->fCaptureSets_.load ();
42 [qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
auto* property,
const auto& captureSets) {
43 Capturer* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &Capturer::captureSets);
44 thisObj->fCaptureSets_ = captureSets;
47 Require (Debug::AppearsDuringMainLifetime ());
56void Capturer::AddMeasurementsCallback (
const NewMeasurementsCallbackType& cb)
58 fCallbacks_.rwget ()->Add (cb);
61void Capturer::RemoveMeasurementsCallback (
const NewMeasurementsCallbackType& cb)
63 fCallbacks_.rwget ()->Remove (cb);
66void Capturer::AddCaptureSet (
const CaptureSet& cs)
69 auto rwLock = fCaptureSets_.rwget ();
71 ++fCaptureSetChangeCount_;
75void Capturer::ManageRunner_ (
bool on)
79 if (not threadPoolRunning) {
80 fThreadPool_.
AddTask ([
this] () { Runner_ (); });
85 if (threadPoolRunning) {
92void Capturer::Runner_ ()
103 uint64_t changeCountForCaptureSet{0};
110 auto recomputeSortOrder = [&] () {
112 auto lock = fCaptureSets_.cget ();
119 for (
const auto& i : lock.load ()) {
120 runQueue.
Add (now + i.runPeriod (), i);
122 changeCountForCaptureSet = fCaptureSetChangeCount_;
127 if (changeCountForCaptureSet < fCaptureSetChangeCount_) {
128 recomputeSortOrder ();
131 Assert (not runQueue.
empty ());
135 auto iterator = runQueue.
begin ();
136 Assert (iterator != runQueue.
end ());
139 RunnerOnce_ (runNext.fValue);
140 runQueue.
erase (iterator);
141 runQueue.
Add (runNext.fKey + runNext.fValue.runPeriod (), runNext.fValue);
145void Capturer::RunnerOnce_ (
const CaptureSet& cs)
152 catch (
const Thread::AbortException&) {
156 using namespace Characters::Literals;
157 DbgTrace (
"Eating exception '{}' in Capturer runner"_f, current_exception ());
160 UpdateMeasurementSet_ (measurements);
165 fCurrentMeasurementSet_.rwget ().rwref ().MergeAdditions (ms);
166 for (
const auto& cb : fCallbacks_.load ()) {
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
nonvirtual bool Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
nonvirtual void erase(ArgByValueType< key_type > key)
nonvirtual void AbortTasks(Time::DurationSeconds timeout=Time::kInfinity)
nonvirtual TaskType AddTask(const TaskType &task, const optional< Characters::String > &name=nullopt)
nonvirtual size_t GetTasksCount() const
return total number of tasks, either pending, or currently running.
nonvirtual void SetPoolSize(unsigned int poolSize)
nonvirtual Iterator< T > begin() const
Support for ranged for, and STL syntax in general.
nonvirtual bool empty() const
Returns true iff size() == 0.
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.
void SleepUntil(Time::TimePointSeconds untilTickCount)