Skip to content

Commit 2aeeb72

Browse files
karalabeobscuren
authored andcommitted
cmd/geth, metrics: separate process metric collection, add disk
1 parent 7c4ed80 commit 2aeeb72

5 files changed

Lines changed: 130 additions & 22 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ import (
3939
"github.com/ethereum/go-ethereum/common"
4040
"github.com/ethereum/go-ethereum/eth"
4141
"github.com/ethereum/go-ethereum/logger"
42+
"github.com/ethereum/go-ethereum/metrics"
4243
"github.com/ethereum/go-ethereum/rpc/codec"
4344
"github.com/ethereum/go-ethereum/rpc/comms"
4445
"github.com/mattn/go-colorable"
4546
"github.com/mattn/go-isatty"
46-
"github.com/rcrowley/go-metrics"
4747
)
4848

4949
const (
@@ -288,27 +288,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
288288
return nil
289289
}
290290
// Start system runtime metrics collection
291-
go func() {
292-
allocs := metrics.GetOrRegisterMeter("system/memory/allocs", metrics.DefaultRegistry)
293-
frees := metrics.GetOrRegisterMeter("system/memory/frees", metrics.DefaultRegistry)
294-
inuse := metrics.GetOrRegisterMeter("system/memory/inuse", metrics.DefaultRegistry)
295-
pauses := metrics.GetOrRegisterMeter("system/memory/pauses", 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-
allocs.Mark(int64(stats[i%2].Mallocs - stats[(i-1)%2].Mallocs))
305-
frees.Mark(int64(stats[i%2].Frees - stats[(i-1)%2].Frees))
306-
inuse.Mark(int64(stats[i%2].Alloc - stats[(i-1)%2].Alloc))
307-
pauses.Mark(int64(stats[i%2].PauseTotalNs - stats[(i-1)%2].PauseTotalNs))
308-
309-
time.Sleep(3 * time.Second)
310-
}
311-
}()
291+
go metrics.CollectProcessMetrics(3 * time.Second)
312292
}
313293

314294
func main() {

‎metrics/disk.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package metrics
2+
3+
// DiskStats is the per process disk io stats.
4+
type DiskStats struct {
5+
ReadCount int64 // Number of read operations executed
6+
ReadBytes int64 // Total number of bytes read
7+
WriteCount int64 // Number of write operations executed
8+
WriteBytes int64 // Total number of byte written
9+
}

‎metrics/disk_linux.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Contains the Linux implementation of process disk IO counter retrieval.
2+
3+
package metrics
4+
5+
import (
6+
"bufio"
7+
"fmt"
8+
"io"
9+
"os"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
// ReadDiskStats retrieves the disk IO stats belonging to the current process.
15+
func ReadDiskStats(stats *DiskStats) error {
16+
// Open the process disk IO counter file
17+
inf, err := os.Open(fmt.Sprintf("/proc/%d/io", os.Getpid()))
18+
if err != nil {
19+
return err
20+
}
21+
in := bufio.NewReader(inf)
22+
23+
// Iterate over the IO counter, and extract what we need
24+
for {
25+
// Read the next line and split to key and value
26+
line, err := in.ReadString('\n')
27+
if err != nil {
28+
if err == io.EOF {
29+
return nil
30+
}
31+
return err
32+
}
33+
key, value := "", int64(0)
34+
if parts := strings.Split(line, ":"); len(parts) != 2 {
35+
continue
36+
} else {
37+
key = strings.TrimSpace(parts[0])
38+
if value, err = strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64); err != nil {
39+
return err
40+
}
41+
}
42+
// Update the counter based on the key
43+
switch key {
44+
case "syscr":
45+
stats.ReadCount = value
46+
case "syscw":
47+
stats.WriteCount = value
48+
case "rchar":
49+
stats.ReadBytes = value
50+
case "wchar":
51+
stats.WriteBytes = value
52+
}
53+
}
54+
return nil
55+
}

‎metrics/disk_nop.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// +build !linux
2+
3+
package metrics
4+
5+
import "errors"
6+
7+
// ReadDiskStats retrieves the disk IO stats belonging to the current process.
8+
func ReadDiskStats(stats *DiskStats) error {
9+
return errors.New("Not implemented")
10+
}

‎metrics/metrics.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Package metrics provides general system and process level metrics collection.
2+
package metrics
3+
4+
import (
5+
"runtime"
6+
"time"
7+
8+
"github.com/ethereum/go-ethereum/logger"
9+
"github.com/ethereum/go-ethereum/logger/glog"
10+
"github.com/rcrowley/go-metrics"
11+
)
12+
13+
// CollectProcessMetrics periodically collects various metrics about the running
14+
// process.
15+
func CollectProcessMetrics(refresh time.Duration) {
16+
// Create the various data collectors
17+
memstats := make([]*runtime.MemStats, 2)
18+
diskstats := make([]*DiskStats, 2)
19+
for i := 0; i < len(memstats); i++ {
20+
memstats[i] = new(runtime.MemStats)
21+
diskstats[i] = new(DiskStats)
22+
}
23+
// Define the various metrics to collect
24+
memAllocs := metrics.GetOrRegisterMeter("system/memory/allocs", metrics.DefaultRegistry)
25+
memFrees := metrics.GetOrRegisterMeter("system/memory/frees", metrics.DefaultRegistry)
26+
memInuse := metrics.GetOrRegisterMeter("system/memory/inuse", metrics.DefaultRegistry)
27+
memPauses := metrics.GetOrRegisterMeter("system/memory/pauses", metrics.DefaultRegistry)
28+
29+
var diskReads, diskReadBytes, diskWrites, diskWriteBytes metrics.Meter
30+
if err := ReadDiskStats(diskstats[0]); err == nil {
31+
diskReads = metrics.GetOrRegisterMeter("system/disk/readcount", metrics.DefaultRegistry)
32+
diskReadBytes = metrics.GetOrRegisterMeter("system/disk/readdata", metrics.DefaultRegistry)
33+
diskWrites = metrics.GetOrRegisterMeter("system/disk/writecount", metrics.DefaultRegistry)
34+
diskWriteBytes = metrics.GetOrRegisterMeter("system/disk/writedata", metrics.DefaultRegistry)
35+
} else {
36+
glog.V(logger.Debug).Infof("failed to read disk metrics: %v", err)
37+
}
38+
// Iterate loading the different stats and updating the meters
39+
for i := 1; ; i++ {
40+
runtime.ReadMemStats(memstats[i%2])
41+
memAllocs.Mark(int64(memstats[i%2].Mallocs - memstats[(i-1)%2].Mallocs))
42+
memFrees.Mark(int64(memstats[i%2].Frees - memstats[(i-1)%2].Frees))
43+
memInuse.Mark(int64(memstats[i%2].Alloc - memstats[(i-1)%2].Alloc))
44+
memPauses.Mark(int64(memstats[i%2].PauseTotalNs - memstats[(i-1)%2].PauseTotalNs))
45+
46+
if ReadDiskStats(diskstats[i%2]) == nil {
47+
diskReads.Mark(int64(diskstats[i%2].ReadCount - diskstats[(i-1)%2].ReadCount))
48+
diskReadBytes.Mark(int64(diskstats[i%2].ReadBytes - diskstats[(i-1)%2].ReadBytes))
49+
diskWrites.Mark(int64(diskstats[i%2].WriteCount - diskstats[(i-1)%2].WriteCount))
50+
diskWriteBytes.Mark(int64(diskstats[i%2].WriteBytes - diskstats[(i-1)%2].WriteBytes))
51+
}
52+
time.Sleep(refresh)
53+
}
54+
}

0 commit comments

Comments
 (0)