This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
cmd/scollector: add a kill switch for total memory used by scollector #1866
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -245,17 +245,27 @@ func main() { | |
| } | ||
| collect.MaxQueueLen = conf.MaxQueueLen | ||
| } | ||
| maxMemMegaBytes := uint64(500) | ||
| maxMemMB := uint64(500) | ||
| if conf.MaxMem != 0 { | ||
| maxMemMegaBytes = conf.MaxMem | ||
| maxMemMB = conf.MaxMem | ||
| } | ||
| go func() { | ||
| maxMemBytes := maxMemMegaBytes * 1024 * 1024 | ||
| var m runtime.MemStats | ||
| for range time.Tick(time.Second * 30) { | ||
| runtime.ReadMemStats(&m) | ||
| if m.Alloc > maxMemBytes { | ||
| panic(fmt.Sprintf("memory max reached: (current: %v bytes, max: %v bytes)", m.Alloc, maxMemBytes)) | ||
| allocMB := m.Alloc / 1024 / 1024 | ||
| if allocMB > maxMemMB { | ||
| slog.Fatalf("memory max runtime reached: (current alloc: %v megabytes, max: %v megabytes)", allocMB, maxMemMB) | ||
| } | ||
| //See proccess_windows.go and process_linux.go for total process memory usage. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it true that this will only work if they happen to be monitoring the scollector process? Or maybe just any process monitoring enabled at all?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tried this with the wmi leaky build and seen it trigger at the appropriate time?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| //Note that in linux the rss metric includes shared pages, where as in | ||
| //Windows the private working set does not include shared memory. | ||
| //Total memory used seems to scale linerarly with m.Alloc. | ||
| //But we want this to catch a memory leak outside the runtime (WMI/CGO). | ||
| //So for now just add any runtime allocations to the allowed total limit. | ||
| maxMemTotalMB := maxMemMB + allocMB | ||
| if collectors.TotalScollectorMemoryMB > maxMemTotalMB { | ||
| slog.Fatalf("memory max total reached: (current total: %v megabytes, current runtime alloc: %v megabytes, max: %v megabytes)", collectors.TotalScollectorMemoryMB, allocMB, maxMemTotalMB) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I love the default of having the kill switch active. May surprise some people.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has always been active, but only for runtime memory. This adds a second check for total memory usage.
We were using over 1GB on many systems (around 80GB total on all systems) due to a WMI leak, which could have caused other systems to fail if we hadn't noticed.