@@ -14,6 +14,77 @@ import (
14
14
sysinfotypes "github.com/elastic/go-sysinfo/types"
15
15
)
16
16
17
+ // Prefix is an SI prefix for a unit.
18
+ type Prefix string
19
+
20
+ // Float64 returns the prefix as a float64.
21
+ func (m * Prefix ) Float64 () (float64 , error ) {
22
+ switch * m {
23
+ case PrefixDeciShort , PrefixDeci :
24
+ return 0.1 , nil
25
+ case PrefixCentiShort , PrefixCenti :
26
+ return 0.01 , nil
27
+ case PrefixMilliShort , PrefixMilli :
28
+ return 0.001 , nil
29
+ case PrefixMicroShort , PrefixMicro :
30
+ return 0.000_001 , nil
31
+ case PrefixNanoShort , PrefixNano :
32
+ return 0.000_000_001 , nil
33
+ case PrefixKiloShort , PrefixKilo :
34
+ return 1_000.0 , nil
35
+ case PrefixMegaShort , PrefixMega :
36
+ return 1_000_000.0 , nil
37
+ case PrefixGigaShort , PrefixGiga :
38
+ return 1_000_000_000.0 , nil
39
+ case PrefixTeraShort , PrefixTera :
40
+ return 1_000_000_000_000.0 , nil
41
+ case PrefixKibiShort , PrefixKibi :
42
+ return 1024.0 , nil
43
+ case PrefixMebiShort , PrefixMebi :
44
+ return 1_048_576.0 , nil
45
+ case PrefixGibiShort , PrefixGibi :
46
+ return 1_073_741_824.0 , nil
47
+ case PrefixTebiShort , PrefixTebi :
48
+ return 1_099_511_627_776.0 , nil
49
+ default :
50
+ return 0 , xerrors .Errorf ("unknown prefix: %s" , * m )
51
+ }
52
+ }
53
+
54
+ const (
55
+ PrefixDeci Prefix = "deci"
56
+ PrefixCenti Prefix = "centi"
57
+ PrefixMilli Prefix = "milli"
58
+ PrefixMicro Prefix = "micro"
59
+ PrefixNano Prefix = "nano"
60
+
61
+ PrefixDeciShort Prefix = "d"
62
+ PrefixCentiShort Prefix = "c"
63
+ PrefixMilliShort Prefix = "m"
64
+ PrefixMicroShort Prefix = "u"
65
+ PrefixNanoShort Prefix = "n"
66
+
67
+ PrefixKilo Prefix = "kilo"
68
+ PrefixMega Prefix = "mega"
69
+ PrefixGiga Prefix = "giga"
70
+ PrefixTera Prefix = "tera"
71
+
72
+ PrefixKiloShort Prefix = "K"
73
+ PrefixMegaShort Prefix = "M"
74
+ PrefixGigaShort Prefix = "G"
75
+ PrefixTeraShort Prefix = "T"
76
+
77
+ PrefixKibi = "kibi"
78
+ PrefixMebi = "mebi"
79
+ PrefixGibi = "gibi"
80
+ PrefixTebi = "tebi"
81
+
82
+ PrefixKibiShort Prefix = "Ki"
83
+ PrefixMebiShort Prefix = "Mi"
84
+ PrefixGibiShort Prefix = "Gi"
85
+ PrefixTebiShort Prefix = "Ti"
86
+ )
87
+
17
88
// Result is a generic result type for a statistic.
18
89
// Total is the total amount of the resource available.
19
90
// It is nil if the resource is not a finite quantity.
@@ -23,6 +94,8 @@ type Result struct {
23
94
Total * float64 `json:"total"`
24
95
Unit string `json:"unit"`
25
96
Used float64 `json:"used"`
97
+ // Prefix controls the string representation of the result.
98
+ Prefix Prefix `json:"-"`
26
99
}
27
100
28
101
// String returns a human-readable representation of the result.
@@ -31,13 +104,20 @@ func (r *Result) String() string {
31
104
return "-"
32
105
}
33
106
var sb strings.Builder
34
- _ , _ = sb .WriteString (strconv .FormatFloat (r .Used , 'f' , 1 , 64 ))
107
+ scale , err := r .Prefix .Float64 ()
108
+ prefix := string (r .Prefix )
109
+ if err != nil {
110
+ prefix = ""
111
+ scale = 1.0
112
+ }
113
+ _ , _ = sb .WriteString (strconv .FormatFloat (r .Used / scale , 'f' , 1 , 64 ))
35
114
if r .Total != (* float64 )(nil ) {
36
115
_ , _ = sb .WriteString ("/" )
37
- _ , _ = sb .WriteString (strconv .FormatFloat (* r .Total , 'f' , 1 , 64 ))
116
+ _ , _ = sb .WriteString (strconv .FormatFloat (* r .Total / scale , 'f' , 1 , 64 ))
38
117
}
39
118
if r .Unit != "" {
40
119
_ , _ = sb .WriteString (" " )
120
+ _ , _ = sb .WriteString (prefix )
41
121
_ , _ = sb .WriteString (r .Unit )
42
122
}
43
123
return sb .String ()
@@ -96,7 +176,7 @@ func New(opts ...Option) (*Statter, error) {
96
176
// This is calculated by taking the difference between the total and idle HostCPU time
97
177
// and scaling it by the number of cores.
98
178
// Units are in "cores".
99
- func (s * Statter ) HostCPU () (* Result , error ) {
179
+ func (s * Statter ) HostCPU (m Prefix ) (* Result , error ) {
100
180
r := & Result {
101
181
Unit : "cores" ,
102
182
Total : ptr .To (float64 (s .nproc )),
@@ -115,19 +195,21 @@ func (s *Statter) HostCPU() (*Result, error) {
115
195
used := total - idle
116
196
scaleFactor := float64 (s .nproc ) / total .Seconds ()
117
197
r .Used = used .Seconds () * scaleFactor
198
+ r .Prefix = m
118
199
return r , nil
119
200
}
120
201
121
202
// HostMemory returns the memory usage of the host, in gigabytes.
122
- func (s * Statter ) HostMemory () (* Result , error ) {
203
+ func (s * Statter ) HostMemory (m Prefix ) (* Result , error ) {
123
204
r := & Result {
124
- Unit : "GB" ,
205
+ Unit : "B" ,
206
+ Prefix : m ,
125
207
}
126
208
hm , err := s .hi .Memory ()
127
209
if err != nil {
128
210
return nil , xerrors .Errorf ("get memory info: %w" , err )
129
211
}
130
- r .Total = ptr .To (float64 (hm .Total ) / 1024 / 1024 / 1024 )
131
- r .Used = float64 (hm .Used ) / 1024 / 1024 / 1024
212
+ r .Total = ptr .To (float64 (hm .Total ))
213
+ r .Used = float64 (hm .Used )
132
214
return r , nil
133
215
}
0 commit comments