Harness

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
#include "boost/program_options.hpp"

#include <atomic>
#include <csignal>
#include <iostream>
#include <string>
#include <thread>
#include <memory>
#include <filesystem>
#include <ctime>

#include "FWCore/TestProcessor/interface/TestProcessor.h"

#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h"
#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/ExternalGeneratorEventInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/ExternalGeneratorLumiInfo.h"

#include "FWCore/Services/interface/ExternalRandomNumberGeneratorService.h"

#include "FWCore/SharedMemory/interface/WriteBuffer.h"
#include "FWCore/SharedMemory/interface/ReadBuffer.h"
#include "FWCore/SharedMemory/interface/WorkerChannel.h"
#include "FWCore/SharedMemory/interface/ROOTSerializer.h"
#include "FWCore/SharedMemory/interface/ROOTDeserializer.h"
#include "FWCore/SharedMemory/interface/WorkerMonitorThread.h"

#include "FWCore/Utilities/interface/thread_safety_macros.h"

static char const* const kMemoryNameOpt = "memory-name";
static char const* const kMemoryNameCommandOpt = "memory-name,m";
static char const* const kUniqueIDOpt = "unique-id";
static char const* const kUniqueIDCommandOpt = "unique-id,i";
static char const* const kHelpOpt = "help";
static char const* const kHelpCommandOpt = "help,h";
static char const* const kVerboseOpt = "verbose";
static char const* const kVerboseCommandOpt = "verbose,v";

//This application only uses 1 thread
CMS_THREAD_SAFE static std::string s_uniqueID;

//NOTE: Can use TestProcessor as the harness for the worker

namespace {
  //Based on MessageLogger time handling
  constexpr char timeFormat[] = "dd-Mon-yyyy hh:mm:ss TZN     ";
  constexpr size_t kTimeSize = sizeof(timeFormat);
  std::array<char, kTimeSize> formattedTime() {
    auto t = time(nullptr);
    std::array<char, kTimeSize> ts;

    struct tm timebuf;
    std::strftime(ts.data(), ts.size(), "%d-%b-%Y %H:%M:%S %Z", localtime_r(&t, &timebuf));
    return ts;
  }
}  // namespace

using namespace edm::shared_memory;
class Harness {
public:
  Harness(std::string const& iConfig, edm::ServiceToken iToken)
      : tester_(edm::test::TestProcessor::Config{iConfig}, iToken) {}

  ExternalGeneratorLumiInfo getBeginLumiValue(unsigned int iLumi) {
    auto lumi = tester_.testBeginLuminosityBlock(iLumi);
    ExternalGeneratorLumiInfo returnValue;
    returnValue.header_ = *lumi.get<GenLumiInfoHeader>();
    return returnValue;
  }

  ExternalGeneratorEventInfo getEventValue() {
    ExternalGeneratorEventInfo returnValue;
    auto event = tester_.test();
    returnValue.hepmc_ = *event.get<edm::HepMCProduct>("unsmeared");
    returnValue.eventInfo_ = *event.get<GenEventInfoProduct>();
    returnValue.keepEvent_ = event.modulePassed();
    return returnValue;
  }

  GenLumiInfoProduct getEndLumiValue() {
    auto lumi = tester_.testEndLuminosityBlock();
    return *lumi.get<GenLumiInfoProduct>();
  }

  GenRunInfoProduct getEndRunValue() {
    auto run = tester_.testEndRun();
    return *run.get<GenRunInfoProduct>();
  }

private:
  edm::test::TestProcessor tester_;
};

template <typename T>
using Serializer = ROOTSerializer<T, WriteBuffer>;

namespace {
  //needed for atexit handling
  CMS_THREAD_SAFE boost::interprocess::scoped_lock<boost::interprocess::named_mutex>* s_sharedLock = nullptr;

  void atexit_handler() {
    if (s_sharedLock) {
      std::cerr << s_uniqueID << " process: early exit called: unlock " << formattedTime().data() << "\n";
      s_sharedLock->unlock();
    }
  }
}  // namespace

int main(int argc, char* argv[]) {
  std::string descString(argv[0]);
  descString += " [--";
  descString += kMemoryNameOpt;
  descString += "] memory_name";
  boost::program_options::options_description desc(descString);

  desc.add_options()(kHelpCommandOpt, "produce help message")(
      kMemoryNameCommandOpt, boost::program_options::value<std::string>(), "memory name")(
      kUniqueIDCommandOpt, boost::program_options::value<std::string>(), "unique id")(kVerboseCommandOpt,
                                                                                      "verbose output");

  boost::program_options::positional_options_description p;
  p.add(kMemoryNameOpt, 1);
  p.add(kUniqueIDOpt, 2);

  boost::program_options::options_description all_options("All Options");
  all_options.add(desc);

  boost::program_options::variables_map vm;
  try {
    store(boost::program_options::command_line_parser(argc, argv).options(all_options).positional(p).run(), vm);
    notify(vm);
  } catch (boost::program_options::error const& iException) {
    std::cout << argv[0] << ": Error while trying to process command line arguments:\n"
              << iException.what() << "\nFor usage and an options list, please do 'cmsRun --help'.";
    return 1;
  }

  if (vm.count(kHelpOpt)) {
    std::cout << desc << std::endl;
    return 0;
  }

  bool verbose = false;
  if (vm.count(kVerboseOpt)) {
    verbose = true;
  }

  if (!vm.count(kMemoryNameOpt)) {
    std::cout << " no argument given" << std::endl;
    return 1;
  }

  if (!vm.count(kUniqueIDOpt)) {
    std::cout << " no second argument given" << std::endl;
    return 1;
  }

  using namespace std::string_literals;
  using namespace std::filesystem;

  auto newDir = path("thread"s + vm[kUniqueIDOpt].as<std::string>());
  create_directory(newDir);
  current_path(newDir);

  WorkerMonitorThread monitorThread;

  monitorThread.startThread();

  std::string presentState = "setting up communicationChannel";

  CMS_SA_ALLOW try {
    std::string const memoryName(vm[kMemoryNameOpt].as<std::string>());
    std::string const uniqueID(vm[kUniqueIDOpt].as<std::string>());
    s_uniqueID = uniqueID;
    {
      //This class is holding the lock
      WorkerChannel communicationChannel(memoryName, uniqueID);

      presentState = "setting up read/write buffers";
      WriteBuffer sm_buffer{memoryName, communicationChannel.fromWorkerBufferInfo()};
      ReadBuffer sm_readbuffer{std::string("Rand") + memoryName, communicationChannel.toWorkerBufferInfo()};
      int counter = 0;

      presentState = "setting up monitor thread";
      //The lock must be released if there is a catastrophic signal
      auto lockPtr = communicationChannel.accessLock();

      monitorThread.setAction([lockPtr]() {
        if (lockPtr) {
          std::cerr << s_uniqueID << " process: SIGNAL CAUGHT: unlock " << formattedTime().data() << "\n";
          lockPtr->unlock();
        }
      });

      presentState = "setting up termination handler";
      //be sure to unset the address of the shared lock before the lock goes away
      s_sharedLock = lockPtr;
      auto unsetLockPtr = [](void*) { s_sharedLock = nullptr; };
      std::unique_ptr<decltype(s_sharedLock), decltype(unsetLockPtr)> sharedLockGuard{&s_sharedLock, unsetLockPtr};
      std::atexit(atexit_handler);
      auto releaseLock = []() {
        if (s_sharedLock) {
          std::cerr << s_uniqueID << " process: terminate called: unlock " << formattedTime().data() << "\n";
          s_sharedLock->unlock();
          s_sharedLock = nullptr;
          //deactivate the abort signal

          struct sigaction act;
          act.sa_sigaction = nullptr;
          act.sa_flags = SA_SIGINFO;
          sigemptyset(&act.sa_mask);
          sigaction(SIGABRT, &act, nullptr);
          std::abort();
        }
      };
      std::set_terminate(releaseLock);

      presentState = "setting up serializers";
      Serializer<ExternalGeneratorEventInfo> serializer(sm_buffer);
      Serializer<ExternalGeneratorLumiInfo> bl_serializer(sm_buffer);
      Serializer<GenLumiInfoProduct> el_serializer(sm_buffer);
      Serializer<GenRunInfoProduct> er_serializer(sm_buffer);

      ROOTDeserializer<edm::RandomNumberGeneratorState, ReadBuffer> random_deserializer(sm_readbuffer);

      presentState = "reading configuration";
      std::cerr << uniqueID << " process: initializing " << formattedTime().data() << std::endl;
      int nlines;
      std::cin >> nlines;

      std::string configuration;
      for (int i = 0; i < nlines; ++i) {
        std::string c;
        std::getline(std::cin, c);
        if (verbose) {
          std::cerr << c << "\n";
        }
        configuration += c + "\n";
      }

      presentState = "setting up random number generator";
      edm::ExternalRandomNumberGeneratorService* randomService = new edm::ExternalRandomNumberGeneratorService;
      auto serviceToken =
          edm::ServiceRegistry::createContaining(std::unique_ptr<edm::RandomNumberGenerator>(randomService));
      Harness harness(configuration, serviceToken);

      //Some generator libraries override the signal handlers
      monitorThread.setupSignalHandling();
      std::set_terminate(releaseLock);

      if (verbose) {
        std::cerr << uniqueID << " process: done initializing " << formattedTime().data() << std::endl;
      }
      presentState = "finished initialization";
      communicationChannel.workerSetupDone();

      presentState = "waiting for transition";
      if (verbose)
        std::cerr << uniqueID << " process: waiting " << counter << " " << formattedTime().data() << std::endl;
      communicationChannel.handleTransitions([&](edm::Transition iTransition, unsigned long long iTransitionID) {
        ++counter;
        switch (iTransition) {
          case edm::Transition::BeginRun: {
            presentState = "beginRun transition";
            if (verbose)
              std::cerr << uniqueID << " process: start beginRun " << formattedTime().data() << std::endl;
            if (verbose)
              std::cerr << uniqueID << " process: end beginRun " << formattedTime().data() << std::endl;

            break;
          }
          case edm::Transition::BeginLuminosityBlock: {
            presentState = "begin lumi";
            if (verbose)
              std::cerr << uniqueID << " process: start beginLumi " << formattedTime().data() << std::endl;
            auto randState = random_deserializer.deserialize();
            presentState = "deserialized random state in begin lumi";
            if (verbose)
              std::cerr << uniqueID << " random " << randState.state_.size() << " " << randState.seed_ << std::endl;
            randomService->setState(randState.state_, randState.seed_);
            presentState = "processing begin lumi";
            auto value = harness.getBeginLumiValue(iTransitionID);
            value.randomState_.state_ = randomService->getState();
            value.randomState_.seed_ = randomService->mySeed();

            presentState = "serialize lumi";
            bl_serializer.serialize(value);
            if (verbose)
              std::cerr << uniqueID << " process: end beginLumi " << formattedTime().data() << std::endl;
            if (verbose)
              std::cerr << uniqueID << "   rand " << value.randomState_.state_.size() << " " << value.randomState_.seed_
                        << std::endl;
            break;
          }
          case edm::Transition::Event: {
            presentState = "begin event";
            if (verbose)
              std::cerr << uniqueID << " process: event " << counter << " " << formattedTime().data() << std::endl;
            presentState = "deserialized random state in event";
            auto randState = random_deserializer.deserialize();
            randomService->setState(randState.state_, randState.seed_);
            presentState = "processing event";
            auto value = harness.getEventValue();
            value.randomState_.state_ = randomService->getState();
            value.randomState_.seed_ = randomService->mySeed();

            if (verbose)
              std::cerr << uniqueID << " process: event " << counter << " " << formattedTime().data() << std::endl;

            presentState = "serialize event";
            serializer.serialize(value);
            if (verbose)
              std::cerr << uniqueID << " process: "
                        << " " << counter << std::endl;
            //usleep(10000000);
            break;
          }
          case edm::Transition::EndLuminosityBlock: {
            presentState = "begin end lumi";
            if (verbose)
              std::cerr << uniqueID << " process: start endLumi " << formattedTime().data() << std::endl;
            presentState = "processing end lumi";
            auto value = harness.getEndLumiValue();

            presentState = "serialize end lumi";
            el_serializer.serialize(value);
            if (verbose)
              std::cerr << uniqueID << " process: end endLumi " << formattedTime().data() << std::endl;

            break;
          }
          case edm::Transition::EndRun: {
            presentState = "begin end run";
            if (verbose)
              std::cerr << uniqueID << " process: start endRun " << formattedTime().data() << std::endl;
            presentState = "process end run";
            auto value = harness.getEndRunValue();

            presentState = "serialize end run";
            er_serializer.serialize(value);
            if (verbose)
              std::cerr << uniqueID << " process: end endRun " << formattedTime().data() << std::endl;

            break;
          }
          default: {
            assert(false);
          }
        }
        presentState = "notifying and waiting after " + presentState;
        if (verbose)
          std::cerr << uniqueID << " process: notifying and waiting " << counter << " " << std::endl;
      });
    }
  } catch (std::exception const& iExcept) {
    std::cerr << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
              << s_uniqueID << " process: caught exception \n"
              << iExcept.what() << " " << formattedTime().data() << "\n"
              << "  while " << presentState << "\n"
              << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
    return 1;
  } catch (...) {
    std::cerr << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
              << s_uniqueID << " process: caught unknown exception " << formattedTime().data() << "\n  while "
              << presentState << "\n"
              << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
    return 1;
  }
  return 0;
}