diff --git a/cmd/sam/flags.go b/cmd/sam/flags.go index a9a5c51b6..042fd9b8c 100644 --- a/cmd/sam/flags.go +++ b/cmd/sam/flags.go @@ -6,8 +6,9 @@ import ( ) type configuration struct { - httpAddr string - dbDSN string + httpAddr string + dbDSN string + monitorInterval int } func flags(prefix string, mountFlags ...func()) configuration { @@ -19,6 +20,7 @@ func flags(prefix string, mountFlags ...func()) configuration { flag.StringVar(&config.httpAddr, p("http-addr"), ":3000", "Listen address for HTTP server") flag.StringVar(&config.dbDSN, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") + flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)") for _, mount := range mountFlags { mount() diff --git a/cmd/sam/main.go b/cmd/sam/main.go index ae9c7464e..db7cf2899 100644 --- a/cmd/sam/main.go +++ b/cmd/sam/main.go @@ -30,6 +30,7 @@ func main() { // log to stdout not stderr log.SetOutput(os.Stdout) + NewMonitor(config.monitorInterval) // set up database connection factory.Database.Add("default", config.dbDSN) diff --git a/cmd/sam/monitor.go b/cmd/sam/monitor.go new file mode 100644 index 000000000..dd1dea662 --- /dev/null +++ b/cmd/sam/monitor.go @@ -0,0 +1,48 @@ +package main + +import ( + "encoding/json" + "fmt" + "runtime" + "time" +) + +type Monitor struct { + runtime.MemStats + NumGoroutine int +} + +func (m Monitor) MarshalJSON() ([]byte, error) { + result := struct { + Alloc, + TotalAlloc, + Sys, + Mallocs, + Frees uint64 + + NumGC uint32 + NumGoroutine int + }{ + m.MemStats.Alloc, + m.MemStats.TotalAlloc, + m.MemStats.Sys, + m.MemStats.Mallocs, + m.MemStats.Frees, + + m.MemStats.NumGC, + m.NumGoroutine, + } + return json.Marshal(result) +} + +func NewMonitor(duration int) { + var m Monitor + var interval = time.Duration(duration) * time.Second + for { + <-time.After(interval) + runtime.ReadMemStats(&m.MemStats) + m.NumGoroutine = runtime.NumGoroutine() + b, _ := json.Marshal(m) + fmt.Println(string(b)) + } +}