Debian Codegraph @

src/com/google/javascript/jscomp/JvmMetrics.java

raw symbols SCIP
1 /*
2 * Copyright 2012 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import java.io.PrintStream;
20 import java.lang.management.CompilationMXBean;
21 import java.lang.management.GarbageCollectorMXBean;
22 import java.lang.management.ManagementFactory;
23 import java.lang.management.MemoryMXBean;
24 import java.lang.management.MemoryPoolMXBean;
25 import java.lang.management.MemoryUsage;
26 import java.util.List;
27 import java.util.StringTokenizer;
28
29 /**
30 * A class to report jvm/jmx statistics.
31 * Borrowed from:
32 * http://code.google.com/p/dart/source/browse/trunk/dart/compiler/java/com/google/dart/compiler/metrics/JvmMetrics.java
33 */
34 class JvmMetrics {
35
36 private static int TABULAR_COLON_POS = 40;
37 private static long ONE_KILO_BYTE = 1L << 10L;
38 private static long ONE_MEGA_BYTE = 1L << 20L;
39 private static long ONE_GIGA_BYTE = 1L << 30L;
40
41 public static void maybeWriteJvmMetrics(PrintStream out, String options) {
42 if (options == null) {
43 return;
44 }
45
46 boolean verboseMode = false;
47 boolean prettyMode = false;
48 StringTokenizer st = new StringTokenizer(options, ":");
49 // options are grouped in order 'detail:format:types'
50 if (st.hasMoreTokens()) {
51 String mode = st.nextToken();
52 if (mode.equalsIgnoreCase("verbose")) {
53 verboseMode = true;
54 }
55 }
56
57 if (st.hasMoreTokens()) {
58 String mode = st.nextToken();
59 if (mode.equalsIgnoreCase("pretty")) {
60 prettyMode = true;
61 }
62 }
63
64 if (st.hasMoreTokens()) {
65 while (st.hasMoreTokens()) {
66 String types = st.nextToken();
67 StringTokenizer typeSt = new StringTokenizer(types, ",");
68 while (typeSt.hasMoreElements()) {
69 String type = typeSt.nextToken();
70 writeMetrics(out, type, verboseMode, prettyMode);
71 }
72 }
73 } else {
74 // the default
75 writeMetrics(out, "all", verboseMode, prettyMode);
76 }
77 }
78
79 private static void writeMetrics(
80 PrintStream out, String type, boolean verbose, boolean pretty) {
81
82 if (type.equals("gc") || type.equalsIgnoreCase("all")) {
83 writeGarbageCollectionStats(out, verbose, pretty);
84 }
85 if (type.equals("mem") || type.equalsIgnoreCase("all")) {
86 writeMemoryMetrics(out, verbose, pretty);
87 }
88 if (type.equals("jit") || type.equalsIgnoreCase("all")) {
89 writeJitMetrics(out, verbose, pretty);
90 }
91 }
92
93 private static void writeJitMetrics(
94 PrintStream out, boolean verbose, boolean pretty) {
95
96 CompilationMXBean cBean = ManagementFactory.getCompilationMXBean();
97
98 String name;
99 if (verbose) {
100 name = cBean.getName();
101 } else {
102 name = "total";
103 }
104
105 if (pretty) {
106 out.println("\nJIT Stats");
107 out.println(String.format(
108 "\t%s jit time: %d ms", name, cBean.getTotalCompilationTime()));
109 } else {
110 out.println(normalizeTabularColonPos(String.format("%s-jit-time-ms : %d",
111 normalizeName(name), cBean.getTotalCompilationTime())));
112 }
113 }
114
115 private static void writeOverallMemoryUsage(
116 PrintStream out, MemoryUsage usage, String prefix, boolean pretty) {
117 if (pretty) {
118 out.format("\t%s\n", prefix);
119 out.format("\t\tavailable : %s\n", formatBytes(usage.getMax()));
120 out.format("\t\tcurrent : %s\n", formatBytes(usage.getUsed()));
121 } else {
122 prefix = normalizeName(prefix);
123 out.println(normalizeTabularColonPos(
124 String.format(prefix + "-available-bytes : %d", usage.getMax())));
125 out.println(normalizeTabularColonPos(
126 String.format(prefix + "-current-bytes : %d", usage.getUsed())));
127 }
128 }
129
130 private static void writePoolMemoryUsage(PrintStream out, MemoryUsage usage,
131 MemoryUsage peakUsage, String prefix, boolean pretty) {
132 if (pretty) {
133 out.format("\t\tavailable : %s\n",
134 formatBytes(usage.getMax()));
135 out.format("\t\tpeak : %s\n",
136 formatBytes(peakUsage.getUsed()));
137 out.format("\t\tcurrent : %s\n",
138 formatBytes(usage.getUsed()));
139 } else {
140 out.println(normalizeTabularColonPos(
141 String.format(prefix + "-available-bytes : %d", usage.getMax())));
142 out.println(normalizeTabularColonPos(
143 String.format(prefix + "-peak-bytes : %d", peakUsage.getUsed())));
144 out.println(normalizeTabularColonPos(
145 String.format(prefix + "-current-bytes : %d", usage.getUsed())));
146 }
147 }
148
149 private static void writeMemoryMetrics(
150 PrintStream out, boolean verbose, boolean pretty) {
151 if (pretty) {
152 out.println("\nMemory usage");
153 }
154
155 // only show overall stats in verbose mode
156 if (verbose) {
157 MemoryMXBean overallMemBean = ManagementFactory.getMemoryMXBean();
158 MemoryUsage usage = overallMemBean.getHeapMemoryUsage();
159 writeOverallMemoryUsage(out, usage, "Heap", pretty);
160
161 usage = overallMemBean.getNonHeapMemoryUsage();
162 writeOverallMemoryUsage(out, usage, "Non-heap", pretty);
163 }
164
165 if (verbose) {
166 List<MemoryPoolMXBean> mpBeans = ManagementFactory.getMemoryPoolMXBeans();
167 for (MemoryPoolMXBean mpBean : mpBeans) {
168 MemoryUsage currentUsage = mpBean.getUsage();
169 MemoryUsage peakUsage = mpBean.getPeakUsage();
170 if (pretty) {
171 out.println("\tPool " + mpBean.getName());
172 writePoolMemoryUsage(out, currentUsage, peakUsage, null, true);
173 } else {
174 writePoolMemoryUsage(out, currentUsage, peakUsage,
175 "mem-pool-" + normalizeName(mpBean.getName()), false);
176 }
177 }
178 } else {
179 long available = 0;
180 long current = 0;
181 long peak = 0;
182 List<MemoryPoolMXBean> mpBeans = ManagementFactory.getMemoryPoolMXBeans();
183 for (MemoryPoolMXBean mpBean : mpBeans) {
184 MemoryUsage currentUsage = mpBean.getUsage();
185 available += currentUsage.getMax();
186 current += currentUsage.getUsed();
187 MemoryUsage peakUsage = mpBean.getPeakUsage();
188 peak += peakUsage.getUsed();
189 }
190 MemoryUsage summaryUsage = new MemoryUsage(
191 0, current, current, available);
192 MemoryUsage summaryPeakUsage = new MemoryUsage(0, peak, peak, peak);
193 if (pretty) {
194 out.format("\tAggregate of %d memory pools\n", mpBeans.size());
195 writePoolMemoryUsage(out, summaryUsage, summaryPeakUsage, null, true);
196 } else {
197 writePoolMemoryUsage(out, summaryUsage, summaryPeakUsage, "mem", false);
198 }
199 }
200 }
201
202 private static void writeGarbageCollectionStats(
203 PrintStream out, boolean verbose, boolean pretty) {
204 List<GarbageCollectorMXBean> gcBeans =
205 ManagementFactory.getGarbageCollectorMXBeans();
206
207 if (verbose) {
208 if (pretty) {
209 out.println("\nGarbage collection stats");
210 for (GarbageCollectorMXBean gcBean : gcBeans) {
211 out.println("\tCollector " + gcBean.getName());
212 out.format(
213 "\t\tcollection count : %d\n", gcBean.getCollectionCount());
214 out.format(
215 "\t\tcollection time : %d ms\n", gcBean.getCollectionTime());
216 }
217 } else {
218 for (GarbageCollectorMXBean gcBean : gcBeans) {
219 String name = normalizeName(gcBean.getName());
220 out.println(normalizeTabularColonPos(String.format("gc-" + name
221 + "-collection-count : %d", gcBean.getCollectionCount())));
222 out.println(normalizeTabularColonPos(String.format("gc-" + name
223 + "-collection-time-ms : %d", gcBean.getCollectionTime())));
224 }
225 }
226 } else {
227 long collectionCount = 0;
228 long collectionTime = 0;
229 int collectorCount = gcBeans.size();
230 for (GarbageCollectorMXBean gcBean : gcBeans) {
231 collectionCount += gcBean.getCollectionCount();
232 collectionTime += gcBean.getCollectionTime();
233 }
234 if (pretty) {
235 out.println("\nGarbage collection stats");
236 out.format("\tAggregate of %d collectors\n", collectorCount);
237 out.format("\t\tcollection count : %d\n", collectionCount);
238 out.format("\t\tcollection time : %d ms\n", collectionTime);
239 } else {
240 String name = normalizeName("aggregate");
241 out.println(normalizeTabularColonPos(
242 String.format("gc-" + name + "-collection-count : %d",
243 collectionCount)));
244 out.println(normalizeTabularColonPos(
245 String.format("gc-" + name + "-collection-time-ms : %d",
246 collectionTime)));
247 }
248 }
249 }
250
251 private static String normalizeName(String name) {
252 return name.replace(" ", "_").toLowerCase();
253 }
254
255 private static String normalizeTabularColonPos(String string) {
256 StringBuilder sb = new StringBuilder(string);
257 int index = sb.indexOf(":");
258 for (; index < TABULAR_COLON_POS; ++index) {
259 sb.insert(index, ' ');
260 }
261 return sb.toString();
262 }
263
264 private static String formatBytes(long numBytes) {
265 if (numBytes < ONE_KILO_BYTE) {
266 return String.format("%d B", numBytes);
267 } else if (numBytes < ONE_MEGA_BYTE) {
268 return String.format("%d KB", numBytes / ONE_KILO_BYTE);
269 } else if (numBytes < ONE_GIGA_BYTE) {
270 return String.format("%d MB", numBytes / ONE_MEGA_BYTE);
271 } else {
272 return String.format("%d GB", numBytes / ONE_GIGA_BYTE);
273 }
274 }
275 }