Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress003.java
41155 views
/*1* Copyright (c) 2007, 2020, 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*/2223/*24* jnistress003 is a class that sets up classes that do the work25* for the test.26*27* The Interrupter objects send interrupts to the JNIters.28* The GarbageGenerator objects generate garbage.29*30* sync[0] synchronizes the test cycles.31* sync[1] synchronizes access to exception counters.32* sync[2] synchronizes the cycle count update. It also insures that33* the interrupts do not interfere with the cycle count updates.34* This is because cycle count updates are used to define cycles.35*/3637/*38* @test39* @key stress40*41* @summary converted from VM testbase nsk/stress/jni/jnistress003.42* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]43*44* @library /vmTestbase45* /test/lib46* @run main/othervm/native47* nsk.stress.jni.jnistress00348* -numTHREADer 2049* -threadInterval 20050* -numInterrupter 251* -interruptInterval 50052* -numGarbage 8053* -garbageInterval 554* -numIteration 26055*/5657package nsk.stress.jni;5859import nsk.share.Consts;60import nsk.share.Debug;61import nsk.share.test.StressOptions;6263import java.lang.reflect.Array;6465public class jnistress003 extends Thread {6667/* Maximum number of iterations. Ignored if <= 0L */68static long numIteration = 0L;69/* Timeout */70static long timeOut;71/* Number of test class objects */72static int numJNIter = 1;73/* Time between JNI stressing by the threads under test */74/* (in milliseconds) */75static int jniInterval = 10000;76/* Number of interrupting threads */77static int numInterrupter = 1;78/* Time between interrupts in milliseconds */79static int interruptInterval = 100;80/* Number of garbage generating threads */81static int numGarbage = 1;82/* Time between garbage allocations in milliseconds */83static int garbageInterval = 100;84// The size of arrays creates via JNI85static int SIZE = 5000;86// The MAX quantity of creates arrays87static int jniStringAllocSize = 100000;8889private static StressOptions stressOptions;9091public static void main(String[] argv) {92try {93int i = 0;94int nJNISync = 10;95jnistress003 dm = null;96boolean errArg = false;9798stressOptions = new StressOptions(argv);99100/* Process arguments */101while (!errArg && i < argv.length) {102/* Number of iterations. Ignored if <= 0. */103if (i < argv.length && argv[i].equals("-numIteration")) {104++i;105if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {106try {107numIteration = Long.parseLong(argv[i++]);108} catch (NumberFormatException e) {109errArg = true;110}111}112} else if (i < argv.length && argv[i].equals("-numTHREADer")) {113++i;114if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {115try {116numJNIter = Integer.parseInt(argv[i++]);117} catch (NumberFormatException e) {118errArg = true;119}120if (numJNIter <= 0) errArg = true;121}122} else if (i < argv.length && argv[i].equals("-threadInterval")) {123++i;124if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {125try {126jniInterval = Integer.parseInt(argv[i++]);127} catch (NumberFormatException e) {128errArg = true;129}130}131} else if (i < argv.length && argv[i].equals("-numInterrupter")) {132++i;133if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {134try {135numInterrupter = Integer.parseInt(argv[i++]);136} catch (NumberFormatException e) {137errArg = true;138}139}140} else if (i < argv.length && argv[i].equals("-interruptInterval")) {141++i;142if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {143try {144interruptInterval = Integer.parseInt(argv[i++]);145} catch (NumberFormatException e) {146errArg = true;147}148}149} else if (i < argv.length && argv[i].equals("-numGarbage")) {150++i;151if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {152try {153numGarbage = Integer.parseInt(argv[i++]);154} catch (NumberFormatException e) {155errArg = true;156}157}158} else if (i < argv.length && argv[i].equals("-garbageInterval")) {159++i;160if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {161try {162garbageInterval = Integer.parseInt(argv[i++]);163} catch (NumberFormatException e) {164errArg = true;165}166}167} else if (i < argv.length && argv[i].equals("-arraysize")) {168++i;169if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {170try {171SIZE = Integer.parseInt(argv[i++]);172} catch (NumberFormatException e) {173errArg = true;174}175}176} else if (i < argv.length && argv[i].equals("-jniStringAllocSize")) {177++i;178if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {179try {180jniStringAllocSize = Integer.parseInt(argv[i++]);181} catch (NumberFormatException e) {182errArg = true;183}184}185} else if (i < argv.length && argv[i].startsWith("-stress")) {186++i;187if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {188++i;189}190} else System.out.println("Argument #" + i++ + " is incorrect");191}192193numIteration *= stressOptions.getIterationsFactor();194numJNIter *= stressOptions.getThreadsFactor();195numInterrupter *= stressOptions.getThreadsFactor();196numGarbage *= stressOptions.getThreadsFactor();197timeOut = stressOptions.getTime() * 1000;198199sync = new Synchronizer[10];200for (i = 0; i < nJNISync; i++)201sync[i] = new Synchronizer();202dm = new jnistress003(numIteration, numJNIter, jniInterval,203numInterrupter, interruptInterval,204numGarbage, garbageInterval);205206dm.start();207208try {209dm.join(timeOut);210} catch (InterruptedException e) {211System.out.println("TESTER THREAD WAS INTERRUPTED");212System.exit(Consts.TEST_FAILED);213}214215if (DEBUG) System.out.println("jnistress003::main(): halt!");216217if (dm.isAlive()) {218System.out.println("TIME LIMIT EXCEEDED");219dm.halt();220if (DEBUG) System.out.println("jnistress003::main(): join!");221try {222dm.join(10000L);223} catch (InterruptedException e) {224System.out.println("TESTER THREAD WAS INTERRUPTED");225System.exit(Consts.TEST_FAILED);226}227} else {228System.out.println("TESTER THREAD FINISHED");229}230231if (DEBUG) System.out.println("jnistress003::main(): zzzz...");232233if (!JNIter003.passed())234System.exit(Consts.TEST_FAILED);235} catch (Throwable e) {236Debug.Fail(e);237}238}239240jnistress003(241long iters,242int nJNI,243int jniInterval,244int nInter,245int iruptInterval,246int nGarb,247int garbInterval248) {249int i = 0;250nCycles = iters;251/* Should have at least one of nCycles>0 */252if (nCycles <= 0) nCycles = Long.MAX_VALUE;253jniter = new JNIter003[nJNI];254interval = jniInterval;255irupt = new Interrupter[nInter];256garb = new GarbageGenerator[nGarb];257for (i = 0; i < nJNI; i++)258jniter[i] = new JNIter003(sync);259for (i = 0; i < nInter; i++) {260irupt[i] = new Interrupter(jniter, sync);261irupt[i].setInterval(iruptInterval);262}263for (i = 0; i < nGarb; i++) {264garb[i] = new GarbageGenerator();265garb[i].setInterval(garbInterval);266}267}268269public void run() {270try {271int i = 0;272long iCycle = 0L;273JNIter003.clearCount();274JNIter003.clearInterruptCount();275for (i = 0; i < jniter.length; i++)276jniter[i].start();277278while (!done && JNIter003.getCount() < jniter.length) {279try {280sleep(100);281} catch (InterruptedException e) {282}283}284JNIter003.clearCount();285// JNIter003.clearInterruptCount();286synchronized (sync[0]) {287sync[0].notifyAll();288}289290for (i = 0; i < garb.length; i++)291garb[i].start();292for (i = 0; i < irupt.length; i++)293irupt[i].start();294295if (DEBUG) System.out.println("Cycles=" + nCycles);296for (iCycle = 0; iCycle < nCycles && !done && JNIter003.passed(); iCycle++) {297System.out.print("Cycle: " + iCycle);298try {299sleep(interval);300} catch (InterruptedException e) {301}302synchronized (sync[1]) {303System.out.println(" Interrupt count=" +304JNIter003.getInterruptCount());305}306JNIter003.clearCount();307synchronized (sync[0]) {308sync[0].notifyAll();309}310int n = 0;311for (i = 0; i < jniter.length; i++)312if (jniter[i].finished()) n++;313if (n == jniter.length) break;314}315if (JNIter003.passed()) {316System.out.println("JNI TEST PASSED");317} else {318System.out.println("JNI TEST FAILED");319}320for (i = 0; i < irupt.length; i++)321irupt[i].halt();322for (i = 0; i < garb.length; i++)323garb[i].halt();324for (i = 0; i < jniter.length; i++)325jniter[i].halt();326/* Flush any waiters */327if (DEBUG) System.out.println("jnistress003::run(): before sync[0]");328synchronized (sync[0]) {329sync[0].notifyAll();330}331if (DEBUG) System.out.println("jnistress003::run(): after sync[0]");332for (i = 0; i < irupt.length; i++) {333try {334irupt[i].join();335} catch (InterruptedException e) {336}337}338if (DEBUG) System.out.println("jnistress003::run(): X");339for (i = 0; i < garb.length; i++) {340try {341garb[i].join();342} catch (InterruptedException e) {343}344}345if (DEBUG) System.out.println("jnistress003::run(): Y");346for (i = 0; i < jniter.length; i++) {347try {348if (jniter[i].isAlive()) {349jniter[i].join();350}351} catch (InterruptedException e) {352}353}354synchronized (sync[0]) {355sync[0].notifyAll();356}357if (DEBUG) System.out.println("jnistress003::run(): Z");358} catch (Throwable e) {359Debug.Fail(e);360}361}362363public void halt() {364done = true;365}366367public boolean finished() {368return done;369}370371long nCycles = 0;372JNIter003[] jniter;373static Synchronizer[] sync;374private int interval = 100;375Interrupter[] irupt;376GarbageGenerator[] garb;377private boolean done = false;378final private static boolean DEBUG = false;379}380381class JNIter003 extends Thread {382383// The native methods for testing JNI Arrays calls384385public native Object[] jniInitArrays(int size);386387public native boolean jniBodyChangeArray(Object[] orig,388Object[] clone, int jniStringAllocSize);389390static {391System.loadLibrary("jnistress003");392}393394public JNIter003(Synchronizer[] aSync) {395sync = aSync;396}397398public void run() {399try {400Object[] mainArray;401Object[] clonedArray;402403int i, j;404int iter = 0;405406/* Synchronize start of work */407incCount();408synchronized (sync[0]) {409try {410sync[0].wait();411} catch (InterruptedException e) {412}413}414while (!done && pass) {415try {416/* Synchronized the JNI stressing */417synchronized (sync[2]) {418incCount();419}420synchronized (sync[0]) {421try {422sync[0].wait();423} catch (InterruptedException e) {424synchronized (sync[1]) {425JNIter003.incInterruptCount();426}427}428}429// synchronized(sync[0]) {430try {431mainArray = jniInitArrays(jnistress003.SIZE);432clonedArray = (Object[]) mainArray.clone();433JNIter003.pass = jniBodyChangeArray(mainArray, clonedArray,434jnistress003.jniStringAllocSize);435for (i = 0; i < 8; i++)436compared &= Array.get(mainArray, i).equals(Array.get(clonedArray, i));437} catch (OutOfMemoryError e) {438System.out.println("Error: " + e);439}440// }441if (DEBUG)442System.out.println("We have " + activeCount() + " threads now.");443JNIter003.pass = JNIter003.pass && compared;444synchronized (this) {445try {446wait(1L);447} catch (InterruptedException e) {448throw new InterruptedException();449}450}451} catch (InterruptedException e) {452synchronized (sync[1]) {453JNIter003.incInterruptCount();454}455}456iter++;457iter = iter % CASECOUNT;458}459if (DEBUG) System.out.println("JNITer::run(): done=" + done);460done = true;461if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter003.pass);462if (DEBUG) System.out.println("JNIter003::run(): done");463} catch (Throwable e) {464Debug.Fail(e);465}466}467468private synchronized static void incCount() {469count++;470}471472public static int getCount() {473return count;474}475476public synchronized static void clearCount() {477count = 0;478}479480private synchronized static void incInterruptCount() {481interruptCount++;482}483484public static int getInterruptCount() {485return interruptCount;486}487488public synchronized static void clearInterruptCount() {489interruptCount = 0;490}491492public void halt() {493done = true;494}495496public boolean finished() {497return done;498}499500public static boolean passed() {501return pass;502}503504Synchronizer[] sync;505private static int count = 0;506private static int interruptCount = 0;507private boolean done = false;508private static boolean pass = true;509private static boolean compared = true;510final private static int CASECOUNT = 2;511final private static boolean DEBUG = false;512}513514515