Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress006.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* jnistress006 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/jnistress006.43* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]44*45* @library /vmTestbase46* /test/lib47* @run main/othervm/native48* nsk.stress.jni.jnistress00649* -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 jnistress006 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 global refs84static int jniStringAllocSize = 30000;8586private static StressOptions stressOptions;8788public static void main(String[] argv) {89try {90int i = 0;91int nJNISync = 10;92jnistress006 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 jnistress006(numIteration, numJNIter, jniInterval,191numInterrupter, interruptInterval, numGarbage, garbageInterval);192193dm.start();194195try {196dm.join(timeOut);197} catch (InterruptedException e) {198System.out.println("TESTER THREAD WAS INTERRUPTED");199System.exit(Consts.TEST_FAILED);200}201202if (DEBUG) System.out.println("jnistress006::main(): halt!");203204if (dm.isAlive()) {205System.out.println("TIME LIMIT EXCEEDED");206dm.halt();207if (DEBUG) System.out.println("jnistress006::main(): join!");208try {209dm.join(10000L);210} catch (InterruptedException e) {211System.out.println("TESTER THREAD WAS INTERRUPTED");212System.exit(Consts.TEST_FAILED);213}214} else {215System.out.println("TESTER THREAD FINISHED");216}217218if (DEBUG) System.out.println("jnistress006::main(): zzzz...");219220if (!JNIter006.passed())221System.exit(Consts.TEST_FAILED);222223} catch (Throwable e) {224Debug.Fail(e);225}226}227228jnistress006(229long iters,230int nJNI,231int jniInterval,232int nInter,233int iruptInterval,234int nGarb,235int garbInterval236) {237int i = 0;238nCycles = iters;239/* Should have at least one of nCycles>0 */240if (nCycles <= 0) nCycles = Long.MAX_VALUE;241jniter = new JNIter006[nJNI];242interval = jniInterval;243irupt = new Interrupter[nInter];244garb = new GarbageGenerator[nGarb];245for (i = 0; i < nJNI; i++)246jniter[i] = new JNIter006(sync);247for (i = 0; i < nInter; i++) {248irupt[i] = new Interrupter(jniter, sync);249irupt[i].setInterval(iruptInterval);250}251for (i = 0; i < nGarb; i++) {252garb[i] = new GarbageGenerator();253garb[i].setInterval(garbInterval);254}255}256257public void run() {258try {259int i = 0;260long iCycle = 0L;261JNIter006.clearCount();262JNIter006.clearInterruptCount();263for (i = 0; i < jniter.length; i++)264jniter[i].start();265266while (JNIter006.getCount() < jniter.length) {267try {268sleep(100);269} catch (InterruptedException e) {270}271}272JNIter006.clearCount();273// JNIter006.clearInterruptCount();274synchronized (sync[0]) {275sync[0].notifyAll();276}277278for (i = 0; i < garb.length; i++)279garb[i].start();280for (i = 0; i < irupt.length; i++)281irupt[i].start();282283if (DEBUG) System.out.println("Cycles=" + nCycles);284for (iCycle = 0; iCycle < nCycles && !done && JNIter006.passed(); iCycle++) {285System.out.print("Cycle: " + iCycle);286try {287sleep(interval);288} catch (InterruptedException e) {289}290synchronized (sync[1]) {291System.out.println(" Interrupt count=" +292JNIter006.getInterruptCount());293}294JNIter006.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 (JNIter006.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("jnistress006::run(): before sync[0]");315synchronized (sync[0]) {316sync[0].notifyAll();317}318if (DEBUG) System.out.println("jnistress006::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("jnistress006::run(): X");326for (i = 0; i < garb.length; i++) {327try {328garb[i].join();329} catch (InterruptedException e) {330}331}332if (DEBUG) System.out.println("jnistress006::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("jnistress006::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;359JNIter006[] jniter;360static Synchronizer[] sync;361private int interval = 100;362Interrupter[] irupt;363GarbageGenerator[] garb;364private boolean done = false;365final private static boolean DEBUG = false;366}367368class JNIter006 extends Thread {369370// The native methods for testing JNI exception calls371public native boolean refs(Object tobj, int jniStringAllocSize);372373static {374System.loadLibrary("jnistress006");375}376377Referenced tobj = new Referenced();378static int CountRefs;379380public JNIter006(Synchronizer[] aSync) {381sync = aSync;382}383384public void run() {385try {386int iter = 0;387388/* Synchronize start of work */389incCount();390synchronized (sync[0]) {391try {392sync[0].wait();393} catch (InterruptedException e) {394}395}396while (!done && pass) {397try {398/* Synchronized the JNI stressing */399synchronized (sync[2]) {400incCount();401}402synchronized (sync[0]) {403try {404sync[0].wait();405} catch (InterruptedException e) {406synchronized (sync[1]) {407JNIter006.incInterruptCount();408}409}410}411synchronized (sync[0]) {412try {413tobj.set_i(123456);414pass = refs(tobj, jnistress006.jniStringAllocSize);415} catch (Exception e) {416System.out.println("Error: " + e);417}418}419if (DEBUG)420System.out.println("We have " + activeCount() + " threads now.");421synchronized (this) {422try {423wait(1L);424} catch (InterruptedException e) {425throw new InterruptedException();426}427}428} catch (Exception e) {429synchronized (sync[1]) {430JNIter006.incInterruptCount();431}432}433iter++;434iter = iter % CASECOUNT;435}436if (DEBUG) System.out.println("JNITer::run(): done=" + done);437done = true;438if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter006.pass);439if (DEBUG) System.out.println("JNIter006::run(): done");440} catch (Throwable e) {441Debug.Fail(e);442}443}444445private synchronized static void incCount() {446count++;447}448449public static int getCount() {450return count;451}452453public synchronized static void clearCount() {454count = 0;455}456457private synchronized static void incInterruptCount() {458interruptCount++;459}460461public static int getInterruptCount() {462return interruptCount;463}464465public synchronized static void clearInterruptCount() {466interruptCount = 0;467}468469public void halt() {470done = true;471}472473public boolean finished() {474return done;475}476477public static boolean passed() {478return pass;479}480481Synchronizer[] sync;482private static int count = 0;483private static int interruptCount = 0;484private static boolean done = false;485private static boolean pass = true;486final private static int CASECOUNT = 2;487final private static boolean DEBUG = false;488}489490class Referenced {491private static int i;492493public static void set_i(int value) {494i = value;495}496497public static int get_i() {498return i;499}500}501502503