CMS 3D CMS Logo

TestInterProcessProd.cc
Go to the documentation of this file.
5 
6 #include "DataFormats/TestObjects/interface/ToyProducts.h"
7 #include "DataFormats/TestObjects/interface/ThingCollection.h"
8 
9 #include <cstdio>
10 #include <iostream>
11 
15 
16 using namespace edm::shared_memory;
17 namespace testinter {
18 
19  struct StreamCache {
20  StreamCache(const std::string& iConfig, int id)
21  : id_{id},
22  channel_("testProd", id_, 60),
23  readBuffer_{channel_.sharedMemoryName(), channel_.fromWorkerBufferInfo()},
24  deserializer_{readBuffer_},
25  br_deserializer_{readBuffer_},
26  er_deserializer_{readBuffer_},
27  bl_deserializer_{readBuffer_},
28  el_deserializer_(readBuffer_) {
29  //make sure output is flushed before popen does any writing
30  fflush(stdout);
31  fflush(stderr);
32 
33  channel_.setupWorker([&]() {
34  using namespace std::string_literals;
35  std::cout << id_ << " starting external process" << std::endl;
36  pipe_ = popen(("cmsTestInterProcess "s + channel_.sharedMemoryName() + " " + channel_.uniqueID()).c_str(), "w");
37 
38  if (nullptr == pipe_) {
39  abort();
40  }
41 
42  {
43  auto nlines = std::to_string(std::count(iConfig.begin(), iConfig.end(), '\n'));
44  auto result = fwrite(nlines.data(), sizeof(char), nlines.size(), pipe_);
45  assert(result == nlines.size());
46  result = fwrite(iConfig.data(), sizeof(char), iConfig.size(), pipe_);
47  assert(result == iConfig.size());
48  fflush(pipe_);
49  }
50  });
51  }
52 
53  template <typename SERIAL>
54  auto doTransition(SERIAL& iDeserializer, edm::Transition iTrans, unsigned long long iTransitionID)
55  -> decltype(iDeserializer.deserialize()) {
56  decltype(iDeserializer.deserialize()) value;
57  if (not channel_.doTransition(
58  [&value, this]() {
59  value = deserializer_.deserialize();
60  std::cout << id_ << " from shared memory " << value.size() << std::endl;
61  },
62  iTrans,
63  iTransitionID)) {
64  std::cout << id_ << " FAILED waiting for external process" << std::endl;
65  externalFailed_ = true;
67  }
68  return value;
69  }
70  edmtest::ThingCollection produce(unsigned long long iTransitionID) {
71  return doTransition(deserializer_, edm::Transition::Event, iTransitionID);
72  }
73 
74  edmtest::ThingCollection beginRunProduce(unsigned long long iTransitionID) {
75  return doTransition(br_deserializer_, edm::Transition::BeginRun, iTransitionID);
76  }
77 
78  edmtest::ThingCollection endRunProduce(unsigned long long iTransitionID) {
79  if (not externalFailed_) {
80  return doTransition(er_deserializer_, edm::Transition::EndRun, iTransitionID);
81  }
82  return edmtest::ThingCollection();
83  }
84 
85  edmtest::ThingCollection beginLumiProduce(unsigned long long iTransitionID) {
86  return doTransition(bl_deserializer_, edm::Transition::BeginLuminosityBlock, iTransitionID);
87  }
88 
89  edmtest::ThingCollection endLumiProduce(unsigned long long iTransitionID) {
90  if (not externalFailed_) {
91  return doTransition(el_deserializer_, edm::Transition::EndLuminosityBlock, iTransitionID);
92  }
93  return edmtest::ThingCollection();
94  }
95 
97  channel_.stopWorker();
98  pclose(pipe_);
99  }
100 
101  private:
103  auto pid = getpid();
104  iBase += std::to_string(pid);
105  iBase += "_";
106  iBase += std::to_string(id_);
107 
108  return iBase;
109  }
110 
111  int id_;
112  FILE* pipe_;
115 
122  bool externalFailed_ = false;
123  };
124 
125  struct RunCache {
126  //Only stream 0 sets this at stream end Run and it is read at global end run
127  // the framework guarantees those calls can not happen simultaneously
128  CMS_THREAD_SAFE mutable edmtest::ThingCollection thingCollection_;
129  };
130  struct LumiCache {
131  //Only stream 0 sets this at stream end Lumi and it is read at global end Lumi
132  // the framework guarantees those calls can not happen simultaneously
133  CMS_THREAD_SAFE mutable edmtest::ThingCollection thingCollection_;
134  };
135 } // namespace testinter
136 
137 class TestInterProcessProd : public edm::global::EDProducer<edm::StreamCache<testinter::StreamCache>,
138  edm::RunCache<testinter::RunCache>,
139  edm::BeginRunProducer,
140  edm::EndRunProducer,
141  edm::LuminosityBlockCache<testinter::LumiCache>,
142  edm::BeginLuminosityBlockProducer,
143  edm::EndLuminosityBlockProducer> {
144 public:
146 
147  std::unique_ptr<testinter::StreamCache> beginStream(edm::StreamID) const final;
148  void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const final;
149 
150  void globalBeginRunProduce(edm::Run&, edm::EventSetup const&) const final;
151  std::shared_ptr<testinter::RunCache> globalBeginRun(edm::Run const&, edm::EventSetup const&) const final;
152  void streamBeginRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const final;
153  void streamEndRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const final;
154  void globalEndRun(edm::Run const&, edm::EventSetup const&) const final {}
155  void globalEndRunProduce(edm::Run&, edm::EventSetup const&) const final;
156 
157  void globalBeginLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const final;
158  std::shared_ptr<testinter::LumiCache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
159  edm::EventSetup const&) const final;
160  void streamBeginLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const final;
161  void streamEndLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const final;
163  void globalEndLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const final;
164 
165 private:
171 
173 
174  //This is set at beginStream and used for globalBeginRun
175  //The framework guarantees that non of those can happen concurrently
176  CMS_THREAD_SAFE mutable testinter::StreamCache* stream0Cache_ = nullptr;
177  //A stream which has finished processing the last lumi is used for the
178  // call to globalBeginLuminosityBlockProduce
179  mutable std::atomic<testinter::StreamCache*> availableForBeginLumi_;
180  //Streams all see the lumis in the same order, we want to be sure to pick a stream cache
181  // to use at globalBeginLumi which just finished the most recent lumi and not a previous one
182  mutable std::atomic<unsigned int> lastLumiIndex_ = 0;
183 };
184 
186  : token_{produces<edmtest::ThingCollection>()},
187  brToken_{produces<edmtest::ThingCollection, edm::Transition::BeginRun>("beginRun")},
188  erToken_{produces<edmtest::ThingCollection, edm::Transition::EndRun>("endRun")},
189  blToken_{produces<edmtest::ThingCollection, edm::Transition::BeginLuminosityBlock>("beginLumi")},
190  elToken_{produces<edmtest::ThingCollection, edm::Transition::EndLuminosityBlock>("endLumi")},
191  config_{iPSet.getUntrackedParameter<std::string>("@python_config")} {}
192 
193 std::unique_ptr<testinter::StreamCache> TestInterProcessProd::beginStream(edm::StreamID iID) const {
194  auto const label = moduleDescription().moduleLabel();
195 
196  using namespace std::string_literals;
197 
198  std::string config = R"_(from FWCore.TestProcessor.TestProcess import *
199 process = TestProcess()
200 )_";
201  config += "process."s + label + "=" + config_ + "\n";
202  config += "process.moduleToTest(process."s + label + ")\n";
203  config += R"_(
204 process.add_(cms.Service("InitRootHandlers", UnloadRootSigHandler=cms.untracked.bool(True)))
205  )_";
206 
207  auto cache = std::make_unique<testinter::StreamCache>(config, iID.value());
208  if (iID.value() == 0) {
209  stream0Cache_ = cache.get();
210 
212  }
213 
214  return cache;
215 }
216 
218  auto value = streamCache(iID)->produce(iEvent.id().event());
219  iEvent.emplace(token_, value);
220 }
221 
223  auto v = stream0Cache_->beginRunProduce(iRun.run());
224  iRun.emplace(brToken_, v);
225 }
226 std::shared_ptr<testinter::RunCache> TestInterProcessProd::globalBeginRun(edm::Run const&,
227  edm::EventSetup const&) const {
228  return std::make_shared<testinter::RunCache>();
229 }
230 
232  if (iID.value() != 0) {
233  (void)streamCache(iID)->beginRunProduce(iRun.run());
234  }
235 }
237  if (iID.value() == 0) {
238  runCache(iRun.index())->thingCollection_ = streamCache(iID)->endRunProduce(iRun.run());
239  } else {
240  (void)streamCache(iID)->endRunProduce(iRun.run());
241  }
242 }
244  iRun.emplace(erToken_, std::move(runCache(iRun.index())->thingCollection_));
245 }
246 
248  edm::EventSetup const&) const {
249  while (not availableForBeginLumi_.load()) {
250  }
251 
252  auto v = availableForBeginLumi_.load()->beginLumiProduce(iLuminosityBlock.run());
253  iLuminosityBlock.emplace(blToken_, v);
254 
255  lastLumiIndex_.store(iLuminosityBlock.index());
256 }
257 
258 std::shared_ptr<testinter::LumiCache> TestInterProcessProd::globalBeginLuminosityBlock(edm::LuminosityBlock const&,
259  edm::EventSetup const&) const {
260  return std::make_shared<testinter::LumiCache>();
261 }
262 
264  edm::LuminosityBlock const& iLuminosityBlock,
265  edm::EventSetup const&) const {
266  auto cache = streamCache(iID);
267  if (cache != availableForBeginLumi_.load()) {
268  (void)cache->beginLumiProduce(iLuminosityBlock.run());
269  } else {
270  availableForBeginLumi_ = nullptr;
271  }
272 }
273 
275  edm::LuminosityBlock const& iLuminosityBlock,
276  edm::EventSetup const&) const {
277  if (iID.value() == 0) {
278  luminosityBlockCache(iLuminosityBlock.index())->thingCollection_ =
279  streamCache(iID)->endLumiProduce(iLuminosityBlock.run());
280  } else {
281  (void)streamCache(iID)->endLumiProduce(iLuminosityBlock.run());
282  }
283 
284  if (lastLumiIndex_ == iLuminosityBlock.index()) {
285  testinter::StreamCache* expected = nullptr;
286 
287  availableForBeginLumi_.compare_exchange_strong(expected, streamCache(iID));
288  }
289 }
290 
292  edm::EventSetup const&) const {
293  iLuminosityBlock.emplace(elToken_, std::move(luminosityBlockCache(iLuminosityBlock.index())->thingCollection_));
294 }
295 
void globalBeginRunProduce(edm::Run &, edm::EventSetup const &) const final
edmtest::ThingCollection beginRunProduce(unsigned long long iTransitionID)
testinter::StreamCache * stream0Cache_
void streamEndLuminosityBlock(edm::StreamID, edm::LuminosityBlock const &, edm::EventSetup const &) const final
void globalEndRun(edm::Run const &, edm::EventSetup const &) const final
dictionary config
Read in AllInOne config in JSON format.
Definition: DMR_cfg.py:21
void emplace(EDPutTokenT< PROD > token, Args &&... args)
puts a new product
Definition: Run.h:245
edm::EDPutTokenT< edmtest::ThingCollection > const blToken_
void globalEndLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const final
Definition: config.py:1
assert(be >=bs)
static std::string to_string(const XMLCh *ch)
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
edmtest::ThingCollection thingCollection_
auto doTransition(SERIAL &iDeserializer, edm::Transition iTrans, unsigned long long iTransitionID) -> decltype(iDeserializer.deserialize())
char const * label
int iEvent
Definition: GenABIO.cc:224
RunNumber_t run() const
Definition: RunBase.h:40
edmtest::ThingCollection beginLumiProduce(unsigned long long iTransitionID)
edmtest::ThingCollection produce(unsigned long long iTransitionID)
void streamBeginRun(edm::StreamID, edm::Run const &, edm::EventSetup const &) const final
edm::EDPutTokenT< edmtest::ThingCollection > const elToken_
ModuleDescription const & moduleDescription() const
Transition
Definition: Transition.h:12
edm::EDPutTokenT< edmtest::ThingCollection > const token_
#define CMS_THREAD_SAFE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: value.py:1
edmtest::ThingCollection thingCollection_
std::atomic< unsigned int > lastLumiIndex_
std::unique_ptr< testinter::StreamCache > beginStream(edm::StreamID) const final
std::atomic< testinter::StreamCache * > availableForBeginLumi_
edmtest::ThingCollection endRunProduce(unsigned long long iTransitionID)
void emplace(EDPutTokenT< PROD > token, Args &&... args)
puts a new product
std::string unique_name(std::string iBase)
static const char pipe_[]
edm::EDPutTokenT< edmtest::ThingCollection > const erToken_
edm::EDPutTokenT< edmtest::ThingCollection > const brToken_
void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const final
std::shared_ptr< testinter::RunCache > globalBeginRun(edm::Run const &, edm::EventSetup const &) const final
void globalBeginLuminosityBlockProduce(edm::LuminosityBlock &, edm::EventSetup const &) const final
edmtest::ThingCollection endLumiProduce(unsigned long long iTransitionID)
TestInterProcessProd(edm::ParameterSet const &)
LuminosityBlockIndex index() const
std::shared_ptr< testinter::LumiCache > globalBeginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const final
RunIndex index() const
Definition: Run.cc:28
void streamEndRun(edm::StreamID, edm::Run const &, edm::EventSetup const &) const final
void globalEndRunProduce(edm::Run &, edm::EventSetup const &) const final
StreamCache(const std::string &iConfig, int id)
def cache(function)
Definition: utilities.py:3
unsigned int value() const
Definition: StreamID.h:43
void globalEndLuminosityBlockProduce(edm::LuminosityBlock &, edm::EventSetup const &) const final
std::string const & moduleLabel() const
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
void streamBeginLuminosityBlock(edm::StreamID, edm::LuminosityBlock const &, edm::EventSetup const &) const final