Belle II Software development
ProcessController Class Reference

Public Member Functions

 ProcessController (RCCallback *callback)
 
bool init (const std::string &parname, int nodeid=0)
 
void clear ()
 
bool load (int timeout)
 
bool start (int expno, int runno)
 
bool pause ()
 
bool resume ()
 
bool stop ()
 
bool abort (unsigned int timeout=60)
 
const std::string & getName ()
 
const std::string & getParName ()
 
const std::string & getExecutable ()
 
RunInfoBuffergetInfo ()
 
RCCallbackgetCallback ()
 
const ProcessgetProcess () const
 
ProcessgetProcess ()
 
void setCallback (RCCallback *callback)
 
void setName (const std::string &name)
 
void setExecutable (const std::string &exe)
 
void addArgument (const std::string &arg)
 
void addArgument (const char *format,...)
 
template<typename T >
void addArgument (T arg)
 
void clearArguments ()
 
bool isAlive () const
 
bool waitReady (int timeout)
 
void lock ()
 
void unlock ()
 

Private Attributes

RunInfoBuffer m_info
 
std::string m_name
 
std::string m_parname
 
RCCallbackm_callback
 
std::string m_exename
 
std::vector< std::string > m_arg_v
 
Process m_process
 
Mutex m_mutex
 
std::string m_message
 
PThread m_th_log
 
PThread m_th_process
 
int m_iopipe [2]
 

Friends

class ProcessSubmitter
 

Detailed Description

Definition at line 28 of file ProcessController.h.

Constructor & Destructor Documentation

◆ ProcessController() [1/2]

ProcessController ( )
inline

Definition at line 33 of file ProcessController.h.

34 {
35 m_callback = NULL;
36 }

◆ ProcessController() [2/2]

ProcessController ( RCCallback callback)
inline

Definition at line 37 of file ProcessController.h.

38 {
39 m_callback = callback;
40 }

◆ ~ProcessController()

~ProcessController ( )
inline

Definition at line 41 of file ProcessController.h.

42 {
43 m_info.close();
44 }

Member Function Documentation

◆ abort()

bool abort ( unsigned int  timeout = 60)

Definition at line 123 of file ProcessController.cc.

124{
125 m_info.clear();
126 m_process.kill(SIGINT);
127
128 bool sigintWasSuccessful = false;
129 for (unsigned int timer = 0; timer < timeout; ++timer) {
130 if (not m_process.isAlive()) {
131 sigintWasSuccessful = true;
132 break;
133 } else {
134 usleep(1000000);
135 }
136 }
137
138 if (sigintWasSuccessful) {
139 LogFile::debug("Process successfully killed with SIGINT.");
140 } else {
141 LogFile::warning("Process could not be killed with SIGINT, sending SIGABRT.");
142 m_process.kill(SIGABRT);
143 }
144
145 if (m_callback != NULL)
146 m_callback->set(m_parname + ".pid", -1);
147 m_process.wait();
148 m_th_log.cancel();
149 m_th_process.cancel();
150 close(m_iopipe[1]);
151 close(m_iopipe[0]);
152 return true;
153}

◆ addArgument() [1/2]

void addArgument ( const char *  format,
  ... 
)

Definition at line 173 of file ProcessController.cc.

174{
175 va_list ap;
176 static char ss[1024];
177 va_start(ap, format);
178 vsprintf(ss, format, ap);
179 va_end(ap);
180 m_arg_v.push_back(ss);
181}

◆ addArgument() [2/2]

void addArgument ( const std::string &  arg)
inline

Definition at line 73 of file ProcessController.h.

73{ m_arg_v.push_back(arg); }

◆ clear()

void clear ( )

Definition at line 39 of file ProcessController.cc.

40{
41 m_info.clear();
42}

◆ clearArguments()

void clearArguments ( )
inline

Definition at line 77 of file ProcessController.h.

77{ m_arg_v = std::vector<std::string>(); }

◆ getCallback()

RCCallback * getCallback ( )
inline

Definition at line 67 of file ProcessController.h.

67{ return m_callback; }

◆ getExecutable()

const std::string & getExecutable ( )
inline

Definition at line 65 of file ProcessController.h.

65{ return m_exename; }

◆ getInfo()

RunInfoBuffer & getInfo ( )
inline

Definition at line 66 of file ProcessController.h.

66{ return m_info; }

◆ getName()

const std::string & getName ( )
inline

Definition at line 63 of file ProcessController.h.

63{ return m_name; }

◆ getParName()

const std::string & getParName ( )
inline

Definition at line 64 of file ProcessController.h.

64{ return m_parname; }

◆ getProcess() [1/2]

Process & getProcess ( )
inline

Definition at line 69 of file ProcessController.h.

69{ return m_process; }

◆ getProcess() [2/2]

const Process & getProcess ( ) const
inline

Definition at line 68 of file ProcessController.h.

68{ return m_process; }

◆ init()

bool init ( const std::string &  parname,
int  nodeid = 0 
)

Definition at line 27 of file ProcessController.cc.

28{
29 m_name = StringUtil::tolower(m_callback->getNode().getName());
30 m_parname = parname;
31 LogFile::open(m_name + "_" + m_parname);
32 if (!m_info.open(m_name + "_" + m_parname, nodeid, true)) {
33 return false;
34 }
35 m_callback->add(new NSMVHandlerInt(m_parname + ".pid", true, false, 0), false, false);
36 return true;
37}

