Belle II Software development
ERecoDistributor.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/expreco/ERecoDistributor.h"
10
11#include <sys/stat.h>
12#include <sys/types.h>
13#include <sys/wait.h>
14#include <unistd.h>
15
16#include <csignal>
17#include <cstring>
18#include <iostream>
19
20#define RFEVSOUT stdout
21
22using namespace std;
23using namespace Belle2;
24
25ERecoDistributor::ERecoDistributor(const string& conffile)
26{
27 // 0. Initialize configuration manager
28 m_conf = new RFConf(conffile.c_str());
29 char* nodename = m_conf->getconf("distributor", "nodename");
30 // char nodename[256];
31 // gethostname ( nodename, sizeof(nodename) );
32
33 // 1. Set execution directory
34 string execdir = string(m_conf->getconf("system", "execdir_base")) + "/distributor";
35
36 mkdir(execdir.c_str(), 0755);
37 chdir(execdir.c_str());
38
39 // 2. Initialize local shared memory
40 string shmname = string(m_conf->getconf("system", "unitname")) + ":" +
41 string(m_conf->getconf("distributor", "nodename"));
42 m_shm = new RFSharedMem(const_cast<char*>(shmname.c_str()));
43
44 // 3. Initialize process manager
45 m_proc = new RFProcessManager(nodename);
46
47 // 4. Initialize RingBuffers
48 // char* ringbuf = m_conf->getconf("distributor", "ringbuffer");
49 string ringbuf = string(m_conf->getconf("system", "unitname")) + ":" +
50 string(m_conf->getconf("distributor", "ringbuffer"));
51 int rbufsize = m_conf->getconfi("distributor", "ringbuffersize");
52 m_rbufin = new RingBuffer(ringbuf.c_str(), rbufsize);
53
54 // 5. Initialize LogManager
55 m_log = new RFLogManager(nodename, m_conf->getconf("system", "lognode"));
56
57 // 6. Initialize data flow monitor
58 m_flow = new RFFlowStat(const_cast<char*>(shmname.c_str()));
59
60}
61
62ERecoDistributor::~ERecoDistributor()
63{
64 delete m_log;
65 delete m_proc;
66 delete m_shm;
67 delete m_conf;
68 delete m_flow;
69 delete m_rbufin;
70}
71
72
73// Functions hooked up by NSM2
74
75int ERecoDistributor::Configure(NSMmsg*, NSMcontext*)
76{
77 // 0. Global parameters
78 // char* ringbuf = m_conf->getconf("distributor", "ringbuffer");
79 string ringbuf = string(m_conf->getconf("system", "unitname")) + ":" +
80 string(m_conf->getconf("distributor", "ringbuffer"));
81
82 // char* shmname = m_conf->getconf("distributor", "nodename");
83 string shmname = string(m_conf->getconf("system", "unitname")) + ":" +
84 string(m_conf->getconf("distributor", "nodename"));
85
86 // 1. Run sender
87 m_nnodes = 0;
88 int maxnodes = m_conf->getconfi("processor", "nnodes");
89 int idbase = m_conf->getconfi("processor", "idbase");
90 //char* hostbase = m_conf->getconf("processor", "hostbase");
91 char* badlist = m_conf->getconf("processor", "badlist");
92
93 char* sender = m_conf->getconf("distributor", "sender", "script");
94 int portbase = m_conf->getconfi("distributor", "sender", "portbase");
95
96
97 char hostname[512], idname[12], shmid[12];
98 for (int i = 0; i < maxnodes; i++) {
99 snprintf(idname, sizeof(idname), "%2.2d", idbase + i);
100 snprintf(shmid, sizeof(shmid), "%2.2d", i);
101 if (badlist == NULL ||
102 strstr(badlist, idname) == 0) {
103 int port = (idbase + i) + portbase;
104 char portchar[256];
105 sprintf(portchar, "%d", port);
106 m_pid_sender[m_nnodes] = m_proc->Execute(sender, const_cast<char*>(ringbuf.c_str()), portchar, const_cast<char*>(shmname.c_str()),
107 shmid);
108 printf("Running sender to %d\n", port);
109 fflush(stdout);
110 m_nnodes++;
111 }
112 }
113
114 // 2. Run receiver
115 m_nrecv = 0;
116 int maxrecv = m_conf->getconfi("distributor", "receiver", "nnodes");
117 int ridbase = m_conf->getconfi("distributor", "receiver", "idbase");
118 const char* rhostbase = m_conf->getconf("distributor", "receiver", "hostbase");
119 char* rbadlist = m_conf->getconf("distributor", "receiver", "badlist");
120 char* port = m_conf->getconf("distributor", "receiver", "port");
121 char* receiver = m_conf->getconf("distributor", "receiver", "script");
122
123 // char hostname[512], idname[12], shmid[12];
124 for (int i = 0; i < maxrecv; i++) {
125 sprintf(hostname, "%s%2.2d", rhostbase, ridbase + i);
126 snprintf(idname, sizeof(idname), "%2.2d", ridbase + i);
127 snprintf(shmid, sizeof(shmid), "%2.2d", i + 20);
128 if (rbadlist == NULL ||
129 strstr(rbadlist, hostname) == 0) {
130 printf("Running receiver for %s\n", hostname);
131 fflush(stdout);
132 m_pid_recv[m_nrecv] = m_proc->Execute(receiver, const_cast<char*>(ringbuf.c_str()), hostname, port,
133 const_cast<char*>(shmname.c_str()), shmid);
134 m_nrecv++;
135 }
136 }
137
138 printf("ERecoDistributor : Configure done\n");
139 return 0;
140}
141
142int ERecoDistributor::UnConfigure(NSMmsg*, NSMcontext*)
143{
144 // system("killall sock2rbr rb2sockr");
145 int status;
146
147 printf("m_nrecv = %d\n", m_nrecv);
148 for (int i = 0; i < m_nrecv; i++) {
149 if (m_pid_recv[i] != 0) {
150 printf("ERecoDistributor : killing receiver pid=%d\n", m_pid_recv[i]);
151 kill(m_pid_recv[i], SIGKILL);
152 waitpid(m_pid_recv[i], &status, 0);
153 }
154 }
155
156 printf("m_nnodes = %d\n", m_nnodes);
157 for (int i = 0; i < m_nnodes; i++) {
158 if (m_pid_sender[i] != 0) {
159 printf("ERecoDistributor : killing sender pid=%d\n", m_pid_sender[i]);
160 // kill(m_pid_sender[i], SIGINT);
161 kill(m_pid_sender[i], SIGKILL);
162 waitpid(m_pid_sender[i], &status, 0);
163 }
164 }
165 // Clear RingBuffer
166 m_rbufin->forceClear();
167
168 // Clear process list
169 m_flow->fillProcessStatus(GetNodeInfo());
170
171 printf("ERecoDistributor : Unconfigure done\n");
172 return 0;
173}
174
175int ERecoDistributor::Start(NSMmsg*, NSMcontext*)
176{
177 m_rbufin->clear();
178 return 0;
179}
180
181
182int ERecoDistributor::Restart(NSMmsg*, NSMcontext*)
183{
184 printf("ERecoDistributor : Restarting!!!!!\n");
185 NSMmsg* nsmmsg = NULL;
186 NSMcontext* nsmcontext = NULL;
187 ERecoDistributor::UnConfigure(nsmmsg, nsmcontext);
188 sleep(2);
189 ERecoDistributor::Configure(nsmmsg, nsmcontext);
190 return 0;
191}
192
193// Server function
194
195void ERecoDistributor::server()
196{
197 // int nevt = 0;
198 m_flow->fillProcessStatus(GetNodeInfo());
199
200 while (true) {
201 pid_t pid = m_proc->CheckProcess();
202 if (pid > 0) {
203 printf("ERecoDistributor : process dead. pid = %d\n", pid);
204 for (int i = 0; i < m_nrecv; i++) {
205 if (pid == m_pid_recv[i]) {
206 m_log->Fatal("ERecoDistributor : receiver process (%d) dead. pid=%d\n", i, pid);
207 m_pid_recv[i] = 0;
208 }
209 }
210 for (int i = 0; i < m_nnodes; i++) {
211 if (pid == m_pid_sender[i]) {
212 m_log->Fatal("ERecoDistributor : sender process (%d) dead. pid=%d\n", i, m_pid_sender[i]);
213 m_pid_sender[i] = 0;
214 }
215 }
216 }
217
218 int st = m_proc->CheckOutput();
219 if (st < 0) {
220 perror("ERecoDistributor::server");
221 // exit ( -1 );
222 } else if (st > 0) {
223 m_log->ProcessLog(m_proc->GetFd());
224 }
225 }
226 // m_flow->fillNodeInfo(RF_INPUT_ID, GetNodeInfo(), false);
227 // m_flow->fillProcessStatus(GetNodeInfo(), m_pid_recv, m_pid_sender[sender_id]);
228 // m_flow->fillNodeInfo(0, GetNodeInfo(), false);
229 // Debug
230 // RfNodeInfo* info = GetNodeInfo();
231 //info->nevent_in = nevt++;
232 // info->nqueue_in = nevt;
233 // printf ( "FillNodeInfo called!! info->nevent_in = %d\n", info->nevent_in );
234}
235
236
237
238
Abstract base class for different kinds of events.
STL namespace.