Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress005.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* jnistress005 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*/363738/*39* @test40* @key stress41*42* @summary converted from VM testbase nsk/stress/jni/jnistress005.43* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]44*45* @library /vmTestbase46* /test/lib47* @run main/othervm/native48* nsk.stress.jni.jnistress00549* -numTHREADer 2050* -threadInterval 20051* -numInterrupter 252* -interruptInterval 50053* -numGarbage 8054* -garbageInterval 555* -numIteration 26056*/5758package nsk.stress.jni;5960import nsk.share.Consts;61import nsk.share.Debug;62import nsk.share.test.StressOptions;6364public class jnistress005 extends Thread {6566/* Maximum number of iterations. Ignored if <= 0L */67static long numIteration = 0L;68/* Timeout */69static long timeOut;70/* Number of test class objects */71static int numJNIter = 1;72/* Time between JNI stressing by the threads under test */73/* (in milliseconds) */74static int jniInterval = 10000;75/* Number of interrupting threads */76static int numInterrupter = 1;77/* Time between interrupts in milliseconds */78static int interruptInterval = 100;79/* Number of garbage generating threads */80static int numGarbage = 1;81/* Time between garbage allocations in milliseconds */82static int garbageInterval = 100;83// The MAX quantity of creates exceptions84static int jniStringAllocSize = 15000;8586private static StressOptions stressOptions;8788public static void main(String[] argv) {89try {90int i = 0;91int nJNISync = 10;92jnistress005 dm = null;93boolean errArg = false;9495stressOptions = new StressOptions(argv);9697/* Process arguments */98while (!errArg && i < argv.length) {99/* Number of iterations. Ignored if <= 0. */100if (i < argv.length && argv[i].equals("-numIteration")) {101++i;102if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {103try {104numIteration = Long.parseLong(argv[i++]);105} catch (NumberFormatException e) {106errArg = true;107}108}109} else if (i < argv.length && argv[i].equals("-numTHREADer")) {110++i;111if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {112try {113numJNIter = Integer.parseInt(argv[i++]);114} catch (NumberFormatException e) {115errArg = true;116}117if (numJNIter <= 0) errArg = true;118}119} else if (i < argv.length && argv[i].equals("-threadInterval")) {120++i;121if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {122try {123jniInterval = Integer.parseInt(argv[i++]);124} catch (NumberFormatException e) {125errArg = true;126}127}128} else if (i < argv.length && argv[i].equals("-numInterrupter")) {129++i;130if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {131try {132numInterrupter = Integer.parseInt(argv[i++]);133} catch (NumberFormatException e) {134errArg = true;135}136}137} else if (i < argv.length && argv[i].equals("-interruptInterval")) {138++i;139if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {140try {141interruptInterval = Integer.parseInt(argv[i++]);142} catch (NumberFormatException e) {143errArg = true;144}145}146} else if (i < argv.length && argv[i].equals("-numGarbage")) {147++i;148if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {149try {150numGarbage = Integer.parseInt(argv[i++]);151} catch (NumberFormatException e) {152errArg = true;153}154}155} else if (i < argv.length && argv[i].equals("-garbageInterval")) {156++i;157if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {158try {159garbageInterval = Integer.parseInt(argv[i++]);160} catch (NumberFormatException e) {161errArg = true;162}163}164} else if (i < argv.length && argv[i].equals("-jniStringAllocSize")) {165++i;166if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {167try {168jniStringAllocSize = Integer.parseInt(argv[i++]);169} catch (NumberFormatException e) {170errArg = true;171}172}173} else if (i < argv.length && argv[i].startsWith("-stress")) {174++i;175if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {176++i;177}178} else System.out.println("Argument #" + i++ + " is incorrect");179}180181numIteration *= stressOptions.getIterationsFactor();182numJNIter *= stressOptions.getThreadsFactor();183numInterrupter *= stressOptions.getThreadsFactor();184numGarbage *= stressOptions.getThreadsFactor();185timeOut = stressOptions.getTime() * 1000;186187sync = new Synchronizer[10];188for (i = 0; i < nJNISync; i++)189sync[i] = new Synchronizer();190dm = new jnistress005(numIteration, numJNIter, jniInterval,191numInterrupter, interruptInterval, numGarbage, garbageInterval);192dm.start();193194try {195dm.join(timeOut);196} catch (InterruptedException e) {197System.out.println("TESTER THREAD WAS INTERRUPTED");198System.exit(Consts.TEST_FAILED);199}200201if (DEBUG) System.out.println("jnistress005::main(): halt!");202203if (dm.isAlive()) {204System.out.println("TIME LIMIT EXCEEDED");205dm.halt();206if (DEBUG) System.out.println("jnistress005::main(): join!");207try {208dm.join(10000L);209} catch (InterruptedException e) {210System.out.println("TESTER THREAD WAS INTERRUPTED");211System.exit(Consts.TEST_FAILED);212}213} else {214System.out.println("TESTER THREAD FINISHED");215}216217if (DEBUG) System.out.println("jnistress005::main(): zzzz...");218219if (!JNIter005.passed())220System.exit(Consts.TEST_FAILED);221222} catch (Throwable e) {223Debug.Fail(e);224}225}226227jnistress005(228long iters,229int nJNI,230int jniInterval,231int nInter,232int iruptInterval,233int nGarb,234int garbInterval235) {236int i = 0;237nCycles = iters;238/* Should have at least one of nCycles>0 */239if (nCycles <= 0) nCycles = Long.MAX_VALUE;240jniter = new JNIter005[nJNI];241interval = jniInterval;242irupt = new Interrupter[nInter];243garb = new GarbageGenerator[nGarb];244for (i = 0; i < nJNI; i++)245jniter[i] = new JNIter005(sync);246for (i = 0; i < nInter; i++) {247irupt[i] = new Interrupter(jniter, sync);248irupt[i].setInterval(iruptInterval);249}250for (i = 0; i < nGarb; i++) {251garb[i] = new GarbageGenerator();252garb[i].setInterval(garbInterval);253}254}255256public void run() {257try {258int i = 0;259long iCycle = 0L;260// JNIter005.clearCount();261JNIter005.clearInterruptCount();262for (i = 0; i < jniter.length; i++)263jniter[i].start();264265while (JNIter005.getCount() < jniter.length) {266try {267sleep(100);268} catch (InterruptedException e) {269}270}271// JNIter005.clearCount();272// JNIter005.clearInterruptCount();273synchronized (sync[0]) {274sync[0].notifyAll();275}276277for (i = 0; i < garb.length; i++)278garb[i].start();279for (i = 0; i < irupt.length; i++)280irupt[i].start();281282if (DEBUG) System.out.println("Cycles=" + nCycles);283for (iCycle = 0; iCycle < nCycles && !done && JNIter005.passed(); iCycle++) {284System.out.print("Cycle: " + iCycle);285try {286sleep(interval);287} catch (InterruptedException e) {288}289synchronized (sync[1]) {290System.out.println(291" Interrupt count=" + JNIter005.getInterruptCount() +292" Native interrupt count=" + JNIter005.CountException);293}294// JNIter005.clearCount();295synchronized (sync[0]) {296sync[0].notifyAll();297}298int n = 0;299for (i = 0; i < jniter.length; i++)300if (jniter[i].finished()) n++;301if (n == jniter.length) break;302}303if (JNIter005.passed())304System.out.println("JNI TEST PASSED");305else306System.out.println("JNI TEST FAILED");307for (i = 0; i < irupt.length; i++)308irupt[i].halt();309for (i = 0; i < garb.length; i++)310garb[i].halt();311for (i = 0; i < jniter.length; i++)312jniter[i].halt();313/* Flush any waiters */314if (DEBUG) System.out.println("jnistress005::run(): before sync[0]");315synchronized (sync[0]) {316sync[0].notifyAll();317}318if (DEBUG) System.out.println("jnistress005::run(): after sync[0]");319for (i = 0; i < irupt.length; i++) {320try {321irupt[i].join();322} catch (InterruptedException e) {323}324}325if (DEBUG) System.out.println("jnistress005::run(): X");326for (i = 0; i < garb.length; i++) {327try {328garb[i].join();329} catch (InterruptedException e) {330}331}332if (DEBUG) System.out.println("jnistress005::run(): Y");333synchronized (sync[0]) {334sync[0].notifyAll();335}336for (i = 0; i < jniter.length; i++) {337try {338if (jniter[i].isAlive()) {339jniter[i].join();340}341} catch (InterruptedException e) {342}343}344if (DEBUG) System.out.println("jnistress005::run(): Z");345} catch (Throwable e) {346Debug.Fail(e);347}348}349350public void halt() {351done = true;352}353354public boolean finished() {355return done;356}357358long nCycles = 0;359JNIter005[] jniter;360static Synchronizer[] sync;361private int interval = 100;362Interrupter[] irupt;363GarbageGenerator[] garb;364private boolean done = false;365final private static boolean DEBUG = false;366}367368class JNIter005 extends Thread {369370// The native methods for testing JNI exception calls371public native void except(Throwable tobj);372373// public native int count();374static {375System.loadLibrary("jnistress005");376}377378Exception nobj = new Exception();379static int CountException = 0;380static int counts = 0;381382public JNIter005(Synchronizer[] aSync) {383sync = aSync;384}385386public void run() {387try {388char[] Sum;389int iter = 0;390391/* Synchronize start of work */392incCount();393synchronized (sync[0]) {394try {395sync[0].wait();396} catch (InterruptedException e) {397}398}399while (!done && pass) {400try {401/* Synchronized the JNI stressing */402synchronized (sync[2]) {403incCount();404}405synchronized (sync[0]) {406try {407sync[0].wait();408} catch (InterruptedException e) {409synchronized (sync[1]) {410JNIter005.incInterruptCount();411}412}413}414synchronized (sync[0]) {415if (CountException < jnistress005.jniStringAllocSize) {416try {417except(nobj);418} catch (Exception e) {419if ((CountException % 1000) == 0)420System.out.println("JAVA: " + e);421System.out.println("Here");422System.out.println("counts " + counts +423" CountException " + CountException);424425++CountException;426}427} else428// if (CountException==counts) halt();429if (CountException == jnistress005.jniStringAllocSize) halt();430}431if (DEBUG)432System.out.println("We have " + activeCount() + " threads now.");433synchronized (this) {434try {435wait(1L);436} catch (InterruptedException e) {437throw new InterruptedException();438}439}440} catch (Exception e) {441synchronized (sync[1]) {442JNIter005.incInterruptCount();443}444}445iter++;446iter = iter % CASECOUNT;447}448if (DEBUG) System.out.println("JNITer::run(): done=" + done);449done = true;450if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter005.pass);451if (DEBUG) System.out.println("JNIter005::run(): done");452} catch (Throwable e) {453Debug.Fail(e);454}455}456457public synchronized static void incCount() {458count++;459}460461public static int getCount() {462return count;463}464465public synchronized static void clearCount() {466count = 0;467}468469private synchronized static void incInterruptCount() {470interruptCount++;471}472473public static int getInterruptCount() {474return interruptCount;475}476477public synchronized static void clearInterruptCount() {478interruptCount = 0;479}480481public void halt() {482done = true;483}484485public boolean finished() {486return done;487}488489public static boolean passed() {490return pass;491}492493public static void setpass(boolean value) {494pass = value;495}496497Synchronizer[] sync;498private static int count = 0;499private static int interruptCount = 0;500private boolean done = false;501private static boolean pass = true;502final private static int CASECOUNT = 2;503final private static boolean DEBUG = false;504}505506507