1 /*
  2  *  Copyright (c) 2023, 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 
 24 package org.openjdk.bench.java.lang.foreign;
 25 
 26 import org.openjdk.jmh.annotations.Benchmark;
 27 import org.openjdk.jmh.annotations.BenchmarkMode;
 28 import org.openjdk.jmh.annotations.Fork;
 29 import org.openjdk.jmh.annotations.Measurement;
 30 import org.openjdk.jmh.annotations.Mode;
 31 import org.openjdk.jmh.annotations.OutputTimeUnit;
 32 import org.openjdk.jmh.annotations.Param;
 33 import org.openjdk.jmh.annotations.Setup;
 34 import org.openjdk.jmh.annotations.State;
 35 import org.openjdk.jmh.annotations.TearDown;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.lang.foreign.Arena;
 39 import java.lang.foreign.FunctionDescriptor;
 40 import java.lang.foreign.Linker;
 41 import java.lang.foreign.MemorySegment;
 42 import java.lang.foreign.MemorySegment.Scope;
 43 import java.lang.foreign.SegmentAllocator;
 44 import java.lang.foreign.ValueLayout;
 45 import java.lang.invoke.MethodHandle;
 46 import java.util.concurrent.TimeUnit;
 47 
 48 @BenchmarkMode(Mode.AverageTime)
 49 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 50 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @State(org.openjdk.jmh.annotations.Scope.Thread)
 52 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 53 @Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
 54 public class AllocTest extends CLayouts {
 55 
 56     Arena arena = Arena.ofConfined();
 57 
 58     @Param({"5", "20", "100", "500", "1000"})
 59     public int size;
 60 
 61     @TearDown
 62     public void tearDown() {
 63         arena.close();
 64     }
 65 
 66     @Benchmark
 67     public MemorySegment alloc_confined() {
 68         Arena arena = Arena.ofConfined();
 69         MemorySegment segment = arena.allocate(size);
 70         arena.close();
 71         return segment;
 72     }
 73 
 74     @Benchmark
 75     public long alloc_calloc_arena() {
 76         CallocArena arena = new CallocArena();
 77         MemorySegment segment = arena.allocate(size);
 78         arena.close();
 79         return segment.address();
 80     }
 81 
 82     @Benchmark
 83     public long alloc_unsafe_arena() {
 84         UnsafeArena arena = new UnsafeArena();
 85         MemorySegment segment = arena.allocate(size);
 86         arena.close();
 87         return segment.address();
 88     }
 89 
 90     public static class CallocArena implements Arena {
 91 
 92         static final MethodHandle CALLOC = Linker.nativeLinker()
 93                 .downcallHandle(
 94                         Linker.nativeLinker().defaultLookup().find("calloc").get(),
 95                         FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG));
 96 
 97         static MemorySegment calloc(long size) {
 98             try {
 99                 return (MemorySegment)CALLOC.invokeExact(size, 1L);
100             } catch (Throwable ex) {
101                 throw new IllegalStateException(ex);
102             }
103         }
104 
105         final Arena arena = Arena.ofConfined();
106 
107         @Override
108         public Scope scope() {
109             return arena.scope();
110         }
111 
112         @Override
113         public void close() {
114             arena.close();
115         }
116 
117         @Override
118         public MemorySegment allocate(long byteSize, long byteAlignment) {
119             return calloc(byteSize)
120                     .reinterpret(byteSize, arena, CLayouts::freeMemory);
121         }
122     }
123 
124     public static class UnsafeArena implements Arena {
125 
126         final Arena arena = Arena.ofConfined();
127 
128         @Override
129         public Scope scope() {
130             return arena.scope();
131         }
132 
133         @Override
134         public void close() {
135             arena.close();
136         }
137 
138         @Override
139         public MemorySegment allocate(long byteSize, long byteAlignment) {
140             MemorySegment segment = MemorySegment.ofAddress(Utils.unsafe.allocateMemory(byteSize));
141             Utils.unsafe.setMemory(segment.address(), byteSize, (byte)0);
142             return segment.reinterpret(byteSize, arena, ms -> Utils.unsafe.freeMemory(segment.address()));
143         }
144     }
145 }