-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage_meta.go
87 lines (69 loc) · 1.92 KB
/
storage_meta.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
package storage
import (
"math"
"runtime"
"sync/atomic"
"github.com/ds2-lab/infinistore/lambda/types"
)
const CalibrateFactor = 0.9
const (
CalibratePriorityNormal types.CalibratePriority = iota
CalibratePriorityRecover
CalibratePriorityMax = CalibratePriorityRecover
)
type StorageMeta struct {
Cap uint64 // Capacity of the lambda.
Overhead uint64 // Minimum overhead reserved.
Rsrved uint64 // Other reserved storage capacity.
overhead float64 // Total storage overhead/usable capacity.
size uint64 // Size of objects stored.
bakSize uint64 // Size of backup objects stored.
memStat runtime.MemStats
modifier types.StorageMeta
}
func (m *StorageMeta) Capacity() uint64 {
return m.Cap
}
func (m *StorageMeta) System() uint64 {
return m.memStat.Sys - m.memStat.HeapSys - m.memStat.GCSys + m.memStat.HeapInuse
}
func (m *StorageMeta) Waterline() uint64 {
return m.memStat.Sys
}
func (m *StorageMeta) Effective() uint64 {
return m.Cap - uint64(math.Ceil(m.overhead)) - m.Rsrved
}
func (m *StorageMeta) Reserved() uint64 {
return m.Rsrved
}
func (m *StorageMeta) ModifiedSize() uint64 {
if m.modifier == nil {
return m.Size()
}
return m.Size() - m.modifier.Size()
}
func (m *StorageMeta) Size() uint64 {
return atomic.LoadUint64(&m.size)
}
func (m *StorageMeta) BackupSize() uint64 {
return m.bakSize
}
func (m *StorageMeta) IncreaseSize(inc uint64) uint64 {
return atomic.AddUint64(&m.size, inc)
}
func (m *StorageMeta) DecreaseSize(dec uint64) uint64 {
return atomic.AddUint64(&m.size, ^(dec - 1))
}
func (m *StorageMeta) Calibrate() {
runtime.GC()
runtime.ReadMemStats(&m.memStat)
overhead := float64(m.memStat.Sys - m.Size() - m.bakSize)
if overhead > m.overhead {
m.overhead = overhead*CalibrateFactor + m.overhead*(1-CalibrateFactor)
} else {
m.overhead = overhead*(1-CalibrateFactor) + m.overhead*CalibrateFactor
}
if m.overhead < float64(m.Overhead) {
m.overhead = float64(m.Overhead)
}
}