Skip to content

Commit

Permalink
Remove CPU template for non-intel instances
Browse files Browse the repository at this point in the history
Helps in testing of AMD instance, by removing CPUTemplate from tests and
adding a test to check CPUTemplate support (Intel only).

Signed-off-by: Vaishnavi Vejella <[email protected]>
  • Loading branch information
vvejell1 committed Oct 10, 2022
1 parent 0b4d482 commit 9dda016
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 44 deletions.
1 change: 0 additions & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func createMachine(ctx context.Context, name string, forwardSignals []os.Signal)
LogLevel: "Info",
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(1),
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
MemSizeMib: Int64(256),
Smt: Bool(false),
},
Expand Down
2 changes: 0 additions & 2 deletions examples/cmd/snapshotting/example_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func createNewConfig(socketPath string, opts ...configOpt) sdk.Config {
kernelImagePath := filepath.Join(dir, "vmlinux")

var vcpuCount int64 = 2
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
var memSizeMib int64 = 256
smt := false

Expand All @@ -91,7 +90,6 @@ func createNewConfig(socketPath string, opts ...configOpt) sdk.Config {
KernelImagePath: kernelImagePath,
MachineCfg: models.MachineConfiguration{
VcpuCount: &vcpuCount,
CPUTemplate: cpuTemplate,
MemSizeMib: &memSizeMib,
Smt: &smt,
},
Expand Down
71 changes: 71 additions & 0 deletions internal/cpu_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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) {
if runtime.GOARCH != "amd64" {
return false, nil
}

var err error
isIntelOnce.Do(func() {
isIntel, err = checkIsIntel()
})
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
}
41 changes: 41 additions & 0 deletions internal/cpu_template_test.go
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)
}
}
74 changes: 33 additions & 41 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"flag"
"fmt"
"github.com/vishvananda/netns"
"io"
"io/ioutil"
"net"
Expand All @@ -35,6 +34,8 @@ import (
"testing"
"time"

"github.com/vishvananda/netns"

"github.com/containerd/fifo"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -44,6 +45,7 @@ import (
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
"github.com/firecracker-microvm/firecracker-go-sdk/fctesting"
"github.com/firecracker-microvm/firecracker-go-sdk/internal"
)

const (
Expand Down Expand Up @@ -114,10 +116,9 @@ func TestNewMachine(t *testing.T) {
Config{
DisableValidation: true,
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(1),
MemSizeMib: Int64(100),
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
Smt: Bool(false),
VcpuCount: Int64(1),
MemSizeMib: Int64(100),
Smt: Bool(false),
},
},
WithLogger(fctesting.NewLogEntry(t)))
Expand Down Expand Up @@ -172,7 +173,6 @@ func TestJailerMicroVMExecution(t *testing.T) {
}

var nCpus int64 = 2
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
var memSz int64 = 256

// short names and directory to prevent SUN_LEN error
Expand Down Expand Up @@ -210,10 +210,9 @@ func TestJailerMicroVMExecution(t *testing.T) {
LogLevel: "Debug",
KernelImagePath: vmlinuxPath,
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(nCpus),
CPUTemplate: cpuTemplate,
MemSizeMib: Int64(memSz),
Smt: Bool(false),
VcpuCount: Int64(nCpus),
MemSizeMib: Int64(memSz),
Smt: Bool(false),
},
Drives: []models.Drive{
{
Expand Down Expand Up @@ -296,7 +295,6 @@ func TestMicroVMExecution(t *testing.T) {
fctesting.RequiresKVM(t)

var nCpus int64 = 2
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
var memSz int64 = 256

dir, err := ioutil.TempDir("", t.Name())
Expand Down Expand Up @@ -326,10 +324,9 @@ func TestMicroVMExecution(t *testing.T) {
MetricsFifo: metricsFifo,
LogLevel: "Debug",
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(nCpus),
CPUTemplate: cpuTemplate,
MemSizeMib: Int64(memSz),
Smt: Bool(false),
VcpuCount: Int64(nCpus),
MemSizeMib: Int64(memSz),
Smt: Bool(false),
},
DisableValidation: true,
NetworkInterfaces: networkIfaces,
Expand Down Expand Up @@ -498,10 +495,9 @@ func testLogAndMetrics(t *testing.T, logLevel string) string {
DisableValidation: true,
KernelImagePath: getVmlinuxPath(t),
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(1),
MemSizeMib: Int64(64),
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
Smt: Bool(false),
VcpuCount: Int64(1),
MemSizeMib: Int64(64),
Smt: Bool(false),
},
MetricsPath: filepath.Join(dir, "fc-metrics.out"),
LogPath: filepath.Join(dir, "fc.log"),
Expand Down Expand Up @@ -553,12 +549,14 @@ func TestStartVMMOnce(t *testing.T) {
DisableValidation: true,
KernelImagePath: getVmlinuxPath(t),
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(1),
MemSizeMib: Int64(64),
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
Smt: Bool(false),
VcpuCount: Int64(1),
MemSizeMib: Int64(64),
Smt: Bool(false),
},
}
if cpu_temp, err := internal.SupportCPUTemplate(); cpu_temp && err == nil {
cfg.MachineCfg.CPUTemplate = models.CPUTemplate(models.CPUTemplateT2)
}
ctx := context.Background()
cmd := VMCommandBuilder{}.
WithSocketPath(cfg.SocketPath).
Expand Down Expand Up @@ -844,10 +842,9 @@ func TestStopVMMCleanup(t *testing.T) {
KernelImagePath: getVmlinuxPath(t),
NetworkInterfaces: []NetworkInterface{networkInterface},
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(1),
MemSizeMib: Int64(64),
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
Smt: Bool(false),
VcpuCount: Int64(1),
MemSizeMib: Int64(64),
Smt: Bool(false),
},
}
ctx := context.Background()
Expand Down Expand Up @@ -939,7 +936,6 @@ func TestMicroVMExecutionWithMmdsV2(t *testing.T) {
fctesting.RequiresKVM(t)

var nCpus int64 = 2
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
var memSz int64 = 256

dir, err := ioutil.TempDir("", t.Name())
Expand Down Expand Up @@ -969,10 +965,9 @@ func TestMicroVMExecutionWithMmdsV2(t *testing.T) {
MetricsFifo: metricsFifo,
LogLevel: "Debug",
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(nCpus),
CPUTemplate: cpuTemplate,
MemSizeMib: Int64(memSz),
Smt: Bool(false),
VcpuCount: Int64(nCpus),
MemSizeMib: Int64(memSz),
Smt: Bool(false),
},
DisableValidation: true,
NetworkInterfaces: networkIfaces,
Expand Down Expand Up @@ -1344,7 +1339,6 @@ func TestPID(t *testing.T) {
defer os.RemoveAll(dir)

var nCpus int64 = 2
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
var memSz int64 = 256
socketPath := filepath.Join(dir, "TestPID.sock")
defer os.Remove(socketPath)
Expand All @@ -1361,10 +1355,9 @@ func TestPID(t *testing.T) {
SocketPath: socketPath,
KernelImagePath: vmlinuxPath,
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(nCpus),
CPUTemplate: cpuTemplate,
MemSizeMib: Int64(memSz),
Smt: Bool(false),
VcpuCount: Int64(nCpus),
MemSizeMib: Int64(memSz),
Smt: Bool(false),
},
Drives: []models.Drive{
{
Expand Down Expand Up @@ -1675,10 +1668,9 @@ func createValidConfig(t *testing.T, socketPath string, opts ...machineConfigOpt
SocketPath: socketPath,
KernelImagePath: getVmlinuxPath(t),
MachineCfg: models.MachineConfiguration{
VcpuCount: Int64(2),
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
MemSizeMib: Int64(256),
Smt: Bool(false),
VcpuCount: Int64(2),
MemSizeMib: Int64(256),
Smt: Bool(false),
},
Drives: []models.Drive{
{
Expand Down

0 comments on commit 9dda016

Please sign in to comment.