add -memprofile option

master
Oliver Tonnhofer 2013-05-13 10:21:12 +02:00
parent ee13f30435
commit bbff114629
2 changed files with 52 additions and 0 deletions

View File

@ -16,7 +16,9 @@ import (
"runtime"
"runtime/pprof"
"strconv"
"strings"
"sync"
"time"
)
var skipCoords, skipNodes, skipWays bool
@ -145,6 +147,7 @@ func parse(cache *cache.OSMCache, progress *stats.Statistics, filename string) {
var (
cpuprofile = flag.String("cpuprofile", "", "filename of cpu profile output")
memprofile = flag.String("memprofile", "", "dir name of mem profile output and interval (fname:interval)")
cachedir = flag.String("cachedir", "/tmp/goposm", "cache directory")
overwritecache = flag.Bool("overwritecache", false, "overwritecache")
appendcache = flag.Bool("appendcache", false, "append cache")
@ -168,6 +171,23 @@ func main() {
defer pprof.StopCPUProfile()
}
if *memprofile != "" {
parts := strings.Split(*memprofile, string(os.PathListSeparator))
var interval time.Duration
if len(parts) < 2 {
interval, _ = time.ParseDuration("1m")
} else {
var err error
interval, err = time.ParseDuration(parts[1])
if err != nil {
log.Fatal(err)
}
}
go stats.MemProfiler(parts[0], interval)
}
osmCache := cache.NewOSMCache(*cachedir)
if *read != "" && osmCache.Exists() {

32
stats/memprof.go Normal file
View File

@ -0,0 +1,32 @@
package stats
import (
"fmt"
"log"
"os"
"path"
"runtime/pprof"
"time"
)
func MemProfiler(dir string, interval time.Duration) {
if err := os.MkdirAll(dir, 0750); err != nil {
panic(err)
}
ticker := time.NewTicker(interval)
i := 0
for _ = range ticker.C {
filename := path.Join(
dir,
fmt.Sprintf("memprof-%03d.pprof", i),
)
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
i++
}
}