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  void renameToNextFree(std::string const& fileName) const;
69 
70  void readSupervisor();
71  void readWorker(unsigned int tid);
72  void threadError();
74 
75  //functions for single buffered reader
77 
78  //variables
81 
83 
84  unsigned int eventChunkSize_; // for buffered read-ahead
85  unsigned int eventChunkBlock_; // how much read(2) asks at the time
86  unsigned int readBlocks_;
87  unsigned int numBuffers_;
88  unsigned int numConcurrentReads_;
89 
90  // get LS from filename instead of event header
91  const bool getLSFromFilename_;
92  const bool verifyAdler32_;
93  const bool useL1EventID_;
95 
97 
99 
101 
102  std::unique_ptr<FRDEventMsgView> event_;
103 
106 
107  unsigned int currentLumiSection_;
108  uint32_t eventRunNumber_=0;
109  uint32_t GTPEventID_ = 0;
110  uint32_t L1EventID_ = 0;
111  unsigned char *tcds_pointer_;
112  unsigned int eventsThisLumi_;
113  unsigned long eventsThisRun_ = 0;
114 
116 
117  /*
118  *
119  * Multithreaded file reader
120  *
121  **/
122 
123  typedef std::pair<InputFile*,InputChunk*> ReaderInfo;
124 
127  bool chunkIsFree_=false;
128 
130  std::unique_ptr<std::thread> readSupervisorThread_;
131  std::vector<std::thread*> workerThreads_;
132 
133  tbb::concurrent_queue<unsigned int> workerPool_;
134  std::vector<ReaderInfo> workerJob_;
135 
136  tbb::concurrent_queue<InputChunk*> freeChunks_;
137  tbb::concurrent_queue<InputFile*> fileQueue_;
138 
140  std::vector<std::condition_variable*> cvReader_;
141 
142  std::atomic<bool> quit_threads_;
143  std::vector<bool> thread_quit_signal;
144  bool setExceptionState_ = false;
146  std::condition_variable startupCv_;
147 
149  std::list<std::pair<int,InputFile*>> filesToDelete_;
150  std::list<std::pair<int,std::string>> fileNamesToDelete_;
152  std::vector<int> *streamFileTrackerPtr_ = nullptr;
153  unsigned int nStreams_ = 0;
154  unsigned int checkEvery_ = 10;
155 
156  //supervisor thread wakeup
158  std::condition_variable cvWakeup_;
159 
160  //variables for the single buffered mode
162  int fileDescriptor_ = -1;
163  uint32_t bufferInputRead_ = 0;
164 
165  std::atomic<bool> threadInit_;
166 
167 };
168 
169 
170 struct InputChunk {
171  unsigned char * buf_;
172  InputChunk *next_ = nullptr;
173  uint32_t size_;
174  uint32_t usedSize_ = 0;
175  unsigned int index_;
176  unsigned int offset_;
177  unsigned int fileIndex_;
178  std::atomic<bool> readComplete_;
179 
180  InputChunk(unsigned int index, uint32_t size): size_(size),index_(index) {
181  buf_ = new unsigned char[size_];
182  reset(0,0,0);
183  }
184  void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex) {
185  offset_=newOffset;
186  usedSize_=toRead;
187  fileIndex_=fileIndex;
188  readComplete_=false;
189  }
190 
191  ~InputChunk() {delete[] buf_;}
192 };
193 
194 
195 struct InputFile {
198  unsigned int lumi_;
200  uint32_t fileSize_;
201  uint32_t nChunks_;
202  unsigned int nEvents_;
203  unsigned int nProcessed_;
204 
205  tbb::concurrent_vector<InputChunk*> chunks_;
206 
207  uint32_t bufferPosition_ = 0;
208  uint32_t chunkPosition_ = 0;
209  unsigned int currentChunk_ = 0;
210 
212  uint32_t fileSize =0, uint32_t nChunks=0, unsigned int nEvents=0, FedRawDataInputSource *parent = nullptr):
213  parent_(parent),
214  status_(status),
215  lumi_(lumi),
216  fileName_(name),
217  fileSize_(fileSize),
218  nChunks_(nChunks),
219  nEvents_(nEvents),
220  nProcessed_(0)
221  {
222  for (unsigned int i=0;i<nChunks;i++)
223  chunks_.push_back(nullptr);
224  }
225 
227 
228  bool waitForChunk(unsigned int chunkid) {
229  //some atomics to make sure everything is cache synchronized for the main thread
230  return chunks_[chunkid]!=nullptr && chunks_[chunkid]->readComplete_;
231  }
232  bool advance(unsigned char* & dataPosition, const size_t size);
233  void moveToPreviousChunk(const size_t size, const size_t offset);
234  void rewindChunk(const size_t size);
235 };
236 
237 
238 #endif // EventFilter_Utilities_FedRawDataInputSource_h
239 
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_
tuple path
else: Piece not in the list, fine.
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)
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.
void renameToNextFree(std::string const &fileName) const