-
Notifications
You must be signed in to change notification settings - Fork 13
/
filter.go
276 lines (246 loc) · 7.52 KB
/
filter.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package yapscan
import (
"bytes"
"fmt"
"reflect"
"strings"
"text/template"
"github.com/dustin/go-humanize"
"github.com/fkie-cad/yapscan/procio"
)
// FilterMatch contains information about the matching of a MemorySegmentFilter.
type FilterMatch struct {
Result bool
MSI *procio.MemorySegmentInfo
Reason string // Reason for filter mismatch, if Result is false
}
// MemorySegmentFilterFunc is a callback, used to filter *procio.MemorySegmentInfo
// instances.
type MemorySegmentFilterFunc func(info *procio.MemorySegmentInfo) bool
// MemorySegmentFilter describes an interface, capable of filtering
// *procio.MemorySegmentInfo instances.
type MemorySegmentFilter interface {
Filter(info *procio.MemorySegmentInfo) *FilterMatch
Description() string
}
type baseFilter struct {
filter MemorySegmentFilterFunc
Parameter interface{}
reasonTemplate string
description string
}
func (f *baseFilter) Description() string {
return f.description
}
func (f *baseFilter) renderReason(info *procio.MemorySegmentInfo) string {
t := template.New("filterReason")
t.Funcs(template.FuncMap{
"bytes": func(val interface{}) string {
n := reflect.ValueOf(val)
num := n.Uint()
return humanize.Bytes(uint64(num))
},
"join": func(glue string, slice interface{}) string {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("argument is not a slice")
}
parts := make([]string, s.Len(), s.Len())
for i := 0; i < s.Len(); i++ {
str, ok := s.Index(i).Interface().(fmt.Stringer)
if !ok {
panic("slice does not contain implementations of the fmt.Stringer interface")
}
parts[i] = str.String()
}
return strings.Join(parts, glue)
},
})
_, err := t.Parse(f.reasonTemplate)
if err != nil {
panic("could not parse filter reason template: " + err.Error())
}
buf := &bytes.Buffer{}
err = t.Execute(buf, &struct {
Filter MemorySegmentFilter
MSI *procio.MemorySegmentInfo
}{
Filter: f,
MSI: info,
})
if err != nil {
panic(err)
}
return buf.String()
}
func (f *baseFilter) Filter(info *procio.MemorySegmentInfo) *FilterMatch {
var reasonForMismatch string
matches := f.filter(info)
if !matches {
reasonForMismatch = f.renderReason(info)
}
return &FilterMatch{
Result: matches,
MSI: info,
Reason: reasonForMismatch,
}
}
// NewFilterFromFunc creates a new filter from a given MemorySegmentFilterFunc.
func NewFilterFromFunc(filter MemorySegmentFilterFunc, parameter interface{}, reasonTemplate string, description string) MemorySegmentFilter {
return &baseFilter{
filter: filter,
Parameter: parameter,
reasonTemplate: reasonTemplate,
description: description,
}
}
// NewMaxSizeFilter creates a new filter, matching *procio.MemorySegmentInfo
// with the given maximum size.
func NewMaxSizeFilter(size uintptr) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
return info.Size <= size
},
size,
"segment too large, size: {{.MSI.Size|bytes}}, max-size: {{.Filter.Parameter|bytes}}",
fmt.Sprintf("segment must be smaller than %s", humanize.Bytes(uint64(size))),
)
}
// NewMinSizeFilter creates a new filter, matching *procio.MemorySegmentInfo
// with the given minimum size.
func NewMinSizeFilter(size uintptr) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
return info.Size >= size
},
size,
"segment too small, size: {{.MSI.Size|bytes}}, min-size: {{.Filter.Parameter|bytes}}",
fmt.Sprintf("segment must be larger than %s", humanize.Bytes(uint64(size))),
)
}
// NewStateFilter creates a new filter, matching *procio.MemorySegmentInfo
// with a procio.State equal to one of the given states.
func NewStateFilter(states []procio.State) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
for _, s := range states {
if info.State == s {
return true
}
}
return false
},
states,
"segment has wrong state, state: {{.MSI.State}}, allowed states: {{.Filter.Parameter|join \", \"}}",
fmt.Sprintf("segment state must be one of %q", states),
)
}
// NewTypeFilter creates a new filter, matching *procio.MemorySegmentInfo
// with a procio.SegmentType equal to one of the given types.
func NewTypeFilter(types []procio.SegmentType) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
for _, t := range types {
if info.Type == t {
return true
}
}
return false
},
types,
"segment has wrong type, type: {{.MSI.SegmentType}}, allowed types: {{.Filter.Parameter|join \", \"}}",
fmt.Sprintf("segment type must be one of %q", types),
)
}
// NewPermissionsFilterExact creates a new filter, matching *procio.MemorySegmentInfo
// with procio.Permissions exactly equal to one of the given perms.
func NewPermissionsFilterExact(perms []procio.Permissions) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
for _, p := range perms {
if info.CurrentPermissions.EqualTo(p) {
return true
}
}
return false
},
perms,
"segment has wrong permissions, permissions: {{.MSI.CurrentPermissions}}, allowed permissions: {{.Filter.Parameter|join \", \"}}",
fmt.Sprintf("permissions must be equal to one of %q", perms),
)
}
// NewPermissionsFilter creates a new filter, matching *procio.MemorySegmentInfo
// with procio.Permissions equal to or more permissive than the given perm.
func NewPermissionsFilter(perm procio.Permissions) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
return info.CurrentPermissions.IsMoreOrEquallyPermissiveThan(perm)
},
perm,
"segment has wrong permissions, permissions: {{.MSI.CurrentPermissions}}, min-permissions: {{.Filter.Parameter}}",
fmt.Sprintf("permissions must be equally or more permissive than %v", perm),
)
}
// NewRSSRatioFilter creates a new filter, matching *procio.MemorySegmentInfo
// with RSS/Size ratio equal or greater than the given value.
func NewRSSRatioFilter(ratio float64) MemorySegmentFilter {
return NewFilterFromFunc(
func(info *procio.MemorySegmentInfo) bool {
return float64(info.RSS)/float64(info.Size) >= ratio
},
ratio,
"segment has too low RSS/Size ratio, actual ratio: {{.MSI.RSS}}/{{.MSI.Size}}, min ratio: {{.Filter.Parameter}}",
fmt.Sprintf("RSS/Size ratio must be at least %v", ratio),
)
}
type andFilter struct {
filters []MemorySegmentFilter
}
// NewAndFilter creates a new filter, which is the logical AND-combination
// of all given MemorySegmentFilter instances.
func NewAndFilter(filters ...MemorySegmentFilter) MemorySegmentFilter {
fil := make([]MemorySegmentFilter, 0, len(filters))
for _, filter := range filters {
if filter != nil {
fil = append(fil, filter)
}
}
return &andFilter{
filters: fil,
}
}
func (f *andFilter) Description() string {
if len(f.filters) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString(f.filters[0].Description())
if len(f.filters) > 1 {
for _, filter := range f.filters[1:] {
if filter == nil {
continue
}
sb.WriteString(" AND ")
sb.WriteString(filter.Description())
}
}
return sb.String()
}
func (f *andFilter) Filter(info *procio.MemorySegmentInfo) *FilterMatch {
result := &FilterMatch{
Result: true,
MSI: info,
}
reasons := make([]string, 0)
for _, filter := range f.filters {
r := filter.Filter(info)
if !r.Result {
result.Result = false
reasons = append(reasons, result.Reason)
}
}
if !result.Result {
result.Reason = strings.Join(reasons, " AND ")
}
return result
}