Skip to content

Commit

Permalink
upgrade pni to 21.0.0.18 and upgrade make scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
wkgcass committed Oct 29, 2023
1 parent 2c80a3a commit a3854bd
Show file tree
Hide file tree
Showing 44 changed files with 1,285 additions and 666 deletions.
File renamed without changes.
51 changes: 51 additions & 0 deletions benchmark/src/main/c-generated/pni.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "pni.h"

#if PNI_GRAAL

static __thread void* _graalThread;
static void* _graalIsolate;

JNIEXPORT void JNICALL SetPNIGraalThread(void* thread) {
_graalThread = thread;
}

JNIEXPORT void* JNICALL GetPNIGraalThread(void) {
return _graalThread;
}

JNIEXPORT void JNICALL SetPNIGraalIsolate(void* isolate) {
_graalIsolate = isolate;
}

JNIEXPORT void* JNICALL GetPNIGraalIsolate(void) {
return _graalIsolate;
}

#endif // PNI_GRAAL

static PNIFuncInvokeFunc _PNIFuncInvokeFunc;

JNIEXPORT PNIFuncInvokeFunc JNICALL GetPNIFuncInvokeFunc(void) {
return _PNIFuncInvokeFunc;
}
JNIEXPORT void JNICALL SetPNIFuncInvokeFunc(PNIFuncInvokeFunc f) {
_PNIFuncInvokeFunc = f;
}

static PNIFuncReleaseFunc _PNIFuncReleaseFunc;

JNIEXPORT PNIFuncReleaseFunc JNICALL GetPNIFuncReleaseFunc(void) {
return _PNIFuncReleaseFunc;
}
JNIEXPORT void JNICALL SetPNIFuncReleaseFunc(PNIFuncReleaseFunc f) {
_PNIFuncReleaseFunc = f;
}

static PNIRefReleaseFunc _PNIRefReleaseFunc;

JNIEXPORT PNIRefReleaseFunc JNICALL GetPNIRefReleaseFunc(void) {
return _PNIRefReleaseFunc;
}
JNIEXPORT void JNICALL SetPNIRefReleaseFunc(PNIRefReleaseFunc f) {
_PNIRefReleaseFunc = f;
}
95 changes: 67 additions & 28 deletions misc/pni.h → benchmark/src/main/c-generated/pni.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
#define PNI_PACK( __t__, __n__, __Declaration__ ) __pragma(pack(push, 1)) __t__ __n__ __Declaration__ __pragma(pack(pop))
#endif

typedef PNI_PACK(struct, PNIException, {
typedef struct PNIException {
char* type;
#define PNIExceptionMessageLen (4096)
char message[PNIExceptionMessageLen];
int32_t errno_; /* padding */ uint64_t :32;
}) PNIException;
int32_t errno_; /* padding uint32_t : 32; */
} PNIException;

typedef PNI_PACK(struct, PNIBuf, {
typedef struct PNIBuf {
void* buf;
uint64_t len;
}) PNIBuf;
} PNIBuf;

typedef PNI_PACK(struct, PNIEnv, {
typedef struct PNIEnv {
PNIException ex;
union {
int8_t return_byte;
Expand All @@ -38,21 +38,21 @@ typedef PNI_PACK(struct, PNIEnv, {
void* return_pointer;
PNIBuf return_buf;
};
}) PNIEnv;
} PNIEnv;

typedef PNI_PACK(struct, PNIEnvUnionPlaceHolder, {
typedef struct PNIEnvUnionPlaceHolder {
uint64_t : 64;
uint64_t : 64;
}) PNIEnvUnionPlaceHolder;
} PNIEnvUnionPlaceHolder;

#define PNIEnvExpand(EnvType, ValueType) \
typedef PNI_PACK(struct, PNIEnv_##EnvType, { \
typedef struct PNIEnv_##EnvType { \
PNIException ex; \
union { \
ValueType return_; \
PNIEnvUnionPlaceHolder __placeholder__; \
}; \
}) PNIEnv_##EnvType;
} PNIEnv_##EnvType;
// end #define PNIEnvExpand

PNIEnvExpand(byte, int8_t)
Expand All @@ -67,10 +67,10 @@ PNIEnvExpand(pointer, void*)
PNIEnvExpand(string, char*)
PNIEnvExpand(buf, PNIBuf)

