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 <memory>
5 #include <cstdio>
6 #include <mutex>
7 #include <condition_variable>
8 #include <thread>
9 #include "tbb/concurrent_queue.h"
10 #include "tbb/concurrent_vector.h"
11 
12 #include "boost/filesystem.hpp"
13 
22 
24 
26 class InputSourceDescription;
27 class ParameterSet;
28 
29 struct InputFile;
30 struct InputChunk;
31 
32 namespace evf {
33 class FastMonitoringService;
34 }
35 
37 
38 friend struct InputFile;
39 friend struct InputChunk;
40 
41 public:
43  ~FedRawDataInputSource() override;
44  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
45 
46  std::pair<bool,unsigned int> getEventReport(unsigned int lumi, bool erase);
47 protected:
48  bool checkNextEvent() override;
49  void read(edm::EventPrincipal& eventPrincipal) override;
50 
51 private:
52  void rewind_() override;
53 
54  void maybeOpenNewLumiSection(const uint32_t lumiSection);
56  evf::EvFDaqDirector::FileStatus getNextEvent();
57  edm::Timestamp fillFEDRawDataCollection(FEDRawDataCollection&);
58  void deleteFile(std::string const&);
59 
60  void readSupervisor();
61  void readWorker(unsigned int tid);
62  void threadError();
63  bool exceptionState() {return setExceptionState_;}
64 
65  //functions for single buffered reader
66  void readNextChunkIntoBuffer(InputFile *file);
67 
68  //monitoring
69  void reportEventsThisLumiInSource(unsigned int lumi,unsigned int events);
70 
71  long initFileList();
72  evf::EvFDaqDirector::FileStatus getFile(unsigned int& ls, std::string& nextFile, uint32_t& fsize, uint64_t& lockWaitTime);
73 
74  //variables
76  evf::EvFDaqDirector* daqDirector_=nullptr;
77 
79 
80  unsigned int eventChunkSize_; // for buffered read-ahead
81  unsigned int eventChunkBlock_; // how much read(2) asks at the time
82  unsigned int readBlocks_;
83  unsigned int numBuffers_;
84  unsigned int maxBufferedFiles_;
85  unsigned int numConcurrentReads_;
86  std::atomic<unsigned int> readingFilesCount_;
87 
88  // get LS from filename instead of event header
89  const bool getLSFromFilename_;
91  const bool verifyAdler32_;
92  const bool verifyChecksum_;
93  const bool useL1EventID_;
94  std::vector<std::string> fileNames_;
96  //std::vector<std::string> fileNamesSorted_;
97 
98  const bool fileListMode_;
99  unsigned int fileListIndex_ = 0;
100  const bool fileListLoopMode_;
101  unsigned int loopModeIterationInc_ = 0;
102 
105 
107 
108  std::unique_ptr<FRDEventMsgView> event_;
109 
112 
113  unsigned int currentLumiSection_;
114  uint32_t eventRunNumber_=0;
115  uint32_t GTPEventID_ = 0;
116  uint32_t L1EventID_ = 0;
117  unsigned char *tcds_pointer_;
118  unsigned int eventsThisLumi_;
119  unsigned long eventsThisRun_ = 0;
120 
121  /*
122  *
123  * Multithreaded file reader
124  *
125  **/
126 
127  typedef std::pair<InputFile*,InputChunk*> ReaderInfo;
128 
129  uint32 detectedFRDversion_=0;
130  InputFile *currentFile_ = nullptr;
131  bool chunkIsFree_=false;
132 
133  bool startedSupervisorThread_ = false;
134  std::unique_ptr<std::thread> readSupervisorThread_;
135  std::vector<std::thread*> workerThreads_;
136 
137  tbb::concurrent_queue<unsigned int> workerPool_;
138  std::vector<ReaderInfo> workerJob_;
139 
140  tbb::concurrent_queue<InputChunk*> freeChunks_;
141  tbb::concurrent_queue<InputFile*> fileQueue_;
142 
144  std::vector<std::condition_variable*> cvReader_;
145  std::vector<unsigned int> tid_active_;
146 
147  std::atomic<bool> quit_threads_;
148  std::vector<unsigned int> thread_quit_signal;
149  bool setExceptionState_ = false;
151  std::condition_variable startupCv_;
152 
153  int currentFileIndex_ = -1;
154  std::list<std::pair<int,InputFile*>> filesToDelete_;
155  std::list<std::pair<int,std::string>> fileNamesToDelete_;
157  std::vector<int> streamFileTracker_;
158  unsigned int nStreams_ = 0;
159  unsigned int checkEvery_ = 10;
160 
161  //supervisor thread wakeup
163  std::condition_variable cvWakeup_;
164 
165  //variables for the single buffered mode
167  int fileDescriptor_ = -1;
168  uint32_t bufferInputRead_ = 0;
169 
170  std::atomic<bool> threadInit_;
171 
172  std::map<unsigned int,unsigned int> sourceEventsReport_;
174 };
175 
176 
177 struct InputChunk {
178  unsigned char * buf_;
179  InputChunk *next_ = nullptr;
180  uint32_t size_;
181  uint32_t usedSize_ = 0;
182  unsigned int index_;
183  unsigned int offset_;
184  unsigned int fileIndex_;
185  std::atomic<bool> readComplete_;
186 
187  InputChunk(unsigned int index, uint32_t size): size_(size),index_(index) {
188  buf_ = new unsigned char[size_];
189  reset(0,0,0);
190  }
191  void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex) {
192  offset_=newOffset;
193  usedSize_=toRead;
194  fileIndex_=fileIndex;
195  readComplete_=false;
196  }
197 
198  ~InputChunk() {delete[] buf_;}
199 };
200 
201 
202 struct InputFile {
205  unsigned int lumi_;
208  uint32_t nChunks_;
209  int nEvents_;
210  unsigned int nProcessed_;
211 
212  tbb::concurrent_vector<InputChunk*> chunks_;
213 
214  uint32_t bufferPosition_ = 0;
215  uint32_t chunkPosition_ = 0;
216  unsigned int currentChunk_ = 0;
217 
219  uint64_t fileSize =0, uint32_t nChunks=0, int nEvents=0, FedRawDataInputSource *parent = nullptr):
220  parent_(parent),
221  status_(status),
222  lumi_(lumi),
223  fileName_(name),
224  fileSize_(fileSize),
225  nChunks_(nChunks),
226  nEvents_(nEvents),
227  nProcessed_(0)
228  {
229  for (unsigned int i=0;i<nChunks;i++)
230  chunks_.push_back(nullptr);
231  }
232 
233  InputFile(std::string & name):fileName_(name) {}
234 
235  bool waitForChunk(unsigned int chunkid) {
236  //some atomics to make sure everything is cache synchronized for the main thread
237  return chunks_[chunkid]!=nullptr && chunks_[chunkid]->readComplete_;
238  }
239  bool advance(unsigned char* & dataPosition, const size_t size);
240  void moveToPreviousChunk(const size_t size, const size_t offset);
241  void rewindChunk(const size_t size);
242 };
243 
244 
245 #endif // EventFilter_Utilities_FedRawDataInputSource_h
246 
size
Write out results.
static boost::mutex mutex
Definition: Proxy.cc:11
unsigned int lumi_
std::vector< std::string > fileNames_
std::condition_variable cvWakeup_
unsigned int offset_
tbb::concurrent_queue< unsigned int > workerPool_
evf::EvFDaqDirector::FileStatus status_
unsigned int index_
std::atomic< bool > quit_threads_
std::vector< ReaderInfo > workerJob_
InputChunk(unsigned int index, uint32_t size)
std::unique_ptr< std::thread > readSupervisorThread_
std::vector< std::condition_variable * > cvReader_
FedRawDataInputSource * parent_
void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex)
InputFile(evf::EvFDaqDirector::FileStatus status, unsigned int lumi=0, std::string const &name=std::string(), uint64_t fileSize=0, uint32_t nChunks=0, int nEvents=0, FedRawDataInputSource *parent=0)
std::vector< int > streamFileTracker_
unsigned char * buf_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
InputFile(std::string &name)
edm::ProcessHistoryID processHistoryID_
std::map< unsigned int, unsigned int > sourceEventsReport_
bool waitForChunk(unsigned int chunkid)
const edm::DaqProvenanceHelper daqProvenanceHelper_
unsigned int fileIndex_
std::vector< std::thread * > workerThreads_
unsigned int uint32
Definition: MsgTools.h:13
std::atomic< bool > readComplete_
def ls(path, rec=False)
Definition: eostools.py:348
std::list< std::pair< int, InputFile * > > filesToDelete_
unsigned int nProcessed_
unsigned long long uint64_t
Definition: Time.h:15
tbb::concurrent_queue< InputFile * > fileQueue_
std::unique_ptr< FRDEventMsgView > event_
std::string fileName_
std::condition_variable startupCv_
tbb::concurrent_vector< InputChunk * > chunks_
std::atomic< bool > threadInit_
std::list< std::pair< int, std::string > > fileNamesToDelete_
std::pair< InputFile *, InputChunk * > ReaderInfo
std::vector< unsigned int > thread_quit_signal
std::atomic< unsigned int > readingFilesCount_
std::vector< unsigned int > tid_active_
unsigned int RunNumber_t
tbb::concurrent_queue< InputChunk * > freeChunks_
UInt_t nEvents
Definition: hcalCalib.cc:42
void reset(double vett[256])
Definition: TPedValues.cc:11