1 /*
  2  * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 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 package org.openjdk.bench.java.lang.foreign;
 24 
 25 import java.lang.foreign.MemorySegment;
 26 
 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.BenchmarkMode;
 29 import org.openjdk.jmh.annotations.Fork;
 30 import org.openjdk.jmh.annotations.Measurement;
 31 import org.openjdk.jmh.annotations.Mode;
 32 import org.openjdk.jmh.annotations.OutputTimeUnit;
 33 import org.openjdk.jmh.annotations.Param;
 34 import org.openjdk.jmh.annotations.Setup;
 35 import org.openjdk.jmh.annotations.State;
 36 import org.openjdk.jmh.annotations.TearDown;
 37 import org.openjdk.jmh.annotations.Warmup;
 38 
 39 import java.lang.foreign.Arena;
 40 import java.util.ArrayList;
 41 import java.util.List;
 42 import java.util.concurrent.TimeUnit;
 43 
 44 @BenchmarkMode(Mode.AverageTime)
 45 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 46 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 47 @State(org.openjdk.jmh.annotations.Scope.Thread)
 48 @OutputTimeUnit(TimeUnit.MICROSECONDS)
 49 @Fork(3)
 50 public class MemorySessionClose {
 51 
 52     static final int ALLOC_SIZE = 1024;
 53 
 54     public enum StressMode {
 55         NONE,
 56         MEMORY,
 57         THREADS
 58     }
 59 
 60     @Param({"NONE", "MEMORY", "THREADS"})
 61     StressMode mode;
 62 
 63     List<byte[]> arrays;
 64     volatile boolean stop = false;
 65     List<Thread> threads;
 66 
 67     @Setup
 68     public void setup() throws Throwable {
 69         if (mode == StressMode.MEMORY) {
 70             arrays = new ArrayList<>();
 71             for (int i = 0; i < 100_000_000; i++) {
 72                 arrays.add(new byte[2]);
 73             }
 74         } else if (mode == StressMode.THREADS) {
 75             threads = new ArrayList<>();
 76             for (int i = 0 ; i < 4 ; i++) {
 77                 threads.add(new Thread(() -> {
 78                     while (true) {
 79                         if (stop) break;
 80                         // busy wait
 81                     }
 82                 }));
 83             }
 84             threads.forEach(Thread::start);
 85         }
 86 
 87     }
 88 
 89     @TearDown
 90     public void tearDown() throws Throwable {
 91         arrays = null;
 92         if (threads != null) {
 93             stop = true;
 94             threads.forEach(t -> {
 95                 try {
 96                     t.join();
 97                 } catch (InterruptedException ex) {
 98                     throw new IllegalStateException(ex);
 99                 }
100             });
101         }
102     }
103 
104     @Benchmark
105     public MemorySegment confined_close() {
106         try (Arena arena = Arena.ofConfined()) {
107             return arena.allocate(ALLOC_SIZE, 4);
108         }
109     }
110 
111     @Benchmark
112     public MemorySegment shared_close() {
113         try (Arena arena = Arena.ofShared()) {
114             return arena.allocate(ALLOC_SIZE, 4);
115         }
116     }
117 
118     @Benchmark
119     public MemorySegment implicit_close() {
120         return Arena.ofAuto().allocate(ALLOC_SIZE, 4);
121     }
122 
123     @Benchmark
124     public MemorySegment implicit_close_systemgc() {
125         if (gcCount++ == 0) System.gc(); // GC when we overflow
126         return Arena.ofAuto().allocate(ALLOC_SIZE, 4);
127     }
128 
129     // keep
130     static byte gcCount = 0;
131 }