changeset 14572:1d35a2b84553

use Java Allocation Instrumenter to observe allocation in Graal compiler tests
author Doug Simon <doug.simon@oracle.com>
date Mon, 17 Mar 2014 23:45:14 +0100
parents 24431a9b878c
children e3888db8b8a1
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/AllocationScope.java graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java mx/projects
diffstat 5 files changed, 120 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/AllocationScope.java	Mon Mar 17 23:45:14 2014 +0100
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.compiler.test;
+
+import static com.oracle.graal.debug.internal.DebugHistogramAsciiPrinter.*;
+
+import java.io.*;
+import java.util.*;
+
+import com.google.monitoring.runtime.instrumentation.*;
+import com.oracle.graal.debug.DebugHistogram.CountedValue;
+import com.oracle.graal.debug.internal.*;
+
+class AllocationScope implements AutoCloseable {
+    final Object context;
+    final AllocationScope parent;
+    final Map<String, CountedValue> bytesPerType = new HashMap<>();
+    final Map<String, CountedValue> instancesPerType = new HashMap<>();
+
+    public AllocationScope(Object context) {
+        this.context = context;
+        parent = allocationCounter.get();
+        allocationCounter.set(this);
+    }
+
+    private static List<CountedValue> sortedValues(Map<String, CountedValue> map) {
+        ArrayList<CountedValue> res = new ArrayList<>(map.values());
+        Collections.sort(res);
+        return res;
+    }
+
+    public void close() {
+        allocationCounter.set(parent);
+        PrintStream out = System.out;
+        out.println("\n\nAllocation histograms for " + context);
+        DebugHistogramAsciiPrinter printer = new DebugHistogramAsciiPrinter(out, 20, DefaultNameSize, DefaultBarSize);
+        printer.print(sortedValues(instancesPerType), "InstancesPerType");
+        printer.print(sortedValues(bytesPerType), "BytesPerType");
+    }
+
+    public CountedValue instancesPerType(String desc) {
+        CountedValue count = instancesPerType.get(desc);
+        if (count == null) {
+            count = new CountedValue(0, desc);
+            instancesPerType.put(desc, count);
+        }
+        return count;
+    }
+
+    public CountedValue bytesPerType(String desc) {
+        CountedValue count = bytesPerType.get(desc);
+        if (count == null) {
+            count = new CountedValue(0, desc);
+            bytesPerType.put(desc, count);
+        }
+        return count;
+    }
+
+    static ThreadLocal<AllocationScope> allocationCounter = new ThreadLocal<>();
+
+    static class AllocationSampler implements Sampler {
+
+        public void sampleAllocation(int count, String desc, Object newObj, long size) {
+            AllocationScope c = allocationCounter.get();
+            if (c != null) {
+                String type;
+                if (count != -1) {
+                    type = desc + "[]";
+                } else {
+                    type = desc;
+                }
+
+                c.instancesPerType(type).inc();
+                c.bytesPerType(type).add((int) size);
+            }
+        }
+
+    }
+
+    static {
+        AllocationRecorder.addSampler(new AllocationSampler());
+    }
+}
--- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java	Mon Mar 17 12:40:35 2014 -0700
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java	Mon Mar 17 23:45:14 2014 +0100
@@ -630,7 +630,7 @@
         final int id = compilationId.incrementAndGet();
 
         InstalledCode installedCode = null;
-        try (Scope ds = Debug.scope("Compiling", new DebugDumpScope(String.valueOf(id), true))) {
+        try (AllocationScope c = new AllocationScope(method); Scope ds = Debug.scope("Compiling", new DebugDumpScope(String.valueOf(id), true))) {
             final boolean printCompilation = PrintCompilation.getValue() && !TTY.isSuppressed();
             if (printCompilation) {
                 TTY.println(String.format("@%-6d Graal %-70s %-45s %-50s ...", id, method.getDeclaringClass().getName(), method.getName(), method.getSignature()));
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java	Mon Mar 17 12:40:35 2014 -0700
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java	Mon Mar 17 23:45:14 2014 +0100
@@ -72,6 +72,10 @@
             count++;
         }
 
+        public void add(int n) {
+            count += n;
+        }
+
         public int getCount() {
             return count;
         }
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java	Mon Mar 17 12:40:35 2014 -0700
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugHistogramAsciiPrinter.java	Mon Mar 17 23:45:14 2014 +0100
@@ -60,9 +60,12 @@
     }
 
     public void print(DebugHistogram histogram) {
-        List<CountedValue> list = histogram.getValues();
+        print(histogram.getValues(), histogram.getName());
+    }
+
+    public void print(List<CountedValue> list, String name) {
         if (list.isEmpty()) {
-            os.printf("%s is empty.%n", histogram.getName());
+            os.printf("%s is empty.%n", name);
             return;
         }
 
@@ -73,7 +76,7 @@
         }
 
         // Print header.
-        os.printf("%s has %d unique elements and %d total elements:%n", histogram.getName(), list.size(), total);
+        os.printf("%s has %d unique elements and %d total elements:%n", name, list.size(), total);
 
         int max = list.get(0).getCount();
         final int lineSize = nameSize + NumberSize + barSize + 10;
--- a/mx/projects	Mon Mar 17 12:40:35 2014 -0700
+++ b/mx/projects	Mon Mar 17 23:45:14 2014 +0100
@@ -38,6 +38,11 @@
 library@OKRA_WITH_SIM@sourcePath=lib/okra-1.8-with-sim-src.jar
 library@OKRA_WITH_SIM@sourceUrls=http://cr.openjdk.java.net/~tdeneau/okra-1.8-with-sim-src.jar
 
+library@ALLOCATION_INSTRUMENTATION@path=lib/allocation.jar
+library@ALLOCATION_INSTRUMENTATION@urls=http://java-allocation-instrumenter.googlecode.com/files/allocation.jar
+library@ALLOCATION_INSTRUMENTATION@sourcePath=lib/allocation-src.jar
+library@ALLOCATION_INSTRUMENTATION@sourceUrls=https://java-allocation-instrumenter.googlecode.com/files/java-allocation-instrumenter-src.jar
+
 distribution@GRAAL@path=graal.jar
 distribution@GRAAL@dependencies=\
 com.oracle.graal.hotspot.amd64,\
@@ -519,7 +524,7 @@
 # graal.compiler.test
 project@com.oracle.graal.compiler.test@subDir=graal
 project@com.oracle.graal.compiler.test@sourceDirs=src
-project@com.oracle.graal.compiler.test@dependencies=com.oracle.graal.test,com.oracle.graal.printer,com.oracle.graal.runtime,com.oracle.graal.baseline
+project@com.oracle.graal.compiler.test@dependencies=com.oracle.graal.test,com.oracle.graal.printer,com.oracle.graal.runtime,com.oracle.graal.baseline,ALLOCATION_INSTRUMENTATION
 project@com.oracle.graal.compiler.test@checkstyle=com.oracle.graal.graph
 project@com.oracle.graal.compiler.test@javaCompliance=1.7
 project@com.oracle.graal.compiler.test@workingSets=Graal,Test