mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
added memory profiling
This commit is contained in:
parent
735ed87ded
commit
d52ff3fe6d
3 changed files with 61 additions and 1 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
2.2.4
|
||||
2.2.5
|
||||
|
|
|
@ -150,8 +150,17 @@ func main() {
|
|||
bird6 := flag.Bool("6", false, "Use bird6 instead of bird")
|
||||
workerPoolSize := flag.Int("worker-pool-size", 8, "Number of go routines used to parse routing tables concurrently")
|
||||
configfile := flag.String("config", "/etc/birdwatcher/birdwatcher.conf", "Configuration file location")
|
||||
|
||||
// Profiling
|
||||
memoryProfile := flag.String("memprofile", "", "write memory profile to this file")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Start memory profiling if filename is present
|
||||
if *memoryProfile != "" {
|
||||
go startMemoryProfile(*memoryProfile)
|
||||
}
|
||||
|
||||
bird.WorkerPoolSize = *workerPoolSize
|
||||
|
||||
conf, err := LoadConfigs([]string{*configfile})
|
||||
|
|
51
profiling.go
Normal file
51
profiling.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Write a heap profile to the given file.
|
||||
func createHeapProfile(filename string) {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
defer f.Close() // error handling omitted for example
|
||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Write a memory allocation profile to the given file.
|
||||
func createAllocProfile(filename string) {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
log.Fatal("could not create alloc profile: ", err)
|
||||
}
|
||||
defer f.Close() // error handling omitted for example
|
||||
if err := pprof.Lookup("allocs").WriteTo(f, 0); err != nil {
|
||||
log.Fatal("could not write alloc profile: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start a goroutine to periodically write memory profiles.
|
||||
func startMemoryProfile(prefix string) {
|
||||
t := 0
|
||||
log.Println("Starting memory profiling:", prefix)
|
||||
for {
|
||||
filename := fmt.Sprintf("%s-heap-%03d", prefix, t)
|
||||
runtime.GC() // get up-to-date statistics (according to docs)
|
||||
createHeapProfile(filename)
|
||||
log.Println("Wrote memory heap profile:", filename)
|
||||
filename = fmt.Sprintf("%s-allocs-%03d", prefix, t)
|
||||
log.Println("Wrote memory allocs profile:", filename)
|
||||
createAllocProfile(filename)
|
||||
time.Sleep(30 * time.Second)
|
||||
t++
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue