Merge branch 'shm' into refactor

This commit is contained in:
maliakal_d 2019-03-13 15:21:12 +01:00
commit 75ce111344
11 changed files with 1230 additions and 1137 deletions

View File

@ -10,42 +10,12 @@
#include "sls_detector_funcs.h" #include "sls_detector_funcs.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#define VERBOSE #define VERBOSE
int main() { int main() {
// const std::string hostname = "beb083";
// auto d = slsDetector(hostname);
// d.setOnline(1);
// std::cout << "type: " << d.getDetectorTypeAsString() << '\n';
// std::cout << "hostname: " << d.getHostname() << '\n';
// std::cout << "receiver: " << d.getReceiverOnline() << '\n';
// std::cout << "control: " << d.getControlPort() << '\n';
// std::cout << "stop: " << d.getStopPort() << '\n';
// std::cout << "receiver: " << d.getReceiverPort() << '\n';
// std::cout << "exptime: " << d.setTimer(slsDetectorDefs::timerIndex::ACQUISITION_TIME) << '\n';
// auto d2 = slsDetector(type, 0, 1);
// d2.setHostname("beb098");.
// auto d2 = slsDetector();
// std::cout << "hn: " << d2.getHostname() << '\n';
// sls::Timer t;
// for (int i = 0; i != 100; ++i) {
// int fnum = 1;
// int ret = slsDetectorDefs::FAIL;
// slsDetectorDefs::detectorType retval = slsDetectorDefs::detectorType::GENERIC;
// auto cs = sls::ClientSocket("beb083", 1952);
// cs.sendData(reinterpret_cast<char *>(&fnum), sizeof(fnum));
// cs.receiveData(reinterpret_cast<char *>(&ret), sizeof(ret));
// cs.receiveData(reinterpret_cast<char *>(&retval), sizeof(retval));
// std::cout << "retval: " << retval << '\n';
// }
// t.print_elapsed();
return 0; return 0;
} }

View File

