Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
frame_farm.cc
Go to the documentation of this file.
1 #include "frame_farm.hh"
3 #include <log.hh>
4 
5 /**
6  * \author cpellegrino
7  */
8 
9 static const useconds_t sc_sleep = 100000; // 100000 = 100 ms, 10000 = 10 ms
10 
11 static const int sc_expsec = 0;
12 
13 static const int sc_expusec = 500000;
14 
16 {
17  for (
18  container_t::iterator it = m_container.begin(), et = m_container.end();
19  it != et;
20  ++it
21  ) {
22  delete it->second;
23  }
24 
25  for (
26  buffers_collector_t::iterator it = m_empty_buffers.begin(), et = m_empty_buffers.end();
27  it != et;
28  ++it
29  ) {
30  delete *it;
31  }
32 }
33 
35 {
36  frame.clear();
37  // Block until data is coming
38  PuzzledFrame* pframe = static_cast<PuzzledFrame*>(m_frames.get());
39 
40  if (pframe) {
41  pframe->getFrame(frame);
42 
43  pframe->reset();
44 
45  boost::mutex::scoped_lock lock(m_ebf_mutex);
46 
47  if (m_empty_buffers.size() > 1000) {
48  delete pframe;
49  } else {
50  m_empty_buffers.push_back(pframe);
51  }
52  }
53 }
54 
56 {
57  /** This method runs on an independent thread the polling of
58  * completed or expired frames.
59  *
60  *
61  **/
62 
63  m_running = true;
64 
65  while (m_running.load()) {
66  {
67  boost::mutex::scoped_lock lock(m_mutex);
68 
69  container_t::iterator it = m_container.begin();
70 
71  container_t::iterator et = m_container.end();
72 
73  m_frames.lock();
74 
75  Timer const t(0, 0);
76 
77  while (it != et) {
78  PuzzledFrame* pframe = it->second;
79  if (pframe->hasExpiredWrt(t)) {
80  // Signal the completion!
81  m_container.erase(it++);
82  et = m_container.end();
83 
84  benchmark::chrono_set(*pframe);
85 
86  m_frames.put_nolock(pframe);
87  } else {
88  ++it;
89  }
90  }
91  m_frames.signal();
92  m_frames.unlock();
93  }
94  usleep(sc_sleep);
95  }
96 }
97 
99 {
100  if (m_max_dump_size && (!datagram->hasValidTimeStamp())) {
101  if (!m_dumpfile.write(datagram, m_detector_id)) {
102  static unsigned int n = 0;
103  const unsigned int n_iter = 10000;
104  if (n % n_iter == 0) {
105  LOG_DEBUG
106  << "Time-unreliable data detected. Dumped to file. "
107  << "This message has a suppression factor of " << n_iter;
108  n = 1;
109  }
110  }
111  Log::Counter::get().add("dumped dgrams");
113  return false;
114  }
115 
116  CLBCommonHeader const*const header = datagram->getCLBCommonHeader();
117 
118  int32_t const seq_num = seq_number(
119  *header,
122 
123  if (seq_num < 0) {
124  LOG_DEBUG << "Discarding data which timestamp is before the run start time. "
125  << "DOM ID: " << header->domIdentifier() << ", "
126  << "seq_num: " << seq_num << ", "
127  << "run start time in ms: " << m_start_run_ms << ", "
128  << "time stamp secs: " << header->timeStamp().sec() << ", "
129  << "timeslice duration in ms: " << m_timeslice_duration;
130 
131  Log::Counter::get().add("old data");
133  return false;
134  }
135 
136  if (
137  m_run_number != -1
138  && header->runNumber() != static_cast<unsigned int>(m_run_number)
139  ) {
140  LOG_DEBUG << "Discarding data which run number ("
141  << header->runNumber()
142  << ") is different from the one of the current run: "
143  << runNumber();
144  Log::Counter::get().add("wrong runno");
146  return false;
147  }
148 
149  frame_idx_t const idx = data2idx(
150  *header,
153 
154  bool insert_value;
155 
156  boost::mutex::scoped_lock lock(m_mutex);
157 
158  benchmark::chrono_set(*datagram);
159 
160  container_t::const_iterator const it = m_container.find(idx);
161 
162  benchmark::chrono_set(*datagram);
163 
164  if (it != m_container.end()) {
165  insert_value = it->second->insert(datagram);
166  } else {
167  PuzzledFrame* const pframe = getEmptyPuzzledFrame();
168 
169  pframe->setSeqNumber(seq_num);
170  pframe->setFrameIndex(idx);
171 
172  pframe->setDataType(header->dataType());
173  pframe->setDetectorId(m_detector_id);
174  insert_value = pframe->insert(datagram);
175 
176  container_t::value_type const pair(idx, pframe);
177 
178  m_container.insert(pair);
179  }
180 
181  return insert_value;
182 }
183 
185 {
186  PuzzledFrame* pframe;
187 
188  boost::mutex::scoped_lock lock(m_ebf_mutex);
189 
190  if (m_empty_buffers.size()) {
191  pframe = m_empty_buffers.front();
192 
193  m_empty_buffers.pop_front();
194  lock.unlock();
195  } else {
196  lock.unlock();
197  pframe = new PuzzledFrame;
198  }
199 
200  pframe->setTimer(Timer(sc_expsec, sc_expusec));
201 
202  return pframe;
203 }
void chrono_set(chronometrable &chr)
static const useconds_t sc_sleep
Definition: frame_farm.cc:9
static const int sc_expusec
Definition: frame_farm.cc:13
int runNumber() const
Definition: frame_farm.hh:87
bool hasExpiredWrt(Timer const &t) const
#define LOG_DEBUG
Definition: log.hh:109
uint32_t domIdentifier() const
uint32_t sec() const
Definition: utctime.hh:17
bool hasValidTimeStamp() const
const CLBCommonHeader * getCLBCommonHeader() const
Definition: clb_datagram.hh:90
void putDataGram(CLBDataGram *p)
bool write(const CLBDataGram *data, unsigned int detector_id)
Definition: dump_file.hh:74
uint32_t runNumber() const
frame_idx_t data2idx(CLBCommonHeader const &header, uint64_t start_run_ms, int ts_duration_ms)
static Counter & get()
Definition: log.hh:53
UTCTime timeStamp() const
bool insert(CLBDataGram *datagram)
void operator()()
Definition: frame_farm.cc:55
PuzzledFrame * getEmptyPuzzledFrame()
Definition: frame_farm.cc:184
boost::mutex m_mutex
Definition: frame_farm.hh:35
DumpFile m_dumpfile
Definition: frame_farm.hh:50
uint64_t frame_idx_t
void setDetectorId(unsigned int detector_id)
buffers_collector_t m_empty_buffers
Definition: frame_farm.hh:33
void setTimer(const Timer &exp)
std::size_t m_max_dump_size
Definition: frame_farm.hh:45
bool insert(CLBDataGram *datagram)
Definition: frame_farm.cc:98
void setDataType(unsigned int datatype)
static const int sc_expsec
Definition: frame_farm.cc:11
unsigned int m_timeslice_duration
Definition: frame_farm.hh:41
ptrDispatcher m_frames
Definition: frame_farm.hh:31
void add(std::string const &tag)
Definition: log.hh:60
void setFrameIndex(frame_idx_t frame_idx)
container_t m_container
Definition: frame_farm.hh:29
boost::atomic< bool > m_running
Definition: frame_farm.hh:39
void put_nolock(void *pointer)
int32_t seq_number(CLBCommonHeader const &header, uint64_t start_run_ms, int ts_duration_ms)
boost::mutex m_ebf_mutex
Definition: frame_farm.hh:37
static InBufferCollector & getCollector()
uint32_t dataType() const
void getFrame(Frame &frame)
Definition: frame_farm.cc:34
unsigned int m_detector_id
Definition: frame_farm.hh:48
Definition: frame.hh:12
void getFrame(Frame &frame)
Definition: timer.hh:10
void setSeqNumber(unsigned int seqnumber)
uint64_t m_start_run_ms
Definition: frame_farm.hh:43
int m_run_number
Definition: frame_farm.hh:47