Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/gclocker/gcl001.java
41161 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*/222324/*25* @test26* @key stress randomness27*28* @summary converted from VM testbase nsk/stress/jni/gclocker/gcl001.29* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]30* VM testbase readme:31* DESCRIPTION32* Check compatibility of GC Locker improvements with JNI CS33* Two types of concurrent threads are present:34* A) Create N 'JNI CS' threads. Each of them will:35* 1. Create primitive array and string with random data36* 2. Pass it to native method37* 3. Sort/Hash data in JNI CS mixing string and array critical sections38* 4. Return from native39* 5. Check data to be processed correctly40* B) Create M ' Garbage producer/memory allocation' threads. Each of them will:41* 1. Allocate memory blocks and make them garbage.42* 2. Check for OOM errors.43*44* @library /vmTestbase45* /test/lib46* @run main/othervm/native/timeout=48047* -XX:-UseGCOverheadLimit48* nsk.stress.jni.gclocker.gcl00149* -stressThreadsFactor 850*/5152package nsk.stress.jni.gclocker;5354import nsk.share.gc.GC;55import nsk.share.gc.ThreadedGCTest;56import nsk.share.gc.gp.array.BooleanArrayProducer;57import nsk.share.gc.gp.array.ByteArrayProducer;58import nsk.share.gc.gp.array.CharArrayProducer;59import nsk.share.gc.gp.array.DoubleArrayProducer;60import nsk.share.gc.gp.array.FloatArrayProducer;61import nsk.share.gc.gp.array.IntArrayProducer;62import nsk.share.gc.gp.array.LongArrayProducer;63import nsk.share.gc.gp.array.ShortArrayProducer;64import nsk.share.test.ExecutionController;65import nsk.share.test.LocalRandom;6667public class gcl001 extends ThreadedGCTest {6869static {70System.loadLibrary("gcl001");71}7273int maxBlockSize;7475public static void main(String[] args) {76GC.runTest(new gcl001(), args);77}7879@Override80public void run() {81// each thread have only one block at the time82// so we should occupy less then 60% of heap with live objects83long maxSize = runParams.getTestMemory() / runParams.getNumberOfThreads();84if (maxSize > Integer.MAX_VALUE - 1) {85maxSize = Integer.MAX_VALUE - 1;86}87maxBlockSize = (int) maxSize;88log.info("Maximium block size = " + maxBlockSize);89super.run();90}9192@Override93protected Runnable createRunnable(int i) {94if (i < runParams.getNumberOfThreads() / 2) {95return CreateJNIWorker(i, maxBlockSize);96} else {97return new GarbageProducer(maxBlockSize);98}99}100101public Runnable CreateJNIWorker(int number, int size) {102JNIAbstractWorker worker = null;103switch (number % 8) {104case 0:105worker = new JNIBooleanWorker(size);106break;107case 1:108worker = new JNIByteWorker(size);109break;110case 2:111worker = new JNICharWorker(size);112break;113case 3:114worker = new JNIShortWorker(size);115break;116case 4:117worker = new JNIIntWorker(size);118break;119case 5:120worker = new JNILongWorker(size);121break;122case 6:123worker = new JNIFloatWorker(size);124break;125case 7:126worker = new JNIDoubleWorker(size);127break;128}129return worker;130}131132int random(int maxSize) {133int res = LocalRandom.nextInt(maxSize);134return res > 128 ? res : 128;135}136137public static Object tmp;138139class GarbageProducer implements Runnable {140141private int maxSize;142ExecutionController stresser;143ByteArrayProducer bp;144145GarbageProducer(int size) {146this.maxSize = size;147bp = new ByteArrayProducer();148}149150public void run() {151if (stresser == null) {152stresser = getExecutionController();153}154155while (stresser.continueExecution()) {156try {157byte[] arr = bp.create(random(maxSize));158tmp = arr;159} catch (OutOfMemoryError oome) {160// Do nothing.161}162}163}164}165166abstract class JNIAbstractWorker extends JNIWorker implements Runnable {167168ExecutionController stresser;169private int maxSize;170171public JNIAbstractWorker(int maxSize) {172this.maxSize = maxSize;173}174175public void check(boolean condition, String message) {176if (!condition) {177throw new Error(message);178}179}180181abstract void doit(int size);182183public void run() {184// create array with random elements185// store min and max element for further check186// create new string187// call JNI methods188// check min/max as sort result189// check string hash190191if (stresser == null) {192stresser = getExecutionController();193}194while (stresser.continueExecution()) {195// let make at least several values for long/float196try {197doit(random(maxSize));198} catch (OutOfMemoryError oome) {199// Do nothing.200}201}202}203}204205// BYTE206class JNIBooleanWorker extends JNIAbstractWorker {207208BooleanArrayProducer gp = new BooleanArrayProducer();209210public JNIBooleanWorker(int size) {211super(size);212}213214void doit(int size) {215216boolean[] array = gp.create(size);217// just to be sure that we have true & false218array[0] = true;219array[array.length - 1] = false;220String str = "unsupported";221int nativeHash = NativeCall(array, str);222int javaHash = 0;223for (int i = 0; i < str.length(); ++i) {224javaHash += (int) str.charAt(i);225}226javaHash += javaHash;227check(array[0] == false && array[array.length - 1] == true228&& javaHash == nativeHash, "Data validation failure");229230}231}232233class JNIByteWorker extends JNIAbstractWorker {234235ByteArrayProducer gp = new ByteArrayProducer();236237public JNIByteWorker(int size) {238super(size);239}240241void doit(int size) {242243byte[] array = gp.create(size);244byte min = Byte.MAX_VALUE, max = Byte.MIN_VALUE;245for (int i = 0; i < array.length; ++i) {246if (array[i] > max) {247max = array[i];248}249250if (array[i] < min) {251min = array[i];252}253}254String str = "Min: " + min + " Max: " + max;255int nativeHash = NativeCall(array, str);256int javaHash = 0;257for (int i = 0; i < str.length(); ++i) {258javaHash += (int) str.charAt(i);259}260javaHash += javaHash;261check(array[0] == min && array[array.length - 1] == max262&& javaHash == nativeHash, "Data validation failure");263264}265}266267// CHAR268class JNICharWorker extends JNIAbstractWorker {269270CharArrayProducer gp = new CharArrayProducer();271272public JNICharWorker(int size) {273super(size);274}275276void doit(int size) {277char[] array = gp.create(size);278char min = 0xffff, max = 0;279for (int i = 0; i < array.length; ++i) {280array[i] = (char) LocalRandom.nextInt();281if (array[i] > max) {282max = array[i];283}284285if (array[i] < min) {286min = array[i];287}288}289String str = "Min: " + min + " Max: " + max;290int nativeHash = NativeCall(array, str);291int javaHash = 0;292for (int i = 0; i < str.length(); ++i) {293javaHash += (int) str.charAt(i);294}295javaHash += javaHash;296check(array[0] == min && array[array.length - 1] == max297&& javaHash == nativeHash, "Data validation failure");298299}300}301302// SHORT303class JNIShortWorker extends JNIAbstractWorker {304305ShortArrayProducer gp = new ShortArrayProducer();306307public JNIShortWorker(int size) {308super(size);309}310311void doit(int size) {312313short[] array = gp.create(size);314short min = Short.MAX_VALUE, max = Short.MIN_VALUE;315for (int i = 0; i < array.length; ++i) {316if (array[i] > max) {317max = array[i];318}319320if (array[i] < min) {321min = array[i];322}323}324String str = "Min: " + min + " Max: " + max;325int nativeHash = NativeCall(array, str);326int javaHash = 0;327for (int i = 0; i < str.length(); ++i) {328javaHash += (int) str.charAt(i);329}330javaHash += javaHash;331check(array[0] == min && array[array.length - 1] == max332&& javaHash == nativeHash, "Data validation failure");333}334}335336// INT337class JNIIntWorker extends JNIAbstractWorker {338339IntArrayProducer gp = new IntArrayProducer();340341public JNIIntWorker(int size) {342super(size);343}344345void doit(int size) {346int[] array = gp.create(size);347int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;348for (int i = 0; i < array.length; ++i) {349if (array[i] > max) {350max = array[i];351}352353if (array[i] < min) {354min = array[i];355}356}357String str = "Min: " + min + " Max: " + max;358int nativeHash = NativeCall(array, str);359int javaHash = 0;360for (int i = 0; i < str.length(); ++i) {361javaHash += (int) str.charAt(i);362}363javaHash += javaHash;364check(array[0] == min && array[array.length - 1] == max365&& javaHash == nativeHash, "Data validation failure");366367}368}369370// LONG371class JNILongWorker extends JNIAbstractWorker {372373LongArrayProducer gp = new LongArrayProducer();374375public JNILongWorker(int size) {376super(size);377}378379void doit(int size) {380381long[] array = gp.create(size);382long min = Long.MAX_VALUE, max = Long.MIN_VALUE;383for (int i = 0; i < array.length; ++i) {384if (array[i] > max) {385max = array[i];386}387388if (array[i] < min) {389min = array[i];390}391}392String str = "Min: " + min + " Max: " + max;393int nativeHash = NativeCall(array, str);394int javaHash = 0;395for (int i = 0; i < str.length(); ++i) {396javaHash += (int) str.charAt(i);397}398javaHash += javaHash;399check(array[0] == min && array[array.length - 1] == max400&& javaHash == nativeHash, "Data validation failure");401402}403}404405// FLOAT406class JNIFloatWorker extends JNIAbstractWorker {407408FloatArrayProducer gp = new FloatArrayProducer();409410public JNIFloatWorker(int size) {411super(size);412}413414void doit(int size) {415416float[] array = gp.create(size);417float min = Float.MAX_VALUE, max = Float.MIN_VALUE;418for (int i = 0; i < array.length; ++i) {419if (array[i] > max) {420max = array[i];421}422423if (array[i] < min) {424min = array[i];425}426}427String str = "Min: " + min + " Max: " + max;428int nativeHash = NativeCall(array, str);429int javaHash = 0;430for (int i = 0; i < str.length(); ++i) {431javaHash += (int) str.charAt(i);432}433javaHash += javaHash;434check(array[0] == min && array[array.length - 1] == max435&& javaHash == nativeHash, "Data validation failure");436}437}438439// DOUBLE440class JNIDoubleWorker extends JNIAbstractWorker {441442DoubleArrayProducer gp = new DoubleArrayProducer();443444public JNIDoubleWorker(int size) {445super(size);446}447448void doit(int size) {449450double[] array = gp.create(size);451double min = Double.MAX_VALUE, max = Double.MIN_VALUE;452for (int i = 0; i < array.length; ++i) {453if (array[i] > max) {454max = array[i];455}456457if (array[i] < min) {458min = array[i];459}460}461String str = "Min: " + min + " Max: " + max;462int nativeHash = NativeCall(array, str);463int javaHash = 0;464for (int i = 0; i < str.length(); ++i) {465javaHash += (int) str.charAt(i);466}467javaHash += javaHash;468check(array[0] == min && array[array.length - 1] == max469&& javaHash == nativeHash, "Data validation failure");470}471}472}473474class JNIWorker {475476protected native int NativeCall(boolean[] array, String str);477478protected native int NativeCall(byte[] array, String str);479480protected native int NativeCall(char[] array, String str);481482protected native int NativeCall(short[] array, String str);483484protected native int NativeCall(int[] array, String str);485486protected native int NativeCall(long[] array, String str);487488protected native int NativeCall(float[] array, String str);489490protected native int NativeCall(double[] array, String str);491}492493494