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 {
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 
52 protected:
53  virtual bool checkNextEvent() override;
54  virtual void read(edm::EventPrincipal& eventPrincipal) override;
55 
56 private:
57  virtual void preForkReleaseResources() override;
58  virtual void postForkReacquireResources(std::shared_ptr<edm::multicore::MessageReceiverForSource>) override;
59  virtual void rewind_() override;
60 
61  void maybeOpenNewLumiSection(const uint32_t lumiSection);
62  void createBoLSFile(const uint32_t lumiSection,bool checkIfExists);
66  void deleteFile(std::string const&);
68 
69  void readSupervisor();
70  void readWorker(unsigned int tid);
71  void threadError();
73 
74  //functions for single buffered reader
76 
77  //variables
80 
82 
83  unsigned int eventChunkSize_; // for buffered read-ahead
84  unsigned int eventChunkBlock_; // how much read(2) asks at the time
85  unsigned int readBlocks_;
86  unsigned int numBuffers_;
87  unsigned int maxBufferedFiles_;
88  unsigned int numConcurrentReads_;
89  std::atomic<unsigned int> readingFilesCount_;
90 
91  // get LS from filename instead of event header
92  const bool getLSFromFilename_;
93  const bool verifyAdler32_;
94  const bool verifyChecksum_;
95  const bool useL1EventID_;
96 
98 
100 
102 
103  std::unique_ptr<FRDEventMsgView> event_;
104 
107 
108  unsigned int currentLumiSection_;
109  uint32_t eventRunNumber_=0;
110  uint32_t GTPEventID_ = 0;
111  uint32_t L1EventID_ = 0;
112  unsigned char *tcds_pointer_;
113  unsigned int eventsThisLumi_;
114  unsigned long eventsThisRun_ = 0;
115 
117 
118  /*
119  *
120  * Multithreaded file reader
121  *
122  **/
123 
124  typedef std::pair<InputFile*,InputChunk*> ReaderInfo;
125 
128  bool chunkIsFree_=false;
129 
131  std::unique_ptr<std::thread> readSupervisorThread_;
132  std::vector<std::thread*> workerThreads_;
133 
134  tbb::concurrent_queue<unsigned int> workerPool_;
135  std::vector<ReaderInfo> workerJob_;
136 
137  tbb::concurrent_queue<InputChunk*> freeChunks_;
138  tbb::concurrent_queue<InputFile*> fileQueue_;
139 
141  std::vector<std::condition_variable*> cvReader_;
142 
143  std::atomic<bool> quit_threads_;
144  std::vector<bool> thread_quit_signal;
145  bool setExceptionState_ = false;
147  std::condition_variable startupCv_;
148 
150  std::list<std::pair<int,InputFile*>> filesToDelete_;
151  std::list<std::pair<int,std::string>> fileNamesToDelete_;
153  std::vector<int> *streamFileTrackerPtr_ = nullptr;
154  unsigned int nStreams_ = 0;
155  unsigned int checkEvery_ = 10;
156 
157  //supervisor thread wakeup
159  std::condition_variable cvWakeup_;
160 
161  //variables for the single buffered mode
163  int fileDescriptor_ = -1;
164  uint32_t bufferInputRead_ = 0;
165 
166  std::atomic<bool> threadInit_;
167 
168 };
169 
170 
171 struct InputChunk {
172  unsigned char * buf_;
173  InputChunk *next_ = nullptr;
174  uint32_t size_;
175  uint32_t usedSize_ = 0;
176  unsigned int index_;
177  unsigned int offset_;
178  unsigned int fileIndex_;
179  std::atomic<bool> readComplete_;
180 
181  InputChunk(unsigned int index, uint32_t size): size_(size),index_(index) {
182  buf_ = new unsigned char[size_];
183  reset(0,0,0);
184  }
185  void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex) {
186  offset_=newOffset;
187  usedSize_=toRead;
188  fileIndex_=fileIndex;
189  readComplete_=false;
190  }
191 
192  ~InputChunk() {delete[] buf_;}
193 };
194 
195 
196 struct InputFile {
199  unsigned int lumi_;
201  uint32_t fileSize_;
202  uint32_t nChunks_;
203  unsigned int nEvents_;
204  unsigned int nProcessed_;
205 
206  tbb::concurrent_vector<InputChunk*> chunks_;
207 
208  uint32_t bufferPosition_ = 0;
209  uint32_t chunkPosition_ = 0;
210  unsigned int currentChunk_ = 0;
211 
213  uint32_t fileSize =0, uint32_t nChunks=0, unsigned int nEvents=0, FedRawDataInputSource *parent = nullptr):
214  parent_(parent),
215  status_(status),
216  lumi_(lumi),
217  fileName_(name),
218  fileSize_(fileSize),
219  nChunks_(nChunks),
220  nEvents_(nEvents),
221  nProcessed_(0)
222  {
223  for (unsigned int i=0;i<nChunks;i++)
224  chunks_.push_back(nullptr);
225  }
226 
228 
229  bool waitForChunk(unsigned int chunkid) {
230  //some atomics to make sure everything is cache synchronized for the main thread
231  return chunks_[chunkid]!=nullptr && chunks_[chunkid]->readComplete_;
232  }
233  bool advance(unsigned char* & dataPosition, const size_t size);
234  void moveToPreviousChunk(const size_t size, const size_t offset);
235  void rewindChunk(const size_t size);
236 };
237 
238 
239 #endif // EventFilter_Utilities_FedRawDataInputSource_h
240 
InputFile(evf::EvFDaqDirector::FileStatus status, unsigned int lumi=0, std::string const &name=std::string(), uint32_t fileSize=0, uint32_t nChunks=0, unsigned int nEvents=0, FedRawDataInputSource *parent=0)
unsigned int lumi_
int i
Definition: DBlmapReader.cc:9
uint32_t chunkPosition_
std::condition_variable cvWakeup_
virtual void read(edm::EventPrincipal &eventPrincipal) override
list parent
Definition: dbtoconf.py:74
virtual void rewind_() override
unsigned int offset_
jsoncollector::DataPointDefinition * dpd_
static boost::mutex mutex
Definition: LHEProxy.cc:11
tbb::concurrent_queue< unsigned int > workerPool_
void maybeOpenNewLumiSection(const uint32_t lumiSection)
tuple lumi
Definition: fjr2json.py:35
void createBoLSFile(const uint32_t lumiSection, bool checkIfExists)
void rewindChunk(const size_t size)
std::vector< int > * streamFileTrackerPtr_
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_
const edm::RunNumber_t runNumber_
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
bool waitForChunk(unsigned int chunkid)
const edm::DaqProvenanceHelper daqProvenanceHelper_
unsigned int fileIndex_
std::vector< std::thread * > workerThreads_
unsigned int uint32
Definition: MsgTools.h:13
const std::string fuOutputDir_
std::atomic< bool > readComplete_
virtual bool checkNextEvent() override
std::list< std::pair< int, InputFile * > > filesToDelete_
unsigned int nProcessed_
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_
void readWorker(unsigned int tid)
std::atomic< unsigned int > readingFilesCount_
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()
tuple status
Definition: ntuplemaker.py:245
UInt_t nEvents
Definition: hcalCalib.cc:42
evf::EvFDaqDirector::FileStatus nextEvent()
void readNextChunkIntoBuffer(InputFile *file)
edm::Timestamp fillFEDRawDataCollection(FEDRawDataCollection &)
unsigned int nEvents_
tuple size
Write out results.