typedef PNI_PACK(struct, PNIEnv_void, {
typedef struct PNIEnv_void {
PNIException ex;
PNIEnvUnionPlaceHolder __placeholder__;
}) PNIEnv_void;
} PNIEnv_void;

static inline int PNIThrowException(void* _env, const char* extype, char* message) {
PNIEnv* env = _env;
Expand All @@ -89,54 +89,92 @@ static inline void PNIStoreErrno(void* _env) {
env->ex.errno_ = errno;
}

typedef PNI_PACK(struct, PNIFunc, {
int64_t index;
int32_t (*func)(int64_t,void*);
void (*release)(int64_t);
#if PNI_GRAAL
JNIEXPORT void JNICALL SetPNIGraalThread(void* thread);
JNIEXPORT void* JNICALL GetPNIGraalThread(void);
JNIEXPORT void JNICALL SetPNIGraalIsolate(void* isolate);
JNIEXPORT void* JNICALL GetPNIGraalIsolate(void);
#endif // PNI_GRAAL

typedef struct PNIFunc {
int64_t index;
union {
void* userdata;
uint64_t udata64;
};
}) PNIFunc;
} PNIFunc;

PNIEnvExpand(func, PNIFunc*)

#define PNIFuncInvokeExceptionCaught ((int32_t) 0x800000f1)
#define PNIFuncInvokeNoSuchFunction ((int32_t) 0x800000f2)

static inline int PNIFuncInvoke(PNIFunc* f, void* data) {
return f->func(f->index, data);
#if PNI_GRAAL
typedef int32_t (*PNIFuncInvokeFunc)(void*,int64_t,void*);
#else
typedef int32_t (*PNIFuncInvokeFunc)(int64_t,void*);
#endif // PNI_GRAAL
JNIEXPORT PNIFuncInvokeFunc JNICALL GetPNIFuncInvokeFunc(void);
JNIEXPORT void JNICALL SetPNIFuncInvokeFunc(PNIFuncInvokeFunc f);

static inline int32_t PNIFuncInvoke(PNIFunc* f, void* data) {
#if PNI_GRAAL
return GetPNIFuncInvokeFunc()(GetPNIGraalThread(), f->index, data);
#else
return GetPNIFuncInvokeFunc()(f->index, data);
#endif // PNI_GRAAL
}

#if PNI_GRAAL
typedef void (*PNIFuncReleaseFunc)(void*,int64_t);
#else
typedef void (*PNIFuncReleaseFunc)(int64_t);
#endif // PNI_GRAAL
JNIEXPORT PNIFuncReleaseFunc JNICALL GetPNIFuncReleaseFunc(void);
JNIEXPORT void JNICALL SetPNIFuncReleaseFunc(PNIFuncReleaseFunc f);

static inline void PNIFuncRelease(PNIFunc* f) {
f->release(f->index);
#if PNI_GRAAL
GetPNIFuncReleaseFunc()(GetPNIGraalThread(), f->index);
#else
GetPNIFuncReleaseFunc()(f->index);
#endif // PNI_GRAAL
}

typedef PNI_PACK(struct, PNIRef, {
typedef struct PNIRef {
int64_t index;
void (*release)(int64_t);

union {
void* userdata;
uint64_t udata64;
};
}) PNIRef;
} PNIRef;

PNIEnvExpand(ref, PNIRef*)

#if PNI_GRAAL
typedef void (*PNIRefReleaseFunc)(void*,int64_t);
#else
typedef void (*PNIRefReleaseFunc)(int64_t);
#endif // PNI_GRAAL
JNIEXPORT PNIRefReleaseFunc JNICALL GetPNIRefReleaseFunc(void);
JNIEXPORT void JNICALL SetPNIRefReleaseFunc(PNIRefReleaseFunc f);

static inline void PNIRefRelease(PNIRef* ref) {
ref->release(ref->index);
#if PNI_GRAAL
GetPNIRefReleaseFunc()(GetPNIGraalThread(), ref->index);
#else
GetPNIRefReleaseFunc()(ref->index);
#endif // PNI_GRAAL
}

#define PNIBufExpand(BufType, ValueType, Size) \
typedef PNI_PACK(struct, PNIBuf_##BufType, { \
typedef struct PNIBuf_##BufType { \
union { \
ValueType* array; \
void* buf; \
}; \
uint64_t bufLen; \
}) PNIBuf_##BufType; \
} PNIBuf_##BufType; \
static inline uint64_t BufType##PNIArrayLen(PNIBuf_##BufType* buf) { \
return buf->bufLen / (Size == 0 ? 1 : Size); \
} \
Expand All @@ -158,5 +196,6 @@ PNIBufExpand(double, double, 8)
PNIBufExpand(short, int16_t, 2)
PNIBufExpand(ushort, uint16_t, 2)
PNIBufExpand(bool, uint8_t, 1)
PNIBufExpand(ptr, void *, 8)

