-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcomponent.go
367 lines (303 loc) · 10.9 KB
/
component.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Package fd tracks the number of file descriptors used on the host.
package fd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"runtime"
"sync"
"time"
"github.com/olekukonko/tablewriter"
"github.com/prometheus/client_golang/prometheus"
apiv1 "github.com/leptonai/gpud/api/v1"
"github.com/leptonai/gpud/components"
"github.com/leptonai/gpud/pkg/eventstore"
"github.com/leptonai/gpud/pkg/file"
"github.com/leptonai/gpud/pkg/kmsg"
"github.com/leptonai/gpud/pkg/log"
"github.com/leptonai/gpud/pkg/process"
)
// Name is the name of the file descriptor component.
const Name = "file-descriptor"
const (
// DefaultThresholdAllocatedFileHandles is some high number, in case the system is under high file descriptor usage.
DefaultThresholdAllocatedFileHandles = 10000000
// DefaultThresholdRunningPIDs is some high number, in case fd-max is unlimited
DefaultThresholdRunningPIDs = 900000
WarningFileHandlesAllocationPercent = 80.0
ErrFileHandlesAllocationExceedsWarning = "file handles allocation exceeds its threshold (80%)"
)
var _ components.Component = &component{}
type component struct {
ctx context.Context
cancel context.CancelFunc
getFileHandlesFunc func() (uint64, uint64, error)
countRunningPIDsFunc func() (uint64, error)
getUsageFunc func() (uint64, error)
getLimitFunc func() (uint64, error)
checkFileHandlesSupportedFunc func() bool
checkFDLimitSupportedFunc func() bool
eventBucket eventstore.Bucket
kmsgSyncer *kmsg.Syncer
// thresholdAllocatedFileHandles is the number of file descriptors that are currently allocated,
// at which we consider the system to be under high file descriptor usage.
thresholdAllocatedFileHandles uint64
// thresholdRunningPIDs is the number of running pids at which
// we consider the system to be under high file descriptor usage.
// This is useful for triggering alerts when the system is under high load.
// And useful when the actual system fd-max is set to unlimited.
thresholdRunningPIDs uint64
lastMu sync.RWMutex
lastCheckResult *checkResult
}
func New(gpudInstance *components.GPUdInstance) (components.Component, error) {
cctx, ccancel := context.WithCancel(gpudInstance.RootCtx)
c := &component{
ctx: cctx,
cancel: ccancel,
getFileHandlesFunc: file.GetFileHandles,
countRunningPIDsFunc: process.CountRunningPids,
getUsageFunc: file.GetUsage,
getLimitFunc: file.GetLimit,
checkFileHandlesSupportedFunc: file.CheckFileHandlesSupported,
checkFDLimitSupportedFunc: file.CheckFDLimitSupported,
thresholdAllocatedFileHandles: DefaultThresholdAllocatedFileHandles,
thresholdRunningPIDs: DefaultThresholdRunningPIDs,
}
if gpudInstance.EventStore != nil && runtime.GOOS == "linux" {
var err error
c.eventBucket, err = gpudInstance.EventStore.Bucket(Name)
if err != nil {
ccancel()
return nil, err
}
if os.Geteuid() == 0 {
c.kmsgSyncer, err = kmsg.NewSyncer(cctx, Match, c.eventBucket)
if err != nil {
ccancel()
return nil, err
}
}
}
return c, nil
}
func (c *component) Name() string { return Name }
func (c *component) Start() error {
go func() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
_ = c.Check()
select {
case <-c.ctx.Done():
return
case <-ticker.C:
}
}
}()
return nil
}
func (c *component) LastHealthStates() apiv1.HealthStates {
c.lastMu.RLock()
lastCheckResult := c.lastCheckResult
c.lastMu.RUnlock()
return lastCheckResult.getLastHealthStates()
}
func (c *component) Events(ctx context.Context, since time.Time) (apiv1.Events, error) {
if c.eventBucket == nil {
return nil, nil
}
return c.eventBucket.Get(ctx, since)
}
func (c *component) Close() error {
log.Logger.Debugw("closing component")
c.cancel()
if c.kmsgSyncer != nil {
c.kmsgSyncer.Close()
}
if c.eventBucket != nil {
c.eventBucket.Close()
}
return nil
}
func (c *component) Check() components.CheckResult {
log.Logger.Infow("checking file descriptors")
cr := &checkResult{
ts: time.Now().UTC(),
}
defer func() {
c.lastMu.Lock()
c.lastCheckResult = cr
c.lastMu.Unlock()
}()
cr.AllocatedFileHandles, _, cr.err = c.getFileHandlesFunc()
if cr.err != nil {
cr.health = apiv1.HealthStateTypeUnhealthy
cr.reason = fmt.Sprintf("error getting file handles -- %s", cr.err)
return cr
}
metricAllocatedFileHandles.With(prometheus.Labels{}).Set(float64(cr.AllocatedFileHandles))
cr.RunningPIDs, cr.err = c.countRunningPIDsFunc()
if cr.err != nil {
cr.health = apiv1.HealthStateTypeUnhealthy
cr.reason = fmt.Sprintf("error getting running pids -- %s", cr.err)
return cr
}
metricRunningPIDs.With(prometheus.Labels{}).Set(float64(cr.RunningPIDs))
// may fail for mac
// e.g.,
// stat /proc: no such file or directory
cr.Usage, cr.err = c.getUsageFunc()
if cr.err != nil {
cr.health = apiv1.HealthStateTypeUnhealthy
cr.reason = fmt.Sprintf("error getting usage -- %s", cr.err)
return cr
}
cr.Limit, cr.err = c.getLimitFunc()
if cr.err != nil {
cr.health = apiv1.HealthStateTypeUnhealthy
cr.reason = fmt.Sprintf("error getting limit -- %s", cr.err)
return cr
}
metricLimit.With(prometheus.Labels{}).Set(float64(cr.Limit))
allocatedFileHandlesPct := calcUsagePct(cr.AllocatedFileHandles, cr.Limit)
cr.AllocatedFileHandlesPercent = fmt.Sprintf("%.2f", allocatedFileHandlesPct)
metricAllocatedFileHandlesPercent.With(prometheus.Labels{}).Set(allocatedFileHandlesPct)
usageVal := cr.RunningPIDs // for mac
if cr.Usage > 0 {
usageVal = cr.Usage
}
usedPct := calcUsagePct(usageVal, cr.Limit)
cr.UsedPercent = fmt.Sprintf("%.2f", usedPct)
metricUsedPercent.With(prometheus.Labels{}).Set(usedPct)
fileHandlesSupported := c.checkFileHandlesSupportedFunc()
cr.FileHandlesSupported = fileHandlesSupported
fdLimitSupported := c.checkFDLimitSupportedFunc()
cr.FDLimitSupported = fdLimitSupported
var thresholdRunningPIDsPct float64
if fdLimitSupported && c.thresholdRunningPIDs > 0 {
thresholdRunningPIDsPct = calcUsagePct(cr.Usage, c.thresholdRunningPIDs)
}
cr.ThresholdRunningPIDs = c.thresholdRunningPIDs
cr.ThresholdRunningPIDsPercent = fmt.Sprintf("%.2f", thresholdRunningPIDsPct)
metricThresholdRunningPIDs.With(prometheus.Labels{}).Set(float64(c.thresholdRunningPIDs))
metricThresholdRunningPIDsPercent.With(prometheus.Labels{}).Set(thresholdRunningPIDsPct)
var thresholdAllocatedFileHandlesPct float64
if c.thresholdAllocatedFileHandles > 0 {
thresholdAllocatedFileHandlesPct = calcUsagePct(cr.Usage, min(c.thresholdAllocatedFileHandles, cr.Limit))
}
cr.ThresholdAllocatedFileHandles = c.thresholdAllocatedFileHandles
cr.ThresholdAllocatedFileHandlesPercent = fmt.Sprintf("%.2f", thresholdAllocatedFileHandlesPct)
metricThresholdAllocatedFileHandles.With(prometheus.Labels{}).Set(float64(c.thresholdAllocatedFileHandles))
metricThresholdAllocatedFileHandlesPercent.With(prometheus.Labels{}).Set(thresholdAllocatedFileHandlesPct)
if thresholdAllocatedFileHandlesPct > WarningFileHandlesAllocationPercent {
cr.health = apiv1.HealthStateTypeDegraded
cr.reason = ErrFileHandlesAllocationExceedsWarning
} else {
cr.health = apiv1.HealthStateTypeHealthy
cr.reason = "no issue found (file descriptor usage is within the threshold)"
}
return cr
}
var _ components.CheckResult = &checkResult{}
type checkResult struct {
// The number of file descriptors currently allocated on the host (not per process).
AllocatedFileHandles uint64 `json:"allocated_file_handles"`
// The number of running PIDs returned by https://pkg.go.dev/github.com/shirou/gopsutil/v4/process#Pids.
RunningPIDs uint64 `json:"running_pids"`
Usage uint64 `json:"usage"`
Limit uint64 `json:"limit"`
// AllocatedFileHandlesPercent is the percentage of file descriptors that are currently allocated,
// based on the current file descriptor limit and the current number of file descriptors allocated on the host (not per process).
AllocatedFileHandlesPercent string `json:"allocated_file_handles_percent"`
// UsedPercent is the percentage of file descriptors that are currently in use,
// based on the current file descriptor limit on the host (not per process).
UsedPercent string `json:"used_percent"`
ThresholdAllocatedFileHandles uint64 `json:"threshold_allocated_file_handles"`
ThresholdAllocatedFileHandlesPercent string `json:"threshold_allocated_file_handles_percent"`
ThresholdRunningPIDs uint64 `json:"threshold_running_pids"`
ThresholdRunningPIDsPercent string `json:"threshold_running_pids_percent"`
// Set to true if the file handles are supported.
FileHandlesSupported bool `json:"file_handles_supported"`
// Set to true if the file descriptor limit is supported.
FDLimitSupported bool `json:"fd_limit_supported"`
// timestamp of the last check
ts time.Time
// error from the last check
err error
// tracks the healthy evaluation result of the last check
health apiv1.HealthStateType
// tracks the reason of the last check
reason string
}
func (cr *checkResult) String() string {
if cr == nil {
return ""
}
buf := bytes.NewBuffer(nil)
table := tablewriter.NewWriter(buf)
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.Append([]string{"Running PIDs", fmt.Sprintf("%d", cr.RunningPIDs)})
table.Append([]string{"Usage", fmt.Sprintf("%d", cr.Usage)})
table.Append([]string{"Limit", fmt.Sprintf("%d", cr.Limit)})
table.Append([]string{"Used %", cr.UsedPercent})
table.Append([]string{"Allocated File Handles", fmt.Sprintf("%d", cr.AllocatedFileHandles)})
table.Append([]string{"Allocated File Handles %", cr.AllocatedFileHandlesPercent})
table.Append([]string{"Threshold Alloc File Handles", fmt.Sprintf("%d", cr.ThresholdAllocatedFileHandles)})
table.Append([]string{"Threshold Alloc File Handles %", cr.ThresholdAllocatedFileHandlesPercent})
table.Append([]string{"Threshold Running PIDs", fmt.Sprintf("%d", cr.ThresholdRunningPIDs)})
table.Append([]string{"Threshold Running PIDs %", cr.ThresholdRunningPIDsPercent})
table.Render()
return buf.String()
}
func (cr *checkResult) Summary() string {
if cr == nil {
return ""
}
return cr.reason
}
func (cr *checkResult) HealthState() apiv1.HealthStateType {
if cr == nil {
return ""
}
return cr.health
}
func (cr *checkResult) getError() string {
if cr == nil || cr.err == nil {
return ""
}
return cr.err.Error()
}
func (cr *checkResult) getLastHealthStates() apiv1.HealthStates {
if cr == nil {
return apiv1.HealthStates{
{
Component: Name,
Name: Name,
Health: apiv1.HealthStateTypeHealthy,
Reason: "no data yet",
},
}
}
state := apiv1.HealthState{
Component: Name,
Name: Name,
Reason: cr.reason,
Error: cr.getError(),
Health: cr.health,
}
b, _ := json.Marshal(cr)
state.ExtraInfo = map[string]string{
"data": string(b),
"encoding": "json",
}
return apiv1.HealthStates{state}
}
func calcUsagePct(usage, limit uint64) float64 {
if limit > 0 {
return float64(usage) / float64(limit) * 100
}
return 0
}