test/hotspot/jtreg/vmTestbase/jit/graph/Globals.java
changeset 58564 218a1a642c6f
parent 50366 4d85990f9c4a
equal deleted inserted replaced
58563:78bbb5dde2a3 58564:218a1a642c6f
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 package jit.graph;
    24 package jit.graph;
    25 import java.io.*;
    25 
    26 import java.util.*;
    26 import jdk.test.lib.Utils;
    27 import java.lang.*;
       
    28 import java.lang.reflect.*;
       
    29 import nsk.share.TestFailure;
    27 import nsk.share.TestFailure;
    30 
    28 
    31 
    29 import java.io.BufferedReader;
    32 public final class Globals
    30 import java.io.File;
    33 {
    31 import java.io.FileNotFoundException;
    34     // Minimum and Maximum number of threads
    32 import java.io.FileReader;
    35     public static int     NUM_THREADS      = 1;
    33 import java.io.IOException;
    36     public static long    RANDOM_SEED      = System.currentTimeMillis();
    34 import java.lang.reflect.InvocationTargetException;
    37     public static int     STATIC_LOOP      = 0;
    35 import java.lang.reflect.Method;
    38     public static int     NUM_TEST_CLASSES = 7;
    36 import java.util.Random;
    39     public static long    RANDOM_LOOP      = 100;
    37 import java.util.StringTokenizer;
    40     public static boolean VERBOSE          = false;
    38 import java.util.Vector;
    41     private static Random indexGenerator   = null;
    39 
    42 
    40 public final class Globals {
    43   //private static TestLoader CGTTestLoader = null;
    41 
    44     private static String [] ClassArray = null;
    42 
    45     private static Class [] ClassInstanceArray = null;
    43     public static int STATIC_LOOP = 0;
    46     private static int       maxClassIndex    = 0;
    44     public static int NUM_TEST_CLASSES = 7;
    47 
    45     public static long RANDOM_LOOP = 100;
    48     private static String [] MethodName_Array = null;
    46     public static boolean VERBOSE = false;
    49     private static Method [] MethodInstance_Array = null;
    47 
    50 
    48     private static final Random indexGenerator = Utils.getRandomInstance();
    51     //Should be prime, so that odds of an incorrect verification reduced
    49     private static String[] ClassArray = null;
    52     public static  int    [] MethodID_Array   = null;
    50     private static Class[] ClassInstanceArray = null;
    53 
    51     private static int maxClassIndex = 0;
    54 
    52 
    55     //Number of threads will be reduced as threads finish
    53     private static String[] MethodName_Array = null;
    56     public static synchronized void decNumThreads(){NUM_THREADS--;};
    54     private static Method[] MethodInstance_Array = null;
    57 
    55 
    58     public static synchronized void initialize(String testListPath)
    56     // Should be prime, so that odds of an incorrect verification reduced
    59     {
    57     public static int[] MethodID_Array = null;
    60 
    58 
    61         File td = new File (testListPath);
    59     public static synchronized void initialize(String testListPath) {
    62         if (!td.exists())
    60         File td = new File(testListPath);
    63             {
    61         if (!td.exists()) {
    64                 System.err.println("File " + testListPath + " Not found");
    62             throw new Error("TESTBUG: File " + testListPath + " Not found");
    65                 System.exit(1);
    63         }
    66             }
    64 
    67         if (!td.isFile())
    65         if (!td.isFile()) {
    68             {
    66             throw new Error("TESTBUG: " + testListPath + " Must be a File");
    69                 System.err.println(testListPath + " Must be a File");
    67         }
    70                 System.exit(1);
       
    71             }
       
    72 
    68 
    73         BufferedReader classList = null;
    69         BufferedReader classList = null;
    74 
    70         try {
    75         try
    71             try {
    76           {
    72                 classList = new BufferedReader(new FileReader(td));
    77             classList = new BufferedReader(new FileReader(td));
    73             } catch (FileNotFoundException e) {
    78           }
    74                 throw new Error("TESTBUG: Error finding Classlist", e);
    79         catch (FileNotFoundException  fnfx)
    75             }
    80           {
    76 
    81             System.err.println("Error finding Classlist");
    77             String line = null;
    82             System.exit(1);
    78             try {
    83           }
       
    84 
       
    85         String line = null;
       
    86         try
       
    87             {
       
    88                 line = classList.readLine();
    79                 line = classList.readLine();
    89             }
    80             } catch (IOException e) {
    90         catch (IOException iox)
    81                 throw new Error("TESTBUG: Error reading Classlist", e);
    91             {
    82             }
    92                 System.err.println("Error reading Classlist");
    83 
    93                 System.exit(1);
    84             try {
    94             }
    85                 // ClassArray.length;
    95 
    86                 maxClassIndex = Math.abs(Integer.parseInt(line));
    96         try
    87             } catch (NumberFormatException e) {
    97             {
    88                 throw new Error("TESTBUG: Error reading Classlist - first number must be number of methods defined", e);
    98                 maxClassIndex = Math.abs(Integer.parseInt(line));//ClassArray.length;
    89             }
    99             }
    90 
   100         catch (NumberFormatException nfx)
    91             ClassArray = new String[maxClassIndex];
   101             {
    92             ClassInstanceArray = new Class[maxClassIndex];
   102                 System.err.println("Error reading Classlist - first number must be number of methods defined");
    93             MethodName_Array = new String[maxClassIndex];
   103                 System.exit(1);
    94             MethodInstance_Array = new Method[maxClassIndex];
   104             }
    95             MethodID_Array = new int[maxClassIndex];
   105 
    96 
   106         ClassArray = new String [maxClassIndex];
    97             int i;
   107 ClassInstanceArray = new Class [maxClassIndex];
    98             for (i = 0; i < maxClassIndex; i++) {
   108         MethodName_Array = new String [maxClassIndex];
    99                 try {
   109         MethodInstance_Array = new Method [maxClassIndex];
   100                     line = classList.readLine();
   110         MethodID_Array = new int [maxClassIndex];
   101                 } catch (IOException e) {
   111 
   102                     throw new Error("TESTBUG: Error reading ClasslistFile: testListPath", e);
   112         int i;
   103                 }
   113         for (i = 0; (i<maxClassIndex) && (line != null); i++)
       
   114             {
       
   115                 try
       
   116                     {
       
   117                         line = classList.readLine();
       
   118                     }
       
   119                 catch (IOException iox)
       
   120                     {
       
   121                         System.err.println("Error reading ClasslistFile: testListPath");
       
   122                         System.exit(1);
       
   123                     }
       
   124                 StringTokenizer lineTokens = new StringTokenizer(line, "\t ");
   104                 StringTokenizer lineTokens = new StringTokenizer(line, "\t ");
   125                 if (lineTokens.countTokens() <3)
   105                 if (lineTokens.countTokens() < 3) {
   126                   {
   106                     throw new Error("TESTBUG: ClasslistFile: unexpected line:" + line);
   127                     System.out.println("Error reading ClasslistFile: Errored line");
   107                 } else {
   128                     i--;
       
   129                   }
       
   130                 else
       
   131                   {
       
   132                     ClassArray[i] = lineTokens.nextToken();
   108                     ClassArray[i] = lineTokens.nextToken();
   133                     MethodName_Array[i] =lineTokens.nextToken();
   109                     MethodName_Array[i] = lineTokens.nextToken();
   134                     MethodID_Array[i] = Integer.parseInt(lineTokens.nextToken());
   110                     MethodID_Array[i] = Integer.parseInt(lineTokens.nextToken());
   135                   }
   111                 }
   136             }
   112             }
   137         maxClassIndex = i;
   113             maxClassIndex = i;
   138 
   114         } finally {
   139         indexGenerator = new Random(RANDOM_SEED);
   115             if (classList != null) {
   140         if ((NUM_TEST_CLASSES < ClassArray.length) && (NUM_TEST_CLASSES > 0))
   116                 try {
   141           maxClassIndex = NUM_TEST_CLASSES;
   117                     classList.close();
   142         else
   118                 } catch (IOException e) {
   143           NUM_TEST_CLASSES = maxClassIndex;
   119                     throw new Error("can't close file", e);
   144     }
   120                 }
   145 
   121             }
   146     //does a binary serach to find the index for the ID of a method
   122         }
   147     private static int ID_BinSearch(int begin, int end, int ID)
   123 
   148     {
   124         if ((NUM_TEST_CLASSES < ClassArray.length) && (NUM_TEST_CLASSES > 0)) {
   149         if (end < begin)
   125             maxClassIndex = NUM_TEST_CLASSES;
   150             return(-1);
   126         } else {
   151 
   127             NUM_TEST_CLASSES = maxClassIndex;
   152         int mid = (begin + end)/2;
   128         }
       
   129     }
       
   130 
       
   131     // does a binary search to find the index for the ID of a method
       
   132     private static int ID_BinSearch(int begin, int end, int ID) {
       
   133         if (end < begin) {
       
   134             return (-1);
       
   135         }
       
   136 
       
   137         int mid = (begin + end) / 2;
   153         int midvalue = MethodID_Array[mid];
   138         int midvalue = MethodID_Array[mid];
   154 
   139 
   155         if (ID == midvalue)
   140         if (ID == midvalue) {
   156             return (mid);
   141             return (mid);
   157         else if (ID < midvalue)
   142         } else if (ID < midvalue) {
   158             return(ID_BinSearch(begin, mid-1, ID));
   143             return (ID_BinSearch(begin, mid - 1, ID));
   159         else
   144         } else {
   160             return(ID_BinSearch(mid+1, end, ID));
   145             return (ID_BinSearch(mid + 1, end, ID));
   161     }
   146         }
   162 
   147     }
   163 
   148 
   164     //based off a static index, this function selects the method to be called
   149 
   165     public static MethodData returnNextStaticMethod(int Method_ID)
   150     // based off a static index, this function selects the method to be called
   166     {
   151     public static MethodData returnNextStaticMethod(int Method_ID) {
   167       //int i = ID_BinSearch(0, MethodID_Array.length - 1, Method_ID);
   152         //int i = ID_BinSearch(0, MethodID_Array.length - 1, Method_ID);
   168       int i = ID_BinSearch(0, maxClassIndex - 1, Method_ID);
   153         int i = ID_BinSearch(0, maxClassIndex - 1, Method_ID);
   169 
   154 
   170       return(nextStaticMethod((i==-1)?0:i));
   155         return (nextStaticMethod((i == -1) ? 0 : i));
   171     }
   156     }
   172 
   157 
   173     //this function randomly selects the next method to be called by the test class
   158     // this function randomly selects the next method to be called by the test class
   174     public static MethodData nextRandomMethod()
   159     public static MethodData nextRandomMethod() {
   175     {
       
   176 
       
   177         int i = indexGenerator.nextInt(maxClassIndex);
   160         int i = indexGenerator.nextInt(maxClassIndex);
   178         return(nextStaticMethod(i));
   161         return (nextStaticMethod(i));
   179     }
   162     }
   180 
   163 
   181     private static MethodData nextStaticMethod(int i)
   164     private static MethodData nextStaticMethod(int i) {
   182     {
       
   183         Class methodsClass = null;
   165         Class methodsClass = null;
   184         Method nextMethod = null;
   166         Method nextMethod = null;
   185 
   167 
   186         try
   168         try {
   187             {
   169             methodsClass = ClassInstanceArray[i];
   188               //methodsClass = CGTTestLoader.findClass(ClassArray[i]);
   170             if (methodsClass == null) {
   189               methodsClass = ClassInstanceArray[i];
   171                 methodsClass = Class.forName(ClassArray[i]);
   190               if (methodsClass == null)
   172                 ClassInstanceArray[i] = methodsClass;
   191               {
   173             }
   192                   methodsClass = Class.forName(ClassArray[i]);
   174             nextMethod = MethodInstance_Array[i];
   193                   ClassInstanceArray[i] = methodsClass;
   175             if (nextMethod == null) {
   194               }
   176                 nextMethod = methodsClass.getMethod(MethodName_Array[i],
   195               nextMethod = MethodInstance_Array[i];
   177                         Vector.class, Vector.class,  Long.class, Integer.class);
   196               if (nextMethod == null )
   178                 // sum vector, ID vector, function depth, static function call depth
   197               {
   179                 MethodInstance_Array[i] = nextMethod;
   198               nextMethod =
   180             }
   199                 methodsClass.getMethod(MethodName_Array[i],
   181         } catch (ClassNotFoundException e) {
   200                                        new Class[]{java.util.Vector.class, java.util.Vector.class,
   182             throw new Error("TESTBUG Class: " + ClassArray[i] + " Not Found", e);
   201                                                      java.lang.Long.class, java.lang.Integer.class});
   183         } catch (NoSuchMethodException e) {
   202               //sum vector, ID vector, function depth, static function call depth
   184             throw new Error("TESTBUG Method: " + ClassArray[i] + "::" + MethodName_Array[i] + " Not Found", e);
   203               MethodInstance_Array[i] = nextMethod;
   185         } catch (SecurityException e) {
   204               }
   186             throw new Error("TESTBUG Security Exception Generated by " + ClassArray[i] + "::" + MethodName_Array[i], e);
   205             }
   187         }
   206         catch (ClassNotFoundException cnfx)
   188         return new MethodData(ClassArray[i], MethodName_Array[i], methodsClass, nextMethod, MethodID_Array[i]);
   207             {
   189     }
   208                 System.out.println("Class: " +ClassArray[i]+ " Not Found");
   190 
   209                 System.exit(-1);
   191 
   210             }
   192     /* These two functions are used to verify that all function were called in the proper order */
   211         catch (NoSuchMethodException nsmx)
   193 
   212             {
   194     // called by "parent" function to add childs ID to vector
   213                 System.out.println("Class: " +ClassArray[i]);
   195     public static void addFunctionIDToVector(int FunctionIndex, Vector IDVector) {
   214                 System.out.println("Method: " +MethodName_Array[i]+" Not Found");
   196         IDVector.addElement(FunctionIndex);
   215                 System.exit(-1);
   197     }
   216             }
   198 
   217         catch (SecurityException sx)
   199     // called by "child" to add Function Index to Vector
   218             {
   200     public static void appendSumToSummationVector(int FunctionIndex, Vector SummationVector) {
   219                 System.out.println("Class: " +ClassArray[i]);
   201         if (SummationVector.isEmpty()) {
   220                 System.out.println("Method: " +MethodName_Array[i]);
   202             SummationVector.addElement((long) FunctionIndex);
   221                 System.out.println("Security Exception Generated, by above method call");
   203         } else {
   222                 System.exit(-1);
   204             SummationVector.addElement((Long) SummationVector.lastElement() + FunctionIndex);
   223             }
   205         }
   224         return(new MethodData(ClassArray[i], MethodName_Array[i], methodsClass, nextMethod, MethodID_Array[i]));
   206     }
   225     }
   207 
   226 
   208     // This function calls a method based off of MethodData
   227 
       
   228     /*These two functions are used to verify that all function were called in the proper order*/
       
   229 
       
   230     //called by "parent" function to add childs ID to vector
       
   231     public static void addFunctionIDToVector(int FunctionIndex, Vector IDVector)
       
   232     {
       
   233         IDVector.addElement(new Integer(FunctionIndex));
       
   234     }
       
   235 
       
   236     //called by "child" to add Function Index to Vector
       
   237     public static void appendSumToSumationVector(int FunctionIndex, Vector SummationVector)
       
   238     {
       
   239         if (SummationVector.isEmpty())
       
   240             SummationVector.addElement(new Long(FunctionIndex));
       
   241         else
       
   242             SummationVector.addElement(new Long(((Long)SummationVector.lastElement()).longValue() + FunctionIndex));
       
   243     }
       
   244 
       
   245     //This function calls a method based off of MethodData
       
   246     public static void callMethod(MethodData methodCallStr,
   209     public static void callMethod(MethodData methodCallStr,
   247                                   Vector summation, Vector ID,
   210                                   Vector summation, Vector ID,
   248                                   Long numFcalls, Integer staticFcalls)
   211                                   Long numFcalls, Integer staticFcalls)
   249                                   throws InvocationTargetException
   212             throws InvocationTargetException {
   250 
   213         try {
   251     {
   214             methodCallStr.nextMethod.invoke(methodCallStr.instance,
   252                 if(NUM_THREADS >1)
   215                     summation, ID, numFcalls, staticFcalls);
   253                     {
   216         } catch (IllegalAccessException e) {
   254                         if ((staticFcalls.intValue() + numFcalls.longValue()) %23 == 0)
   217             // should never happen with a valid testfile
   255                             {
   218             throw new TestFailure("Illegal Access Exception", e);
   256                                 try
   219         }
   257                                     {
       
   258                                         Thread.sleep(225);
       
   259                                     }
       
   260                                 catch (InterruptedException ie)
       
   261                                     {}
       
   262                                 if (VERBOSE)
       
   263                                     System.out.println("\t\tCurrentThread:" + Thread.currentThread().getName());
       
   264                             }
       
   265                     }
       
   266 
       
   267                 try
       
   268             {
       
   269                         methodCallStr.nextMethod.invoke(methodCallStr.instance,
       
   270                                 new Object []{summation, ID, numFcalls, staticFcalls});
       
   271             }
       
   272                 catch (IllegalAccessException iax)  //should never happen with a valid testfile
       
   273             {
       
   274                         throw new TestFailure("Illegal Access Exception");
       
   275             }
       
   276                     /*
       
   277                 catch (InvocationTargetException itx)
       
   278                     {
       
   279                         itx.printStackTrace();
       
   280                         System.out.println("Invocation Target Exception");
       
   281                         System.exit(1);
       
   282                     }*/
       
   283     }
   220     }
   284 }
   221 }