Belle II Software  release-05-01-25
ERecoEventSampler.cc
1 //+
2 // File : ERecoEventSampler.cc
3 // Description : Receive events from Storage and distribute them to
4 // processing nodes
5 //
6 // Author : Ryosuke Itoh, IPNS, KEK
7 // Date : 31 - Jul - 2017
8 //-
9 
10 #include "daq/expreco/ERecoEventSampler.h"
11 
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16 
17 #include <csignal>
18 #include <iostream>
19 
20 #define RFEVSOUT stdout
21 
22 using namespace std;
23 using namespace Belle2;
24 
25 ERecoEventSampler::ERecoEventSampler(string conffile)
26 {
27 
28  // 0. Initialize configuration manager
29  m_conffile = conffile;
30  m_conf = new RFConf(conffile.c_str());
31  char* nodename = m_conf->getconf("eventsampler", "nodename");
32 
33  // 1. Set execution directory
34  string execdir = string(m_conf->getconf("system", "execdir_base")) + "/sampler";
35 
36  mkdir(execdir.c_str(), 0755);
37  chdir(execdir.c_str());
38 
39  // 2. Initialize RingBuffers
40  char* rbufout = m_conf->getconf("eventsampler", "ringbufout");
41  int rbufsize = m_conf->getconfi("eventsampler", "ringbufoutsize");
42  m_rbufout = new RingBuffer(rbufout, rbufsize);
43 
44  // 3. Initialize process manager
45  m_proc = new RFProcessManager(nodename);
46 
47  // 4. Initialize LogManager
48  m_log = new RFLogManager(nodename, m_conf->getconf("system", "lognode"));
49 
50  m_pid_sampler = 0;
51 
52 }
53 
54 ERecoEventSampler::~ERecoEventSampler()
55 {
56  delete m_log;
57  delete m_proc;
58  delete m_conf;
59  delete m_rbufout;
60 }
61 
62 
63 // Functions hooked up by NSM2
64 
65 int ERecoEventSampler::Configure(NSMmsg*, NSMcontext*)
66 {
67 
68  /*
69  // 1. Run EventSampler
70  char* sampler = m_conf->getconf("eventsampler", "script");
71  m_pid_sampler = m_proc->Execute(sampler, (char*)m_conffile.c_str());
72  */
73 
74  /* Public event server moved outside
75  // 2. Run EventServer
76  char* server = m_conf->getconf("eventsampler", "server", "script");
77  char* rbuf = m_conf->getconf("eventsampler", "ringbufout");
78  char* port = m_conf->getconf("eventsampler", "server", "port");
79  m_pid_server = m_proc->Execute(server, rbuf, port);
80  */
81 
82  printf("ERecoEventSampler : Configure done\n");
83  return 0;
84 }
85 
86 int ERecoEventSampler::UnConfigure(NSMmsg*, NSMcontext*)
87 {
88  /*
89  int status;
90  printf("ERecoEventSampler: Unconfigure pids = %d %d\n", m_pid_sampler, m_pid_server);
91  fflush(stdout);
92  if (m_pid_sampler != 0) {
93  kill(m_pid_sampler, SIGINT);
94  waitpid(m_pid_sampler, &status, 0);
95  m_pid_sampler = 0;
96  }
97  */
98  /*
99  if (m_pid_server != 0) {
100  kill(m_pid_server, SIGINT);
101  waitpid(m_pid_server, &status, 0);
102  m_pid_server = 0;
103  }
104  */
105  printf("ERecoEventSampler : Unconfigure done\n");
106  return 0;
107 }
108 
109 int ERecoEventSampler::Start(NSMmsg*, NSMcontext*)
110 {
111  // 1. Run EventSampler
112  char* sampler = m_conf->getconf("eventsampler", "script");
113  m_pid_sampler = m_proc->Execute(sampler, (char*)m_conffile.c_str());
114 
115  /* Public event server moved outside
116  // 2. Run EventServer
117  char* server = m_conf->getconf("eventsampler", "server", "script");
118  char* rbuf = m_conf->getconf("eventsampler", "ringbufout");
119  char* port = m_conf->getconf("eventsampler", "server", "port");
120  m_pid_server = m_proc->Execute(server, rbuf, port);
121  */
122 
123  printf("ERecoEventSampler : Configure done\n");
124 
125 
126  // m_rbufin->clear();
127  return 0;
128 }
129 
130 int ERecoEventSampler::Stop(NSMmsg*, NSMcontext*)
131 {
132  int status;
133  printf("ERecoEventSampler: Unconfigure pids = %d %d\n", m_pid_sampler, m_pid_server);
134  fflush(stdout);
135  if (m_pid_sampler != 0) {
136  kill(m_pid_sampler, SIGINT);
137  waitpid(m_pid_sampler, &status, 0);
138  m_pid_sampler = 0;
139  }
140 
141 // m_rbufin->clear();
142  return 0;
143 }
144 
145 
146 int ERecoEventSampler::Restart(NSMmsg*, NSMcontext*)
147 {
148 }
149 
150 // Server function
151 
152 void ERecoEventSampler::server()
153 {
154  while (true) {
155  pid_t pid = m_proc->CheckProcess();
156  if (pid > 0) {
157  if (pid == m_pid_sampler)
158  printf("ERecoEventSampler : eventsampler process dead. pid = %d\n", pid);
159  else
160  printf("ERecoEventSampler : unknown process dead. pid = %d\n", pid);
161  }
162 
163  int st = m_proc->CheckOutput();
164  if (st < 0) {
165  perror("ERecoEventSampler::server");
166  // exit ( -1 );
167  } else if (st > 0) {
168  m_log->ProcessLog(m_proc->GetFd());
169  }
170  }
171 }
172 
173 
174 
175 
NSMmsg
Definition: nsm2.h:217
Belle2::RFLogManager
Definition: RFLogManager.h:18
Belle2::RingBuffer
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:36
Belle2::RFProcessManager
Definition: RFProcessManager.h:22
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RFConf
Definition: RFConf.h:24
NSMcontext_struct
Definition: nsmlib2.h:66