Selaa lähdekoodia

Support CPU and memory profiling; log number of goroutines every minute

Ask Bjørn Hansen 13 vuotta sitten
vanhempi
commit
70b6e4e413
2 muutettua tiedostoa jossa 50 lisäystä ja 4 poistoa
  1. 37 4
      geodns.go
  2. 13 0
      monitor.go

+ 37 - 4
geodns.go

@@ -5,6 +5,7 @@ import (
 	"log"
 	"os"
 	"os/signal"
+	"runtime/pprof"
 	"time"
 )
 
@@ -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() {
@@ -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)
@@ -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 - 0
monitor.go

@@ -0,0 +1,13 @@
+package main
+
+import (
+	"runtime"
+	"time"
+)
+
+func monitor() {
+	for {
+		logPrintln("goroutines", runtime.NumGoroutine())
+		time.Sleep( 60 * time.Second)
+	}
+}