Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/graph/Globals.java
40948 views
/*1* Copyright (c) 1998, 2019, 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*/2223package jit.graph;2425import jdk.test.lib.Utils;26import nsk.share.TestFailure;2728import java.io.BufferedReader;29import java.io.File;30import java.io.FileNotFoundException;31import java.io.FileReader;32import java.io.IOException;33import java.lang.reflect.InvocationTargetException;34import java.lang.reflect.Method;35import java.util.Random;36import java.util.StringTokenizer;37import java.util.Vector;3839public final class Globals {404142public static int STATIC_LOOP = 0;43public static int NUM_TEST_CLASSES = 7;44public static long RANDOM_LOOP = 100;45public static boolean VERBOSE = false;4647private static final Random indexGenerator = Utils.getRandomInstance();48private static String[] ClassArray = null;49private static Class[] ClassInstanceArray = null;50private static int maxClassIndex = 0;5152private static String[] MethodName_Array = null;53private static Method[] MethodInstance_Array = null;5455// Should be prime, so that odds of an incorrect verification reduced56public static int[] MethodID_Array = null;5758public static synchronized void initialize(String testListPath) {59File td = new File(testListPath);60if (!td.exists()) {61throw new Error("TESTBUG: File " + testListPath + " Not found");62}6364if (!td.isFile()) {65throw new Error("TESTBUG: " + testListPath + " Must be a File");66}6768BufferedReader classList = null;69try {70try {71classList = new BufferedReader(new FileReader(td));72} catch (FileNotFoundException e) {73throw new Error("TESTBUG: Error finding Classlist", e);74}7576String line = null;77try {78line = classList.readLine();79} catch (IOException e) {80throw new Error("TESTBUG: Error reading Classlist", e);81}8283try {84// ClassArray.length;85maxClassIndex = Math.abs(Integer.parseInt(line));86} catch (NumberFormatException e) {87throw new Error("TESTBUG: Error reading Classlist - first number must be number of methods defined", e);88}8990ClassArray = new String[maxClassIndex];91ClassInstanceArray = new Class[maxClassIndex];92MethodName_Array = new String[maxClassIndex];93MethodInstance_Array = new Method[maxClassIndex];94MethodID_Array = new int[maxClassIndex];9596int i;97for (i = 0; i < maxClassIndex; i++) {98try {99line = classList.readLine();100} catch (IOException e) {101throw new Error("TESTBUG: Error reading ClasslistFile: testListPath", e);102}103StringTokenizer lineTokens = new StringTokenizer(line, "\t ");104if (lineTokens.countTokens() < 3) {105throw new Error("TESTBUG: ClasslistFile: unexpected line:" + line);106} else {107ClassArray[i] = lineTokens.nextToken();108MethodName_Array[i] = lineTokens.nextToken();109MethodID_Array[i] = Integer.parseInt(lineTokens.nextToken());110}111}112maxClassIndex = i;113} finally {114if (classList != null) {115try {116classList.close();117} catch (IOException e) {118throw new Error("can't close file", e);119}120}121}122123if ((NUM_TEST_CLASSES < ClassArray.length) && (NUM_TEST_CLASSES > 0)) {124maxClassIndex = NUM_TEST_CLASSES;125} else {126NUM_TEST_CLASSES = maxClassIndex;127}128}129130// does a binary search to find the index for the ID of a method131private static int ID_BinSearch(int begin, int end, int ID) {132if (end < begin) {133return (-1);134}135136int mid = (begin + end) / 2;137int midvalue = MethodID_Array[mid];138139if (ID == midvalue) {140return (mid);141} else if (ID < midvalue) {142return (ID_BinSearch(begin, mid - 1, ID));143} else {144return (ID_BinSearch(mid + 1, end, ID));145}146}147148149// based off a static index, this function selects the method to be called150public static MethodData returnNextStaticMethod(int Method_ID) {151//int i = ID_BinSearch(0, MethodID_Array.length - 1, Method_ID);152int i = ID_BinSearch(0, maxClassIndex - 1, Method_ID);153154return (nextStaticMethod((i == -1) ? 0 : i));155}156157// this function randomly selects the next method to be called by the test class158public static MethodData nextRandomMethod() {159int i = indexGenerator.nextInt(maxClassIndex);160return (nextStaticMethod(i));161}162163private static MethodData nextStaticMethod(int i) {164Class methodsClass = null;165Method nextMethod = null;166167try {168methodsClass = ClassInstanceArray[i];169if (methodsClass == null) {170methodsClass = Class.forName(ClassArray[i]);171ClassInstanceArray[i] = methodsClass;172}173nextMethod = MethodInstance_Array[i];174if (nextMethod == null) {175nextMethod = methodsClass.getMethod(MethodName_Array[i],176Vector.class, Vector.class, Long.class, Integer.class);177// sum vector, ID vector, function depth, static function call depth178MethodInstance_Array[i] = nextMethod;179}180} catch (ClassNotFoundException e) {181throw new Error("TESTBUG Class: " + ClassArray[i] + " Not Found", e);182} catch (NoSuchMethodException e) {183throw new Error("TESTBUG Method: " + ClassArray[i] + "::" + MethodName_Array[i] + " Not Found", e);184} catch (SecurityException e) {185throw new Error("TESTBUG Security Exception Generated by " + ClassArray[i] + "::" + MethodName_Array[i], e);186}187return new MethodData(ClassArray[i], MethodName_Array[i], methodsClass, nextMethod, MethodID_Array[i]);188}189190191/* These two functions are used to verify that all function were called in the proper order */192193// called by "parent" function to add childs ID to vector194public static void addFunctionIDToVector(int FunctionIndex, Vector IDVector) {195IDVector.addElement(FunctionIndex);196}197198// called by "child" to add Function Index to Vector199public static void appendSumToSummationVector(int FunctionIndex, Vector SummationVector) {200if (SummationVector.isEmpty()) {201SummationVector.addElement((long) FunctionIndex);202} else {203SummationVector.addElement((Long) SummationVector.lastElement() + FunctionIndex);204}205}206207// This function calls a method based off of MethodData208public static void callMethod(MethodData methodCallStr,209Vector summation, Vector ID,210Long numFcalls, Integer staticFcalls)211throws InvocationTargetException {212try {213methodCallStr.nextMethod.invoke(methodCallStr.instance,214summation, ID, numFcalls, staticFcalls);215} catch (IllegalAccessException e) {216// should never happen with a valid testfile217throw new TestFailure("Illegal Access Exception", e);218}219}220}221222223