forked from lbryio/lbcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourceLogging.go
74 lines (63 loc) · 1.48 KB
/
resourceLogging.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
package main
import (
"fmt"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/process"
"os"
"path/filepath"
"time"
)
func toGB(n uint64) float64 {
return float64(n) / 1024.0 / 1024.0 / 1024.0
}
func dirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func logMemoryUsage() {
last := ""
tick := time.NewTicker(40 * time.Second)
for range tick.C {
m, err := mem.VirtualMemory()
if err != nil {
btcdLog.Warnf("When reading memory size: %s", err.Error())
continue
}
d, err := disk.Usage(cfg.DataDir)
if err != nil {
btcdLog.Warnf("When reading disk usage: %s", err.Error())
continue
}
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
btcdLog.Warnf("When reading process: %s", err.Error())
continue
}
m2, err := p.MemoryInfo()
if err != nil {
btcdLog.Warnf("When reading memory info: %s", err.Error())
continue
}
ds, err := dirSize(cfg.DataDir)
if err != nil {
btcdLog.Debugf("When reading directory: %s", err.Error())
continue
}
cur := fmt.Sprintf("RAM: using %.1f GB with %.1f available, DISK: using %.1f GB with %.1f available",
toGB(m2.RSS), toGB(m.Available), toGB(uint64(ds)), toGB(d.Free))
if cur != last {
btcdLog.Infof(cur)
last = cur
}
}
}