Belle II Software development
RFMaster.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/RFMaster.h"
10#include "daq/rfarm/manager/RFNSM.h"
11
12#include <nsm2/belle2nsm.h>
13
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <unistd.h>
17
18#include <cstring>
19
20using namespace std;
21using namespace Belle2;
22
23//#define DESY
24
25// Main
26RFMaster::RFMaster(const string& conffile)
27{
28 // 0. Initialize configuration manager
29 m_conf = new RFConf(conffile.c_str());
30 char* nodename = m_conf->getconf("master", "nodename");
31 // char nodename[256];
32 // gethostname ( nodename, sizeof(nodename) );
33
34 // 1. Initialize local shared memory
35 // m_shm = new RFSharedMem(nodename);
36
37 // 2. Set execution directory
38 string execdir = string(m_conf->getconf("system", "execdir_base")) + "/master";
39
40 mkdir(execdir.c_str(), 0755);
41 chdir(execdir.c_str());
42
43 // 3. Initialize LogManager
44 m_log = new RFLogManager(nodename, m_conf->getconf("system", "lognode"));
45
46 // 4. Leave PID file
47 FILE* f = fopen("pid.data", "w");
48 if (f != NULL) {
49 fprintf(f, "%d", getpid());
50 fclose(f);
51 }
52
53}
54
55RFMaster::~RFMaster()
56{
57 delete m_log;
58 // delete m_shm;
59 delete m_conf;
60}
61
62void RFMaster::Hook_Message_Handlers()
63{
64 // 5. Hook message handlers
65 if (b2nsm_callback("LOG", Log_Handler) < 0) {
66 fprintf(stderr, "RFMaster : hooking INFO handler failed, %s\n",
67 b2nsm_strerror());
68 }
69 printf("RFMaster: Message Handlers - Ready\n");
70
71}
72
73// NSM callback functions for message
74
75void RFMaster::Log_Handler(NSMmsg* /*msg*/, NSMcontext* /*ctx*/)
76{
77 // printf ( "RFMaster : [INFO] received\n" );
78 // b2nsm_ok ( msg, "INFO!!", NULL );
79 // fflush ( stdout );
80}
81
82
83// Functions hooked up by NSM2
84
85int RFMaster::Configure(NSMmsg*, NSMcontext*)
86{
87 int* pars = nullptr;
88
89 // 0. Configure DqmServer
90 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
91 RFNSM_Status::Instance().set_flag(0);
92 // b2nsm_sendreq(dqmserver, "RF_CONFIGURE", 0, pars);
93 b2nsm_sendreq(dqmserver, "RC_LOAD", 0, pars);
94 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
95 printf("RFMaster:: dqmserver configured\n");
96 // sleep(2);
97
98 // 1. Configure distributor
99 const char* distributor = m_conf->getconf("distributor", "nodename");
100 RFNSM_Status::Instance().set_flag(0);
101 // b2nsm_sendreq(distributor, "RF_CONFIGURE", 0, pars);
102 b2nsm_sendreq(distributor, "RC_LOAD", 0, pars);
103 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
104 printf("RFMaster:: distributor configured\n");
105
106 // sleep(2);
107
108 // 2. Configure event processors
109 int maxnodes = m_conf->getconfi("processor", "nnodes");
110 int idbase = m_conf->getconfi("processor", "idbase");
111 const char* hostbase = m_conf->getconf("processor", "nodebase");
112 char* badlist = m_conf->getconf("processor", "badlist");
113
114 char hostnode[512], idname[3];
115 int nnodes = 0;
116 RFNSM_Status::Instance().set_flag(0);
117 for (int i = 0; i < maxnodes; i++) {
118 sprintf(idname, "%2.2d", idbase + i);
119 if (badlist == NULL ||
120 strstr(badlist, idname) == 0) {
121 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
122 // b2nsm_sendreq(hostnode, "RF_CONFIGURE", 0, pars);
123 b2nsm_sendreq(hostnode, "RC_LOAD", 0, pars);
124 nnodes++;
125 }
126 }
127 // while (RFNSM_Status::Instance().get_flag() != nnodes) b2nsm_wait(1);
128 sleep(10);
129
130 printf("RFMaster:: distributor configured\n");
131
132 // 3. Configure collector
133 const char* collector = m_conf->getconf("collector", "nodename");
134 RFNSM_Status::Instance().set_flag(0);
135 // b2nsm_sendreq(collector, "RF_CONFIGURE", 0, pars);
136 b2nsm_sendreq(collector, "RC_LOAD", 0, pars);
137 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
138 printf("RFMaster:: collector configured\n");
139 // sleep ( 5 );
140
141 // 4. Configure
142 const char* roisender = m_conf->getconf("roisender", "nodename");
143 RFNSM_Status::Instance().set_flag(0);
144 // b2nsm_sendreq(roisender, "RF_CONFIGURE", 0, pars);
145 b2nsm_sendreq(roisender, "RC_LOAD", 0, pars);
146 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
147 printf("RFMaster:: roisender configured\n");
148
149 return 0;
150}
151
152int RFMaster::UnConfigure(NSMmsg* msgm, NSMcontext* /*msgc*/)
153{
154 int pars[10];
155 pars[0] = msgm->pars[0];
156 pars[1] = msgm->pars[1];
157
158 // Unconfigure RoiSender
159 const char* roisender = m_conf->getconf("roisender", "nodename");
160 RFNSM_Status::Instance().set_flag(0);
161 // b2nsm_sendreq(roisender, "RF_UNCONFIGURE", 0, pars);
162 b2nsm_sendreq(roisender, "RC_ABORT", 0, pars);
163 while (RFNSM_Status::Instance().get_flag() == 1) b2nsm_wait(1);
164
165 // Unconfigure collector
166 const char* collector = m_conf->getconf("collector", "nodename");
167 RFNSM_Status::Instance().set_flag(0);
168 // b2nsm_sendreq(collector, "RF_UNCONFIGURE", 0, pars);
169 b2nsm_sendreq(collector, "RC_ABORT", 0, pars);
170 while (RFNSM_Status::Instance().get_flag() == 1) b2nsm_wait(1);
171
172 // Unconfigure event processors
173 int maxnodes = m_conf->getconfi("processor", "nnodes");
174 int idbase = m_conf->getconfi("processor", "idbase");
175 const char* hostbase = m_conf->getconf("processor", "nodebase");
176 char* badlist = m_conf->getconf("processor", "badlist");
177
178 char hostnode[512], idname[3];
179 RFNSM_Status::Instance().set_flag(0);
180 int nnodes = 0;
181 for (int i = 0; i < maxnodes; i++) {
182 sprintf(idname, "%2.2d", idbase + i);
183 if (badlist == NULL ||
184 strstr(badlist, idname) == 0) {
185 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
186 // b2nsm_sendreq(hostnode, "RF_UNCONFIGURE", 0, pars);
187 b2nsm_sendreq(hostnode, "RC_ABORT", 0, pars);
188 nnodes++;
189 }
190 }
191#ifdef DESY
192 b2nsm_wait(5);
193#else
194 // printf ( "RFMaster : Unconfigure : started. - nnodes=%d, flag=%d\n",
195 // nnodes, RFNSM_Status::Instance().get_flag() );
196 while (RFNSM_Status::Instance().get_flag() != nnodes) {
197 b2nsm_wait(1);
198 // printf ( "RFMaster : Unconfigure : in prog. - nnodes=%d, flag=%d\n",
199 // nnodes, RFNSM_Status::Instance().get_flag() );
200 }
201 // printf ( "RFMaster : Unconfigure - done: nnodes=%d, flag=%d\n",
202 // nnodes, RFNSM_Status::Instance().get_flag() );
203#endif
204
205 // Unconfigure DqmServer
206 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
207 RFNSM_Status::Instance().set_flag(0);
208 // b2nsm_sendreq(dqmserver, "RF_UNCONFIGURE", 0, pars);
209 b2nsm_sendreq(dqmserver, "RC_ABORT", 0, pars);
210 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
211
212 // Unconfigure distributor
213 const char* distributor = m_conf->getconf("distributor", "nodename");
214 RFNSM_Status::Instance().set_flag(0);
215 // b2nsm_sendreq(distributor, "RF_UNCONFIGURE", 0, pars);
216 b2nsm_sendreq(distributor, "RC_ABORT", 0, pars);
217 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
218
219 printf("RFMaster : Unconfigure done.\n");
220 fflush(stdout);
221
222 return 0;
223
224}
225
226int RFMaster::Start(NSMmsg* msgm, NSMcontext* /*msgc*/)
227{
228 int pars[10];
229 pars[0] = msgm->pars[0];
230 pars[1] = msgm->pars[1];
231 printf("RFMaster : Start exp=%d, run=%d\n", pars[0], pars[1]);
232 fflush(stdout);
233
234 // 0. Start DQMserver
235 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
236 RFNSM_Status::Instance().set_flag(0);
237 // b2nsm_sendreq(dqmserver, "RF_STOP", 0, pars);
238 b2nsm_sendreq(dqmserver, "RC_START", 2, pars);
239 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
240
241 // 1. Start worker nodes
242 int maxnodes = m_conf->getconfi("processor", "nnodes");
243 int idbase = m_conf->getconfi("processor", "idbase");
244 const char* hostbase = m_conf->getconf("processor", "nodebase");
245 char* badlist = m_conf->getconf("processor", "badlist");
246
247 char hostnode[512], idname[3];
248 RFNSM_Status::Instance().set_flag(0);
249 int nnodes = 0;
250 for (int i = 0; i < maxnodes; i++) {
251 sprintf(idname, "%2.2d", idbase + i);
252 if (badlist == NULL ||
253 strstr(badlist, idname) == 0) {
254 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
255 // b2nsm_sendreq(hostnode, "RF_STOP", 0, pars);
256 b2nsm_sendreq(hostnode, "RC_START", 2, pars);
257 nnodes++;
258 }
259 }
260
261 return 0;
262}
263
264int RFMaster::Stop(NSMmsg* msg, NSMcontext*)
265{
266 int pars[10];
267 pars[0] = msg->pars[0];
268 pars[1] = msg->pars[1];
269
270 // 1. Stop worker nodes
271 // Unconfigure event processors
272 int maxnodes = m_conf->getconfi("processor", "nnodes");
273 int idbase = m_conf->getconfi("processor", "idbase");
274 const char* hostbase = m_conf->getconf("processor", "nodebase");
275 char* badlist = m_conf->getconf("processor", "badlist");
276
277 char hostnode[512], idname[3];
278 RFNSM_Status::Instance().set_flag(0);
279 int nnodes = 0;
280 for (int i = 0; i < maxnodes; i++) {
281 sprintf(idname, "%2.2d", idbase + i);
282 if (badlist == NULL ||
283 strstr(badlist, idname) == 0) {
284 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
285 // b2nsm_sendreq(hostnode, "RF_STOP", 0, pars);
286 b2nsm_sendreq(hostnode, "RC_STOP", 0, pars);
287 nnodes++;
288 }
289 }
290#ifdef DESY
291 b2nsm_wait(5);
292#else
293 while (RFNSM_Status::Instance().get_flag() != nnodes) b2nsm_wait(1);
294#endif
295
296 // 2. Stop DqmServer node
297 // Unconfigure DqmServer
298 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
299 RFNSM_Status::Instance().set_flag(0);
300 // b2nsm_sendreq(dqmserver, "RF_STOP", 0, pars);
301 b2nsm_sendreq(dqmserver, "RC_STOP", 0, pars);
302 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
303
304 printf("RFMaster : Stopped. exp=%d, run=%d\n", pars[0], pars[1]);
305 fflush(stdout);
306
307 return 0;
308}
309
310
311int RFMaster::Restart(NSMmsg*, NSMcontext*)
312{
313 int* pars = nullptr;
314
315 // 0. Configure DqmServer
316 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
317 // b2nsm_sendreq(dqmserver, "RF_RESTART", 0, pars);
318 b2nsm_sendreq(dqmserver, "RC_RECOVER", 0, pars);
319 sleep(2);
320
321 // 1. Configure distributor
322 const char* distributor = m_conf->getconf("distributor", "nodename");
323 // b2nsm_sendreq(distributor, "RF_RESTART", 0, pars);
324 b2nsm_sendreq(distributor, "RC_RECOVER", 0, pars);
325 sleep(2);
326
327 // 2. Configure event processors
328 int maxnodes = m_conf->getconfi("processor", "nnodes");
329 int idbase = m_conf->getconfi("processor", "idbase");
330 const char* hostbase = m_conf->getconf("processor", "hostbase");
331 char* badlist = m_conf->getconf("processor", "badlist");
332
333 char hostnode[512], idname[3];
334 for (int i = 0; i < maxnodes; i++) {
335 sprintf(idname, "%2.2d", idbase + i);
336 if (badlist == NULL ||
337 strstr(badlist, idname) == 0) {
338 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
339 // b2nsm_sendreq(hostnode, "RF_RESTART", 0, pars);
340 b2nsm_sendreq(hostnode, "RC_RECOVER", 0, pars);
341 }
342 }
343 sleep(2);
344
345 // 3. Configure collector
346 const char* collector = m_conf->getconf("collector", "nodename");
347 // b2nsm_sendreq(collector, "RF_RESTART", 0, pars);
348 b2nsm_sendreq(collector, "RC_RECOVER", 0, pars);
349
350 return 0;
351}
352
353// Server function
354
355void RFMaster::monitor_loop()
356{
357 while (true) {
358 b2nsm_wait(10);
359 }
360}
361
362
363
Abstract base class for different kinds of events.
STL namespace.