Skip to content

Commit c0343c8

Browse files
committed
cmd/geth: add memory stat collection too
1 parent c6e2af1 commit c0343c8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cmd/geth/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"runtime"
3131
"strconv"
3232
"strings"
33+
"time"
3334

3435
"github.com/codegangsta/cli"
3536
"github.com/ethereum/ethash"
@@ -42,6 +43,7 @@ import (
4243
"github.com/ethereum/go-ethereum/rpc/comms"
4344
"github.com/mattn/go-colorable"
4445
"github.com/mattn/go-isatty"
46+
"github.com/rcrowley/go-metrics"
4547
)
4648

4749
const (
@@ -285,6 +287,28 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
285287
}
286288
return nil
287289
}
290+
// Start system runtime metrics collection
291+
go func() {
292+
used := metrics.GetOrRegisterMeter("system/memory/used", metrics.DefaultRegistry)
293+
total := metrics.GetOrRegisterMeter("system/memory/total", metrics.DefaultRegistry)
294+
mallocs := metrics.GetOrRegisterMeter("system/memory/mallocs", metrics.DefaultRegistry)
295+
frees := metrics.GetOrRegisterMeter("system/memory/frees", metrics.DefaultRegistry)
296+
297+
stats := make([]*runtime.MemStats, 2)
298+
for i := 0; i < len(stats); i++ {
299+
stats[i] = new(runtime.MemStats)
300+
}
301+
for i := 1; ; i++ {
302+
runtime.ReadMemStats(stats[i%2])
303+
304+
used.Mark(int64(stats[i%2].Alloc - stats[(i-1)%2].Alloc))
305+
total.Mark(int64(stats[i%2].TotalAlloc - stats[(i-1)%2].TotalAlloc))
306+
mallocs.Mark(int64(stats[i%2].Mallocs - stats[(i-1)%2].Mallocs))
307+
frees.Mark(int64(stats[i%2].Frees - stats[(i-1)%2].Frees))
308+
309+
time.Sleep(3 * time.Second)
310+
}
311+
}()
288312
}
289313

290314
func main() {

0 commit comments

Comments
 (0)