< prev index next >

test/jdk/java/foreign/TestHandshake.java

Print this page

 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @modules jdk.incubator.foreign java.base/jdk.internal.vm.annotation java.base/jdk.internal.misc
 27  * @key randomness
 28  * @run testng/othervm TestHandshake
 29  * @run testng/othervm -Xint TestHandshake
 30  * @run testng/othervm -XX:TieredStopAtLevel=1 TestHandshake
 31  * @run testng/othervm -XX:-TieredCompilation TestHandshake
 32  */
 33 
 34 import jdk.incubator.foreign.MemoryAccess;
 35 import jdk.incubator.foreign.MemorySegment;
 36 
 37 import java.lang.invoke.MethodHandles;
 38 import java.lang.invoke.VarHandle;
 39 import java.nio.ByteBuffer;
 40 import java.nio.ByteOrder;
 41 import java.util.concurrent.ExecutorService;
 42 import java.util.concurrent.Executors;
 43 import java.util.concurrent.ThreadLocalRandom;
 44 import java.util.concurrent.TimeUnit;
 45 import java.util.concurrent.atomic.AtomicBoolean;
 46 import java.util.concurrent.atomic.AtomicLong;
 47 
 48 import jdk.incubator.foreign.ResourceScope;
 49 import org.testng.annotations.DataProvider;
 50 import org.testng.annotations.Test;


 51 import static org.testng.Assert.*;
 52 
 53 public class TestHandshake {
 54 
 55     static final int ITERATIONS = 5;
 56     static final int SEGMENT_SIZE = 1_000_000;
 57     static final int MAX_DELAY_MILLIS = 500;
 58     static final int MAX_EXECUTOR_WAIT_SECONDS = 20;
 59     static final int MAX_THREAD_SPIN_WAIT_MILLIS = 200;
 60 
 61     static final int NUM_ACCESSORS = Math.min(10, Runtime.getRuntime().availableProcessors());
 62 
 63     static final AtomicLong start = new AtomicLong();
 64     static final AtomicBoolean started = new AtomicBoolean();
 65 
 66     @Test(dataProvider = "accessors")
 67     public void testHandshake(String testName, AccessorFactory accessorFactory) throws InterruptedException {
 68         for (int it = 0 ; it < ITERATIONS ; it++) {
 69             ResourceScope scope = ResourceScope.newSharedScope();
 70             MemorySegment segment = MemorySegment.allocateNative(SEGMENT_SIZE, 1, scope);

133 
134     static abstract class AbstractBufferAccessor extends AbstractSegmentAccessor {
135         final ByteBuffer bb;
136 
137         AbstractBufferAccessor(int id, MemorySegment segment) {
138             super(id, segment);
139             this.bb = segment.asByteBuffer();
140         }
141     }
142 
143     static class SegmentAccessor extends AbstractSegmentAccessor {
144 
145         SegmentAccessor(int id, MemorySegment segment) {
146             super(id, segment);
147         }
148 
149         @Override
150         void doAccess() {
151             int sum = 0;
152             for (int i = 0; i < segment.byteSize(); i++) {
153                 sum += MemoryAccess.getByteAtOffset(segment, i);
154             }
155         }
156     }
157 
158     static class SegmentCopyAccessor extends AbstractSegmentAccessor {
159 
160         MemorySegment first, second;
161 
162 
163         SegmentCopyAccessor(int id, MemorySegment segment) {
164             super(id, segment);
165             long split = segment.byteSize() / 2;
166             first = segment.asSlice(0, split);
167             second = segment.asSlice(split);
168         }
169 
170         @Override
171         public void doAccess() {
172             first.copyFrom(second);
173         }

176     static class SegmentFillAccessor extends AbstractSegmentAccessor {
177 
178         SegmentFillAccessor(int id, MemorySegment segment) {
179             super(id, segment);
180         }
181 
182         @Override
183         public void doAccess() {
184             segment.fill((byte) ThreadLocalRandom.current().nextInt(10));
185         }
186     }
187 
188     static class SegmentMismatchAccessor extends AbstractSegmentAccessor {
189 
190         final MemorySegment copy;
191 
192         SegmentMismatchAccessor(int id, MemorySegment segment) {
193             super(id, segment);
194             this.copy = MemorySegment.allocateNative(SEGMENT_SIZE, 1, segment.scope());
195             copy.copyFrom(segment);
196             MemoryAccess.setByteAtOffset(copy, ThreadLocalRandom.current().nextInt(SEGMENT_SIZE), (byte)42);
197         }
198 
199         @Override
200         public void doAccess() {
201             segment.mismatch(copy);
202         }
203     }
204 
205     static class BufferAccessor extends AbstractBufferAccessor {
206 
207         BufferAccessor(int id, MemorySegment segment) {
208             super(id, segment);
209         }
210 
211         @Override
212         public void doAccess() {
213             int sum = 0;
214             for (int i = 0; i < bb.capacity(); i++) {
215                 sum += bb.get(i);
216             }

 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @modules jdk.incubator.foreign java.base/jdk.internal.vm.annotation java.base/jdk.internal.misc
 27  * @key randomness
 28  * @run testng/othervm TestHandshake
 29  * @run testng/othervm -Xint TestHandshake
 30  * @run testng/othervm -XX:TieredStopAtLevel=1 TestHandshake
 31  * @run testng/othervm -XX:-TieredCompilation TestHandshake
 32  */
 33 

 34 import jdk.incubator.foreign.MemorySegment;
 35 
 36 import java.lang.invoke.MethodHandles;
 37 import java.lang.invoke.VarHandle;
 38 import java.nio.ByteBuffer;
 39 import java.nio.ByteOrder;
 40 import java.util.concurrent.ExecutorService;
 41 import java.util.concurrent.Executors;
 42 import java.util.concurrent.ThreadLocalRandom;
 43 import java.util.concurrent.TimeUnit;
 44 import java.util.concurrent.atomic.AtomicBoolean;
 45 import java.util.concurrent.atomic.AtomicLong;
 46 
 47 import jdk.incubator.foreign.ResourceScope;
 48 import org.testng.annotations.DataProvider;
 49 import org.testng.annotations.Test;
 50 
 51 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 52 import static org.testng.Assert.*;
 53 
 54 public class TestHandshake {
 55 
 56     static final int ITERATIONS = 5;
 57     static final int SEGMENT_SIZE = 1_000_000;
 58     static final int MAX_DELAY_MILLIS = 500;
 59     static final int MAX_EXECUTOR_WAIT_SECONDS = 20;
 60     static final int MAX_THREAD_SPIN_WAIT_MILLIS = 200;
 61 
 62     static final int NUM_ACCESSORS = Math.min(10, Runtime.getRuntime().availableProcessors());
 63 
 64     static final AtomicLong start = new AtomicLong();
 65     static final AtomicBoolean started = new AtomicBoolean();
 66 
 67     @Test(dataProvider = "accessors")
 68     public void testHandshake(String testName, AccessorFactory accessorFactory) throws InterruptedException {
 69         for (int it = 0 ; it < ITERATIONS ; it++) {
 70             ResourceScope scope = ResourceScope.newSharedScope();
 71             MemorySegment segment = MemorySegment.allocateNative(SEGMENT_SIZE, 1, scope);

134 
135     static abstract class AbstractBufferAccessor extends AbstractSegmentAccessor {
136         final ByteBuffer bb;
137 
138         AbstractBufferAccessor(int id, MemorySegment segment) {
139             super(id, segment);
140             this.bb = segment.asByteBuffer();
141         }
142     }
143 
144     static class SegmentAccessor extends AbstractSegmentAccessor {
145 
146         SegmentAccessor(int id, MemorySegment segment) {
147             super(id, segment);
148         }
149 
150         @Override
151         void doAccess() {
152             int sum = 0;
153             for (int i = 0; i < segment.byteSize(); i++) {
154                 sum += segment.get(JAVA_BYTE, i);
155             }
156         }
157     }
158 
159     static class SegmentCopyAccessor extends AbstractSegmentAccessor {
160 
161         MemorySegment first, second;
162 
163 
164         SegmentCopyAccessor(int id, MemorySegment segment) {
165             super(id, segment);
166             long split = segment.byteSize() / 2;
167             first = segment.asSlice(0, split);
168             second = segment.asSlice(split);
169         }
170 
171         @Override
172         public void doAccess() {
173             first.copyFrom(second);
174         }

177     static class SegmentFillAccessor extends AbstractSegmentAccessor {
178 
179         SegmentFillAccessor(int id, MemorySegment segment) {
180             super(id, segment);
181         }
182 
183         @Override
184         public void doAccess() {
185             segment.fill((byte) ThreadLocalRandom.current().nextInt(10));
186         }
187     }
188 
189     static class SegmentMismatchAccessor extends AbstractSegmentAccessor {
190 
191         final MemorySegment copy;
192 
193         SegmentMismatchAccessor(int id, MemorySegment segment) {
194             super(id, segment);
195             this.copy = MemorySegment.allocateNative(SEGMENT_SIZE, 1, segment.scope());
196             copy.copyFrom(segment);
197             copy.set(JAVA_BYTE, ThreadLocalRandom.current().nextInt(SEGMENT_SIZE), (byte)42);
198         }
199 
200         @Override
201         public void doAccess() {
202             segment.mismatch(copy);
203         }
204     }
205 
206     static class BufferAccessor extends AbstractBufferAccessor {
207 
208         BufferAccessor(int id, MemorySegment segment) {
209             super(id, segment);
210         }
211 
212         @Override
213         public void doAccess() {
214             int sum = 0;
215             for (int i = 0; i < bb.capacity(); i++) {
216                 sum += bb.get(i);
217             }
< prev index next >