This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
bandwidth_manager.go
136 lines (108 loc) · 3.15 KB
/
bandwidth_manager.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
package bandwidth_manager
import (
"bytes"
"fmt"
"os/exec"
"path"
"regexp"
"strconv"
"github.com/cloudfoundry-incubator/garden"
"github.com/cloudfoundry-incubator/garden-linux/logging"
"github.com/cloudfoundry/gunk/command_runner"
"github.com/pivotal-golang/lager"
)
var IN_RATE_PATTERN = regexp.MustCompile(`qdisc tbf [0-9a-f]+: root refcnt \d+ rate (\d+)([KMG]?)bit burst (\d+)([KMG]?)b`)
var OUT_RATE_PATTERN = regexp.MustCompile(`police 0x[0-9a-f]+ rate (\d+)([KMG]?)bit burst (\d+)([KMG]?)b`)
type ContainerBandwidthManager struct {
containerPath string
containerID string
runner command_runner.CommandRunner
}
func New(containerPath, containerID string, runner command_runner.CommandRunner) *ContainerBandwidthManager {
return &ContainerBandwidthManager{
containerPath: containerPath,
containerID: containerID,
runner: runner,
}
}
func (m *ContainerBandwidthManager) SetLimits(
logger lager.Logger,
limits garden.BandwidthLimits,
) error {
runner := logging.Runner{
CommandRunner: m.runner,
Logger: logger,
}
setRate := exec.Command(path.Join(m.containerPath, "net_rate.sh"))
setRate.Env = []string{
fmt.Sprintf("BURST=%d", limits.BurstRateInBytesPerSecond),
fmt.Sprintf("RATE=%d", limits.RateInBytesPerSecond*8),
}
return runner.Run(setRate)
}
func (m *ContainerBandwidthManager) GetLimits(logger lager.Logger) (garden.ContainerBandwidthStat, error) {
limits := garden.ContainerBandwidthStat{}
runner := logging.Runner{
CommandRunner: m.runner,
Logger: logger,
}
egressOut := new(bytes.Buffer)
egress := exec.Command(path.Join(m.containerPath, "net.sh"), "get_egress_info")
egress.Env = []string{"ID=" + m.containerID}
egress.Stdout = egressOut
err := runner.Run(egress)
if err != nil {
return limits, err
}
matches := IN_RATE_PATTERN.FindStringSubmatch(string(egressOut.Bytes()))
if matches != nil {
inRate, err := strconv.ParseUint(matches[1], 10, 0)
if err != nil {
return limits, err
}
inBurst, err := strconv.ParseUint(matches[3], 10, 0)
if err != nil {
return limits, err
}
inRateUnit := matches[2]
inBurstUnit := matches[4]
limits.InRate = convertUnits(inRate, inRateUnit) / 8
limits.InBurst = convertUnits(inBurst, inBurstUnit)
}
ingressOut := new(bytes.Buffer)
ingress := exec.Command(path.Join(m.containerPath, "net.sh"), "get_ingress_info")
ingress.Env = []string{"ID=" + m.containerID}
ingress.Stdout = ingressOut
err = runner.Run(ingress)
if err != nil {
return limits, err
}
matches = OUT_RATE_PATTERN.FindStringSubmatch(string(ingressOut.Bytes()))
if matches != nil {
outRate, err := strconv.ParseUint(matches[1], 10, 0)
if err != nil {
return limits, err
}
outBurst, err := strconv.ParseUint(matches[3], 10, 0)
if err != nil {
return limits, err
}
outRateUnit := matches[2]
outBurstUnit := matches[4]
limits.OutRate = convertUnits(outRate, outRateUnit) / 8
limits.OutBurst = convertUnits(outBurst, outBurstUnit)
}
return limits, err
}
func convertUnits(num uint64, unit string) uint64 {
switch unit {
case "K":
return num * 1024
case "M":
return num * (1024 ^ 2)
case "G":
return num * (1024 ^ 3)
default:
return num
}
}