misc: Added dot_writer for Ruby's network topology

Change-Id: Ic71ca7bc2eb4174d70afa368bc9cc987f3df89e9
Signed-off-by: Tiago Muck <tiago.muck@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/17548
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/python/SConscript b/src/python/SConscript
index fb1666c..758ab3d 100644
--- a/src/python/SConscript
+++ b/src/python/SConscript
@@ -52,6 +52,7 @@
 PySource('m5.util', 'm5/util/code_formatter.py')
 PySource('m5.util', 'm5/util/convert.py')
 PySource('m5.util', 'm5/util/dot_writer.py')
+PySource('m5.util', 'm5/util/dot_writer_ruby.py')
 PySource('m5.util', 'm5/util/grammar.py')
 PySource('m5.util', 'm5/util/jobfile.py')
 PySource('m5.util', 'm5/util/multidict.py')
diff --git a/src/python/m5/simulate.py b/src/python/m5/simulate.py
index 6f02327..f8da1fb 100644
--- a/src/python/m5/simulate.py
+++ b/src/python/m5/simulate.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2012 ARM Limited
+# Copyright (c) 2012,2019 ARM Limited
 # All rights reserved.
 #
 # The license below extends only to copyright in the software and shall
@@ -56,6 +56,7 @@
 from . import ticks
 from . import objects
 from m5.util.dot_writer import do_dot, do_dvfs_dot
+from m5.util.dot_writer_ruby import do_ruby_dot
 
 from .util import fatal
 from .util import attrdict
@@ -110,6 +111,7 @@
             pass
 
     do_dot(root, options.outdir, options.dot_config)
+    do_ruby_dot(root, options.outdir, options.dot_config)
 
     # Initialize the global statistics
     stats.initSimStats()
diff --git a/src/python/m5/util/dot_writer_ruby.py b/src/python/m5/util/dot_writer_ruby.py
new file mode 100644
index 0000000..db115f5
--- /dev/null
+++ b/src/python/m5/util/dot_writer_ruby.py
@@ -0,0 +1,136 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Authors: Tiago Muck
+
+# Creates a visual representation of a Ruby network topology
+
+import os
+import m5
+from m5.util import warn
+try:
+    import pydot
+except:
+    pydot = False
+
+
+def _dot_rgb_to_html(r, g, b):
+    return "#%.2x%.2x%.2x" % (r, g, b)
+
+def _dot_create_router_node(full_path, label):
+    return pydot.Node( \
+                         full_path, \
+                         shape = "Mrecord", \
+                         label = label, \
+                         style = "\"rounded, filled\"", \
+                         color = "#000000", \
+                         fillcolor = _dot_rgb_to_html(204, 230, 252), \
+                         fontname = "Arial", \
+                         fontsize = "14", \
+                         fontcolor = "#000000" \
+                         )
+
+def _dot_create_ctrl_node(full_path, label):
+    return pydot.Node( \
+                         full_path, \
+                         shape = "Mrecord", \
+                         label = label, \
+                         style = "\"rounded, filled\"", \
+                         color = "#000000", \
+                         fillcolor = _dot_rgb_to_html(229, 188, 208), \
+                         fontname = "Arial", \
+                         fontsize = "14", \
+                         fontcolor = "#000000" \
+                         )
+
+
+def _dot_create(network, callgraph):
+    for r in network.routers:
+        callgraph.add_node(_dot_create_router_node(r.path(),
+            'R %d' % r.router_id))
+
+    # One link for each direction but draw one edge only
+    connected = dict()
+    for link in network.int_links:
+        if (link.src_node.path() in connected) and \
+           (connected[link.src_node.path()] == link.dst_node.path()):
+           continue
+        callgraph.add_edge(
+            pydot.Edge(link.src_node.path(), link.dst_node.path())
+        )
+        connected[link.dst_node.path()] = link.src_node.path()
+
+    for link in network.ext_links:
+        ctrl = link.ext_node
+        label = ctrl._name
+        if hasattr(ctrl, '_node_type'):
+            label += ' (' + ctrl._node_type + ')'
+        callgraph.add_node(
+            _dot_create_ctrl_node(ctrl.path(), label)
+        )
+
+        callgraph.add_edge(
+            pydot.Edge(link.ext_node.path(), link.int_node.path())
+        )
+
+def _do_dot(network, outdir, dotFilename):
+    callgraph = pydot.Dot(graph_type='graph', rankdir='LR')
+    _dot_create(network, callgraph)
+    dot_filename = os.path.join(outdir, dotFilename)
+    callgraph.write(dot_filename)
+    try:
+        # dot crashes if the figure is extremely wide.
+        # So avoid terminating simulation unnecessarily
+        callgraph.write_svg(dot_filename + ".svg", prog='neato')
+        callgraph.write_pdf(dot_filename + ".pdf", prog='neato')
+    except:
+        warn("failed to generate dot output from %s", dot_filename)
+
+
+def do_ruby_dot(root, outdir, dotFilename):
+    if not pydot:
+        return
+
+    # Generate a graph for all ruby systems
+    networks = []
+    for obj in root.descendants():
+        if isinstance(obj, m5.objects.RubyNetwork):
+            networks.append(obj)
+
+    for network in networks:
+        # We assume each ruby system has a single network
+        rubydotFilename = dotFilename.replace(".dot",
+                                "." + network.get_parent().path() + ".dot")
+        _do_dot(network, outdir, rubydotFilename)