Belle II Software development
RFEventProcessor.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include "daq/rfarm/manager/RFEventProcessor.h"
10
11#include <sys/stat.h>
12#include <sys/types.h>
13#include <sys/wait.h>
14#include <unistd.h>
15
16#include <cstring>
17#include <iostream>
18
19#define RFOTSOUT stdout
20
21//#define DESY
22
23using namespace std;
24using namespace Belle2;
25
26RFEventProcessor::RFEventProcessor(const string& conffile)
27{
28 // 0. Initialize configuration manager
29 m_conf = new RFConf(conffile.c_str());
30 // char* nodename = m_conf->getconf ( "processor", "nodename" );
31 // char nodename[256];
32 strcpy(m_nodename, "evp_");
33#ifndef DESY
34 gethostname(&m_nodename[4], sizeof(m_nodename) - 4);
35#else
36 // Special treatment for DESY test nodes!!
37 char hostnamebuf[256];
38 gethostname(hostnamebuf, sizeof(hostnamebuf));
39 strcat(&m_nodename[4], &hostnamebuf[6]);
40 int lend = strlen(m_nodename);
41 m_nodename[lend + 1] = (char)0;
42 m_nodename[lend] = m_nodename[lend - 1];
43 strncpy(&m_nodename[lend - 1], "0", 1);
44 // End of DESY special treatment
45#endif
46 printf("nodename = %s\n", m_nodename);
47
48 // 1. Set execution directory
49 string execdir = string(m_conf->getconf("system", "execdir_base")) + "/" + string(m_nodename);
50 printf("execdir = %s\n", execdir.c_str());
51
52 mkdir(execdir.c_str(), 0755);
53 chdir(execdir.c_str());
54
55 // 2. Initialize local shared memory
56 string shmname = string(m_conf->getconf("system", "unitname")) + ":" +
57 string(m_nodename);
58 m_shm = new RFSharedMem(const_cast<char*>(shmname.c_str()));
59
60 // 3. Initialize process manager
61 m_proc = new RFProcessManager(m_nodename);
62
63 // 4. Initialize RingBuffers
64 // char* rbufin = m_conf->getconf("processor", "ringbufin");
65 string rbufin = string(m_conf->getconf("system", "unitname")) + ":" +
66 string(m_conf->getconf("processor", "ringbufin"));
67 int rbinsize = m_conf->getconfi("processor", "ringbufinsize");
68 // m_rbufin = new RingBuffer(rbufin, rbinsize);
69 m_rbufin = new RingBuffer(rbufin.c_str(), rbinsize);
70 // char* rbufout = m_conf->getconf("processor", "ringbufout");
71 string rbufout = string(m_conf->getconf("system", "unitname")) + ":" +
72 string(m_conf->getconf("processor", "ringbufout"));
73 int rboutsize = m_conf->getconfi("processor", "ringbufoutsize");
74 // m_rbufout = new RingBuffer(rbufout, rboutsize);
75 m_rbufout = new RingBuffer(rbufout.c_str(), rboutsize);
76
77 // 5. Initialize LogManager
78 m_log = new RFLogManager(m_nodename, m_conf->getconf("system", "lognode"));
79
80 // 6. Initialize data flow monitor
81 m_flow = new RFFlowStat(const_cast<char*>(shmname.c_str()));
82
83 // 7. Clear PID list
84 m_pid_sender = 0;
85 m_pid_basf2 = 0;
86 m_pid_receiver = 0;
87 m_pid_hrecv = 0;
88 m_pid_hrelay = 0;
89
90}
91
92RFEventProcessor::~RFEventProcessor()
93{
94 delete m_log;
95 delete m_proc;
96 delete m_shm;
97 delete m_conf;
98 delete m_flow;
99 delete m_rbufin;
100 delete m_rbufout;
101}
102
103
104// Functions hooked up by NSM2
105
106int RFEventProcessor::Configure(NSMmsg* /*nsmm*/, NSMcontext* /*nsmc*/)
107{
108 // Start processes from down stream
109
110 // 0. Get common parameters
111 // char* rbufin = m_conf->getconf("processor", "ringbufin");
112 string rbufin = string(m_conf->getconf("system", "unitname")) + ":" +
113 string(m_conf->getconf("processor", "ringbufin"));
114 // char* rbufout = m_conf->getconf("processor", "ringbufout");
115 string rbufout = string(m_conf->getconf("system", "unitname")) + ":" +
116 string(m_conf->getconf("processor", "ringbufout"));
117
118 string shmname = string(m_conf->getconf("system", "unitname")) + ":" +
119 string(m_nodename);
120
121 // 1. Histogram Receiver
122 char* hrecv = m_conf->getconf("processor", "historecv", "script");
123 char* hport = m_conf->getconf("processor", "historecv", "port");
124 char* mapfile = m_conf->getconf("processor", "historecv", "mapfile");
125 m_pid_hrecv = m_proc->Execute(hrecv, hport, mapfile);
126
127 sleep(5); // make sure that TMapFile is created.
128
129 // 2. Histogram Relay
130 char* hrelay = m_conf->getconf("processor", "historelay", "script");
131 char* dqmdest = m_conf->getconf("dqmserver", "host");
132 char* dqmport = m_conf->getconf("dqmserver", "port");
133 char* interval = m_conf->getconf("processor", "historelay", "interval");
134 m_pid_hrelay = m_proc->Execute(hrelay, mapfile, dqmdest, dqmport, interval);
135
136 // 3. Run sender / logger
137 char* sender = m_conf->getconf("processor", "sender", "script");
138 char* port = m_conf->getconf("processor", "sender", "port");
139 m_pid_sender = m_proc->Execute(sender, const_cast<char*>(rbufout.c_str()), port, const_cast<char*>(shmname.c_str()),
140 const_cast<char*>("1"));
141 m_flow->clear(1);
142
143 /*
144 // 4. Run basf2
145 char* basf2 = m_conf->getconf("processor", "basf2", "script");
146 if (nsmm->len > 0) {
147 basf2 = (char*) nsmm->datap;
148 printf("Configure: basf2 script overridden : %s\n", basf2);
149 }
150 m_pid_basf2 = m_proc->Execute(basf2, (char*)rbufin.c_str(), (char*)rbufout.c_str(), hport);
151 */
152
153 // 5. Run receiver
154 char* receiver = m_conf->getconf("processor", "receiver", "script");
155 char* srchost = m_conf->getconf("distributor", "host");
156 // char* port = m_conf->getconf ( "distributor", "port" );
157 int portbase = m_conf->getconfi("distributor", "sender", "portbase");
158 // char* hostbase = m_conf->getconf("processor", "nodebase");
159 /* OLD impl
160 int baselen = strlen(hostbase);
161 char hostname[256];
162 gethostname(hostname, sizeof(hostname));
163 char id[3];
164 strcpy(id, &hostname[baselen + 1]);
165 int rport = atoi(id) + portbase;
166 */
167 int rport;
168 sscanf(&m_nodename[strlen(m_nodename) - 2], "%d", &rport);
169 rport += portbase;
170 char portchar[256];
171 sprintf(portchar, "%d", rport);
172 m_pid_receiver = m_proc->Execute(receiver, const_cast<char*>(rbufin.c_str()), srchost, portchar, const_cast<char*>(shmname.c_str()),
173 const_cast<char*>("0"));
174 m_flow->clear(0);
175
176 printf("Configure : done\n");
177 fflush(stdout);
178
179 // 6 Clear RingBuffers
180 m_rbufin->forceClear();
181 m_rbufout->forceClear();
182
183 return 0;
184
185}
186
187int RFEventProcessor::UnConfigure(NSMmsg*, NSMcontext*)
188{
189 // Simple implementation to stop all processes
190 // system("killall basf2 sock2rbr rb2sockr hrelay hserver");
191
192 // Emergency stop
193 system("killall -9 python");
194
195 // Normal abort
196 int status;
197 if (m_pid_sender != 0) {
198 printf("RFEventProcessor : killing sender pid=%d\n", m_pid_sender);
199 kill(m_pid_sender, SIGINT);
200 waitpid(m_pid_sender, &status, 0);
201 }
202 if (m_pid_basf2 != 0) {
203 printf("RFEventProcessor : killing basf2 pid=%d\n", m_pid_basf2);
204 // kill(m_pid_basf2, SIGINT);
205 kill(m_pid_basf2, SIGINT);
206 waitpid(m_pid_basf2, &status, 0);
207 m_pid_basf2 = 0;
208 }
209 if (m_pid_receiver != 0) {
210 printf("RFEventProcessor : killing receiver pid=%d\n", m_pid_receiver);
211 kill(m_pid_receiver, SIGINT);
212 waitpid(m_pid_receiver, &status, 0);
213 }
214 if (m_pid_hrecv != 0) {
215 printf("RFEventProcessor : killing hserver pid=%d\n", m_pid_hrecv);
216 kill(m_pid_hrecv, SIGINT);
217 waitpid(m_pid_hrecv, &status, 0);
218 }
219 if (m_pid_hrelay != 0) {
220 printf("RFEventProcessor : killing hrelay pid=%d\n", m_pid_hrelay);
221 kill(m_pid_hrelay, SIGINT);
222 waitpid(m_pid_hrelay, &status, 0);
223 }
224
225 // Clear RingBuffers
226 m_rbufin->forceClear();
227 m_rbufout->forceClear();
228
229 // Clear PID list
230 m_flow->fillProcessStatus(GetNodeInfo());
231
232 printf("Unconfigure : done\n");
233 fflush(stdout);
234 return 0;
235}
236
237int RFEventProcessor::Start(NSMmsg* nsmm, NSMcontext* /*nsmc*/)
238{
239 string rbufin = string(m_conf->getconf("system", "unitname")) + ":" +
240 string(m_conf->getconf("processor", "ringbufin"));
241 string rbufout = string(m_conf->getconf("system", "unitname")) + ":" +
242 string(m_conf->getconf("processor", "ringbufout"));
243 char* hport = m_conf->getconf("processor", "historecv", "port");
244
245 // 0. Set run numbers
246 m_expno = nsmm->pars[0];
247 m_runno = nsmm->pars[1];
248
249 // 1. Clear RingBuffers
250 m_rbufout->forceClear();
251 m_rbufin->forceClear();
252
253 // 2. Run basf2
254 char* basf2 = m_conf->getconf("processor", "basf2", "script");
255 if (nsmm->len > 0) {
256 basf2 = const_cast<char*>(nsmm->datap);
257 printf("Configure: basf2 script overridden : %s\n", basf2);
258 }
259 m_pid_basf2 = m_proc->Execute(basf2, const_cast<char*>(rbufin.c_str()), const_cast<char*>(rbufout.c_str()), hport);
260
261 return 0;
262}
263
264int RFEventProcessor::Stop(NSMmsg* /*msgm*/, NSMcontext* /*msgc*/)
265{
266 printf("RFEventProcessor : STOP processing started\n");
267 // fflush ( stdout );
268 /*
269 char* hcollect = m_conf->getconf("processor", "dqm", "hcollect");
270 char* filename = m_conf->getconf("processor", "dqm", "file");
271 char* nprocs = m_conf->getconf("processor", "basf2", "nprocs");
272 int pid_hcollect = m_proc->Execute(hcollect, filename, nprocs);
273 int status;
274 waitpid(pid_hcollect, &status, 0);
275 */
276
277 /*
278 // Need to wait until all the events are processed and sent
279 RfNodeInfo* nodeinfo = GetNodeInfo();
280 int ncount = 0;
281 for (;;) {
282 RfShm_Cell& cellin = m_flow->getinfo(0);
283 RfShm_Cell& cellout = m_flow->getinfo(1);
284 if (cellin.nqueue == 0 && cellout.nqueue == 0) break;
285 printf("inqueue = %d : outqueue = %d\n", cellin.nqueue, cellout.nqueue);
286 if (ncount > 30) {
287 printf("RFEventProcessor : Timeout (30sec) in stopping\n");
288 break;
289 }
290 sleep(1);
291 ncount ++;
292 }
293 */
294 // Checking number of processed events using RfShm_Cell does not work as expected and is
295 // equivalent to wait for 30 sec. Just replaced with 5 sec waiting.
296 sleep(5);
297
298 int status;
299 if (m_pid_basf2 != 0) {
300 printf("RFEventProcessor : killing basf2 pid=%d\n", m_pid_basf2);
301 kill(m_pid_basf2, SIGINT);
302 waitpid(m_pid_basf2, &status, 0);
303 m_pid_basf2 = 0;
304 char outfile[1024];
305 sprintf(outfile, "dqm_e%4.4dr%6.6d.root", m_expno, m_runno);
306 std::rename("histofile.root", outfile);
307 printf("output file name = %s\n", outfile);
308 fflush(stdout);
309
310 }
311 printf("RFEventProcessor : STOP processing done\n");
312 return 0;
313}
314
315
316int RFEventProcessor::Restart(NSMmsg*, NSMcontext*)
317{
318 printf("RFEventProcessor : Restarting!!!!!\n");
319 /* Original impl.
320 if (m_pid_sender != 0) {
321 printf("RFEventProcessor : killing sender pid=%d\n", m_pid_sender);
322 kill(m_pid_sender, SIGINT);
323 }
324 if (m_pid_basf2 != 0) {
325 printf("RFEventProcessor : killing basf2 pid=%d\n", m_pid_basf2);
326 kill(m_pid_basf2, SIGINT);
327 }
328 if (m_pid_receiver != 0) {
329 printf("RFEventProcessor : killing receiver pid=%d\n", m_pid_receiver);
330 kill(m_pid_receiver, SIGINT);
331 }
332 if (m_pid_hrecv != 0) {
333 printf("RFEventProcessor : killing hserver pid=%d\n", m_pid_hrecv);
334 kill(m_pid_receiver, SIGINT);
335 }
336 if (m_pid_hrelay != 0) {
337 printf("RFEventProcessor : killing hrelay pid=%d\n", m_pid_hrelay);
338 kill(m_pid_receiver, SIGINT);
339 }
340 // Simple implementation to stop all processes
341 system("killall basf2 sock2rbr rb2sockr hrelay hserver");
342 fflush(stdout);
343 */
344 NSMmsg* nsmmsg = NULL;
345 NSMcontext* nsmcontext = NULL;
346 RFEventProcessor::UnConfigure(nsmmsg, nsmcontext);
347 sleep(2);
348 RFEventProcessor::Configure(nsmmsg, nsmcontext);
349 return 0;
350}
351
352// Server function
353
354void RFEventProcessor::server()
355{
356 // Clear PID list
357 m_flow->fillProcessStatus(GetNodeInfo());
358 // Start Loop
359 while (true) {
360 pid_t pid = m_proc->CheckProcess();
361 if (pid > 0) {
362 printf("RFEventProcessor : process dead pid=%d\n", pid);
363 if (pid == m_pid_sender) {
364 m_log->Fatal("RFEventProcessor : sender dead. pid=%d\n", m_pid_sender);
365 m_pid_sender = 0;
366 } else if (pid == m_pid_basf2) {
367 m_log->Fatal("RFEventProcessor : basf2 dead. pid=%d\n", m_pid_basf2);
368 m_pid_basf2 = 0;
369 } else if (pid == m_pid_receiver) {
370 m_log->Fatal("RFEventProcessor : receiver dead. pid=%d\n", m_pid_receiver);
371 m_pid_receiver = 0;
372 } else if (pid == m_pid_hrecv) {
373 m_log->Fatal("RFEventProcessor : hserver dead. pid=%d\n", m_pid_hrecv);
374 m_pid_hrecv = 0;
375 } else if (pid == m_pid_hrelay) {
376 m_log->Fatal("RFEventProcessor : hrelay dead. pid=%d\n", m_pid_hrelay);
377 m_pid_hrelay = 0;
378 }
379 }
380 int st = m_proc->CheckOutput();
381 if (st < 0) {
382 perror("RFEventProcessor::server");
383 // exit ( -1 );
384 } else if (st > 0) {
385 m_log->ProcessLog(m_proc->GetFd());
386 }
387 m_flow->fillNodeInfo(0, GetNodeInfo(), false);
388 m_flow->fillNodeInfo(1, GetNodeInfo(), true);
389 m_flow->fillProcessStatus(GetNodeInfo(), m_pid_receiver, m_pid_sender, m_pid_basf2,
390 m_pid_hrecv, m_pid_hrelay);
391 }
392}
393
394void RFEventProcessor::cleanup()
395{
396 printf("RFEventProcessor : cleaning up\n");
397 UnConfigure(NULL, NULL);
398 printf("RFEventProcessor: Done. Exitting\n");
399 exit(-1);
400}
401
402
403
Abstract base class for different kinds of events.
STL namespace.