-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
116 lines (108 loc) · 3.02 KB
/
stats.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package stats
import (
"os"
"runtime"
"sync"
"time"
"github.com/TheThingsNetwork/api"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/process"
)
var startTime = time.Now()
var percentMu sync.RWMutex
var cpuPercentage float64
var processPercentage float64
func init() {
go func() {
for {
cpu, err := cpu.Percent(10*time.Second, false) // Percent sleeps for this duration
if err != nil {
return // CPU profiling not supported on this platform
}
if len(cpu) != 1 {
return // gopsutil no ignores the "false" above
}
percentMu.Lock()
cpuPercentage = cpu[0]
percentMu.Unlock()
}
}()
go func() {
process, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return // Process info not supported on this platform
}
for {
cpu, err := process.Percent(10 * time.Second) // Percent sleeps for this duration
if err != nil {
break // Process CPU profiling not supported on this platform
}
percentMu.Lock()
processPercentage = cpu
percentMu.Unlock()
}
}()
}
// GetSystem gets statistics about the system
func GetSystem() *api.SystemStats {
status := new(api.SystemStats)
if load, err := load.Avg(); err == nil {
status.Load = &api.SystemStats_Loadstats{
Load1: float32(load.Load1),
Load5: float32(load.Load5),
Load15: float32(load.Load15),
}
}
status.Cpu = &api.SystemStats_CPUStats{
Percentage: float32(cpuPercentage),
}
if cpu, err := cpu.Times(false); err == nil && len(cpu) == 1 {
status.Cpu.User = float32(cpu[0].User)
status.Cpu.System = float32(cpu[0].System)
status.Cpu.Idle = float32(cpu[0].Idle)
}
if mem, err := mem.VirtualMemory(); err == nil {
status.Memory = &api.SystemStats_MemoryStats{
Total: mem.Total,
Available: mem.Available,
Used: mem.Used,
}
}
return status
}
// GetComponent gets statistics about this component
func GetComponent() *api.ComponentStats {
status := new(api.ComponentStats)
status.Uptime = uint64(time.Now().Sub(startTime).Seconds())
process, err := process.NewProcess(int32(os.Getpid()))
if err == nil {
if memory, err := process.MemoryInfo(); err == nil {
status.Memory = &api.ComponentStats_MemoryStats{
Memory: memory.RSS,
Swap: memory.Swap,
}
}
status.Cpu = &api.ComponentStats_CPUStats{
Percentage: float32(processPercentage),
}
if cpu, err := process.Times(); err == nil {
status.Cpu.User = float32(cpu.User)
status.Cpu.System = float32(cpu.System)
status.Cpu.Idle = float32(cpu.Idle)
}
}
status.Goroutines = uint64(runtime.NumGoroutine())
memstats := new(runtime.MemStats)
runtime.ReadMemStats(memstats)
status.GcCpuFraction = float32(memstats.GCCPUFraction)
if status.Memory == nil {
status.Memory = new(api.ComponentStats_MemoryStats)
}
status.Memory.Heap = memstats.HeapInuse
status.Memory.Stack = memstats.StackInuse
return status
}