-
Notifications
You must be signed in to change notification settings - Fork 315
/
kidled_util.go
225 lines (203 loc) · 6.4 KB
/
kidled_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
/*
Copyright 2022 The Koordinator 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 system
import (
"bufio"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"go.uber.org/atomic"
"k8s.io/klog/v2"
)
var (
isSupportColdMemory *atomic.Bool = atomic.NewBool(false)
isStartColdMemory *atomic.Bool = atomic.NewBool(false)
kidledColdBoundary = defaultKidledColdBoundary
defaultKidledScanPeriodInseconds uint32 = 5
defaultKidledUseHierarchy uint8 = 1
defaultKidledColdBoundary int = 5
)
// the unit of Csei, Dsei, Cfei ... is byte
// the detailed description can be seen in https://github.com/alibaba/cloud-kernel/blob/linux-next/Documentation/vm/kidled.rst
type ColdPageInfoByKidled struct {
Version string `json:"version"`
PageScans uint64 `json:"page_scans"`
SlabScans uint64 `json:"slab_scans"`
ScanPeriodInSeconds uint64 `json:"scan_period_in_seconds"`
UseHierarchy uint64 `json:"use_hierarchy"`
Buckets []uint64 `json:"buckets"`
Csei []uint64 `json:"csei"`
Dsei []uint64 `json:"dsei"`
Cfei []uint64 `json:"cfei"`
Dfei []uint64 `json:"dfei"`
Csui []uint64 `json:"csui"`
Dsui []uint64 `json:"dsui"`
Cfui []uint64 `json:"cfui"`
Dfui []uint64 `json:"dfui"`
Csea []uint64 `json:"csea"`
Dsea []uint64 `json:"dsea"`
Cfea []uint64 `json:"cfea"`
Dfea []uint64 `json:"dfea"`
Csua []uint64 `json:"csua"`
Dsua []uint64 `json:"dsua"`
Cfua []uint64 `json:"cfua"`
Dfua []uint64 `json:"dfua"`
Slab []uint64 `json:"slab"`
}
type KidledConfig struct {
ScanPeriodInseconds uint32
UseHierarchy uint8
KidledColdBoundary int
}
func ParseMemoryIdlePageStats(content string) (*ColdPageInfoByKidled, error) {
lines := strings.Split(content, "\n")
statMap := make(map[string]interface{})
var info = ColdPageInfoByKidled{}
if (len(lines)) != 31 {
return nil, fmt.Errorf("format err")
}
for i, line := range lines {
if i == 0 {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
statMap[fields[1][:len(fields[1])-1]] = fields[2]
} else if i < 5 {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
val, _ := strconv.ParseUint(fields[2], 10, 64)
statMap[fields[1][:len(fields[1])-1]] = val
} else if i == 5 {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
s := strings.Split(fields[2], ",")
var val = make([]uint64, len(s))
for k, v := range s {
val[k], _ = strconv.ParseUint(v, 10, 64)
}
statMap[fields[1][:len(fields[1])-1]] = val
} else if i >= 14 {
fields := strings.Fields(line)
if len(fields) < 1 {
continue
}
var val = make([]uint64, len(fields)-1)
for i := 1; i < len(fields); i++ {
val[i-1], _ = strconv.ParseUint(fields[i], 10, 64)
}
statMap[fields[0]] = val
}
}
elem := reflect.ValueOf(&info).Elem()
typeOfElem := elem.Type()
for i := 0; i < elem.NumField(); i++ {
val, ok := statMap[typeOfElem.Field(i).Tag.Get("json")]
if ok {
if typeOfElem.Field(i).Type.Kind() == reflect.String {
elem.Field(i).SetString(val.(string))
} else if typeOfElem.Field(i).Type.Kind() == reflect.Uint64 {
elem.Field(i).SetUint(val.(uint64))
} else if typeOfElem.Field(i).Type.Kind() == reflect.Slice {
sliceValue := reflect.ValueOf(val)
elem.Field(i).Set(sliceValue)
}
}
}
return &info, nil
}
// boundary is the index of [1,2) [2,5) [5,15) [15,30) [30,60) [60,120) [120,240) [240,+inf).
// if boundary is equal to 3, it will compute sum([5*scan_period_scands,+inf)) of cold page cache
func (i *ColdPageInfoByKidled) GetColdPageTotalBytes() uint64 {
return sumUint64Slice(i.Cfei, i.Dfei, i.Cfui, i.Dfui)
}
// check kidled and set var isSupportColdSupport
func IsKidledSupport() bool {
isSupportColdMemory.Store(false)
isSupport, str := KidledScanPeriodInSeconds.IsSupported("")
if !isSupport {
klog.V(4).Infof("file scan_period_in_seconds is not exist %s", str)
return isSupportColdMemory.Load()
}
isSupport, str = KidledUseHierarchy.IsSupported("")
if !isSupport {
klog.V(4).Infof("file use_hierarchy is not exist %s", str)
return isSupportColdMemory.Load()
}
isSupportColdMemory.Store(true)
return isSupportColdMemory.Load()
}
func GetIsSupportColdMemory() bool {
return isSupportColdMemory.Load()
}
func SetIsSupportColdMemory(flag bool) {
isSupportColdMemory.Store(flag)
}
func GetIsStartColdMemory() bool {
return isStartColdMemory.Load()
}
func SetIsStartColdMemory(flag bool) {
isStartColdMemory.Store(flag)
}
func SetKidledScanPeriodInSeconds(period uint32) error {
path := KidledScanPeriodInSeconds.Path("")
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
write := bufio.NewWriter(file)
write.WriteString(fmt.Sprintf("%d", period))
write.Flush()
return nil
}
func SetKidledUseHierarchy(useHierarchy uint8) error {
path := KidledUseHierarchy.Path("")
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
write := bufio.NewWriter(file)
write.WriteString(fmt.Sprintf("%d", useHierarchy))
write.Flush()
return nil
}
func GetKidledColdBoundary() int {
return kidledColdBoundary
}
func SetKidledColdBoundary(boudary int) {
kidledColdBoundary = boudary
}
func NewDefaultKidledConfig() *KidledConfig {
return &KidledConfig{
ScanPeriodInseconds: defaultKidledScanPeriodInseconds,
UseHierarchy: defaultKidledUseHierarchy,
KidledColdBoundary: defaultKidledColdBoundary,
}
}
func sumUint64Slice(nums ...[]uint64) uint64 {
var total uint64
for _, v := range nums {
for i := kidledColdBoundary; i < len(v); i++ {
total = total + v[i]
}
}
return total
}