-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
292 lines (274 loc) · 6.26 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"sort"
"strings"
"os"
"errors"
"github.com/BurntSushi/toml"
)
type nicConfig struct {
Test int `toml:"test"`
Rx_queues_enable bool
Rx_queues_begin int
Rx_queues_end int
Rx_queues_step int
Rps_enable bool
Rps_begin int
Rps_end int
Rps_step int
}
type generalConfig struct {
Reverse_queues bool
Cpu map[string]nicConfig
}
type NicBindConfig struct {
General generalConfig
}
type Dev struct {
Name string
ints []int
queue_num int
}
//const INTERRUPTS_FILE = "./interrupts"
const INTERRUPTS_FILE = "/proc/interrupts"
const NET_DIR = "/sys/class/net/"
//const NET_DIR = "./net/"
var g_config NicBindConfig
var interrupts []byte
func findDevIsReal(dev string) bool {
var err error
if len(interrupts) == 0 {
interrupts, err = ioutil.ReadFile(INTERRUPTS_FILE)
if err != nil {
panic(err)
}
}
reg, err := regexp.Compile(" " + dev)
if err != nil {
panic(err)
}
return reg.Match(interrupts)
}
func getDevInterrupts(dev *Dev) {
dev.ints = make([]int, 0, 3)
var err error
if len(interrupts) == 0 {
fmt.Println("read")
interrupts, err = ioutil.ReadFile(INTERRUPTS_FILE)
if err != nil {
panic(err)
}
}
lines := strings.Split(string(interrupts), "\n")
reg := regexp.MustCompile("^\\s*(\\d*):[\\s*\\d*]*\\s*.*" + dev.Name + "-")
for _, line := range(lines) {
machines := reg.FindStringSubmatch(line)
if len(machines) != 0 {
inter, err := strconv.Atoi(machines[1])
if err != nil {
panic(err)
}
dev.ints = append(dev.ints, inter)
}
}
}
func GetRealDev() []Dev {
fs, err := ioutil.ReadDir(NET_DIR)
if err != nil {
panic(err)
}
devs := make([]Dev, 0, 2)
for _, f := range(fs) {
if findDevIsReal(f.Name()) {
dev := Dev{Name:f.Name()}
getDevInterrupts(&dev)
dev.queue_num = len(dev.ints)
devs = append(devs, dev)
}
}
return devs
}
func getCpuNum() int {
b, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
panic(err)
}
cpuCount := 0
lines := strings.Split(string(b), "\n")
for i, line := range lines {
if len(line) == 0 && i != len(lines)-1 {
cpuCount++
continue
} else if i == len(lines)-1 {
continue
}
}
return cpuCount
}
func getCpuMask(id int) string {
return strconv.FormatUint(1 << uint32(id), 16)
}
func getGeneralNicConfigKey(cpuNums int) string {
var keys []int
for cpu := range(g_config.General.Cpu) {
c, err := strconv.Atoi(cpu)
if err != nil {
fmt.Println(err)
return ""
}
keys = append(keys, c)
}
sort.Ints(keys)
for _, key := range(keys) {
if cpuNums <= key {
return strconv.Itoa(key)
}
}
return ""
}
func buildNicConfig(c *nicConfig) (error) {
cpuNums := getCpuNum()
if c.Rx_queues_begin >= cpuNums {
return errors.New("rx_queues_begin too big")
}
if c.Rx_queues_end > cpuNums || c.Rx_queues_end <= 0 {
c.Rx_queues_end = cpuNums
}
if c.Rps_begin >= cpuNums {
return errors.New("rps_begin too big")
}
if c.Rps_end > cpuNums || c.Rps_end <= 0 {
c.Rps_end = cpuNums
}
return nil
}
func getInterrupts(devs []Dev) []int {
ints := make([]int, 0, 10)
for _, dev := range(devs) {
fmt.Printf("process %s, queues num: %d \n", dev.Name, len(dev.ints))
for i := range(dev.ints) {
ints = append(ints, dev.ints[i])
}
}
return ints
}
func main() {
if len(os.Args) != 2 {
fmt.Println("argument error")
return
}
if _, err := toml.DecodeFile(os.Args[1], &g_config); err != nil {
fmt.Println(err)
return
}
// get real dev
devs := GetRealDev()
// get cpu
cpuNums := getCpuNum()
fmt.Println("current cpu num:", cpuNums)
// get general config
gNicConfig := g_config.General.Cpu[getGeneralNicConfigKey(cpuNums)]
err := buildNicConfig(&gNicConfig)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(gNicConfig, devs)
for _, d := range(devs) {
fmt.Printf("dev: %s queues num: %d", d.Name, d.queue_num)
}
incr := true
ints := getInterrupts(devs)
id := gNicConfig.Rx_queues_begin
if gNicConfig.Rx_queues_enable {
for _, irq := range(ints) {
f, err := os.OpenFile("/proc/irq/" + strconv.Itoa(irq) +"/smp_affinity",
//f, err := os.OpenFile("./irq/" + strconv.Itoa(irq) +"/smp_affinity",
os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
panic(err)
}
fmt.Println("bind id :", id)
f.Write([]byte(getCpuMask(id)))
f.Close()
if g_config.General.Reverse_queues {
if incr {
id++
if id == gNicConfig.Rx_queues_end {
id = gNicConfig.Rx_queues_end - 1
incr = false
}
} else {
id--
if id == gNicConfig.Rx_queues_begin - 1 {
id = gNicConfig.Rx_queues_begin
incr = true
}
}
} else {
id++
if id == gNicConfig.Rx_queues_end {
id = gNicConfig.Rx_queues_begin
}
}
}
} else {
fmt.Println("unbind rx-queues")
for _, irq := range(ints) {
f, err := os.OpenFile("/proc/irq/" + strconv.Itoa(irq) +"/smp_affinity",
//f, err := os.OpenFile("./irq/" + strconv.Itoa(irq) +"/smp_affinity",
os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
panic(err)
}
f.Write([]byte("0"))
f.Close()
}
}
for _, dev := range(devs) {
queues_dir := NET_DIR + dev.Name + "/queues/"
fs, err := ioutil.ReadDir(queues_dir)
if err != nil {
panic(err)
}
// bins irq
if gNicConfig.Rps_enable {
fmt.Println("bind rps")
id := gNicConfig.Rps_begin
for _, f := range(fs) {
if strings.Contains(f.Name(), "tx") {
continue
}
file, err := os.OpenFile(queues_dir + f.Name() + "/rps_cpus",
os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
panic(err)
}
file.Write([]byte(getCpuMask(id % gNicConfig.Rps_end)))
file.Close()
id += gNicConfig.Rps_step
if id == gNicConfig.Rps_end {
id = gNicConfig.Rps_begin
}
}
} else {
fmt.Println("unbind rps")
for _, f := range(fs) {
if strings.Contains(f.Name(), "tx") {
continue
}
file, err := os.OpenFile(queues_dir + f.Name() + "/rps_cpus",
os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
panic(err)
}
file.Write([]byte("0"))
file.Close()
}
}
}
}