Skip to content

Commit

Permalink
Merge pull request #14 from denismakogon/jdk21
Browse files Browse the repository at this point in the history
try to build on macos
  • Loading branch information
denismakogon authored Oct 9, 2023
2 parents a2f5ce1 + 4003c55 commit 6f2240b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 54 deletions.
12 changes: 12 additions & 0 deletions .github/conan2/macos_default
Original file line number Diff line number Diff line change
@@ -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++
99 changes: 87 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,73 @@ on:
branches: [ main ]

jobs:
build-and-test:
build:
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
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
Expand Down Expand Up @@ -49,42 +108,58 @@ 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
- 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: version
id: version
shell: bash
run: echo "version_var=$(date +'%Y.%m.%d')" >> $GITHUB_ENV
- 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
Expand Down
82 changes: 40 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -89,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(
Expand All @@ -128,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();
}

}
```

Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down

0 comments on commit 6f2240b

Please sign in to comment.