/* * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.share.gc; import nsk.share.*; /** * Parser for GC tests' arguments. *

* ArgumentHandler handles specific command line arguments * related to way of execution of a test in addition to general arguments * recognized by {@link ArgumentParser ArgumentParser}. *

* Following is the list of specific options for ArgumentHandler: *

* @see ArgumentParser */ public class ArgumentHandler extends ArgumentParser { // Define all possible arguments private final static String ITERATIONS = "iterations"; private final static String AGGREGATION_DEPTH = "aggregationDepth"; private final static String GC_TIMEOUT = "gcTimeout"; private final static String THREADS = "threads"; private final static String MEM_EATER = "memoryEater"; private final static String LARGE_CLASSES_PATH = "largeClassesPath"; private final static String FIELDS_LIMITATION = "fieldsLimitation"; // An acceptible value for ITERATIONS private final static String INFINITY = "infinity"; // Acceptible values for MEM_EATER private final static String ME_SINGLE = "single"; private final static String ME_MULTI = "multi"; // Acceptible values for FIELDS_LIMITATION private final static String FL_OVER = "over"; private final static String FL_UNDER = "under"; /** * Keep a copy of raw command-line arguments and parse them; * but throw an exception on parsing error. * * @param args Array of the raw command-line arguments. * * @throws BadOption If unknown option or illegal option value found * * @see ArgumentParser */ public ArgumentHandler(String args[]) { super(args); } /** * Returns number of iterations. *

* If -iterations="infinity", the method returns -1. * If the argument is not set, the method returns 1. Otherwise, the * specified number is returned. * * @return number of iterations. * */ public int getIterations() { String value = options.getProperty(ITERATIONS, "1"); if (INFINITY.equals(value)) return -1; try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw new TestBug("Not an integer value of \"" + ITERATIONS + "\" argument: " + value); } } /** * Returns the depth of object aggregation. *

* If the argument is not set, the method returns 0. Otherwise, the * specified number is returned. * * @return number of aggregation depth. * */ public int getAggregationDepth() { String value = options.getProperty(AGGREGATION_DEPTH, "0"); try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw new TestBug("Not an integer value of \"" + AGGREGATION_DEPTH + "\" argument: " + value); } } /** * Returns number of minutes to run the test. *

* @return number of minutes to run the test. * */ public int getGCTimeout() { String value = options.getProperty(GC_TIMEOUT); try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw new TestBug("\"" + GC_TIMEOUT + "\" argument is not defined " + "or is not integer: " + value); } } /** * Returns a directory to load large classes from. *

* @return a directory to load large classes from. * */ public String getLargeClassesPath() { return options.getProperty(LARGE_CLASSES_PATH); } /** * Returns number of threads to start in a test. If threads * is not set, the method returns specified number of threads. *

* @param defaultValue default value, if threads is not set. * @return number of threads to start in a test. * */ public int getThreads(int defaultValue) { String value = options.getProperty(THREADS); if (value == null) return defaultValue; try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw new TestBug("Not an integer value of \"" + THREADS + "\" argument: " + value); } } /** * Returns true if single thread should be used to eat the whole heap, * false otherwise. * * @return true if single thread should be used to eat the whole heap, * false otherwise. * */ public boolean isSingleMemoryEater() { String value = options.getProperty(MEM_EATER); if (value == null) return true; else if (value.equals(ME_SINGLE)) return true; else if (value.equals(ME_MULTI)) return false; else throw new TestBug("Value for \"" + MEM_EATER + "\" must be either " + ME_SINGLE + ", or " + ME_MULTI); } /** * Returns true if classes with number of fileds over limitation should be * loaded, false otherwise. * * @return true if classes with number of fileds over limitation should be * loaded, false otherwise. * */ public boolean isOverFieldsLimitation() { String value = options.getProperty(FIELDS_LIMITATION); if (value == null) return false; else if (value.equals(FL_OVER)) return true; else if (value.equals(FL_UNDER)) return false; else throw new TestBug("Value for \"" + FIELDS_LIMITATION + "\" must be " + "either " + FL_OVER + ", or " + FL_UNDER); } /** * Checks if an option is allowed and has proper value. * This method is invoked by parseArguments() * * @param option option name * @param value string representation of value * (could be an empty string too) * null if this option has no value * @return true if option is allowed and has proper value, * false if option is not admissible * * @throws BadOption if option has an illegal value * * @see #parseArguments() */ protected boolean checkOption(String option, String value) { // Define iterations if (option.equals(ITERATIONS)) { if (INFINITY.equals(value)) return true; try { int number = Integer.parseInt(value); if (number < 1) throw new BadOption(option + ": value must be greater than " + "zero."); } catch (NumberFormatException e) { throw new BadOption("Value for option \"" + option + "\" must " + "be integer or \"" + INFINITY + "\": " + value); } return true; } // Define timeout if (option.equals(GC_TIMEOUT)) { try { int number = Integer.parseInt(value); if (number < 0) throw new BadOption(option + ": value must be a positive " + "integer"); } catch (NumberFormatException e) { throw new BadOption("Value for option \"" + option + "\" must " + "be integer: " + value); } return true; } // Define threads if (option.equals(THREADS)) { try { int number = Integer.parseInt(value); if (number < 0) throw new BadOption(option + ": value must be a positive " + "integer"); } catch (NumberFormatException e) { throw new BadOption("Value for option \"" + option + "\" must " + "be integer: " + value); } return true; } // Define path to large classes if (option.equals(LARGE_CLASSES_PATH)) return true; // Define memory eater if (option.equals(MEM_EATER)) { if ( (ME_SINGLE.equals(value)) || (ME_MULTI.equals(value)) ) return true; else throw new BadOption("Value for option \"" + option + "\" must " + "be either " + ME_SINGLE + ", or " + ME_MULTI + ": " + value); } // Define fields limitation if (option.equals(FIELDS_LIMITATION)) { if ( (FL_OVER.equals(value)) || (FL_UNDER.equals(value)) ) return true; else throw new BadOption("Value for option \"" + option + "\" must " + "be either " + FL_OVER + ", or " + FL_UNDER + ": " + value); } // Define aggregationDepth if (option.equals(AGGREGATION_DEPTH)) { try { int number = Integer.parseInt(value); if (number < 0) throw new BadOption(option + ": value must be a positive " + "integer"); } catch (NumberFormatException e) { throw new BadOption("Value for option \"" + option + "\" must " + "be integer: " + value); } return true; } return super.checkOption(option, value); } /** * Checks if the values of all options are consistent. * This method is invoked by parseArguments() * * @throws BadOption if options have inconsistent values * * @see ArgumentParser#parseArguments() */ protected void checkOptions() { super.checkOptions(); } } // ArgumentHandler