Skip to content

Commit

Permalink
Merge pull request #311 from zhangwm404/main
Browse files Browse the repository at this point in the history
add memory stats to node rest api
  • Loading branch information
huo-ju authored Jun 27, 2023
2 parents fbd5d36 + 450065c commit 7dd400c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
48 changes: 48 additions & 0 deletions pkg/chainapi/handlers/getnodeinfo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package handlers

import (
"encoding/json"
"fmt"
"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"
Expand All @@ -17,6 +20,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 := uint64(v)
s := humanize.Bytes(i)
return json.Marshal(s)
}

func (v *ByteSize) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
n, err := humanize.ParseBytes(s)
if err != nil {
return err
}

*v = ByteSize(n)

return nil
}

func updateNodeStatus(nodenetworkname string) {
Expand Down Expand Up @@ -59,5 +95,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
}

0 comments on commit 7dd400c

Please sign in to comment.