-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: saintube <saintube@foxmail.com>
- Loading branch information
Showing
5 changed files
with
221 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//go:build !arm64 | ||
// +build !arm64 | ||
|
||
/* | ||
Copyright 2022 The Koordinator 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 system | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// GetCacheInfo parses the output of `lscpu -e=CACHE` into l1l2 and l3 infos | ||
// e.g. | ||
// - input: "1:1:1:0" | ||
// - output: "1", 0, nil | ||
func GetCacheInfo(str string) (string, int32, error) { | ||
// e.g. | ||
// $ `lscpu -e=CPU,NODE,SOCKET,CORE,CACHE,ONLINE` | ||
// CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE | ||
// 0 0 0 0 0:0:0:0 yes | ||
// 1 0 0 0 0:0:0:0 yes | ||
// 2 0 0 1 1:1:1:0 yes | ||
// 3 0 0 1 1:1:1:0 yes | ||
infos := strings.Split(strings.TrimSpace(str), ":") | ||
// assert l1, l2 are private cache, so they has the same id with the core | ||
if len(infos) != 4 { | ||
return "", 0, fmt.Errorf("invalid cache info %s", str) | ||
} | ||
l1l2 := infos[0] | ||
l3, err := strconv.Atoi(infos[3]) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
return l1l2, int32(l3), nil | ||
} |
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,57 @@ | ||
//go:build arm64 | ||
// +build arm64 | ||
|
||
/* | ||
Copyright 2022 The Koordinator 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 system | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// GetCacheInfo parses the output of `lscpu -e=CACHE` into l1l2 and l3 infos | ||
// e.g. | ||
// - input: "-" | ||
// - output: "0", 0, nil | ||
func GetCacheInfo(str string) (string, int32, error) { | ||
// NOTE: `lscpu` can return empty cache info on arm64 platforms. We return a mocked info in this case. | ||
// e.g. | ||
// $ `lscpu -e=CPU,NODE,SOCKET,CORE,CACHE,ONLINE` | ||
// CPU NODE SOCKET CORE CACHE ONLINE | ||
// 0 0 0 0 - yes | ||
// 1 0 0 1 - yes | ||
// 2 0 0 2 - yes | ||
// 3 0 0 3 - yes | ||
s := strings.TrimSpace(str) | ||
if s == "-" { | ||
return "0", 0, nil | ||
} | ||
// otherwise we suppose the input is valid cache info | ||
// assert l1, l2 are private cache, so they has the same id with the core | ||
infos := strings.Split(s, ":") | ||
if len(infos) != 4 { | ||
return "", 0, fmt.Errorf("invalid format for cache info") | ||
} | ||
l1l2 := infos[0] | ||
l3, err := strconv.Atoi(infos[3]) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
return l1l2, int32(l3), nil | ||
} |
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,71 @@ | ||
//go:build arm64 | ||
// +build arm64 | ||
|
||
/* | ||
Copyright 2022 The Koordinator 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 system | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetCacheInfo(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
arg string | ||
want string | ||
want1 int32 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty cache info", | ||
arg: "- ", | ||
want: "0", | ||
want1: 0, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "empty cache info 1", | ||
arg: "-", | ||
want: "0", | ||
want1: 0, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid cache info", | ||
arg: "1:1:1:0", | ||
want: "1", | ||
want1: 0, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid cache info", | ||
arg: "1:1:1:xxxx", | ||
want: "", | ||
want1: 0, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
got, got1, gotErr := GetCacheInfo(tt.arg) | ||
assert.Equal(t, tt.want, got) | ||
assert.Equal(t, tt.want1, got1) | ||
assert.Equal(t, tt.wantErr, gotErr != nil) | ||
} | ||
} |