-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,394 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
#include "go_asm.h" | ||
#include "funcdata.h" | ||
#include "textflag.h" | ||
|
||
/* | ||
This function should only used in user goroutine, it will spin the P until finish. | ||
TODO: support signal | ||
func __c_systemstack_1arg( | ||
arg unsafe.Pointer | ||
fn unsafe.Pointer | ||
) uintptr | ||
*/ | ||
TEXT ·__c_systemstack_1arg(SB), NOSPLIT, $16-24 | ||
MOVD arg0+0(FP), R0 | ||
MOVD fn+8(FP), R1 | ||
MOVD 48(g), R2 // R2 = g.m | ||
MOVD 0(R2), R3 // R3 = m.g0 | ||
|
||
// store caller's fp and lr | ||
STP (R29, R30), 16(RSP) | ||
|
||
// switch to g0 | ||
MOVD R3, g | ||
MOVD 0x38(g), R4 // g_sched+gobuf_sp | ||
|
||
// call cb in system stack | ||
MOVD RSP, R29 // mov fp, sp | ||
MOVD R4, RSP | ||
WORD $0xd63f0020 // blr x2 | ||
|
||
// restore sp | ||
MOVD R29, RSP // mov sp, fp | ||
|
||
// restore caller's fp and lr | ||
LDP 16(RSP), (R29, R30) | ||
|
||
// back to usrg | ||
MOVD 0x30(g), R2 // R2 = g_m | ||
MOVD 0xc0(R2), g // g = m_curg | ||
|
||
// restore ret | ||
MOVD R0, ret+16(FP) | ||
RET | ||
|
||
|
||
/* | ||
This function should only used in user goroutine, it will spin the P until finish. | ||
func __c_systemstack_1arg( | ||
arg unsafe.Pointer | ||
fn unsafe.Pointer | ||
) uintptr | ||
*/ | ||
TEXT ·__c_systemstack_2arg(SB), NOSPLIT, $16-24 | ||
MOVD arg0+0(FP), R0 | ||
MOVD fn+8(FP), R1 | ||
MOVD 48(g), R2 // R2 = g.m | ||
MOVD 0(R2), R3 // R3 = m.g0 | ||
|
||
// store caller's fp and lr | ||
STP (R29, R30), 16(RSP) | ||
|
||
// switch to g0 | ||
MOVD R3, g | ||
MOVD 0x38(g), R4 // g_sched+gobuf_sp | ||
|
||
// call cb in system stack | ||
MOVD RSP, R29 // mov fp, sp | ||
MOVD R4, RSP | ||
WORD $0xd63f0020 // blr x2 | ||
|
||
// restore sp | ||
MOVD R29, RSP // mov sp, fp | ||
|
||
// restore caller's fp and lr | ||
LDP 16(RSP), (R29, R30) | ||
|
||
// back to usrg | ||
MOVD 0x30(g), R2 // R2 = g_m | ||
MOVD 0xc0(R2), g // g = m_curg | ||
|
||
// restore ret | ||
MOVD R0, ret+16(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ccall | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
//go:nosplit | ||
func C_systemstack_1arg(arg0 uintptr, callback unsafe.Pointer) uintptr { | ||
return __c_systemstack_1arg(arg0, callback) | ||
|
||
// TODO: keepalive | ||
} | ||
|
||
//go:nosplit | ||
//go:noescape | ||
//goland:noinspection GoUnusedParameter | ||
func __c_systemstack_1arg(arg0 uintptr, callback unsafe.Pointer) uintptr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package link | ||
|
||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
//go:embed librs_wrapper.so | ||
Check failure on line 8 in dev/internal/link/blob.go GitHub Actions / build (1.17.x)
Check failure on line 8 in dev/internal/link/blob.go GitHub Actions / build (1.18.x)
Check failure on line 8 in dev/internal/link/blob.go GitHub Actions / build (1.19.x)
Check failure on line 8 in dev/internal/link/blob.go GitHub Actions / build (1.20.x)
|
||
var sonic_rs_blob []byte | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// +build cgo | ||
|
||
package link | ||
|
||
// #cgo LDFLAGS: -ldl | ||
// #include <dlfcn.h> | ||
import "C" | ||
import "unsafe" | ||
|
||
func DlOpen(filename string, flags int32) unsafe.Pointer { | ||
handle := C.dlopen(C.CString(filename), C.int(flags)) | ||
if handle == nil { | ||
panic("dlopen failed") | ||
} | ||
return handle | ||
} | ||
|
||
func DlClose(handle unsafe.Pointer) int32 { | ||
ret := int32(C.dlclose(handle)) | ||
if ret != 0 { | ||
panic("dlcolse failed") | ||
} | ||
return ret | ||
} | ||
|
||
func DlSym(handle unsafe.Pointer, symbol string) unsafe.Pointer { | ||
addr := C.dlsym(handle, C.CString(symbol)) | ||
if addr == nil { | ||
panic(DlError()) | ||
} | ||
return addr | ||
} | ||
|
||
func DlError() (err string) { | ||
cerr := C.dlerror() | ||
if cerr != nil { | ||
err = C.GoString(cerr) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package link | ||
|
||
import ( | ||
"fmt" | ||
"unsafe" | ||
"os" | ||
) | ||
|
||
func Link(blob []byte, names []string) []unsafe.Pointer { | ||
tmpFile, err := os.CreateTemp("/tmp", "libsonic-blob*.so") | ||
defer os.Remove(tmpFile.Name()) | ||
if err != nil { | ||
fmt.Println("unable to create temp files:", err) | ||
return nil | ||
} | ||
|
||
if _, err := tmpFile.Write(blob); err != nil { | ||
fmt.Println("unable to write temp file:", err) | ||
return nil | ||
} | ||
handle := DlOpen(tmpFile.Name(), 2 /* RTLD_NOW */) | ||
|
||
cfuncs := make([]unsafe.Pointer, len(names)) | ||
for i, sym := range(names) { | ||
cfuncs[i] = DlSym(handle, sym) | ||
} | ||
return cfuncs | ||
} | ||
|
||
func AddPcSp() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package link | ||
|
||
|
||
import ( | ||
"testing" | ||
"unsafe" | ||
"github.com/bytedance/sonic/dev/ccall" | ||
"github.com/bytedance/sonic/dev/internal" | ||
) | ||
|
||
|
||
var cfunc unsafe.Pointer | ||
|
||
|
||
func init() { | ||
cfuncs := Link(sonic_rs_blob, []string{"func_1args"}); | ||
ret := ccall.C_systemstack_1arg(123, cfuncs[0]) | ||
cfunc = cfuncs[0] | ||
println("ret is ", ret); | ||
} | ||
|
||
func Testx(t *testing.T) { | ||
|
||
} | ||
|
||
|
||
func BenchmarkFfi(b *testing.B) { | ||
r1 := internal.Cgo_func(123) | ||
r2 := ccall.C_systemstack_1arg(123, cfunc) | ||
if r1 != r2 || r1 != 123 { | ||
panic("invalid tests") | ||
} | ||
b.Run("Cgo", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_ = internal.Cgo_func(123) | ||
} | ||
}) | ||
|
||
b.Run("Ccall", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_ = ccall.C_systemstack_1arg(123, cfunc) | ||
} | ||
}) | ||
|
||
b.Run("Asm2Asm", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_ = ccall.C_systemstack_1arg(123, cfunc) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ build/ | |
|
||
|
||
!lib/linux/*.a | ||
!lib/darwin/*.a | ||
!lib/darwin/*.a | ||
!lib/linux/*.so | ||
!lib/darwin/*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+1.49 MB
(130%)
dev/rs_wrapper/lib/linux/libsonic_rs_aarch64-unknown-linux-gnu.a
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.