Add support for profiling
[pstop.git] / main.go
diff --git a/main.go b/main.go
index 40e39ea..708a77a 100644 (file)
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
        "log"
        "os"
        "os/signal"
+       "runtime/pprof"
        "syscall"
        "time"
 
@@ -26,6 +27,11 @@ const (
        db         = "performance_schema"
 )
 
+var flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
+var flag_debug = flag.Bool("debug", false, "Enabling debug logging")
+var flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
+var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
+
 func get_db_handle() *sql.DB {
        var err error
        var dbh *sql.DB
@@ -67,12 +73,18 @@ func usage() {
 }
 
 func main() {
-       var flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
-       var flag_debug = flag.Bool("debug", false, "Enabling debug logging")
-       var flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
        flag.Parse()
 
        // clean me up
+       if *cpuprofile != "" {
+               f, err := os.Create(*cpuprofile)
+               if err != nil {
+                       log.Fatal(err)
+               }
+               pprof.StartCPUProfile(f)
+               defer pprof.StopCPUProfile()
+       }
+
        if *flag_debug {
                lib.Logger.EnableLogging(true)
        }
@@ -88,7 +100,7 @@ func main() {
        lib.Logger.Println("Starting " + lib.MyName())
        var state state.State
 
-       interval := time.Second * 1 // for the wait, should be configurable
+       interval := time.Second
        sigChan := make(chan os.Signal, 1)
        done := make(chan struct{})
        defer close(done)
@@ -110,6 +122,7 @@ func main() {
                        fmt.Println("Caught a signal", sig)
                        done <- struct{}{}
                case <-ticker.C:
+                       state.Collect()
                        state.Display()
                case event := <-termboxChan:
                        // switch on event type
@@ -120,6 +133,7 @@ func main() {
                                        finished = true
                                case termbox.KeyTab: // tab - change display modes
                                        state.DisplayNext()
+                                       state.Display()
                                }
                                switch event.Ch {
                                case '-': // decrease the interval if > 1
@@ -138,11 +152,14 @@ func main() {
                                        finished = true
                                case 't': // toggle between absolute/relative statistics
                                        state.SetWantRelativeStats(!state.WantRelativeStats())
+                                       state.Display()
                                case 'z': // reset the statistics to now by taking a query of current values
                                        state.ResetDBStatistics()
+                                       state.Display()
                                }
                        case termbox.EventResize: // set sizes
                                state.ScreenSetSize(event.Width, event.Height)
+                               state.Display()
                        case termbox.EventError: // quit
                                log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
                        }