From e860c7925b73c900fcebe733a405059bf60f102a Mon Sep 17 00:00:00 2001 From: Denys Makogon Date: Mon, 9 Oct 2023 22:15:55 +0200 Subject: [PATCH 1/4] try to build on macos --- .github/conan2/macos_default | 12 ++++++++++++ .github/workflows/build.yml | 8 ++++++-- README.md | 8 +++++--- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 .github/conan2/macos_default diff --git a/.github/conan2/macos_default b/.github/conan2/macos_default new file mode 100644 index 0000000..e0590e5 --- /dev/null +++ b/.github/conan2/macos_default @@ -0,0 +1,12 @@ +[settings] +arch=x86_64 +build_type=Release +compiler=apple-clang +compiler.cppstd=gnu17 +compiler.libcxx=libc++ +compiler.version=14 +os=Macos + +[buildenv] +CC=/usr/bin/clang +CXX=/usr/bin/clang++ diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2333399..25efa87 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,10 +11,14 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-22.04] + os: [ubuntu-latest, macos-latest] include: - - os: ubuntu-22.04 + - os: ubuntu-latest TARGET: linux + PLATFORM: x64 + - os: macos-latest + TARGET: macos + PLATFORM: x64 steps: - name: 'Check out repository' uses: actions/checkout@v3 diff --git a/README.md b/README.md index 60f5242..91616e4 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,12 @@ If you're on something else like `linux` or `x86_64` arch, please following [the ## How to build ```shell -mkdir -p build && cd build -conan install --build=missing .. -cmake .. -DCMAKE_BUILD_TYPE=release +mkdir -p build +conan install . --output-folder=build --build=missing +cd build +cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release cmake --build . + make java-sources make jar ``` From d005278485be69be27158b044cce8bc9b9f9fe19 Mon Sep 17 00:00:00 2001 From: Denys Makogon Date: Mon, 9 Oct 2023 22:55:14 +0200 Subject: [PATCH 2/4] reworking gha build workflow --- .github/workflows/build.yml | 93 ++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 25efa87..3a4e368 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ on: branches: [ main ] jobs: - build-and-test: + build: runs-on: ${{ matrix.os }} strategy: matrix: @@ -16,9 +16,64 @@ jobs: - os: ubuntu-latest TARGET: linux PLATFORM: x64 + LIB_EXT: so - os: macos-latest TARGET: macos PLATFORM: x64 + LIB_EXT: dylib + steps: + - name: 'Check out repository' + uses: actions/checkout@v3 + with: + fetch-depth: 1 + + - name: Set up Homebrew + id: set-up-homebrew + uses: Homebrew/actions/setup-homebrew@master + with: + test-bot: false + + - name: install prerequisites + shell: bash + run: | + brew install gcc + brew install conan + + - name: conan-profile + shell: bash + run: | + conan profile detect + conan version + cp .github/conan2/${{ matrix.TARGET }}_default ~/.conan2/profiles/default + conan profile show + + - name: conan-install + shell: bash + run: | + mkdir -p build + conan install . --output-folder=build --build=missing + cd build + cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release + cmake --build . + + - name: artifacts + uses: actions/upload-artifact@v3 + with: + name: libjudy.${{ matrix.LIB_EXT }} + path: build/libjudy.${{ matrix.LIB_EXT }} + retention-days: 1 + + package: + needs: + - build + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ ubuntu-latest ] + include: + - os: ubuntu-latest + TARGET: linux + PLATFORM: x64 steps: - name: 'Check out repository' uses: actions/checkout@v3 @@ -53,42 +108,56 @@ jobs: mkdir -p build jextract -I /usr/local/include @jextract_dump.txt include/c_api.h + - name: version + id: version + shell: bash + run: echo "version_var=$(date +'%Y.%m.%d')" >> $GITHUB_ENV + - name: Set up Homebrew id: set-up-homebrew uses: Homebrew/actions/setup-homebrew@master with: test-bot: false - - name: 'install prerequisites' + - name: install prerequisites shell: bash run: | brew install gcc brew install conan - - name: conan profile + - name: conan-profile shell: bash run: | conan profile detect conan version cp .github/conan2/${{ matrix.TARGET }}_default ~/.conan2/profiles/default conan profile show - mkdir -p build - conan install . --output-folder=build --build=missing - cd build - cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release - cmake --build . - - name: version - id: version + - name: conan-install shell: bash - run: echo "version_var=$(date +'%Y.%m.%d')" >> $GITHUB_ENV + run: | + mkdir -p build + conan install . --output-folder=build --build=missing - - name: mvn + - name: java sources shell: bash run: | export PATH=${PATH}:/tmp/jextract/bin cd build make java-sources + mkdir src/main/resources + + - name: libjudy.so + uses: actions/download-artifact@v3 + with: + name: libjudy.so + path: build/src/main/resources + + - name: libjudy.dylib + uses: actions/download-artifact@v3 + with: + name: libjudy.dylib + path: build/src/main/resources - name: jar shell: bash From 8bc563cf7c1bcb6b4564681e7add732208391f5e Mon Sep 17 00:00:00 2001 From: Denys Makogon Date: Mon, 9 Oct 2023 23:15:12 +0200 Subject: [PATCH 3/4] adding cmake --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3a4e368..226dd52 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -138,6 +138,8 @@ jobs: run: | mkdir -p build conan install . --output-folder=build --build=missing + cd build + cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release - name: java sources shell: bash From 4003c552446bdbf6444919853e9009d4fc10c6d9 Mon Sep 17 00:00:00 2001 From: Denys Makogon Date: Tue, 10 Oct 2023 00:13:12 +0200 Subject: [PATCH 4/4] updating readme --- README.md | 74 ++++++++++++++++++++++++++----------------------------- pom.xml | 4 +++ 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 91616e4..a30eca5 100644 --- a/README.md +++ b/README.md @@ -91,37 +91,39 @@ int main() { ```java package com.boost.server; -import java.lang.foreign.*; +import java.lang.foreign.Arena; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.nio.charset.StandardCharsets; import java.time.Instant; +import java.util.Objects; +import java.util.logging.Logger; +import static judy.server.c_api_h.boostServerWithHandler; public class BoostServer implements AutoClosable { - static final Linker linker = Linker.nativeLinker(); - MemorySession memorySession = MemorySession.openShared(); - MemorySegment handlerSegment; static { try { - System.load(System.getProperty("boost.server.library")); + System.load(System.getProperty("judy.server.lib")); } catch (Exception e) { throw new RuntimeException(e); } } - private static final SymbolLookup linkerLookup = linker.defaultLookup(); - private static final SymbolLookup loaderLookup = SymbolLookup.loaderLookup(); - private static final SymbolLookup lookup = name -> - loaderLookup.lookup(name).or(() -> linkerLookup.lookup(name)); + + static final Linker linker = Linker.nativeLinker(); + static final Arena sharedArena = Arena.ofAuto(); + static MemorySegment handlerSegment = null; public BoostServer() throws Exception { + Objects.requireNonNull(configuration); + this.configuration = configuration; var requestHandlerMH = MethodHandles.lookup().findVirtual( BoostServer.class, "requestHandler", MethodType.methodType( void.class, - MemoryAddress.class, long.class, - MemoryAddress.class, int.class + MemorySegment.class, long.class, + MemorySegment.class, int.class ) ).bindTo(this); handlerSegment = linker.upcallStub( @@ -130,43 +132,37 @@ public class BoostServer implements AutoClosable { ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.ADDRESS, ValueLayout.JAVA_INT ), - memorySession + sharedArena ); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + try { + close(); + } catch (Exception _) { + } + })); } - @Override - public void build() throws Exception { - var serverHandle = lookup.lookup("boostServerWithHandler").map( - address -> linker.downcallHandle(address, FunctionDescriptor.ofVoid( - ValueLayout.ADDRESS, ValueLayout.JAVA_INT, - ValueLayout.JAVA_INT, ValueLayout.JAVA_INT - )) - ).orElseThrow(); - try { - serverHandle.invoke( - handlerSegment.address(), - configuration.port, - configuration.bufferSize, - Integer.parseInt(System.getProperty("boost.threadpool.size", "1000")) - ); - } catch (Throwable e) { - throw new RuntimeException(e); - } - } - - private void requestHandler(MemoryAddress dataPtr, long ignoredBytesTransferred, MemoryAddress clientHost, int clientPort) { - var data = MemorySegment.ofAddress(dataPtr, configuration.bufferSize, memorySession) + void requestHandler(MemorySegment dataPtr, long bytesTransferred, + MemorySegment clientHost, int clientPort) { + logger.info(String.format("[%s] a new request", getClass().getName())); + var data = dataPtr.reinterpret(2048, sharedArena, null) .toArray(ValueLayout.JAVA_BYTE); - var client = new String(MemorySegment.ofAddress(clientHost, 9, memorySession) + var client = new String(clientHost.reinterpret(9, sharedArena, null) .toArray(ValueLayout.JAVA_BYTE), StandardCharsets.UTF_8); - System.out.println(String.format("a new packet from %s:%d of %d bytes received!", - client, clientPort, data.length)); + System.out.printf("a new packet from %s:%d of %d bytes received!\n", + client, clientPort, bytesTransferred); + + } + + public void build() { + boostServerWithHandler(handlerSegment, 20777, 2048, 100); } @Override - public void close() throws Exception { - memorySession.close(); + public void close() { + sharedArena.close(); } + } ``` diff --git a/pom.xml b/pom.xml index b63c25b..17db34a 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,10 @@ org.apache.maven.plugins maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + org.apache.maven.plugins