#endif // PNIENV_H
2 changes: 1 addition & 1 deletion benchmark/src/main/c/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ rm -f "$target"
gcc -std=gnu99 -O2 \
$GCC_OPTS \
-I "$LUA_INC" \
-I "../../../../misc" \
-I "../c-generated" \
-I "../../../../core/src/main/c-generated" \
-L "$LUA_LD/" \
-shared -Werror -lc -lluajit-5.1 -fPIC \
../c-generated/io_vproxy_luajn_benchmark_BenchmarkUpcall.c \
../c-generated/pni.c \
-o "$target"
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ private static int oneRefArgIntReturn(MemorySegment ref) {
} catch (Throwable t) {
throw new RuntimeException(t);
}
oneIntArgIntReturn = PanamaUtils.defineCFunction(ARENA, oneIntArgIntReturnMH, int.class, int.class);
twoIntArgIntReturn = PanamaUtils.defineCFunction(ARENA, twoIntArgIntReturnMH, int.class, int.class, int.class);
oneRefArgIntReturn = PanamaUtils.defineCFunction(ARENA, oneRefArgIntReturnMH, int.class, MemorySegment.class);
oneIntArgIntReturn = PanamaUtils.defineCFunction(new PNILinkOptions(), ARENA, oneIntArgIntReturnMH, int.class, int.class);
twoIntArgIntReturn = PanamaUtils.defineCFunction(new PNILinkOptions(), ARENA, twoIntArgIntReturnMH, int.class, int.class, int.class);
oneRefArgIntReturn = PanamaUtils.defineCFunction(new PNILinkOptions(), ARENA, oneRefArgIntReturnMH, int.class, MemorySegment.class);

