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 /// Construct an MemoryToIceAdapter from an existing Memory.
105
106 void setMemoryListener(client::MemoryListenerInterfacePrx memoryListenerTopic);
107
108 // WRITING
109 data::AddSegmentResult addSegment(const data::AddSegmentInput& input,
110 bool addCoreSegments = false);
111
112 data::AddSegmentsResult addSegments(const data::AddSegmentsInput& input,
113 bool addCoreSegments = false);
114
115
116 data::CommitResult commit(const data::Commit& commitIce, Time timeArrived);
117 data::CommitResult commit(const data::Commit& commitIce);
119 data::CommitResult commitLocking(const data::Commit& commitIce, Time timeArrived);
120 data::CommitResult commitLocking(const data::Commit& commitIce);
122
123
124 // READING
125 query::data::Result query(const armem::query::data::Input& input);
127
128 /**
129 * Query the LTMs of the memory server. Stored into WM if recording mode is enabled.
130 * @param storeIntoWM if set the result is also stored into the wm, no matter the recording mode.
131 */
132 query::data::Result queryLTM(const armem::query::data::Input& input, bool storeIntoWM);
133
134 armem::structure::data::GetServerStructureResult getServerStructure();
135
136 // LTM LOADING AND REPLAYING
137
138 /**
139 * Loads all core segments and their data from the LTM.
140 */
142
143 /**
144 * Only load specific core segments and their data from the LTM.
145 */
146 armem::CommitResult reloadCoreSegmentsFromLTM(std::list<std::string>& coreSegmentname);
147
149
150 /**
151 * Triggers a reload (@see reloadFromLTM) only if load on startup property is set.
152 * Loads only the core segments defined in the property 'loadedCoreSegments'
153 */
155
156 // LTM STORING AND RECORDING
157 dto::DirectlyStoreResult directlyStore(const dto::DirectlyStoreInput& directlStoreInput);
158 dto::StartRecordResult startRecord(const dto::StartRecordInput& startRecordInput);
159 dto::StopRecordResult stopRecord();
160 dto::RecordStatusResult getRecordStatus();
161
162 // PREDICTION
163 prediction::data::PredictionResultSeq
164 predict(prediction::data::PredictionRequestSeq requests);
165
166 prediction::data::EngineSupportMap getAvailableEngines();
167
168 /// Get statistics for memory operations (commits and queries)
169 const MemoryOperationStatistics& getStatistics() const { return statistics; }
170
171 /// Reset statistics counters
172 void resetStatistics() { statistics.reset(); }
173
174 /**
175 * @brief Report all debug metrics to the debug observer.
176 *
177 * This should be called periodically (e.g., every second) to update
178 * the debug observer with current memory performance metrics.
179 */
180 void reportDebugMetrics();
181
182 public:
185
186 client::MemoryListenerInterfacePrx memoryListenerTopic;
187
188
189 private:
190 armem::CommitResult _commit(const armem::Commit& commit, bool locking);
191
192 /// Statistics for tracking memory operations
193 MemoryOperationStatistics statistics;
194 };
195
196
197} // 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()
MemoryToIceAdapter(server::wm::Memory *workingMemory=nullptr, server::ltm::Memory *longtermMemory=nullptr)
Construct an MemoryToIceAdapter from an existing Memory.
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)
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.