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>&) const;
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_;
89 
91 
93 
95 
96  std::unique_ptr<FRDEventMsgView> event_;
97 
100 
101  unsigned int currentLumiSection_;
102  unsigned int eventsThisLumi_;
103  unsigned long eventsThisRun_ = 0;
104 
106 
107  /*
108  *
109  * Multithreaded file reader
110  *
111  **/
112 
113  typedef std::pair<InputFile*,InputChunk*> ReaderInfo;
114 
117  bool chunkIsFree_=false;
118 
120  std::unique_ptr<std::thread> readSupervisorThread_;
121  std::vector<std::thread*> workerThreads_;
122 
123  tbb::concurrent_queue<unsigned int> workerPool_;
124  std::vector<ReaderInfo> workerJob_;
125 
126  tbb::concurrent_queue<InputChunk*> freeChunks_;
127  tbb::concurrent_queue<InputFile*> fileQueue_;
128 
130  std::vector<std::condition_variable*> cvReader_;
131 
132  std::atomic<bool> quit_threads_;
133  std::vector<bool> thread_quit_signal;
134  bool setExceptionState_ = false;
136  std::condition_variable startupCv_;
137 
139  std::list<std::pair<int,InputFile*>> filesToDelete_;
140  std::list<std::pair<int,std::string>> fileNamesToDelete_;
142  std::vector<int> *streamFileTrackerPtr_ = nullptr;
143  unsigned int nStreams_ = 0;
144  unsigned int checkEvery_ = 10;
145 
146  //supervisor thread wakeup
148  std::condition_variable cvWakeup_;
149 
150  //variables for the single buffered mode
152  int fileDescriptor_ = -1;
153  uint32_t bufferInputRead_ = 0;
154 
155  std::atomic<bool> threadInit_;
156 
157 };
158 
159 
160 struct InputChunk {
161  unsigned char * buf_;
162  InputChunk *next_ = nullptr;
163  uint32_t size_;
164  uint32_t usedSize_ = 0;
165  unsigned int index_;
166  unsigned int offset_;
167  unsigned int fileIndex_;
168  std::atomic<bool> readComplete_;
169 
170  InputChunk(unsigned int index, uint32_t size): size_(size),index_(index) {
171  buf_ = new unsigned char[size_];
172  reset(0,0,0);
173  }
174  void reset(unsigned int newOffset, unsigned int toRead, unsigned int fileIndex) {
175  offset_=newOffset;
176  usedSize_=toRead;
177  fileIndex_=fileIndex;
178  readComplete_=false;
179  }
180 
181  ~InputChunk() {delete[] buf_;}
182 };
183 
184 
185 struct InputFile {
188  unsigned int lumi_;
190  uint32_t fileSize_;
191  uint32_t nChunks_;
192  unsigned int nEvents_;
193  unsigned int nProcessed_;
194 
195  tbb::concurrent_vector<InputChunk*> chunks_;
196 
197  uint32_t bufferPosition_ = 0;
198  uint32_t chunkPosition_ = 0;
199  unsigned int currentChunk_ = 0;
200 
202  uint32_t fileSize =0, uint32_t nChunks=0, unsigned int nEvents=0, FedRawDataInputSource *parent = nullptr):
203  parent_(parent),
204  status_(status),
205  lumi_(lumi),
206  fileName_(name),
207  fileSize_(fileSize),
208  nChunks_(nChunks),
209  nEvents_(nEvents),
210  nProcessed_(0)
211  {
212  for (unsigned int i=0;i<nChunks;i++)
213  chunks_.push_back(nullptr);
214  }
215 
217 
218  bool waitForChunk(unsigned int chunkid) {
219  //some atomics to make sure everything is cache synchronized for the main thread
220  return chunks_[chunkid]!=nullptr && chunks_[chunkid]->readComplete_;
221  }
222  bool advance(unsigned char* & dataPosition, const size_t size);
223  void moveToPreviousChunk(const size_t size, const size_t offset);
224  void rewindChunk(const size_t size);
225 };
226 
227 
228 #endif // EventFilter_Utilities_FedRawDataInputSource_h
229 
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_
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_
edm::Timestamp fillFEDRawDataCollection(std::auto_ptr< FEDRawDataCollection > &) const
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