MemoryToIceAdapter.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <mutex>
6
7#include <RobotAPI/interface/armem/client/MemoryListenerInterface.h>
8#include <RobotAPI/interface/armem/server/MemoryInterface.h>
13
15{
16 /**
17 * @brief Statistics for memory read/write operations.
18 *
19 * Thread-safe statistics tracking for debugging memory performance.
20 * Tracks counts, rates, and timing information for commits and queries.
21 */
23 {
24 // Commit (write) statistics
25 std::atomic<uint64_t> totalCommitCount{0};
26 std::atomic<uint64_t> totalEntityUpdates{0};
27 std::atomic<uint64_t> successfulUpdates{0};
28 std::atomic<uint64_t> failedUpdates{0};
29 std::atomic<double> totalCommitBlockingTimeMs{0.0};
30 std::atomic<double> maxCommitBlockingTimeMs{0.0};
31
32 // Query (read) statistics
33 std::atomic<uint64_t> totalQueryCount{0};
34 std::atomic<double> totalQueryBlockingTimeMs{0.0};
35 std::atomic<double> maxQueryBlockingTimeMs{0.0};
36
37 // Rate calculation helpers (protected by mutex for rate calculations)
38 mutable std::mutex rateMutex;
39 std::chrono::steady_clock::time_point lastRateCalculation{std::chrono::steady_clock::now()};
40 uint64_t lastCommitCount{0};
41 uint64_t lastQueryCount{0};
42 double commitsPerSecond{0.0};
43 double queriesPerSecond{0.0};
44
45 /**
46 * @brief Calculate and update the current rates.
47 * @return Pair of (commits/sec, queries/sec)
48 */
49 std::pair<double, double> updateRates()
50 {
51 std::lock_guard<std::mutex> lock(rateMutex);
52 auto now = std::chrono::steady_clock::now();
53 double elapsedSec = std::chrono::duration<double>(now - lastRateCalculation).count();
54
55 if (elapsedSec >= 1.0) // Update rates at most once per second
56 {
57 uint64_t currentCommits = totalCommitCount.load(std::memory_order_relaxed);
58 uint64_t currentQueries = totalQueryCount.load(std::memory_order_relaxed);
59
60 commitsPerSecond = (currentCommits - lastCommitCount) / elapsedSec;
61 queriesPerSecond = (currentQueries - lastQueryCount) / elapsedSec;
62
63 lastCommitCount = currentCommits;
64 lastQueryCount = currentQueries;
66 }
67
69 }
70
71 void reset()
72 {
76 failedUpdates = 0;
82
83 std::lock_guard<std::mutex> lock(rateMutex);
84 lastRateCalculation = std::chrono::steady_clock::now();
87 commitsPerSecond = 0.0;
88 queriesPerSecond = 0.0;
89 }
90 };
91
92
93 /**
94 * @brief Helps connecting a Memory server to the Ice interface.
95 *
96 * This involves conversion of ice types to C++ types as well as
97 * catchin exceptions and converting them to error messages
98 */
100 {
101 public:
102 /**
103 * @brief Construct a MemoryToIceAdapter from an existing Memory.
104 *
105 * Both pointers are non-owning and must be non-null: the commit and query paths
106 * dereference them unconditionally. This is checked in the constructor.
107 */
110
111 void setMemoryListener(client::MemoryListenerInterfacePrx memoryListenerTopic);
112
113 // WRITING
114 data::AddSegmentResult addSegment(const data::AddSegmentInput& input,
115 bool addCoreSegments = false);
116
117 data::AddSegmentsResult addSegments(const data::AddSegmentsInput& input,
118 bool addCoreSegments = false);
119
120
121 data::CommitResult commit(const data::Commit& commitIce, Time timeArrived);
122 data::CommitResult commit(const data::Commit& commitIce);
124 data::CommitResult commitLocking(const data::Commit& commitIce, Time timeArrived);
125 data::CommitResult commitLocking(const data::Commit& commitIce);
127
128
129 // READING
130 query::data::Result query(const armem::query::data::Input& input);
132
133 /**
134 * Query the LTMs of the memory server. Stored into WM if recording mode is enabled.
135 * @param storeIntoWM if set the result is also stored into the wm, no matter the recording mode.
136 */
137 query::data::Result queryLTM(const armem::query::data::Input& input, bool storeIntoWM);
138
139 armem::structure::data::GetServerStructureResult getServerStructure();
140
141 // LTM LOADING AND REPLAYING
142
143 /**
144 * Loads all core segments and their data from the LTM.
145 */
147
148 /**
149 * Only load specific core segments and their data from the LTM.
150 */
151 armem::CommitResult reloadCoreSegmentsFromLTM(std::list<std::string>& coreSegmentname);
152
154
155 /**
156 * Triggers a reload (@see reloadFromLTM) only if load on startup property is set.
157 * Loads only the core segments defined in the property 'loadedCoreSegments'
158 */
160
161 // LTM STORING AND RECORDING
162 dto::DirectlyStoreResult directlyStore(const dto::DirectlyStoreInput& directlStoreInput);
163 dto::StartRecordResult startRecord(const dto::StartRecordInput& startRecordInput);
164 dto::StopRecordResult stopRecord();
165 dto::RecordStatusResult getRecordStatus();
166
167 // PREDICTION
168 prediction::data::PredictionResultSeq
169 predict(prediction::data::PredictionRequestSeq requests);
170
171 prediction::data::EngineSupportMap getAvailableEngines();
172
173 /// Get statistics for memory operations (commits and queries)
174 const MemoryOperationStatistics& getStatistics() const { return statistics; }
175
176 /// Reset statistics counters
177 void resetStatistics() { statistics.reset(); }
178
179 /**
180 * @brief Report all debug metrics to the debug observer.
181 *
182 * This should be called periodically (e.g., every second) to update
183 * the debug observer with current memory performance metrics.
184 */
185 void reportDebugMetrics();
186
187 public:
190
191 client::MemoryListenerInterfacePrx memoryListenerTopic;
192
193
194 private:
195 armem::CommitResult _commit(const armem::Commit& commit, bool locking);
196
197 /// Statistics for tracking memory operations
198 MemoryOperationStatistics statistics;
199 };
200
201
202} // namespace armarx::armem::server
armem::structure::data::GetServerStructureResult getServerStructure()
void setMemoryListener(client::MemoryListenerInterfacePrx memoryListenerTopic)
dto::StartRecordResult startRecord(const dto::StartRecordInput &startRecordInput)
const MemoryOperationStatistics & getStatistics() const
Get statistics for memory operations (commits and queries)
armem::CommitResult reloadFromLTMOnStartup()
Triggers a reload (.
armem::CommitResult reloadAllFromLTM()
Loads all core segments and their data from the LTM.
query::data::Result queryLTM(const armem::query::data::Input &input, bool storeIntoWM)
Query the LTMs of the memory server.
client::MemoryListenerInterfacePrx memoryListenerTopic
query::data::Result query(const armem::query::data::Input &input)
armem::CommitResult reloadCoreSegmentsFromLTM(std::list< std::string > &coreSegmentname)
Only load specific core segments and their data from the LTM.
prediction::data::PredictionResultSeq predict(prediction::data::PredictionRequestSeq requests)
dto::DirectlyStoreResult directlyStore(const dto::DirectlyStoreInput &directlStoreInput)
void reportDebugMetrics()
Report all debug metrics to the debug observer.
void resetStatistics()
Reset statistics counters.
armem::CommitResult reloadPropertyDefinedCoreSegmentsFromLTM()
data::AddSegmentResult addSegment(const data::AddSegmentInput &input, bool addCoreSegments=false)
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
prediction::data::EngineSupportMap getAvailableEngines()
data::AddSegmentsResult addSegments(const data::AddSegmentsInput &input, bool addCoreSegments=false)
data::CommitResult commit(const data::Commit &commitIce, Time timeArrived)
MemoryToIceAdapter(server::wm::Memory *workingMemory, server::ltm::Memory *longtermMemory)
Construct a MemoryToIceAdapter from an existing Memory.
A memory storing data on the hard drive and in mongodb (needs 'armarx memory start' to start the mong...
Definition Memory.h:24
armarx::core::time::DateTime Time
Result of a Commit.
Definition Commit.h:111
A bundle of updates to be sent to the memory.
Definition Commit.h:90
A query for parts of a memory.
Definition Query.h:24
Result of a QueryInput.
Definition Query.h:51
Statistics for memory read/write operations.
std::chrono::steady_clock::time_point lastRateCalculation
std::pair< double, double > updateRates()
Calculate and update the current rates.