Skip to content

Commit 4648d28

Browse files
authored
Merge pull request #456 from vvejell1/CpuTemplate
Removing CPU template from Config for non-intel instance
2 parents 0b4d482 + 9dda016 commit 4648d28

File tree

5 files changed

+145
-44
lines changed

5 files changed

+145
-44
lines changed

benchmark_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func createMachine(ctx context.Context, name string, forwardSignals []os.Signal)
5151
LogLevel: "Info",
5252
MachineCfg: models.MachineConfiguration{
5353
VcpuCount: Int64(1),
54-
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
5554
MemSizeMib: Int64(256),
5655
Smt: Bool(false),
5756
},

examples/cmd/snapshotting/example_demo.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func createNewConfig(socketPath string, opts ...configOpt) sdk.Config {
7777
kernelImagePath := filepath.Join(dir, "vmlinux")
7878

7979
var vcpuCount int64 = 2
80-
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
8180
var memSizeMib int64 = 256
8281
smt := false
8382

@@ -91,7 +90,6 @@ func createNewConfig(socketPath string, opts ...configOpt) sdk.Config {
9190
KernelImagePath: kernelImagePath,
9291
MachineCfg: models.MachineConfiguration{
9392
VcpuCount: &vcpuCount,
94-
CPUTemplate: cpuTemplate,
9593
MemSizeMib: &memSizeMib,
9694
Smt: &smt,
9795
},

internal/cpu_template.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package internal
15+
16+
import (
17+
"bufio"
18+
"io"
19+
"os"
20+
"regexp"
21+
"runtime"
22+
"sync"
23+
)
24+
25+
var (
26+
isIntel bool
27+
isIntelOnce sync.Once
28+
)
29+
30+
// SupportCPUTemplate returns true if Firecracker supports CPU templates on
31+
// the current architecture.
32+
func SupportCPUTemplate() (bool, error) {
33+
if runtime.GOARCH != "amd64" {
34+
return false, nil
35+
}
36+
37+
var err error
38+
isIntelOnce.Do(func() {
39+
isIntel, err = checkIsIntel()
40+
})
41+
return isIntel, err
42+
}
43+
44+
var vendorID = regexp.MustCompile(`^vendor_id\s*:\s*(.+)$`)
45+
46+
func checkIsIntel() (bool, error) {
47+
f, err := os.Open("/proc/cpuinfo")
48+
if err != nil {
49+
return false, err
50+
}
51+
defer f.Close()
52+
53+
id, err := findFirstVendorID(f)
54+
if err != nil {
55+
return false, err
56+
}
57+
58+
return id == "GenuineIntel", nil
59+
}
60+
61+
func findFirstVendorID(r io.Reader) (string, error) {
62+
s := bufio.NewScanner(r)
63+
for s.Scan() {
64+
line := s.Text()
65+
matches := vendorID.FindStringSubmatch(line)
66+
if len(matches) == 2 {
67+
return matches[1], nil
68+
}
69+
}
70+
return "", nil
71+
}

internal/cpu_template_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package internal
15+
16+
import (
17+
"strings"
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestFindFirstVendorID(t *testing.T) {
25+
cases := []struct {
26+
input string
27+
vendorID string
28+
}{
29+
{"vendor_id : GenuineIntel", "GenuineIntel"},
30+
{"vendor_id : AuthenticAMD", "AuthenticAMD"},
31+
32+
// aarch64 doesn't have vendor IDs.
33+
{"", ""},
34+
}
35+
for _, c := range cases {
36+
r := strings.NewReader(c.input)
37+
id, err := findFirstVendorID(r)
38+
require.NoError(t, err)
39+
assert.Equal(t, c.vendorID, id)
40+
}
41+
}

machine_test.go

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"errors"
2020
"flag"
2121
"fmt"
22-
"github.com/vishvananda/netns"
2322
"io"
2423
"io/ioutil"
2524
"net"
@@ -35,6 +34,8 @@ import (
3534
"testing"
3635
"time"
3736

37+
"github.com/vishvananda/netns"
38+
3839
"github.com/containerd/fifo"
3940
"github.com/sirupsen/logrus"
4041
"github.com/stretchr/testify/assert"
@@ -44,6 +45,7 @@ import (
4445
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
4546
ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
4647
"github.com/firecracker-microvm/firecracker-go-sdk/fctesting"
48+
"github.com/firecracker-microvm/firecracker-go-sdk/internal"
4749
)
4850

4951
const (
@@ -114,10 +116,9 @@ func TestNewMachine(t *testing.T) {
114116
Config{
115117
DisableValidation: true,
116118
MachineCfg: models.MachineConfiguration{
117-
VcpuCount: Int64(1),
118-
MemSizeMib: Int64(100),
119-
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
120-
Smt: Bool(false),
119+
VcpuCount: Int64(1),
120+
MemSizeMib: Int64(100),
121+
Smt: Bool(false),
121122
},
122123
},
123124
WithLogger(fctesting.NewLogEntry(t)))
@@ -172,7 +173,6 @@ func TestJailerMicroVMExecution(t *testing.T) {
172173
}
173174

174175
var nCpus int64 = 2
175-
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
176176
var memSz int64 = 256
177177

178178
// short names and directory to prevent SUN_LEN error
@@ -210,10 +210,9 @@ func TestJailerMicroVMExecution(t *testing.T) {
210210
LogLevel: "Debug",
211211
KernelImagePath: vmlinuxPath,
212212
MachineCfg: models.MachineConfiguration{
213-
VcpuCount: Int64(nCpus),
214-
CPUTemplate: cpuTemplate,
215-
MemSizeMib: Int64(memSz),
216-
Smt: Bool(false),
213+
VcpuCount: Int64(nCpus),
214+
MemSizeMib: Int64(memSz),
215+
Smt: Bool(false),
217216
},
218217
Drives: []models.Drive{
219218
{
@@ -296,7 +295,6 @@ func TestMicroVMExecution(t *testing.T) {
296295
fctesting.RequiresKVM(t)
297296

298297
var nCpus int64 = 2
299-
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
300298
var memSz int64 = 256
301299

302300
dir, err := ioutil.TempDir("", t.Name())
@@ -326,10 +324,9 @@ func TestMicroVMExecution(t *testing.T) {
326324
MetricsFifo: metricsFifo,
327325
LogLevel: "Debug",
328326
MachineCfg: models.MachineConfiguration{
329-
VcpuCount: Int64(nCpus),
330-
CPUTemplate: cpuTemplate,
331-
MemSizeMib: Int64(memSz),
332-
Smt: Bool(false),
327+
VcpuCount: Int64(nCpus),
328+
MemSizeMib: Int64(memSz),
329+
Smt: Bool(false),
333330
},
334331
DisableValidation: true,
335332
NetworkInterfaces: networkIfaces,
@@ -498,10 +495,9 @@ func testLogAndMetrics(t *testing.T, logLevel string) string {
498495
DisableValidation: true,
499496
KernelImagePath: getVmlinuxPath(t),
500497
MachineCfg: models.MachineConfiguration{
501-
VcpuCount: Int64(1),
502-
MemSizeMib: Int64(64),
503-
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
504-
Smt: Bool(false),
498+
VcpuCount: Int64(1),
499+
MemSizeMib: Int64(64),
500+
Smt: Bool(false),
505501
},
506502
MetricsPath: filepath.Join(dir, "fc-metrics.out"),
507503
LogPath: filepath.Join(dir, "fc.log"),
@@ -553,12 +549,14 @@ func TestStartVMMOnce(t *testing.T) {
553549
DisableValidation: true,
554550
KernelImagePath: getVmlinuxPath(t),
555551
MachineCfg: models.MachineConfiguration{
556-
VcpuCount: Int64(1),
557-
MemSizeMib: Int64(64),
558-
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
559-
Smt: Bool(false),
552+
VcpuCount: Int64(1),
553+
MemSizeMib: Int64(64),
554+
Smt: Bool(false),
560555
},
561556
}
557+
if cpu_temp, err := internal.SupportCPUTemplate(); cpu_temp && err == nil {
558+
cfg.MachineCfg.CPUTemplate = models.CPUTemplate(models.CPUTemplateT2)
559+
}
562560
ctx := context.Background()
563561
cmd := VMCommandBuilder{}.
564562
WithSocketPath(cfg.SocketPath).
@@ -844,10 +842,9 @@ func TestStopVMMCleanup(t *testing.T) {
844842
KernelImagePath: getVmlinuxPath(t),
845843
NetworkInterfaces: []NetworkInterface{networkInterface},
846844
MachineCfg: models.MachineConfiguration{
847-
VcpuCount: Int64(1),
848-
MemSizeMib: Int64(64),
849-
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
850-
Smt: Bool(false),
845+
VcpuCount: Int64(1),
846+
MemSizeMib: Int64(64),
847+
Smt: Bool(false),
851848
},
852849
}
853850
ctx := context.Background()
@@ -939,7 +936,6 @@ func TestMicroVMExecutionWithMmdsV2(t *testing.T) {
939936
fctesting.RequiresKVM(t)
940937

941938
var nCpus int64 = 2
942-
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
943939
var memSz int64 = 256
944940

945941
dir, err := ioutil.TempDir("", t.Name())
@@ -969,10 +965,9 @@ func TestMicroVMExecutionWithMmdsV2(t *testing.T) {
969965
MetricsFifo: metricsFifo,
970966
LogLevel: "Debug",
971967
MachineCfg: models.MachineConfiguration{
972-
VcpuCount: Int64(nCpus),
973-
CPUTemplate: cpuTemplate,
974-
MemSizeMib: Int64(memSz),
975-
Smt: Bool(false),
968+
VcpuCount: Int64(nCpus),
969+
MemSizeMib: Int64(memSz),
970+
Smt: Bool(false),
976971
},
977972
DisableValidation: true,
978973
NetworkInterfaces: networkIfaces,
@@ -1344,7 +1339,6 @@ func TestPID(t *testing.T) {
13441339
defer os.RemoveAll(dir)
13451340

13461341
var nCpus int64 = 2
1347-
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
13481342
var memSz int64 = 256
13491343
socketPath := filepath.Join(dir, "TestPID.sock")
13501344
defer os.Remove(socketPath)
@@ -1361,10 +1355,9 @@ func TestPID(t *testing.T) {
13611355
SocketPath: socketPath,
13621356
KernelImagePath: vmlinuxPath,
13631357
MachineCfg: models.MachineConfiguration{
1364-
VcpuCount: Int64(nCpus),
1365-
CPUTemplate: cpuTemplate,
1366-
MemSizeMib: Int64(memSz),
1367-
Smt: Bool(false),
1358+
VcpuCount: Int64(nCpus),
1359+
MemSizeMib: Int64(memSz),
1360+
Smt: Bool(false),
13681361
},
13691362
Drives: []models.Drive{
13701363
{
@@ -1675,10 +1668,9 @@ func createValidConfig(t *testing.T, socketPath string, opts ...machineConfigOpt
16751668
SocketPath: socketPath,
16761669
KernelImagePath: getVmlinuxPath(t),
16771670
MachineCfg: models.MachineConfiguration{
1678-
VcpuCount: Int64(2),
1679-
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
1680-
MemSizeMib: Int64(256),
1681-
Smt: Bool(false),
1671+
VcpuCount: Int64(2),
1672+
MemSizeMib: Int64(256),
1673+
Smt: Bool(false),
16821674
},
16831675
Drives: []models.Drive{
16841676
{

0 commit comments

Comments
 (0)