From d7ebcc54e55f5fe41da370b9fa01dd3fe0502be5 Mon Sep 17 00:00:00 2001 From: zhangwm <442798+zhangwm404@users.noreply.github.com> Date: Mon, 26 Jun 2023 21:15:04 +0800 Subject: [PATCH 1/2] add memory stats to node rest api --- pkg/chainapi/handlers/getnodeinfo.go | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pkg/chainapi/handlers/getnodeinfo.go b/pkg/chainapi/handlers/getnodeinfo.go index 720e1674..a0c32e7a 100644 --- a/pkg/chainapi/handlers/getnodeinfo.go +++ b/pkg/chainapi/handlers/getnodeinfo.go @@ -1,7 +1,9 @@ package handlers import ( + "encoding/json" "fmt" + "runtime" "strings" p2pcrypto "github.com/libp2p/go-libp2p/core/crypto" @@ -17,6 +19,39 @@ type NodeInfo struct { NodeType string `json:"node_type" validate:"required" example:"peer"` NodeVersion string `json:"node_version" validate:"required" example:"1.0.0 - 99bbd8e65105c72b5ca57e94ae5be117eaf05f0d"` Peers map[string][]string `json:"peers" validate:"required"` // Example: {"/quorum/nevis/meshsub/1.1.0": ["16Uiu2HAmM4jFjs5EjakvGgJkHS6Lg9jS6miNYPgJ3pMUvXGWXeTc"]} + Mem NodeInfoMem `json:"mem"` +} + +type ByteSize uint64 +type NodeInfoMem struct { + Sys ByteSize `json:"sys"` // OS memory being used + HeapSys ByteSize `json:"heap_sys"` + HeapAlloc ByteSize `json:"heap_alloc"` + HeapInuse ByteSize `json:"heap_inuse"` + StackSys ByteSize `json:"stack_sys"` + StackInuse ByteSize `json:"stack_inuse"` + NumGC uint32 `json:"num_gc"` +} + +func (v ByteSize) MarshalJSON() ([]byte, error) { + i := int64(v) + s := b2m(i) + return json.Marshal(s) +} + +func b2m(i int64) string { + const unit = 1024 + + if i < unit { + return fmt.Sprintf("%d B", i) + } + + div, exp := int64(unit), 0 + for n := i / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %ciB", float64(i)/float64(div), "KMGTPE"[exp]) } func updateNodeStatus(nodenetworkname string) { @@ -59,5 +94,17 @@ func GetNodeInfo(networkName string) (*NodeInfo, error) { peers := nodectx.GetNodeCtx().PeersProtocol() info.Peers = *peers + var m runtime.MemStats + runtime.ReadMemStats(&m) + info.Mem = NodeInfoMem{ + Sys: ByteSize(m.Sys), + HeapSys: ByteSize(m.HeapSys), + HeapAlloc: ByteSize(m.HeapAlloc), + HeapInuse: ByteSize(m.HeapInuse), + StackSys: ByteSize(m.StackSys), + StackInuse: ByteSize(m.StackInuse), + NumGC: m.NumGC, + } + return &info, nil } From 450065c4c0d23eeb7f87dcf68020d92701d407a0 Mon Sep 17 00:00:00 2001 From: zhangwm <442798+zhangwm404@users.noreply.github.com> Date: Mon, 26 Jun 2023 21:56:31 +0800 Subject: [PATCH 2/2] fix test case failure --- go.mod | 2 +- go.sum | 2 ++ pkg/chainapi/handlers/getnodeinfo.go | 27 ++++++++++++++------------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index c54cd075..ae2984a5 100644 --- a/go.mod +++ b/go.mod @@ -93,7 +93,7 @@ require ( github.com/deckarep/golang-set v1.8.0 // indirect github.com/dgraph-io/ristretto v0.1.0 // indirect github.com/docker/go-units v0.5.0 // indirect - github.com/dustin/go-humanize v1.0.0 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/elastic/gosigar v0.14.2 // indirect github.com/flynn/noise v1.0.0 // indirect github.com/francoispqt/gojay v1.2.13 // indirect diff --git a/go.sum b/go.sum index 9e2c45e1..e007b183 100644 --- a/go.sum +++ b/go.sum @@ -168,6 +168,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4 github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= github.com/edwingeng/deque/v2 v2.1.1 h1:+xjC3TnaeMPLZMi7QQf9jN2K00MZmTwruApqplbL9IY= github.com/edwingeng/deque/v2 v2.1.1/go.mod h1:HukI8CQe9KDmZCcURPZRYVYjH79Zy2tIjTF9sN3Bgb0= diff --git a/pkg/chainapi/handlers/getnodeinfo.go b/pkg/chainapi/handlers/getnodeinfo.go index a0c32e7a..7d2268ee 100644 --- a/pkg/chainapi/handlers/getnodeinfo.go +++ b/pkg/chainapi/handlers/getnodeinfo.go @@ -6,6 +6,7 @@ import ( "runtime" "strings" + "github.com/dustin/go-humanize" p2pcrypto "github.com/libp2p/go-libp2p/core/crypto" "github.com/rumsystem/quorum/internal/pkg/conn/p2p" "github.com/rumsystem/quorum/internal/pkg/nodectx" @@ -34,24 +35,24 @@ type NodeInfoMem struct { } func (v ByteSize) MarshalJSON() ([]byte, error) { - i := int64(v) - s := b2m(i) + i := uint64(v) + s := humanize.Bytes(i) return json.Marshal(s) } -func b2m(i int64) string { - const unit = 1024 - - if i < unit { - return fmt.Sprintf("%d B", i) +func (v *ByteSize) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return err } - - div, exp := int64(unit), 0 - for n := i / unit; n >= unit; n /= unit { - div *= unit - exp++ + n, err := humanize.ParseBytes(s) + if err != nil { + return err } - return fmt.Sprintf("%.1f %ciB", float64(i)/float64(div), "KMGTPE"[exp]) + + *v = ByteSize(n) + + return nil } func updateNodeStatus(nodenetworkname string) {