Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress007.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* jnistress007 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/jnistress007.43* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]44*45* @library /vmTestbase46* /test/lib47* @run main/othervm/native48* nsk.stress.jni.jnistress00749* -numTHREADer 1050* -threadInterval 2051* -numInterrupter 252* -interruptInterval 5053* -numGarbage 8054* -garbageInterval 555* -numIteration 13056*/5758package nsk.stress.jni;5960import nsk.share.Consts;61import nsk.share.Debug;62import nsk.share.test.StressOptions;6364public class jnistress007 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 = 1000;79/* Number of garbage generating threads */80static int numGarbage = 1;81/* Time between garbage allocations in milliseconds */82static int garbageInterval = 1000;83// The MAX quantity of monitor's call84static int jniStringAllocSize = 300;8586private static StressOptions stressOptions;8788public static void main(String[] argv) {89try {90int i = 0;91int nJNISync = 10;92jnistress007 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 jnistress007(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("jnistress007::main(): halt!");202203if (dm.isAlive()) {204System.out.println("TIME LIMIT EXCEEDED");205dm.halt();206if (DEBUG) System.out.println("jnistress007::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("jnistress007::main(): zzzz...");218219if (JNIter007.passed())220System.exit(Consts.TEST_FAILED);221222} catch (Throwable e) {223Debug.Fail(e);224}225}226227jnistress007(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 JNIter007[nJNI];241interval = jniInterval;242irupt = new Interrupter[nInter];243garb = new GarbageGenerator[nGarb];244for (i = 0; i < nJNI; i++)245jniter[i] = new JNIter007(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;260JNIter007.clearCount();261JNIter007.clearInterruptCount();262for (i = 0; i < jniter.length; i++)263jniter[i].start();264265while (JNIter007.getCount() < jniter.length) {266try {267sleep(100);268} catch (InterruptedException e) {269}270}271JNIter007.clearCount();272// JNIter007.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 && !JNIter007.passed(); iCycle++) {284System.out.print("Cycle: " + iCycle);285try {286sleep(interval);287} catch (InterruptedException e) {288}289synchronized (sync[1]) {290System.out.println("\tInterrupt count=" +291JNIter007.getInterruptCount());292}293JNIter007.clearCount();294synchronized (sync[0]) {295sync[0].notifyAll();296}297int n = 0;298for (i = 0; i < jniter.length; i++)299if (jniter[i].finished()) n++;300if (n == jniter.length) break;301}302for (i = 0; i < irupt.length; i++)303irupt[i].halt();304for (i = 0; i < garb.length; i++)305garb[i].halt();306for (i = 0; i < jniter.length; i++)307jniter[i].halt();308/* Flush any waiters */309if (DEBUG) System.out.println("jnistress007::run(): before sync[0]");310synchronized (sync[0]) {311sync[0].notifyAll();312}313if (DEBUG) System.out.println("jnistress007::run(): after sync[0]");314for (i = 0; i < irupt.length; i++) {315try {316irupt[i].join();317} catch (InterruptedException e) {318}319}320if (DEBUG) System.out.println("jnistress007::run(): X");321for (i = 0; i < garb.length; i++) {322try {323garb[i].join();324} catch (InterruptedException e) {325}326}327if (DEBUG) System.out.println("jnistress007::run(): Y");328synchronized (sync[0]) {329sync[0].notifyAll();330}331for (i = 0; i < jniter.length; i++) {332try {333jniter[i].join();334} catch (InterruptedException e) {335}336}337if (JNIter007.passed()) {338System.out.println("JNI TEST PASSED");339} else {340System.out.println("JNI TEST FAILED");341}342if (DEBUG) System.out.println("jnistress007::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;357JNIter007[] jniter;358static Synchronizer[] sync;359private int interval = 100;360Interrupter[] irupt;361GarbageGenerator[] garb;362private boolean done = false;363final private static boolean DEBUG = false;364}365366class JNIter007 extends Thread {367368// The native methods for testing JNI monitors calls369public native void incCount(String name);370371static {372System.loadLibrary("jnistress007");373}374375public static int nativeCount, javaCount = 0;376377public JNIter007(Synchronizer[] aSync) {378sync = aSync;379}380381public void run() {382try {383int iter = 0;384385/* Synchronize start of work */386incCount();387synchronized (sync[0]) {388try {389sync[0].wait();390} catch (InterruptedException e) {391}392}393while (!done && !pass) {394try {395/* Synchronized the JNI stressing */396synchronized (sync[2]) {397incCount();398}399synchronized (sync[0]) {400try {401sync[0].wait();402} catch (InterruptedException e) {403synchronized (sync[1]) {404JNIter007.incInterruptCount();405}406}407}408synchronized (sync[0]) {409try {410if ((javaCount < jnistress007.jniStringAllocSize) &&411(nativeCount < jnistress007.jniStringAllocSize)) {412javaCount++;413incCount(getName());414if ((javaCount % 1000) == 0)415System.out.println("Count in java " +416getName() + " " + javaCount);417} else if (javaCount == nativeCount) {418done = true;419} else {420System.out.println("Native: " + nativeCount +421"\t" + "Java: " + javaCount);422pass = true;423}424} catch (Exception e) {425System.out.println("Error: " + e);426}427}428if (DEBUG)429System.out.println("We have " + activeCount() + " threads now.");430synchronized (this) {431try {432wait(1L);433} catch (InterruptedException e) {434throw new InterruptedException();435}436}437} catch (Exception e) {438synchronized (sync[1]) {439JNIter007.incInterruptCount();440}441}442iter++;443iter = iter % CASECOUNT;444}445if (DEBUG) System.out.println("JNITer::run(): done=" + done);446done = true;447if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter007.pass);448if (DEBUG) System.out.println("JNIter::run(): done");449} catch (Throwable e) {450Debug.Fail(e);451}452}453454private synchronized static void incCount() {455count++;456}457458public static int getCount() {459return count;460}461462public synchronized static void clearCount() {463count = 0;464}465466private synchronized static void incInterruptCount() {467interruptCount++;468}469470public static int getInterruptCount() {471return interruptCount;472}473474public synchronized static void clearInterruptCount() {475interruptCount = 0;476}477478public void halt() {479done = true;480}481482public boolean finished() {483return done;484}485486public static boolean passed() {487return pass;488}489490Synchronizer[] sync;491private static int count = 0;492private static int interruptCount = 0;493private boolean done = false;494private static boolean pass = false;495final private static int CASECOUNT = 2;496final private static boolean DEBUG = false;497}498499500