Using profiles to track allocation and release of objects
ajaysusarla opened this issue · comments
Partha Susarla commented
v8go
has a leakcheck sanitiser for the c++ code. But we can potentially use profiles to track allocation and release of objects within go, based on build tags.
I've created a PR, with profiles for isolate
and context
creation and disposal to show how these changes will look: #391. This can be used as shown below:
package main
import (
"fmt"
"os"
"runtime/pprof"
"strings"
v8 "rogchap.com/v8go"
)
func DumpProfiles() {
for _, prof := range pprof.Profiles() {
if strings.HasPrefix(prof.Name(), "rogchap.com") {
_ = prof.WriteTo(os.Stdout, 1)
}
}
}
func Must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}
func main() {
i := v8.NewIsolate()
var vs []*v8.Value
Must(v8.NewValue(i, "derp"))
for j := 0; j < 1000; j++ {
vs = append(vs, Must(v8.NewValue(i, "derp")))
}
fmt.Printf("Profile before Dispose:\n")
DumpProfiles()
for j := 1; j < 1000; j++ {
vs[j].Release()
}
i.Dispose()
fmt.Printf("Profile after Dispose:\n")
DumpProfiles()
}
Which results in something like this, when compiled without tags:
profile-demo$ go run ./cmd/profile-demo
Profile before Dispose:
Profile after Dispose:
And with the tags, we get profiling output, like so:
profile-demo$ go run -tags v8profile ./cmd/profile-demo
Profile before Dispose:
rogchap.com/v8go/gv8go.Isolate profile: total 1
1 @ 0x8ca645 0x8cc0fc 0x8ce3dd 0x84afa7 0x876581
# 0x8ca644 rogchap.com/v8go.addIsolate+0x44 /home/partha/src/profile-demo/vendor/rogchap.com/v8go/profile.go:17
# 0x8cc0fb rogchap.com/v8go.NewIsolate+0x9b /home/partha/src/profile-demo/vendor/rogchap.com/v8go/isolate.go:62
# 0x8ce3dc main.main+0x1c /home/partha/src/profile-demo/cmd/profile-demo/main.go:28
# 0x84afa6 runtime.main+0x206 /usr/local/go/1.20.3/src/runtime/proc.go:250
1 @ 0x8ca725 0x8cd4d0 0x8cc875 0x8ce3f5 0x84afa7 0x876581
# 0x8ca724 rogchap.com/v8go.addValue+0x44 /home/partha/src/profile-demo/vendor/rogchap.com/v8go/profile.go:25
# 0x8cd4cf rogchap.com/v8go.NewValue.func1+0x2f /home/partha/src/profile-demo/vendor/rogchap.com/v8go/value.go:67
# 0x8cc874 rogchap.com/v8go.NewValue+0x2b4 /home/partha/src/profile-demo/vendor/rogchap.com/v8go/value.go:81
# 0x8ce3f4 main.main+0x34 /home/partha/src/profile-demo/cmd/profile-demo/main.go:30
# 0x84afa6 runtime.main+0x206 /usr/local/go/1.20.3/src/runtime/proc.go:250
Profile after Dispose:
rogchap.com/v8go/gv8go.Isolate profile: total 0
rogchap.com/v8go/gv8go.Value profile: total 2
1 @ 0x8ca725 0x8cd4d0 0x8cc875 0x8ce3f5 0x84afa7 0x876581
# 0x8ca724 rogchap.com/v8go.addValue+0x44 /home/partha/src/profile-demo/vendor/rogchap.com/v8go/profile.go:25
# 0x8cd4cf rogchap.com/v8go.NewValue.func1+0x2f /home/partha/src/profile-demo/vendor/rogchap.com/v8go/value.go:67
# 0x8cc874 rogchap.com/v8go.NewValue+0x2b4 /home/partha/src/profile-demo/vendor/rogchap.com/v8go/value.go:81
# 0x8ce3f4 main.main+0x34 /home/partha/src/profile-demo/cmd/profile-demo/main.go:30
# 0x84afa6 runtime.main+0x206
If there is an appetite for it, for both the maintainers and the community, I will work on the PR further to profile value.go
as well.