forked from cloudfoundry/grootfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurer_linux.go
145 lines (116 loc) · 3.35 KB
/
measurer_linux.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package store
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall"
"code.cloudfoundry.org/lager"
errorspkg "github.com/pkg/errors"
)
//go:generate counterfeiter . VolumeDriver
//go:generate counterfeiter . UnusedVolumeGetter
type VolumeDriver interface {
VolumeSize(lager.Logger, string) (int64, error)
Volumes(lager.Logger) ([]string, error)
}
type UnusedVolumeGetter interface {
UnusedVolumes(lager.Logger) ([]string, error)
}
type StoreMeasurer struct {
storePath string
volumeDriver VolumeDriver
unusedVolumeGetter UnusedVolumeGetter
}
func NewStoreMeasurer(storePath string, volumeDriver VolumeDriver, unusedVolumeGetter UnusedVolumeGetter) *StoreMeasurer {
return &StoreMeasurer{
storePath: storePath,
volumeDriver: volumeDriver,
unusedVolumeGetter: unusedVolumeGetter,
}
}
func (s *StoreMeasurer) UnusedVolumesSize(logger lager.Logger) (int64, error) {
unusedVols, err := s.unusedVolumeGetter.UnusedVolumes(logger)
if err != nil {
return 0, err
}
return s.countVolumesSize(logger, unusedVols)
}
func (s *StoreMeasurer) UsedVolumesSize(logger lager.Logger) (int64, error) {
totalVolumesSize, err := s.TotalVolumesSize(logger)
if err != nil {
return 0, err
}
unusedVolumesSize, err := s.UnusedVolumesSize(logger)
if err != nil {
return 0, err
}
usedVolumesSize := totalVolumesSize - unusedVolumesSize
return usedVolumesSize, nil
}
func (s *StoreMeasurer) TotalVolumesSize(logger lager.Logger) (int64, error) {
vols, err := s.volumeDriver.Volumes(logger)
if err != nil {
return 0, err
}
return s.countVolumesSize(logger, vols)
}
func (s *StoreMeasurer) countVolumesSize(logger lager.Logger, volumes []string) (int64, error) {
var size int64
for _, volume := range volumes {
volumeSize, err := s.volumeDriver.VolumeSize(logger, volume)
if err != nil && !os.IsNotExist(err) {
return 0, err
}
size += volumeSize
}
return size, nil
}
func (s *StoreMeasurer) CommittedQuota(logger lager.Logger) (int64, error) {
logger = logger.Session("measuring-committed-size")
logger.Debug("starting")
defer logger.Debug("ending")
var totalCommittedSpace int64
imageDir := filepath.Join(s.storePath, "images")
files, err := ioutil.ReadDir(imageDir)
if err != nil {
return 0, errorspkg.Wrapf(err, "Cannot list images in %s", imageDir)
}
for _, file := range files {
if !file.IsDir() {
continue
}
imageQuota, err := readImageQuota(filepath.Join(imageDir, file.Name()))
if err != nil && !os.IsNotExist(err) {
return 0, err
}
totalCommittedSpace += imageQuota
}
return totalCommittedSpace, nil
}
func readImageQuota(imageDir string) (int64, error) {
quotaFilePath := filepath.Join(imageDir, "image_quota")
imageQuotaBytes, err := ioutil.ReadFile(quotaFilePath)
if err != nil {
return 0, err
}
if len(imageQuotaBytes) == 0 {
return 0, nil
}
imageQuota, err := strconv.ParseInt(string(imageQuotaBytes), 10, 64)
if err != nil {
return 0, err
}
return imageQuota, nil
}
func (s *StoreMeasurer) PathStats(path string) (totalBytes, UsedBytes int64, err error) {
stats := syscall.Statfs_t{}
if err = syscall.Statfs(s.storePath, &stats); err != nil {
return 0, 0, errorspkg.Wrapf(err, "Invalid path %s", s.storePath)
}
bsize := uint64(stats.Bsize)
free := stats.Bfree * bsize
total := stats.Blocks * bsize
used := int64(total - free)
return int64(total), used, nil
}