forked from TencentBlueKing/bkunifylogbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpulimit.go
193 lines (169 loc) · 5.37 KB
/
cpulimit.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
// Tencent is pleased to support the open source community by making bkunifylogbeat 蓝鲸日志采集器 available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// bkunifylogbeat 蓝鲸日志采集器 is licensed under the MIT License.
//
// License for bkunifylogbeat 蓝鲸日志采集器:
// --------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package utils
import (
"os"
"runtime"
"sync"
"time"
"github.com/TencentBlueKing/bkmonitor-datalink/pkg/libgse/logp"
"github.com/shirou/gopsutil/process"
)
var (
IsEnableRateLimiter bool // 是否开启速率限制
GlobalCpuLimiter *CPULimit // CPU使用率限制
)
const (
MaxCheckTimes = 10
)
// CPULimit cpu limit object
type CPULimit struct {
limit int // cpu limit percent
checkTimes int // check cpu usage in one second
checkInterval time.Duration
isAllowRun bool
closeOnce sync.Once
done chan struct{}
}
// NewCPULimit new cpu limit object
func NewCPULimit(l, checkTimes int) *CPULimit {
if checkTimes < 1 || checkTimes > MaxCheckTimes {
checkTimes = MaxCheckTimes
}
cpuLimit := &CPULimit{
limit: l,
checkTimes: checkTimes,
checkInterval: time.Duration(float64(time.Second) / float64(checkTimes)),
isAllowRun: true,
done: make(chan struct{}, 1),
}
cpuLimit.Start()
return cpuLimit
}
// Start : start cpu limit
func (c *CPULimit) Start() {
logp.L.Info("start cpu limit.")
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
logp.L.Errorf("cpu limit, get process error=>(%v)", err)
return
}
numCPU := runtime.NumCPU()
if c.limit < 0 || c.limit > numCPU*100 {
logp.L.Errorf("cpu limit, limit(%d) config not valid, cpu core(%d)",
c.limit, numCPU)
return
}
// 在1s内可运行的时长
timeToRun := time.Duration((float64(time.Second)) * (float64(c.limit) / 100))
logp.L.Infof("cpu limit, max time to run in second is %s \n", timeToRun)
timeToRunSeconds := timeToRun.Seconds()
go func() {
var (
ticker = time.NewTicker(c.GetCheckInterval())
tickCount = 0
tickInterval = c.GetCheckInterval().Seconds()
)
defer ticker.Stop()
lastCPUTime := time.Now()
lastCPUTimes, err := c.GetCpuTimes(p)
if err != nil {
logp.L.Errorf("cpu limit, get cpu stat err=>(%+v)", err)
return
}
for {
select {
case <-c.done:
c.isAllowRun = true
return
case <-ticker.C:
now := time.Now()
curCpuTimes, _ := c.GetCpuTimes(p)
delta := now.Sub(lastCPUTime).Seconds()
deltaCPUTime := curCpuTimes.Total() - lastCPUTimes.Total()
if deltaCPUTime > timeToRunSeconds-tickInterval {
c.isAllowRun = false
} else {
c.isAllowRun = true
}
tickCount++
if tickCount == c.checkTimes {
tickCount = 0
c.isAllowRun = true
lastCPUTimes = curCpuTimes
lastCPUTime = now
}
logp.L.Debugf("%d current cpu usage is =>(%.2f%%), "+
"cpu use time is =>(%.2f), "+
"time elapsed is =>(%.2f), "+
"time allow run in seconds =>(%.2f), "+
"isAllowRun(%v)\n",
tickCount, deltaCPUTime/delta*100, deltaCPUTime,
delta, timeToRunSeconds, c.isAllowRun)
}
}
}()
}
// Stop : stop cpu limit
func (c *CPULimit) Stop() {
c.closeOnce.Do(func() {
close(c.done)
})
logp.L.Info("stop cpu limit.")
}
// Allow : judge current
func (c *CPULimit) Allow() bool {
return c.isAllowRun
}
// GetCheckInterval : get cpu check interval
func (c *CPULimit) GetCheckInterval() time.Duration {
return c.checkInterval
}
// SetResourceLimit : 在一定程度上限制CPU使用
// maxCpuLimit: 最大CPU使用率限制
// checkTimes: 资源限制的检查次数
func SetResourceLimit(maxCpuLimit, checkTimes int) {
numCPU := runtime.NumCPU()
// 在docker富容器中 并且 开启了速率限制
if IsInDocker() {
logp.L.Infof("enable rate limit. because numOfCpu(%d) && isInDocker(%v)",
numCPU, true,
)
if maxCpuLimit > 0 && maxCpuLimit <= numCPU*100 {
GlobalCpuLimiter = NewCPULimit(maxCpuLimit, checkTimes)
IsEnableRateLimiter = true
} else {
logp.L.Infof("disable rate limit. because cpu limit config(%d) is not valid",
maxCpuLimit,
)
}
} else {
logp.L.Infof("disable rate limit. because numOfCpu(%d) && isInDocker(%v), "+
"cpu limit config(%d)",
numCPU, false, maxCpuLimit,
)
if GlobalCpuLimiter != nil {
GlobalCpuLimiter.Stop()
}
IsEnableRateLimiter = false
}
}