diff graal/com.oracle.graal.options/src/com/oracle/graal/options/OptionValue.java @ 12682:5dbfb4d5eaba

added a histogram to OptionValue (enabled by -Dgraal.showOptionValueReadsHistogram=true) to see which options are most frequently read
author Doug Simon <doug.simon@oracle.com>
date Tue, 05 Nov 2013 19:02:50 +0100
parents ecd519b39f10
children bb85b81258a0
line wrap: on
line diff
--- a/graal/com.oracle.graal.options/src/com/oracle/graal/options/OptionValue.java	Tue Nov 05 19:02:14 2013 +0100
+++ b/graal/com.oracle.graal.options/src/com/oracle/graal/options/OptionValue.java	Tue Nov 05 19:02:50 2013 +0100
@@ -22,6 +22,7 @@
  */
 package com.oracle.graal.options;
 
+import java.io.*;
 import java.util.*;
 import java.util.Map.Entry;
 
@@ -136,8 +137,24 @@
 
     private OptionDescriptor descriptor;
 
+    private long reads;
+    private OptionValue<?> next;
+    private static OptionValue head;
+
+    private static final boolean ShowReadsHistogram = Boolean.getBoolean("graal.showOptionValueReadsHistogram");
+
+    private static void addToHistogram(OptionValue<?> option) {
+        if (ShowReadsHistogram) {
+            synchronized (OptionValue.class) {
+                option.next = head;
+                head = option;
+            }
+        }
+    }
+
     public OptionValue(T value) {
         this.value = value;
+        addToHistogram(this);
     }
 
     private static final Object UNINITIALIZED = "UNINITIALIZED";
@@ -149,6 +166,7 @@
     @SuppressWarnings("unchecked")
     protected OptionValue() {
         this.value = (T) UNINITIALIZED;
+        addToHistogram(this);
     }
 
     /**
@@ -167,10 +185,11 @@
 
     /**
      * Gets the name of this option. The name for an option value with a null
-     * {@linkplain #setDescriptor(OptionDescriptor) descriptor} is {@code "<anonymous>"}.
+     * {@linkplain #setDescriptor(OptionDescriptor) descriptor} is the value of
+     * {@link Object#toString()}.
      */
     public String getName() {
-        return descriptor == null ? "<anonymous>" : (descriptor.getDeclaringClass().getName() + "." + descriptor.getName());
+        return descriptor == null ? super.toString() : (descriptor.getDeclaringClass().getName() + "." + descriptor.getName());
     }
 
     @Override
@@ -182,6 +201,9 @@
      * Gets the value of this option.
      */
     public T getValue() {
+        if (ShowReadsHistogram) {
+            reads++;
+        }
         if (!(this instanceof StableOptionValue)) {
             OverrideScope overrideScope = overrideScopes.get();
             if (overrideScope != null) {
@@ -317,4 +339,35 @@
             }
         }
     }
+
+    static {
+        if (ShowReadsHistogram) {
+            Runtime.getRuntime().addShutdownHook(new Thread() {
+                @Override
+                public void run() {
+                    ArrayList<OptionValue<?>> options = new ArrayList<>();
+                    for (OptionValue<?> option = head; option != null; option = option.next) {
+                        options.add(option);
+                    }
+                    Collections.sort(options, new Comparator<OptionValue<?>>() {
+
+                        public int compare(OptionValue<?> o1, OptionValue<?> o2) {
+                            if (o1.reads < o2.reads) {
+                                return -1;
+                            } else if (o1.reads > o2.reads) {
+                                return 1;
+                            } else {
+                                return o1.getName().compareTo(o2.getName());
+                            }
+                        }
+                    });
+                    PrintStream out = System.out;
+                    out.println("=== OptionValue reads histogram ===");
+                    for (OptionValue<?> option : options) {
+                        out.println(option.reads + "\t" + option);
+                    }
+                }
+            });
+        }
+    }
 }