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 
24 #include "IOVTestInfo.h"
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:
45  TestESSourceTestProxy(TestESSource* testESSource);
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  KeyedProxiesVector registerProxies(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::ESSourceDataProxyNonConcurrentBase(&testESSource->queue_, &testESSource->mutex_),
88  testESSource_(testESSource) {}
89 
92  if (testESSource_->count_.load() > 1) {
93  throw cms::Exception("TestFailure") << "TestESSourceTestProxy::getImpl,"
94  << " functions in mutex should not run concurrently";
95  }
96  testESSource_->busyWait("getImpl");
97 
99  edm::LogAbsolute("TestESSourceTestProxy")
100  << "TestESSoureTestProxy::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("TestESSourceTestProxy::initializeForNewIOV") << "TestESSourceTestProxy::initializeForNewIOV";
117  }
118 
120  : count_(0),
121  count1_(0),
122  count2_(0),
123  iterations_(pset.getParameter<unsigned int>("iterations")),
124  pi_(std::acos(-1)),
125  expectedNumberOfConcurrentIOVs_(pset.getParameter<unsigned int>("expectedNumberOfConcurrentIOVs")),
126  checkIOVInitialization_(pset.getParameter<bool>("checkIOVInitialization")) {
127  std::vector<unsigned int> temp(pset.getParameter<std::vector<unsigned int>>("firstValidLumis"));
128  for (auto val : temp) {
130  }
131 
132  findingRecord<ESTestRecordI>();
133  usingRecord<ESTestRecordI>();
134  }
135 
137 
140  std::vector<unsigned int> emptyVector;
141  desc.add<unsigned int>("iterations", 10 * 1000 * 1000);
142  desc.add<bool>("checkIOVInitialization", false);
143  desc.add<unsigned int>("expectedNumberOfConcurrentIOVs", 0);
144  desc.add<std::vector<unsigned int>>("firstValidLumis", emptyVector);
145  descriptions.addDefault(desc);
146  }
147 
149  edm::IOVSyncValue const& syncValue,
150  edm::ValidityInterval& iov) {
151  std::lock_guard<std::mutex> guard(mutex_);
153  // Note that this check should pass with the specific configuration where I enable
154  // the check, but in general it does not have to be true. The counts are offset
155  // by 1 because the beginRun IOV is invalid (no IOV initialization).
156  if (count1_ > 0 && count2_ + 1 != count1_) {
157  throw cms::Exception("TestFailure") << "TestESSource::setIntervalFor,"
158  << " unexpected number of IOV initializations";
159  }
160  }
161  ++count_;
162  ++count1_;
163  if (count_.load() > 1) {
164  throw cms::Exception("TestFailure") << "TestESSource::setIntervalFor,"
165  << " functions in mutex should not run concurrently";
166  }
167  busyWait("setIntervalFor");
169 
170  if (setOfIOV_.empty()) {
171  --count_;
172  return;
173  }
174 
175  std::pair<std::set<edm::IOVSyncValue>::iterator, std::set<edm::IOVSyncValue>::iterator> itFound =
176  setOfIOV_.equal_range(syncValue);
177 
178  if (itFound.first == itFound.second) {
179  if (itFound.first == setOfIOV_.begin()) {
180  //request is before first valid interval, so fail
181  --count_;
182  return;
183  }
184  //go back one step
185  --itFound.first;
186  }
187 
189  if (itFound.second != setOfIOV_.end()) {
190  endOfInterval = edm::IOVSyncValue(
191  edm::EventID(1, itFound.second->eventID().luminosityBlock() - 1, edm::EventID::maxEventNumber()));
192  }
193  iov = edm::ValidityInterval(*(itFound.first), endOfInterval);
194  --count_;
195  }
196 
198  unsigned int iovIndex) {
200  throw cms::Exception("TestFailure") << "TestESSource::registerProxies,"
201  << " unexpected number of concurrent IOVs";
202  }
203  KeyedProxiesVector keyedProxiesVector;
204 
205  edm::eventsetup::DataKey dataKey(edm::eventsetup::DataKey::makeTypeTag<IOVTestInfo>(), edm::eventsetup::IdTags(""));
206  keyedProxiesVector.emplace_back(dataKey, std::make_shared<TestESSourceTestProxy>(this));
207 
208  return keyedProxiesVector;
209  }
210 
211  void TestESSource::initConcurrentIOVs(EventSetupRecordKey const& key, unsigned int nConcurrentIOVs) {
212  edm::LogAbsolute("TestESSource::initConcurrentIOVs")
213  << "Start TestESSource::initConcurrentIOVs " << nConcurrentIOVs << " " << key.name();
214  if (EventSetupRecordKey::makeKey<ESTestRecordI>() != key) {
215  throw cms::Exception("TestFailure") << "TestESSource::initConcurrentIOVs,"
216  << " unexpected EventSetupRecordKey";
217  }
218  if (expectedNumberOfConcurrentIOVs_ != 0 && nConcurrentIOVs != expectedNumberOfConcurrentIOVs_) {
219  throw cms::Exception("TestFailure") << "TestESSource::initConcurrentIOVs,"
220  << " unexpected number of concurrent IOVs";
221  }
222  nConcurrentIOVs_ = nConcurrentIOVs;
223  }
224 
225  void TestESSource::busyWait(char const* msg) const {
226  edm::LogAbsolute("TestESSource::busyWait") << "Start TestESSource::busyWait " << msg;
227  double sum = 0.;
228  const double stepSize = pi_ / iterations_;
229  for (unsigned int i = 0; i < iterations_; ++i) {
230  sum += stepSize * cos(i * stepSize);
231  }
232  edm::LogAbsolute("TestESSource::busyWait") << "Stop TestESSource::busyWait " << msg << " " << sum;
233  }
234 } // namespace edmtest
235 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
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
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 const * getAfterPrefetchImpl() const override
TestESSourceTestProxy(TestESSource *testESSource)
Definition: TestESSource.cc:86
void initializeForNewIOV() override
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
void prefetch(edm::eventsetup::DataKey const &, edm::EventSetupRecordDetails) override
Definition: TestESSource.cc:90
unsigned int iovStartLumi_
Definition: IOVTestInfo.h:22
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
void busyWait(char const *msg) const
std::vector< std::pair< DataKey, std::shared_ptr< DataProxy > >> KeyedProxiesVector
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
tuple msg
Definition: mps_check.py:286
static EventNumber_t maxEventNumber()
Definition: EventID.h:96
const unsigned int iterations_
Definition: TestESSource.cc:79
HLT enums.
static const ValidityInterval & invalidInterval()
Log< level::System, true > LogAbsolute
KeyedProxiesVector registerProxies(EventSetupRecordKey const &, unsigned int iovIndex) override
std::atomic< unsigned int > count2_
Definition: TestESSource.cc:68