-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: helper: add the numanode helper package
Add a package to consolidate common usage of Zones as NUMA Nodes. This encapsulate patterns commonly seen in the consumer code. Since no better or cleaner approach emerge, better to cleanup and centralize this code. Signed-off-by: Francesco Romani <[email protected]> Signed-off-by: Francesco Romani <[email protected]>
- Loading branch information
Showing
3 changed files
with
240 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 helper | ||
|
||
// known zone types | ||
const ( | ||
// NUMA Node. Synonyms: NUMA zone, NUMA cell... | ||
ZoneTypeNUMANode string = "Node" | ||
) |
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,60 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 numanode | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
canonicalName = "node" | ||
canonicalSep = "-" | ||
) | ||
|
||
// IDToName creates the canonical NUMA node name given the NUMA cell ID | ||
func IDToName(numaID int) (string, error) { | ||
if numaID < 0 { | ||
return "", fmt.Errorf("invalid NUMA id range numaID: %d", numaID) | ||
} | ||
return canonicalName + canonicalSep + strconv.Itoa(numaID), nil | ||
} | ||
|
||
// NameToID retrieves the NUMA cell ID (integer) from the canonical | ||
// NUMA node name (node-X, cell ID would be X). | ||
func NameToID(node string) (int, error) { | ||
splitted := strings.Split(node, canonicalSep) | ||
if len(splitted) != 2 { | ||
return -1, fmt.Errorf("invalid name format: %s", node) | ||
} | ||
|
||
if splitted[0] != canonicalName { | ||
return -1, fmt.Errorf("invalid name format: %s", node) | ||
} | ||
|
||
numaID, err := strconv.Atoi(splitted[1]) | ||
if err != nil { | ||
return -1, fmt.Errorf("invalid name format: %s : %v", node, err) | ||
} | ||
|
||
if numaID < 0 { | ||
return -1, fmt.Errorf("invalid NUMA id range numaID: %d", numaID) | ||
} | ||
|
||
return numaID, nil | ||
} |
157 changes: 157 additions & 0 deletions
157
pkg/apis/topology/v1alpha2/helper/numanode/numanode_test.go
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,157 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 numanode | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIDToName(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
cellID int | ||
expected string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "zero", | ||
cellID: 0, | ||
expected: "node-0", | ||
}, | ||
{ | ||
name: "positive", | ||
cellID: 7, | ||
expected: "node-7", | ||
}, | ||
{ | ||
name: "excessive", | ||
cellID: 65535, | ||
expected: "node-65535", | ||
}, | ||
{ | ||
name: "negative", | ||
cellID: -1, | ||
expected: "", | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := IDToName(tt.cellID) | ||
gotErr := (err != nil) | ||
if gotErr != tt.expectedError { | ||
t.Fatalf("expected error=%v got=%v", tt.expectedError, gotErr) | ||
} | ||
if got != tt.expected { | ||
t.Fatalf("expected value %v got value %v", tt.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNameToID(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
cellName string | ||
expected int | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "zero", | ||
cellName: "node-0", | ||
expected: 0, | ||
}, | ||
{ | ||
name: "positive", | ||
cellName: "node-7", | ||
expected: 7, | ||
}, | ||
{ | ||
name: "excessive", | ||
cellName: "node-65535", | ||
expected: 65535, | ||
}, | ||
{ | ||
name: "negative", | ||
cellName: "node--1", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "wrong node id", | ||
cellName: "cell-1", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (1)", | ||
cellName: "Node-1", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (2)", | ||
cellName: "NODE-2", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (3)", | ||
cellName: "node_3", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (4)", | ||
cellName: "node 4", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (5)", | ||
cellName: "node-5-1", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (6)", | ||
cellName: "node6", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "case node id (7)", | ||
cellName: "--node-7", | ||
expected: -1, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := NameToID(tt.cellName) | ||
gotErr := (err != nil) | ||
if gotErr != tt.expectedError { | ||
t.Fatalf("expected error=%v got=%v", tt.expectedError, gotErr) | ||
} | ||
if got != tt.expected { | ||
t.Fatalf("expected value %v got value %v", tt.expected, got) | ||
} | ||
}) | ||
} | ||
} |