Belle II Software  release-08-01-10
ERecoEventSampler.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/ERecoEventSampler.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 <iostream>
18 
19 #define RFEVSOUT stdout
20 
21 using namespace std;
22 using namespace Belle2;
23 
24 ERecoEventSampler::ERecoEventSampler(string conffile)
25 {
26 
27  // 0. Initialize configuration manager
28  m_conffile = conffile;
29  m_conf = new RFConf(conffile.c_str());
30  char* nodename = m_conf->getconf("eventsampler", "nodename");
31 
32  // 1. Set execution directory
33  string execdir = string(m_conf->getconf("system", "execdir_base")) + "/sampler";
34 
35  mkdir(execdir.c_str(), 0755);
36  chdir(execdir.c_str());
37 
38  // 2. Initialize RingBuffers
39  char* rbufout = m_conf->getconf("eventsampler", "ringbufout");
40  int rbufsize = m_conf->getconfi("eventsampler", "ringbufoutsize");
41  m_rbufout = new RingBuffer(rbufout, rbufsize);
42 
43  // 3. Initialize process manager
44  m_proc = new RFProcessManager(nodename);
45 
46  // 4. Initialize LogManager
47  m_log = new RFLogManager(nodename, m_conf->getconf("system", "lognode"));
48 
49  m_pid_sampler = 0;
50 
51 }
52 
53 ERecoEventSampler::~ERecoEventSampler()
54 {
55  delete m_log;
56  delete m_proc;
57  delete m_conf;
58  delete m_rbufout;
59 }
60 
61 
62 // Functions hooked up by NSM2
63 
64 int ERecoEventSampler::Configure(NSMmsg*, NSMcontext*)
65 {
66 
67  /*
68  // 1. Run EventSampler
69  char* sampler = m_conf->getconf("eventsampler", "script");
70  m_pid_sampler = m_proc->Execute(sampler, (char*)m_conffile.c_str());
71  */
72 
73  /* Public event server moved outside
74  // 2. Run EventServer
75  char* server = m_conf->getconf("eventsampler", "server", "script");
76  char* rbuf = m_conf->getconf("eventsampler", "ringbufout");
77  char* port = m_conf->getconf("eventsampler", "server", "port");
78  m_pid_server = m_proc->Execute(server, rbuf, port);
79  */
80 
81  printf("ERecoEventSampler : Configure done\n");
82  return 0;
83 }
84 
85 int ERecoEventSampler::UnConfigure(NSMmsg*, NSMcontext*)
86 {
87  /*
88  int status;
89  printf("ERecoEventSampler: Unconfigure pids = %d %d\n", m_pid_sampler, m_pid_server);
90  fflush(stdout);
91  if (m_pid_sampler != 0) {
92  kill(m_pid_sampler, SIGINT);
93  waitpid(m_pid_sampler, &status, 0);
94  m_pid_sampler = 0;
95  }
96  */
97  /*
98  if (m_pid_server != 0) {
99  kill(m_pid_server, SIGINT);
100  waitpid(m_pid_server, &status, 0);
101  m_pid_server = 0;
102  }
103  */
104  printf("ERecoEventSampler : Unconfigure done\n");
105  return 0;
106 }
107 
108 int ERecoEventSampler::Start(NSMmsg*, NSMcontext*)
109 {
110  // 1. Run EventSampler
111  char* sampler = m_conf->getconf("eventsampler", "script");
112  m_pid_sampler = m_proc->Execute(sampler, (char*)m_conffile.c_str());
113 
114  /* Public event server moved outside
115  // 2. Run EventServer
116  char* server = m_conf->getconf("eventsampler", "server", "script");
117  char* rbuf = m_conf->getconf("eventsampler", "ringbufout");
118  char* port = m_conf->getconf("eventsampler", "server", "port");
119  m_pid_server = m_proc->Execute(server, rbuf, port);
120  */
121 
122  printf("ERecoEventSampler : Configure done\n");
123 
124 
125  // m_rbufin->clear();
126  return 0;
127 }
128 
129 int ERecoEventSampler::Stop(NSMmsg*, NSMcontext*)
130 {
131  int status;
132  printf("ERecoEventSampler: Unconfigure pids = %d %d\n", m_pid_sampler, m_pid_server);
133  fflush(stdout);
134  if (m_pid_sampler != 0) {
135  kill(m_pid_sampler, SIGINT);
136  waitpid(m_pid_sampler, &status, 0);
137  m_pid_sampler = 0;
138  }
139 
140 // m_rbufin->clear();
141  return 0;
142 }
143 
144 
145 int ERecoEventSampler::Restart(NSMmsg*, NSMcontext*)
146 {
147  return 0;
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 
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:39
Abstract base class for different kinds of events.
Definition: nsm2.h:224