@ -1,6 +1,6 @@
set(SOURCES set(SOURCES
multiSlsDetector/multiSlsDetector.cpp multiSlsDetector/multiSlsDetector.cpp
sharedMemory/SharedMemory.cpp # sharedMemory/SharedMemory.cpp
slsDetector/slsDetectorUsers.cpp slsDetector/slsDetectorUsers.cpp
slsDetector/slsDetectorCommand.cpp slsDetector/slsDetectorCommand.cpp
slsDetector/slsDetector.cpp slsDetector/slsDetector.cpp
@ -49,6 +49,10 @@ if(DOXYGEN_FOUND)
) )
endif() endif()
if (SLS_USE_TESTS)
add_subdirectory(tests)
endif(SLS_USE_TESTS)
install(TARGETS slsDetectorShared install(TARGETS slsDetectorShared
EXPORT "${TARGETS_EXPORT_NAME}" EXPORT "${TARGETS_EXPORT_NAME}"

View File

@ -31,12 +31,7 @@ multiSlsDetector::multiSlsDetector(int id, bool verify, bool update)
setupMultiDetector(verify, update); setupMultiDetector(verify, update);
} }
multiSlsDetector::~multiSlsDetector() { multiSlsDetector::~multiSlsDetector() = default;
if (sharedMemory) {
sharedMemory->UnmapSharedMemory(thisMultiDetector);
delete sharedMemory;
}
}
void multiSlsDetector::setupMultiDetector(bool verify, bool update) { void multiSlsDetector::setupMultiDetector(bool verify, bool update) {
initSharedMemory(verify); initSharedMemory(verify);
@ -197,21 +192,21 @@ void multiSlsDetector::setErrorMaskFromAllDetectors() {
} }
void multiSlsDetector::setAcquiringFlag(bool b) { void multiSlsDetector::setAcquiringFlag(bool b) {
thisMultiDetector->acquiringFlag = b; sharedMemory()->acquiringFlag = b;
} }
bool multiSlsDetector::getAcquiringFlag() const { bool multiSlsDetector::getAcquiringFlag() const {
return thisMultiDetector->acquiringFlag; return sharedMemory()->acquiringFlag;
} }
bool multiSlsDetector::isAcquireReady() { bool multiSlsDetector::isAcquireReady() {
if (thisMultiDetector->acquiringFlag) { if (sharedMemory()->acquiringFlag) {
FILE_LOG(logWARNING) << "Acquire has already started. " FILE_LOG(logWARNING) << "Acquire has already started. "
"If previous acquisition terminated unexpectedly, " "If previous acquisition terminated unexpectedly, "
"reset busy flag to restart.(sls_detector_put busy 0)"; "reset busy flag to restart.(sls_detector_put busy 0)";
return FAIL; return FAIL;
} }
thisMultiDetector->acquiringFlag = true; sharedMemory()->acquiringFlag = true;
return OK; return OK;
} }
@ -249,23 +244,18 @@ void multiSlsDetector::freeSharedMemory(int multiId, int detPos) {
return; return;
} }
// multi // multi - get number of detectors from shm
// get number of detectors SharedMemory<sharedMultiSlsDetector> multiShm(multiId, -1);
int numDetectors = 0; int numDetectors = 0;
auto shm = SharedMemory(multiId, -1);
// get number of detectors from multi shm if (multiShm.IsExisting()) {
if (shm.IsExisting()) { multiShm.OpenSharedMemory();
sharedMultiSlsDetector *mdet = numDetectors = multiShm()->numberOfDetectors;
(sharedMultiSlsDetector *)shm.OpenSharedMemory( multiShm.RemoveSharedMemory();
sizeof(sharedMultiSlsDetector));
numDetectors = mdet->numberOfDetectors;
shm.UnmapSharedMemory(mdet);
shm.RemoveSharedMemory();
} }
for (int i = 0; i < numDetectors; ++i) { for (int i = 0; i < numDetectors; ++i) {
auto shm = SharedMemory(multiId, i); SharedMemory<sharedSlsDetector> shm(multiId, i);
shm.RemoveSharedMemory(); shm.RemoveSharedMemory();
} }
} }
@ -286,17 +276,7 @@ void multiSlsDetector::freeSharedMemory(int detPos) {
detectors.clear(); detectors.clear();
// clear multi detector shm // clear multi detector shm
if (sharedMemory) { sharedMemory.RemoveSharedMemory();
if (thisMultiDetector) {
sharedMemory->UnmapSharedMemory(thisMultiDetector);
thisMultiDetector = nullptr;
}
sharedMemory->RemoveSharedMemory();
delete sharedMemory;
sharedMemory = nullptr;
}
// zmq
client_downstream = false; client_downstream = false;
} }
@ -312,78 +292,56 @@ std::string multiSlsDetector::getUserDetails() {
sstream << d->getDetectorTypeAsString() << "+"; sstream << d->getDetectorTypeAsString() << "+";
} }
sstream << "\nPID: " << thisMultiDetector->lastPID sstream << "\nPID: " << sharedMemory()->lastPID
<< "\nUser: " << thisMultiDetector->lastUser << "\nUser: " << sharedMemory()->lastUser
<< "\nDate: " << thisMultiDetector->lastDate << std::endl; << "\nDate: " << sharedMemory()->lastDate << std::endl;
return sstream.str(); return sstream.str();
} }
/* /*
* pre: sharedMemory=0, thisMultiDetector = 0, detectors.size() = 0 * pre: sharedMemory=0, sharedMemory() = 0, detectors.size() = 0
* exceptions are caught in calling function, shm unmapped and deleted
*/ */
void multiSlsDetector::initSharedMemory(bool verify) { void multiSlsDetector::initSharedMemory(bool verify) {
try { sharedMemory = SharedMemory<sharedMultiSlsDetector>(detId, -1);
// shared memory object with name if (!sharedMemory.IsExisting()) {
sharedMemory = new SharedMemory(detId, -1); sharedMemory.CreateSharedMemory();
size_t sz = sizeof(sharedMultiSlsDetector); initializeDetectorStructure();
}
// create else {
if (!sharedMemory->IsExisting()) { sharedMemory.OpenSharedMemory();
thisMultiDetector = if (verify && sharedMemory()->shmversion != MULTI_SHMVERSION) {
(sharedMultiSlsDetector *)sharedMemory->CreateSharedMemory(sz); FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
initializeDetectorStructure(); "(expected 0x"
<< std::hex << MULTI_SHMVERSION << " but got 0x" << sharedMemory()->shmversion << std::dec;
throw SharedMemoryException();
} }
// open and verify version
else {
thisMultiDetector =
(sharedMultiSlsDetector *)sharedMemory->OpenSharedMemory(sz);
if (verify && thisMultiDetector->shmversion != MULTI_SHMVERSION) {
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
"(expected 0x"
<< std::hex << MULTI_SHMVERSION << " but got 0x" << thisMultiDetector->shmversion << std::dec;
throw SharedMemoryException();
}
}
} catch (...) {
if (sharedMemory) {
// unmap
if (thisMultiDetector) {
sharedMemory->UnmapSharedMemory(thisMultiDetector);
thisMultiDetector = nullptr;
}
// delete
delete sharedMemory;
sharedMemory = nullptr;
}
throw;
} }
} }
void multiSlsDetector::initializeDetectorStructure() { void multiSlsDetector::initializeDetectorStructure() {
thisMultiDetector->shmversion = MULTI_SHMVERSION; sharedMemory()->shmversion = MULTI_SHMVERSION;
thisMultiDetector->numberOfDetectors = 0; sharedMemory()->numberOfDetectors = 0;
thisMultiDetector->numberOfDetector[X] = 0; sharedMemory()->numberOfDetector[X] = 0;
thisMultiDetector->numberOfDetector[Y] = 0; sharedMemory()->numberOfDetector[Y] = 0;
thisMultiDetector->onlineFlag = 1; sharedMemory()->onlineFlag = 1;
thisMultiDetector->stoppedFlag = 0; sharedMemory()->stoppedFlag = 0;
thisMultiDetector->dataBytes = 0; sharedMemory()->dataBytes = 0;
thisMultiDetector->dataBytesInclGapPixels = 0; sharedMemory()->dataBytesInclGapPixels = 0;
thisMultiDetector->numberOfChannels = 0; sharedMemory()->numberOfChannels = 0;
thisMultiDetector->numberOfChannel[X] = 0; sharedMemory()->numberOfChannel[X] = 0;
thisMultiDetector->numberOfChannel[Y] = 0; sharedMemory()->numberOfChannel[Y] = 0;
thisMultiDetector->numberOfChannelInclGapPixels[X] = 0; sharedMemory()->numberOfChannelInclGapPixels[X] = 0;
thisMultiDetector->numberOfChannelInclGapPixels[Y] = 0; sharedMemory()->numberOfChannelInclGapPixels[Y] = 0;
thisMultiDetector->maxNumberOfChannelsPerDetector[X] = 0; sharedMemory()->maxNumberOfChannelsPerDetector[X] = 0;
thisMultiDetector->maxNumberOfChannelsPerDetector[Y] = 0; sharedMemory()->maxNumberOfChannelsPerDetector[Y] = 0;
for (int64_t &i : thisMultiDetector->timerValue) { for (int64_t &i : sharedMemory()->timerValue) {
i = 0; i = 0;
} }
thisMultiDetector->acquiringFlag = false; sharedMemory()->acquiringFlag = false;
thisMultiDetector->receiverOnlineFlag = OFFLINE_FLAG; sharedMemory()->receiverOnlineFlag = OFFLINE_FLAG;
thisMultiDetector->receiver_upstream = false; sharedMemory()->receiver_upstream = false;
} }
void multiSlsDetector::initializeMembers(bool verify) { void multiSlsDetector::initializeMembers(bool verify) {
@ -391,7 +349,7 @@ void multiSlsDetector::initializeMembers(bool verify) {
zmqSocket.clear(); zmqSocket.clear();
// get objects from single det shared memory (open) // get objects from single det shared memory (open)
for (int i = 0; i < thisMultiDetector->numberOfDetectors; i++) { for (int i = 0; i < sharedMemory()->numberOfDetectors; i++) {
try { try {
detectors.push_back( detectors.push_back(
sls::make_unique<slsDetector>(detId, i, verify)); sls::make_unique<slsDetector>(detId, i, verify));
@ -406,15 +364,15 @@ void multiSlsDetector::initializeMembers(bool verify) {
} }
void multiSlsDetector::updateUserdetails() { void multiSlsDetector::updateUserdetails() {
thisMultiDetector->lastPID = getpid(); sharedMemory()->lastPID = getpid();
memset(thisMultiDetector->lastUser, 0, SHORT_STRING_LENGTH); memset(sharedMemory()->lastUser, 0, SHORT_STRING_LENGTH);
memset(thisMultiDetector->lastDate, 0, SHORT_STRING_LENGTH); memset(sharedMemory()->lastDate, 0, SHORT_STRING_LENGTH);
try { try {
sls::strcpy_safe(thisMultiDetector->lastUser, exec("whoami").c_str()); sls::strcpy_safe(sharedMemory()->lastUser, exec("whoami").c_str());
sls::strcpy_safe(thisMultiDetector->lastDate, exec("date").c_str()); sls::strcpy_safe(sharedMemory()->lastDate, exec("date").c_str());
} catch (...) { } catch (...) {
sls::strcpy_safe(thisMultiDetector->lastUser, "errorreading"); sls::strcpy_safe(sharedMemory()->lastUser, "errorreading");
sls::strcpy_safe(thisMultiDetector->lastDate, "errorreading"); sls::strcpy_safe(sharedMemory()->lastDate, "errorreading");
} }
} }
@ -450,7 +408,7 @@ void multiSlsDetector::setHostname(const char *name, int detPos) {
// multi // multi
// this check is there only to allow the previous detsizechan command // this check is there only to allow the previous detsizechan command
if (thisMultiDetector->numberOfDetectors) { if (sharedMemory()->numberOfDetectors) {
FILE_LOG(logWARNING) << "There are already detector(s) in shared memory." FILE_LOG(logWARNING) << "There are already detector(s) in shared memory."
"Freeing Shared memory now."; "Freeing Shared memory now.";
freeSharedMemory(); freeSharedMemory();
@ -503,11 +461,11 @@ void multiSlsDetector::addSlsDetector(const std::string &hostname) {
int pos = (int)detectors.size(); int pos = (int)detectors.size();
detectors.push_back(sls::make_unique<slsDetector>(type, detId, pos, false)); detectors.push_back(sls::make_unique<slsDetector>(type, detId, pos, false));
thisMultiDetector->numberOfDetectors = detectors.size(); sharedMemory()->numberOfDetectors = detectors.size();
thisMultiDetector->dataBytes += detectors[pos]->getDataBytes(); sharedMemory()->dataBytes += detectors[pos]->getDataBytes();
thisMultiDetector->dataBytesInclGapPixels += sharedMemory()->dataBytesInclGapPixels +=
detectors[pos]->getDataBytesInclGapPixels(); detectors[pos]->getDataBytesInclGapPixels();
thisMultiDetector->numberOfChannels += sharedMemory()->numberOfChannels +=
detectors[pos]->getTotalNumberOfChannels(); detectors[pos]->getTotalNumberOfChannels();
detectors[pos]->setHostname(hostname); detectors[pos]->setHostname(hostname);
@ -541,12 +499,12 @@ int multiSlsDetector::getNumberOfDetectors() const {
} }
int multiSlsDetector::getNumberOfDetectors(dimension d) const { int multiSlsDetector::getNumberOfDetectors(dimension d) const {
return thisMultiDetector->numberOfDetector[d]; return sharedMemory()->numberOfDetector[d];
} }
void multiSlsDetector::getNumberOfDetectors(int &nx, int &ny) const { void multiSlsDetector::getNumberOfDetectors(int &nx, int &ny) const {
nx = thisMultiDetector->numberOfDetector[X]; nx = sharedMemory()->numberOfDetector[X];
ny = thisMultiDetector->numberOfDetector[Y]; ny = sharedMemory()->numberOfDetector[Y];
} }
int multiSlsDetector::getTotalNumberOfChannels(int detPos) { int multiSlsDetector::getTotalNumberOfChannels(int detPos) {
@ -556,7 +514,7 @@ int multiSlsDetector::getTotalNumberOfChannels(int detPos) {
} }
// multi // multi
return thisMultiDetector->numberOfChannels; return sharedMemory()->numberOfChannels;
} }
int multiSlsDetector::getTotalNumberOfChannels(dimension d, int detPos) { int multiSlsDetector::getTotalNumberOfChannels(dimension d, int detPos) {
@ -566,7 +524,7 @@ int multiSlsDetector::getTotalNumberOfChannels(dimension d, int detPos) {
} }
// multi // multi
return thisMultiDetector->numberOfChannel[d]; return sharedMemory()->numberOfChannel[d];
} }
int multiSlsDetector::getTotalNumberOfChannelsInclGapPixels(dimension d, int multiSlsDetector::getTotalNumberOfChannelsInclGapPixels(dimension d,
@ -577,16 +535,16 @@ int multiSlsDetector::getTotalNumberOfChannelsInclGapPixels(dimension d,
} }
// multi // multi
return thisMultiDetector->numberOfChannelInclGapPixels[d]; return sharedMemory()->numberOfChannelInclGapPixels[d];
} }
int multiSlsDetector::getMaxNumberOfChannelsPerDetector(dimension d) { int multiSlsDetector::getMaxNumberOfChannelsPerDetector(dimension d) {
return thisMultiDetector->maxNumberOfChannelsPerDetector[d]; return sharedMemory()->maxNumberOfChannelsPerDetector[d];
} }
int multiSlsDetector::setMaxNumberOfChannelsPerDetector(dimension d, int i) { int multiSlsDetector::setMaxNumberOfChannelsPerDetector(dimension d, int i) {
thisMultiDetector->maxNumberOfChannelsPerDetector[d] = i; sharedMemory()->maxNumberOfChannelsPerDetector[d] = i;
return thisMultiDetector->maxNumberOfChannelsPerDetector[d]; return sharedMemory()->maxNumberOfChannelsPerDetector[d];
} }
int multiSlsDetector::getDetectorOffset(dimension d, int detPos) { int multiSlsDetector::getDetectorOffset(dimension d, int detPos) {
@ -601,22 +559,22 @@ void multiSlsDetector::updateOffsets() {
FILE_LOG(logDEBUG1) << "Updating Multi-Detector Offsets"; FILE_LOG(logDEBUG1) << "Updating Multi-Detector Offsets";
int offsetX = 0, offsetY = 0, numX = 0, numY = 0; int offsetX = 0, offsetY = 0, numX = 0, numY = 0;
int maxChanX = thisMultiDetector->maxNumberOfChannelsPerDetector[X]; int maxChanX = sharedMemory()->maxNumberOfChannelsPerDetector[X];
int maxChanY = thisMultiDetector->maxNumberOfChannelsPerDetector[Y]; int maxChanY = sharedMemory()->maxNumberOfChannelsPerDetector[Y];
int prevChanX = 0; int prevChanX = 0;
int prevChanY = 0; int prevChanY = 0;
bool firstTime = true; bool firstTime = true;
thisMultiDetector->numberOfChannel[X] = 0; sharedMemory()->numberOfChannel[X] = 0;
thisMultiDetector->numberOfChannel[Y] = 0; sharedMemory()->numberOfChannel[Y] = 0;
thisMultiDetector->numberOfDetector[X] = 0; sharedMemory()->numberOfDetector[X] = 0;
thisMultiDetector->numberOfDetector[Y] = 0; sharedMemory()->numberOfDetector[Y] = 0;
// gap pixels // gap pixels
int offsetX_gp = 0, offsetY_gp = 0, numX_gp = 0, numY_gp = 0; int offsetX_gp = 0, offsetY_gp = 0, numX_gp = 0, numY_gp = 0;
int prevChanX_gp = 0, prevChanY_gp = 0; int prevChanX_gp = 0, prevChanY_gp = 0;
thisMultiDetector->numberOfChannelInclGapPixels[X] = 0; sharedMemory()->numberOfChannelInclGapPixels[X] = 0;
thisMultiDetector->numberOfChannelInclGapPixels[Y] = 0; sharedMemory()->numberOfChannelInclGapPixels[Y] = 0;
for (size_t idet = 0; idet < detectors.size(); ++idet) { for (size_t idet = 0; idet < detectors.size(); ++idet) {
FILE_LOG(logDEBUG1) << "offsetX:" << offsetX << " prevChanX:" << prevChanX << " offsetY:" << offsetY << " prevChanY:" << prevChanY << " offsetX_gp:" << offsetX_gp << " prevChanX_gp:" << prevChanX_gp << " offsetY_gp:" << offsetY_gp << " prevChanY_gp:" << prevChanY_gp; FILE_LOG(logDEBUG1) << "offsetX:" << offsetX << " prevChanX:" << prevChanX << " offsetY:" << offsetY << " prevChanY:" << prevChanY << " offsetX_gp:" << offsetX_gp << " prevChanX_gp:" << prevChanX_gp << " offsetY_gp:" << offsetY_gp << " prevChanY_gp:" << prevChanY_gp;
@ -649,8 +607,8 @@ void multiSlsDetector::updateOffsets() {
detectors[idet]->getTotalNumberOfChannelsInclGapPixels(X); detectors[idet]->getTotalNumberOfChannelsInclGapPixels(X);
numY_gp += numY_gp +=
detectors[idet]->getTotalNumberOfChannelsInclGapPixels(Y); detectors[idet]->getTotalNumberOfChannelsInclGapPixels(Y);
++thisMultiDetector->numberOfDetector[X]; ++sharedMemory()->numberOfDetector[X];
++thisMultiDetector->numberOfDetector[Y]; ++sharedMemory()->numberOfDetector[Y];
FILE_LOG(logDEBUG1) << "incrementing in both direction"; FILE_LOG(logDEBUG1) << "incrementing in both direction";
} }
@ -668,8 +626,8 @@ void multiSlsDetector::updateOffsets() {
numY_gp += numY_gp +=
detectors[idet]->getTotalNumberOfChannelsInclGapPixels(Y); detectors[idet]->getTotalNumberOfChannelsInclGapPixels(Y);
// increment in y again only in the first column (else you double increment) // increment in y again only in the first column (else you double increment)
if (thisMultiDetector->numberOfDetector[X] == 1) if (sharedMemory()->numberOfDetector[X] == 1)
++thisMultiDetector->numberOfDetector[Y]; ++sharedMemory()->numberOfDetector[Y];
FILE_LOG(logDEBUG1) << "incrementing in y direction"; FILE_LOG(logDEBUG1) << "incrementing in y direction";
} }
@ -698,7 +656,7 @@ void multiSlsDetector::updateOffsets() {
numX += detectors[idet]->getTotalNumberOfChannels(X); numX += detectors[idet]->getTotalNumberOfChannels(X);
numX_gp += numX_gp +=
detectors[idet]->getTotalNumberOfChannelsInclGapPixels(X); detectors[idet]->getTotalNumberOfChannelsInclGapPixels(X);
++thisMultiDetector->numberOfDetector[X]; ++sharedMemory()->numberOfDetector[X];
FILE_LOG(logDEBUG1) << "incrementing in x direction"; FILE_LOG(logDEBUG1) << "incrementing in x direction";
} }
@ -716,28 +674,28 @@ void multiSlsDetector::updateOffsets() {
<< detectors[idet]->getDetectorOffset(Y) << ")"; << detectors[idet]->getDetectorOffset(Y) << ")";
// offsetY has been reset sometimes and offsetX the first time, // offsetY has been reset sometimes and offsetX the first time,
// but remember the highest values // but remember the highest values
if (numX > thisMultiDetector->numberOfChannel[X]) { if (numX > sharedMemory()->numberOfChannel[X]) {
thisMultiDetector->numberOfChannel[X] = numX; sharedMemory()->numberOfChannel[X] = numX;
} }
if (numY > thisMultiDetector->numberOfChannel[Y]) { if (numY > sharedMemory()->numberOfChannel[Y]) {
thisMultiDetector->numberOfChannel[Y] = numY; sharedMemory()->numberOfChannel[Y] = numY;
} }
if (numX_gp > thisMultiDetector->numberOfChannelInclGapPixels[X]) { if (numX_gp > sharedMemory()->numberOfChannelInclGapPixels[X]) {
thisMultiDetector->numberOfChannelInclGapPixels[X] = numX_gp; sharedMemory()->numberOfChannelInclGapPixels[X] = numX_gp;
} }
if (numY_gp > thisMultiDetector->numberOfChannelInclGapPixels[Y]) { if (numY_gp > sharedMemory()->numberOfChannelInclGapPixels[Y]) {
thisMultiDetector->numberOfChannelInclGapPixels[Y] = numY_gp; sharedMemory()->numberOfChannelInclGapPixels[Y] = numY_gp;
} }
} }
FILE_LOG(logDEBUG1) << "\n\tNumber of Channels in X direction:" << thisMultiDetector->numberOfChannel[X] << "\n\tNumber of Channels in Y direction:" << thisMultiDetector->numberOfChannel[Y] << "\n\tNumber of Channels in X direction with Gap Pixels:" << thisMultiDetector->numberOfChannelInclGapPixels[X] << "\n\tNumber of Channels in Y direction with Gap Pixels:" << thisMultiDetector->numberOfChannelInclGapPixels[Y]; FILE_LOG(logDEBUG1) << "\n\tNumber of Channels in X direction:" << sharedMemory()->numberOfChannel[X] << "\n\tNumber of Channels in Y direction:" << sharedMemory()->numberOfChannel[Y] << "\n\tNumber of Channels in X direction with Gap Pixels:" << sharedMemory()->numberOfChannelInclGapPixels[X] << "\n\tNumber of Channels in Y direction with Gap Pixels:" << sharedMemory()->numberOfChannelInclGapPixels[Y];
thisMultiDetector->numberOfChannels = sharedMemory()->numberOfChannels =
thisMultiDetector->numberOfChannel[0] * sharedMemory()->numberOfChannel[0] *
thisMultiDetector->numberOfChannel[1]; sharedMemory()->numberOfChannel[1];
for (auto &d : detectors) { for (auto &d : detectors) {
d->updateMultiSize(thisMultiDetector->numberOfDetector[0], d->updateMultiSize(sharedMemory()->numberOfDetector[0],
thisMultiDetector->numberOfDetector[1]); sharedMemory()->numberOfDetector[1]);
} }
} }
@ -750,9 +708,9 @@ int multiSlsDetector::setOnline(int value, int detPos) {
// multi // multi
if (value != GET_ONLINE_FLAG) { if (value != GET_ONLINE_FLAG) {
auto r = parallelCall(&slsDetector::setOnline, value); auto r = parallelCall(&slsDetector::setOnline, value);
thisMultiDetector->onlineFlag = sls::minusOneIfDifferent(r); sharedMemory()->onlineFlag = sls::minusOneIfDifferent(r);
} }
return thisMultiDetector->onlineFlag; return sharedMemory()->onlineFlag;
} }
std::string multiSlsDetector::checkOnline(int detPos) { std::string multiSlsDetector::checkOnline(int detPos) {
@ -1104,12 +1062,12 @@ int multiSlsDetector::stopAcquisition(int detPos) {
if (detPos >= 0) { if (detPos >= 0) {
// if only 1 detector, set flag to stop current acquisition // if only 1 detector, set flag to stop current acquisition
if (detectors.size() == 1) { if (detectors.size() == 1) {
thisMultiDetector->stoppedFlag = 1; sharedMemory()->stoppedFlag = 1;
} }
return detectors[detPos]->stopAcquisition(); return detectors[detPos]->stopAcquisition();
} else { } else {
thisMultiDetector->stoppedFlag = 1; sharedMemory()->stoppedFlag = 1;
auto r = parallelCall(&slsDetector::stopAcquisition); auto r = parallelCall(&slsDetector::stopAcquisition);
return sls::allEqualTo(r, static_cast<int>(OK)) ? OK : FAIL; return sls::allEqualTo(r, static_cast<int>(OK)) ? OK : FAIL;
} }
@ -1196,7 +1154,7 @@ int64_t multiSlsDetector::setTimer(timerIndex index, int64_t t, int detPos) {
<< "Cannot set number of frames, cycles, " << "Cannot set number of frames, cycles, "
"storage cells or measurements individually."; "storage cells or measurements individually.";
setErrorMask(getErrorMask() | MUST_BE_MULTI_CMD); setErrorMask(getErrorMask() | MUST_BE_MULTI_CMD);
return thisMultiDetector->timerValue[index]; return sharedMemory()->timerValue[index];
default: default:
break; break;
} }
@ -1227,7 +1185,7 @@ int64_t multiSlsDetector::setTimer(timerIndex index, int64_t t, int detPos) {
} }
} }
thisMultiDetector->timerValue[index] = ret; sharedMemory()->timerValue[index] = ret;
return ret; return ret;
} }
@ -1395,16 +1353,16 @@ int multiSlsDetector::setDynamicRange(int dr, int detPos) {
int ret = sls::minusOneIfDifferent(r); int ret = sls::minusOneIfDifferent(r);
// update shm // update shm
int prevValue = thisMultiDetector->dataBytes; int prevValue = sharedMemory()->dataBytes;
int prevGValue = thisMultiDetector->dataBytesInclGapPixels; int prevGValue = sharedMemory()->dataBytesInclGapPixels;
thisMultiDetector->dataBytes = 0; sharedMemory()->dataBytes = 0;
thisMultiDetector->dataBytesInclGapPixels = 0; sharedMemory()->dataBytesInclGapPixels = 0;
thisMultiDetector->numberOfChannels = 0; sharedMemory()->numberOfChannels = 0;
for (auto &d : detectors) { for (auto &d : detectors) {
thisMultiDetector->dataBytes += d->getDataBytes(); sharedMemory()->dataBytes += d->getDataBytes();
thisMultiDetector->dataBytesInclGapPixels += sharedMemory()->dataBytesInclGapPixels +=
d->getDataBytesInclGapPixels(); d->getDataBytesInclGapPixels();
thisMultiDetector->numberOfChannels += d->getTotalNumberOfChannels(); sharedMemory()->numberOfChannels += d->getTotalNumberOfChannels();
} }
// for usability // for usability
@ -1427,8 +1385,8 @@ int multiSlsDetector::setDynamicRange(int dr, int detPos) {
// update offsets if there was a change FIXME:add dr to sls shm and check // update offsets if there was a change FIXME:add dr to sls shm and check
// that instead // that instead
if ((prevValue != thisMultiDetector->dataBytes) || if ((prevValue != sharedMemory()->dataBytes) ||
(prevGValue != thisMultiDetector->dataBytesInclGapPixels)) { (prevGValue != sharedMemory()->dataBytesInclGapPixels)) {
updateOffsets(); updateOffsets();
} }
@ -1955,7 +1913,6 @@ std::string multiSlsDetector::getAdditionalJsonParameter(const std::string &key,
return sls::concatenateIfDifferent(r); return sls::concatenateIfDifferent(r);
} }
int multiSlsDetector::setDetectorMinMaxEnergyThreshold(const int index, int value, int detPos) { int multiSlsDetector::setDetectorMinMaxEnergyThreshold(const int index, int value, int detPos) {
std::string parameter = (index ? "emax" : "emin"); std::string parameter = (index ? "emax" : "emin");
@ -1971,7 +1928,7 @@ int multiSlsDetector::setDetectorMinMaxEnergyThreshold(const int index, int valu
return stoi(result); return stoi(result);
} }
// not found or cannot scan integer // not found or cannot scan integer
catch(...) { catch (...) {
return -1; return -1;
} }
} }
@ -2064,7 +2021,7 @@ int multiSlsDetector::loadImageToDetector(imageType index,
// multi // multi
// read image for all // read image for all
int nch = thisMultiDetector->numberOfChannels; int nch = sharedMemory()->numberOfChannels;
short int imageVals[nch]; short int imageVals[nch];
if (readDataFile(fname, imageVals, nch) < nch * (int)sizeof(short int)) { if (readDataFile(fname, imageVals, nch) < nch * (int)sizeof(short int)) {
FILE_LOG(logERROR) << "Could not open file or not enough data in file " FILE_LOG(logERROR) << "Could not open file or not enough data in file "
@ -2093,7 +2050,7 @@ int multiSlsDetector::writeCounterBlockFile(const std::string &fname,
// multi // multi
// get image from all // get image from all
int nch = thisMultiDetector->numberOfChannels; int nch = sharedMemory()->numberOfChannels;
short int imageVals[nch]; short int imageVals[nch];
std::vector<int> r; std::vector<int> r;
for (size_t idet = 0; idet < detectors.size(); ++idet) { for (size_t idet = 0; idet < detectors.size(); ++idet) {
@ -2537,7 +2494,7 @@ int multiSlsDetector::enableGapPixels(int val, int detPos) {
// update data bytes incl gap pixels // update data bytes incl gap pixels
if (val != -1) { if (val != -1) {
auto r = serialCall(&slsDetector::getDataBytesInclGapPixels); auto r = serialCall(&slsDetector::getDataBytesInclGapPixels);
thisMultiDetector->dataBytesInclGapPixels = sls::sum(r); sharedMemory()->dataBytesInclGapPixels = sls::sum(r);
// update // update
updateOffsets(); updateOffsets();
@ -2744,9 +2701,9 @@ int multiSlsDetector::setReceiverOnline(int value, int detPos) {
// multi // multi
if (value != GET_ONLINE_FLAG) { if (value != GET_ONLINE_FLAG) {
auto r = parallelCall(&slsDetector::setReceiverOnline, value); auto r = parallelCall(&slsDetector::setReceiverOnline, value);
thisMultiDetector->receiverOnlineFlag = sls::minusOneIfDifferent(r); sharedMemory()->receiverOnlineFlag = sls::minusOneIfDifferent(r);
} }
return thisMultiDetector->receiverOnlineFlag; return sharedMemory()->receiverOnlineFlag;
} }
std::string multiSlsDetector::checkReceiverOnline(int detPos) { std::string multiSlsDetector::checkReceiverOnline(int detPos) {
@ -3084,8 +3041,8 @@ int multiSlsDetector::createReceivingDataSockets(const bool destroy) {
void multiSlsDetector::readFrameFromReceiver() { void multiSlsDetector::readFrameFromReceiver() {
int nX = int nX =
thisMultiDetector->numberOfDetector[X]; // to copy data in multi module sharedMemory()->numberOfDetector[X]; // to copy data in multi module
int nY = thisMultiDetector int nY = sharedMemory()
->numberOfDetector[Y]; // for eiger, to reverse the data ->numberOfDetector[Y]; // for eiger, to reverse the data
bool gappixelsenable = false; bool gappixelsenable = false;
bool eiger = false; bool eiger = false;
@ -3233,8 +3190,8 @@ void multiSlsDetector::readFrameFromReceiver() {
// 4bit gap pixels // 4bit gap pixels
if (dynamicRange == 4 && gappixelsenable) { if (dynamicRange == 4 && gappixelsenable) {
int n = processImageWithGapPixels(multiframe, multigappixels); int n = processImageWithGapPixels(multiframe, multigappixels);
nPixelsX = thisMultiDetector->numberOfChannelInclGapPixels[X]; nPixelsX = sharedMemory()->numberOfChannelInclGapPixels[X];
nPixelsY = thisMultiDetector->numberOfChannelInclGapPixels[Y]; nPixelsY = sharedMemory()->numberOfChannelInclGapPixels[Y];
thisData = new detectorData(getCurrentProgress(), thisData = new detectorData(getCurrentProgress(),
currentFileName.c_str(), nPixelsX, currentFileName.c_str(), nPixelsX,
nPixelsY, multigappixels, n, nPixelsY, multigappixels, n,
@ -3290,12 +3247,12 @@ void multiSlsDetector::readFrameFromReceiver() {
int multiSlsDetector::processImageWithGapPixels(char *image, char *&gpImage) { int multiSlsDetector::processImageWithGapPixels(char *image, char *&gpImage) {
// eiger 4 bit mode // eiger 4 bit mode
int nxb = thisMultiDetector->numberOfDetector[X] * (512 + 3); int nxb = sharedMemory()->numberOfDetector[X] * (512 + 3);
int nyb = thisMultiDetector->numberOfDetector[Y] * (256 + 1); int nyb = sharedMemory()->numberOfDetector[Y] * (256 + 1);
int gapdatabytes = nxb * nyb; int gapdatabytes = nxb * nyb;
int nxchip = thisMultiDetector->numberOfDetector[X] * 4; int nxchip = sharedMemory()->numberOfDetector[X] * 4;
int nychip = thisMultiDetector->numberOfDetector[Y] * 1; int nychip = sharedMemory()->numberOfDetector[Y] * 1;
// allocate // allocate
if (gpImage == nullptr) { if (gpImage == nullptr) {
@ -3559,7 +3516,7 @@ uint64_t multiSlsDetector::setPatternWord(int addr, uint64_t word, int detPos) {
} }
int multiSlsDetector::setPatternLoops(int level, int &start, int &stop, int &n, int multiSlsDetector::setPatternLoops(int level, int &start, int &stop, int &n,
int detPos) { int detPos) {
// single // single
if (detPos >= 0) { if (detPos >= 0) {
return detectors[detPos]->setPatternLoops(level, start, stop, n); return detectors[detPos]->setPatternLoops(level, start, stop, n);
@ -3902,20 +3859,20 @@ void multiSlsDetector::registerDataCallback(
int multiSlsDetector::setTotalProgress() { int multiSlsDetector::setTotalProgress() {
int nf = 1, nc = 1, ns = 1, nm = 1; int nf = 1, nc = 1, ns = 1, nm = 1;
if (thisMultiDetector->timerValue[FRAME_NUMBER]) { if (sharedMemory()->timerValue[FRAME_NUMBER]) {
nf = thisMultiDetector->timerValue[FRAME_NUMBER]; nf = sharedMemory()->timerValue[FRAME_NUMBER];
} }
if (thisMultiDetector->timerValue[CYCLES_NUMBER] > 0) { if (sharedMemory()->timerValue[CYCLES_NUMBER] > 0) {
nc = thisMultiDetector->timerValue[CYCLES_NUMBER]; nc = sharedMemory()->timerValue[CYCLES_NUMBER];
} }
if (thisMultiDetector->timerValue[STORAGE_CELL_NUMBER] > 0) { if (sharedMemory()->timerValue[STORAGE_CELL_NUMBER] > 0) {
ns = thisMultiDetector->timerValue[STORAGE_CELL_NUMBER] + 1; ns = sharedMemory()->timerValue[STORAGE_CELL_NUMBER] + 1;
} }
if (thisMultiDetector->timerValue[MEASUREMENTS_NUMBER] > 0) { if (sharedMemory()->timerValue[MEASUREMENTS_NUMBER] > 0) {
nm = thisMultiDetector->timerValue[MEASUREMENTS_NUMBER]; nm = sharedMemory()->timerValue[MEASUREMENTS_NUMBER];
} }
totalProgress = nm * nf * nc * ns; totalProgress = nm * nf * nc * ns;
@ -3967,10 +3924,10 @@ int multiSlsDetector::acquire() {
bool receiver = (setReceiverOnline() == ONLINE_FLAG); bool receiver = (setReceiverOnline() == ONLINE_FLAG);
progressIndex = 0; progressIndex = 0;
thisMultiDetector->stoppedFlag = 0; sharedMemory()->stoppedFlag = 0;
setJoinThreadFlag(false); setJoinThreadFlag(false);
int nm = thisMultiDetector->timerValue[MEASUREMENTS_NUMBER]; int nm = sharedMemory()->timerValue[MEASUREMENTS_NUMBER];
if (nm < 1) { if (nm < 1) {
nm = 1; nm = 1;
} }
@ -3980,7 +3937,7 @@ int multiSlsDetector::acquire() {
std::lock_guard<std::mutex> lock(mg); std::lock_guard<std::mutex> lock(mg);
if (getReceiverStatus() != IDLE) { if (getReceiverStatus() != IDLE) {
if (stopReceiver() == FAIL) { if (stopReceiver() == FAIL) {
thisMultiDetector->stoppedFlag = 1; sharedMemory()->stoppedFlag = 1;
} }
} }
} }
@ -3991,13 +3948,13 @@ int multiSlsDetector::acquire() {
if (receiver) { if (receiver) {
std::lock_guard<std::mutex> lock(mg); std::lock_guard<std::mutex> lock(mg);
if (resetFramesCaught() == FAIL) { if (resetFramesCaught() == FAIL) {
thisMultiDetector->stoppedFlag = 1; sharedMemory()->stoppedFlag = 1;
} }
} }
// loop through measurements // loop through measurements
for (int im = 0; im < nm; ++im) { for (int im = 0; im < nm; ++im) {
if (thisMultiDetector->stoppedFlag) { if (sharedMemory()->stoppedFlag) {
break; break;
} }
@ -4007,7 +3964,7 @@ int multiSlsDetector::acquire() {
if (startReceiver() == FAIL) { if (startReceiver() == FAIL) {
FILE_LOG(logERROR) << "Start receiver failed "; FILE_LOG(logERROR) << "Start receiver failed ";
stopReceiver(); stopReceiver();
thisMultiDetector->stoppedFlag = 1; sharedMemory()->stoppedFlag = 1;
break; break;
} }
// let processing thread listen to these packets // let processing thread listen to these packets
@ -4020,7 +3977,7 @@ int multiSlsDetector::acquire() {
std::lock_guard<std::mutex> lock(mg); std::lock_guard<std::mutex> lock(mg);
if (receiver) { if (receiver) {
if (stopReceiver() == FAIL) { if (stopReceiver() == FAIL) {
thisMultiDetector->stoppedFlag = 1; sharedMemory()->stoppedFlag = 1;
} else { } else {
if (dataReady) { if (dataReady) {
sem_wait(&sem_endRTAcquisition); // waits for receiver's sem_wait(&sem_endRTAcquisition); // waits for receiver's
@ -4035,7 +3992,7 @@ int multiSlsDetector::acquire() {
if (measurement_finished) { if (measurement_finished) {
measurement_finished(im, findex, measFinished_p); measurement_finished(im, findex, measFinished_p);
} }
if (thisMultiDetector->stoppedFlag) { if (sharedMemory()->stoppedFlag) {
break; break;
} }

View File

@ -7,12 +7,11 @@
* @short This is the base class for multi detector system functionalities * @short This is the base class for multi detector system functionalities
* @author Anna Bergamaschi * @author Anna Bergamaschi
*/ */
#include "SharedMemory.h"
#include "error_defs.h" #include "error_defs.h"
#include "logger.h" #include "logger.h"
#include "sls_detector_defs.h" #include "sls_detector_defs.h"
class slsDetector; class slsDetector;
class SharedMemory;
class ZmqSocket; class ZmqSocket;
class detectorData; class detectorData;
@ -27,91 +26,91 @@ class detectorData;
#define SHORT_STRING_LENGTH 50 #define SHORT_STRING_LENGTH 50
#define DATE_LENGTH 30 #define DATE_LENGTH 30
class multiSlsDetector : public virtual slsDetectorDefs, /**
public virtual errorDefs {
private:
/**
* @short structure allocated in shared memory to store detector settings * @short structure allocated in shared memory to store detector settings
* for IPC and cache * for IPC and cache
*/ */
typedef struct sharedMultiSlsDetector { struct sharedMultiSlsDetector {
/* FIXED PATTERN FOR STATIC FUNCTIONS. DO NOT CHANGE, ONLY APPEND /* FIXED PATTERN FOR STATIC FUNCTIONS. DO NOT CHANGE, ONLY APPEND
* ------*/ * ------*/
/** shared memory version */ /** shared memory version */
int shmversion; int shmversion;
/** last process id accessing the shared memory */ /** last process id accessing the shared memory */
pid_t lastPID; pid_t lastPID;
/** last user name accessing the shared memory */ /** last user name accessing the shared memory */
char lastUser[SHORT_STRING_LENGTH]; char lastUser[SHORT_STRING_LENGTH];
/** last time stamp when accessing the shared memory */ /** last time stamp when accessing the shared memory */
char lastDate[SHORT_STRING_LENGTH]; char lastDate[SHORT_STRING_LENGTH];
/** number of sls detectors in shared memory */ /** number of sls detectors in shared memory */
int numberOfDetectors; int numberOfDetectors;
/** END OF FIXED PATTERN /** END OF FIXED PATTERN
* -----------------------------------------------*/ * -----------------------------------------------*/
/** Number of detectors operated at once */ /** Number of detectors operated at once */
int numberOfDetector[2]; int numberOfDetector[2];
/** online flag - is set if the detector is connected, unset if socket /** online flag - is set if the detector is connected, unset if socket
* connection is not possible */ * connection is not possible */
int onlineFlag; int onlineFlag;
/** stopped flag - is set if an acquisition error occurs or the detector /** stopped flag - is set if an acquisition error occurs or the detector
* is stopped manually. Is reset to 0 at the start of the acquisition */ * is stopped manually. Is reset to 0 at the start of the acquisition */
int stoppedFlag; int stoppedFlag;
/** size of the data that are transfered from all detectors */ /** size of the data that are transfered from all detectors */
int dataBytes; int dataBytes;
/** data bytes including gap pixels transferred from all detectors */ /** data bytes including gap pixels transferred from all detectors */
int dataBytesInclGapPixels; int dataBytesInclGapPixels;
/** total number of channels for all detectors */ /** total number of channels for all detectors */
int numberOfChannels; int numberOfChannels;
/** total number of channels for all detectors in one dimension*/ /** total number of channels for all detectors in one dimension*/
int numberOfChannel[2]; int numberOfChannel[2];
/** total number of channels including gap pixels in one dimension */ /** total number of channels including gap pixels in one dimension */
int numberOfChannelInclGapPixels[2]; int numberOfChannelInclGapPixels[2];
/** total number of channels for all detectors */ /** total number of channels for all detectors */
int maxNumberOfChannels; int maxNumberOfChannels;
/** max number of channels for all detectors in one dimension*/ /** max number of channels for all detectors in one dimension*/
int maxNumberOfChannel[2]; int maxNumberOfChannel[2];
/** max number of channels including gap pixels for all detectors in /** max number of channels including gap pixels for all detectors in
* one dimension*/ * one dimension*/
int maxNumberOfChannelInclGapPixels[2]; int maxNumberOfChannelInclGapPixels[2];
/** max number of channels allowed for the complete set of detectors in /** max number of channels allowed for the complete set of detectors in
* one dimension */ * one dimension */
int maxNumberOfChannelsPerDetector[2]; int maxNumberOfChannelsPerDetector[2];
/** timer values */ /** timer values */
int64_t timerValue[MAX_TIMERS]; int64_t timerValue[slsDetectorDefs::timerIndex::MAX_TIMERS];
/** flag for acquiring */ /** flag for acquiring */
bool acquiringFlag; bool acquiringFlag;
/** receiver online flag - is set if the receiver is connected, /** receiver online flag - is set if the receiver is connected,
* unset if socket connection is not possible */ * unset if socket connection is not possible */
int receiverOnlineFlag; int receiverOnlineFlag;
/** data streaming (up stream) enable in receiver */ /** data streaming (up stream) enable in receiver */
bool receiver_upstream; bool receiver_upstream;
};
} sharedMultiSlsDetector; class multiSlsDetector : public virtual slsDetectorDefs,
public virtual errorDefs {
// private:
public: public:
/** /**
@ -433,7 +432,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
*/ */
int setStopPort(int port_number = -1, int detPos = -1); int setStopPort(int port_number = -1, int detPos = -1);
/** /**
* Set/Gets TCP Port of the receiver * Set/Gets TCP Port of the receiver
* @param port_number (-1 gets) * @param port_number (-1 gets)
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
@ -441,8 +440,6 @@ class multiSlsDetector : public virtual slsDetectorDefs,
*/ */
int setReceiverPort(int port_number = -1, int detPos = -1); int setReceiverPort(int port_number = -1, int detPos = -1);
/** /**
* Lock server for this client IP * Lock server for this client IP
* @param p 0 to unlock, 1 to lock * @param p 0 to unlock, 1 to lock
@ -471,7 +468,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns OK or FAIL * @returns OK or FAIL
*/ */
int execCommand(const std::string& cmd, int detPos); int execCommand(const std::string &cmd, int detPos);
/** /**
* Load configuration from a configuration File * Load configuration from a configuration File
@ -542,7 +539,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the trimbit/settings directory * @returns the trimbit/settings directory
*/ */
std::string setSettingsDir(const std::string& directory, int detPos = -1); std::string setSettingsDir(const std::string &directory, int detPos = -1);
/** /**
* Loads the modules settings/trimbits reading from a specific file * Loads the modules settings/trimbits reading from a specific file
@ -551,7 +548,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* returns OK or FAIL * returns OK or FAIL
*/ */
int loadSettingsFile(const std::string& fname, int detPos = -1); int loadSettingsFile(const std::string &fname, int detPos = -1);
/** /**
* Saves the modules settings/trimbits to a specific file * Saves the modules settings/trimbits to a specific file
@ -560,7 +557,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* returns OK or FAIL * returns OK or FAIL
*/ */
int saveSettingsFile(const std::string& fname, int detPos = -1); int saveSettingsFile(const std::string &fname, int detPos = -1);
/** /**
* Get Detector run status * Get Detector run status
@ -869,7 +866,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the detector MAC address * @returns the detector MAC address
*/ */
std::string setDetectorMAC(const std::string& detectorMAC, int detPos = -1); std::string setDetectorMAC(const std::string &detectorMAC, int detPos = -1);
/** /**
* Returns the detector MAC address * Returns the detector MAC address
@ -884,7 +881,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the detector IP address * @returns the detector IP address
*/ */
std::string setDetectorIP(const std::string& detectorIP, int detPos = -1); std::string setDetectorIP(const std::string &detectorIP, int detPos = -1);
/** /**
* Returns the detector IP address * Returns the detector IP address
@ -901,7 +898,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the receiver IP address from shared memory * @returns the receiver IP address from shared memory
*/ */
std::string setReceiver(const std::string& receiver, int detPos = -1); std::string setReceiver(const std::string &receiver, int detPos = -1);
/** /**
* Returns the receiver IP address * Returns the receiver IP address
@ -916,7 +913,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the receiver UDP IP address * @returns the receiver UDP IP address
*/ */
std::string setReceiverUDPIP(const std::string& udpip, int detPos = -1); std::string setReceiverUDPIP(const std::string &udpip, int detPos = -1);
/** /**
* Returns the receiver UDP IP address * Returns the receiver UDP IP address
@ -931,7 +928,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the receiver UDP MAC address * @returns the receiver UDP MAC address
*/ */
std::string setReceiverUDPMAC(const std::string& udpmac, int detPos = -1); std::string setReceiverUDPMAC(const std::string &udpmac, int detPos = -1);
/** /**
* Returns the receiver UDP MAC address * Returns the receiver UDP MAC address
@ -948,7 +945,6 @@ class multiSlsDetector : public virtual slsDetectorDefs,
*/ */
int setReceiverUDPPort(int udpport, int detPos = -1); int setReceiverUDPPort(int udpport, int detPos = -1);
/** /**
* Returns the receiver UDP port * Returns the receiver UDP port
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
@ -981,13 +977,13 @@ class multiSlsDetector : public virtual slsDetectorDefs,
*/ */
void setClientDataStreamingInPort(int i = -1, int detPos = -1); void setClientDataStreamingInPort(int i = -1, int detPos = -1);
/** /**
* Returns the client zmq port * Returns the client zmq port
* If detPos is -1(multi module), port returns client streaming port of first module * If detPos is -1(multi module), port returns client streaming port of first module
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the client zmq port * @returns the client zmq port
*/ */
int getClientStreamingPort(int detPos = -1); int getClientStreamingPort(int detPos = -1);
/** /**
* (advanced users) * (advanced users)
@ -999,13 +995,13 @@ class multiSlsDetector : public virtual slsDetectorDefs,
*/ */
void setReceiverDataStreamingOutPort(int i = -1, int detPos = -1); void setReceiverDataStreamingOutPort(int i = -1, int detPos = -1);
/** /**
* Returns the receiver zmq port * Returns the receiver zmq port
* If detPos is -1(multi module), port returns receiver streaming port of first module * If detPos is -1(multi module), port returns receiver streaming port of first module
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the receiver zmq port * @returns the receiver zmq port
*/ */
int getReceiverStreamingPort(int detPos = -1); int getReceiverStreamingPort(int detPos = -1);
/** /**
* (advanced users) * (advanced users)
@ -1014,8 +1010,8 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* By default, it is the IP of receiver hostname * By default, it is the IP of receiver hostname
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
*/ */
void setClientDataStreamingInIP(const std::string& ip = "", void setClientDataStreamingInIP(const std::string &ip = "",
int detPos = -1); int detPos = -1);
/** /**
* Returns the client zmq ip * Returns the client zmq ip
@ -1023,7 +1019,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the client zmq ip * @returns the client zmq ip
*/ */
std::string getClientStreamingIP(int detPos = -1); std::string getClientStreamingIP(int detPos = -1);
/** /**
* (advanced users) * (advanced users)
@ -1032,8 +1028,8 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* By default, it is the IP of receiver hostname * By default, it is the IP of receiver hostname
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
*/ */
void setReceiverDataStreamingOutIP(const std::string& ip = "", void setReceiverDataStreamingOutIP(const std::string &ip = "",
int detPos = -1); int detPos = -1);
/** /**
* Returns the receiver zmq ip * Returns the receiver zmq ip
@ -1041,7 +1037,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the receiver zmq ip * @returns the receiver zmq ip
*/ */
std::string getReceiverStreamingIP(int detPos = -1); std::string getReceiverStreamingIP(int detPos = -1);
/** /**
* Sets the transmission delay for left, right or entire frame * Sets the transmission delay for left, right or entire frame
@ -1059,7 +1055,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns additional json header, default is empty * @returns additional json header, default is empty
*/ */
std::string setAdditionalJsonHeader(const std::string& jsonheader, int detPos = -1); std::string setAdditionalJsonHeader(const std::string &jsonheader, int detPos = -1);
/** /**
* Returns the additional json header * Returns the additional json header
@ -1076,7 +1072,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @returns the additional json header parameter value, * @returns the additional json header parameter value,
* empty if no parameter found in additional json header * empty if no parameter found in additional json header
*/ */
std::string setAdditionalJsonParameter(const std::string& key, const std::string& value, int detPos = -1); std::string setAdditionalJsonParameter(const std::string &key, const std::string &value, int detPos = -1);
/** /**
* Returns the additional json header parameter value * Returns the additional json header parameter value
@ -1085,7 +1081,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @returns the additional json header parameter value, * @returns the additional json header parameter value,
* empty if no parameter found in additional json header * empty if no parameter found in additional json header
*/ */
std::string getAdditionalJsonParameter(const std::string& key, int detPos = -1); std::string getAdditionalJsonParameter(const std::string &key, int detPos = -1);
/** /**
* Sets the detector minimum/maximum energy threshold in processor (for Moench only) * Sets the detector minimum/maximum energy threshold in processor (for Moench only)
@ -1117,14 +1113,14 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns receiver udp socket buffer size * @returns receiver udp socket buffer size
*/ */
uint64_t setReceiverUDPSocketBufferSize(uint64_t udpsockbufsize=-1, int detPos = -1); uint64_t setReceiverUDPSocketBufferSize(uint64_t udpsockbufsize = -1, int detPos = -1);
/** /**
* Returns the receiver UDP socket buffer size * Returns the receiver UDP socket buffer size
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns the receiver UDP socket buffer size * @returns the receiver UDP socket buffer size
*/ */
uint64_t getReceiverUDPSocketBufferSize(int detPos = -1) ; uint64_t getReceiverUDPSocketBufferSize(int detPos = -1);
/** /**
* Returns the receiver real UDP socket buffer size * Returns the receiver real UDP socket buffer size
@ -1360,7 +1356,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns OK or FAIL * @returns OK or FAIL
*/ */
int programFPGA(const std::string& fname, int detPos = -1); int programFPGA(const std::string &fname, int detPos = -1);
/** /**
* Resets FPGA (Jungfrau) * Resets FPGA (Jungfrau)
@ -1461,7 +1457,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns OK or FAIL * @returns OK or FAIL
*/ */
int execReceiverCommand(const std::string& cmd, int detPos = -1); int execReceiverCommand(const std::string &cmd, int detPos = -1);
/** /**
* Returns output file directory * Returns output file directory
@ -1476,7 +1472,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param s file directory * @param s file directory
* @returns file dir * @returns file dir
*/ */
std::string setFilePath(const std::string& path, int detPos = -1); std::string setFilePath(const std::string &path, int detPos = -1);
/** /**
* Returns file name prefix * Returns file name prefix
@ -1491,7 +1487,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param s file name prefix * @param s file name prefix
* @returns file name prefix * @returns file name prefix
*/ */
std::string setFileName(const std::string& fname, int detPos = -1); std::string setFileName(const std::string &fname, int detPos = -1);
/** /**
* Sets the max frames per file in receiver * Sets the max frames per file in receiver
@ -1693,7 +1689,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @param detPos -1 for all detectors in list or specific detector position * @param detPos -1 for all detectors in list or specific detector position
* @returns OK/FAIL * @returns OK/FAIL
*/ */
int setPattern(const std::string& fname, int detPos = -1); int setPattern(const std::string &fname, int detPos = -1);
/** /**
* Writes a pattern word to the CTB * Writes a pattern word to the CTB
@ -1715,7 +1711,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* @returns OK/FAIL * @returns OK/FAIL
*/ */
int setPatternLoops(int level, int &start, int &stop, int &n, int setPatternLoops(int level, int &start, int &stop, int &n,
int detPos = -1); int detPos = -1);
/** /**
* Sets the wait address in the CTB * Sets the wait address in the CTB
@ -1897,7 +1893,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
* Add sls detector * Add sls detector
* @param s hostname of the single detector * @param s hostname of the single detector
*/ */
void addSlsDetector(const std::string& hostname); void addSlsDetector(const std::string &hostname);
/** /**
* add gap pixels to the image (only for Eiger in 4 bit mode) * add gap pixels to the image (only for Eiger in 4 bit mode)
@ -1963,16 +1959,13 @@ class multiSlsDetector : public virtual slsDetectorDefs,
int detId; int detId;
/** Shared Memory object */ /** Shared Memory object */
SharedMemory *sharedMemory {nullptr}; SharedMemory<sharedMultiSlsDetector> sharedMemory{0, -1};
/** Shared memory structure */
sharedMultiSlsDetector *thisMultiDetector {nullptr};
/** pointers to the slsDetector structures */ /** pointers to the slsDetector structures */
std::vector<std::unique_ptr<slsDetector>> detectors; std::vector<std::unique_ptr<slsDetector>> detectors;
/** data streaming (down stream) enabled in client (zmq sckets created) */ /** data streaming (down stream) enabled in client (zmq sckets created) */
bool client_downstream {false}; bool client_downstream{false};
/** ZMQ Socket - Receiver to Client */ /** ZMQ Socket - Receiver to Client */
std::vector<std::unique_ptr<ZmqSocket>> zmqSocket; std::vector<std::unique_ptr<ZmqSocket>> zmqSocket;
@ -1986,10 +1979,10 @@ class multiSlsDetector : public virtual slsDetectorDefs,
sem_t sem_endRTAcquisition; sem_t sem_endRTAcquisition;
/** Total number of frames/images for next acquisition */ /** Total number of frames/images for next acquisition */
int totalProgress {0}; int totalProgress{0};
/** Current progress or frames/images processed in current acquisition */ /** Current progress or frames/images processed in current acquisition */
int progressIndex {0}; int progressIndex{0};
/** mutex to synchronize main and data processing threads */ /** mutex to synchronize main and data processing threads */
mutable std::mutex mp; mutable std::mutex mp;
@ -1998,26 +1991,26 @@ class multiSlsDetector : public virtual slsDetectorDefs,
mutable std::mutex mg; mutable std::mutex mg;
/** sets when the acquisition is finished */ /** sets when the acquisition is finished */
bool jointhread {false}; bool jointhread{false};
/** the data processing thread */ /** the data processing thread */
// pthread_t dataProcessingThread; // pthread_t dataProcessingThread;
std::thread dataProcessingThread; std::thread dataProcessingThread;
/** detector data packed for the gui */ /** detector data packed for the gui */
detectorData *thisData {nullptr}; detectorData *thisData{nullptr};
int (*acquisition_finished)(double, int, void *) {nullptr}; int (*acquisition_finished)(double, int, void *){nullptr};
void *acqFinished_p {nullptr}; void *acqFinished_p{nullptr};
int (*measurement_finished)(int, int, void *) {nullptr}; int (*measurement_finished)(int, int, void *){nullptr};
void *measFinished_p {nullptr}; void *measFinished_p{nullptr};
int (*progress_call)(double, void *); int (*progress_call)(double, void *);
void *pProgressCallArg {nullptr}; void *pProgressCallArg{nullptr};
int (*dataReady)(detectorData *, int, int, void *){nullptr}; int (*dataReady)(detectorData *, int, int, void *){nullptr};
void *pCallbackArg {nullptr}; void *pCallbackArg{nullptr};
}; };
#endif #endif

View File

@ -1,165 +0,0 @@
#include "SharedMemory.h"
#include "sls_detector_exceptions.h"
#include "ansi.h"
#include "logger.h"
#include <iostream>
#include <stdio.h> // printf
#include <cerrno> // errno
#include <cstring> // strerror
#include <unistd.h>
#include <fcntl.h> // O_CREAT, O_TRUNC..
#include <sys/stat.h> // fstat
#include <sys/mman.h> // shared memory
#include <sstream>
#include "stdlib.h"
#define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_"
#define SHM_SLS_PREFIX "_sls_"
#define SHM_ENV_NAME "SLSDETNAME"
SharedMemory::SharedMemory(int multiId, int slsId):
fd(-1),
shmSize(0)
{
name = ConstructSharedMemoryName(multiId, slsId);
}
SharedMemory::~SharedMemory(){
if (fd >= 0)
close(fd);
}
bool SharedMemory::IsExisting() {
bool ret = true;
int tempfd = shm_open(name.c_str(), O_RDWR, 0);
if ((tempfd < 0) && (errno == ENOENT)) {
ret = false;
}
close(tempfd);
return ret;
}
std::string SharedMemory::GetName() {
return name;
}
void* SharedMemory::CreateSharedMemory(size_t sz){
// create
fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno);
throw SharedMemoryException();
}
// resize
if (ftruncate(fd, sz) < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
close(fd);
RemoveSharedMemory();
throw SharedMemoryException();
}
// map
void* addr = MapSharedMemory(sz);
FILE_LOG(logINFO) << "Shared memory created " << name;
return addr;
}
void* SharedMemory::OpenSharedMemory(size_t sz){
// open
fd = shm_open(name.c_str(), O_RDWR, 0);
if (fd < 0) {
FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno);
throw SharedMemoryException();
}
return MapSharedMemory(sz);
}
void SharedMemory::UnmapSharedMemory(void* addr) {
if (munmap(addr, shmSize) < 0) {
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
close(fd);
throw SharedMemoryException();
}
}
void SharedMemory::RemoveSharedMemory() {
if (shm_unlink(name.c_str()) < 0) {
// silent exit if shm did not exist anyway
if (errno == ENOENT)
return;
FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno);
throw SharedMemoryException();
}
FILE_LOG(logINFO) << "Shared memory deleted " << name;
}
void* SharedMemory::MapSharedMemory(size_t sz) {
void* addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno);
close(fd);
throw SharedMemoryException();
}
shmSize = sz;
close(fd);
return addr;
}
std::string SharedMemory::ConstructSharedMemoryName(int multiId, int slsId) {
// using environment path
std::string sEnvPath = "";
char* envpath = getenv(SHM_ENV_NAME);
if (envpath != nullptr) {
sEnvPath.assign(envpath);
sEnvPath.insert(0,"_");
}
std::stringstream ss;
if (slsId < 0)
ss << SHM_MULTI_PREFIX << multiId << sEnvPath;
else
ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath;
std::string temp = ss.str();
if (temp.length() > NAME_MAX) {
FILE_LOG(logERROR) << "Shared memory initialization failed. " <<
temp << " has " << temp.length() << " characters. \n"
"Maximum is " << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME;
throw SharedMemoryException();
}
return temp;
}
int SharedMemory::VerifySizeMatch(size_t expectedSize) {
struct stat sb;
// could not fstat
if (fstat(fd, &sb) < 0) {
FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match "
"(could not fstat): " << strerror(errno);
close(fd);
throw SharedMemoryException();
}
//size does not match
long unsigned int sz = (long unsigned int)sb.st_size;
if (sz != expectedSize) {
FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match";
FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz;
throw SharedMemoryException();
return 1;
}
return 0;
}

View File

@ -8,68 +8,210 @@
*@short functions basic implemenation of shared memory *@short functions basic implemenation of shared memory
*/ */
#include "ansi.h"
#include "logger.h"
#include "sls_detector_exceptions.h"
#include "stdlib.h"
#include <cerrno> // errno
#include <cstring> // strerror
#include <fcntl.h> // O_CREAT, O_TRUNC..
#include <iostream>
#include <sstream>
#include <stdio.h> // printf
#include <sys/mman.h> // shared memory
#include <sys/stat.h> // fstat
#include <unistd.h>
#define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_"
#define SHM_SLS_PREFIX "_sls_"
#define SHM_ENV_NAME "SLSDETNAME"
#include <iostream> #include <iostream>
#include <string> #include <string>
class SharedMemory{ template <typename T>
public: class SharedMemory {
/** public:
/**
* Constructor * Constructor
* creates the single/multi detector shared memory name * creates the single/multi detector shared memory name
* @param multiId multi detector id * @param multiId multi detector id
* @param slsId sls detector id, -1 if a multi detector shared memory * @param slsId sls detector id, -1 if a multi detector shared memory
*/ */
SharedMemory(int multiId, int slsId); SharedMemory(int multiId, int slsId) {
name = ConstructSharedMemoryName(multiId, slsId);
}
/** /**
* Destructor * Delete the copy constructor and copy assignment since we don't want two
*/ * objects managing the same resource
~SharedMemory(); */
SharedMemory(const SharedMemory &) = delete;
SharedMemory &operator=(const SharedMemory &other) = delete;
//Move constructor
SharedMemory(SharedMemory &&other) : name(other.name),
fd(other.fd),
shmSize(other.shmSize),
shared_struct(other.shared_struct) {
other.fd = -1;
other.shared_struct = nullptr;
other.shmSize = 0;
}
//Move assignment
SharedMemory &operator=(SharedMemory &&other) {
name = other.name;
if (fd) {
close(fd);
}
fd = other.fd;
other.fd = -1;
if (shared_struct != nullptr) {
UnmapSharedMemory();
}
shared_struct = other.shared_struct;
other.shared_struct = nullptr;
shmSize = other.shmSize;
other.shmSize = 0;
return *this;
}
~SharedMemory() {
if (fd >= 0)
close(fd);
if (shared_struct) {
UnmapSharedMemory();
}
}
/** /**
* Verify if it exists * Verify if it exists
* @param name of shared memory * @param name of shared memory
* @return true if exists, else false * @return true if exists, else false
*/ */
bool IsExisting(); bool IsExisting() {
bool ret = true;
int tempfd = shm_open(name.c_str(), O_RDWR, 0);
if ((tempfd < 0) && (errno == ENOENT)) {
ret = false;
}
close(tempfd);
return ret;
}
/** /**
* Get shared memory name * Get shared memory name
*/ */
std::string GetName(); std::string GetName() const {
return name;
}
size_t size() const {
return shmSize;
}
/** /**
* Create Shared memory and call MapSharedMemory to map it to an address * Create Shared memory and call MapSharedMemory to map it to an address
* throws a SharedMemoryException exception on failure to create, ftruncate or map * throws a SharedMemoryException exception on failure to create, ftruncate or map
* @param sz of shared memory * @param sz of shared memory
*/ */
void* CreateSharedMemory(size_t sz); void CreateSharedMemory(size_t sz = 0) {
if (sz == 0) {
sz = sizeof(T);
}
fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno);
throw SharedMemoryException();
}
if (ftruncate(fd, sz) < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
close(fd);
RemoveSharedMemory();
throw SharedMemoryException();
}
shared_struct = MapSharedMemory(sz);
FILE_LOG(logINFO) << "Shared memory created " << name;
}
/** /**
* Open existing Shared memory and call MapSharedMemory to map it to an address * Open existing Shared memory and call MapSharedMemory to map it to an address
* throws a SharedMemoryException exception on failure to open or map * throws a SharedMemoryException exception on failure to open or map
* @param sz of shared memory * @param sz of shared memory
*/ */
void* OpenSharedMemory(size_t sz); void OpenSharedMemory(size_t sz = 0) {
if (sz == 0) {
sz = sizeof(T);
}
fd = shm_open(name.c_str(), O_RDWR, 0);
if (fd < 0) {
FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno);
throw SharedMemoryException();
}
shared_struct = MapSharedMemory(sz);
}
/** /**
* Unmap shared memory from an address * Unmap shared memory from an address
* throws a SharedMemoryException exception on failure * throws a SharedMemoryException exception on failure
* @param addr double pointer to address to be mapped
*/ */
void UnmapSharedMemory(void* addr); void UnmapSharedMemory() {
if (shared_struct != nullptr) {
if (munmap(shared_struct, shmSize) < 0) {
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
close(fd);
throw SharedMemoryException();
}
shared_struct = nullptr;
}
}
/** /**
* Remove existing Shared memory * Remove existing Shared memory
*/ */
void RemoveSharedMemory(); void RemoveSharedMemory() {
UnmapSharedMemory();
if (shm_unlink(name.c_str()) < 0) {
// silent exit if shm did not exist anyway
if (errno == ENOENT)
return;
FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno);
throw SharedMemoryException();
}
FILE_LOG(logINFO) << "Shared memory deleted " << name;
}
/** /**
* Maximum length of name as from man pages * Maximum length of name as from man pages
*/ */
static const int NAME_MAX = 255; static const int NAME_MAX = 255;
private: /**
*Using the call operator to access the pointer
*/
T *operator()() {
return shared_struct;
}
/**
*Using the call operator to access the pointer, const overload
*/
const T *operator()() const {
return shared_struct;
}
private:
/** /**
* Create Shared memory name * Create Shared memory name
* throws exception if name created is longer than required 255(manpages) * throws exception if name created is longer than required 255(manpages)
@ -77,29 +219,85 @@ private:
* @param slsId sls detector id, -1 if a multi detector shared memory * @param slsId sls detector id, -1 if a multi detector shared memory
* @returns shared memory name * @returns shared memory name
*/ */
std::string ConstructSharedMemoryName(int multiId, int slsId); std::string ConstructSharedMemoryName(int multiId, int slsId) {
// using environment path
std::string sEnvPath = "";
char *envpath = getenv(SHM_ENV_NAME);
if (envpath != nullptr) {
sEnvPath.assign(envpath);
sEnvPath.insert(0, "_");
}
std::stringstream ss;
if (slsId < 0)
ss << SHM_MULTI_PREFIX << multiId << sEnvPath;
else
ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath;
std::string temp = ss.str();
if (temp.length() > NAME_MAX) {
FILE_LOG(logERROR) << "Shared memory initialization failed. " << temp << " has " << temp.length() << " characters. \n"
"Maximum is "
<< NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME;
throw SharedMemoryException();
}
return temp;
}
/** /**
* Map shared memory to an address * Map shared memory to an address
* throws a SharedMemoryException exception on failure * throws a SharedMemoryException exception on failure
* @param sz of shared memory * @param sz of shared memory
*/ */
void* MapSharedMemory(size_t sz);
T *MapSharedMemory(size_t sz) {
void *addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno);
close(fd);
throw SharedMemoryException();
}
shmSize = sz;
close(fd);
return (T *)addr;
}
/** /**
* Verify if existing shared memory size matches expected size * Verify if existing shared memory size matches expected size
* @param expectedSize expected size of shared memory, replaced with smaller size if size does not match * @param expectedSize expected size of shared memory, replaced with smaller size if size does not match
* @return 0 for success, 1 for fail * @return 0 for success, 1 for fail
*/ */
int VerifySizeMatch(size_t expectedSize); int VerifySizeMatch(size_t expectedSize) {
struct stat sb;
// could not fstat
if (fstat(fd, &sb) < 0) {
FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match "
"(could not fstat): "
<< strerror(errno);
close(fd);
throw SharedMemoryException();
}
/** Shared memory name */ //size does not match
std::string name; long unsigned int sz = (long unsigned int)sb.st_size;
if (sz != expectedSize) {
FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match";
FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz;
throw SharedMemoryException();
return 1;
}
return 0;
}
/** File descriptor */ /** Shared memory name */
int fd; std::string name;
/** shm size */ /** File descriptor */
size_t shmSize; int fd{-1};
/** shm size */
size_t shmSize{0};
T *shared_struct{nullptr};
}; };

View File

@ -31,7 +31,7 @@ slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify)
* so sls shared memory will be created */ * so sls shared memory will be created */
// ensure shared memory was not created before // ensure shared memory was not created before
auto shm = SharedMemory(multiId, id); SharedMemory<sharedSlsDetector> shm(multiId, id);
if (shm.IsExisting()) { if (shm.IsExisting()) {
FILE_LOG(logWARNING) << "This shared memory should have been " FILE_LOG(logWARNING) << "This shared memory should have been "
"deleted before! " "deleted before! "
@ -59,7 +59,7 @@ slsDetector::slsDetector(int multiId, int id, bool verify)
slsDetector::~slsDetector() { slsDetector::~slsDetector() {
if (sharedMemory) { if (sharedMemory) {
sharedMemory->UnmapSharedMemory(thisDetector); sharedMemory->UnmapSharedMemory();
delete sharedMemory; delete sharedMemory;
} }
} }
@ -202,13 +202,13 @@ int64_t slsDetector::getId(idMode mode) {
} }
void slsDetector::freeSharedMemory(int multiId, int slsId) { void slsDetector::freeSharedMemory(int multiId, int slsId) {
auto shm = SharedMemory(multiId, slsId); SharedMemory<sharedSlsDetector> shm(multiId, slsId);
shm.RemoveSharedMemory(); shm.RemoveSharedMemory();
} }
void slsDetector::freeSharedMemory() { void slsDetector::freeSharedMemory() {
if (sharedMemory) { if (sharedMemory) {
sharedMemory->UnmapSharedMemory(thisDetector); sharedMemory->UnmapSharedMemory();
sharedMemory->RemoveSharedMemory(); sharedMemory->RemoveSharedMemory();
delete sharedMemory; delete sharedMemory;
sharedMemory = nullptr; sharedMemory = nullptr;
@ -236,15 +236,17 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId,
int sz = calculateSharedMemorySize(type); int sz = calculateSharedMemorySize(type);
// shared memory object with name // shared memory object with name
sharedMemory = new SharedMemory(multiId, detId); sharedMemory = new SharedMemory<sharedSlsDetector>(multiId, detId);
// create // create
if (created) { if (created) {
thisDetector = (sharedSlsDetector *)sharedMemory->CreateSharedMemory(sz); sharedMemory->CreateSharedMemory(sz);
thisDetector = (*sharedMemory)();
} }
// open and verify version // open and verify version
else { else {
thisDetector = (sharedSlsDetector *)sharedMemory->OpenSharedMemory(sz); sharedMemory->OpenSharedMemory(sz);
thisDetector = (*sharedMemory)();
if (verify && thisDetector->shmversion != SLS_SHMVERSION) { if (verify && thisDetector->shmversion != SLS_SHMVERSION) {
FILE_LOG(logERROR) << "Single shared memory " FILE_LOG(logERROR) << "Single shared memory "
"(" "("
@ -259,7 +261,7 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId,
if (sharedMemory) { if (sharedMemory) {
// unmap // unmap
if (thisDetector) { if (thisDetector) {
sharedMemory->UnmapSharedMemory(thisDetector); sharedMemory->UnmapSharedMemory();
thisDetector = nullptr; thisDetector = nullptr;
} }
// delete // delete
@ -636,17 +638,16 @@ int slsDetector::receiveModule(sls_detector_module *myMod) {
} }
slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, bool verify) { slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, bool verify) {
auto shm = SharedMemory(multiId, detId); SharedMemory<sharedSlsDetector> shm(multiId, detId);
if (!shm.IsExisting()) { if (!shm.IsExisting()) {
FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n" FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n"
"Corrupted Multi Shared memory. Please free shared memory."; "Corrupted Multi Shared memory. Please free shared memory.";
throw SharedMemoryException(); throw SharedMemoryException();
} }
size_t sz = sizeof(sharedSlsDetector);
// open, map, verify version // open, map, verify version
auto sdet = (sharedSlsDetector *)shm.OpenSharedMemory(sz); shm.OpenSharedMemory();
auto sdet = shm();
if (verify && sdet->shmversion != SLS_SHMVERSION) { if (verify && sdet->shmversion != SLS_SHMVERSION) {
FILE_LOG(logERROR) << "Single shared memory " FILE_LOG(logERROR) << "Single shared memory "
"(" "("
@ -654,13 +655,13 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
"(expected 0x" "(expected 0x"
<< std::hex << SLS_SHMVERSION << " but got 0x" << sdet->shmversion << ")" << std::dec; << std::hex << SLS_SHMVERSION << " but got 0x" << sdet->shmversion << ")" << std::dec;
// unmap and throw // unmap and throw
sharedMemory->UnmapSharedMemory(thisDetector); sharedMemory->UnmapSharedMemory();
throw SharedMemoryException(); throw SharedMemoryException();
} }
// get type, unmap // get type, unmap
auto type = sdet->myDetectorType; auto type = sdet->myDetectorType;
shm.UnmapSharedMemory(sdet); shm.UnmapSharedMemory();
return type; return type;
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
include_directories(
${PROJECT_SOURCE_DIR}/catch
)
set(SOURCES
test.cpp
test-SharedMemory.cpp
)
add_executable(testSlsDetector ${SOURCES})
target_link_libraries(testSlsDetector
slsSupportLib
slsDetectorShared
pthread
rt
)
set_target_properties(testSlsDetector PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
#TODO! Move to automatic test discovery
add_test(test-testSlsDetector ${CMAKE_BINARY_DIR}/bin/testSlsDetector)

View File

@ -0,0 +1,128 @@
#include "SharedMemory.h"
#include "catch.hpp"
#include "string_utils.h"
#include <iostream>
struct Data {
int x;
double y;
char mess[50];
};
TEST_CASE("Create SharedMemory read and write") {
SharedMemory<Data> shm(0, -1);
shm.CreateSharedMemory();
CHECK(shm.GetName() == "/slsDetectorPackage_multi_0");
shm()->x = 3;
shm()->y = 5.7;
sls::strcpy_safe(shm()->mess, "Some string");
CHECK(shm()->x == 3);
CHECK(shm()->y == 5.7);
CHECK(std::string(shm()->mess) == "Some string");
shm.UnmapSharedMemory();
shm.RemoveSharedMemory();
CHECK(shm.IsExisting() == false);
}
TEST_CASE("Open existing SharedMemory and read") {
{
SharedMemory<double> shm(0, -1);
shm.CreateSharedMemory();
*shm() = 5.3;
}
SharedMemory<double> shm2(0, -1);
shm2.OpenSharedMemory();
CHECK(*shm2() == 5.3);
shm2.RemoveSharedMemory();
}
TEST_CASE("Creating a second shared memory with the same name throws") {
SharedMemory<double> shm0(0, -1);
SharedMemory<double> shm1(0, -1);
shm0.CreateSharedMemory();
CHECK_THROWS(shm1.CreateSharedMemory());
shm0.RemoveSharedMemory();
}
TEST_CASE("Open two shared memories to the same place") {
//Create the first shared memory
SharedMemory<Data> shm(0, -1);
shm.CreateSharedMemory();
shm()->x = 5;
CHECK(shm()->x == 5);
//Open the second shared memory with the same name
SharedMemory<Data> shm2(0, -1);
shm2.OpenSharedMemory();
CHECK(shm2()->x == 5);
CHECK(shm.GetName() == shm2.GetName());
//Check that they still point to the same place
shm2()->x = 7;
CHECK(shm()->x == 7);
//Remove only needs to be done once since they refer
//to the same memory
shm2.RemoveSharedMemory();
CHECK(shm.IsExisting() == false);
CHECK(shm2.IsExisting() == false);
}
TEST_CASE("Move SharedMemory"){
SharedMemory<Data> shm(0,-1);
CHECK(shm.GetName() == "/slsDetectorPackage_multi_0");
shm.CreateSharedMemory();
shm()->x = 9;
CHECK(shm.size()== sizeof(Data));
SharedMemory<Data> shm2(1,-1);
shm2 = std::move(shm); //shm is now a moved from object!
CHECK(shm2()->x == 9);
CHECK(shm() == nullptr);
CHECK(shm.size() == 0);
CHECK(shm2.GetName() == "/slsDetectorPackage_multi_0");
shm2.RemoveSharedMemory();
}
TEST_CASE("Create several shared memories") {
constexpr int N = 5;
std::vector<SharedMemory<int>> v;
v.reserve(N);
for (int i = 0; i != N; ++i) {
v.emplace_back(i, -1);
CHECK(v[i].IsExisting() == false);
v[i].CreateSharedMemory();
*v[i]() = i;
CHECK(*v[i]() == i);
}
for (int i = 0; i != N; ++i) {
CHECK(*v[i]() == i);
CHECK(v[i].GetName() == std::string("/slsDetectorPackage_multi_")+std::to_string(i));
}
for (int i = 0; i != N; ++i) {
v[i].RemoveSharedMemory();
CHECK(v[i].IsExisting() == false);
}
}

View File

@ -0,0 +1,3 @@
// tests-main.cpp
#define CATCH_CONFIG_MAIN
#include "catch.hpp"