Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/tools/jstatd/JstatGCUtilParser.java
41149 views
1
/*
2
* Copyright (c) 2013, 2020, 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
24
import java.util.Arrays;
25
import java.text.NumberFormat;
26
27
import static jdk.test.lib.Asserts.*;
28
import jdk.test.lib.Utils;
29
30
/**
31
* The helper class for parsing following output from command 'jstat -gcutil':
32
*
33
* S0 S1 E O M CCS YGC YGCT FGC FGCT CGC CGCT GCT
34
* 0.00 0.00 0.00 52.39 97.76 92.71 4 0.286 28 28.006 2 0.086 28.378
35
*
36
* It will be verified that numerical values have defined types and are reasonable,
37
* for example percentage should fit within 0-100 interval.
38
*/
39
public class JstatGCUtilParser {
40
41
public enum GcStatisticsType {
42
INTEGER, DOUBLE, PERCENTAGE, PERCENTAGE_OR_DASH;
43
}
44
45
public enum GcStatistics {
46
S0(GcStatisticsType.PERCENTAGE),
47
S1(GcStatisticsType.PERCENTAGE),
48
E(GcStatisticsType.PERCENTAGE),
49
O(GcStatisticsType.PERCENTAGE),
50
M(GcStatisticsType.PERCENTAGE_OR_DASH),
51
CCS(GcStatisticsType.PERCENTAGE_OR_DASH),
52
YGC(GcStatisticsType.INTEGER),
53
YGCT(GcStatisticsType.DOUBLE),
54
FGC(GcStatisticsType.INTEGER),
55
FGCT(GcStatisticsType.DOUBLE),
56
CGC(GcStatisticsType.INTEGER),
57
CGCT(GcStatisticsType.DOUBLE),
58
GCT(GcStatisticsType.DOUBLE);
59
60
private final GcStatisticsType type;
61
62
private GcStatistics(GcStatisticsType type) {
63
this.type = type;
64
}
65
66
private GcStatisticsType getType() {
67
return type;
68
}
69
70
public static boolean isHeadline(String... valueArray) {
71
if (valueArray.length != values().length) {
72
return false;
73
}
74
int headersCount = 0;
75
for (int i = 0; i < values().length; i++) {
76
if (valueArray[i].equals(values()[i].toString())) {
77
headersCount++;
78
}
79
}
80
if (headersCount != values().length) {
81
return false;
82
}
83
return true;
84
}
85
86
private static void verifyLength(String... valueArray) throws Exception {
87
assertEquals(valueArray.length, values().length,
88
"Invalid number of data columns: " + Arrays.toString(valueArray));
89
}
90
91
public static void verify(String... valueArray) throws Exception {
92
verifyLength(valueArray);
93
for (int i = 0; i < values().length; i++) {
94
GcStatisticsType type = values()[i].getType();
95
String value = valueArray[i].trim();
96
if (type.equals(GcStatisticsType.INTEGER)) {
97
NumberFormat.getInstance().parse(value).intValue();
98
break;
99
}
100
if (type.equals(GcStatisticsType.DOUBLE)) {
101
NumberFormat.getInstance().parse(value).doubleValue();
102
break;
103
}
104
if (type.equals(GcStatisticsType.PERCENTAGE_OR_DASH) &&
105
value.equals("-")) {
106
break;
107
}
108
double percentage = NumberFormat.getInstance().parse(value).doubleValue();
109
assertTrue(0 <= percentage && percentage <= 100,
110
"Not a percentage: " + value);
111
}
112
}
113
114
}
115
116
private final String output;
117
118
public JstatGCUtilParser(String output) {
119
this.output = output;
120
}
121
122
public String getOutput() {
123
return output;
124
}
125
126
/**
127
* The function will discard any lines that come before the header line.
128
* This can happen if the JVM outputs a warning message for some reason
129
* before running jstat.
130
*/
131
public void parse(int samples) throws Exception {
132
boolean headlineFound = false;
133
int datalineCount = 0;
134
135
String[] lines = output.split(Utils.NEW_LINE);
136
for (String line : lines) {
137
line = line.replaceAll("\\s+", " ").trim();
138
String[] valueArray = line.split(" ");
139
140
if (!headlineFound) {
141
headlineFound = GcStatistics.isHeadline(valueArray);
142
continue;
143
}
144
145
GcStatistics.verify(valueArray);
146
datalineCount++;
147
}
148
149
assertTrue(headlineFound, "No or invalid headline found, expected: " +
150
Utils.NEW_LINE + Arrays.toString(GcStatistics.values()).replaceAll(",", " "));
151
assertEquals(samples, datalineCount,
152
"Expected " + samples + " samples, got " + datalineCount);
153
}
154
155
}
156
157