From f717019d887a59d1be6447d6278d374b58872db6 Mon Sep 17 00:00:00 2001 From: David Norton Date: Thu, 11 Jun 2015 15:12:14 -0400 Subject: [PATCH 1/3] fix #2935: hook CPU and memory profiling back up --- CHANGELOG.md | 1 + cmd/influxd/main.go | 51 ------------------------------ cmd/influxd/run/command.go | 2 ++ cmd/influxd/run/server.go | 64 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3093941f21..efd311e110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - [#2943](https://github.com/influxdb/influxdb/issues/2943): Ensure default retention policies are fully replicated - [#2948](https://github.com/influxdb/influxdb/issues/2948): Field mismatch error message to include measurement name - [#2919](https://github.com/influxdb/influxdb/issues/2919): Unable to insert negative floats +- [#2935](https://github.com/influxdb/influxdb/issues/2935): Hook CPU and memory profiling back up. ## v0.9.0 [2015-06-11] diff --git a/cmd/influxd/main.go b/cmd/influxd/main.go index a36032ff13..690b98e7a4 100644 --- a/cmd/influxd/main.go +++ b/cmd/influxd/main.go @@ -4,12 +4,8 @@ import ( "flag" "fmt" "io" - "log" "math/rand" "os" - "os/signal" - "runtime" - "runtime/pprof" "strings" "time" @@ -135,53 +131,6 @@ func ParseCommandName(args []string) (string, []string) { return "", args } -// prof stores the file locations of active profiles. -var prof struct { - cpu *os.File - mem *os.File -} - -// StartProfile initializes the cpu and memory profile, if specified. -func StartProfile(cpuprofile, memprofile string) { - if cpuprofile != "" { - f, err := os.Create(cpuprofile) - if err != nil { - log.Fatalf("cpuprofile: %v", err) - } - prof.cpu = f - pprof.StartCPUProfile(prof.cpu) - } - - if memprofile != "" { - f, err := os.Create(memprofile) - if err != nil { - log.Fatalf("memprofile: %v", err) - } - prof.mem = f - runtime.MemProfileRate = 4096 - } - - go func() { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - StopProfile() - os.Exit(0) - }() -} - -// StopProfile closes the cpu and memory profiles if they are running. -func StopProfile() { - if prof.cpu != nil { - pprof.StopCPUProfile() - prof.cpu.Close() - } - if prof.mem != nil { - pprof.Lookup("heap").WriteTo(prof.mem, 0) - prof.mem.Close() - } -} - // Command represents the command executed by "influxd version". type VersionCommand struct { Stdout io.Writer diff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.go index 91fe04b392..dd0b823d58 100644 --- a/cmd/influxd/run/command.go +++ b/cmd/influxd/run/command.go @@ -93,6 +93,8 @@ func (cmd *Command) Run(args ...string) error { } s.Commit = cmd.Commit s.Version = cmd.Version + s.CPUProfile = options.CPUProfile + s.MemProfile = options.MemProfile if err := s.Open(); err != nil { return fmt.Errorf("open server: %s", err) } diff --git a/cmd/influxd/run/server.go b/cmd/influxd/run/server.go index 3d8707108c..5132a11b01 100644 --- a/cmd/influxd/run/server.go +++ b/cmd/influxd/run/server.go @@ -6,7 +6,10 @@ import ( "log" "net" "net/http" + "os" + "os/signal" "runtime" + "runtime/pprof" "time" "github.com/influxdb/influxdb/cluster" @@ -55,6 +58,10 @@ type Server struct { // Server reporting reportingDisabled bool + + // Profiling + CPUProfile string + MemProfile string } // NewServer returns a new instance of Server built from a config. @@ -247,6 +254,9 @@ func (s *Server) Err() <-chan error { return s.err } // Open opens the meta and data store and all services. func (s *Server) Open() error { if err := func() error { + // Start profiling, if set. + StartProfile(s.CPUProfile, s.MemProfile) + // Resolve host to address. _, port, err := net.SplitHostPort(s.BindAddress) if err != nil { @@ -316,6 +326,9 @@ func (s *Server) Open() error { // Close shuts down the meta and data stores and all services. func (s *Server) Close() error { + fmt.Println("Close()") + StopProfile() + if s.Listener != nil { s.Listener.Close() } @@ -409,3 +422,54 @@ type Service interface { Open() error Close() error } + +// prof stores the file locations of active profiles. +var prof struct { + cpu *os.File + mem *os.File +} + +// StartProfile initializes the cpu and memory profile, if specified. +func StartProfile(cpuprofile, memprofile string) { + if cpuprofile != "" { + f, err := os.Create(cpuprofile) + if err != nil { + log.Fatalf("cpuprofile: %v", err) + } + fmt.Printf("writing CPU profile to: %s\n", cpuprofile) + prof.cpu = f + pprof.StartCPUProfile(prof.cpu) + } + + if memprofile != "" { + f, err := os.Create(memprofile) + if err != nil { + log.Fatalf("memprofile: %v", err) + } + fmt.Printf("writing mem profile to: %s\n", memprofile) + prof.mem = f + go func() { time.Sleep(30 * time.Second); runtime.MemProfileRate = 4096 }() + } + + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + StopProfile() + os.Exit(0) + }() +} + +// StopProfile closes the cpu and memory profiles if they are running. +func StopProfile() { + if prof.cpu != nil { + pprof.StopCPUProfile() + prof.cpu.Close() + fmt.Println("CPU profile stopped") + } + if prof.mem != nil { + pprof.Lookup("heap").WriteTo(prof.mem, 0) + prof.mem.Close() + fmt.Println("mem profile stopped") + } +} From fd2f1bb223ea6dd8cf827db1a245d4e50fca7477 Mon Sep 17 00:00:00 2001 From: David Norton Date: Fri, 12 Jun 2015 10:10:46 -0400 Subject: [PATCH 2/3] cr #2935: make code review changes --- cmd/influxd/run/server.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/cmd/influxd/run/server.go b/cmd/influxd/run/server.go index 5132a11b01..1a487ba6a7 100644 --- a/cmd/influxd/run/server.go +++ b/cmd/influxd/run/server.go @@ -255,7 +255,7 @@ func (s *Server) Err() <-chan error { return s.err } func (s *Server) Open() error { if err := func() error { // Start profiling, if set. - StartProfile(s.CPUProfile, s.MemProfile) + startProfile(s.CPUProfile, s.MemProfile) // Resolve host to address. _, port, err := net.SplitHostPort(s.BindAddress) @@ -326,8 +326,7 @@ func (s *Server) Open() error { // Close shuts down the meta and data stores and all services. func (s *Server) Close() error { - fmt.Println("Close()") - StopProfile() + stopProfile() if s.Listener != nil { s.Listener.Close() @@ -430,13 +429,13 @@ var prof struct { } // StartProfile initializes the cpu and memory profile, if specified. -func StartProfile(cpuprofile, memprofile string) { +func startProfile(cpuprofile, memprofile string) { if cpuprofile != "" { f, err := os.Create(cpuprofile) if err != nil { log.Fatalf("cpuprofile: %v", err) } - fmt.Printf("writing CPU profile to: %s\n", cpuprofile) + log.Printf("writing CPU profile to: %s\n", cpuprofile) prof.cpu = f pprof.StartCPUProfile(prof.cpu) } @@ -446,7 +445,7 @@ func StartProfile(cpuprofile, memprofile string) { if err != nil { log.Fatalf("memprofile: %v", err) } - fmt.Printf("writing mem profile to: %s\n", memprofile) + log.Printf("writing mem profile to: %s\n", memprofile) prof.mem = f go func() { time.Sleep(30 * time.Second); runtime.MemProfileRate = 4096 }() } @@ -455,21 +454,21 @@ func StartProfile(cpuprofile, memprofile string) { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) <-c - StopProfile() + stopProfile() os.Exit(0) }() } // StopProfile closes the cpu and memory profiles if they are running. -func StopProfile() { +func stopProfile() { if prof.cpu != nil { pprof.StopCPUProfile() prof.cpu.Close() - fmt.Println("CPU profile stopped") + log.Println("CPU profile stopped") } if prof.mem != nil { pprof.Lookup("heap").WriteTo(prof.mem, 0) prof.mem.Close() - fmt.Println("mem profile stopped") + log.Println("mem profile stopped") } } From b0b8d2575222702ac65534234bc05bdb31ce1a08 Mon Sep 17 00:00:00 2001 From: David Norton Date: Fri, 12 Jun 2015 12:07:01 -0400 Subject: [PATCH 3/3] fix #2935: remove delay on memprofile --- cmd/influxd/run/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/influxd/run/server.go b/cmd/influxd/run/server.go index 1a487ba6a7..7b79a5ed4a 100644 --- a/cmd/influxd/run/server.go +++ b/cmd/influxd/run/server.go @@ -447,7 +447,7 @@ func startProfile(cpuprofile, memprofile string) { } log.Printf("writing mem profile to: %s\n", memprofile) prof.mem = f - go func() { time.Sleep(30 * time.Second); runtime.MemProfileRate = 4096 }() + runtime.MemProfileRate = 4096 } go func() {