forked from intel/goresctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroupblkio.go
313 lines (285 loc) · 11.4 KB
/
cgroupblkio.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2020 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgroups
import (
"fmt"
"strconv"
"strings"
"github.com/hashicorp/go-multierror"
)
// cgroups blkio parameter filenames.
var blkioWeightFiles = []string{"blkio.bfq.weight", "blkio.weight"}
var blkioWeightDeviceFiles = []string{"blkio.bfq.weight_device", "blkio.weight_device"}
var blkioThrottleReadBpsFiles = []string{"blkio.throttle.read_bps_device"}
var blkioThrottleWriteBpsFiles = []string{"blkio.throttle.write_bps_device"}
var blkioThrottleReadIOPSFiles = []string{"blkio.throttle.read_iops_device"}
var blkioThrottleWriteIOPSFiles = []string{"blkio.throttle.write_iops_device"}
// OciBlockIOParameters contains OCI standard configuration of cgroups blkio parameters.
//
// Effects of Weight and Rate values in SetBlkioParameters():
// Value | Effect
// -------+-------------------------------------------------------------------
// -1 | Do not write to cgroups, value is missing
// 0 | Write to cgroups, will remove the setting as specified in cgroups blkio interface
// other | Write to cgroups, sets the value
type OciBlockIOParameters struct {
Weight int64
WeightDevice OciDeviceWeights
ThrottleReadBpsDevice OciDeviceRates
ThrottleWriteBpsDevice OciDeviceRates
ThrottleReadIOPSDevice OciDeviceRates
ThrottleWriteIOPSDevice OciDeviceRates
}
// OciDeviceWeight contains values for
// - blkio.[io-scheduler].weight
type OciDeviceWeight struct {
Major int64
Minor int64
Weight int64
}
// OciDeviceRate contains values for
// - blkio.throttle.read_bps_device
// - blkio.throttle.write_bps_device
// - blkio.throttle.read_iops_device
// - blkio.throttle.write_iops_device
type OciDeviceRate struct {
Major int64
Minor int64
Rate int64
}
// OciDeviceWeights contains weights for devices
type OciDeviceWeights []OciDeviceWeight
// OciDeviceRates contains throttling rates for devices
type OciDeviceRates []OciDeviceRate
// OciDeviceParameters interface provides functions common to OciDeviceWeights and OciDeviceRates
type OciDeviceParameters interface {
Append(maj, min, val int64)
Update(maj, min, val int64)
}
// Append appends (major, minor, value) to OciDeviceWeights slice.
func (w *OciDeviceWeights) Append(maj, min, val int64) {
*w = append(*w, OciDeviceWeight{Major: maj, Minor: min, Weight: val})
}
// Append appends (major, minor, value) to OciDeviceRates slice.
func (r *OciDeviceRates) Append(maj, min, val int64) {
*r = append(*r, OciDeviceRate{Major: maj, Minor: min, Rate: val})
}
// Update updates device weight in OciDeviceWeights slice, or appends it if not found.
func (w *OciDeviceWeights) Update(maj, min, val int64) {
for index, devWeight := range *w {
if devWeight.Major == maj && devWeight.Minor == min {
(*w)[index].Weight = val
return
}
}
w.Append(maj, min, val)
}
// Update updates device rate in OciDeviceRates slice, or appends it if not found.
func (r *OciDeviceRates) Update(maj, min, val int64) {
for index, devRate := range *r {
if devRate.Major == maj && devRate.Minor == min {
(*r)[index].Rate = val
return
}
}
r.Append(maj, min, val)
}
// NewOciBlockIOParameters creates new OciBlockIOParameters instance.
func NewOciBlockIOParameters() OciBlockIOParameters {
return OciBlockIOParameters{
Weight: -1,
}
}
// NewOciDeviceWeight creates new OciDeviceWeight instance.
func NewOciDeviceWeight() OciDeviceWeight {
return OciDeviceWeight{
Major: -1,
Minor: -1,
Weight: -1,
}
}
// NewOciDeviceRate creates new OciDeviceRate instance.
func NewOciDeviceRate() OciDeviceRate {
return OciDeviceRate{
Major: -1,
Minor: -1,
Rate: -1,
}
}
type devMajMin struct {
Major int64
Minor int64
}
// ResetBlkioParameters adds new, changes existing and removes missing blockIO parameters in cgroupsDir
func ResetBlkioParameters(cntnrDir string, blockIO OciBlockIOParameters) error {
var errors *multierror.Error
oldBlockIO, getErr := GetBlkioParameters(cntnrDir)
errors = multierror.Append(errors, getErr)
newBlockIO := NewOciBlockIOParameters()
newBlockIO.Weight = blockIO.Weight
newBlockIO.WeightDevice = resetDevWeights(oldBlockIO.WeightDevice, blockIO.WeightDevice)
newBlockIO.ThrottleReadBpsDevice = resetDevRates(oldBlockIO.ThrottleReadBpsDevice, blockIO.ThrottleReadBpsDevice)
newBlockIO.ThrottleWriteBpsDevice = resetDevRates(oldBlockIO.ThrottleWriteBpsDevice, blockIO.ThrottleWriteBpsDevice)
newBlockIO.ThrottleReadIOPSDevice = resetDevRates(oldBlockIO.ThrottleReadIOPSDevice, blockIO.ThrottleReadIOPSDevice)
newBlockIO.ThrottleWriteIOPSDevice = resetDevRates(oldBlockIO.ThrottleWriteIOPSDevice, blockIO.ThrottleWriteIOPSDevice)
errors = multierror.Append(errors, SetBlkioParameters(cntnrDir, newBlockIO))
return errors.ErrorOrNil()
}
// resetDevWeights adds wanted weight parameters to new and resets unwanted weights
func resetDevWeights(old, wanted []OciDeviceWeight) []OciDeviceWeight {
new := []OciDeviceWeight{}
seenDev := map[devMajMin]bool{}
for _, wdp := range wanted {
seenDev[devMajMin{wdp.Major, wdp.Minor}] = true
new = append(new, wdp)
}
for _, wdp := range old {
if !seenDev[devMajMin{wdp.Major, wdp.Minor}] {
new = append(new, OciDeviceWeight{wdp.Major, wdp.Minor, 0})
}
}
return new
}
// resetDevRates adds wanted rate parameters to new and resets unwanted rates
func resetDevRates(old, wanted []OciDeviceRate) []OciDeviceRate {
new := []OciDeviceRate{}
seenDev := map[devMajMin]bool{}
for _, rdp := range wanted {
new = append(new, rdp)
seenDev[devMajMin{rdp.Major, rdp.Minor}] = true
}
for _, rdp := range old {
if !seenDev[devMajMin{rdp.Major, rdp.Minor}] {
new = append(new, OciDeviceRate{rdp.Major, rdp.Minor, 0})
}
}
return new
}
// GetBlkioParameters returns OCI BlockIO parameters from files in cgroups blkio controller directory.
func GetBlkioParameters(group string) (OciBlockIOParameters, error) {
var errors *multierror.Error
blockIO := NewOciBlockIOParameters()
errors = multierror.Append(errors, readWeight(group, blkioWeightFiles, &blockIO.Weight))
errors = multierror.Append(errors, readOciDeviceParameters(group, blkioWeightDeviceFiles, &blockIO.WeightDevice))
errors = multierror.Append(errors, readOciDeviceParameters(group, blkioThrottleReadBpsFiles, &blockIO.ThrottleReadBpsDevice))
errors = multierror.Append(errors, readOciDeviceParameters(group, blkioThrottleWriteBpsFiles, &blockIO.ThrottleWriteBpsDevice))
errors = multierror.Append(errors, readOciDeviceParameters(group, blkioThrottleReadIOPSFiles, &blockIO.ThrottleReadIOPSDevice))
errors = multierror.Append(errors, readOciDeviceParameters(group, blkioThrottleWriteIOPSFiles, &blockIO.ThrottleWriteIOPSDevice))
return blockIO, errors.ErrorOrNil()
}
// readWeight parses int64 from a cgroups entry
func readWeight(cntnrDir string, filenames []string, rv *int64) error {
contents, err := readFirstFile(cntnrDir, filenames)
if err != nil {
return err
}
parsed, err := strconv.ParseInt(strings.TrimSuffix(contents, "\n"), 10, 64)
if err != nil {
return fmt.Errorf("parsing weight from %#v found in %v failed: %w", contents, filenames, err)
}
*rv = parsed
return nil
}
// readOciDeviceParameters parses device lines used for weights and throttling rates
func readOciDeviceParameters(cntnrDir string, filenames []string, params OciDeviceParameters) error {
var errors *multierror.Error
contents, err := readFirstFile(cntnrDir, filenames)
if err != nil {
return err
}
for _, line := range strings.Split(contents, "\n") {
// Device weight files may have "default NNN" line at the beginning. Skip it.
if line == "" || strings.HasPrefix(line, "default ") {
continue
}
// Expect syntax MAJOR:MINOR VALUE
devVal := strings.Split(line, " ")
if len(devVal) != 2 {
errors = multierror.Append(errors, fmt.Errorf("invalid line %q, single space expected", line))
continue
}
majMin := strings.Split(devVal[0], ":")
if len(majMin) != 2 {
errors = multierror.Append(errors, fmt.Errorf("invalid line %q, single colon expected before space", line))
continue
}
major, majErr := strconv.ParseInt(majMin[0], 10, 64)
minor, minErr := strconv.ParseInt(majMin[1], 10, 64)
value, valErr := strconv.ParseInt(devVal[1], 10, 64)
if majErr != nil || minErr != nil || valErr != nil {
errors = multierror.Append(errors, fmt.Errorf("invalid number when parsing \"major:minor value\" from \"%s:%s %s\"", majMin[0], majMin[1], devVal[1]))
continue
}
params.Append(major, minor, value)
}
return errors.ErrorOrNil()
}
// readFirstFile returns contents of the first successfully read entry.
func readFirstFile(cntnrDir string, filenames []string) (string, error) {
var errors *multierror.Error
// If reading all the files fails, return list of read errors.
for _, filename := range filenames {
content, err := Blkio.Group(cntnrDir).Read(filename)
if err == nil {
return content, nil
}
errors = multierror.Append(errors, err)
}
err := errors.ErrorOrNil()
if err != nil {
return "", fmt.Errorf("could not read any of files %q: %w", filenames, err)
}
return "", nil
}
// SetBlkioParameters writes OCI BlockIO parameters to files in cgroups blkio contoller directory.
func SetBlkioParameters(group string, blockIO OciBlockIOParameters) error {
var errors *multierror.Error
if blockIO.Weight >= 0 {
errors = multierror.Append(errors, writeFirstFile(group, blkioWeightFiles, "%d", blockIO.Weight))
}
for _, wd := range blockIO.WeightDevice {
errors = multierror.Append(errors, writeFirstFile(group, blkioWeightDeviceFiles, "%d:%d %d", wd.Major, wd.Minor, wd.Weight))
}
for _, rd := range blockIO.ThrottleReadBpsDevice {
errors = multierror.Append(errors, writeFirstFile(group, blkioThrottleReadBpsFiles, "%d:%d %d", rd.Major, rd.Minor, rd.Rate))
}
for _, rd := range blockIO.ThrottleWriteBpsDevice {
errors = multierror.Append(errors, writeFirstFile(group, blkioThrottleWriteBpsFiles, "%d:%d %d", rd.Major, rd.Minor, rd.Rate))
}
for _, rd := range blockIO.ThrottleReadIOPSDevice {
errors = multierror.Append(errors, writeFirstFile(group, blkioThrottleReadIOPSFiles, "%d:%d %d", rd.Major, rd.Minor, rd.Rate))
}
for _, rd := range blockIO.ThrottleWriteIOPSDevice {
errors = multierror.Append(errors, writeFirstFile(group, blkioThrottleWriteIOPSFiles, "%d:%d %d", rd.Major, rd.Minor, rd.Rate))
}
return errors.ErrorOrNil()
}
// writeFirstFile writes content to the first existing file in the list under cntnrDir.
func writeFirstFile(cntnrDir string, filenames []string, format string, args ...interface{}) error {
var errors *multierror.Error
// Returns list of errors from writes, list of single error due to all filenames missing or nil on success.
for _, filename := range filenames {
if err := Blkio.Group(cntnrDir).Write(filename, format, args...); err != nil {
errors = multierror.Append(errors, err)
continue
}
return nil
}
err := errors.ErrorOrNil()
if err != nil {
data := fmt.Sprintf(format, args...)
return fmt.Errorf("writing all files %v failed, errors: %w, content %q", filenames, err, data)
}
return nil
}