Improve golang profiling (#193)

This commit is contained in:
Matias Insaurralde 2018-06-09 14:51:17 -04:00 committed by Ryan Dahl
parent d325a1d386
commit fe9ea6dcf8

41
main.go
View File

@ -5,15 +5,17 @@ package deno
import (
"flag"
"fmt"
"github.com/ry/v8worker2"
"os"
"runtime/pprof"
"github.com/ry/v8worker2"
)
var flagReload = flag.Bool("reload", false, "Reload cached remote source code.")
var flagV8Options = flag.Bool("v8-options", false, "Print V8 command line options.")
var flagDebug = flag.Bool("debug", false, "Enable debug output.")
var flagGoProf = flag.String("goprof", "", "Write golang cpu profile to file.")
var flagCPUProf = flag.String("cpuprof", "", "Write golang cpu profile to file.")
var flagMemProf = flag.String("memprof", "", "Write golang memory profile to file.")
var flagAllowRead = flag.Bool("allow-read", true,
"Allow program to read file system.")
@ -22,6 +24,8 @@ var flagAllowWrite = flag.Bool("allow-write", false,
var flagAllowNet = flag.Bool("allow-net", false,
"Allow program to make network connection.")
var memProfile *os.File
var Perms struct {
FsRead bool
FsWrite bool
@ -72,14 +76,9 @@ func Init() {
os.Exit(1)
}
// Maybe start Golang CPU profiler.
// Maybe start Golang profilers.
// Use --prof for profiling JS.
if *flagGoProf != "" {
f, err := os.Create(*flagGoProf)
check(err)
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
StartProfiling()
createDirs()
InitOS()
@ -95,6 +94,29 @@ func Init() {
main_map = stringAsset("main.map")
}
func StartProfiling() {
if *flagCPUProf != "" {
cpuProfile, err := os.Create(*flagCPUProf)
check(err)
check(pprof.StartCPUProfile(cpuProfile))
}
if *flagMemProf != "" {
var err error
memProfile, err = os.Create(*flagMemProf)
check(err)
check(pprof.WriteHeapProfile(memProfile))
}
}
func stopProfiling() {
if *flagCPUProf != "" {
pprof.StopCPUProfile()
}
if *flagMemProf != "" {
check(memProfile.Close())
}
}
// It's up to library users to call
// deno.Eval("deno_main.js", "denoMain()")
func Eval(filename string, code string) {
@ -114,4 +136,5 @@ func Loop() {
StartMainMap: main_map,
})
DispatchLoop()
stopProfiling()
}