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