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

[windows] - Introduce PdhGetRawCounterArrayW #254

Merged
merged 6 commits into from
Nov 26, 2024

Conversation

VihasMakwana
Copy link
Contributor

@VihasMakwana VihasMakwana commented Nov 23, 2024

While performing benchmarking, I noticed that by using https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetrawcounterarrayw we can achieve faster results with reduced memory usage.

This PR adds the methods for PdhGetRawCounterArrayW function.

It also does following thing to improve memory benchmarks:

  • Performs sort on the returned array based on instance name. This is helpful as each index of the returned array will correspond to one particular instance (i.e. CPU Core). This helps us save big on memory as we wouldn't need to build a map or perform any segregation based on CPU Cores.
    • The term "instance name" refers to a specific CPU core.
    • For the Processor Information performance counter, there will be n+1 instance names corresponding to n CPU cores.
      • Processor Information(0)/ % Idle Time refers to the idle time spent by CPU core 0
      • Processor Information(1)/ % Idle Time refers to the idle time spent by CPU core 1
      • ...
      • Processor Information(n)/ % Idle Time refers to the idle time spent by CPU core n
      • Processor Information(_Total)/ % Idle Time refers to the average idle time spent for all CPUs.

@VihasMakwana VihasMakwana requested a review from a team as a code owner November 23, 2024 11:59
@VihasMakwana VihasMakwana requested review from mauri870 and khushijain21 and removed request for a team November 23, 2024 11:59
@VihasMakwana VihasMakwana self-assigned this Nov 23, 2024
@VihasMakwana VihasMakwana added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Nov 23, 2024
@VihasMakwana VihasMakwana changed the title [windows] - Introduce [windows] - Introduce PdhGetRawCounterArrayW Nov 23, 2024
helpers/windows/pdh/pdh_windows.go Show resolved Hide resolved
ret := make([]PdhRawCounterItem, 0, len(items))
for _, item := range items {
instance := windows.UTF16PtrToString(item.SzName)
if filterTotal && strings.Contains(instance, "_Total") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have to worry if this is a non-English Windows system? Just wondering if we might have something besides "_Total".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For instance name? Nope. The name remains _Total.
I did a quick test on windows VM by changing language to Spanish.

For counter name? yes, the language makes a lot of change.
Hence, I've updated the AddCounter function in this PR. PTAL!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we have to worry if this is a non-English Windows system? Just wondering if we might have something besides "_Total".

For this specific piece of code? Nope.

h, err = PdhAddEnglishCounter(q.Handle, counterPath, 0)
} else {
h, err = PdhAddCounter(q.Handle, counterPath, 0)
}
Copy link
Contributor Author

@VihasMakwana VihasMakwana Nov 25, 2024

Choose a reason for hiding this comment

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

@leehinman These are two different API calls.
PdhAddCounter is language specific.
PdhAddEnglishCounter is language neutral.

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @VihasMakwana

Copy link
Contributor

@leehinman leehinman left a comment

Choose a reason for hiding this comment

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

Thanks for checking on the language pieces.

@VihasMakwana
Copy link
Contributor Author

@mauri870 can you take a look when you have a moment? You left some comments :)

@mauri870
Copy link
Member

@mauri870 can you take a look when you have a moment? You left some comments :)

Thanks, I have looked at it again and codewise it looks good to me. I'm not too familiar with windows but the usage here seems correct.

@VihasMakwana VihasMakwana merged commit 6ed7541 into elastic:main Nov 26, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants