forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug: Windows numCPU returning incorrectly for values above 64
- Loading branch information
1 parent
5815336
commit 43cd383
Showing
10 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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
3 changes: 1 addition & 2 deletions
3
agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs/client/ecs_client.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/utils/utils_unix.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/utils/utils_windows.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package utils | ||
|
||
import "runtime" | ||
|
||
func GetNumCPU() int { | ||
return runtime.NumCPU() | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"runtime" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
var ( | ||
win32APIGetAllActiveProcessorCount = windows.GetActiveProcessorCount | ||
golangRuntimeNumCPU = runtime.NumCPU | ||
) | ||
|
||
// GetNumCPU On Windows, runtime.NumCPU() does not return the correct value for vCPU > 64 | ||
// To resolve this, we are using MSFT implementation of the function: https://github.com/microsoft/hcsshim/blob/dd45838a9bf9ff8f431847aaf3e4421763c15c49/internal/processorinfo/processor_count.go#L14 | ||
// Background around issue: https://github.com/moby/moby/issues/38935, https://learn.microsoft.com/en-us/windows/win32/procthread/processor-groups | ||
// Explanation: As was mentioned above, many of the Windows processor affinity functions will only return the information for a single Processor Group. | ||
// Since a single group can only hold 64 logical processors, this means when there are more they will be divided into multiple groups. | ||
// Golang runtime.NumCPU will only return the value of one Processor Group (not the sum of all). | ||
func GetNumCPU() int { | ||
if amount := win32APIGetAllActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS); amount != 0 { | ||
return int(amount) | ||
} | ||
// If the Win32 API does not return correctly, fall back to runtime.NumCPU() | ||
return golangRuntimeNumCPU() | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func TestGetNumCPU(t *testing.T) { | ||
testCases := []struct { | ||
win32APIReturn uint32 | ||
runtimeNumCPUReturn int | ||
expectedAnswer int | ||
name string | ||
}{ | ||
{128, 64, 128, "Both are valid"}, | ||
{0, 64, 64, "win32 API invalid"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
defer func() { | ||
win32APIGetAllActiveProcessorCount = windows.GetActiveProcessorCount | ||
golangRuntimeNumCPU = runtime.NumCPU | ||
}() | ||
|
||
win32APIGetAllActiveProcessorCount = func(groupNumber uint16) (ret uint32) { | ||
return tc.win32APIReturn | ||
} | ||
|
||
golangRuntimeNumCPU = func() int { | ||
return tc.runtimeNumCPUReturn | ||
} | ||
|
||
assert.Equal(t, tc.expectedAnswer, GetNumCPU(), tc.name) | ||
}) | ||
} | ||
|
||
} |