var initMH = PanamaUtils.lookupPNICriticalFunction(true, void.class, "JavaCritical_io_vproxy_luajn_benchmark_BenchmarkUpcall_INIT", MemorySegment.class, MemorySegment.class, MemorySegment.class);
var initMH = PanamaUtils.lookupPNICriticalFunction(new PNILinkOptions().setCritical(true), void.class, "JavaCritical_io_vproxy_luajn_benchmark_BenchmarkUpcall_INIT", MemorySegment.class, MemorySegment.class, MemorySegment.class);
try {
initMH.invoke(oneIntArgIntReturn, twoIntArgIntReturn, oneRefArgIntReturn);
} catch (Throwable t) {
Expand All @@ -87,5 +87,5 @@ public interface Interface {
int oneRefArgIntReturn(java.lang.Integer ref);
}
}
// metadata.generator-version: pni 21.0.0.8
// sha256:a1a57313e69f6b14abf4cdec0ca0693c0499aeae9eb87b9dd9f88b9ab19b3754
// metadata.generator-version: pni 21.0.0.17
// sha256:aba7429d8d24bdf5341eaaae5acb16b393d9118cf8d43fa3d32bc352d5b5d094
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.vproxy.luajn.LuaJNative;
import io.vproxy.luajn.n.LuaLib;
import io.vproxy.pni.Allocator;
import io.vproxy.pni.PNILinkOptions;
import io.vproxy.pni.PNIRef;
import io.vproxy.pni.PanamaUtils;
import org.openjdk.jmh.annotations.*;
Expand Down Expand Up @@ -38,9 +39,9 @@ public void setUp() {
L = new LuaJNState(allocator);
LuaLib.get().openLibs(L.getLuaState());

var ffiOneIntArgIntReturnJavaFunc = PanamaUtils.defineCFunctionByName(arena, CallJavaMethodFromLua.class, "ffiOneIntArgIntReturnJavaFunc");
var ffiTwoIntArgsIntReturnJavaFunc = PanamaUtils.defineCFunctionByName(arena, CallJavaMethodFromLua.class, "ffiTwoIntArgsIntReturnJavaFunc");
var ffiOneRefArgIntReturnJavaFunc = PanamaUtils.defineCFunctionByName(arena, CallJavaMethodFromLua.class, "ffiOneRefArgIntReturnJavaFunc");
var ffiOneIntArgIntReturnJavaFunc = PanamaUtils.defineCFunctionByName(new PNILinkOptions(), arena, CallJavaMethodFromLua.class, "ffiOneIntArgIntReturnJavaFunc");
var ffiTwoIntArgsIntReturnJavaFunc = PanamaUtils.defineCFunctionByName(new PNILinkOptions(), arena, CallJavaMethodFromLua.class, "ffiTwoIntArgsIntReturnJavaFunc");
var ffiOneRefArgIntReturnJavaFunc = PanamaUtils.defineCFunctionByName(new PNILinkOptions(), arena, CallJavaMethodFromLua.class, "ffiOneRefArgIntReturnJavaFunc");

L.getGlobal().put("ffiOneIntArgIntReturnJavaFunc", ffiOneIntArgIntReturnJavaFunc);
L.getGlobal().put("ffiTwoIntArgsIntReturnJavaFunc", ffiTwoIntArgsIntReturnJavaFunc);
Expand Down
13 changes: 12 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent

buildscript {
def PNI_VERSION = '21.0.0.15'
def PNI_VERSION = '21.0.0.18'
ext.set("PNI_VERSION", PNI_VERSION)

repositories {
Expand Down Expand Up @@ -101,6 +101,10 @@ project(':template') {
.setClasspath(List.of(workingDir + '/template/build/classes/java/main'))
.setJavaOutputBaseDirectory(workingDir + '/core/src/main/generated')
.setCOutputDirectory(workingDir + '/core/src/main/c-generated')
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.TYPE_NAME_PREFIX, "PNI")
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_PNI_H_FILE)
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_PNI_C_FILE)
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_JNI_H_MOCK_FILE)
)

doLast {
Expand Down Expand Up @@ -153,6 +157,9 @@ project(':tutorial-template') {
.setFilters(List.of(
java.util.regex.Pattern.compile('io\\.vproxy\\.luajn\\.tutorial\\..*')
))
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.TYPE_NAME_PREFIX, "PNI")
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_PNI_H_FILE)
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_JNI_H_MOCK_FILE)
)

doLast {
Expand Down Expand Up @@ -258,6 +265,10 @@ project(':benchmark-template') {
.setFilters(List.of(
java.util.regex.Pattern.compile('io\\.vproxy\\.luajn\\.benchmark\\..*')
))
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.TYPE_NAME_PREFIX, "PNI")
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_PNI_H_FILE)
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_PNI_C_FILE)
.setCompilationFlag(io.vproxy.pni.exec.CompilationFlag.RELEASE_JNI_H_MOCK_FILE)
)

doLast {
Expand Down
36 changes: 36 additions & 0 deletions core/src/main/c-generated/jni.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef PNI_JNIMOCK_H
#define PNI_JNIMOCK_H

#include <stdio.h>
#include <stdarg.h>

#ifdef WIN32
#define JNIEXPORT __declspec(dllexport)
#else
#define JNIEXPORT __attribute__((visibility("default")))
#endif

#ifdef WIN32
#define JNICALL __stdcall
#else
#define JNICALL
#endif

#include <inttypes.h>

typedef int8_t jbyte;
typedef uint16_t jchar;
typedef double jdouble;
typedef float jfloat;
typedef int32_t jint;
typedef int64_t jlong;
typedef int16_t jshort;
typedef uint8_t jboolean;

#define JNI_FALSE (0)
#define JNI_TRUE (1)

#define JNI_OK (0)
#define JNI_ERR (-1)

#endif // PNI_JNIMOCK_H
Loading

0 comments on commit a3854bd

Please sign in to comment.