Skip to content

Commit

Permalink
feat: ignore SIGPROF while calling native funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Dec 29, 2022
1 parent f421ee8 commit 782121b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ func (self *Parser) Parse() (Node, types.ParsingError) {

func (self *Parser) skip() (int, types.ParsingError) {
fsm := types.NewStateMachine()
rt.StopProf()
start := native.SkipOne(&self.s, &self.p, fsm, uint64(0))
rt.StartProf()
types.FreeStateMachine(fsm)

if start < 0 {
Expand Down
5 changes: 4 additions & 1 deletion decoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func decodeTypedPointer(s string, i int, vt *rt.GoType, vp unsafe.Pointer, sb *_
return 0, err
} else {
rt.MoreStack(_FP_size + _VD_size + native.MaxFrameSize)
return fn(s, i, vp, sb, fv, "", nil)
rt.StopProf()
ret, err := fn(s, i, vp, sb, fv, "", nil)
rt.StartProf()
return ret, err
}
}

Expand Down
10 changes: 8 additions & 2 deletions encoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ func encodeTypedPointer(buf *[]byte, vt *rt.GoType, vp *unsafe.Pointer, sb *_Sta
return err
} else if vt.Indirect() {
rt.MoreStack(_FP_size + native.MaxFrameSize)
return fn(buf, *vp, sb, fv)
rt.StopProf()
err := fn(buf, *vp, sb, fv)
rt.StartProf()
return err
} else {
rt.MoreStack(_FP_size + native.MaxFrameSize)
return fn(buf, unsafe.Pointer(vp), sb, fv)
rt.StopProf()
err := fn(buf, unsafe.Pointer(vp), sb, fv)
rt.StartProf()
return err
}
}

Expand Down
3 changes: 0 additions & 3 deletions internal/rt/fastmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,3 @@ func FuncAddr(f interface{}) unsafe.Pointer {
return *(*unsafe.Pointer)(vv.Value)
}
}

//go:nosplit
func MoreStack(size uintptr)
38 changes: 38 additions & 0 deletions internal/rt/gcwb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,41 @@ func GcwbAddr() uintptr {
return uintptr(fp) + off
}
}

// WARN: must be aligned with runtime.Prof
type Prof struct {
signalLock uint32
hz int32
}

var (
//go:linkname runtimeProf runtime.prof
runtimeProf Prof

// count of native-C calls
yieldCount uint32

// previous value of runtimeProf.hz
oldHz int32
)

//go:nosplit
func MoreStack(size uintptr)

func StopProf() {
atomic.AddUint32(&yieldCount, 1)
if runtimeProf.hz != 0 {
oldHz = runtimeProf.hz
runtimeProf.hz = 0
}
}

func StartProf() {
atomic.AddUint32(&yieldCount, ^uint32(0))
if yieldCount == 0 && runtimeProf.hz == 0 {
if oldHz == 0 {
oldHz = 100
}
runtimeProf.hz = oldHz
}
}

0 comments on commit 782121b

Please sign in to comment.