CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 class InputFile;
32 class InputChunk;
33 
34 namespace evf {
35 class FastMonitoringService;
36 }
37 
38 namespace jsoncollector {
39 class DataPointDefinition;
40 }
41 
42 
44 
45 friend class InputFile;
46 friend class 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 preForkReleaseResources() override;
60  virtual void postForkReacquireResources(std::shared_ptr<edm::multicore::MessageReceiverForSource>) override;
61  virtual void rewind_() override;
62 
63  void maybeOpenNewLumiSection(const uint32_t lumiSection);
64  void createBoLSFile(const uint32_t lumiSection,bool checkIfExists);
68  void deleteFile(std::string const&);
70 
71  void readSupervisor();
72  void readWorker(unsigned int tid);
73  void threadError();
75 
76  //functions for single buffered reader
78 
79  //monitoring
80  void reportEventsThisLumiInSource(unsigned int lumi,unsigned int events);
81 
82  long initFileList();
83  evf::EvFDaqDirector::FileStatus getFile(unsigned int& ls, std::string& nextFile, uint32_t& fsize, uint64_t& lockWaitTime);
84 
85  //variables
88 
90 
91  unsigned int eventChunkSize_; // for buffered read-ahead
92  unsigned int eventChunkBlock_; // how much read(2) asks at the time
93  unsigned int readBlocks_;
94  unsigned int numBuffers_;
95  unsigned int maxBufferedFiles_;
96  unsigned int numConcurrentReads_;
97  std::atomic<unsigned int> readingFilesCount_;
98 
99  // get LS from filename instead of event header
100  const bool getLSFromFilename_;
101  const bool verifyAdler32_;
102  const bool verifyChecksum_;
103  const bool useL1EventID_;
104  std::vector<std::string> fileNames_;
105  //std::vector<std::string> fileNamesSorted_;
106 
107  const bool fileListMode_;
108  unsigned int fileListIndex_ = 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 
140  bool chunkIsFree_=false;
141 
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 
155  std::atomic<bool> quit_threads_;
156  std::vector<bool> thread_quit_signal;
157  bool setExceptionState_ = false;
159  std::condition_variable startupCv_;
160 
162  std::list<std::pair<int,InputFile*>> filesToDelete_;
163  std::list<std::pair<int,std::string>> fileNamesToDelete_;
165  std::vector<int> *streamFileTrackerPtr_ = nullptr;
166  unsigned int nStreams_ = 0;
167  unsigned int checkEvery_ = 10;
168 
169  //supervisor thread wakeup
171  std::condition_variable cvWakeup_;
172 
173  //variables for the single buffered mode
175  int fileDescriptor_ = -1;
176  uint32_t bufferInputRead_ = 0;
177 
178  std::atomic<bool> threadInit_;
179 
180  std::map<unsigned int,unsigned int> sourceEventsReport_;
182 };
183 
184 
185 struct InputChunk {
186  unsigned char * buf_;
187  InputChunk *next_ = nullptr;
188  uint32_t size_;
189  uint32_t usedSize_ = 0;
190  unsigned int index_;
191  unsigned int offset_;
192  unsigned int fileIndex_;
193  std::atomic<bool> readComplete_;
194 
195  InputChunk(unsigned int index, uint32_t size): size_(size),index_(index) {
196  buf_ = new unsigned char[size_];
197  reset(0,0,0);
198  }
199  void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex) {
200  offset_=newOffset;
201  usedSize_=toRead;
202  fileIndex_=fileIndex;
203  readComplete_=false;
204  }
205 
206  ~InputChunk() {delete[] buf_;}
207 };
208 
209 
210 struct InputFile {
213  unsigned int lumi_;
215  uint32_t fileSize_;
216  uint32_t nChunks_;
217  int nEvents_;
218  unsigned int nProcessed_;
219 
220  tbb::concurrent_vector<InputChunk*> chunks_;
221 
222  uint32_t bufferPosition_ = 0;
223  uint32_t chunkPosition_ = 0;
224  unsigned int currentChunk_ = 0;
225 
227  uint32_t fileSize =0, uint32_t nChunks=0, int nEvents=0, FedRawDataInputSource *parent = nullptr):
228  parent_(parent),
229  status_(status),
230  lumi_(lumi),
231  fileName_(name),
232  fileSize_(fileSize),
233  nChunks_(nChunks),
234  nEvents_(nEvents),
235  nProcessed_(0)
236  {
237  for (unsigned int i=0;i<nChunks;i++)
238  chunks_.push_back(nullptr);
239  }
240 
242 
243  bool waitForChunk(unsigned int chunkid) {
244  //some atomics to make sure everything is cache synchronized for the main thread
245  return chunks_[chunkid]!=nullptr && chunks_[chunkid]->readComplete_;
246  }
247  bool advance(unsigned char* & dataPosition, const size_t size);
248  void moveToPreviousChunk(const size_t size, const size_t offset);
249  void rewindChunk(const size_t size);
250 };
251 
252 
253 #endif // EventFilter_Utilities_FedRawDataInputSource_h
254 
unsigned int lumi_
int i
Definition: DBlmapReader.cc:9
std::vector< std::string > fileNames_
uint32_t chunkPosition_
std::condition_variable cvWakeup_
virtual void read(edm::EventPrincipal &eventPrincipal) override
virtual void rewind_() override
unsigned int offset_
jsoncollector::DataPointDefinition * dpd_
std::pair< bool, unsigned int > getEventReport(unsigned int lumi, bool erase)
static boost::mutex mutex
Definition: LHEProxy.cc:11
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
tbb::concurrent_queue< unsigned int > workerPool_
void maybeOpenNewLumiSection(const uint32_t lumiSection)
evf::EvFDaqDirector::FileStatus getFile(unsigned int &ls, std::string &nextFile, uint32_t &fsize, uint64_t &lockWaitTime)
tuple lumi
Definition: fjr2json.py:35
void createBoLSFile(const uint32_t lumiSection, bool checkIfExists)
void rewindChunk(const size_t size)
std::vector< int > * streamFileTrackerPtr_
def ls
Definition: eostools.py:348
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_
FedRawDataInputSource(edm::ParameterSet const &, edm::InputSourceDescription const &)
void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex)
std::vector< bool > thread_quit_signal
uint32_t bufferPosition_
bool advance(unsigned char *&dataPosition, const size_t size)
void moveToPreviousChunk(const size_t size, const size_t offset)
unsigned char * buf_
int grabNextJsonFile(boost::filesystem::path const &)
InputFile(std::string &name)
edm::ProcessHistoryID processHistoryID_
virtual void preForkReleaseResources() override
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_
virtual bool checkNextEvent() override
std::list< std::pair< int, InputFile * > > filesToDelete_
unsigned int nProcessed_
unsigned long long uint64_t
Definition: Time.h:15
unsigned int currentChunk_
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
evf::EvFDaqDirector * daqDirector_
tuple events
Definition: patZpeak.py:19
void readWorker(unsigned int tid)
std::atomic< unsigned int > readingFilesCount_
void reportEventsThisLumiInSource(unsigned int lumi, unsigned int events)
void deleteFile(std::string const &)
evf::FastMonitoringService * fms_
virtual void postForkReacquireResources(std::shared_ptr< edm::multicore::MessageReceiverForSource >) override
unsigned int RunNumber_t
InputChunk * next_
tbb::concurrent_queue< InputChunk * > freeChunks_
evf::EvFDaqDirector::FileStatus getNextEvent()
UInt_t nEvents
Definition: hcalCalib.cc:42
evf::EvFDaqDirector::FileStatus nextEvent()
void readNextChunkIntoBuffer(InputFile *file)
edm::Timestamp fillFEDRawDataCollection(FEDRawDataCollection &)
tuple size
Write out results.
tuple status
Definition: mps_update.py:57