comparison truffle/com.oracle.truffle.object/src/com/oracle/truffle/object/DebugCounter.java @ 22291:c3ea80aa5762

make debug counters optional
author Andreas Woess <andreas.woess@oracle.com>
date Thu, 08 Oct 2015 14:21:33 +0200
parents dc83cc1f94f2
children
comparison
equal deleted inserted replaced
22290:ca426f243321 22291:c3ea80aa5762
24 24
25 import java.io.PrintStream; 25 import java.io.PrintStream;
26 import java.util.ArrayList; 26 import java.util.ArrayList;
27 import java.util.concurrent.atomic.AtomicLong; 27 import java.util.concurrent.atomic.AtomicLong;
28 28
29 public final class DebugCounter { 29 public abstract class DebugCounter {
30 private static final ArrayList<DebugCounter> allCounters = new ArrayList<>(); 30 private DebugCounter() {
31
32 private final String name;
33 private final AtomicLong value;
34
35 private DebugCounter(String name) {
36 this.name = name;
37 this.value = new AtomicLong();
38 allCounters.add(this);
39 } 31 }
40 32
33 public abstract long get();
34
35 public abstract void inc();
36
41 public static DebugCounter create(String name) { 37 public static DebugCounter create(String name) {
42 return new DebugCounter(name); 38 return ObjectStorageOptions.DebugCounters ? DebugCounterImpl.createImpl(name) : Dummy.INSTANCE;
43 }
44
45 public long get() {
46 return value.get();
47 }
48
49 public void inc() {
50 value.incrementAndGet();
51 }
52
53 @Override
54 public String toString() {
55 return name + ": " + value;
56 } 39 }
57 40
58 public static void dumpCounters() { 41 public static void dumpCounters() {
59 dumpCounters(System.out); 42 if (ObjectStorageOptions.DebugCounters) {
43 DebugCounterImpl.dumpCounters(System.out);
44 }
60 } 45 }
61 46
62 public static void dumpCounters(PrintStream out) { 47 private static final class DebugCounterImpl extends DebugCounter {
63 for (DebugCounter counter : allCounters) { 48 private static final ArrayList<DebugCounter> allCounters = new ArrayList<>();
64 out.println(counter); 49
50 private final String name;
51 private final AtomicLong value;
52
53 private DebugCounterImpl(String name) {
54 this.name = name;
55 this.value = new AtomicLong();
56 allCounters.add(this);
57 }
58
59 private static DebugCounter createImpl(String name) {
60 return new DebugCounterImpl(name);
61 }
62
63 @Override
64 public long get() {
65 return value.get();
66 }
67
68 @Override
69 public void inc() {
70 value.incrementAndGet();
71 }
72
73 @Override
74 public String toString() {
75 return name + ": " + get();
76 }
77
78 private static void dumpCounters(PrintStream out) {
79 for (DebugCounter counter : allCounters) {
80 out.println(counter);
81 }
82 }
83 }
84
85 private static final class Dummy extends DebugCounter {
86 static final DebugCounter INSTANCE = new Dummy();
87
88 private Dummy() {
89 }
90
91 @Override
92 public long get() {
93 return 0;
94 }
95
96 @Override
97 public void inc() {
65 } 98 }
66 } 99 }
67 } 100 }