Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/Debug.java @ 22356:364bab1763c9
add option for dumping shapes to a DOT file
author | Andreas Woess <andreas.woess@oracle.com> |
---|---|
date | Tue, 10 Nov 2015 19:59:29 +0100 |
parents | dc83cc1f94f2 |
children | 9c1d40221bf9 |
rev | line source |
---|---|
18408
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
1 /* |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
2 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved. |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
4 * |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
8 * |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
13 * accompanied this code). |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
14 * |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
18 * |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
21 * questions. |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
22 */ |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
23 package com.oracle.truffle.object; |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
24 |
22356
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
25 import java.io.File; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
26 import java.io.FileNotFoundException; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
27 import java.io.PrintWriter; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
28 import java.io.UnsupportedEncodingException; |
22356
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
29 import java.nio.file.Paths; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
30 import java.util.Collection; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
31 import java.util.concurrent.ConcurrentLinkedQueue; |
18408
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
32 |
22356
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
33 import com.oracle.truffle.object.debug.GraphvizShapeVisitor; |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
34 import com.oracle.truffle.object.debug.JSONShapeVisitor; |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
35 |
18408
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
36 class Debug { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
37 private static Collection<ShapeImpl> allShapes; |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
38 |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
39 static void registerShape(ShapeImpl newShape) { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
40 allShapes.add(newShape); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
41 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
42 |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
43 static { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
44 if (ObjectStorageOptions.DumpShapes) { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
45 allShapes = new ConcurrentLinkedQueue<>(); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
46 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
47 |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
48 if (ObjectStorageOptions.DumpShapes) { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
49 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
50 public void run() { |
22356
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
51 try { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
52 if (ObjectStorageOptions.DumpShapesDOT) { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
53 dumpDOT(); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
54 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
55 if (ObjectStorageOptions.DumpShapesJSON) { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
56 dumpJSON(); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
57 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
58 } catch (FileNotFoundException | UnsupportedEncodingException e) { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
59 throw new RuntimeException(e); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
60 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
61 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
62 |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
63 private void dumpDOT() throws FileNotFoundException, UnsupportedEncodingException { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
64 try (PrintWriter out = new PrintWriter(getOutputFile("dot"), "UTF-8")) { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
65 GraphvizShapeVisitor visitor = new GraphvizShapeVisitor(); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
66 for (ShapeImpl shape : allShapes) { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
67 shape.accept(visitor); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
68 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
69 out.println(visitor); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
70 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
71 } |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
72 |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
73 private void dumpJSON() throws FileNotFoundException, UnsupportedEncodingException { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
74 try (PrintWriter out = new PrintWriter(getOutputFile("json"), "UTF-8")) { |
18408
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
75 out.println("{\"shapes\": ["); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
76 boolean first = true; |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
77 for (ShapeImpl shape : allShapes) { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
78 if (!first) { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
79 out.println(","); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
80 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
81 first = false; |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
82 out.print(shape.accept(new JSONShapeVisitor())); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
83 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
84 if (!first) { |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
85 out.println(); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
86 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
87 out.println("]}"); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
88 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
89 } |
22356
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
90 |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
91 private File getOutputFile(String extension) { |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
92 return Paths.get(ObjectStorageOptions.DumpShapesPath, "shapes." + extension).toFile(); |
364bab1763c9
add option for dumping shapes to a DOT file
Andreas Woess <andreas.woess@oracle.com>
parents:
22157
diff
changeset
|
93 } |
18408
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
94 })); |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
95 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
96 } |
2c3666f44855
Truffle: initial commit of object API implementation
Andreas Woess <andreas.woess@jku.at>
parents:
diff
changeset
|
97 } |