forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mem.go
76 lines (64 loc) · 2.37 KB
/
mem.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
package devops
import (
"math"
"math/rand"
"time"
"github.com/timescale/tsbs/cmd/tsbs_generate_data/common"
"github.com/timescale/tsbs/cmd/tsbs_generate_data/serialize"
)
var (
labelMem = []byte("mem") // heap optimization
// Choices for modeling a host's memory capacity.
MemoryMaxBytesChoices = []int64{8 << 30, 12 << 30, 16 << 30}
// Field keys for 'mem' points.
MemoryFieldKeys = [][]byte{
[]byte("total"),
[]byte("available"),
[]byte("used"),
[]byte("free"),
[]byte("cached"),
[]byte("buffered"),
[]byte("used_percent"),
[]byte("available_percent"),
[]byte("buffered_percent"),
}
)
type MemMeasurement struct {
*subsystemMeasurement
bytesTotal int64 // this doesn't change
}
func NewMemMeasurement(start time.Time) *MemMeasurement {
sub := newSubsystemMeasurement(start, 3)
bytesTotal := MemoryMaxBytesChoices[rand.Intn(len(MemoryMaxBytesChoices))]
// Reuse NormalDistributions as arguments to other distributions. This is
// safe to do because the higher-level distribution advances the ND and
// immediately uses its value and saves the state
nd := common.ND(0.0, float64(bytesTotal)/64)
// used bytes
sub.distributions[0] = common.CWD(nd, 0.0, float64(bytesTotal), rand.Float64()*float64(bytesTotal))
// cached bytes
sub.distributions[1] = common.CWD(nd, 0.0, float64(bytesTotal), rand.Float64()*float64(bytesTotal))
// buffered bytes
sub.distributions[2] = common.CWD(nd, 0.0, float64(bytesTotal), rand.Float64()*float64(bytesTotal))
return &MemMeasurement{
subsystemMeasurement: sub,
bytesTotal: bytesTotal,
}
}
func (m *MemMeasurement) ToPoint(p *serialize.Point) {
p.SetMeasurementName(labelMem)
p.SetTimestamp(&m.timestamp)
total := m.bytesTotal
used := m.distributions[0].Get()
cached := m.distributions[1].Get()
buffered := m.distributions[2].Get()
p.AppendField(MemoryFieldKeys[0], total)
p.AppendField(MemoryFieldKeys[1], int(math.Floor(float64(total)-used)))
p.AppendField(MemoryFieldKeys[2], int(math.Floor(used)))
p.AppendField(MemoryFieldKeys[3], int(math.Floor(cached)))
p.AppendField(MemoryFieldKeys[4], int(math.Floor(buffered)))
p.AppendField(MemoryFieldKeys[5], int(math.Floor(used)))
p.AppendField(MemoryFieldKeys[6], 100.0*(used/float64(total)))
p.AppendField(MemoryFieldKeys[7], 100.0*(float64(total)-used)/float64(total))
p.AppendField(MemoryFieldKeys[8], 100.0*(float64(total)-buffered)/float64(total))
}