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 class InputSourceDescription;
25 class ParameterSet;
26 
27 class InputFile;
28 class InputChunk;
29 
30 namespace evf {
32 }
33 
34 namespace jsoncollector {
35 class DataPointDefinition;
36 }
37 
38 
40 
41 friend class InputFile;
42 friend class InputChunk;
43 
44 public:
46  virtual ~FedRawDataInputSource();
47 
48 protected:
49  virtual bool checkNextEvent() override;
50  virtual void read(edm::EventPrincipal& eventPrincipal) override;
51 
52 private:
53  virtual void preForkReleaseResources() override;
54  virtual void postForkReacquireResources(boost::shared_ptr<edm::multicore::MessageReceiverForSource>) override;
55  virtual void rewind_() override;
56 
57  void maybeOpenNewLumiSection(const uint32_t lumiSection);
60  edm::Timestamp fillFEDRawDataCollection(std::auto_ptr<FEDRawDataCollection>&);
61  void deleteFile(std::string const&);
63  void renameToNextFree(std::string const& fileName) const;
64 
65  void readSupervisor();
66  void readWorker(unsigned int tid);
67  void threadError();
69 
70  //functions for single buffered reader
72 
73  //variables
76 
78 
79  unsigned int eventChunkSize_; // for buffered read-ahead
80  unsigned int eventChunkBlock_; // how much read(2) asks at the time
81  unsigned int readBlocks_;
82  unsigned int numBuffers_;
83  unsigned int numConcurrentReads_;
84 
85  // get LS from filename instead of event header
86  const bool getLSFromFilename_;
87  const bool verifyAdler32_;
88  const bool useL1EventID_;
90 
92 
94 
96 
97  std::unique_ptr<FRDEventMsgView> event_;
98 
101 
102  unsigned int currentLumiSection_;
103  uint32_t eventRunNumber_=0;
104  uint32_t GTPEventID_ = 0;
105  uint32_t L1EventID_ = 0;
106  unsigned int eventsThisLumi_;
107  unsigned long eventsThisRun_ = 0;
108 
110 
111  /*
112  *
113  * Multithreaded file reader
114  *
115  **/
116 
117  typedef std::pair<InputFile*,InputChunk*> ReaderInfo;
118 
121  bool chunkIsFree_=false;
122 
124  std::unique_ptr<std::thread> readSupervisorThread_;
125  std::vector<std::thread*> workerThreads_;
126 
127  tbb::concurrent_queue<unsigned int> workerPool_;
128  std::vector<ReaderInfo> workerJob_;
129 
130  tbb::concurrent_queue<InputChunk*> freeChunks_;
131  tbb::concurrent_queue<InputFile*> fileQueue_;
132 
134  std::vector<std::condition_variable*> cvReader_;
135 
136  std::atomic<bool> quit_threads_;
137  std::vector<bool> thread_quit_signal;
138  bool setExceptionState_ = false;
140  std::condition_variable startupCv_;
141 
143  std::list<std::pair<int,InputFile*>> filesToDelete_;
144  std::list<std::pair<int,std::string>> fileNamesToDelete_;
146  std::vector<int> *streamFileTrackerPtr_ = nullptr;
147  unsigned int nStreams_ = 0;
148  unsigned int checkEvery_ = 10;
149 
150  //supervisor thread wakeup
152  std::condition_variable cvWakeup_;
153 
154  //variables for the single buffered mode
156  int fileDescriptor_ = -1;
157  uint32_t bufferInputRead_ = 0;
158 
159  std::atomic<bool> threadInit_;
160 
161 };
162 
163 
164 struct InputChunk {
165  unsigned char * buf_;
166  InputChunk *next_ = nullptr;
167  uint32_t size_;
168  uint32_t usedSize_ = 0;
169  unsigned int index_;
170  unsigned int offset_;
171  unsigned int fileIndex_;
172  std::atomic<bool> readComplete_;
173 
174  InputChunk(unsigned int index, uint32_t size): size_(size),index_(index) {
175  buf_ = new unsigned char[size_];
176  reset(0,0,0);
177  }
178  void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex) {
179  offset_=newOffset;
180  usedSize_=toRead;
181  fileIndex_=fileIndex;
182  readComplete_=false;
183  }
184 
185  ~InputChunk() {delete[] buf_;}
186 };
187 
188 
189 struct InputFile {
192  unsigned int lumi_;
194  uint32_t fileSize_;
195  uint32_t nChunks_;
196  unsigned int nEvents_;
197  unsigned int nProcessed_;
198 
199  tbb::concurrent_vector<InputChunk*> chunks_;
200 
201  uint32_t bufferPosition_ = 0;
202  uint32_t chunkPosition_ = 0;
203  unsigned int currentChunk_ = 0;
204 
206  uint32_t fileSize =0, uint32_t nChunks=0, unsigned int nEvents=0, FedRawDataInputSource *parent = nullptr):
207  parent_(parent),
208  status_(status),
209  lumi_(lumi),
210  fileName_(name),
211  fileSize_(fileSize),
212  nChunks_(nChunks),
213  nEvents_(nEvents),
214  nProcessed_(0)
215  {
216  for (unsigned int i=0;i<nChunks;i++)
217  chunks_.push_back(nullptr);
218  }
219 
221 
222  bool waitForChunk(unsigned int chunkid) {
223  //some atomics to make sure everything is cache synchronized for the main thread
224  return chunks_[chunkid]!=nullptr && chunks_[chunkid]->readComplete_;
225  }
226  bool advance(unsigned char* & dataPosition, const size_t size);
227  void moveToPreviousChunk(const size_t size, const size_t offset);
228  void rewindChunk(const size_t size);
229 };
230 
231 
232 #endif // EventFilter_Utilities_FedRawDataInputSource_h
233 
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
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=nullptr)
tbb::concurrent_queue< unsigned int > workerPool_
void maybeOpenNewLumiSection(const uint32_t lumiSection)
tuple lumi
Definition: fjr2json.py:35
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_
unsigned int offset(bool)
std::vector< std::thread * > workerThreads_
unsigned int uint32
Definition: MsgTools.h:13
const std::string fuOutputDir_
edm::Timestamp fillFEDRawDataCollection(std::auto_ptr< FEDRawDataCollection > &)
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_
virtual void postForkReacquireResources(boost::shared_ptr< edm::multicore::MessageReceiverForSource >) override
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_
unsigned int RunNumber_t
Definition: EventRange.h:32
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)
unsigned int nEvents_
tuple size
Write out results.
void renameToNextFree(std::string const &fileName) const