/
global_metrics.go
78 lines (68 loc) · 2.03 KB
/
global_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package metrics
import (
"sync"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/identity"
"go.uber.org/atomic"
"github.com/iotaledger/goshimmer/plugins/analysis/packet"
analysisserver "github.com/iotaledger/goshimmer/plugins/analysis/server"
"github.com/iotaledger/goshimmer/plugins/banner"
)
// NodeInfo holds info of a node.
type NodeInfo struct {
OS string
// Arch defines the system architecture of the node.
Arch string
// NumCPU defines number of logical cores of the node.
NumCPU int
// CPUUsage defines the CPU usage of the node.
CPUUsage float64
// MemoryUsage defines the memory usage of the node.
MemoryUsage uint64
}
var (
nodesMetrics = make(map[string]NodeInfo)
nodesMetricsMutex sync.RWMutex
networkDiameter atomic.Int32
)
var onMetricHeartbeatReceived = events.NewClosure(func(hb *packet.MetricHeartbeat) {
nodesMetricsMutex.Lock()
defer nodesMetricsMutex.Unlock()
nodesMetrics[shortNodeIDString(hb.OwnID)] = NodeInfo{
OS: hb.OS,
Arch: hb.Arch,
NumCPU: hb.NumCPU,
CPUUsage: hb.CPUUsage,
MemoryUsage: hb.MemoryUsage,
}
})
// NodesMetrics returns info about the OS, arch, number of cpu cores, cpu load and memory usage.
func NodesMetrics() map[string]NodeInfo {
nodesMetricsMutex.RLock()
defer nodesMetricsMutex.RUnlock()
// create copy of the map
metricsCopy := make(map[string]NodeInfo)
// manually copy content
for node, clientInfo := range nodesMetrics {
metricsCopy[node] = clientInfo
}
return metricsCopy
}
func calculateNetworkDiameter() {
diameter := 0
// TODO: send data for all available networkIDs, not just current
if analysisserver.Networks[banner.SimplifiedAppVersion] != nil {
g := analysisserver.Networks[banner.SimplifiedAppVersion].NetworkGraph()
diameter = g.Diameter()
}
networkDiameter.Store(int32(diameter))
}
// NetworkDiameter returns the current network diameter.
func NetworkDiameter() int32 {
return networkDiameter.Load()
}
func shortNodeIDString(b []byte) string {
var id identity.ID
copy(id[:], b)
return id.String()
}