mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-19 16:27:13 +02:00
default args
This commit is contained in:
@ -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}"
|
||||||
|
@ -259,7 +259,7 @@ void multiSlsDetector::freeSharedMemory(int multiId, int detPos) {
|
|||||||
// sharedMultiSlsDetector *mdet =
|
// sharedMultiSlsDetector *mdet =
|
||||||
// (sharedMultiSlsDetector *)shm.OpenSharedMemory(
|
// (sharedMultiSlsDetector *)shm.OpenSharedMemory(
|
||||||
// sizeof(sharedMultiSlsDetector));
|
// sizeof(sharedMultiSlsDetector));
|
||||||
shm.OpenSharedMemory(sizeof(sharedMultiSlsDetector));
|
shm.OpenSharedMemory();
|
||||||
numDetectors = shm()->numberOfDetectors;
|
numDetectors = shm()->numberOfDetectors;
|
||||||
shm.UnmapSharedMemory();
|
shm.UnmapSharedMemory();
|
||||||
shm.RemoveSharedMemory();
|
shm.RemoveSharedMemory();
|
||||||
@ -328,13 +328,13 @@ void multiSlsDetector::initSharedMemory(bool verify) {
|
|||||||
try {
|
try {
|
||||||
// shared memory object with name
|
// shared memory object with name
|
||||||
sharedMemory = new SharedMemory<sharedMultiSlsDetector>(detId, -1);
|
sharedMemory = new SharedMemory<sharedMultiSlsDetector>(detId, -1);
|
||||||
size_t sz = sizeof(sharedMultiSlsDetector);
|
// size_t sz = sizeof(sharedMultiSlsDetector);
|
||||||
|
|
||||||
// create
|
// create
|
||||||
if (!sharedMemory->IsExisting()) {
|
if (!sharedMemory->IsExisting()) {
|
||||||
// thisMultiDetector =
|
// thisMultiDetector =
|
||||||
// (sharedMultiSlsDetector *)sharedMemory->CreateSharedMemory(sz);
|
// (sharedMultiSlsDetector *)sharedMemory->CreateSharedMemory(sz);
|
||||||
sharedMemory->CreateSharedMemory(sz);
|
sharedMemory->CreateSharedMemory();
|
||||||
thisMultiDetector= (*sharedMemory)(); //TODO remove line
|
thisMultiDetector= (*sharedMemory)(); //TODO remove line
|
||||||
initializeDetectorStructure();
|
initializeDetectorStructure();
|
||||||
}
|
}
|
||||||
@ -342,7 +342,7 @@ void multiSlsDetector::initSharedMemory(bool verify) {
|
|||||||
else {
|
else {
|
||||||
// thisMultiDetector =
|
// thisMultiDetector =
|
||||||
// (sharedMultiSlsDetector *)sharedMemory->OpenSharedMemory(sz);
|
// (sharedMultiSlsDetector *)sharedMemory->OpenSharedMemory(sz);
|
||||||
sharedMemory->OpenSharedMemory(sz);
|
sharedMemory->OpenSharedMemory();
|
||||||
thisMultiDetector = (*sharedMemory)();
|
thisMultiDetector = (*sharedMemory)();
|
||||||
if (verify && thisMultiDetector->shmversion != MULTI_SHMVERSION) {
|
if (verify && thisMultiDetector->shmversion != MULTI_SHMVERSION) {
|
||||||
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
|
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
|
||||||
|
@ -1,178 +1,178 @@
|
|||||||
#include "SharedMemory.h"
|
// #include "SharedMemory.h"
|
||||||
#include "sls_detector_exceptions.h"
|
// #include "sls_detector_exceptions.h"
|
||||||
#include "ansi.h"
|
// #include "ansi.h"
|
||||||
#include "logger.h"
|
// #include "logger.h"
|
||||||
|
|
||||||
#include "slsDetector.h"
|
// #include "slsDetector.h"
|
||||||
#include "multiSlsDetector.h"
|
// #include "multiSlsDetector.h"
|
||||||
|
|
||||||
#include <iostream>
|
// #include <iostream>
|
||||||
#include <stdio.h> // printf
|
// #include <stdio.h> // printf
|
||||||
#include <cerrno> // errno
|
// #include <cerrno> // errno
|
||||||
#include <cstring> // strerror
|
// #include <cstring> // strerror
|
||||||
#include <unistd.h>
|
// #include <unistd.h>
|
||||||
#include <fcntl.h> // O_CREAT, O_TRUNC..
|
// #include <fcntl.h> // O_CREAT, O_TRUNC..
|
||||||
#include <sys/stat.h> // fstat
|
// #include <sys/stat.h> // fstat
|
||||||
#include <sys/mman.h> // shared memory
|
// #include <sys/mman.h> // shared memory
|
||||||
#include <sstream>
|
// #include <sstream>
|
||||||
#include "stdlib.h"
|
// #include "stdlib.h"
|
||||||
|
|
||||||
#define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_"
|
// #define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_"
|
||||||
#define SHM_SLS_PREFIX "_sls_"
|
// #define SHM_SLS_PREFIX "_sls_"
|
||||||
#define SHM_ENV_NAME "SLSDETNAME"
|
// #define SHM_ENV_NAME "SLSDETNAME"
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
SharedMemory<T>::SharedMemory(int multiId, int slsId):
|
// SharedMemory<T>::SharedMemory(int multiId, int slsId):
|
||||||
fd(-1),
|
// fd(-1),
|
||||||
shmSize(0)
|
// shmSize(0)
|
||||||
{
|
// {
|
||||||
name = ConstructSharedMemoryName(multiId, slsId);
|
// name = ConstructSharedMemoryName(multiId, slsId);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
SharedMemory<T>::~SharedMemory(){
|
// SharedMemory<T>::~SharedMemory(){
|
||||||
if (fd >= 0)
|
// if (fd >= 0)
|
||||||
close(fd);
|
// close(fd);
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
bool SharedMemory<T>::IsExisting() {
|
// bool SharedMemory<T>::IsExisting() {
|
||||||
bool ret = true;
|
// bool ret = true;
|
||||||
int tempfd = shm_open(name.c_str(), O_RDWR, 0);
|
// int tempfd = shm_open(name.c_str(), O_RDWR, 0);
|
||||||
if ((tempfd < 0) && (errno == ENOENT)) {
|
// if ((tempfd < 0) && (errno == ENOENT)) {
|
||||||
ret = false;
|
// ret = false;
|
||||||
}
|
// }
|
||||||
close(tempfd);
|
// close(tempfd);
|
||||||
return ret;
|
// return ret;
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
std::string SharedMemory<T>::GetName() {
|
// std::string SharedMemory<T>::GetName() {
|
||||||
return name;
|
// return name;
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
void SharedMemory<T>::CreateSharedMemory(size_t sz){
|
// void SharedMemory<T>::CreateSharedMemory(size_t sz){
|
||||||
// create
|
// // create
|
||||||
fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
// fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||||
if (fd < 0) {
|
// if (fd < 0) {
|
||||||
FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno);
|
// FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno);
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
|
|
||||||
// resize
|
// // resize
|
||||||
if (ftruncate(fd, sz) < 0) {
|
// if (ftruncate(fd, sz) < 0) {
|
||||||
FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
|
// FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
|
||||||
close(fd);
|
// close(fd);
|
||||||
RemoveSharedMemory();
|
// RemoveSharedMemory();
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
|
|
||||||
// map
|
// // map
|
||||||
// void* addr = MapSharedMemory(sz);
|
// // void* addr = MapSharedMemory(sz);
|
||||||
shared_struct = MapSharedMemory(sz);
|
// shared_struct = MapSharedMemory(sz);
|
||||||
FILE_LOG(logINFO) << "Shared memory created " << name;
|
// FILE_LOG(logINFO) << "Shared memory created " << name;
|
||||||
|
|
||||||
// return addr;
|
// // return addr;
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
void SharedMemory<T>::OpenSharedMemory(size_t sz){
|
// void SharedMemory<T>::OpenSharedMemory(size_t sz){
|
||||||
// open
|
// // open
|
||||||
fd = shm_open(name.c_str(), O_RDWR, 0);
|
// fd = shm_open(name.c_str(), O_RDWR, 0);
|
||||||
if (fd < 0) {
|
// if (fd < 0) {
|
||||||
FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno);
|
// FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno);
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
|
|
||||||
shared_struct = MapSharedMemory(sz);
|
// shared_struct = MapSharedMemory(sz);
|
||||||
// return MapSharedMemory(sz);
|
// // return MapSharedMemory(sz);
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
void SharedMemory<T>::UnmapSharedMemory() {
|
// void SharedMemory<T>::UnmapSharedMemory() {
|
||||||
if (munmap(shared_struct, shmSize) < 0) {
|
// if (munmap(shared_struct, shmSize) < 0) {
|
||||||
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
|
// FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
|
||||||
close(fd);
|
// close(fd);
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
void SharedMemory<T>::RemoveSharedMemory() {
|
// void SharedMemory<T>::RemoveSharedMemory() {
|
||||||
if (shm_unlink(name.c_str()) < 0) {
|
// if (shm_unlink(name.c_str()) < 0) {
|
||||||
// silent exit if shm did not exist anyway
|
// // silent exit if shm did not exist anyway
|
||||||
if (errno == ENOENT)
|
// if (errno == ENOENT)
|
||||||
return;
|
// return;
|
||||||
FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno);
|
// FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno);
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
FILE_LOG(logINFO) << "Shared memory deleted " << name;
|
// FILE_LOG(logINFO) << "Shared memory deleted " << name;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
T* SharedMemory<T>::MapSharedMemory(size_t sz) {
|
// T* SharedMemory<T>::MapSharedMemory(size_t sz) {
|
||||||
void* addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
// void* addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
if (addr == MAP_FAILED) {
|
// if (addr == MAP_FAILED) {
|
||||||
FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno);
|
// FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno);
|
||||||
close(fd);
|
// close(fd);
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
shmSize = sz;
|
// shmSize = sz;
|
||||||
close(fd);
|
// close(fd);
|
||||||
return (T*)addr;
|
// return (T*)addr;
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
std::string SharedMemory<T>::ConstructSharedMemoryName(int multiId, int slsId) {
|
// std::string SharedMemory<T>::ConstructSharedMemoryName(int multiId, int slsId) {
|
||||||
|
|
||||||
// using environment path
|
// // using environment path
|
||||||
std::string sEnvPath = "";
|
// std::string sEnvPath = "";
|
||||||
char* envpath = getenv(SHM_ENV_NAME);
|
// char* envpath = getenv(SHM_ENV_NAME);
|
||||||
if (envpath != nullptr) {
|
// if (envpath != nullptr) {
|
||||||
sEnvPath.assign(envpath);
|
// sEnvPath.assign(envpath);
|
||||||
sEnvPath.insert(0,"_");
|
// sEnvPath.insert(0,"_");
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::stringstream ss;
|
// std::stringstream ss;
|
||||||
if (slsId < 0)
|
// if (slsId < 0)
|
||||||
ss << SHM_MULTI_PREFIX << multiId << sEnvPath;
|
// ss << SHM_MULTI_PREFIX << multiId << sEnvPath;
|
||||||
else
|
// else
|
||||||
ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath;
|
// ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath;
|
||||||
|
|
||||||
std::string temp = ss.str();
|
// std::string temp = ss.str();
|
||||||
if (temp.length() > NAME_MAX) {
|
// if (temp.length() > NAME_MAX) {
|
||||||
FILE_LOG(logERROR) << "Shared memory initialization failed. " <<
|
// FILE_LOG(logERROR) << "Shared memory initialization failed. " <<
|
||||||
temp << " has " << temp.length() << " characters. \n"
|
// temp << " has " << temp.length() << " characters. \n"
|
||||||
"Maximum is " << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME;
|
// "Maximum is " << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME;
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
return temp;
|
// return temp;
|
||||||
}
|
// // }
|
||||||
|
|
||||||
template<typename T>
|
// template<typename T>
|
||||||
int SharedMemory<T>::VerifySizeMatch(size_t expectedSize) {
|
// int SharedMemory<T>::VerifySizeMatch(size_t expectedSize) {
|
||||||
struct stat sb;
|
// struct stat sb;
|
||||||
// could not fstat
|
// // could not fstat
|
||||||
if (fstat(fd, &sb) < 0) {
|
// if (fstat(fd, &sb) < 0) {
|
||||||
FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match "
|
// FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match "
|
||||||
"(could not fstat): " << strerror(errno);
|
// "(could not fstat): " << strerror(errno);
|
||||||
close(fd);
|
// close(fd);
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
}
|
// }
|
||||||
|
|
||||||
//size does not match
|
// //size does not match
|
||||||
long unsigned int sz = (long unsigned int)sb.st_size;
|
// long unsigned int sz = (long unsigned int)sb.st_size;
|
||||||
if (sz != expectedSize) {
|
// if (sz != expectedSize) {
|
||||||
FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match";
|
// FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match";
|
||||||
FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz;
|
// FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz;
|
||||||
throw SharedMemoryException();
|
// throw SharedMemoryException();
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
|
|
||||||
template class SharedMemory<sharedSlsDetector>;
|
// template class SharedMemory<sharedSlsDetector>;
|
||||||
template class SharedMemory<sharedMultiSlsDetector>;
|
// template class SharedMemory<sharedMultiSlsDetector>;
|
@ -8,81 +8,168 @@
|
|||||||
*@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>
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class SharedMemory{
|
class SharedMemory {
|
||||||
public:
|
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) : fd(-1),
|
||||||
|
shmSize(0) {
|
||||||
|
name = ConstructSharedMemoryName(multiId, slsId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor
|
* Destructor
|
||||||
*/
|
*/
|
||||||
~SharedMemory();
|
~SharedMemory() {
|
||||||
|
if (fd >= 0)
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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) {
|
||||||
|
// create
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
shared_struct = MapSharedMemory(sz);
|
||||||
|
FILE_LOG(logINFO) << "Shared memory created " << name;
|
||||||
|
|
||||||
|
// return addr;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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() {
|
||||||
|
// open
|
||||||
|
size_t 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);
|
||||||
|
// return 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
|
||||||
*/
|
*/
|
||||||
void UnmapSharedMemory();
|
void UnmapSharedMemory() {
|
||||||
|
if (munmap(shared_struct, shmSize) < 0) {
|
||||||
|
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
|
||||||
|
close(fd);
|
||||||
|
throw SharedMemoryException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove existing Shared memory
|
* Remove existing Shared memory
|
||||||
*/
|
*/
|
||||||
void RemoveSharedMemory();
|
void 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Using the call operator to access the pointer
|
Using the call operator to access the pointer
|
||||||
|
|
||||||
*/
|
*/
|
||||||
T* operator()(){
|
|
||||||
|
T *operator()() {
|
||||||
return shared_struct;
|
return shared_struct;
|
||||||
}
|
}
|
||||||
|
|
||||||
const T* operator()() const{
|
const T *operator()() const {
|
||||||
return shared_struct;
|
return shared_struct;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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)
|
||||||
@ -90,31 +177,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
|
||||||
*/
|
*/
|
||||||
T* 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;
|
||||||
|
|
||||||
T* shared_struct;
|
/** shm size */
|
||||||
|
size_t shmSize;
|
||||||
|
|
||||||
|
T *shared_struct;
|
||||||
};
|
};
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -208,7 +208,7 @@ void slsDetector::freeSharedMemory(int multiId, int slsId) {
|
|||||||
|
|
||||||
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;
|
||||||
@ -240,11 +240,15 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId,
|
|||||||
|
|
||||||
// create
|
// create
|
||||||
if (created) {
|
if (created) {
|
||||||
thisDetector = (sharedSlsDetector *)sharedMemory->CreateSharedMemory(sz);
|
// 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);
|
// thisDetector = (sharedSlsDetector *)sharedMemory->OpenSharedMemory(sz);
|
||||||
|
sharedMemory->OpenSharedMemory();
|
||||||
|
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 +263,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
|
||||||
@ -643,10 +647,10 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
|
|||||||
throw SharedMemoryException();
|
throw SharedMemoryException();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t sz = sizeof(sharedSlsDetector);
|
|
||||||
|
|
||||||
// open, map, verify version
|
// open, map, verify version
|
||||||
auto sdet = (sharedSlsDetector *)shm.OpenSharedMemory(sz);
|
// 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 +658,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
slsDetectorSoftware/tests/CMakeLists.txt
Normal file
23
slsDetectorSoftware/tests/CMakeLists.txt
Normal 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)
|
30
slsDetectorSoftware/tests/test-SharedMemory.cpp
Normal file
30
slsDetectorSoftware/tests/test-SharedMemory.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
#include "catch.hpp"
|
||||||
|
#include "SharedMemory.h"
|
||||||
|
#include "string_utils.h"
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("SharedMemory") {
|
||||||
|
struct Data{
|
||||||
|
int x;
|
||||||
|
double y;
|
||||||
|
char mess[50];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
3
slsDetectorSoftware/tests/test.cpp
Normal file
3
slsDetectorSoftware/tests/test.cpp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// tests-main.cpp
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "catch.hpp"
|
Reference in New Issue
Block a user