aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/opencontainers/runc/libcontainer/cgroups/fs/blkio.go
blob: c81b6562ac50f2c0a25e0b8d70dbfe9b758a88b2 (plain)
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
package fs

import (
	"bufio"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/opencontainers/runc/libcontainer/cgroups"
	"github.com/opencontainers/runc/libcontainer/configs"
)

type BlkioGroup struct {
	weightFilename       string
	weightDeviceFilename string
}

func (s *BlkioGroup) Name() string {
	return "blkio"
}

func (s *BlkioGroup) Apply(path string, _ *configs.Resources, pid int) error {
	return apply(path, pid)
}

func (s *BlkioGroup) Set(path string, r *configs.Resources) error {
	s.detectWeightFilenames(path)
	if r.BlkioWeight != 0 {
		if err := cgroups.WriteFile(path, s.weightFilename, strconv.FormatUint(uint64(r.BlkioWeight), 10)); err != nil {
			return err
		}
	}

	if r.BlkioLeafWeight != 0 {
		if err := cgroups.WriteFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(r.BlkioLeafWeight), 10)); err != nil {
			return err
		}
	}
	for _, wd := range r.BlkioWeightDevice {
		if wd.Weight != 0 {
			if err := cgroups.WriteFile(path, s.weightDeviceFilename, wd.WeightString()); err != nil {
				return err
			}
		}
		if wd.LeafWeight != 0 {
			if err := cgroups.WriteFile(path, "blkio.leaf_weight_device", wd.LeafWeightString()); err != nil {
				return err
			}
		}
	}
	for _, td := range r.BlkioThrottleReadBpsDevice {
		if err := cgroups.WriteFile(path, "blkio.throttle.read_bps_device", td.String()); err != nil {
			return err
		}
	}
	for _, td := range r.BlkioThrottleWriteBpsDevice {
		if err := cgroups.WriteFile(path, "blkio.throttle.write_bps_device", td.String()); err != nil {
			return err
		}
	}
	for _, td := range r.BlkioThrottleReadIOPSDevice {
		if err := cgroups.WriteFile(path, "blkio.throttle.read_iops_device", td.String()); err != nil {
			return err
		}
	}
	for _, td := range r.BlkioThrottleWriteIOPSDevice {
		if err := cgroups.WriteFile(path, "blkio.throttle.write_iops_device", td.String()); err != nil {
			return err
		}
	}

	return nil
}

/*
examples:

    blkio.sectors
    8:0 6792

    blkio.io_service_bytes
    8:0 Read 1282048
    8:0 Write 2195456
    8:0 Sync 2195456
    8:0 Async 1282048
    8:0 Total 3477504
    Total 3477504

    blkio.io_serviced
    8:0 Read 124
    8:0 Write 104
    8:0 Sync 104
    8:0 Async 124
    8:0 Total 228
    Total 228

    blkio.io_queued
    8:0 Read 0
    8:0 Write 0
    8:0 Sync 0
    8:0 Async 0
    8:0 Total 0
    Total 0
*/

func splitBlkioStatLine(r rune) bool {
	return r == ' ' || r == ':'
}

func getBlkioStat(dir, file string) ([]cgroups.BlkioStatEntry, error) {
	var blkioStats []cgroups.BlkioStatEntry
	f, err := cgroups.OpenFile(dir, file, os.O_RDONLY)
	if err != nil {
		if os.IsNotExist(err) {
			return blkioStats, nil
		}
		return nil, err
	}
	defer f.Close()

	sc := bufio.NewScanner(f)
	for sc.Scan() {
		// format: dev type amount
		fields := strings.FieldsFunc(sc.Text(), splitBlkioStatLine)
		if len(fields) < 3 {
			if len(fields) == 2 && fields[0] == "Total" {
				// skip total line
				continue
			} else {
				return nil, malformedLine(dir, file, sc.Text())
			}
		}

		v, err := strconv.ParseUint(fields[0], 10, 64)
		if err != nil {
			return nil, &parseError{Path: dir, File: file, Err: err}
		}
		major := v

		v, err = strconv.ParseUint(fields[1], 10, 64)
		if err != nil {
			return nil, &parseError{Path: dir, File: file, Err: err}
		}
		minor := v

		op := ""
		valueField := 2
		if len(fields) == 4 {
			op = fields[2]
			valueField = 3
		}
		v, err = strconv.ParseUint(fields[valueField], 10, 64)
		if err != nil {
			return nil, &parseError{Path: dir, File: file, Err: err}
		}
		blkioStats = append(blkioStats, cgroups.BlkioStatEntry{Major: major, Minor: minor, Op: op, Value: v})
	}
	if err := sc.Err(); err != nil {
		return nil, &parseError{Path: dir, File: file, Err: err}
	}

	return blkioStats, nil
}

func (s *BlkioGroup) GetStats(path string, stats *cgroups.Stats) error {
	type blkioStatInfo struct {
		filename            string
		blkioStatEntriesPtr *[]cgroups.BlkioStatEntry
	}
	bfqDebugStats := []blkioStatInfo{
		{
			filename:            "blkio.bfq.sectors_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.SectorsRecursive,
		},
		{
			filename:            "blkio.bfq.io_service_time_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceTimeRecursive,
		},
		{
			filename:            "blkio.bfq.io_wait_time_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoWaitTimeRecursive,
		},
		{
			filename:            "blkio.bfq.io_merged_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoMergedRecursive,
		},
		{
			filename:            "blkio.bfq.io_queued_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoQueuedRecursive,
		},
		{
			filename:            "blkio.bfq.time_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoTimeRecursive,
		},
		{
			filename:            "blkio.bfq.io_serviced_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServicedRecursive,
		},
		{
			filename:            "blkio.bfq.io_service_bytes_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceBytesRecursive,
		},
	}
	bfqStats := []blkioStatInfo{
		{
			filename:            "blkio.bfq.io_serviced_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServicedRecursive,
		},
		{
			filename:            "blkio.bfq.io_service_bytes_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceBytesRecursive,
		},
	}
	cfqStats := []blkioStatInfo{
		{
			filename:            "blkio.sectors_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.SectorsRecursive,
		},
		{
			filename:            "blkio.io_service_time_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceTimeRecursive,
		},
		{
			filename:            "blkio.io_wait_time_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoWaitTimeRecursive,
		},
		{
			filename:            "blkio.io_merged_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoMergedRecursive,
		},
		{
			filename:            "blkio.io_queued_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoQueuedRecursive,
		},
		{
			filename:            "blkio.time_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoTimeRecursive,
		},
		{
			filename:            "blkio.io_serviced_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServicedRecursive,
		},
		{
			filename:            "blkio.io_service_bytes_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceBytesRecursive,
		},
	}
	throttleRecursiveStats := []blkioStatInfo{
		{
			filename:            "blkio.throttle.io_serviced_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServicedRecursive,
		},
		{
			filename:            "blkio.throttle.io_service_bytes_recursive",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceBytesRecursive,
		},
	}
	baseStats := []blkioStatInfo{
		{
			filename:            "blkio.throttle.io_serviced",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServicedRecursive,
		},
		{
			filename:            "blkio.throttle.io_service_bytes",
			blkioStatEntriesPtr: &stats.BlkioStats.IoServiceBytesRecursive,
		},
	}
	orderedStats := [][]blkioStatInfo{
		bfqDebugStats,
		bfqStats,
		cfqStats,
		throttleRecursiveStats,
		baseStats,
	}

	var blkioStats []cgroups.BlkioStatEntry
	var err error

	for _, statGroup := range orderedStats {
		for i, statInfo := range statGroup {
			if blkioStats, err = getBlkioStat(path, statInfo.filename); err != nil || blkioStats == nil {
				// if error occurs on first file, move to next group
				if i == 0 {
					break
				}
				return err
			}
			*statInfo.blkioStatEntriesPtr = blkioStats
			// finish if all stats are gathered
			if i == len(statGroup)-1 {
				return nil
			}
		}
	}
	return nil
}

func (s *BlkioGroup) detectWeightFilenames(path string) {
	if s.weightFilename != "" {
		// Already detected.
		return
	}
	if cgroups.PathExists(filepath.Join(path, "blkio.weight")) {
		s.weightFilename = "blkio.weight"
		s.weightDeviceFilename = "blkio.weight_device"
	} else {
		s.weightFilename = "blkio.bfq.weight"
		s.weightDeviceFilename = "blkio.bfq.weight_device"
	}
}