CMS 3D CMS Logo

TestESSource.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/Integration
4 // Class : TestESSource
5 //
6 // Implementation:
7 // ESSource used for tests of Framework support for
8 // ESSources and ESProducers. This is primarily focused
9 // on the infrastructure used by CondDBESSource.
10 //
11 // Original Author: W. David Dagenhart
12 // Created: 15 August 2019
13 
30 
31 #include <atomic>
32 #include <cmath>
33 #include <limits>
34 #include <set>
35 #include <utility>
36 #include <vector>
37 #include <mutex>
38 
39 namespace edmtest {
40 
41  class TestESSource;
42 
44  public:
46 
47  private:
49  void initializeForNewIOV() override;
50  void const* getAfterPrefetchImpl() const override;
51 
54  };
55 
57  public:
59  explicit TestESSource(edm::ParameterSet const&);
60  ~TestESSource() override;
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
63 
64  void busyWait(char const* msg) const;
65 
66  std::atomic<unsigned int> count_;
67  std::atomic<unsigned int> count1_;
68  std::atomic<unsigned int> count2_;
71 
72  private:
73  bool isConcurrentFinder() const override { return true; }
75  KeyedResolversVector registerResolvers(EventSetupRecordKey const&, unsigned int iovIndex) override;
76  void initConcurrentIOVs(EventSetupRecordKey const&, unsigned int nConcurrentIOVs) override;
77 
78  std::set<edm::IOVSyncValue> setOfIOV_;
79  const unsigned int iterations_;
80  const double pi_;
82  unsigned int nConcurrentIOVs_ = 0;
84  };
85 
87  : edm::eventsetup::ESSourceProductResolverNonConcurrentBase(&testESSource->queue_, &testESSource->mutex_),
88  testESSource_(testESSource) {}
89 
92  if (testESSource_->count_.load() > 1) {
93  throw cms::Exception("TestFailure") << "TestESSourceTestResolver::getImpl,"
94  << " functions in mutex should not run concurrently";
95  }
96  testESSource_->busyWait("getImpl");
97 
99  edm::LogAbsolute("TestESSourceTestResolver")
100  << "TestESSoureTestResolver::getImpl startIOV = " << iov.first().luminosityBlockNumber()
101  << " endIOV = " << iov.last().luminosityBlockNumber() << " IOV index = " << iRecord.iovIndex()
102  << " cache identifier = " << iRecord.cacheIdentifier();
103 
106  iovTestInfo_.iovIndex_ = iRecord.iovIndex();
108 
110  }
111 
113 
115  edm::LogAbsolute("TestESSourceTestResolver::initializeForNewIOV")
116  << "TestESSourceTestResolver::initializeForNewIOV";
118  }
119 
121  : count_(0),
122  count1_(0),
123  count2_(0),
124  iterations_(pset.getParameter<unsigned int>("iterations")),
125  pi_(std::acos(-1)),
126  expectedNumberOfConcurrentIOVs_(pset.getParameter<unsigned int>("expectedNumberOfConcurrentIOVs")),
127  checkIOVInitialization_(pset.getParameter<bool>("checkIOVInitialization")) {
128  std::vector<unsigned int> temp(pset.getParameter<std::vector<unsigned int>>("firstValidLumis"));
129  for (auto val : temp) {
131  }
132 
133  findingRecord<ESTestRecordI>();
134  usingRecord<ESTestRecordI>();
135  }
136 
138 
141  std::vector<unsigned int> emptyVector;
142  desc.add<unsigned int>("iterations", 10 * 1000 * 1000);
143  desc.add<bool>("checkIOVInitialization", false);
144  desc.add<unsigned int>("expectedNumberOfConcurrentIOVs", 0);
145  desc.add<std::vector<unsigned int>>("firstValidLumis", emptyVector);
146  descriptions.addDefault(desc);
147  }
148 
150  edm::IOVSyncValue const& syncValue,
151  edm::ValidityInterval& iov) {
152  std::lock_guard<std::mutex> guard(mutex_);
154  // Note that this check should pass with the specific configuration where I enable
155  // the check, but in general it does not have to be true. The counts are offset
156  // by 1 because the beginRun IOV is invalid (no IOV initialization).
157  if (count1_ > 0 && count2_ + 1 != count1_) {
158  throw cms::Exception("TestFailure") << "TestESSource::setIntervalFor,"
159  << " unexpected number of IOV initializations";
160  }
161  }
162  ++count_;
163  ++count1_;
164  if (count_.load() > 1) {
165  throw cms::Exception("TestFailure") << "TestESSource::setIntervalFor,"
166  << " functions in mutex should not run concurrently";
167  }
168  busyWait("setIntervalFor");
170 
171  if (setOfIOV_.empty()) {
172  --count_;
173  return;
174  }
175 
176  std::pair<std::set<edm::IOVSyncValue>::iterator, std::set<edm::IOVSyncValue>::iterator> itFound =
177  setOfIOV_.equal_range(syncValue);
178 
179  if (itFound.first == itFound.second) {
180  if (itFound.first == setOfIOV_.begin()) {
181  //request is before first valid interval, so fail
182  --count_;
183  return;
184  }
185  //go back one step
186  --itFound.first;
187  }
188 
190  if (itFound.second != setOfIOV_.end()) {
191  endOfInterval = edm::IOVSyncValue(
192  edm::EventID(1, itFound.second->eventID().luminosityBlock() - 1, edm::EventID::maxEventNumber()));
193  }
194  iov = edm::ValidityInterval(*(itFound.first), endOfInterval);
195  --count_;
196  }
197 
199  EventSetupRecordKey const&, unsigned int iovIndex) {
201  throw cms::Exception("TestFailure") << "TestESSource::registerResolvers,"
202  << " unexpected number of concurrent IOVs";
203  }
204  KeyedResolversVector keyedResolversVector;
205 
206  edm::eventsetup::DataKey dataKey(edm::eventsetup::DataKey::makeTypeTag<IOVTestInfo>(), edm::eventsetup::IdTags(""));
207  keyedResolversVector.emplace_back(dataKey, std::make_shared<TestESSourceTestResolver>(this));
208 
209  return keyedResolversVector;
210  }
211 
212  void TestESSource::initConcurrentIOVs(EventSetupRecordKey const& key, unsigned int nConcurrentIOVs) {
213  edm::LogAbsolute("TestESSource::initConcurrentIOVs")
214  << "Start TestESSource::initConcurrentIOVs " << nConcurrentIOVs << " " << key.name();
215  if (EventSetupRecordKey::makeKey<ESTestRecordI>() != key) {
216  throw cms::Exception("TestFailure") << "TestESSource::initConcurrentIOVs,"
217  << " unexpected EventSetupRecordKey";
218  }
219  if (expectedNumberOfConcurrentIOVs_ != 0 && nConcurrentIOVs != expectedNumberOfConcurrentIOVs_) {
220  throw cms::Exception("TestFailure") << "TestESSource::initConcurrentIOVs,"
221  << " unexpected number of concurrent IOVs";
222  }
223  nConcurrentIOVs_ = nConcurrentIOVs;
224  }
225 
226  void TestESSource::busyWait(char const* msg) const {
227  edm::LogAbsolute("TestESSource::busyWait") << "Start TestESSource::busyWait " << msg;
228  double sum = 0.;
229  const double stepSize = pi_ / iterations_;
230  for (unsigned int i = 0; i < iterations_; ++i) {
231  sum += stepSize * cos(i * stepSize);
232  }
233  edm::LogAbsolute("TestESSource::busyWait") << "Stop TestESSource::busyWait " << msg << " " << sum;
234  }
235 } // namespace edmtest
236 using namespace edmtest;
const IOVSyncValue & last() const
LuminosityBlockNumber_t luminosityBlockNumber() const
Definition: IOVSyncValue.h:41
const IOVSyncValue & first() const
unsigned int iovEndLumi_
Definition: IOVTestInfo.h:24
std::set< edm::IOVSyncValue > setOfIOV_
Definition: TestESSource.cc:78
void const * getAfterPrefetchImpl() const override
unsigned int expectedNumberOfConcurrentIOVs_
Definition: TestESSource.cc:81
unsigned int iovIndex_
Definition: IOVTestInfo.h:25
static std::mutex mutex
Definition: Proxy.cc:8
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:82
KeyedResolversVector registerResolvers(EventSetupRecordKey const &, unsigned int iovIndex) override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
unsigned long long cacheIdentifier() const
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:17
unsigned int nConcurrentIOVs_
Definition: TestESSource.cc:82
void prefetch(edm::eventsetup::DataKey const &, edm::EventSetupRecordDetails) override
Definition: TestESSource.cc:90
edm::SerialTaskQueue queue_
Definition: TestESSource.cc:69
std::atomic< unsigned int > count1_
Definition: TestESSource.cc:67
TestESSource(edm::ParameterSet const &)
ValidityInterval validityInterval() const
std::atomic< unsigned int > count_
Definition: TestESSource.cc:66
void initConcurrentIOVs(EventSetupRecordKey const &, unsigned int nConcurrentIOVs) override
void addDefault(ParameterSetDescription const &psetDescription)
bool isConcurrentFinder() const override
Definition: TestESSource.cc:73
unsigned int iovStartLumi_
Definition: IOVTestInfo.h:22
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
key
prepare the HTCondor submission files and eventually submit them
void busyWait(char const *msg) const
void setIntervalFor(EventSetupRecordKey const &, edm::IOVSyncValue const &, edm::ValidityInterval &) override
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
unsigned long long cacheIdentifier_
Definition: IOVTestInfo.h:26
TestESSourceTestResolver(TestESSource *testESSource)
Definition: TestESSource.cc:86
tuple msg
Definition: mps_check.py:286
static EventNumber_t maxEventNumber()
Definition: EventID.h:96
std::vector< std::pair< DataKey, std::shared_ptr< ESProductResolver > >> KeyedResolversVector
const unsigned int iterations_
Definition: TestESSource.cc:79
HLT enums.
static const ValidityInterval & invalidInterval()
Log< level::System, true > LogAbsolute
std::atomic< unsigned int > count2_
Definition: TestESSource.cc:68