CMS 3D CMS Logo

DependencyGraph.cc
Go to the documentation of this file.
1 /*
2  * Simple Service to make a GraphViz graph of the modules runtime dependencies:
3  * - draw hard dependencies according to the "consumes" dependencies;
4  * - draw soft dependencies to reflect the order of scheduled modue in each path;
5  * - draw SubProcesses in subgraphs.
6  *
7  * Use GraphViz dot to generate an SVG representation of the dependencies:
8  *
9  * dot -v -Tsvg dependency.gv -o dependency.svg
10  *
11  */
12 
13 #include <iostream>
14 #include <vector>
15 #include <string>
16 #include <type_traits>
17 
18 // boost optional (used by boost graph) results in some false positives with -Wmaybe-uninitialized
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
21 #include <boost/graph/adjacency_list.hpp>
22 #include <boost/graph/graphviz.hpp>
23 #include <boost/graph/lookup_edge.hpp>
24 #pragma GCC diagnostic pop
25 
37 
38 using namespace edm;
39 using namespace edm::service;
40 
41 namespace {
42  namespace {
43 
44  template<typename T>
45  std::unordered_set<T> make_unordered_set(std::vector<T> && entries) {
46  std::unordered_set<T> u;
47  for (T & entry: entries)
48  u.insert(std::move(entry));
49  return u;
50  }
51 
52  } // (anonymous)
53 } // (anonymous)
54 
55 
57 public:
59 
60  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
61 
62  void preSourceConstruction(ModuleDescription const &);
63  void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &);
64  void postBeginJob();
65 
66 private:
67  bool highlighted(std::string const & module) {
68  return (m_highlightModules.find(module) != m_highlightModules.end());
69  }
70 
71  enum class EDMModuleType {
72  Unknown,
73  Source,
74  ESSource,
75  ESProducer,
76  EDAnalyzer,
77  EDProducer,
78  EDFilter,
80  };
81 
82  static constexpr
83  const char * module_type_desc[] {
84  "Unknown",
85  "Source",
86  "ESSource",
87  "ESProducer",
88  "EDAnalyzer",
89  "EDProducer",
90  "EDFilter",
91  "OutputModule"
92  };
93 
94  static constexpr
95  const char * shapes[] {
96  "note", // Unknown
97  "oval", // Source
98  "cylinder", // ESSource
99  "cylinder", // ESProducer
100  "oval", // EDAnalyzer
101  "box", // EDProducer
102  "diamond", // EDFilter
103  "oval", // OutputModule
104  };
105 
106  static
108 
109  static
110  const char * edmModuleType(edm::ModuleDescription const & module);
111 
112  struct node {
115  unsigned int id;
117  bool scheduled;
118  };
119 
120  using GraphvizAttributes = std::map<std::string, std::string>;
121 
122  // directed graph, with `node` properties attached to each vertex
123  boost::subgraph<boost::adjacency_list<
124  // edge list
125  boost::vecS,
126  // vertex list
127  boost::vecS,
128  boost::directedS,
129  // vertex properties
130  boost::property<boost::vertex_attribute_t, GraphvizAttributes, // Graphviz vertex attributes
131  node>,
132  // edge propoerties
133  boost::property<boost::edge_index_t, int, // used internally by boost::subgraph
134  boost::property<boost::edge_attribute_t, GraphvizAttributes>>, // Graphviz edge attributes
135  // graph properties
136  boost::property<boost::graph_name_t, std::string, // name each boost::subgraph
137  boost::property<boost::graph_graph_attribute_t, GraphvizAttributes, // Graphviz graph attributes
138  boost::property<boost::graph_vertex_attribute_t, GraphvizAttributes,
139  boost::property<boost::graph_edge_attribute_t, GraphvizAttributes>>>>
141 
142  std::string m_filename;
143  std::unordered_set<std::string> m_highlightModules;
144 
147 };
148 
149 constexpr
150 const char * DependencyGraph::module_type_desc[];
151 
152 constexpr
153 const char * DependencyGraph::shapes[];
154 
155 
157 {
158  auto const & registry = * edm::pset::Registry::instance();
159  auto const & pset = * registry.getMapped(module.parameterSetID());
160 
161  if (not pset.existsAs<std::string>("@module_edm_type"))
162  return EDMModuleType::Unknown;
163 
164  std::string const & t = pset.getParameter<std::string>("@module_edm_type");
165  for (EDMModuleType v: {
167  EDMModuleType::ESSource,
169  EDMModuleType::EDAnalyzer,
170  EDMModuleType::EDProducer,
171  EDMModuleType::EDFilter,
172  EDMModuleType::OutputModule
173  }) {
174  if (t == module_type_desc[static_cast<std::underlying_type_t<EDMModuleType>>(v)])
175  return v;
176  }
177  return EDMModuleType::Unknown;
178 }
179 
180 
182 {
183  return module_type_desc[static_cast<std::underlying_type_t<EDMModuleType>>(edmModuleTypeEnum(module))];
184 }
185 
186 
187 void
189 {
191  desc.addUntracked<std::string>("fileName", "dependency.gv");
192  desc.addUntracked<std::vector<std::string>>("highlightModules", {});
193  desc.addUntracked<bool>("showPathDependencies", true);
194  descriptions.add("DependencyGraph", desc);
195 }
196 
197 
199  m_filename( config.getUntrackedParameter<std::string>("fileName") ),
200  m_highlightModules( make_unordered_set(config.getUntrackedParameter<std::vector<std::string>>("highlightModules")) ),
201  m_showPathDependencies( config.getUntrackedParameter<bool>("showPathDependencies") ),
202  m_initialized( false )
203 {
207 }
208 
209 
210 // adaptor to use range-based for loops with boost::graph edges(...) and vertices(...) functions
211 template <typename I>
212 struct iterator_pair_as_a_range : std::pair<I, I>
213 {
214 public:
215  using std::pair<I, I>::pair;
216 
217  I begin() { return this->first; }
218  I end() { return this->second; }
219 };
220 
221 template <typename I>
223 {
225 }
226 
227 
228 void
230  // create graph vertex for the source module and fill its attributes
231  boost::add_vertex(m_graph);
232  m_graph.m_graph[module.id()] = node{ module.moduleLabel(), module.moduleName(), module.id(), EDMModuleType::Source, true };
233  auto & attributes = boost::get(boost::get(boost::vertex_attribute, m_graph), 0);
234  attributes["label"] = module.moduleLabel();
235  attributes["tooltip"] = module.moduleName();
236  attributes["shape"] = shapes[static_cast<std::underlying_type_t<EDMModuleType>>(EDMModuleType::Source)];
237  attributes["style"] = "filled";
238  attributes["color"] = "black";
239  attributes["fillcolor"] = highlighted(module.moduleLabel()) ? "lightgreen" : "white";
240 }
241 
242 
243 void
245 
246  // if the Service is not in the main Process do not do anything
247  if (context.isSubProcess() and not m_initialized) {
248  edm::LogError("DependencyGraph")
249  << "You have requested an instance of the DependencyGraph Service in the \"" << context.processName()
250  << "\" SubProcess, which is not supported.\nPlease move it to the main process.";
251  return;
252  }
253 
254  if (not context.isSubProcess()) {
255  // set the graph name property to the process name
256  boost::get_property(m_graph, boost::graph_name) = context.processName();
257  boost::get_property(m_graph, boost::graph_graph_attribute)["label"] = "process " + context.processName();
258  boost::get_property(m_graph, boost::graph_graph_attribute)["labelloc"] = "top";
259 
260  // create graph vertices associated to all modules in the process
261  auto size = pathsAndConsumes.allModules().size();
262  for (size_t i = 0; i < size; ++i)
263  boost::add_vertex(m_graph);
264 
265  m_initialized = true;
266  } else {
267  // create a subgraph to match the subprocess
268  auto & graph = m_graph.create_subgraph();
269 
270  // set the subgraph name property to the subprocess name
271  boost::get_property(graph, boost::graph_name) = "cluster" + context.processName();
272  boost::get_property(graph, boost::graph_graph_attribute)["label"] = "subprocess " + context.processName();
273  boost::get_property(graph, boost::graph_graph_attribute)["labelloc"] = "top";
274 
275  // create graph vertices associated to all modules in the subprocess
276  auto size = pathsAndConsumes.allModules().size();
277  for (size_t i = 0; i < size; ++i)
278  boost::add_vertex(graph);
279  }
280 
281  // set the vertices properties (use the module id as the global index into the graph)
282  for (edm::ModuleDescription const * module: pathsAndConsumes.allModules()) {
283  m_graph.m_graph[module->id()] = { module->moduleLabel(), module->moduleName(), module->id(), edmModuleTypeEnum(*module), false };
284 
285  auto & attributes = boost::get(boost::get(boost::vertex_attribute, m_graph), module->id());
286  attributes["label"] = module->moduleLabel();
287  attributes["tooltip"] = module->moduleName();
288  attributes["shape"] = shapes[static_cast<std::underlying_type_t<EDMModuleType>>(edmModuleTypeEnum(*module))];
289  attributes["style"] = "filled";
290  attributes["color"] = "black";
291  attributes["fillcolor"] = highlighted(module->moduleLabel()) ? "green" : "lightgrey";
292  }
293 
294  // paths and endpaths
295  auto const & paths = pathsAndConsumes.paths();
296  auto const & endps = pathsAndConsumes.endPaths();
297 
298  // add graph edges associated to module dependencies
299  for (edm::ModuleDescription const * consumer: pathsAndConsumes.allModules()) {
300  for (edm::ModuleDescription const * module: pathsAndConsumes.modulesWhoseProductsAreConsumedBy(consumer->id())) {
301  edm::LogInfo("DependencyGraph") << "module " << consumer->moduleLabel() << " depends on module " << module->moduleLabel();
302  auto edge_status = boost::add_edge(consumer->id(), module->id(), m_graph);
303  // highlight the arrow between highlighted nodes
304  if (highlighted(module->moduleLabel()) and highlighted(consumer->moduleLabel())) {
305  auto const & edge = edge_status.first;
306  auto & attributes = boost::get(boost::get(boost::edge_attribute, m_graph), edge);
307  attributes["color"] = "darkgreen";
308  }
309  }
310  }
311 
312  // marke the modules in the paths as scheduled, and add a soft dependency to reflect the order of modules along each path
313  edm::ModuleDescription const * previous;
314  for (unsigned int i = 0; i < paths.size(); ++i) {
315  previous = nullptr;
316  for (edm::ModuleDescription const * module: pathsAndConsumes.modulesOnPath(i)) {
317  m_graph.m_graph[module->id()].scheduled = true;
318  auto & attributes = boost::get(boost::get(boost::vertex_attribute, m_graph), module->id());
319  attributes["fillcolor"] = highlighted(module->moduleLabel()) ? "lightgreen" : "white";
320  if (previous and m_showPathDependencies) {
321  edm::LogInfo("DependencyGraph") << "module " << module->moduleLabel() << " follows module " << previous->moduleLabel() << " in Path " << i;
322  auto edge_status = boost::lookup_edge(module->id(), previous->id(), m_graph);
323  bool found = edge_status.second;
324  if (not found) {
325  edge_status = boost::add_edge(module->id(), previous->id(), m_graph);
326  auto const & edge = edge_status.first;
327  auto & attributes = boost::get(boost::get(boost::edge_attribute, m_graph), edge);
328  attributes["style"] = "dashed";
329  // highlight the arrow between highlighted nodes
330  if (highlighted(module->moduleLabel()) and highlighted(previous->moduleLabel()))
331  attributes["color"] = "darkgreen";
332  }
333  }
334  previous = module;
335  }
336  }
337  for (unsigned int i = 0; i < endps.size(); ++i) {
338  previous = nullptr;
339  for (edm::ModuleDescription const * module: pathsAndConsumes.modulesOnEndPath(i)) {
340  m_graph.m_graph[module->id()].scheduled = true;
341  auto & attributes = boost::get(boost::get(boost::vertex_attribute, m_graph), module->id());
342  attributes["fillcolor"] = highlighted(module->moduleLabel()) ? "lightgreen" : "white";
343  if (previous and m_showPathDependencies) {
344  edm::LogInfo("DependencyGraph") << "module " << module->moduleLabel() << " follows module " << previous->moduleLabel() << " in EndPath " << i;
345  auto edge_status = boost::lookup_edge(module->id(), previous->id(), m_graph);
346  bool found = edge_status.second;
347  if (not found) {
348  edge_status = boost::add_edge(module->id(), previous->id(), m_graph);
349  auto const & edge = edge_status.first;
350  auto & attributes = boost::get(boost::get(boost::edge_attribute, m_graph), edge);
351  attributes["style"] = "dashed";
352  // highlight the arrow between highlighted nodes
353  if (highlighted(module->moduleLabel()) and highlighted(previous->moduleLabel()))
354  attributes["color"] = "darkgreen";
355  }
356  }
357  previous = module;
358  }
359  }
360 }
361 
362 void
364 
365  if (not m_initialized)
366  return;
367 
368  // draw the dependency graph
369  std::ofstream out(m_filename);
370  boost::write_graphviz(
371  out,
372  m_graph
373  );
374  out.close();
375 }
376 
377 namespace edm {
378 namespace service {
379 
380 inline
382  return true;
383 }
384 
385 } // namespace service
386 } // namespace edm
387 
388 // define as a framework servie
size
Write out results.
std::string const & processName() const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
iterator_pair_as_a_range< I > make_range(std::pair< I, I > p)
void watchPreSourceConstruction(PreSourceConstruction::slot_type const &iSlot)
void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &)
std::vector< ModuleDescription const * > const & modulesOnEndPath(unsigned int endPathIndex) const
std::string const & moduleName() const
Definition: config.py:1
std::vector< ModuleDescription const * > const & modulesOnPath(unsigned int pathIndex) const
constexpr const char * module_type_desc[]
Definition: EDMModuleType.h:20
std::string const & moduleLabel() const
#define constexpr
std::vector< ModuleDescription const * > const & allModules() const
EDMModuleType edmModuleTypeEnum(edm::ModuleDescription const &module)
std::vector< std::string > const & endPaths() const
U second(std::pair< T, U > const &p)
std::map< std::string, std::string > GraphvizAttributes
void preSourceConstruction(ModuleDescription const &)
const std::complex< double > I
Definition: I.h:8
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
boost::subgraph< boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::property< boost::vertex_attribute_t, GraphvizAttributes, node >, boost::property< boost::edge_index_t, int, boost::property< boost::edge_attribute_t, GraphvizAttributes > >, boost::property< boost::graph_name_t, std::string, boost::property< boost::graph_graph_attribute_t, GraphvizAttributes, boost::property< boost::graph_vertex_attribute_t, GraphvizAttributes, boost::property< boost::graph_edge_attribute_t, GraphvizAttributes > > > > > > m_graph
static const char * edmModuleType(edm::ModuleDescription const &module)
std::unordered_set< std::string > m_highlightModules
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
bool isProcessWideService(ZombieKillerService const *)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static EDMModuleType edmModuleTypeEnum(edm::ModuleDescription const &module)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< ModuleDescription const * > const & modulesWhoseProductsAreConsumedBy(unsigned int moduleID) const
void watchPreBeginJob(PreBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
DependencyGraph(const ParameterSet &, ActivityRegistry &)
HLT enums.
std::vector< std::string > const & paths() const
std::string m_filename
static const char * shapes[]
ParameterSetID const & parameterSetID() const
bool highlighted(std::string const &module)
static Interceptor::Registry registry("Interceptor")
static const char * module_type_desc[]
long double T
bool isSubProcess() const
Definition: vlib.h:208
def move(src, dest)
Definition: eostools.py:511
const char * edmModuleType(edm::ModuleDescription const &module)
T get(const Candidate &c)
Definition: component.h:55
static Registry * instance()
Definition: Registry.cc:13
unsigned int id() const
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal