Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/profile/GlobalMetrics.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.profile; | |
24 | |
25 import java.io.*; | |
26 import java.util.*; | |
27 import java.util.Arrays; | |
28 | |
29 import com.sun.max.*; | |
30 import com.sun.max.profile.Metrics.*; | |
31 import com.sun.max.util.timer.*; | |
32 | |
33 public class GlobalMetrics { | |
34 | |
35 static class EntryComparator implements Comparator<Map.Entry<String, Metric>> { | |
36 public int compare(Map.Entry<String, Metric> a, Map.Entry<String, Metric> b) { | |
37 return String.CASE_INSENSITIVE_ORDER.compare(a.getKey(), b.getKey()); | |
38 } | |
39 } | |
40 | |
41 static class MetricSet<T extends Metric> { | |
42 private final Class<T> clazz; | |
43 private final Map<String, T> metrics = new HashMap<String, T>(); | |
44 | |
45 MetricSet(Class<T> mClass) { | |
46 clazz = mClass; | |
47 } | |
48 } | |
49 | |
50 protected static final Map<Class<? extends Metric>, MetricSet> metricSets = new HashMap<Class<? extends Metric>, MetricSet>(); | |
51 | |
52 /** | |
53 * This method allocates a new counter with the specified name and adds it to the global | |
54 * metric list. If a previous metric with the same name exists, it will return a reference | |
55 * to the first one created. | |
56 * @param name the name of the metric for which to create a counter | |
57 * @return a reference to a code {@code Counter} object which can be incremented and accumulated | |
58 */ | |
59 public static Metrics.Counter newCounter(String name) { | |
60 if (name == null) { | |
61 return new Metrics.Counter(); | |
62 } | |
63 return getCounter(name); | |
64 } | |
65 | |
66 public static TimerMetric newTimer(String name, Clock clock) { | |
67 if (name == null) { | |
68 return new TimerMetric(new MultiThreadTimer(clock)); | |
69 } | |
70 return getTimer(name, clock); | |
71 } | |
72 | |
73 public static Metrics.Rate newRate(String name, Metrics.Counter count, Clock clock) { | |
74 if (name == null) { | |
75 return new Rate(count, clock); | |
76 } | |
77 return getRate(name, count, clock); | |
78 } | |
79 | |
80 static synchronized Metrics.Counter getCounter(String name) { | |
81 Metrics.Counter counter = getMetric(name, Metrics.Counter.class); | |
82 if (counter == null) { | |
83 counter = setMetric(name, Metrics.Counter.class, new Metrics.Counter()); | |
84 } | |
85 return counter; | |
86 } | |
87 | |
88 static synchronized TimerMetric getTimer(String name, Clock clock) { | |
89 TimerMetric timer = getMetric(name, TimerMetric.class); | |
90 if (timer == null) { | |
91 timer = setMetric(name, TimerMetric.class, new TimerMetric(new MultiThreadTimer(clock))); | |
92 } | |
93 return timer; | |
94 } | |
95 | |
96 static synchronized Metrics.Rate getRate(String name, Metrics.Counter count, Clock clock) { | |
97 Metrics.Rate rate = getMetric(name, Metrics.Rate.class); | |
98 if (rate == null) { | |
99 rate = setMetric(name, Metrics.Rate.class, new Metrics.Rate(count, clock)); | |
100 } | |
101 return rate; | |
102 } | |
103 | |
104 public static <T extends Metric> T getMetric(String name, Class<T> mClass) { | |
105 final MetricSet<T> metricSet = Utils.cast(metricSets.get(mClass)); | |
106 if (metricSet != null) { | |
107 final T metric = metricSet.metrics.get(name); | |
108 if (metric != null) { | |
109 return metric; | |
110 } | |
111 } | |
112 return null; | |
113 } | |
114 | |
115 public static <T extends Metric> T setMetric(String name, Class<T> mClass, T metric) { | |
116 MetricSet<T> metricSet = Utils.cast(metricSets.get(mClass)); | |
117 if (metricSet == null) { | |
118 metricSet = new MetricSet<T>(mClass); | |
119 metricSets.put(mClass, metricSet); | |
120 } | |
121 metricSet.metrics.put(name, metric); | |
122 return metric; | |
123 } | |
124 | |
125 /** | |
126 * Resets of all the currently registered metrics. | |
127 */ | |
128 public static synchronized void reset() { | |
129 for (MetricSet<? extends Metric> metricSet : metricSets.values()) { | |
130 for (Metric metric : metricSet.metrics.values()) { | |
131 metric.reset(); | |
132 } | |
133 } | |
134 } | |
135 | |
136 /** | |
137 * This method prints a report of all the metrics that have been created during this | |
138 * execution run. | |
139 * @param stream the print stream to which to print the report | |
140 */ | |
141 public static synchronized void report(PrintStream stream) { | |
142 final Map<String, Metric> allMetrics = new HashMap<String, Metric>(); | |
143 for (MetricSet<? extends Metric> metricSet : metricSets.values()) { | |
144 allMetrics.putAll(metricSet.metrics); | |
145 } | |
146 | |
147 Map.Entry<String, Metric>[] array = Utils.cast(new Map.Entry[allMetrics.size()]); | |
148 array = allMetrics.entrySet().toArray(array); | |
149 Arrays.sort(array, new GlobalMetrics.EntryComparator()); | |
150 for (Map.Entry<String, Metric> entry : array) { | |
151 if (entry.getKey().length() > Metrics.longestMetricName) { | |
152 Metrics.longestMetricName = entry.getKey().length(); | |
153 } | |
154 } | |
155 for (Map.Entry<String, Metric> entry : array) { | |
156 entry.getValue().report(entry.getKey(), stream); | |
157 } | |
158 stream.flush(); | |
159 } | |
160 | |
161 } |