Skip to content

Commit 129bbc8

Browse files
authored
Merge pull request #643 from Junnplus/cpu-type
add cpuType for specify cpu
2 parents b61e2a3 + c54aa89 commit 129bbc8

File tree

7 files changed

+60
-32
lines changed

7 files changed

+60
-32
lines changed

pkg/limayaml/default.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ images:
2727
- location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-arm64.img"
2828
arch: "aarch64"
2929

30+
# Specify QEMU CPU emulation, e.g., "host", "qemu64", "Haswell".
31+
# You can see what options are available for host emulation with: `qemu-system-$(arch) -cpu help`.
32+
# Setting of instructions is supported like this: "qemu64,+ssse3".
33+
# Default: "host" (native arch), "qemu64" (Intel on ARM), or "cortex-a72" (ARM on Intel)
34+
cpuType: null
35+
3036
# CPUs: if you see performance issues, try limiting cpus to 1.
3137
# Default: 4
3238
cpus: null

pkg/limayaml/defaults.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
8181
}
8282
}
8383

84+
if y.CPUType == nil {
85+
y.CPUType = d.CPUType
86+
}
87+
if o.CPUType != nil {
88+
y.CPUType = o.CPUType
89+
}
90+
if y.CPUType == nil || *y.CPUType == "" {
91+
if IsNativeArch(*y.Arch) {
92+
y.CPUType = pointer.String("host")
93+
} else if *y.Arch == X8664 {
94+
// Intel on ARM
95+
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
96+
y.CPUType = pointer.String("qemu64")
97+
} else {
98+
// ARM on Intel
99+
y.CPUType = pointer.String("cortex-a72")
100+
}
101+
}
102+
84103
if y.CPUs == nil {
85104
y.CPUs = d.CPUs
86105
}
@@ -461,3 +480,9 @@ func ResolveArch(s *string) Arch {
461480
}
462481
return *s
463482
}
483+
484+
func IsNativeArch(arch Arch) bool {
485+
nativeX8664 := arch == X8664 && runtime.GOARCH == "amd64"
486+
nativeAARCH64 := arch == AARCH64 && runtime.GOARCH == "arm64"
487+
return nativeX8664 || nativeAARCH64
488+
}

