-
Notifications
You must be signed in to change notification settings - Fork 11
Part1 of : Feat double buffer pushtype #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de> | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #pragma once | ||
|
|
||
| #include "NDRegisterAccessor.h" | ||
| #include "NDRegisterAccessorDecorator.h" | ||
| #include "NumericAddressedBackend.h" | ||
| #include "RegisterInfo.h" | ||
| #include "TransferElement.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace ChimeraTK { | ||
|
|
||
| template<typename UserType> | ||
| class DoubleBufferAccessor : public NDRegisterAccessor<UserType> { | ||
| public: | ||
| DoubleBufferAccessor(NumericAddressedRegisterInfo::DoubleBufferInfo doubleBufferConfig, | ||
| const boost::shared_ptr<DeviceBackend>& backend, std::shared_ptr<detail::CountedRecursiveMutex> mutex, | ||
| const RegisterPath& registerPathName, size_t numberOfWords, size_t wordOffsetInRegister, AccessModeFlags flags); | ||
|
|
||
| void doPreRead(TransferType type) override; | ||
|
|
||
| void doReadTransferSynchronously() override; | ||
|
|
||
| void doPostRead(TransferType type, bool hasNewData) override; | ||
|
|
||
| [[nodiscard]] bool isWriteable() const override { return false; } | ||
| [[nodiscard]] bool isReadOnly() const override { return true; } | ||
| [[nodiscard]] bool isReadable() const override { return true; }; | ||
|
|
||
| bool doWriteTransfer(ChimeraTK::VersionNumber /* VersionNumber */) override { return false; } | ||
|
|
||
| void doPreWrite(TransferType, VersionNumber) override { | ||
| throw ChimeraTK::logic_error("DoubleBufferAccessor: Writing is not allowed atm."); | ||
| } | ||
|
|
||
| void doPostWrite(TransferType, VersionNumber) override { | ||
| // do not throw here again | ||
| } | ||
|
|
||
| // below functions are needed for TransferGroup to work | ||
| std::vector<boost::shared_ptr<TransferElement>> getHardwareAccessingElements() override; | ||
|
|
||
| std::list<boost::shared_ptr<TransferElement>> getInternalElements() override { return {}; } | ||
|
|
||
| void replaceTransferElement(boost::shared_ptr<ChimeraTK::TransferElement> /* newElement */) override {} | ||
| [[nodiscard]] bool mayReplaceOther(const boost::shared_ptr<TransferElement const>& other) const override; | ||
|
|
||
| protected: | ||
| using ChimeraTK::NDRegisterAccessor<UserType>::buffer_2D; | ||
| NumericAddressedRegisterInfo::DoubleBufferInfo _doubleBufferInfo; | ||
| boost::shared_ptr<DeviceBackend> _backend; | ||
| std::shared_ptr<detail::CountedRecursiveMutex> _mutex; | ||
| std::unique_lock<detail::CountedRecursiveMutex> _transferLock; | ||
| boost::shared_ptr<NDRegisterAccessor<UserType>> _buffer0; | ||
| boost::shared_ptr<NDRegisterAccessor<UserType>> _buffer1; | ||
| boost::shared_ptr<ChimeraTK::NDRegisterAccessor<uint32_t>> _enableDoubleBufferReg; | ||
| boost::shared_ptr<ChimeraTK::NDRegisterAccessor<uint32_t>> _currentBufferNumberReg; | ||
| uint32_t _currentBuffer{0}; | ||
|
nshehz marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| DECLARE_TEMPLATE_FOR_CHIMERATK_USER_TYPES(DoubleBufferAccessor); | ||
| } // namespace ChimeraTK | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de> | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #pragma once | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include <optional> | ||
|
|
||
| namespace nlohmann { | ||
|
|
||
| template<typename T> | ||
| struct adl_serializer<std::optional<T>> { | ||
| static void to_json(json& j, const std::optional<T>& opt) { | ||
| if(opt.has_value()) { | ||
| j = *opt; | ||
| } | ||
| else { | ||
| j = nullptr; | ||
| } | ||
| } | ||
|
|
||
| static void from_json(const json& j, std::optional<T>& opt) { | ||
| if(j.is_null()) { | ||
| opt = std::nullopt; | ||
| } | ||
| else { | ||
| opt = j.get<T>(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace nlohmann |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de> | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| #include "DoubleBufferAccessor.h" | ||
| namespace ChimeraTK { | ||
|
|
||
| template<typename UserType> | ||
| DoubleBufferAccessor<UserType>::DoubleBufferAccessor( | ||
| NumericAddressedRegisterInfo::DoubleBufferInfo doubleBufferConfig, | ||
| const boost::shared_ptr<DeviceBackend>& backend, std::shared_ptr<detail::CountedRecursiveMutex> mutex, | ||
| const RegisterPath& registerPathName, size_t numberOfWords, size_t wordOffsetInRegister, AccessModeFlags flags) | ||
| : NDRegisterAccessor<UserType>(registerPathName, flags), _doubleBufferInfo(std::move(doubleBufferConfig)), | ||
| _backend(boost::dynamic_pointer_cast<NumericAddressedBackend>(backend)), _mutex(std::move(mutex)), | ||
| _transferLock(*_mutex, std::defer_lock) { | ||
| _enableDoubleBufferReg = | ||
| backend->getRegisterAccessor<uint32_t>(_doubleBufferInfo.enableRegisterPath, 1, _doubleBufferInfo.index, {}); | ||
| _currentBufferNumberReg = backend->getRegisterAccessor<uint32_t>( | ||
| _doubleBufferInfo.inactiveBufferRegisterPath, 1, _doubleBufferInfo.index, {}); | ||
|
|
||
| auto buf0Name = registerPathName + ".BUF0"; | ||
| auto buf1Name = registerPathName + ".BUF1"; | ||
|
|
||
| _buffer0 = backend->getRegisterAccessor<UserType>(buf0Name, numberOfWords, wordOffsetInRegister, flags); | ||
| _buffer1 = backend->getRegisterAccessor<UserType>(buf1Name, numberOfWords, wordOffsetInRegister, flags); | ||
| size_t nChannels = _buffer0->getNumberOfChannels(); | ||
| // size_t nSamples = _buffer0->getNumberOfSamples(); | ||
|
|
||
| this->buffer_2D.resize(nChannels); | ||
| for(size_t i = 0; i < nChannels; ++i) { | ||
| buffer_2D[i].resize(numberOfWords); | ||
| } | ||
|
|
||
| { | ||
| std::lock_guard<detail::CountedRecursiveMutex> lg(*_mutex); | ||
| if(_mutex->useCount() == 1) { | ||
| _enableDoubleBufferReg->accessChannel(0)[0] = 1; | ||
| _enableDoubleBufferReg->write(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template<typename UserType> | ||
| void DoubleBufferAccessor<UserType>::doPreRead(TransferType type) { | ||
| // Acquire lock for full transfer lifecycle | ||
| _transferLock.lock(); // blocks other threads | ||
| // acquire a lock in firmware (disable buffer swapping) | ||
| if(_mutex->useCount() == 1) { | ||
| _enableDoubleBufferReg->accessData(0) = 0; | ||
| _enableDoubleBufferReg->write(); | ||
| } | ||
| // check which buffer is now in use by the firmware | ||
| _currentBufferNumberReg->read(); | ||
| _currentBuffer = _currentBufferNumberReg->accessData(0); | ||
| // if current buffer 1, it means firmware writes now to buffer1, so use target (buffer 0), else use | ||
| // _secondBufferReg (buffer 1) | ||
| if(_currentBuffer == 1) { | ||
| _buffer0->preRead(type); | ||
| } | ||
| else { | ||
| _buffer1->preRead(type); | ||
| } | ||
| } | ||
|
|
||
| template<typename UserType> | ||
| void DoubleBufferAccessor<UserType>::doReadTransferSynchronously() { | ||
| if(_currentBuffer == 1) { | ||
| _buffer0->readTransfer(); | ||
| } | ||
| else { | ||
| _buffer1->readTransfer(); | ||
| } | ||
| } | ||
|
|
||
| template<typename UserType> | ||
| void DoubleBufferAccessor<UserType>::doPostRead(TransferType type, bool hasNewData) { | ||
| auto unlocker = cppext::finally([&] { _transferLock.unlock(); }); | ||
| if(_currentBuffer == 1) { | ||
| _buffer0->postRead(type, hasNewData); | ||
| } | ||
| else { | ||
| _buffer1->postRead(type, hasNewData); | ||
| } | ||
|
|
||
| // release a lock in firmware (enable buffer swapping) | ||
| if(_mutex->useCount() == 1) { | ||
| _enableDoubleBufferReg->accessData(0) = 1; | ||
| _enableDoubleBufferReg->write(); | ||
| } | ||
| // set version and data validity of this object | ||
| this->_versionNumber = {}; | ||
| if(_currentBuffer == 1) { | ||
| this->_dataValidity = _buffer0->dataValidity(); | ||
| } | ||
| else { | ||
| this->_dataValidity = _buffer1->dataValidity(); | ||
| } | ||
|
|
||
| // Note: TransferElement Spec E.6.1 dictates that the version number and data validity needs to be set before this | ||
| // check. | ||
| if(!hasNewData) { | ||
| return; | ||
| } | ||
|
|
||
| // Swap buffer_2D if new data | ||
| if(hasNewData) { | ||
| auto& reg = (_currentBuffer == 1) ? _buffer0 : _buffer1; | ||
| for(size_t i = 0; i < reg->getNumberOfChannels(); ++i) { | ||
| buffer_2D[i].swap(reg->accessChannel(i)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template<typename UserType> | ||
| std::vector<boost::shared_ptr<TransferElement>> DoubleBufferAccessor<UserType>::getHardwareAccessingElements() { | ||
| // returning only this means the DoubleBufferAccessor will not be optimized when put into TransferGroup | ||
| // optimizing would break our handshake protocol, since it reorders transfers | ||
| return {TransferElement::shared_from_this()}; | ||
| } | ||
|
|
||
| template<typename UserType> | ||
| bool DoubleBufferAccessor<UserType>::mayReplaceOther(const boost::shared_ptr<const TransferElement>& other) const { | ||
| auto otherDoubleBuffer = boost::dynamic_pointer_cast<const DoubleBufferAccessor<UserType>>(other); | ||
| if(!otherDoubleBuffer || otherDoubleBuffer.get() == this) { | ||
| return false; | ||
| } | ||
| return (_buffer0->mayReplaceOther(otherDoubleBuffer->_buffer0)); | ||
|
nshehz marked this conversation as resolved.
|
||
| } | ||
|
|
||
| INSTANTIATE_TEMPLATE_FOR_CHIMERATK_USER_TYPES(DoubleBufferAccessor); | ||
|
|
||
| } // namespace ChimeraTK | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.