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