Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress004.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* jnistress004 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/jnistress004.42* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]43*44* @library /vmTestbase45* /test/lib46* @run main/othervm/native47* nsk.stress.jni.jnistress00448* -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;6263public class jnistress004 extends Thread {6465/* Maximum number of iterations. Ignored if <= 0L */66static long numIteration = 2L;67/* Timeout */68static long timeOut;69/* Number of test class objects */70static int numJNIter = 100;71/* Time between JNI stressing by the threads under test */72/* (in milliseconds) */73static int jniInterval = 25;74/* Number of interrupting threads */75static int numInterrupter = 10;76/* Time between interrupts in milliseconds */77static int interruptInterval = 45;78/* Number of garbage generating threads */79static int numGarbage = 1;80/* Time between garbage allocations in milliseconds */81static int garbageInterval = 100;82// The MAX quantity of critical operations83static int jniStringAllocSize = 50000;8485private static StressOptions stressOptions;8687public static void main(String[] argv) {88try {89int i = 0;90int nJNISync = 10;91jnistress004 dm = null;92boolean errArg = false;9394stressOptions = new StressOptions(argv);9596/* Process arguments */97while (!errArg && i < argv.length) {98/* Number of iterations. Ignored if <= 0. */99if (i < argv.length && argv[i].equals("-numIteration")) {100++i;101if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {102try {103numIteration = Long.parseLong(argv[i++]);104} catch (NumberFormatException e) {105errArg = true;106}107}108} else if (i < argv.length && argv[i].equals("-numTHREADer")) {109++i;110if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {111try {112numJNIter = Integer.parseInt(argv[i++]);113} catch (NumberFormatException e) {114errArg = true;115}116if (numJNIter <= 0) errArg = true;117}118} else if (i < argv.length && argv[i].equals("-threadInterval")) {119++i;120if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {121try {122jniInterval = Integer.parseInt(argv[i++]);123} catch (NumberFormatException e) {124errArg = true;125}126}127} else if (i < argv.length && argv[i].equals("-numInterrupter")) {128++i;129if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {130try {131numInterrupter = Integer.parseInt(argv[i++]);132} catch (NumberFormatException e) {133errArg = true;134}135}136} else if (i < argv.length && argv[i].equals("-interruptInterval")) {137++i;138if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {139try {140interruptInterval = Integer.parseInt(argv[i++]);141} catch (NumberFormatException e) {142errArg = true;143}144}145} else if (i < argv.length && argv[i].equals("-numGarbage")) {146++i;147if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {148try {149numGarbage = Integer.parseInt(argv[i++]);150} catch (NumberFormatException e) {151errArg = true;152}153}154} else if (i < argv.length && argv[i].equals("-garbageInterval")) {155++i;156if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {157try {158garbageInterval = Integer.parseInt(argv[i++]);159} catch (NumberFormatException e) {160errArg = true;161}162}163} else if (i < argv.length && argv[i].equals("-jniStringAllocSize")) {164++i;165if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {166try {167jniStringAllocSize = Integer.parseInt(argv[i++]);168} catch (NumberFormatException e) {169errArg = true;170}171}172} else if (i < argv.length && argv[i].startsWith("-stress")) {173++i;174if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {175++i;176}177} else System.out.println("Argument #" + i++ + " is incorrect");178}179180numIteration *= stressOptions.getIterationsFactor();181numJNIter *= stressOptions.getThreadsFactor();182numInterrupter *= stressOptions.getThreadsFactor();183numGarbage *= stressOptions.getThreadsFactor();184timeOut = stressOptions.getTime() * 1000;185186sync = new Synchronizer[10];187for (i = 0; i < nJNISync; i++)188sync[i] = new Synchronizer();189dm = new jnistress004(numIteration, numJNIter, jniInterval,190numInterrupter, interruptInterval, numGarbage, garbageInterval);191dm.start();192193try {194dm.join(timeOut);195} catch (InterruptedException e) {196System.out.println("TESTER THREAD WAS INTERRUPTED");197System.exit(Consts.TEST_FAILED);198}199200if (DEBUG) System.out.println("jnistress004::main(): halt!");201202if (dm.isAlive()) {203System.out.println("TIME LIMIT EXCEEDED");204dm.halt();205if (DEBUG) System.out.println("jnistress004::main(): join!");206try {207dm.join(10000L);208} catch (InterruptedException e) {209System.out.println("TESTER THREAD WAS INTERRUPTED");210System.exit(Consts.TEST_FAILED);211}212} else {213System.out.println("TESTER THREAD FINISHED");214}215216if (DEBUG) System.out.println("jnistress004::main(): zzzz...");217218if (!JNIter004.passed())219System.exit(Consts.TEST_FAILED);220221} catch (Throwable e) {222Debug.Fail(e);223}224}225226jnistress004(227long iters,228int nJNI,229int jniInterval,230int nInter,231int iruptInterval,232int nGarb,233int garbInterval234) {235int i = 0;236nCycles = iters;237/* Should have at least one of nCycles>0 */238if (nCycles <= 0) nCycles = Long.MAX_VALUE;239jniter = new JNIter004[nJNI];240interval = jniInterval;241irupt = new Interrupter[nInter];242garb = new GarbageGenerator[nGarb];243for (i = 0; i < nJNI; i++)244jniter[i] = new JNIter004(sync);245for (i = 0; i < nInter; i++) {246irupt[i] = new Interrupter(jniter, sync);247irupt[i].setInterval(iruptInterval);248}249for (i = 0; i < nGarb; i++) {250garb[i] = new GarbageGenerator();251garb[i].setInterval(garbInterval);252}253}254255public void run() {256try {257int i = 0;258long iCycle = 0L;259JNIter004.clearCount();260JNIter004.clearInterruptCount();261for (i = 0; i < jniter.length; i++)262jniter[i].start();263264while (JNIter004.getCount() < jniter.length) {265try {266sleep(100);267} catch (InterruptedException e) {268}269}270JNIter004.clearCount();271// JNIter004.clearInterruptCount();272synchronized (sync[0]) {273sync[0].notifyAll();274}275276for (i = 0; i < garb.length; i++)277garb[i].start();278for (i = 0; i < irupt.length; i++)279irupt[i].start();280281if (DEBUG) System.out.println("Cycles=" + nCycles);282for (iCycle = 0; iCycle < nCycles && !done && JNIter004.passed(); iCycle++) {283System.out.print("Cycle: " + iCycle);284try {285sleep(interval);286} catch (InterruptedException e) {287}288synchronized (sync[1]) {289System.out.println(" Interrupt count=" +290JNIter004.getInterruptCount());291}292JNIter004.clearCount();293synchronized (sync[0]) {294sync[0].notifyAll();295}296int n = 0;297for (i = 0; i < jniter.length; i++)298if (jniter[i].finished()) n++;299if (n == jniter.length) break;300}301if (JNIter004.passed())302System.out.println("JNI TEST PASSED");303else304System.out.println("JNI TEST FAILED");305for (i = 0; i < irupt.length; i++)306irupt[i].halt();307for (i = 0; i < garb.length; i++)308garb[i].halt();309for (i = 0; i < jniter.length; i++)310jniter[i].halt();311/* Flush any waiters */312if (DEBUG) System.out.println("jnistress004::run(): before sync[0]");313synchronized (sync[0]) {314sync[0].notifyAll();315}316if (DEBUG) System.out.println("jnistress004::run(): after sync[0]");317for (i = 0; i < irupt.length; i++) {318try {319irupt[i].join();320} catch (InterruptedException e) {321}322}323if (DEBUG) System.out.println("jnistress004::run(): X");324for (i = 0; i < garb.length; i++) {325try {326garb[i].join();327} catch (InterruptedException e) {328}329}330if (DEBUG) System.out.println("jnistress004::run(): Y");331synchronized (sync[0]) {332sync[0].notifyAll();333}334for (i = 0; i < jniter.length; i++) {335try {336if (jniter[i].isAlive()) {337jniter[i].join();338}339} catch (InterruptedException e) {340}341}342if (DEBUG) System.out.println("jnistress004::run(): Z");343} catch (Throwable e) {344Debug.Fail(e);345}346}347348public void halt() {349done = true;350}351352public boolean finished() {353return done;354}355356long nCycles = 0;357JNIter004[] jniter;358static Synchronizer[] sync;359private int interval = 100;360Interrupter[] irupt;361GarbageGenerator[] garb;362private boolean done = false;363final private static boolean DEBUG = false;364}365366class JNIter004 extends Thread {367368// The native methods for testing JNI critical calls369public native char[] CheckSum(String str);370371public native boolean CheckCompare(String name, char[] sum, int upper);372373static {374System.loadLibrary("jnistress004");375}376377Runtime myRT = Runtime.getRuntime();378static int Count = 0;379380public JNIter004(Synchronizer[] aSync) {381sync = aSync;382}383384public void run() {385try {386char[] Sum;387int iter = 0;388389/* Synchronize start of work */390incCount();391synchronized (sync[0]) {392try {393sync[0].wait();394} catch (InterruptedException e) {395}396}397while (!done && pass) {398try {399/* Synchronized the JNI stressing */400synchronized (sync[2]) {401incCount();402}403synchronized (sync[0]) {404try {405sync[0].wait();406} catch (InterruptedException e) {407synchronized (sync[1]) {408JNIter004.incInterruptCount();409}410}411}412synchronized (sync[0]) {413try {414if (Count++ < jnistress004.jniStringAllocSize) {415System.out.println("JAVA: comparing " + (getName()) + " with " + CheckSum(getName()));416if (!CheckCompare(getName(), CheckSum(getName()), jnistress004.jniStringAllocSize))417pass = true;418}419} catch (OutOfMemoryError e) {420System.out.println("Error in Java code" + e);421}422}423if (DEBUG)424System.out.println(getName() + "\t\t" + myRT.freeMemory());425synchronized (this) {426try {427wait(1L);428} catch (InterruptedException e) {429throw new InterruptedException();430}431}432} catch (Exception e) {433synchronized (sync[1]) {434JNIter004.incInterruptCount();435}436}437iter++;438iter = iter % CASECOUNT;439}440if (DEBUG) System.out.println("JNITer::run(): done=" + done);441done = true;442if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter004.pass);443if (DEBUG) System.out.println("JNIter004::run(): done");444} catch (Throwable e) {445Debug.Fail(e);446}447}448449private synchronized static void incCount() {450count++;451}452453public static int getCount() {454return count;455}456457public synchronized static void clearCount() {458count = 0;459}460461private synchronized static void incInterruptCount() {462interruptCount++;463}464465public static int getInterruptCount() {466return interruptCount;467}468469public synchronized static void clearInterruptCount() {470interruptCount = 0;471}472473public static void halt() {474done = true;475}476477public boolean finished() {478return done;479}480481public static boolean passed() {482return pass;483}484485public static void setpass(boolean value) {486pass = value;487}488489Synchronizer[] sync;490private static int count = 0;491private static int interruptCount = 0;492private static boolean done = false;493private static boolean pass = true;494final private static int CASECOUNT = 2;495final private static boolean DEBUG = false;496}497498499