amazon-ecs-agent icon indicating copy to clipboard operation
amazon-ecs-agent copied to clipboard

windows: changed method for retrieving Windows network statistics in case of awsvpc network mode

Open rawahars opened this issue 3 years ago • 0 comments

Summary

Previously, we were running a Powershell method (Get-NetAdapterStatistics) to retrieve the network statistics when the task is using awsvpc network mode. However, this can cause CPU spikes on smaller instance types.

Another method to obtain the network statistics would be access them using Win32 API. This method is significantly more performant than using Powershell cmdlet. Therefore, we are switching the agent implementation to use native Win32 API for retrieving the same.

Implementation

When we initialise the stats module for a given task, we will query and store the interface LUID of the task ENI. During the subsequent calls to retrieve the network statistics, we will query the statistics specific to the interface using it's LUID.

In order to accomplish the same, we are introducing two new APIs in NetworkUtils interface would be the preferred way to access Windows networking APIs in the agent. The two APIs are-

  • ConvertInterfaceAliasToLUID: Converts the interface alias into the corresponding LUID.
    • Internally invokes ConvertInterfaceAliasToLuid Win32 API to convert interface alias into LUID.
  • GetMIBIfEntryFromLUID: Returns the MIB_IF_ROW2 for the interface with the given LUID.
    • Internally invokes GetIfEntry2Ex Win32 API to obtain the specific interface which has given LUID.

Reference:

  • https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-convertinterfacealiastoluid
  • https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-getifentry2ex

Note: As part of this PR, refactoring has been done to improve the code quality of existing Windows workflow.

  • Removed Setter method from networkUtils package
  • Removed Setter method from watcher package
  • Introduced mocks for networkUtils package and used the same in watcher tests instead of object injection.

Testing

Tested the new agent using a custom AMI. Tested it out on smaller instance types for any periodic spikes.

New tests cover the changes: Yes

Description for the changelog

Licensing

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

rawahars avatar Oct 10 '22 18:10 rawahars