Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress001.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* jnistress001 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/jnistress001.43* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]44*45* @library /vmTestbase46* /test/lib47* @run main/othervm/native48* nsk.stress.jni.jnistress00149* -numTHREADer 2050* -threadInterval 20051* -numInterrupter 252* -interruptInterval 50053* -numGarbage 8054* -garbageInterval 555* -numIteration 20056*/5758package nsk.stress.jni;5960import nsk.share.Consts;61import nsk.share.Debug;62import nsk.share.test.StressOptions;6364public class jnistress001 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// Size of string's array in native method84static int jniStringAllocSize = 1000;85// Print period for native method86static int printPeriod = 200;8788private static StressOptions stressOptions;8990public static void main(String[] argv) {91try {92int i = 0;93int nJNISync = 10;94jnistress001 dm = null;95boolean errArg = false;9697stressOptions = new StressOptions(argv);9899/* Process arguments */100while (!errArg && i < argv.length) {101/* Number of iterations. Ignored if <= 0. */102if (i < argv.length && argv[i].equals("-numIteration")) {103++i;104if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {105try {106numIteration = Long.parseLong(argv[i++]);107} catch (NumberFormatException e) {108errArg = true;109}110}111} else if (i < argv.length && argv[i].equals("-numTHREADer")) {112++i;113if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {114try {115numJNIter = Integer.parseInt(argv[i++]);116} catch (NumberFormatException e) {117errArg = true;118}119if (numJNIter <= 0) errArg = true;120}121} else if (i < argv.length && argv[i].equals("-threadInterval")) {122++i;123if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {124try {125jniInterval = Integer.parseInt(argv[i++]);126} catch (NumberFormatException e) {127errArg = true;128}129}130} else if (i < argv.length && argv[i].equals("-numInterrupter")) {131++i;132if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {133try {134numInterrupter = Integer.parseInt(argv[i++]);135} catch (NumberFormatException e) {136errArg = true;137}138}139} else if (i < argv.length && argv[i].equals("-interruptInterval")) {140++i;141if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {142try {143interruptInterval = Integer.parseInt(argv[i++]);144} catch (NumberFormatException e) {145errArg = true;146}147}148} else if (i < argv.length && argv[i].equals("-numGarbage")) {149++i;150if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {151try {152numGarbage = Integer.parseInt(argv[i++]);153} catch (NumberFormatException e) {154errArg = true;155}156}157} else if (i < argv.length && argv[i].equals("-garbageInterval")) {158++i;159if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {160try {161garbageInterval = Integer.parseInt(argv[i++]);162} catch (NumberFormatException e) {163errArg = true;164}165}166} else if (i < argv.length && argv[i].equals("-jniStringAllocSize")) {167++i;168if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {169try {170jniStringAllocSize = Integer.parseInt(argv[i++]);171} catch (NumberFormatException e) {172errArg = true;173}174}175} else if (i < argv.length && argv[i].equals("-printperiod")) {176++i;177if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {178try {179printPeriod = Integer.parseInt(argv[i++]);180} catch (NumberFormatException e) {181errArg = true;182}183}184} else if (i < argv.length && argv[i].startsWith("-stress")) {185++i;186if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {187++i;188}189} else System.out.println("Argument #" + i++ + " is incorrect");190191}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 jnistress001(numIteration, numJNIter, jniInterval,203numInterrupter, interruptInterval, numGarbage, garbageInterval);204dm.start();205206try {207dm.join(timeOut);208} catch (InterruptedException e) {209System.out.println("TESTER THREAD WAS INTERRUPTED");210System.exit(Consts.TEST_FAILED);211}212213if (DEBUG) System.out.println("jnistress001::main(): halt!");214215if (dm.isAlive()) {216System.out.println("TIME LIMIT EXCEEDED");217dm.halt();218if (DEBUG) System.out.println("jnistress001::main(): join!");219try {220dm.join(10000L);221} catch (InterruptedException e) {222System.out.println("TESTER THREAD WAS INTERRUPTED");223System.exit(Consts.TEST_FAILED);224}225} else {226System.out.println("TESTER THREAD FINISHED");227}228229if (DEBUG) System.out.println("jnistress001::main(): zzzz...");230231if (!JNIter001.passed())232System.exit(Consts.TEST_FAILED);233234} catch (Throwable e) {235Debug.Fail(e);236}237}238239jnistress001(240long iters,241int nJNI,242int jniInterval,243int nInter,244int iruptInterval,245int nGarb,246int garbInterval247) {248int i = 0;249nCycles = iters;250/* Should have at least one of nCycles>0 */251if (nCycles <= 0) nCycles = Long.MAX_VALUE;252jniter = new JNIter001[nJNI];253interval = jniInterval;254irupt = new Interrupter[nInter];255garb = new GarbageGenerator[nGarb];256for (i = 0; i < nJNI; i++)257jniter[i] = new JNIter001(sync);258for (i = 0; i < nInter; i++) {259irupt[i] = new Interrupter(jniter, sync);260irupt[i].setInterval(iruptInterval);261}262for (i = 0; i < nGarb; i++) {263garb[i] = new GarbageGenerator();264garb[i].setInterval(garbInterval);265}266}267268public void run() {269int i = 0;270long iCycle = 0L;271JNIter001.clearCount();272JNIter001.clearInterruptCount();273for (i = 0; i < jniter.length; i++)274jniter[i].start();275276while (JNIter001.getCount() < jniter.length) {277try {278sleep(100);279} catch (InterruptedException e) {280}281}282JNIter001.clearCount();283// JNIter001.clearInterruptCount();284synchronized (sync[0]) {285sync[0].notifyAll();286}287288for (i = 0; i < garb.length; i++)289garb[i].start();290for (i = 0; i < irupt.length; i++)291irupt[i].start();292293if (DEBUG) System.out.println("Cycles=" + nCycles);294for (iCycle = 0; iCycle < nCycles && !done && JNIter001.passed(); iCycle++) {295System.out.print("Cycle: " + iCycle);296try {297sleep(interval);298} catch (InterruptedException e) {299}300synchronized (sync[1]) {301System.out.println(" Interrupt count=" +302JNIter001.getInterruptCount());303}304JNIter001.clearCount();305synchronized (sync[0]) {306sync[0].notifyAll();307}308int n = 0;309for (i = 0; i < jniter.length; i++)310if (jniter[i].finished()) n++;311if (n == jniter.length) break;312}313if (JNIter001.passed()) { /* Use of setpass was backwards */314System.out.println("JNI TEST PASSED");315} else {316System.out.println("JNI TEST FAILED");317}318for (i = 0; i < irupt.length; i++)319irupt[i].halt();320for (i = 0; i < garb.length; i++)321garb[i].halt();322for (i = 0; i < jniter.length; i++)323jniter[i].halt();324/* Flush any waiters */325if (DEBUG) System.out.println("jnistress001::run(): before sync[0]");326synchronized (sync[0]) {327sync[0].notifyAll();328}329if (DEBUG) System.out.println("jnistress001::run(): after sync[0]");330for (i = 0; i < irupt.length; i++) {331try {332irupt[i].join();333} catch (InterruptedException e) {334}335}336if (DEBUG) System.out.println("jnistress001::run(): X");337for (i = 0; i < garb.length; i++) {338try {339garb[i].join();340} catch (InterruptedException e) {341}342}343if (DEBUG) System.out.println("jnistress001::run(): Y");344System.out.println("jniter.length is " + jniter.length);345for (i = 0; i < jniter.length; i++) {346try {347if (jniter[i].isAlive()) {348jniter[i].join();349}350} catch (InterruptedException e) {351}352}353if (DEBUG) System.out.println("jnistress001::run(): Z");354}355356public void halt() {357done = true;358}359360public boolean finished() {361return done;362}363364long nCycles = 0;365JNIter001[] jniter;366static Synchronizer[] sync;367private int interval = 100;368Interrupter[] irupt;369GarbageGenerator[] garb;370private boolean done = false;371final private static boolean DEBUG = false;372}373374class JNIter001 extends Thread {375376// The native method for testing JNI UTF-8 calls377public native String jnistress(String threadName, int nstr, int printPeriod);378379// The native method for testing JNI Unicode calls380public native String jnistress1(String threadName, int nstr, int printPeriod);381382static {383System.loadLibrary("jnistress001");384}385386public JNIter001(Synchronizer[] aSync) {387sync = aSync;388}389390public void run() {391int iter = 0;392393/* Synchronize start of work */394incCount();395synchronized (sync[0]) {396try {397sync[0].wait();398} catch (InterruptedException e) {399}400}401while (!done && pass) {402try {403/* Synchronized the JNI stressing */404synchronized (sync[2]) {405incCount();406}407synchronized (sync[0]) {408try {409sync[0].wait();410} catch (InterruptedException e) {411synchronized (sync[1]) {412JNIter001.incInterruptCount();413}414}415}416synchronized (sync[0]) {417String s1 = jnistress(getName(),418jnistress001.jniStringAllocSize,419jnistress001.printPeriod);420String s2 = jnistress1(getName(),421jnistress001.jniStringAllocSize,422jnistress001.printPeriod);423if (!s1.equals(s2))424System.out.println("Wrong compare in thread " + getName());425}426if (DEBUG)427System.out.println("We have " + activeCount() +428" threads now.");429} catch (Exception e) {430synchronized (sync[1]) {431JNIter001.incInterruptCount();432}433}434iter++;435iter = iter % CASECOUNT;436}437if (DEBUG) System.out.println("JNITer::run(): done=" + done);438done = true;439if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter001.pass);440if (DEBUG) System.out.println("JNITer::run(): done");441}442443private synchronized static void incCount() {444count++;445}446447public static int getCount() {448return count;449}450451public synchronized static void clearCount() {452count = 0;453}454455private synchronized static void incInterruptCount() {456interruptCount++;457}458459public static int getInterruptCount() {460return interruptCount;461}462463public synchronized static void clearInterruptCount() {464interruptCount = 0;465}466467public static void halt() {468done = true;469}470471public boolean finished() {472return done;473}474475public static boolean passed() {476return pass;477}478479public static void setpass(boolean value) {480pass = value;481}482483Synchronizer[] sync;484private static int count = 0;485private static int interruptCount = 0;486private static boolean done = false;487private static boolean pass = true;488final private static int CASECOUNT = 2;489final private static boolean DEBUG = false;490}491492493