Skip to content

Commit

Permalink
Support CPU and memory profiling; log number of goroutines every minute
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Aug 31, 2012
1 parent 98020a3 commit 70b6e4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
41 changes: 37 additions & 4 deletions geodns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"runtime/pprof"
"time"
)

Expand All @@ -17,6 +18,9 @@ var (
listen = flag.String("listen", ":8053", "set the listener address")
flaglog = flag.Bool("log", false, "be more verbose")
flagrun = flag.Bool("run", false, "run server")

cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
memprofile = flag.String("memprofile", "", "write memory profile to this file")
)

func main() {
Expand All @@ -27,6 +31,25 @@ func main() {

log.Printf("Starting geodns/%s\n", VERSION)

if *cpuprofile != "" {
prof, err := os.Create(*cpuprofile)
if err != nil {
panic(err.Error())
}

pprof.StartCPUProfile(prof)
defer func() {
log.Println("closing file")
prof.Close()
}()
defer func() {
log.Println("stopping profile")
pprof.StopCPUProfile()
}()
}

go monitor()

dirName := "dns"

Zones := make(Zones)
Expand All @@ -37,11 +60,21 @@ func main() {
go listenAndServe(&Zones)

if *flagrun {
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
terminate := make(chan os.Signal)
signal.Notify(terminate, os.Interrupt)

<-sig
<-terminate
log.Printf("geodns: signal received, stopping")
os.Exit(0)

if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}

//os.Exit(0)
}
}
13 changes: 13 additions & 0 deletions monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"runtime"
"time"
)

func monitor() {
for {
logPrintln("goroutines", runtime.NumGoroutine())
time.Sleep( 60 * time.Second)
}
}

0 comments on commit 70b6e4e

Please sign in to comment.