Commits

Maciej Fijalkowski committed 5ddb542 Draft

General progress and playing around

Comments (0)

Files changed (7)

_jitviewer2/jitrecorder.py

 try:
     import pypyjit
 except ImportError:
-    print "This is a PyPy JIT debug program, it requires pypy as a running interpreter."
-    sys.exit(1)
+    raise ImportError("This is a PyPy JIT debug program, it requires pypy as a running interpreter.")
 import json
-import inspect
 from disassembler import disassemble
 
 def process_times(times):
 
 def append_sources(allcodes):
     names = {}
-    source_cache = {}
     code_repr_cache = {}
     for key, codeinfo in allcodes.iteritems():
         for callid, pycode, bytecode_no, op_i in codeinfo.code_points:

_jitviewer2/server.py

 import tornado.ioloop
 import tornado.web
 import jinja2
+import json
 
 from _jitviewer2.traces import plot_traces, traces_from_json,\
      attach_bridges_and_jumps
     def get(self, num):
         with open(os.path.join(TEMPLATE_DIR, 'loop.html')) as f:
             TMPL = jinja2.Template(f.read())
+        self.write(TMPL.render(num=int(num)))
+
+class GetLoopDataHandler(tornado.web.RequestHandler):
+    def get(self, num):
         trace = self.application.traces[int(num)]
         data = plot_traces(trace, 0, 0)
-        self.write(TMPL.render(data=data))
+        loop_details = {}
+        for trace in self.application.all_traces:
+            loop_details['%s-%s' % (trace.type, trace.no)] = {
+                'name': trace.name,
+                'count': trace.count,
+                'numops': len(trace.operations),
+            }
+        self.write(json.dumps({'coords': data, 'loop_data': loop_details}))
 
 def main(argv):
     settings = {
     
     application = tornado.web.Application([
         (r"/", MainHandler),
-        (r'/showloop/([0-9]+)', ShowHandler), 
+        (r'/showloop/([0-9]+)', ShowHandler),
+        (r'/data/loop/([0-9]+)', GetLoopDataHandler),
         (r"/static", tornado.web.StaticFileHandler, STATIC_DIR),
     ], **settings)
 
     with open(argv[0]) as f:
         traces, application.trace_info = traces_from_json(f.read())
+        application.all_traces = traces
         application.traces = attach_bridges_and_jumps(traces,
                                                       application.trace_info)
     application.listen(8888)
+    print "Server started"
     tornado.ioloop.IOLoop.instance().start()

_jitviewer2/static/style.css

     stroke-opacity: 0.2;
     fill: none;
 }
+
+.trace-path:hover {
+    stroke-opacity: 0.5;
+}
+
+#draw-area {
+    position: absolute;
+}
+
+body {
+    font-family: Verdana;
+}
+
+#main {
+    margin-left: 400px;
+}

_jitviewer2/static/viewer.js

 
 function plot_path(input)
 {
-    // "M 100 100 q 100 0 200 -50 q 100 -50 200 -50")
-
     var so_far = 0; // used by compute path
     var total_width = 30;
 
         max_w = Math.max(max_w, elem[4]);
     }
     var scale_y = ($(window).height() - 40) / maximal_y;
-    var scale_x = 2;
+    var scale_x = 380 / (maximal_x - minimal_x);
     
     var shift_x = - minimal_x * scale_x + 20;
     var shift_y = 20;
 
-    function XXcompute_path(d) {
-        //var move = d[1] * 100;
-        var left = d[0] < data.length / 2;
-        var pos_x = so_far + 300 + d[1] * total_width / 2;
-        so_far += d[1] * total_width;
-        console.log(so_far);
-        if (left) {
-            return "M " + pos_x + " 100 q 0 100 -50 200 q -50 100 -50 200";
-        } else {
-            return "M " + pos_x + " 100 q 0 100 50 200 q 50 100 50 200";            
-        }
-    }
-
     function compute_width(d) {
         return (d[4] / max_w) * total_width;
     }
         var s_y = y * scale_y + shift_y
         var w_x = w * scale_x;
         var w_y = h * scale_y;
