From e93c8834704aec28c32df33965ed9367c20c9a9f Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Sat, 3 Mar 2018 13:42:36 +0000 Subject: [PATCH] Hadrons: basic GraphViz visualisation --- extras/Hadrons/VirtualMachine.cc | 43 +++++++++++++++++++++++++++++++ extras/Hadrons/VirtualMachine.hpp | 6 ++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/extras/Hadrons/VirtualMachine.cc b/extras/Hadrons/VirtualMachine.cc index 1c60aa5b..eb1f68ba 100644 --- a/extras/Hadrons/VirtualMachine.cc +++ b/extras/Hadrons/VirtualMachine.cc @@ -111,6 +111,7 @@ void VirtualMachine::pushModule(VirtualMachine::ModPt &pt) { // output does not exists, add it env().addObject(out, address); + module_[address].output.push_back(env().getObjectAddress(out)); } else { @@ -313,6 +314,48 @@ void VirtualMachine::makeModuleGraph(void) graph_ = graph; } +// dump GraphViz graph ///////////////////////////////////////////////////////// +void VirtualMachine::dumpModuleGraph(std::ostream &out) +{ + makeModuleGraph(); + out << "digraph hadrons {" << std::endl; + out << "node [shape=record, fontname=\"Courier\", fontsize=\"11\"];" << std::endl; + out << "graph [fontname = \"Courier\", fontsize=\"11\"];" << std::endl; + out << "edge [fontname = \"Courier\", fontsize=\"11\"];"<< std::endl; + for (unsigned int m = 0; m < module_.size(); ++m) + { + + } + for (unsigned int m = 0; m < module_.size(); ++m) + { + for (auto &in: module_[m].input) + { + int min = env().getObjectModule(in); + + out << min << " -> " << m << " [ label = \"" + << env().getObjectName(in) << "\" ];" << std::endl; + } + } + for (unsigned int m = 0; m < module_.size(); ++m) + { + out << m << " [ label = \"{ " << getModule(m)->getRegisteredName() + << " | " << getModuleName(m) << "}\" ];" << std::endl; + } + out << "}\n" << std::endl; +} + +void VirtualMachine::dumpModuleGraph(void) +{ + dumpModuleGraph(std::cout); +} + +void VirtualMachine::dumpModuleGraph(const std::string filename) +{ + std::ofstream f(filename); + + dumpModuleGraph(f); +} + // memory profile ////////////////////////////////////////////////////////////// const VirtualMachine::MemoryProfile & VirtualMachine::getMemoryProfile(void) { diff --git a/extras/Hadrons/VirtualMachine.hpp b/extras/Hadrons/VirtualMachine.hpp index 19a74f94..68eeb0c0 100644 --- a/extras/Hadrons/VirtualMachine.hpp +++ b/extras/Hadrons/VirtualMachine.hpp @@ -84,7 +84,7 @@ private: const std::type_info *type{nullptr}; std::string name; ModPt data{nullptr}; - std::vector input; + std::vector input, output; size_t maxAllocated; }; public: @@ -120,6 +120,10 @@ public: void printContent(void) const; // module graph (could be a const reference if topoSort was const) Graph getModuleGraph(void); + // dump GraphViz graph + void dumpModuleGraph(std::ostream &out); + void dumpModuleGraph(void); + void dumpModuleGraph(const std::string filename); // memory profile const MemoryProfile &getMemoryProfile(void); // garbage collector