Commit 97b89072 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

example/loopback: use signals to capture memory profiles at specific times.

Change-Id: Idddaef2216c2fd216476da8744efcda679f12dd6
parent a27cac55
...@@ -8,9 +8,11 @@ import ( ...@@ -8,9 +8,11 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/signal"
"path" "path"
"path/filepath" "path/filepath"
"runtime/pprof" "runtime/pprof"
"syscall"
"time" "time"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
...@@ -18,6 +20,25 @@ import ( ...@@ -18,6 +20,25 @@ import (
"github.com/hanwen/go-fuse/fuse/pathfs" "github.com/hanwen/go-fuse/fuse/pathfs"
) )
func writeMemProfile(fn string, sigs <-chan os.Signal) {
i := 0
for range sigs {
fn := fmt.Sprintf("%s-%d.memprof", fn, i)
i++
log.Printf("Writing mem profile to %s\n", fn)
f, err := os.Create(fn)
if err != nil {
log.Printf("Create: %v", err)
continue
}
pprof.WriteHeapProfile(f)
if err := f.Close(); err != nil {
log.Printf("close %v", err)
}
}
}
func main() { func main() {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
// Scans the arg list and sets up flags // Scans the arg list and sets up flags
...@@ -44,17 +65,10 @@ func main() { ...@@ -44,17 +65,10 @@ func main() {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
if *memprofile != "" { if *memprofile != "" {
fmt.Printf("Writing mem profile to %s\n", *memprofile) log.Printf("send SIGUSR1 to %d to dump memory profile", os.Getpid())
f, err := os.Create(*memprofile) profSig := make(chan os.Signal, 1)
if err != nil { signal.Notify(profSig, syscall.SIGUSR1)
fmt.Println(err) go writeMemProfile(*memprofile, profSig)
os.Exit(4)
}
defer func() {
pprof.WriteHeapProfile(f)
f.Close()
return
}()
} }
if *cpuprofile != "" || *memprofile != "" { if *cpuprofile != "" || *memprofile != "" {
fmt.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n") fmt.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment