Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jstatd/JstatGCUtilParser.java
38838 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Arrays;2425import jdk.testlibrary.Utils;26import static jdk.testlibrary.Asserts.*;2728/**29* The helper class for parsing following output from command 'jstat -gcutil':30*31* S0 S1 E O M CCS YGC YGCT FGC FGCT GCT32* 100.00 0.00 64.68 13.17 73.39 33.46 2 0.003 1 0.156 0.15833* 100.00 0.00 76.54 13.17 73.39 33.46 2 0.003 1 0.156 0.15834* 100.00 0.00 83.49 13.17 73.39 33.46 2 0.003 1 0.156 0.15835* 100.00 0.00 84.53 13.17 73.39 33.46 2 0.003 1 0.156 0.15836* 100.00 0.00 85.57 13.17 73.39 33.46 2 0.003 1 0.156 0.15837*38* It will be verified that numerical values have defined types and are reasonable,39* for example percentage should fit within 0-100 interval.40*/41public class JstatGCUtilParser {4243public enum GcStatisticsType {44INTEGER, DOUBLE, PERCENTAGE, PERCENTAGE_OR_DASH;45}4647public enum GcStatistics {48S0(GcStatisticsType.PERCENTAGE),49S1(GcStatisticsType.PERCENTAGE),50E(GcStatisticsType.PERCENTAGE),51O(GcStatisticsType.PERCENTAGE),52M(GcStatisticsType.PERCENTAGE),53CCS(GcStatisticsType.PERCENTAGE_OR_DASH),54YGC(GcStatisticsType.INTEGER),55YGCT(GcStatisticsType.DOUBLE),56FGC(GcStatisticsType.INTEGER),57FGCT(GcStatisticsType.DOUBLE),58GCT(GcStatisticsType.DOUBLE);5960private final GcStatisticsType type;6162private GcStatistics(GcStatisticsType type) {63this.type = type;64}6566private GcStatisticsType getType() {67return type;68}6970public static boolean isHeadline(String... valueArray) {71if (valueArray.length != values().length) {72return false;73}74int headersCount = 0;75for (int i = 0; i < values().length; i++) {76if (valueArray[i].equals(values()[i].toString())) {77headersCount++;78}79}80if (headersCount != values().length) {81return false;82}83return true;84}8586private static void verifyLength(String... valueArray) throws Exception {87assertEquals(valueArray.length, values().length,88"Invalid number of data columns: " + Arrays.toString(valueArray));89}9091public static void verify(String... valueArray) throws Exception {92verifyLength(valueArray);93for (int i = 0; i < values().length; i++) {94GcStatisticsType type = values()[i].getType();95String value = valueArray[i].trim();96if (type.equals(GcStatisticsType.INTEGER)) {97Integer.parseInt(value);98break;99}100if (type.equals(GcStatisticsType.DOUBLE)) {101Double.parseDouble(value);102break;103}104if (type.equals(GcStatisticsType.PERCENTAGE_OR_DASH) &&105value.equals("-")) {106break;107}108double percentage = Double.parseDouble(value);109assertTrue(0 <= percentage && percentage <= 100,110"Not a percentage: " + value);111}112}113114}115116private final String output;117118public JstatGCUtilParser(String output) {119this.output = output;120}121122public String getOutput() {123return output;124}125126/**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 reason129* before running jstat.130*/131public void parse(int samples) throws Exception {132boolean headlineFound = false;133int datalineCount = 0;134135String[] lines = output.split(Utils.NEW_LINE);136for (String line : lines) {137line = line.replaceAll("\\s+", " ").trim();138String[] valueArray = line.split(" ");139140if (!headlineFound) {141headlineFound = GcStatistics.isHeadline(valueArray);142continue;143}144145GcStatistics.verify(valueArray);146datalineCount++;147}148149assertTrue(headlineFound, "No or invalid headline found, expected: " +150Utils.NEW_LINE + Arrays.toString(GcStatistics.values()).replaceAll(",", " "));151assertEquals(samples, datalineCount,152"Expected " + samples + " samples, got " + datalineCount);153}154155}156157158