annotate graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugHistogram.java @ 14586:d2fe05d5cc96

added support for lazy computation of names for use with Debug
author Doug Simon <doug.simon@oracle.com>
date Tue, 18 Mar 2014 17:15:06 +0100
parents e3888db8b8a1
children cda2a7d1dcff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
1 /*
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
4 *
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
8 *
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
13 * accompanied this code).
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
14 *
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
18 *
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
21 * questions.
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
22 */
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
23 package com.oracle.graal.debug;
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
24
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
25 import java.util.*;
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
26
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
27 /**
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
28 * Facility for recording value frequencies.
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
29 */
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
30 public interface DebugHistogram {
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
31
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
32 /**
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
33 * Gets the name specified when this objected was {@linkplain Debug#createHistogram(String)
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
34 * created}.
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
35 */
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
36 String getName();
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
37
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
38 /**
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
39 * Increments the count for a given value.
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
40 */
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
41 void add(Object value);
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
42
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
43 /**
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
44 * A value and a frequency. The ordering imposed by {@link #compareTo(CountedValue)} places
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
45 * values with higher frequencies first.
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
46 */
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
47 public class CountedValue implements Comparable<CountedValue> {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
48
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
49 private int count;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
50 private final Object value;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
51
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
52 public CountedValue(int count, Object value) {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
53 this.count = count;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
54 this.value = value;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
55 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
56
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
57 public int compareTo(CountedValue o) {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
58 if (count < o.count) {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
59 return 1;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
60 } else if (count > o.count) {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
61 return -1;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
62 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
63 return 0;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
64 }
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
65
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
66 @Override
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
67 public String toString() {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
68 return count + " -> " + value;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
69 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
70
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
71 public void inc() {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
72 count++;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
73 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
74
14586
d2fe05d5cc96 added support for lazy computation of names for use with Debug
Doug Simon <doug.simon@oracle.com>
parents: 14573
diff changeset
75 public void add(int n) {
d2fe05d5cc96 added support for lazy computation of names for use with Debug
Doug Simon <doug.simon@oracle.com>
parents: 14573
diff changeset
76 count += n;
d2fe05d5cc96 added support for lazy computation of names for use with Debug
Doug Simon <doug.simon@oracle.com>
parents: 14573
diff changeset
77 }
d2fe05d5cc96 added support for lazy computation of names for use with Debug
Doug Simon <doug.simon@oracle.com>
parents: 14573
diff changeset
78
11551
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
79 public int getCount() {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
80 return count;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
81 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
82
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
83 public Object getValue() {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
84 return value;
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
85 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
86 }
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
87
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
88 /**
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
89 * Gets a list of the counted values, sorted in descending order of frequency.
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
90 */
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
91 List<CountedValue> getValues();
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
92
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
93 /**
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
94 * Interface for a service that can render a visualization of a histogram.
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
95 */
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
96 public interface Printer {
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
97
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
98 void print(DebugHistogram histogram);
63b4694d3627 split DebugHistogram printing out into separate service
Doug Simon <doug.simon@oracle.com>
parents: 9103
diff changeset
99 }
9103
e7541d478e38 Added DebugHistory utility and corresponding unit tests.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
diff changeset
100 }