Belle II Software  release-05-02-19
ProcHandler Class Reference

A class to manage processes for parallel processing. More...

#include <ProcHandler.h>

Collaboration diagram for ProcHandler:

Public Member Functions

 ProcHandler (unsigned int nWorkerProc, bool markChildrenAsLocal=false)
 Constructor. More...
 
 ~ProcHandler ()
 Destructor.
 
void startInputProcess ()
 Fork and initialize an input process.
 
void startWorkerProcesses ()
 Fork and initialize worker processes.
 
void startOutputProcess ()
 There is no real output process, but marks current process as output.
 
bool waitForAllProcesses ()
 Wait until all forked processes handled by this ProcHandler terminate. More...
 
std::set< int > processList () const
 Return list of PIDs managed by this ProcHandler instance.
 

Static Public Member Functions

static bool parallelProcessingUsed ()
 Returns true if multiple processes have been spawned, false in single-core mode.
 
static bool isInputProcess ()
 Return true if the process is an input process.
 
static bool isWorkerProcess ()
 Return true if the process is a worker process.
 
static bool isOutputProcess ()
 Return true if the process is an output process.
 
static int numEventProcesses ()
 Return number of worker processes (configured value, not current)
 
static std::set< int > globalProcessList ()
 Return list of all PIDs (from all ProcHandler instances).
 
static int EvtProcID ()
 Return ID of the current process. More...
 
static void setProcessID (int processID)
 Set the process ID of this process.
 
static std::string getProcessName ()
 Get a name for this process. More...
 

Private Member Functions

bool startProc (std::set< int > *processList, const std::string &procType, int id)
 Start a new process, adding its PID to processList, and setting s_processID = id. More...
 

Private Attributes

bool m_markChildrenAsLocal
 Anormal termination of child will not stop parent, waitForAllProcesses() returns status.
 
std::set< int > m_processList
 PIDs of processes controlled by this ProcHandler.
 
unsigned int m_numWorkerProcesses
 Number of worker processes controlled by this ProcHandler.
 

Detailed Description

A class to manage processes for parallel processing.

Definition at line 18 of file ProcHandler.h.

Constructor & Destructor Documentation

◆ ProcHandler()

ProcHandler ( unsigned int  nWorkerProc,
bool  markChildrenAsLocal = false 
)
explicit

Constructor.

Children marked as local will not cause the parent process to die in case of anormal termination. Errors can be detected using the return value of waitForAllProcesses(). Note that you probably shouldn't have multiple ProcHandlers with local children running at the same time.

Definition at line 183 of file ProcHandler.cc.

183  :
184  m_markChildrenAsLocal(markChildrenAsLocal),
185  m_numWorkerProcesses(nWorkerProc)
186 {
187  if ((int)nWorkerProc > s_numEventProcesses)
188  s_numEventProcesses = nWorkerProc;
189 
190  if (!pidListEmpty())
191  B2FATAL("Constructing ProcHandler after forking is not allowed!");
192 
193  //s_pidVector size shouldn't be changed once processes are forked (race condition)
194  s_pidVector.reserve(s_pidVector.size() + nWorkerProc + 2);
195  s_pids = s_pidVector.data();
196  setsid();
197 
198 }

Member Function Documentation

◆ EvtProcID()

int EvtProcID ( )
static

Return ID of the current process.

Return values mean: -1: no parallel processing used <10000 one of the worker processes (between input and output paths). In range 0..Environment::getInstance().getNumberProcesses()-1. >=10000 input path >=20000 output path

Definition at line 243 of file ProcHandler.cc.

◆ getProcessName()

std::string getProcessName ( )
static

Get a name for this process.

(input, event, output...).

Definition at line 247 of file ProcHandler.cc.

◆ startProc()

bool startProc ( std::set< int > *  processList,
const std::string &  procType,
int  id 
)
private

Start a new process, adding its PID to processList, and setting s_processID = id.

Returns true in child process.

Definition at line 151 of file ProcHandler.cc.

◆ waitForAllProcesses()

bool waitForAllProcesses ( )

Wait until all forked processes handled by this ProcHandler terminate.

In case the markChildrenAsLocal option was set when a process was started and this process terminated anormaly (via signal or non-zero exit code), the return value is set to false. Otherwise, true is returned.

Definition at line 261 of file ProcHandler.cc.


The documentation for this class was generated from the following files:
Belle2::ProcHandler::m_numWorkerProcesses
unsigned int m_numWorkerProcesses
Number of worker processes controlled by this ProcHandler.
Definition: ProcHandler.h:87
Belle2::ProcHandler::m_markChildrenAsLocal
bool m_markChildrenAsLocal
Anormal termination of child will not stop parent, waitForAllProcesses() returns status.
Definition: ProcHandler.h:85