CMS 3D CMS Logo

FedRawDataInputSource.h
Go to the documentation of this file.
1 #ifndef EventFilter_Utilities_FedRawDataInputSource_h
2 #define EventFilter_Utilities_FedRawDataInputSource_h
3 
4 #include <condition_variable>
5 #include <cstdio>
6 #include <filesystem>
7 #include <memory>
8 #include <mutex>
9 #include <thread>
10 #include <random>
11 #include <algorithm>
12 
13 #include "oneapi/tbb/concurrent_queue.h"
14 #include "oneapi/tbb/concurrent_vector.h"
15 
24 
27 
29 class InputSourceDescription;
30 class ParameterSet;
31 
32 class InputFile;
33 struct InputChunk;
34 
35 namespace evf {
37  namespace FastMonState {
38  enum InputState : short;
39  }
40 } // namespace evf
41 
43  friend class InputFile;
44  friend struct InputChunk;
45 
46 public:
48  ~FedRawDataInputSource() override;
49  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
50 
51  std::pair<bool, unsigned int> getEventReport(unsigned int lumi, bool erase);
52 
53 protected:
54  Next checkNext() override;
55  void read(edm::EventPrincipal& eventPrincipal) override;
58 
59 private:
60  void rewind_() override;
61 
62  void maybeOpenNewLumiSection(const uint32_t lumiSection);
66 
67  void readSupervisor();
68  void readWorker(unsigned int tid);
69  void threadError();
71 
72  //functions for single buffered reader
74 
75  //monitoring
76  void reportEventsThisLumiInSource(unsigned int lumi, unsigned int events);
77 
78  long initFileList();
80  std::string& nextFile,
81  uint32_t& fsize,
82  uint64_t& lockWaitTime);
83 
84  //variables
87 
89 
90  unsigned int eventChunkSize_; // for buffered read-ahead
91  unsigned int eventChunkBlock_; // how much read(2) asks at the time
92  unsigned int readBlocks_;
93  unsigned int numBuffers_;
94  unsigned int maxBufferedFiles_;
95  unsigned int numConcurrentReads_;
96  std::atomic<unsigned int> readingFilesCount_;
97 
98  // get LS from filename instead of event header
99  const bool getLSFromFilename_;
101  const bool verifyChecksum_;
102  const bool useL1EventID_;
103  const std::vector<unsigned int> testTCDSFEDRange_;
104  std::vector<std::string> fileNames_;
106  //std::vector<std::string> fileNamesSorted_;
107 
108  const bool fileListMode_;
109  unsigned int fileListIndex_ = 0;
110  const bool fileListLoopMode_;
111  unsigned int loopModeIterationInc_ = 0;
112 
115 
117 
118  std::unique_ptr<FRDEventMsgView> event_;
119 
122 
123  unsigned int currentLumiSection_;
124  uint32_t eventRunNumber_ = 0;
125  uint32_t GTPEventID_ = 0;
126  uint32_t L1EventID_ = 0;
127  unsigned char* tcds_pointer_;
128  unsigned int eventsThisLumi_;
129  unsigned long eventsThisRun_ = 0;
130 
133 
134  /*
135  *
136  * Multithreaded file reader
137  *
138  **/
139 
140  typedef std::pair<InputFile*, InputChunk*> ReaderInfo;
141 
142  uint16_t detectedFRDversion_ = 0;
143  std::unique_ptr<InputFile> currentFile_;
144  bool chunkIsFree_ = false;
145 
147  std::unique_ptr<std::thread> readSupervisorThread_;
148  std::vector<std::thread*> workerThreads_;
149 
150  tbb::concurrent_queue<unsigned int> workerPool_;
151  std::vector<ReaderInfo> workerJob_;
152 
153  tbb::concurrent_queue<InputChunk*> freeChunks_;
154  tbb::concurrent_queue<std::unique_ptr<InputFile>> fileQueue_;
155 
157  std::vector<std::condition_variable*> cvReader_;
158  std::vector<unsigned int> tid_active_;
159 
160  std::atomic<bool> quit_threads_;
161  std::vector<unsigned int> thread_quit_signal;
162  bool setExceptionState_ = false;
164  std::condition_variable startupCv_;
165 
167  std::list<std::pair<int, std::unique_ptr<InputFile>>> filesToDelete_;
168  std::list<std::pair<int, std::string>> fileNamesToDelete_;
170  std::vector<int> streamFileTracker_;
171  unsigned int nStreams_ = 0;
172  unsigned int checkEvery_ = 10;
173 
174  //supervisor thread wakeup
176  std::condition_variable cvWakeup_;
177 
178  //variables for the single buffered mode
180  int fileDescriptor_ = -1;
181  uint32_t bufferInputRead_ = 0;
182 
183  std::atomic<bool> threadInit_;
184 
185  std::map<unsigned int, unsigned int> sourceEventsReport_;
187 };
188 
189 struct InputChunk {
190  unsigned char* buf_;
191  InputChunk* next_ = nullptr;
194  //unsigned int index_;
196  unsigned int fileIndex_;
197  std::atomic<bool> readComplete_;
198 
200  buf_ = new unsigned char[size_];
201  reset(0, 0, 0);
202  }
203  void reset(uint64_t newOffset, uint64_t toRead, unsigned int fileIndex) {
204  offset_ = newOffset;
205  usedSize_ = toRead;
206  fileIndex_ = fileIndex;
207  readComplete_ = false;
208  }
209 
210  bool resize(uint64_t wantedSize, uint64_t maxSize) {
211  if (wantedSize > maxSize)
212  return false;
213  if (size_ < wantedSize) {
214  size_ = uint64_t(wantedSize * 1.05);
215  delete[] buf_;
216  buf_ = new unsigned char[size_];
217  }
218  return true;
219  }
220 
221  ~InputChunk() { delete[] buf_; }
222 };
223 
224 class InputFile {
225 public:
228  unsigned int lumi_;
230  //used by DAQSource
231  std::vector<std::string> fileNames_;
232  std::vector<uint64_t> diskFileSizes_;
233  std::vector<uint64_t> bufferOffsets_;
234  std::vector<uint64_t> fileSizes_;
235  std::vector<unsigned int> fileOrder_;
237  int rawFd_;
239  uint16_t rawHeaderSize_;
240  uint16_t nChunks_;
241  uint16_t numFiles_;
242  int nEvents_;
243  unsigned int nProcessed_;
244 
245  tbb::concurrent_vector<InputChunk*> chunks_;
246 
247  uint32_t bufferPosition_ = 0;
248  uint32_t chunkPosition_ = 0;
249  unsigned int currentChunk_ = 0;
250 
252  unsigned int lumi = 0,
253  std::string const& name = std::string(),
254  bool deleteFile = true,
255  int rawFd = -1,
256  uint64_t fileSize = 0,
257  uint16_t rawHeaderSize = 0,
258  uint16_t nChunks = 0,
259  int nEvents = 0,
260  FedRawDataInputSource* parent = nullptr)
261  : parent_(parent),
262  status_(status),
263  lumi_(lumi),
264  fileName_(name),
265  deleteFile_(deleteFile),
266  rawFd_(rawFd),
267  fileSize_(fileSize),
268  rawHeaderSize_(rawHeaderSize),
269  nChunks_(nChunks),
270  numFiles_(1),
271  nEvents_(nEvents),
272  nProcessed_(0) {
273  fileNames_.push_back(name);
274  fileOrder_.push_back(fileOrder_.size());
275  diskFileSizes_.push_back(fileSize);
276  fileSizes_.push_back(0);
277  bufferOffsets_.push_back(0);
278  chunks_.reserve(nChunks_);
279  for (unsigned int i = 0; i < nChunks; i++)
280  chunks_.push_back(nullptr);
281  }
282  virtual ~InputFile();
283 
284  void setChunks(uint16_t nChunks) {
285  nChunks_ = nChunks;
286  chunks_.clear();
287  chunks_.reserve(nChunks_);
288  for (unsigned int i = 0; i < nChunks_; i++)
289  chunks_.push_back(nullptr);
290  }
291 
293  size_t prevOffset = bufferOffsets_.back();
294  size_t prevSize = diskFileSizes_.back();
295  numFiles_++;
296  fileNames_.push_back(name);
297  fileOrder_.push_back(fileOrder_.size());
298  diskFileSizes_.push_back(size);
299  fileSizes_.push_back(0);
300  bufferOffsets_.push_back(prevOffset + prevSize);
301  }
302 
303  bool waitForChunk(unsigned int chunkid) {
304  //some atomics to make sure everything is cache synchronized for the main thread
305  return chunks_[chunkid] != nullptr && chunks_[chunkid]->readComplete_;
306  }
307  bool advance(unsigned char*& dataPosition, const size_t size);
308  void moveToPreviousChunk(const size_t size, const size_t offset);
309  void rewindChunk(const size_t size);
310  void unsetDeleteFile() { deleteFile_ = false; }
311  void randomizeOrder(std::default_random_engine& rng) {
312  std::shuffle(std::begin(fileOrder_), std::end(fileOrder_), rng);
313  }
314  uint64_t currentChunkSize() const { return chunks_[currentChunk_]->size_; }
315 };
316 
317 #endif // EventFilter_Utilities_FedRawDataInputSource_h
318 
size
Write out results.
void setChunks(uint16_t nChunks)
Definition: fillJson.h:27
std::vector< std::string > fileNames_
std::condition_variable cvWakeup_
void read(edm::EventPrincipal &eventPrincipal) override
std::vector< uint64_t > fileSizes_
std::pair< bool, unsigned int > getEventReport(unsigned int lumi, bool erase)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tbb::concurrent_queue< unsigned int > workerPool_
void maybeOpenNewLumiSection(const uint32_t lumiSection)
std::vector< std::string > fileNames_
evf::EvFDaqDirector::FileStatus getFile(unsigned int &ls, std::string &nextFile, uint32_t &fsize, uint64_t &lockWaitTime)
static std::mutex mutex
Definition: Proxy.cc:8
FedRawDataInputSource * parent_
tbb::concurrent_queue< std::unique_ptr< InputFile > > fileQueue_
bool advance(unsigned char *&dataPosition, const size_t size)
std::vector< uint64_t > bufferOffsets_
std::atomic< bool > quit_threads_
std::vector< ReaderInfo > workerJob_
edm::Timestamp fillFEDRawDataCollection(FEDRawDataCollection &rawData, bool &tcdsInRange)
std::pair< InputFile *, InputChunk * > ReaderInfo
std::unique_ptr< std::thread > readSupervisorThread_
std::vector< std::condition_variable * > cvReader_
uint16_t rawHeaderSize_
bool waitForChunk(unsigned int chunkid)
FedRawDataInputSource(edm::ParameterSet const &, edm::InputSourceDescription const &)
std::list< std::pair< int, std::unique_ptr< InputFile > > > filesToDelete_
std::list< std::pair< int, std::string > > fileNamesToDelete_
const std::vector< unsigned int > testTCDSFEDRange_
void rewindChunk(const size_t size)
void moveToPreviousChunk(const size_t size, const size_t offset)
std::vector< int > streamFileTracker_
void reset(uint64_t newOffset, uint64_t toRead, unsigned int fileIndex)
void randomizeOrder(std::default_random_engine &rng)
unsigned char * buf_
evf::EvFDaqDirector::FileStatus status_
void setMonStateSup(evf::FastMonState::InputState state)
std::map< unsigned int, unsigned int > sourceEventsReport_
edm::ProcessHistoryID processHistoryID_
const edm::DaqProvenanceHelper daqProvenanceHelper_
unsigned int fileIndex_
void appendFile(std::string const &name, uint64_t size)
std::vector< std::thread * > workerThreads_
void setMonState(evf::FastMonState::InputState state)
std::atomic< bool > readComplete_
InputFile(evf::EvFDaqDirector::FileStatus status, unsigned int lumi=0, std::string const &name=std::string(), bool deleteFile=true, int rawFd=-1, uint64_t fileSize=0, uint16_t rawHeaderSize=0, uint16_t nChunks=0, int nEvents=0, FedRawDataInputSource *parent=nullptr)
def ls(path, rec=False)
Definition: eostools.py:349
tbb::concurrent_vector< InputChunk * > chunks_
unsigned long long uint64_t
Definition: Time.h:13
std::unique_ptr< FRDEventMsgView > event_
unsigned int nProcessed_
uint32_t chunkPosition_
std::condition_variable startupCv_
ItemType state() const
Definition: InputSource.h:332
std::atomic< bool > threadInit_
unsigned int lumi_
evf::EvFDaqDirector * daqDirector_
void readWorker(unsigned int tid)
std::vector< unsigned int > fileOrder_
std::vector< unsigned int > thread_quit_signal
std::atomic< unsigned int > readingFilesCount_
uint64_t currentChunkSize() const
void reportEventsThisLumiInSource(unsigned int lumi, unsigned int events)
evf::FastMonitoringService * fms_
std::vector< unsigned int > tid_active_
unsigned int RunNumber_t
bool resize(uint64_t wantedSize, uint64_t maxSize)
InputChunk * next_
tbb::concurrent_queue< InputChunk * > freeChunks_
evf::EvFDaqDirector::FileStatus getNextEvent()
unsigned int currentChunk_
evf::EvFDaqDirector::FileStatus nextEvent()
bidiiter shuffle(bidiiter begin, bidiiter end, size_t num_random)
Definition: Utilities.h:27
void readNextChunkIntoBuffer(InputFile *file)
std::unique_ptr< InputFile > currentFile_
int events
std::vector< uint64_t > diskFileSizes_
InputChunk(uint64_t size)
std::string fileName_
uint32_t bufferPosition_