pkg/limayaml/defaults_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ func TestFillDefault(t *testing.T) {
4747

4848
// Builtin default values
4949
builtin := LimaYAML{
50-
Arch: pointer.String(arch),
51-
CPUs: pointer.Int(4),
52-
Memory: pointer.String("4GiB"),
53-
Disk: pointer.String("100GiB"),
50+
Arch: pointer.String(arch),
51+
CPUType: pointer.String("host"),
52+
CPUs: pointer.Int(4),
53+
Memory: pointer.String("4GiB"),
54+
Disk: pointer.String("100GiB"),
5455
Containerd: Containerd{
5556
System: pointer.Bool(false),
5657
User: pointer.Bool(true),
@@ -168,10 +169,11 @@ func TestFillDefault(t *testing.T) {
168169

169170
// Choose values that are different from the "builtin" defaults
170171
d = LimaYAML{
171-
Arch: pointer.String("unknown"),
172-
CPUs: pointer.Int(7),
173-
Memory: pointer.String("5GiB"),
174-
Disk: pointer.String("105GiB"),
172+
Arch: pointer.String("unknown"),
173+
CPUType: pointer.String("host"),
174+
CPUs: pointer.Int(7),
175+
Memory: pointer.String("5GiB"),
176+
Disk: pointer.String("105GiB"),
175177
Containerd: Containerd{
176178
System: pointer.Bool(true),
177179
User: pointer.Bool(false),
@@ -280,10 +282,11 @@ func TestFillDefault(t *testing.T) {
280282
// User-provided overrides should override user-provided config settings
281283

282284
o = LimaYAML{
283-
Arch: pointer.String(arch),
284-
CPUs: pointer.Int(12),
285-
Memory: pointer.String("7GiB"),
286-
Disk: pointer.String("117GiB"),
285+
Arch: pointer.String(arch),
286+
CPUType: pointer.String("host"),
287+
CPUs: pointer.Int(12),
288+
Memory: pointer.String("7GiB"),
289+
Disk: pointer.String("117GiB"),
287290
Containerd: Containerd{
288291
System: pointer.Bool(true),
289292
User: pointer.Bool(false),

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type LimaYAML struct {
1010
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
1111
Images []File `yaml:"images" json:"images"` // REQUIRED
12+
CPUType *string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
1213
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
1314
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes
1415
Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes

pkg/limayaml/validate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func Validate(y LimaYAML, warn bool) error {
5050
}
5151
}
5252

53+
if *y.CPUType == "" {
54+
return fmt.Errorf("field `cpuType` must be set")
55+
}
56+
5357
if *y.CPUs == 0 {
5458
return errors.New("field `cpus` must be set")
5559
}

pkg/qemu/qemu.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,12 @@ func Cmdline(cfg Config) (string, []string, error) {
206206
}
207207
return "", nil, errors.New(errStr)
208208
}
209+
210+
cpu := *y.CPUType
211+
args = appendArgsIfNoConflict(args, "-cpu", cpu)
209212
switch *y.Arch {
210213
case limayaml.X8664:
211-
cpu := "qemu64"
212-
if isNativeArch(*y.Arch) {
213-
cpu = "host"
214-
}
215-
args = appendArgsIfNoConflict(args, "-cpu", cpu)
216-
if isNativeArch(*y.Arch) {
217-
args = appendArgsIfNoConflict(args, "-machine", "q35,accel="+accel)
218-
} else {
214+
if strings.HasPrefix(cpu, "qemu64") {
219215
// use q35 machine with vmware io port disabled.
220216
args = appendArgsIfNoConflict(args, "-machine", "q35,vmport=off")
221217
// use tcg accelerator with multi threading with 512MB translation block size
@@ -225,13 +221,10 @@ func Cmdline(cfg Config) (string, []string, error) {
225221
args = appendArgsIfNoConflict(args, "-accel", "tcg,thread=multi,tb-size=512")
226222
// This will disable CPU S3 state.
227223
args = append(args, "-global", "ICH9-LPC.disable_s3=1")
224+
} else {
225+
args = appendArgsIfNoConflict(args, "-machine", "q35,accel="+accel)
228226
}
229227
case limayaml.AARCH64:
230-
cpu := "cortex-a72"
231-
if isNativeArch(*y.Arch) {
232-
cpu = "host"
233-
}
234-
args = appendArgsIfNoConflict(args, "-cpu", cpu)
235228
args = appendArgsIfNoConflict(args, "-machine", "virt,accel="+accel+",highmem=off")
236229
}
237230

@@ -396,14 +389,8 @@ func getExe(arch limayaml.Arch) (string, []string, error) {
396389
return exe, args, nil
397390
}
398391

399-
func isNativeArch(arch limayaml.Arch) bool {
400-
nativeX8664 := arch == limayaml.X8664 && runtime.GOARCH == "amd64"
401-
nativeAARCH64 := arch == limayaml.AARCH64 && runtime.GOARCH == "arm64"
402-
return nativeX8664 || nativeAARCH64
403-
}
404-
405392
func getAccel(arch limayaml.Arch) string {
406-
if isNativeArch(arch) {
393+
if limayaml.IsNativeArch(arch) {
407394
switch runtime.GOOS {
408395
case "darwin":
409396
return "hvf"

pkg/store/instance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Instance struct {
3333
Status Status `json:"status"`
3434
Dir string `json:"dir"`
3535
Arch limayaml.Arch `json:"arch"`
36+
CPUType string `json:"cpuType"`
3637
CPUs int `json:"cpus,omitempty"`
3738
Memory int64 `json:"memory,omitempty"` // bytes
3839
Disk int64 `json:"disk,omitempty"` // bytes
@@ -75,6 +76,7 @@ func Inspect(instName string) (*Instance, error) {
7576
}
7677
inst.Dir = instDir
7778
inst.Arch = *y.Arch
79+
inst.CPUType = *y.CPUType
7880
inst.CPUs = *y.CPUs
7981
memory, err := units.RAMInBytes(*y.Memory)
8082
if err == nil {

0 commit comments

Comments
 (0)