Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.

Commit a35382d

Browse files
authored
metrics: replace gosigar with gopsutil (ethereum#21041)
* replace gosigar with gopsutil * removed check for whether GOOS is openbsd * removed accidental import of runtime * potential fix for difference in units between gosig and gopsutil * fixed lint error * remove multiplication factor * uses cpu.ClocksPerSec as the multiplication factor * changed dependency from shirou to renaynay (#20) * updated dep * switching back from using renaynay fork to using upstream as PRs were merged on upstream * removed empty line * optimized imports * tidied go mod
1 parent a5eee8d commit a35382d

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

cmd/geth/main.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import (
2121
"fmt"
2222
"math"
2323
"os"
24-
"runtime"
2524
godebug "runtime/debug"
2625
"sort"
2726
"strconv"
2827
"strings"
2928
"time"
3029

31-
"github.com/elastic/gosigar"
3230
"github.com/ethereum/go-ethereum/accounts"
3331
"github.com/ethereum/go-ethereum/accounts/keystore"
3432
"github.com/ethereum/go-ethereum/cmd/utils"
@@ -42,6 +40,7 @@ import (
4240
"github.com/ethereum/go-ethereum/log"
4341
"github.com/ethereum/go-ethereum/metrics"
4442
"github.com/ethereum/go-ethereum/node"
43+
gopsutil "github.com/shirou/gopsutil/mem"
4544
cli "gopkg.in/urfave/cli.v1"
4645
)
4746

@@ -310,20 +309,16 @@ func prepare(ctx *cli.Context) {
310309
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(128))
311310
}
312311
// Cap the cache allowance and tune the garbage collector
313-
var mem gosigar.Mem
314-
// Workaround until OpenBSD support lands into gosigar
315-
// Check https://github.com/elastic/gosigar#supported-platforms
316-
if runtime.GOOS != "openbsd" {
317-
if err := mem.Get(); err == nil {
318-
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
319-
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
320-
mem.Total = 2 * 1024 * 1024 * 1024
321-
}
322-
allowance := int(mem.Total / 1024 / 1024 / 3)
323-
if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance {
324-
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
325-
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance))
326-
}
312+
mem, err := gopsutil.VirtualMemory()
313+
if err == nil {
314+
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
315+
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
316+
mem.Total = 2 * 1024 * 1024 * 1024
317+
}
318+
allowance := int(mem.Total / 1024 / 1024 / 3)
319+
if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance {
320+
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
321+
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance))
327322
}
328323
}
329324
// Ensure Go's GC ignores the database cache for trigger percentage

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf
2020
github.com/dop251/goja v0.0.0-20200219165308-d1232e640a87
2121
github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c
22-
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa
2322
github.com/fatih/color v1.3.0
2423
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc
2524
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
@@ -50,6 +49,7 @@ require (
5049
github.com/rjeczalik/notify v0.9.1
5150
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00
5251
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521 // indirect
52+
github.com/shirou/gopsutil v2.20.5-0.20200531151128-663af789c085+incompatible
5353
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
5454
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570
5555
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ github.com/dop251/goja v0.0.0-20200219165308-d1232e640a87 h1:OMbqMXf9OAXzH1dDH82
6161
github.com/dop251/goja v0.0.0-20200219165308-d1232e640a87/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
6262
github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c h1:JHHhtb9XWJrGNMcrVP6vyzO4dusgi/HnceHTgxSejUM=
6363
github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
64-
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa h1:XKAhUk/dtp+CV0VO6mhG2V7jA9vbcGcnYF/Ay9NjZrY=
65-
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa/go.mod h1:cdorVVzy1fhmEqmtgqkoE3bYtCfSCkVyjTyCIo22xvs=
6664
github.com/fatih/color v1.3.0 h1:YehCCcyeQ6Km0D6+IapqPinWBK6y+0eB5umvZXK9WPs=
6765
github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
6866
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc h1:jtW8jbpkO4YirRSyepBOH8E+2HEw6/hKkBvFPwhUN8c=
@@ -167,6 +165,8 @@ github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00/go.mod h1:gFx+x8UowdsKA9Ac
167165
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521 h1:3hxavr+IHMsQBrYUPQM5v0CgENFktkkbg1sfpgM3h20=
168166
github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ=
169167
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
168+
github.com/shirou/gopsutil v2.20.5-0.20200531151128-663af789c085+incompatible h1:+gAR1bMhuoQnZMTWFIvp7ukynULPsteLzG+siZKLtD8=
169+
github.com/shirou/gopsutil v2.20.5-0.20200531151128-663af789c085+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
170170
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
171171
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
172172
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg=

metrics/cpu_enabled.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818

1919
package metrics
2020

21-
import "github.com/elastic/gosigar"
21+
import (
22+
"github.com/ethereum/go-ethereum/log"
23+
"github.com/shirou/gopsutil/cpu"
24+
)
2225

2326
// ReadCPUStats retrieves the current CPU stats.
2427
func ReadCPUStats(stats *CPUStats) {
25-
global := gosigar.Cpu{}
26-
global.Get()
27-
28-
stats.GlobalTime = int64(global.User + global.Nice + global.Sys)
29-
stats.GlobalWait = int64(global.Wait)
28+
// passing false to request all cpu times
29+
timeStats, err := cpu.Times(false)
30+
if err != nil {
31+
log.Error("Could not read cpu stats", "err", err)
32+
return
33+
}
34+
// requesting all cpu times will always return an array with only one time stats entry
35+
timeStat := timeStats[0]
36+
stats.GlobalTime = int64((timeStat.User + timeStat.Nice + timeStat.System) * cpu.ClocksPerSec)
37+
stats.GlobalWait = int64((timeStat.Iowait) * cpu.ClocksPerSec)
3038
stats.LocalTime = getProcessCPUTime()
3139
}

0 commit comments

Comments
 (0)