◆ isAlive()

bool isAlive ( ) const
inline

Definition at line 78 of file ProcessController.h.

78{ return m_process.isAlive(); }

◆ load()

bool load ( int  timeout)

Definition at line 52 of file ProcessController.cc.

53{
54 m_info.clear();
55 m_process.cancel();
56 m_process.kill(SIGABRT);
57 if (pipe(m_iopipe) < 0) {
58 perror("pipe");
59 return false;
60 }
61 m_process = Process(new ProcessSubmitter(this, m_iopipe));
62 m_th_log = PThread(new LogListener(this, m_iopipe));
63 m_th_process = PThread(new ProcessListener(this));
64 //close(iopipe[1]);
65 if (timeout > 0) {
66 if (!m_info.waitReady(timeout)) {
67 throw (RCHandlerException("Failed to boot " + m_parname));
68 }
69 }
70 m_callback->set(m_parname + ".pid", m_process.get_id());
71 return true;
72}

◆ lock()

void lock ( )
inline

Definition at line 82 of file ProcessController.h.

82{ m_mutex.lock(); }

◆ pause()

bool pause ( )

Definition at line 101 of file ProcessController.cc.

102{
103 GenericLockGuard<RunInfoBuffer> lockGuard(m_info);
104 if (m_info.isRunning()) {
105 m_info.setState(RunInfoBuffer::PAUSED);
106 } else {
107 LogFile::warning("Process is not running. Pause request was ignored.");
108 }
109 return true;
110}
Lock Guard for a Mutex instance.
Definition: LockGuard.h:47

◆ resume()

bool resume ( )

Definition at line 112 of file ProcessController.cc.

113{
114 GenericLockGuard<RunInfoBuffer> lockGuard(m_info);
115 if (m_info.isPaused()) {
116 m_info.setState(RunInfoBuffer::RUNNING);
117 } else {
118 LogFile::warning("Process is not paused. Resume request was ignored.");
119 }
120 return true;
121}

◆ setCallback()

void setCallback ( RCCallback callback)
inline

Definition at line 70 of file ProcessController.h.

70{ m_callback = callback; }

◆ setExecutable()

void setExecutable ( const std::string &  exe)
inline

Definition at line 72 of file ProcessController.h.

72{ m_exename = exe; }

◆ setName()

void setName ( const std::string &  name)
inline

Definition at line 71 of file ProcessController.h.

71{ m_name = name; }

◆ start()

bool start ( int  expno,
int  runno 
)

Definition at line 74 of file ProcessController.cc.

75{
76 GenericLockGuard<RunInfoBuffer> lockGuard(m_info);
77 m_info.setExpNumber(expno);
78 m_info.setRunNumber(runno);
79 m_info.setSubNumber(0);
80 /*
81 if (m_info.getState() != RunInfoBuffer::RUNNING) {
82 throw (RCHandlerException(m_parname + " is not running"));
83 }
84 */
85 return true;
86}

◆ stop()

bool stop ( )

Definition at line 88 of file ProcessController.cc.

89{
90 GenericLockGuard<RunInfoBuffer> lockGuard(m_info);
91 m_info.setExpNumber(0);
92 m_info.setRunNumber(0);
93 m_info.setSubNumber(0);
94 m_info.setInputCount(0);
95 m_info.setInputNBytes(0);
96 m_info.setOutputCount(0);
97 m_info.setOutputNBytes(0);
98 return true;
99}

◆ unlock()

void unlock ( )
inline

Definition at line 83 of file ProcessController.h.

83{ m_mutex.unlock(); }

◆ waitReady()

bool waitReady ( int  timeout)

Definition at line 44 of file ProcessController.cc.

45{
46 if (!m_info.waitReady(timeout)) {
47 return false;
48 }
49 return true;
50}

Friends And Related Function Documentation

◆ ProcessSubmitter

friend class ProcessSubmitter
friend

Definition at line 30 of file ProcessController.h.

Member Data Documentation

◆ m_arg_v

std::vector<std::string> m_arg_v
private

Definition at line 91 of file ProcessController.h.

◆ m_callback

RCCallback* m_callback
private

Definition at line 89 of file ProcessController.h.

◆ m_exename

std::string m_exename
private

Definition at line 90 of file ProcessController.h.

◆ m_info

RunInfoBuffer m_info
private

Definition at line 86 of file ProcessController.h.

◆ m_iopipe

int m_iopipe[2]
private

Definition at line 97 of file ProcessController.h.

◆ m_message

std::string m_message
private

Definition at line 94 of file ProcessController.h.

◆ m_mutex

Mutex m_mutex
private

Definition at line 93 of file ProcessController.h.

◆ m_name

std::string m_name
private

Definition at line 87 of file ProcessController.h.

◆ m_parname

std::string m_parname
private

Definition at line 88 of file ProcessController.h.

◆ m_process

Process m_process
private

Definition at line 92 of file ProcessController.h.

◆ m_th_log

PThread m_th_log
private

Definition at line 95 of file ProcessController.h.

◆ m_th_process

PThread m_th_process
private

Definition at line 96 of file ProcessController.h.


The documentation for this class was generated from the following files: