added memory profiling

This commit is contained in:
Annika Hannig 2023-04-21 14:37:37 +02:00
parent 735ed87ded
commit d52ff3fe6d
3 changed files with 61 additions and 1 deletions

View File

@ -1 +1 @@
2.2.4
2.2.5

View File

@ -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
View 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++
}
}