Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PssFreeSnapshot #115

Merged
merged 2 commits into from
Nov 21, 2023
Merged

add PssFreeSnapshot #115

merged 2 commits into from
Nov 21, 2023

Conversation

leehinman
Copy link
Contributor

What does this PR do?

Frees the memory that is allocated when PssCaptureSnapshot is called.

Why is it important?

memory leak

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.md

Author's Checklist

  • Written by hand. Need to figure out what needs to happen for "go generate" to work. Does not work from Mac.

Related issues

Testing

  • run metricbeat 8.11.0 or 8.11.1 on Windows, notice memory consumption of metricbeat continually climbs
  • rebuild metricbeat using this path
  • memory consumption rises in first 10min or so, but is steady after that

@leehinman leehinman requested a review from a team as a code owner November 16, 2023 21:26
@leehinman leehinman requested review from rdner, fearful-symmetry and AndersonQ and removed request for a team November 16, 2023 21:26
@leehinman leehinman added bug Something isn't working Team:Elastic-Agent Label for the Agent team labels Nov 16, 2023
Comment on lines 97 to 100
defer func() {
cHandle, _ := syscall.GetCurrentProcess()
PssFreeSnapshot(cHandle, snapshotHandle)
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only possible issue with putting this in a defer is that if os.Exit is called this won't run. https://pkg.go.dev/os#Exit

If the memory here is allocated in the memory of the current process this won't matter as it'll all be freed on exit, if the memory is held by the kernel then I'm not sure what happens. Hopefully it is also freed because it is associated with the existence of this process in some way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer to call PssFreeSnapshot after calling PssQuerySnapshot but before checking its error code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to free after query, regardless of query return code.

@@ -94,6 +94,10 @@ func FetchNumThreads(pid int) (int, error) {
if err != nil {
return 0, fmt.Errorf("PssCaptureSnapshot failed: %w", err)
}
defer func() {
cHandle, _ := syscall.GetCurrentProcess()
PssFreeSnapshot(cHandle, snapshotHandle)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the errors should be returned to caller of FetchNumThreads. If I were using this as a library I would want to know that PssFreeSnapshot is failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I was going back and forth over that. changed to return error codes from GetCurrentProcess and PssFreeSnapshot

@leehinman leehinman merged commit f61b864 into elastic:main Nov 21, 2023
@nimarezainia
Copy link
Contributor

Thank you @leehinman

@@ -76,3 +77,11 @@ func PssQuerySnapshot(snapshotHandle syscall.Handle, informationClass uint32, bu
}
return
}

func PssFreeSnapshot(processHandle syscall.Handle, snapshotHandle syscall.Handle) (err error) {
r1, _, e1 := syscall.Syscall6(procPssFreeSnapshot.Addr(), 2, uintptr(processHandle), uintptr(snapshotHandle), 0, 0, 0, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amolnater-qasource
Copy link

Hi @pierrehilbert
We have revalidated this PR on latest 8.11.0 BC1 kibana cloud environment and found it working fine :

Observations:

  • We observe 3 windows agents and 1 metricbeat on different hosts.
  • We didn't observe any memory leaks under windows Performance Monitor for keeping them over 18 hours .

Build details:
VERSION: 8.11.2 BC1
BUILD: 68299
COMMIT: 92746356b61c3e3ac62b6d7045727f8d737fa4b5
Artifact link: https://staging.elastic.co/8.11.2-c2650cf6/downloads/beats/elastic-agent/elastic-agent-8.11.2-windows-x86_64.zip

Screen Recording:

Harshit-windows.-.ec2-100-26-159-124.compute-1.amazonaws.com.-.Remote.Desktop.Connection.2023-12-07.14-14-41.mp4
Amol.Win.-.ec2-100-26-253-68.compute-1.amazonaws.com.-.Remote.Desktop.Connection.2023-12-07.14-14-00.mp4
Amol.Windows.10GB.RAM.-.ec2-3-83-217-17.compute-1.amazonaws.com.-.Remote.Desktop.Connection.2023-12-07.14-12-35.mp4

Hence we are marking this as [QA:Validated].

Please let us know if we are missing anything here.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants