CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
edm::shared_memory::ControllerChannel Class Reference

#include <ControllerChannel.h>

Classes

struct  CheckWorkerStatus
 

Public Member Functions

 ControllerChannel (std::string const &iName, int iID, unsigned int iMaxWaitInSeconds)
 
 ControllerChannel (const ControllerChannel &)=delete
 
 ControllerChannel (ControllerChannel &&)=delete
 
template<typename F >
bool doTransition (F &&iF, edm::Transition iTrans, unsigned long long iTransitionID)
 
template<typename F , typename FRETRY >
bool doTransitionWithRetry (F &&iF, FRETRY &&iRetry, edm::Transition iTrans, unsigned long long iTransitionID)
 
BufferInfofromWorkerBufferInfo ()
 This can be used with ReadBuffer to keep Controller and Worker in sync. More...
 
unsigned int maxWaitInSeconds () const
 
const ControllerChanneloperator= (const ControllerChannel &)=delete
 
const ControllerChanneloperator= (ControllerChannel &&)=delete
 
template<typename F >
void setupWorker (F &&iF)
 
template<typename F , typename FRETRY >
void setupWorkerWithRetry (F &&iF, FRETRY &&iRetry)
 
std::string const & sharedMemoryName () const
 
bool shouldKeepEvent () const
 
void stopWorker ()
 
BufferInfotoWorkerBufferInfo ()
 This can be used with WriteBuffer to keep Controller and Worker in sync. More...
 
std::string uniqueID () const
 
 ~ControllerChannel ()
 

Private Member Functions

bool continueWait (boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock)
 
CheckWorkerStatus initCheckWorkerStatus (unsigned long long *iPtr) const
 
std::string uniqueName (std::string iBase) const
 
bool wait (boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
 
bool wait (boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock)
 

Static Private Member Functions

static BufferInfobufferInfo (const char *iWhich, boost::interprocess::managed_shared_memory &mem)
 

Private Attributes

boost::interprocess::named_condition cndFromMain_
 
boost::interprocess::named_condition cndToMain_
 
BufferInfofromWorkerBufferInfo_
 
int id_
 
bool * keepEvent_
 
boost::interprocess::managed_shared_memory managed_sm_
 
unsigned int maxWaitInSeconds_
 
boost::interprocess::named_mutex mutex_
 
std::string smName_
 
bool * stop_
 
BufferInfotoWorkerBufferInfo_
 
unsigned long long * transitionID_
 
edm::TransitiontransitionType_
 

Detailed Description

Definition at line 37 of file ControllerChannel.h.

Constructor & Destructor Documentation

ControllerChannel::ControllerChannel ( std::string const &  iName,
int  iID,
unsigned int  iMaxWaitInSeconds 
)

iName is used as the base for the shared memory name. The full name uses iID as well as getpid() to create the value sharedMemoryName(). iID allows multiple ControllChannels to use the same base name iName.

Definition at line 34 of file ControllerChannel.cc.

References bufferInfo(), cndFromMain_, cndToMain_, fromWorkerBufferInfo_, edm::shared_memory::channel_names::kConditionFromMain, edm::shared_memory::channel_names::kConditionToMain, keepEvent_, edm::shared_memory::channel_names::kFromWorkerBufferInfo, edm::shared_memory::channel_names::kKeepEvent, edm::shared_memory::channel_names::kMutex, edm::shared_memory::channel_names::kStop, edm::shared_memory::channel_names::kToWorkerBufferInfo, edm::shared_memory::channel_names::kTransitionID, edm::shared_memory::channel_names::kTransitionType, managed_sm_, maxWaitInSeconds_, mutex_, edm::NumberOfTransitions, smName_, stop_, toWorkerBufferInfo_, transitionID_, transitionType_, and uniqueName().

35  : id_{id},
36  maxWaitInSeconds_{iMaxWaitInSeconds},
37  smName_{uniqueName(iName)},
38  managed_sm_{open_or_create, smName_.c_str(), 1024},
41  mutex_{open_or_create, uniqueName(channel_names::kMutex).c_str()},
43  cndToMain_{open_or_create, uniqueName(channel_names::kConditionToMain).c_str()} {
44  managed_sm_.destroy<bool>(channel_names::kStop);
45  stop_ = managed_sm_.construct<bool>(channel_names::kStop)(false);
46  assert(stop_);
47  keepEvent_ = managed_sm_.construct<bool>(channel_names::kKeepEvent)(true);
48  assert(keepEvent_);
49 
53  assert(transitionType_);
54 
55  managed_sm_.destroy<unsigned long long>(channel_names::kTransitionID);
56  transitionID_ = managed_sm_.construct<unsigned long long>(channel_names::kTransitionID)(0);
57  assert(transitionID_);
58 }
constexpr char const *const kConditionToMain
Definition: channel_names.h:33
boost::interprocess::named_condition cndToMain_
constexpr char const *const kConditionFromMain
Definition: channel_names.h:32
constexpr char const *const kStop
Definition: channel_names.h:34
constexpr char const *const kTransitionType
Definition: channel_names.h:36
boost::interprocess::named_mutex mutex_
constexpr char const *const kMutex
Definition: channel_names.h:31
Transition
Definition: Transition.h:12
constexpr char const *const kKeepEvent
Definition: channel_names.h:35
boost::interprocess::named_condition cndFromMain_
constexpr char const *const kFromWorkerBufferInfo
Definition: channel_names.h:30
constexpr char const *const kTransitionID
Definition: channel_names.h:37
constexpr char const *const kToWorkerBufferInfo
Definition: channel_names.h:29
boost::interprocess::managed_shared_memory managed_sm_
std::string uniqueName(std::string iBase) const
static BufferInfo * bufferInfo(const char *iWhich, boost::interprocess::managed_shared_memory &mem)
ControllerChannel::~ControllerChannel ( )

Definition at line 60 of file ControllerChannel.cc.

References edm::shared_memory::channel_names::kConditionFromMain, edm::shared_memory::channel_names::kConditionToMain, edm::shared_memory::channel_names::kFromWorkerBufferInfo, edm::shared_memory::channel_names::kKeepEvent, edm::shared_memory::channel_names::kMutex, edm::shared_memory::channel_names::kStop, edm::shared_memory::channel_names::kToWorkerBufferInfo, edm::shared_memory::channel_names::kTransitionID, edm::shared_memory::channel_names::kTransitionType, managed_sm_, MatrixUtil::remove(), and uniqueName().

60  {
62  managed_sm_.destroy<bool>(channel_names::kStop);
63  managed_sm_.destroy<unsigned int>(channel_names::kTransitionType);
64  managed_sm_.destroy<unsigned long long>(channel_names::kTransitionID);
67 
71 }
constexpr char const *const kConditionToMain
Definition: channel_names.h:33
constexpr char const *const kConditionFromMain
Definition: channel_names.h:32
constexpr char const *const kStop
Definition: channel_names.h:34
constexpr char const *const kTransitionType
Definition: channel_names.h:36
constexpr char const *const kMutex
Definition: channel_names.h:31
constexpr char const *const kKeepEvent
Definition: channel_names.h:35
constexpr char const *const kFromWorkerBufferInfo
Definition: channel_names.h:30
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:212
constexpr char const *const kTransitionID
Definition: channel_names.h:37
constexpr char const *const kToWorkerBufferInfo
Definition: channel_names.h:29
boost::interprocess::managed_shared_memory managed_sm_
std::string uniqueName(std::string iBase) const
edm::shared_memory::ControllerChannel::ControllerChannel ( const ControllerChannel )
delete
edm::shared_memory::ControllerChannel::ControllerChannel ( ControllerChannel &&  )
delete

Member Function Documentation

BufferInfo * ControllerChannel::bufferInfo ( const char *  iWhich,
boost::interprocess::managed_shared_memory &  mem 
)
staticprivate

Definition at line 137 of file ControllerChannel.cc.

References findQualityFiles::v.

Referenced by ControllerChannel(), and initCheckWorkerStatus().

137  {
138  mem.destroy<BufferInfo>(iWhich);
139  BufferInfo* v = mem.construct<BufferInfo>(iWhich)();
140  return v;
141 }
uint16_t mem[nChs][nEvts]
bool ControllerChannel::continueWait ( boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &  lock)
private

Definition at line 116 of file ControllerChannel.cc.

References cndToMain_, initCheckWorkerStatus(), maxWaitInSeconds_, seconds(), and transitionID_.

Referenced by doTransitionWithRetry(), and initCheckWorkerStatus().

116  {
117  //std::cout << id_ << " waiting" << std::endl;
118  using namespace boost::posix_time;
119  //NOTE: value of *transitionID_ can not have been changed by the worker since call to wait()
120  // as we've had the lock since the end of that call.
121  auto workerStatus = initCheckWorkerStatus(transitionID_);
122  if (not cndToMain_.timed_wait(lock, microsec_clock::universal_time() + seconds(maxWaitInSeconds_)) and
123  not workerStatus.workerFinished()) {
124  //std::cout << id_ << " waiting FAILED" << std::endl;
125  return false;
126  }
127  return true;
128 }
boost::interprocess::named_condition cndToMain_
double seconds()
CheckWorkerStatus initCheckWorkerStatus(unsigned long long *iPtr) const
template<typename F >
bool edm::shared_memory::ControllerChannel::doTransition ( F &&  iF,
edm::Transition  iTrans,
unsigned long long  iTransitionID 
)
inline

Definition at line 102 of file ControllerChannel.h.

References CommonMethods::lock(), mutex_, and wait().

102  {
103  using namespace boost::interprocess;
104 
105  //std::cout << id_ << " taking from lock" << std::endl;
106  scoped_lock<named_mutex> lock(mutex_);
107  if (not wait(lock, iTrans, iTransitionID)) {
108  return false;
109  }
110  //std::cout <<id_<<"running doTranstion command"<<std::endl;
111  iF();
112  return true;
113  }
bool wait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
boost::interprocess::named_mutex mutex_
template<typename F , typename FRETRY >
bool edm::shared_memory::ControllerChannel::doTransitionWithRetry ( F &&  iF,
FRETRY &&  iRetry,
edm::Transition  iTrans,
unsigned long long  iTransitionID 
)
inline

Definition at line 116 of file ControllerChannel.h.

References continueWait(), CommonMethods::lock(), mutex_, and wait().

116  {
117  using namespace boost::interprocess;
118 
119  //std::cout << id_ << " taking from lock" << std::endl;
120  scoped_lock<named_mutex> lock(mutex_);
121  if (not wait(lock, iTrans, iTransitionID)) {
122  if (not iRetry()) {
123  return false;
124  }
125  bool shouldContinue = true;
126  do {
127  using namespace boost::posix_time;
128  if (not continueWait(lock)) {
129  if (not iRetry()) {
130  return false;
131  }
132  } else {
133  shouldContinue = false;
134  }
135  } while (shouldContinue);
136  }
137  //std::cout <<id_<<"running doTranstion command"<<std::endl;
138  iF();
139  return true;
140  }
bool wait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
boost::interprocess::named_mutex mutex_
bool continueWait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock)
BufferInfo* edm::shared_memory::ControllerChannel::fromWorkerBufferInfo ( )
inline

This can be used with ReadBuffer to keep Controller and Worker in sync.

Definition at line 145 of file ControllerChannel.h.

References fromWorkerBufferInfo_.

CheckWorkerStatus edm::shared_memory::ControllerChannel::initCheckWorkerStatus ( unsigned long long *  iPtr) const
inlineprivate

Definition at line 173 of file ControllerChannel.h.

References bufferInfo(), continueWait(), CommonMethods::lock(), mem, AlCaHLTBitMon_QueryRunRegistry::string, uniqueName(), and wait().

Referenced by continueWait(), and uniqueName().

173  {
174  return {*iPtr, iPtr};
175  }
unsigned int edm::shared_memory::ControllerChannel::maxWaitInSeconds ( ) const
inline

Definition at line 163 of file ControllerChannel.h.

References maxWaitInSeconds_.

const ControllerChannel& edm::shared_memory::ControllerChannel::operator= ( const ControllerChannel )
delete
const ControllerChannel& edm::shared_memory::ControllerChannel::operator= ( ControllerChannel &&  )
delete
template<typename F >
void edm::shared_memory::ControllerChannel::setupWorker ( F &&  iF)
inline

setupWorker must be called only once and done before any calls to doTransition. The functor iF should setup values associated with shared memory use, such as manipulating the value from toWorkerBufferInfo(). The call to setupWorker proper synchronizes the Controller and Worker processes.

Definition at line 56 of file ControllerChannel.h.

References Exception, CommonMethods::lock(), maxWaitInSeconds_, mutex_, stop_, and wait().

56  {
57  using namespace boost::interprocess;
58  scoped_lock<named_mutex> lock(mutex_);
59  iF();
60  using namespace boost::posix_time;
61  //std::cout << id_ << " waiting for external process" << std::endl;
62  if (not wait(lock)) {
63  //std::cout << id_ << " FAILED waiting for external process" << std::endl;
64  *stop_ = true;
65  throw cms::Exception("ExternalFailed")
66  << "Failed waiting for external process while setting up the process. Timed out after " << maxWaitInSeconds_
67  << " seconds.";
68  } else {
69  //std::cout << id_ << " done waiting for external process" << std::endl;
70  }
71  }
bool wait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
boost::interprocess::named_mutex mutex_
template<typename F , typename FRETRY >
void edm::shared_memory::ControllerChannel::setupWorkerWithRetry ( F &&  iF,
FRETRY &&  iRetry 
)
inline

setupWorkerWithRetry works just like setupWorker except it gives a way to continue waiting. The functor iRetry should return true if, after a timeout, the code should continue to wait.

Definition at line 77 of file ControllerChannel.h.

References Exception, CommonMethods::lock(), maxWaitInSeconds_, mutex_, stop_, and wait().

77  {
78  using namespace boost::interprocess;
79  scoped_lock<named_mutex> lock(mutex_);
80  iF();
81  using namespace boost::posix_time;
82  //std::cout << id_ << " waiting for external process" << std::endl;
83  bool shouldContinue = true;
84  long long int retryCount = 0;
85  do {
86  if (not wait(lock)) {
87  if (not iRetry()) {
88  *stop_ = true;
89  throw cms::Exception("ExternalFailed")
90  << "Failed waiting for external process while setting up the process. Timed out after "
91  << maxWaitInSeconds_ << " seconds with " << retryCount << " retries.";
92  }
93  //std::cerr<<"retrying\n";
94  ++retryCount;
95  } else {
96  shouldContinue = false;
97  }
98  } while (shouldContinue);
99  }
bool wait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
boost::interprocess::named_mutex mutex_
std::string const& edm::shared_memory::ControllerChannel::sharedMemoryName ( ) const
inline

Definition at line 157 of file ControllerChannel.h.

References smName_.

bool edm::shared_memory::ControllerChannel::shouldKeepEvent ( ) const
inline

Definition at line 161 of file ControllerChannel.h.

References keepEvent_.

void edm::shared_memory::ControllerChannel::stopWorker ( )
inline

Definition at line 147 of file ControllerChannel.h.

References cndFromMain_, CommonMethods::lock(), mutex_, and stop_.