-        var angle = w_x / w_y;
-        console.log(x, y, w, h);
-        var coords = ["M" + s_x, s_y, "q" + 0, w_y/4, w_x/2, w_y/2,
-                      "q" + (w_x / 4), 0, w_x/2, w_y/2];
-        console.log(coords);
+        if (w_x == 0 || w_y == 0) {
+            // straight line
+            var dx  = w_x / 4;
+            var dx2 = w_x / 4;
+            var dy  = w_y / 4;
+            var dy2 = w_y / 4;
+        } else {
+            var angle = w_x / w_y;
+            var distance = Math.sqrt(w_x * w_x + w_y * w_y) / 8;
+            if (w_x > 0) {
+                var dx = w_x / 4 - distance * angle;
+                var dy = w_y / 4 + distance / angle;
+                var dx2 = w_x / 4 + distance * angle;
+                var dy2 = w_y / 4 - distance / angle;
+            } else {
+                var dx = w_x / 4 - distance * angle;
+                var dy = w_y / 4 - distance / angle;
+                var dx2 = w_x / 4 + distance * angle;
+                var dy2 = w_y / 4 + distance / angle;
+            }                
+        }
+        var coords = ["M" + s_x, s_y, "q" + " " + dx, dy, w_x/2, w_y/2,
+                      "q" + " " + dx2, dy2, w_x/2, w_y/2];
+        //console.log(coords);
         return coords;
     }
 
         return move(d[0], d[1], d[2], d[3]);
     }
 
+    function compute_onmouseover(d)
+    {
+        return "display_loop('" + d[5] + "', " + d[6] + "); return false;";
+    }
+
     var svg = d3.select("svg");
+    //input = input.slice(0, 5);
     svg.selectAll("path").data(input).enter().append("path")
         .attr("d", compute_path)
         .attr("class", "trace-path")
         .attr("stroke", "grey")
-        .attr("stroke-width", compute_width);
+        .attr("stroke-width", compute_width)
+        .attr("onmouseover", compute_onmouseover)
 }
+
+function display_loop(tp, no)
+{
+    var info = loop_data[tp + "-" + no];
+    $("#info").html(info.name + ", " + info.numops + " operations, executed "
+                   + info.count + " times");
+}
+
+var loop_data;
+
+function init_loop(num)
+{
+    $.getJSON("/data/loop/" + num, function (data) {
+        loop_data = data['loop_data'];
+        plot_path(data['coords']);
+    });
+}

_jitviewer2/templates/loop.html

   </head>
   <body>
     <script>
-      $(document).ready(function () { plot_path({{ data }}); });
+      $(document).ready(function () { init_loop({{ num }}); });
     </script>
-    <svg>
+    <svg width="400px" id="draw-area">
     </svg>
+    <div id="main">
+      <p id="info">
+      </p>
+    </div>
   </body>
 </html>

_jitviewer2/traces.py

         lgt = max(MIN_LEN, self.len)
         size_x = ANGLE_X * slant * lgt
         size_y = ANGLE_Y * lgt
-        p = [[parent_x, parent_y, size_x, size_y, self.width]]
+        p = [[parent_x, parent_y, size_x, size_y, self.width, self.id[0],
+              self.id[1]]]
         if self.left is not None:
             p += self.left.plot(parent_x + size_x, parent_y + size_y, -1)
             p += self.right.plot(parent_x + size_x, parent_y + size_y, 1)
             parent.left = left
             parent.right = right
             parent.len = pos - prev_pos
+            parent.id = (trace.type, trace.no)
             prev_pos = pos
             add_to_stack(subtrace, right)
             parent = left
         parent.len = len(trace.operations) - prev_pos
+        parent.id = (trace.type, trace.no)
 
     node = Node(start_trace.count)
     add_to_stack(start_trace, node)

testing/test_server.py

+
+import json
+from _jitviewer2.server import GetLoopDataHandler
+from _jitviewer2.traces import Trace, plot_traces
+
+class Application(object):
+    def __init__(self, traces, all_traces=None):
+        self.traces = traces
+        if all_traces is None:
+            self.all_traces = traces
+        else:
+            self.all_traces = all_traces
+
+def test_get_loop_data():
+    app = Application([Trace(range(5), "name1", 15, 'l', 0),
+                       Trace(range(10), "name2", 10, 'l', 1)])
+    handler = object.__new__(GetLoopDataHandler)
+    handler.application = app
+    l = []
+    def write(stuff):
+        l.append(stuff)
+    handler.write = write
+    handler.get(1)
+    assert len(l) == 1
+    d = json.loads(l[0])
+    assert d['coords'] == plot_traces(app.traces[1], 0, 0)
+    assert d['loop_data']['l-0']['name'] == 'name1'