forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
154 lines (138 loc) · 4.56 KB
/
cgroup.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
// Copyright 2021 Chaos Mesh Authors.
//
// 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 server
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"github.com/chaos-mesh/chaos-mesh/pkg/bpm"
"github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/model"
)
// GetCgroups returns result of cat /proc/cgroups
func (r *Resolver) GetCgroups(ctx context.Context, obj *model.PodStressChaos) (*model.Cgroups, error) {
cmd := "cat /proc/cgroups"
// the raw looks like:
// ```
// #subsys_name hierarchy num_cgroups enabled
// cpuset 0 127 1
// cpu 0 127 1
// cpuacct 0 127 1
// blkio 0 127 1
// memory 0 127 1
// devices 0 127 1
// freezer 0 127 1
// net_cls 0 127 1
// perf_event 0 127 1
// net_prio 0 127 1
// hugetlb 0 127 1
// pids 0 127 1
// rdma 0 127 1
// misc 0 127 1
// ```
raw, err := r.ExecBypass(ctx, obj.Pod, cmd, bpm.PidNS, bpm.MountNS)
if err != nil {
return nil, err
}
cgroups := &model.Cgroups{
Raw: raw,
}
// no more info for StressngStressors
if obj.StressChaos.Spec.StressngStressors != "" || obj.StressChaos.Spec.Stressors == nil {
return cgroups, nil
}
isCPU := true
if obj.StressChaos.Spec.Stressors.CPUStressor == nil {
isCPU = false
}
if isCPU {
var cpuMountType string
if regexp.MustCompile("(cpu,cpuacct)").MatchString(string(raw)) {
cpuMountType = "cpu,cpuacct"
} else {
// cgroup does not support cpuacct sub-system
cpuMountType = "cpu"
}
cgroups.CPU = &model.CgroupsCPU{}
cgroups.CPU.Quota, err = r.GetCPUQuota(ctx, obj.Pod, cpuMountType)
if err != nil {
return nil, err
}
cgroups.CPU.Period, err = r.GetCPUPeriod(ctx, obj.Pod, cpuMountType)
if err != nil {
return nil, err
}
} else {
cgroups.Memory = &model.CgroupsMemory{}
cgroups.Memory.Limit, err = r.GetMemoryLimit(ctx, obj.Pod)
if err != nil {
return nil, err
}
}
return cgroups, nil
}
// GetCgroup returns result of cat /proc/:pid/cgroup
// The output looks like:
// ```
// 11:freezer:/
// 10:hugetlb:/
// 9:memory:/system.slice/sshd.service
// 8:pids:/system.slice/sshd.service
// 7:perf_event:/
// 6:net_cls,net_prio:/
// 5:devices:/system.slice/sshd.service
// 4:blkio:/system.slice/sshd.service
// 3:cpu,cpuacct:/system.slice/sshd.service
// 2:cpuset:/
// 1:name=systemd:/system.slice/sshd.service
// ```
func (r *Resolver) GetCgroup(ctx context.Context, obj *v1.Pod, pid string) (string, error) {
cmd := fmt.Sprintf("cat /proc/%s/cgroup", pid)
return r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.MountNS)
}
// GetCPUQuota returns result of cat /sys/fs/cgroup/:cpuMountType/cpu.cfs_quota_us
func (r *Resolver) GetCPUQuota(ctx context.Context, obj *v1.Pod, cpuMountType string) (int, error) {
cmd := fmt.Sprintf("cat /sys/fs/cgroup/%s/cpu.cfs_quota_us", cpuMountType)
out, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.MountNS)
if err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSuffix(string(out), "\n"))
}
// GetCPUPeriod returns result of cat /sys/fs/cgroup/:cpuMountType/cpu.cfs_period_us
func (r *Resolver) GetCPUPeriod(ctx context.Context, obj *v1.Pod, cpuMountType string) (int, error) {
cmd := fmt.Sprintf("cat /sys/fs/cgroup/%s/cpu.cfs_period_us", cpuMountType)
out, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.MountNS)
if err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSuffix(string(out), "\n"))
}
// GetMemoryLimit returns result of cat /sys/fs/cgroup/memory/memory.limit_in_bytes
func (r *Resolver) GetMemoryLimit(ctx context.Context, obj *v1.Pod) (int64, error) {
cmd := "cat /sys/fs/cgroup/memory/memory.limit_in_bytes"
rawLimit, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.MountNS)
if err != nil {
return 0, errors.Wrap(err, "could not get memory.limit_in_bytes")
}
limit, err := strconv.ParseUint(strings.TrimSuffix(rawLimit, "\n"), 10, 64)
if err != nil {
return 0, errors.Wrap(err, "could not parse memory.limit_in_bytes")
}
return int64(limit), nil
}