io.go 3.79 KB
Newer Older
“李磊”'s avatar
“李磊” committed
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 fs2

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

	"linkfog.com/public/lib/cgroup/types"
	"linkfog.com/public/lib/l"
)

func SetIORWLimit(cgroupDir string, devinfos []types.DevInfo, rlimit, wlimit uint64) (map[string]*types.DevLimit, error) {
	ioLimitPath := path.Join(cgroupDir, "io.max")
	l.Infof("io limit path: %s, rlimit size: %d, wlimit size: %d", ioLimitPath, rlimit, wlimit)
	f, err := os.OpenFile(ioLimitPath, os.O_WRONLY|os.O_CREATE, 0)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	devLmts := make(map[string]*types.DevLimit, 0)
	ioRdLimitSuccCnt := 0
	ioWrLimitSuccCnt := 0
	for _, devinfo := range devinfos {
		devKey := fmt.Sprintf("%s:%s", devinfo.Major, devinfo.Minor)
		devLmts[devKey] = &types.DevLimit{
			DevName: devinfo.Name,
			RdLimit: false,
			WrLimit: false,
		}

		var ioRdLimit string
		var ioWrLimit string
		if rlimit == 0 { // delete limit
			ioRdLimit = fmt.Sprintf("%s:%s %s=max", devinfo.Major, devinfo.Minor, "rbps")
		} else {
			ioRdLimit = fmt.Sprintf("%s:%s %s=%d", devinfo.Major, devinfo.Minor, "rbps", rlimit)
		}
		if wlimit == 0 { // delete limit
			ioWrLimit = fmt.Sprintf("%s:%s %s=max", devinfo.Major, devinfo.Minor, "wbps")
		} else {
			ioWrLimit = fmt.Sprintf("%s:%s %s=%d", devinfo.Major, devinfo.Minor, "wbps", wlimit)
		}

		// set io read limit
		_, err := f.WriteString(ioRdLimit)
		if err != nil {
			l.Debugf("set %s io read limit error %v, config:%s", devinfo.Name, err, ioRdLimit)
		} else {
			devLmts[devKey].RdLimit = true
			ioRdLimitSuccCnt++
			l.Debugf("set %s io read limit success, config:%s", devinfo.Name, ioRdLimit)
		}

		// set io write limit
		_, err = f.WriteString(ioWrLimit)
		if err != nil {
			l.Debugf("set %s io write limit error %v, config:%s", devinfo.Name, err, ioWrLimit)
		} else {
			devLmts[devKey].WrLimit = true
			ioWrLimitSuccCnt++
			l.Debugf("set %s io write limit success, config:%s", devinfo.Name, ioWrLimit)
		}
	}
	if ioRdLimitSuccCnt == 0 || ioWrLimitSuccCnt == 0 {
		return nil, fmt.Errorf("set io limit error, read_limit:%d, write_limit:%d",
			ioRdLimitSuccCnt, ioWrLimitSuccCnt)
	}

	return devLmts, nil
}

// # cat io.stat
// 253:0 rbytes=0 wbytes=12288 rios=0 wios=3 dbytes=0 dios=0
// 8:0 rbytes=0 wbytes=12288 rios=0 wios=3 dbytes=0 dios=0
func GetAllDevIOStat(cgroupDir string) (map[string]*types.IOStat, error) {
	values, err := parseIOStatFile(filepath.Join(cgroupDir, "io.stat"))
	if err != nil {
		return nil, err
	}

	ioStats := make(map[string]*types.IOStat, 0)
	ioStats["total"] = &types.IOStat{}
	for k, v := range values {
		d := strings.Split(k, ":")
		if len(d) != 2 {
			continue
		}
		device := k
		for _, item := range v {
			d := strings.Split(item, "=")
			if len(d) != 2 {
				continue
			}
			op := d[0]
			if op == "rbytes" || op == "wbytes" {
				sizeUint64, err := strconv.ParseUint(d[1], 10, 64)
				if err != nil {
					return nil, err
				}
				if _, ok := ioStats[device]; !ok {
					ioStats[device] = &types.IOStat{}
				}
				if op == "rbytes" {
					ioStats[device].Read = sizeUint64
					ioStats["total"].Read += sizeUint64
				}
				if op == "wbytes" {
					ioStats[device].Write = sizeUint64
					ioStats["total"].Write += sizeUint64
				}
			}
		}
	}
	return ioStats, nil
}

func parseIOStatFile(ioStatFile string) (map[string][]string, error) {
	f, err := os.OpenFile(ioStatFile, os.O_RDONLY, 0)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	ret := map[string][]string{}
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Fields(line)
		if len(parts) < 2 {
			continue
		}
		ret[parts[0]] = parts[1:]
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return ret, nil
}