147  {
148  //std::cout <<"stopWorker"<<std::endl;
149  using namespace boost::interprocess;
150  scoped_lock<named_mutex> lock(mutex_);
151  *stop_ = true;
152  //std::cout <<"stopWorker sending notification"<<std::endl;
153  cndFromMain_.notify_all();
154  }
boost::interprocess::named_mutex mutex_
boost::interprocess::named_condition cndFromMain_
BufferInfo* edm::shared_memory::ControllerChannel::toWorkerBufferInfo ( )
inline

This can be used with WriteBuffer to keep Controller and Worker in sync.

Definition at line 143 of file ControllerChannel.h.

References toWorkerBufferInfo_.

std::string edm::shared_memory::ControllerChannel::uniqueID ( ) const
inline

Definition at line 158 of file ControllerChannel.h.

References uniqueName().

158 { return uniqueName(""); }
std::string uniqueName(std::string iBase) const
std::string ControllerChannel::uniqueName ( std::string  iBase) const
private

Definition at line 76 of file ControllerChannel.cc.

References cndFromMain_, cndToMain_, id_, initCheckWorkerStatus(), CommonMethods::lock(), maxWaitInSeconds_, sysUtil::pid, seconds(), transitionID_, transitionType_, and wait().

Referenced by ControllerChannel(), initCheckWorkerStatus(), uniqueID(), and ~ControllerChannel().

76  {
77  auto pid = getpid();
78  iBase += std::to_string(pid);
79  iBase += "_";
80  iBase += std::to_string(id_);
81 
82  return iBase;
83 }
bool edm::shared_memory::ControllerChannel::wait ( boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &  lock,
edm::Transition  iTrans,
unsigned long long  iTransID 
)
private
bool edm::shared_memory::ControllerChannel::wait ( boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &  lock)
private

Member Data Documentation

boost::interprocess::named_condition edm::shared_memory::ControllerChannel::cndFromMain_
private

Definition at line 196 of file ControllerChannel.h.

Referenced by ControllerChannel(), stopWorker(), and uniqueName().

boost::interprocess::named_condition edm::shared_memory::ControllerChannel::cndToMain_
private

Definition at line 198 of file ControllerChannel.h.

Referenced by continueWait(), ControllerChannel(), and uniqueName().

BufferInfo* edm::shared_memory::ControllerChannel::fromWorkerBufferInfo_
private

Definition at line 193 of file ControllerChannel.h.

Referenced by ControllerChannel(), and fromWorkerBufferInfo().

int edm::shared_memory::ControllerChannel::id_
private

Definition at line 188 of file ControllerChannel.h.

Referenced by uniqueName().

bool* edm::shared_memory::ControllerChannel::keepEvent_
private

Definition at line 203 of file ControllerChannel.h.

Referenced by ControllerChannel(), and shouldKeepEvent().

boost::interprocess::managed_shared_memory edm::shared_memory::ControllerChannel::managed_sm_
private

Definition at line 191 of file ControllerChannel.h.

Referenced by ControllerChannel(), and ~ControllerChannel().

unsigned int edm::shared_memory::ControllerChannel::maxWaitInSeconds_
private
boost::interprocess::named_mutex edm::shared_memory::ControllerChannel::mutex_
private
std::string edm::shared_memory::ControllerChannel::smName_
private

Definition at line 190 of file ControllerChannel.h.

Referenced by ControllerChannel(), and sharedMemoryName().

bool* edm::shared_memory::ControllerChannel::stop_
private
BufferInfo* edm::shared_memory::ControllerChannel::toWorkerBufferInfo_
private

Definition at line 192 of file ControllerChannel.h.

Referenced by ControllerChannel(), and toWorkerBufferInfo().

unsigned long long* edm::shared_memory::ControllerChannel::transitionID_
private

Definition at line 201 of file ControllerChannel.h.

Referenced by continueWait(), ControllerChannel(), and uniqueName().

edm::Transition* edm::shared_memory::ControllerChannel::transitionType_
private

Definition at line 200 of file ControllerChannel.h.

Referenced by ControllerChannel(), and uniqueName().