rogue
Loading...
Searching...
No Matches
Transaction.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <inttypes.h>
22#include <stdarg.h>
23#include <sys/time.h>
24
25#include <cstdio>
26#include <memory>
27#include <string>
28
29#include "rogue/GeneralError.h"
30#include "rogue/GilRelease.h"
31#include "rogue/ScopedGil.h"
35
37
38#ifndef NO_PYTHON
39 #include <boost/python.hpp>
40namespace bp = boost::python;
41#endif
42
43// Init class counter
44uint32_t rim::Transaction::classIdx_ = 0;
45
47std::mutex rim::Transaction::classMtx_;
48
50rim::TransactionPtr rim::Transaction::create(struct timeval timeout) {
51 rim::TransactionPtr m = std::make_shared<rim::Transaction>(timeout);
52 return (m);
53}
54
55void rim::Transaction::setup_python() {
56#ifndef NO_PYTHON
57 bp::class_<rim::Transaction, rim::TransactionPtr, boost::noncopyable>("Transaction", bp::no_init)
58 .def("lock", &rim::Transaction::lock)
59 .def("id", &rim::Transaction::id)
60 .def("address", &rim::Transaction::address)
61 .def("size", &rim::Transaction::size)
62 .def("type", &rim::Transaction::type)
63 .def("done", &rim::Transaction::done)
64 .def("error", &rim::Transaction::errorStr)
65 .def("expired", &rim::Transaction::expired)
66 .def("setData", &rim::Transaction::setData)
67 .def("getData", &rim::Transaction::getData);
68#endif
69}
70
72rim::Transaction::Transaction(struct timeval timeout) : timeout_(timeout) {
73 gettimeofday(&startTime_, NULL);
74
75 endTime_.tv_sec = 0;
76 endTime_.tv_usec = 0;
77 warnTime_.tv_sec = 0;
78 warnTime_.tv_usec = 0;
79
80 pyValid_ = false;
81
82 iter_ = NULL;
83 address_ = 0;
84 size_ = 0;
85 type_ = 0;
86 error_ = "";
87 done_ = false;
88
89 isSubTransaction_ = false;
91
92 log_ = rogue::Logging::create("memory.Transaction", true);
93
94 classMtx_.lock();
95 if (classIdx_ == 0) classIdx_ = 1;
96 id_ = classIdx_;
97 classIdx_++;
98 classMtx_.unlock();
99}
100
102rim::Transaction::~Transaction() {}
103
105rim::TransactionLockPtr rim::Transaction::lock() {
106 return (rim::TransactionLock::create(shared_from_this()));
107}
108
110bool rim::Transaction::expired() {
111 bool done = false;
112 if (isSubTransaction_) {
113 // The child's iter_ points into the parent's buffer, which the parent
114 // releases in wait() even while it is still referenced elsewhere.
115 rim::TransactionPtr parentTran = parentTransaction_.lock();
116 if (!parentTran)
117 done = true;
118 else
119 done = (parentTran->iter_ == NULL || parentTran->done_);
120 }
121 return done || (iter_ == NULL || done_);
122}
123
125uint32_t rim::Transaction::id() {
126 return id_;
127}
128
130uint64_t rim::Transaction::address() {
131 return address_;
132}
133
135uint32_t rim::Transaction::size() {
136 return size_;
137}
138
140uint32_t rim::Transaction::type() {
141 return type_;
142}
143
145rim::TransactionPtr rim::Transaction::createSubTransaction() {
146 // Create a new transaction and set up pointers back and forth
147 rim::TransactionPtr subTran = std::make_shared<rim::Transaction>(timeout_);
148 subTran->parentTransaction_ = shared_from_this();
149 subTran->isSubTransaction_ = true;
150 subTranMap_[subTran->id()] = subTran;
151 log_->debug("Created subTransaction id=%" PRIu32 ", parent=%" PRIu32, subTran->id_, this->id_);
152
153 // Should subtransactions be given default address identical to parent?
154 return (subTran);
155}
156
157void rim::Transaction::doneSubTransactions() {
158 doneCreatingSubTransactions_ = true;
159}
160
162void rim::Transaction::done() {
163 log_->debug("Transaction done. type=%" PRIu32 " id=%" PRIu32 ", address=0x%016" PRIx64 ", size=%" PRIu32,
164 type_,
165 id_,
166 address_,
167 size_);
168
169 error_ = "";
170 done_ = true;
171 cond_.notify_all();
172
173 // If applicable, notify parent transaction about completion of a sub-transaction
174 if (isSubTransaction_) {
175 rim::TransactionPtr parentTran = this->parentTransaction_.lock();
176 if (parentTran) parentTran->subTransactionDone(id_, "");
177 }
178}
179
181void rim::Transaction::errorStr(std::string error) {
182 error_ += error;
183 done_ = true;
184
185 log_->debug("Transaction error. type=%" PRIu32 " id=%" PRIu32 ", address=0x%016" PRIx64 ", size=%" PRIu32
186 ", error=%s",
187 type_,
188 id_,
189 address_,
190 size_,
191 error_.c_str());
192
193 cond_.notify_all();
194
195 // If applicable, notify parent transaction about completion of a sub-transaction
196 if (isSubTransaction_) {
197 rim::TransactionPtr parentTran = parentTransaction_.lock();
198 if (parentTran) parentTran->subTransactionDone(id_, error);
199 }
200}
201
203void rim::Transaction::subTransactionDone(uint32_t subId, const std::string& subError) {
204 // Remove the child from the pending map
205 subTranMap_.erase(subId);
206
207 // Retain the failure rather than completing now. Completing the parent on
208 // the first failure could let the caller release a buffer that other
209 // children are still accessing.
210 if (!subError.empty()) {
211 subTranError_ += "Transaction error. Subtransaction " + std::to_string(subId) + " failed with error: " +
212 subError + ".\n";
213 }
214
215 // Complete the parent once the last child has finished, reporting any
216 // child failure collected along the way
217 if (subTranMap_.empty() && doneCreatingSubTransactions_) {
218 if (subTranError_.empty())
219 done();
220 else
221 errorStr(subTranError_);
222 }
223}
224
226void rim::Transaction::error(const char* fmt, ...) {
227 va_list args;
228 char buffer[10000];
229
230 va_start(args, fmt);
231 vsnprintf(buffer, sizeof(buffer), fmt, args);
232 va_end(args);
233
234 errorStr(std::string(buffer));
235}
236
238std::string rim::Transaction::wait() {
239 struct timeval currTime;
240
241 std::unique_lock<std::mutex> lock(lock_);
242
243 while (!done_) {
244 // Timeout?
245 gettimeofday(&currTime, NULL);
246 if (timerisset(&endTime_) && timercmp(&currTime, &(endTime_), >)) {
247 done_ = true;
248 error_ = "Timeout waiting for register transaction " + std::to_string(id_) + " message response.";
249
250 log_->debug("Transaction timeout. type=%" PRIu32 " id=%" PRIu32
251 ", address=0x%" PRIx64 ", size=%" PRIu32 ", timeout=%" PRId64 ".%06" PRId64 "s",
252 type_,
253 id_,
254 address_,
255 size_,
256 static_cast<int64_t>(timeout_.tv_sec),
257 static_cast<int64_t>(timeout_.tv_usec));
258 } else {
259 cond_.wait_for(lock, std::chrono::microseconds(1000));
260 }
261 }
262
263 // Reset
264 if (pyValid_) {
266#ifndef NO_PYTHON
267 PyBuffer_Release(&(pyBuf_));
268#endif
269 }
270 iter_ = NULL;
271 pyValid_ = false;
272
273 return (error_);
274}
275
277void rim::Transaction::refreshTimer(rim::TransactionPtr ref) {
278 struct timeval currTime;
279 struct timeval nextTime;
280
281 gettimeofday(&currTime, NULL);
282 std::lock_guard<std::mutex> lock(lock_);
283
284 // Refresh if start time is later then the reference
285 if (ref == NULL || timercmp(&startTime_, &(ref->startTime_), >=)) {
286 timeradd(&currTime, &timeout_, &endTime_);
287
288 if (warnTime_.tv_sec == 0 && warnTime_.tv_usec == 0) {
289 warnTime_ = endTime_;
290 } else if (timercmp(&warnTime_, &currTime, >=)) {
291 log_->warning("Transaction timer refresh! Possible slow link! type=%" PRIu32 " id=%" PRIu32
292 ", address=0x%016" PRIx64 ", size=%" PRIu32 ", timeout=%" PRId64 ".%06" PRId64 "s",
293 type_,
294 id_,
295 address_,
296 size_,
297 static_cast<int64_t>(timeout_.tv_sec),
298 static_cast<int64_t>(timeout_.tv_usec));
299 warnTime_ = endTime_;
300 }
301 }
302}
303
305rim::Transaction::iterator rim::Transaction::begin() {
306 if (iter_ == NULL) throw(rogue::GeneralError("Transaction::begin", "Invalid data"));
307 return iter_;
308}
309
311rim::Transaction::iterator rim::Transaction::end() {
312 if (iter_ == NULL) throw(rogue::GeneralError("Transaction::end", "Invalid data"));
313 return iter_ + size_;
314}
315
316#ifndef NO_PYTHON
317
319void rim::Transaction::setData(boost::python::object p, uint32_t offset) {
320 Py_buffer pyBuf;
321
322 if (PyObject_GetBuffer(p.ptr(), &pyBuf, PyBUF_SIMPLE) < 0)
323 throw(rogue::GeneralError("Transaction::setData", "Python Buffer Error In Frame"));
324
325 uint32_t count = pyBuf.len;
326
327 if ((offset + count) > size_) {
328 PyBuffer_Release(&pyBuf);
329 throw(rogue::GeneralError::create("Transaction::setData",
330 "Attempt to set %" PRIu32 " bytes at offset %" PRIu32
331 " to python buffer with size %" PRIu32,
332 count,
333 offset,
334 size_));
335 }
336
337 std::memcpy(begin() + offset, reinterpret_cast<uint8_t*>(pyBuf.buf), count);
338 PyBuffer_Release(&pyBuf);
339}
340
342void rim::Transaction::getData(boost::python::object p, uint32_t offset) {
343 Py_buffer pyBuf;
344
345 if (PyObject_GetBuffer(p.ptr(), &pyBuf, PyBUF_CONTIG) < 0)
346 throw(rogue::GeneralError("Transaction::getData", "Python Buffer Error In Frame"));
347
348 uint32_t count = pyBuf.len;
349
350 if ((offset + count) > size_) {
351 PyBuffer_Release(&pyBuf);
352 throw(rogue::GeneralError::create("Transaction::getData",
353 "Attempt to get %" PRIu32 " bytes from offset %" PRIu32
354 " to python buffer with size %" PRIu32,
355 count,
356 offset,
357 size_));
358 }
359
360 std::memcpy(reinterpret_cast<uint8_t*>(pyBuf.buf), begin() + offset, count);
361 PyBuffer_Release(&pyBuf);
362}
363
364#endif
Generic Rogue exception type.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
std::shared_ptr< rogue::Logging > log_
std::shared_ptr< rogue::interfaces::memory::TransactionLock > TransactionLockPtr
Shared pointer alias for TransactionLock.
std::shared_ptr< rogue::interfaces::memory::Transaction > TransactionPtr
Shared pointer alias for Transaction.