1 /* 2 * Copyright (c) 2013, 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 GCT(GcStatisticsType.DOUBLE); 60 61 private final GcStatisticsType type; 62 63 private GcStatistics(GcStatisticsType type) { 64 this.type = type; 65 } 66 67 private GcStatisticsType getType() { 68 return type; 69 } 70 71 public static boolean isHeadline(String... valueArray) { 72 if (valueArray.length != values().length) { 73 return false; 74 } 75 int headersCount = 0; 76 for (int i = 0; i < values().length; i++) { 77 if (valueArray[i].equals(values()[i].toString())) { 78 headersCount++; 79 } 80 } 81 if (headersCount != values().length) { 82 return false; 83 } 84 return true; 85 } 86 87 private static void verifyLength(String... valueArray) throws Exception { 88 assertEquals(valueArray.length, values().length, 89 "Invalid number of data columns: " + Arrays.toString(valueArray)); 90 } 91 92 public static void verify(String... valueArray) throws Exception { 93 verifyLength(valueArray); 94 for (int i = 0; i < values().length; i++) { 95 GcStatisticsType type = values()[i].getType(); 96 String value = valueArray[i].trim(); 97 if (type.equals(GcStatisticsType.INTEGER)) { 98 Integer.parseInt(value); 99 break; 100 } 101 if (type.equals(GcStatisticsType.DOUBLE)) { 102 Double.parseDouble(value); 103 break; 104 } 105 if (type.equals(GcStatisticsType.PERCENTAGE_OR_DASH) && 106 value.equals("-")) { 107 break; 108 } 109 double percentage = Double.parseDouble(value); 110 assertTrue(0 <= percentage && percentage <= 100, 111 "Not a percentage: " + value); 112 } 113 } 114 115 } 116 117 private final String output; 118 119 public JstatGCUtilParser(String output) { 120 this.output = output; 121 } 122 123 public String getOutput() { 124 return output; 125 } 126 127 /** 128 * The function will discard any lines that come before the header line. 129 * This can happen if the JVM outputs a warning message for some reason 130 * before running jstat. 131 */ 132 public void parse(int samples) throws Exception { 133 boolean headlineFound = false; 134 int datalineCount = 0; 135 136 String[] lines = output.split(Utils.NEW_LINE); 137 for (String line : lines) { 138 line = line.replaceAll("\\s+", " ").trim(); 139 String[] valueArray = line.split(" "); 140 141 if (!headlineFound) { 142 headlineFound = GcStatistics.isHeadline(valueArray); 143 continue; 144 } 145 146 GcStatistics.verify(valueArray); 147 datalineCount++; 148 } 149 150 assertTrue(headlineFound, "No or invalid headline found, expected: " + 151 Utils.NEW_LINE + Arrays.toString(GcStatistics.values()).replaceAll(",", " ")); 152 assertEquals(samples, datalineCount, 153 "Expected " + samples + " samples, got " + datalineCount); 154 } 155 156 }