forked from firecracker-microvm/firecracker-containerd
-
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.
CPU templates are only available for Intel's x86_64 processors
It doesn't work AMD's x86_64 processors sadly. Fixes firecracker-microvm#525. Signed-off-by: Kazuyoshi Kato <[email protected]>
- Loading branch information
Showing
7 changed files
with
138 additions
and
16 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,73 @@ | ||
// 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 internal | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"regexp" | ||
"runtime" | ||
"sync" | ||
) | ||
|
||
var ( | ||
isIntel bool | ||
isIntelOnce sync.Once | ||
) | ||
|
||
// SupportCPUTemplate returns true if Firecracker supports CPU templates on | ||
// the current architecture. | ||
func SupportCPUTemplate() (bool, error) { | ||
var err error | ||
|
||
isIntelOnce.Do(func() { | ||
if runtime.GOARCH == "amd64" { | ||
isIntel, err = checkIsIntel() | ||
} else { | ||
isIntel = false | ||
} | ||
}) | ||
|
||
return isIntel, err | ||
} | ||
|
||
var vendorID = regexp.MustCompile(`^vendor_id\s*:\s*(.+)$`) | ||
|
||
func checkIsIntel() (bool, error) { | ||
f, err := os.Open("/proc/cpuinfo") | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
id, err := findFirstVendorID(f) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return id == "GenuineIntel", nil | ||
} | ||
|
||
func findFirstVendorID(r io.Reader) (string, error) { | ||
s := bufio.NewScanner(r) | ||
for s.Scan() { | ||
line := s.Text() | ||
matches := vendorID.FindStringSubmatch(line) | ||
if len(matches) == 2 { | ||
return matches[1], nil | ||
} | ||
} | ||
return "", 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,41 @@ | ||
// 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 internal | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFindFirstVendorID(t *testing.T) { | ||
cases := []struct { | ||
input string | ||
vendorID string | ||
}{ | ||
{"vendor_id : GenuineIntel", "GenuineIntel"}, | ||
{"vendor_id : AuthenticAMD", "AuthenticAMD"}, | ||
|
||
// aarch64 doesn't have vendor IDs. | ||
{"", ""}, | ||
} | ||
for _, c := range cases { | ||
r := strings.NewReader(c.input) | ||
id, err := findFirstVendorID(r) | ||
require.NoError(t, err) | ||
assert.Equal(t, c.vendorID, id) | ||
} | ||
} |
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