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
#include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
#include "FWCore/ServiceRegistry/interface/InternalContext.h"
#include "FWCore/ServiceRegistry/interface/PathContext.h"
#include "FWCore/ServiceRegistry/interface/PlaceInPathContext.h"
#include "FWCore/ServiceRegistry/interface/StreamContext.h"
#include "FWCore/ServiceRegistry/interface/GlobalContext.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "DataFormats/Provenance/interface/ModuleDescription.h"

#include <ostream>

namespace edm {

  ModuleCallingContext::ModuleCallingContext(ModuleDescription const* moduleDescription) noexcept
      : previousModuleOnThread_(nullptr),
        moduleDescription_(moduleDescription),
        parent_(),
        id_(0),
        state_(State::kInvalid) {}

  ModuleCallingContext::ModuleCallingContext(ModuleDescription const* moduleDescription,
                                             std::uintptr_t id,
                                             State state,
                                             ParentContext const& parent,
                                             ModuleCallingContext const* previousOnThread) noexcept
      : previousModuleOnThread_(previousOnThread),
        moduleDescription_(moduleDescription),
        parent_(parent),
        id_(id),
        state_(state) {}

  void ModuleCallingContext::setContext(State state,
                                        ParentContext const& parent,
                                        ModuleCallingContext const* previousOnThread) noexcept {
    state_ = state;
    parent_ = parent;
    previousModuleOnThread_ = previousOnThread;
  }

  StreamContext const* ModuleCallingContext::getStreamContext() const noexcept(false) {
    ModuleCallingContext const* mcc = getTopModuleCallingContext();
    if (mcc->type() == ParentContext::Type::kPlaceInPath) {
      return mcc->placeInPathContext()->pathContext()->streamContext();
    } else if (mcc->type() != ParentContext::Type::kStream) {
      throw Exception(errors::LogicError)
          << "ModuleCallingContext::getStreamContext() called in context not linked to a StreamContext\n";
    }
    return mcc->streamContext();
  }

  GlobalContext const* ModuleCallingContext::getGlobalContext() const noexcept(false) {
    ModuleCallingContext const* mcc = getTopModuleCallingContext();
    if (mcc->type() != ParentContext::Type::kGlobal) {
      throw Exception(errors::LogicError)
          << "ModuleCallingContext::getGlobalContext() called in context not linked to a GlobalContext\n";
    }
    return mcc->globalContext();
  }

  ModuleCallingContext const* ModuleCallingContext::getTopModuleCallingContext() const noexcept {
    ModuleCallingContext const* mcc = this;
    while (mcc->type() == ParentContext::Type::kModule) {
      mcc = mcc->moduleCallingContext();
    }
    if (mcc->type() == ParentContext::Type::kInternal) {
      mcc = mcc->internalContext()->moduleCallingContext();
    }
    while (mcc->type() == ParentContext::Type::kModule) {
      mcc = mcc->moduleCallingContext();
    }
    return mcc;
  }

  unsigned ModuleCallingContext::depth() const noexcept {
    unsigned depth = 0;
    ModuleCallingContext const* mcc = this;
    while (mcc->type() == ParentContext::Type::kModule) {
      ++depth;
      mcc = mcc->moduleCallingContext();
    }
    if (mcc->type() == ParentContext::Type::kInternal) {
      ++depth;
      mcc = mcc->internalContext()->moduleCallingContext();
    }
    while (mcc->type() == ParentContext::Type::kModule) {
      ++depth;
      mcc = mcc->moduleCallingContext();
    }
    return depth;
  }

  void exceptionContext(cms::Exception& ex, ModuleCallingContext const& mcc) {
    ModuleCallingContext const* imcc = &mcc;
    while ((imcc->type() == ParentContext::Type::kModule) or (imcc->type() == ParentContext::Type::kInternal)) {
      std::ostringstream iost;
      if (imcc->state() == ModuleCallingContext::State::kPrefetching) {
        iost << "Prefetching for module ";
      } else {
        iost << "Calling method for module ";
      }
      iost << imcc->moduleDescription()->moduleName() << "/'" << imcc->moduleDescription()->moduleLabel() << "'";

      if (imcc->type() == ParentContext::Type::kInternal) {
        iost << " (probably inside some kind of mixing module)";
        imcc = imcc->internalContext()->moduleCallingContext();
      } else {
        imcc = imcc->moduleCallingContext();
      }
      ex.addContext(iost.str());
    }
    std::ostringstream ost;
    if (imcc->state() == ModuleCallingContext::State::kPrefetching) {
      ost << "Prefetching for module ";
    } else {
      ost << "Calling method for module ";
    }
    ost << imcc->moduleDescription()->moduleName() << "/'" << imcc->moduleDescription()->moduleLabel() << "'";
    ex.addContext(ost.str());

    if (imcc->type() == ParentContext::Type::kPlaceInPath) {
      ost.str("");
      ost << "Running path '";
      ost << imcc->placeInPathContext()->pathContext()->pathName() << "'";
      ex.addContext(ost.str());
      auto streamContext = imcc->placeInPathContext()->pathContext()->streamContext();
      if (streamContext) {
        ost.str("");
        edm::exceptionContext(ost, *streamContext);
        ex.addContext(ost.str());
      }
    } else {
      if (imcc->type() == ParentContext::Type::kStream) {
        ost.str("");
        edm::exceptionContext(ost, *(imcc->streamContext()));
        ex.addContext(ost.str());
      } else if (imcc->type() == ParentContext::Type::kGlobal) {
        ost.str("");
        edm::exceptionContext(ost, *(imcc->globalContext()));
        ex.addContext(ost.str());
      }
    }
  }

  std::ostream& operator<<(std::ostream& os, ModuleCallingContext const& mcc) {
    os << "ModuleCallingContext state = ";
    switch (mcc.state()) {
      case ModuleCallingContext::State::kInvalid:
        os << "Invalid";
        break;
      case ModuleCallingContext::State::kPrefetching:
        os << "Prefetching";
        break;
      case ModuleCallingContext::State::kRunning:
        os << "Running";
        break;
    }
    os << "\n";
    if (mcc.state() == ModuleCallingContext::State::kInvalid) {
      return os;
    }
    if (mcc.moduleDescription()) {
      os << "    moduleDescription: " << *mcc.moduleDescription() << "\n";
    }
    os << "    " << mcc.parent();
    if (mcc.previousModuleOnThread()) {
      if (mcc.type() == ParentContext::Type::kModule && mcc.moduleCallingContext() == mcc.previousModuleOnThread()) {
        os << "    previousModuleOnThread: same as parent module\n";
      } else {
        os << "    previousModuleOnThread: " << *mcc.previousModuleOnThread();
      }
    }
    return os;
  }
}  // namespace edm