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
|
||||
multiSlsDetector/multiSlsDetector.cpp
|
||||
sharedMemory/SharedMemory.cpp
|
||||
# sharedMemory/SharedMemory.cpp
|
||||
slsDetector/slsDetectorUsers.cpp
|
||||
slsDetector/slsDetectorCommand.cpp
|
||||
slsDetector/slsDetector.cpp
|
||||
@ -49,6 +49,10 @@ if(DOXYGEN_FOUND)
|
||||
)
|
||||
endif()
|
||||
|
||||
if (SLS_USE_TESTS)
|
||||
add_subdirectory(tests)
|
||||
endif(SLS_USE_TESTS)
|
||||
|
||||
|
||||
install(TARGETS slsDetectorShared
|
||||
EXPORT "${TARGETS_EXPORT_NAME}"
|
||||
|
@ -259,7 +259,7 @@ void multiSlsDetector::freeSharedMemory(int multiId, int detPos) {
|
||||
// sharedMultiSlsDetector *mdet =
|
||||
// (sharedMultiSlsDetector *)shm.OpenSharedMemory(
|
||||
// sizeof(sharedMultiSlsDetector));
|
||||
shm.OpenSharedMemory(sizeof(sharedMultiSlsDetector));
|
||||
shm.OpenSharedMemory();
|
||||
numDetectors = shm()->numberOfDetectors;
|
||||
shm.UnmapSharedMemory();
|
||||
shm.RemoveSharedMemory();
|
||||
@ -328,13 +328,13 @@ void multiSlsDetector::initSharedMemory(bool verify) {
|
||||
try {
|
||||
// shared memory object with name
|
||||
sharedMemory = new SharedMemory<sharedMultiSlsDetector>(detId, -1);
|
||||
size_t sz = sizeof(sharedMultiSlsDetector);
|
||||
// size_t sz = sizeof(sharedMultiSlsDetector);
|
||||
|
||||
// create
|
||||
if (!sharedMemory->IsExisting()) {
|
||||
// thisMultiDetector =
|
||||
// (sharedMultiSlsDetector *)sharedMemory->CreateSharedMemory(sz);
|
||||
sharedMemory->CreateSharedMemory(sz);
|
||||
sharedMemory->CreateSharedMemory();
|
||||
thisMultiDetector= (*sharedMemory)(); //TODO remove line
|
||||
initializeDetectorStructure();
|
||||
}
|
||||
@ -342,7 +342,7 @@ void multiSlsDetector::initSharedMemory(bool verify) {
|
||||
else {
|
||||
// thisMultiDetector =
|
||||
// (sharedMultiSlsDetector *)sharedMemory->OpenSharedMemory(sz);
|
||||
sharedMemory->OpenSharedMemory(sz);
|
||||
sharedMemory->OpenSharedMemory();
|
||||
thisMultiDetector = (*sharedMemory)();
|
||||
if (verify && thisMultiDetector->shmversion != MULTI_SHMVERSION) {
|
||||
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
|
||||
|
@ -1,178 +1,178 @@
|
||||
#include "SharedMemory.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
#include "ansi.h"
|
||||
#include "logger.h"
|
||||
// #include "SharedMemory.h"
|
||||
// #include "sls_detector_exceptions.h"
|
||||
// #include "ansi.h"
|
||||
// #include "logger.h"
|
||||
|
||||
#include "slsDetector.h"
|
||||
#include "multiSlsDetector.h"
|
||||
// #include "slsDetector.h"
|
||||
// #include "multiSlsDetector.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"
|
||||
// #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"
|
||||
// #define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_"
|
||||
// #define SHM_SLS_PREFIX "_sls_"
|
||||
// #define SHM_ENV_NAME "SLSDETNAME"
|
||||
|
||||
template<typename T>
|
||||
SharedMemory<T>::SharedMemory(int multiId, int slsId):
|
||||
fd(-1),
|
||||
shmSize(0)
|
||||
{
|
||||
name = ConstructSharedMemoryName(multiId, slsId);
|
||||
}
|
||||
// template<typename T>
|
||||
// SharedMemory<T>::SharedMemory(int multiId, int slsId):
|
||||
// fd(-1),
|
||||
// shmSize(0)
|
||||
// {
|
||||
// name = ConstructSharedMemoryName(multiId, slsId);
|
||||
// }
|
||||
|
||||
|
||||
template<typename T>
|
||||
SharedMemory<T>::~SharedMemory(){
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
}
|
||||
// template<typename T>
|
||||
// SharedMemory<T>::~SharedMemory(){
|
||||
// if (fd >= 0)
|
||||
// close(fd);
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
bool SharedMemory<T>::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;
|
||||
}
|
||||
// template<typename T>
|
||||
// bool SharedMemory<T>::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;
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
std::string SharedMemory<T>::GetName() {
|
||||
return name;
|
||||
}
|
||||
// template<typename T>
|
||||
// std::string SharedMemory<T>::GetName() {
|
||||
// return name;
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
void SharedMemory<T>::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();
|
||||
}
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::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();
|
||||
}
|
||||
// // 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;
|
||||
// // map
|
||||
// // void* addr = MapSharedMemory(sz);
|
||||
// shared_struct = MapSharedMemory(sz);
|
||||
// FILE_LOG(logINFO) << "Shared memory created " << name;
|
||||
|
||||
// return addr;
|
||||
}
|
||||
// // return addr;
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
void SharedMemory<T>::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();
|
||||
}
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::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();
|
||||
// }
|
||||
|
||||
shared_struct = MapSharedMemory(sz);
|
||||
// return MapSharedMemory(sz);
|
||||
}
|
||||
// shared_struct = MapSharedMemory(sz);
|
||||
// // return MapSharedMemory(sz);
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
void SharedMemory<T>::UnmapSharedMemory() {
|
||||
if (munmap(shared_struct, shmSize) < 0) {
|
||||
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
|
||||
close(fd);
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
}
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::UnmapSharedMemory() {
|
||||
// if (munmap(shared_struct, shmSize) < 0) {
|
||||
// FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
|
||||
// close(fd);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
void SharedMemory<T>::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;
|
||||
}
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::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;
|
||||
// }
|
||||
|
||||
|
||||
template<typename T>
|
||||
T* SharedMemory<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;
|
||||
}
|
||||
// template<typename T>
|
||||
// T* SharedMemory<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;
|
||||
// }
|
||||
|
||||
template<typename T>
|
||||
std::string SharedMemory<T>::ConstructSharedMemoryName(int multiId, int slsId) {
|
||||
// template<typename T>
|
||||
// std::string SharedMemory<T>::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,"_");
|
||||
}
|
||||
// // 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::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;
|
||||
}
|
||||
// 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;
|
||||
// // }
|
||||
|
||||
template<typename T>
|
||||
int SharedMemory<T>::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();
|
||||
}
|
||||
// template<typename T>
|
||||
// int SharedMemory<T>::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;
|
||||
}
|
||||
// //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;
|
||||
// }
|
||||
|
||||
template class SharedMemory<sharedSlsDetector>;
|
||||
template class SharedMemory<sharedMultiSlsDetector>;
|
||||
// template class SharedMemory<sharedSlsDetector>;
|
||||
// template class SharedMemory<sharedMultiSlsDetector>;
|
@ -8,6 +8,25 @@
|
||||
*@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 <string>
|
||||
|
||||
@ -20,60 +39,128 @@ public:
|
||||
* @param multiId multi detector id
|
||||
* @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
|
||||
*/
|
||||
~SharedMemory();
|
||||
~SharedMemory() {
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if it exists
|
||||
* @param name of shared memory
|
||||
* @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
|
||||
*/
|
||||
std::string GetName();
|
||||
std::string GetName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Shared memory and call MapSharedMemory to map it to an address
|
||||
* throws a SharedMemoryException exception on failure to create, ftruncate or map
|
||||
* @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
|
||||
* throws a SharedMemoryException exception on failure to open or map
|
||||
* @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
|
||||
* 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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
static const int NAME_MAX = 255;
|
||||
|
||||
|
||||
/*
|
||||
Using the call operator to access the pointer
|
||||
|
||||
*/
|
||||
|
||||
T *operator()() {
|
||||
return shared_struct;
|
||||
}
|
||||
@ -90,21 +177,76 @@ private:
|
||||
* @param slsId sls detector id, -1 if a multi detector shared memory
|
||||
* @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
|
||||
* throws a SharedMemoryException exception on failure
|
||||
* @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
|
||||
* @param expectedSize expected size of shared memory, replaced with smaller size if size does not match
|
||||
* @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();
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
/** Shared memory name */
|
||||
std::string name;
|
||||
@ -116,5 +258,4 @@ private:
|
||||
size_t shmSize;
|
||||
|
||||
T *shared_struct;
|
||||
|
||||
};
|
||||
|
@ -59,7 +59,7 @@ slsDetector::slsDetector(int multiId, int id, bool verify)
|
||||
|
||||
slsDetector::~slsDetector() {
|
||||
if (sharedMemory) {
|
||||
sharedMemory->UnmapSharedMemory(thisDetector);
|
||||
sharedMemory->UnmapSharedMemory();
|
||||
delete sharedMemory;
|
||||
}
|
||||
}
|
||||
@ -208,7 +208,7 @@ void slsDetector::freeSharedMemory(int multiId, int slsId) {
|
||||
|
||||
void slsDetector::freeSharedMemory() {
|
||||
if (sharedMemory) {
|
||||
sharedMemory->UnmapSharedMemory(thisDetector);
|
||||
sharedMemory->UnmapSharedMemory();
|
||||
sharedMemory->RemoveSharedMemory();
|
||||
delete sharedMemory;
|
||||
sharedMemory = nullptr;
|
||||
@ -240,11 +240,15 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId,
|
||||
|
||||
// create
|
||||
if (created) {
|
||||
thisDetector = (sharedSlsDetector *)sharedMemory->CreateSharedMemory(sz);
|
||||
// thisDetector = (sharedSlsDetector *)sharedMemory->CreateSharedMemory(sz);
|
||||
sharedMemory->CreateSharedMemory(sz);
|
||||
thisDetector = (*sharedMemory)();
|
||||
}
|
||||
// open and verify version
|
||||
else {
|
||||
thisDetector = (sharedSlsDetector *)sharedMemory->OpenSharedMemory(sz);
|
||||
// thisDetector = (sharedSlsDetector *)sharedMemory->OpenSharedMemory(sz);
|
||||
sharedMemory->OpenSharedMemory();
|
||||
thisDetector = (*sharedMemory)();
|
||||
if (verify && thisDetector->shmversion != SLS_SHMVERSION) {
|
||||
FILE_LOG(logERROR) << "Single shared memory "
|
||||
"("
|
||||
@ -259,7 +263,7 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId,
|
||||
if (sharedMemory) {
|
||||
// unmap
|
||||
if (thisDetector) {
|
||||
sharedMemory->UnmapSharedMemory(thisDetector);
|
||||
sharedMemory->UnmapSharedMemory();
|
||||
thisDetector = nullptr;
|
||||
}
|
||||
// delete
|
||||
@ -643,10 +647,10 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
size_t sz = sizeof(sharedSlsDetector);
|
||||
|
||||
// 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) {
|
||||
FILE_LOG(logERROR) << "Single shared memory "
|
||||
"("
|
||||
@ -654,13 +658,13 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
|
||||
"(expected 0x"
|
||||
<< std::hex << SLS_SHMVERSION << " but got 0x" << sdet->shmversion << ")" << std::dec;
|
||||
// unmap and throw
|
||||
sharedMemory->UnmapSharedMemory(thisDetector);
|
||||
sharedMemory->UnmapSharedMemory();
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
// get type, unmap
|
||||
auto type = sdet->myDetectorType;
|
||||
shm.UnmapSharedMemory(sdet);
|
||||
shm.UnmapSharedMemory();
|
||||
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