-
Notifications
You must be signed in to change notification settings - Fork 135
/
util.go
247 lines (213 loc) · 6.76 KB
/
util.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 holmes
import (
"bytes"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
mem_util "github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/process"
)
// copied from https://github.com/containerd/cgroups/blob/318312a373405e5e91134d8063d04d59768a1bff/utils.go#L251
func parseUint(s string, base, bitSize int) (uint64, error) {
v, err := strconv.ParseUint(s, base, bitSize)
if err != nil {
intValue, intErr := strconv.ParseInt(s, base, bitSize)
// 1. Handle negative values greater than MinInt64 (and)
// 2. Handle negative values lesser than MinInt64
if intErr == nil && intValue < 0 {
return 0, nil
} else if intErr != nil &&
intErr.(*strconv.NumError).Err == strconv.ErrRange &&
intValue < 0 {
return 0, nil
}
return 0, err
}
return v, nil
}
// copied from https://github.com/containerd/cgroups/blob/318312a373405e5e91134d8063d04d59768a1bff/utils.go#L243
func readUint(path string) (uint64, error) {
v, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
return parseUint(strings.TrimSpace(string(v)), 10, 64)
}
// only reserve the top n.
func trimResultTop(buffer bytes.Buffer) []byte {
index := TrimResultTopN
arr := strings.SplitN(buffer.String(), "\n\n", TrimResultTopN+1)
if len(arr) <= TrimResultTopN {
index = len(arr) - 1
}
return []byte(strings.Join(arr[:index], "\n\n"))
}
// only reserve the front n bytes
func trimResultFront(buffer bytes.Buffer) []byte {
if buffer.Len() <= TrimResultMaxBytes {
return buffer.Bytes()
}
return buffer.Bytes()[:TrimResultMaxBytes-1]
}
// return values:
// 1. cpu percent, not division cpu cores yet,
// 2. RSS mem in bytes,
// 3. goroutine num,
// 4. thread num
func getUsage() (float64, uint64, int, int, error) {
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return 0, 0, 0, 0, err
}
cpuPercent, err := p.Percent(time.Second)
if err != nil {
return 0, 0, 0, 0, err
}
mem, err := p.MemoryInfo()
if err != nil {
return 0, 0, 0, 0, err
}
rss := mem.RSS
gNum := runtime.NumGoroutine()
tNum := getThreadNum()
return cpuPercent, rss, gNum, tNum, nil
}
// get cpu core number limited by CGroup.
func getCGroupCPUCore() (float64, error) {
var cpuQuota uint64
cpuPeriod, err := readUint(cgroupCpuPeriodPath)
if cpuPeriod == 0 || err != nil {
return 0, err
}
if cpuQuota, err = readUint(cgroupCpuQuotaPath); err != nil {
return 0, err
}
return float64(cpuQuota) / float64(cpuPeriod), nil
}
func getCGroupMemoryLimit() (uint64, error) {
usage, err := readUint(cgroupMemLimitPath)
if err != nil {
return 0, err
}
machineMemory, err := mem_util.VirtualMemory()
if err != nil {
return 0, err
}
limit := uint64(math.Min(float64(usage), float64(machineMemory.Total)))
return limit, nil
}
func getNormalMemoryLimit() (uint64, error) {
machineMemory, err := mem_util.VirtualMemory()
if err != nil {
return 0, err
}
return machineMemory.Total, nil
}
func getThreadNum() int {
return pprof.Lookup("threadcreate").Count()
}
// cpu mem goroutine thread err.
func collect(cpuCore float64, memoryLimit uint64) (int, int, int, int, error) {
cpu, mem, gNum, tNum, err := getUsage()
if err != nil {
return 0, 0, 0, 0, err
}
// The default percent is from all cores, multiply by cpu core
// but it's inconvenient to calculate the proper percent
// here we divide by core number, so we can set a percent bar more intuitively
cpuPercent := cpu / cpuCore
memPercent := float64(mem) / float64(memoryLimit) * 100
return int(cpuPercent), int(memPercent), gNum, tNum, nil
}
func matchRule(history ring, curVal, ruleMin, ruleAbs, ruleDiff, ruleMax int) (bool, ReasonType) {
// should bigger than rule min
if curVal < ruleMin {
return false, ReasonCurlLessMin
//fmt.Sprintf("curVal [%d]< ruleMin [%d]", curVal, ruleMin)
}
// if ruleMax is enable and current value bigger max, skip dumping
if ruleMax != NotSupportTypeMaxConfig && curVal >= ruleMax {
return false, ReasonCurGreaterMax
}
// the current peak load exceed the absolute value
if curVal > ruleAbs {
return true, ReasonCurGreaterAbs
// fmt.Sprintf("curVal [%d] > ruleAbs [%d]", curVal, ruleAbs)
}
// the peak load matches the rule
avg := history.avg()
if curVal >= avg*(100+ruleDiff)/100 {
return true, ReasonDiff
// fmt.Sprintf("curVal[%d] >= avg[%d]*(100+ruleDiff)/100", curVal, avg)
}
return false, ReasonCurlGreaterMin
}
func getBinaryFileName(filePath string, dumpType configureType, eventID string) string {
suffix := time.Now().Format("20060102150405.000") + ".log"
if len(eventID) == 0 {
return filepath.Join(filePath, check2name[dumpType]+"."+suffix)
}
return filepath.Join(filePath, check2name[dumpType]+"."+eventID+"."+suffix)
}
// fix #89
func getBinaryFileNameAndCreate(dump string, dumpType configureType, eventID string) (*os.File, string, error) {
filePath := getBinaryFileName(dump, dumpType, eventID)
f, err := os.OpenFile(filePath, defaultLoggerFlags, defaultLoggerPerm)
if err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dump, 0o755); err != nil {
return nil, filePath, err
}
f, err = os.OpenFile(filePath, defaultLoggerFlags, defaultLoggerPerm)
if err != nil {
return nil, filePath, err
}
}
return f, filePath, err
}
func writeFile(data bytes.Buffer, dumpType configureType, dumpOpts *DumpOptions, eventID string) (string, error) {
var buf []byte
if dumpOpts.DumpProfileType == textDump && !dumpOpts.DumpFullStack {
switch dumpType {
case mem, gcHeap, goroutine:
buf = trimResultTop(data)
case thread:
buf = trimResultFront(data)
default:
buf = data.Bytes()
}
} else {
buf = data.Bytes()
}
file, fileName, err := getBinaryFileNameAndCreate(dumpOpts.DumpPath, dumpType, eventID)
if err != nil {
return fileName, fmt.Errorf("pprof %v open file failed : %w", type2name[dumpType], err)
}
defer file.Close() //nolint:errcheck,gosec
if _, err = file.Write(buf); err != nil {
return fileName, fmt.Errorf("pprof %v write to file failed : %w", type2name[dumpType], err)
}
return fileName, nil
}