Skip to content

Commit

Permalink
Make cpuType a map instead of a single value
Browse files Browse the repository at this point in the history
That way a template lima.yaml can specify alternate cpu types for
multiple archs. Also makes it possible to specify defaults and
overrides for cpu type globally.

Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed Feb 17, 2022
1 parent 129bbc8 commit 7cc1ad0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
10 changes: 7 additions & 3 deletions pkg/limayaml/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ images:
- location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-arm64.img"
arch: "aarch64"

# Specify QEMU CPU emulation, e.g., "host", "qemu64", "Haswell".
# Specify QEMU CPU type for each arch. Only used for emulation; cpu type is always "host"
# when the desired arch matches the host arch.
# You can see what options are available for host emulation with: `qemu-system-$(arch) -cpu help`.
# Setting of instructions is supported like this: "qemu64,+ssse3".
# Default: "host" (native arch), "qemu64" (Intel on ARM), or "cortex-a72" (ARM on Intel)
cpuType: null
cpuType:
# Default: "cortex-a72"
aarch64: null
# Default: "qemu64"
x86_64: null

# CPUs: if you see performance issues, try limiting cpus to 1.
# Default: 4
Expand Down
42 changes: 26 additions & 16 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,27 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
}
}

if y.CPUType == nil {
y.CPUType = d.CPUType
}
if o.CPUType != nil {
y.CPUType = o.CPUType
}
if y.CPUType == nil || *y.CPUType == "" {
if IsNativeArch(*y.Arch) {
y.CPUType = pointer.String("host")
} else if *y.Arch == X8664 {
// Intel on ARM
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
y.CPUType = pointer.String("qemu64")
} else {
// ARM on Intel
y.CPUType = pointer.String("cortex-a72")
cpuType := map[Arch]string{
AARCH64: "cortex-a72",
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
X8664: "qemu64",
}
for k, v := range d.CPUType {
if len(v) > 0 {
cpuType[k] = v
}
}
for k, v := range y.CPUType {
if len(v) > 0 {
cpuType[k] = v
}
}
for k, v := range o.CPUType {
if len(v) > 0 {
cpuType[k] = v
}
}
y.CPUType = cpuType

if y.CPUs == nil {
y.CPUs = d.CPUs
Expand Down Expand Up @@ -486,3 +489,10 @@ func IsNativeArch(arch Arch) bool {
nativeAARCH64 := arch == AARCH64 && runtime.GOARCH == "arm64"
return nativeX8664 || nativeAARCH64
}

func CPUType(y *LimaYAML) string {
if IsNativeArch(*y.Arch) {
return "host"
}
return y.CPUType[*y.Arch]
}
15 changes: 12 additions & 3 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func TestFillDefault(t *testing.T) {
// Builtin default values
builtin := LimaYAML{
Arch: pointer.String(arch),
CPUType: pointer.String("host"),
CPUType: map[Arch]string{
AARCH64: "cortex-a72",
X8664: "qemu64",
},
CPUs: pointer.Int(4),
Memory: pointer.String("4GiB"),
Disk: pointer.String("100GiB"),
Expand Down Expand Up @@ -170,7 +173,10 @@ func TestFillDefault(t *testing.T) {
// Choose values that are different from the "builtin" defaults
d = LimaYAML{
Arch: pointer.String("unknown"),
CPUType: pointer.String("host"),
CPUType: map[Arch]string{
AARCH64: "arm64",
X8664: "amd64",
},
CPUs: pointer.Int(7),
Memory: pointer.String("5GiB"),
Disk: pointer.String("105GiB"),
Expand Down Expand Up @@ -283,7 +289,10 @@ func TestFillDefault(t *testing.T) {

o = LimaYAML{
Arch: pointer.String(arch),
CPUType: pointer.String("host"),
CPUType: map[Arch]string{
AARCH64: "uber-arm",
X8664: "pentium",
},
CPUs: pointer.Int(12),
Memory: pointer.String("7GiB"),
Disk: pointer.String("117GiB"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type LimaYAML struct {
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
Images []File `yaml:"images" json:"images"` // REQUIRED
CPUType *string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
CPUType map[Arch]string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes
Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes
Expand Down
9 changes: 7 additions & 2 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ func Validate(y LimaYAML, warn bool) error {
}
}

if *y.CPUType == "" {
return fmt.Errorf("field `cpuType` must be set")
for arch := range y.CPUType {
switch arch {
case AARCH64, X8664:
// these are the only supported architectures
default:
return fmt.Errorf("field `cpuType` uses unsupported arch %q", arch)
}
}

if *y.CPUs == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func Cmdline(cfg Config) (string, []string, error) {
return "", nil, errors.New(errStr)
}

cpu := *y.CPUType
cpu := limayaml.CPUType(y)
args = appendArgsIfNoConflict(args, "-cpu", cpu)
switch *y.Arch {
case limayaml.X8664:
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Inspect(instName string) (*Instance, error) {
}
inst.Dir = instDir
inst.Arch = *y.Arch
inst.CPUType = *y.CPUType
inst.CPUType = limayaml.CPUType(y)
inst.CPUs = *y.CPUs
memory, err := units.RAMInBytes(*y.Memory)
if err == nil {
Expand Down

0 comments on commit 7cc1ad0

Please sign in to comment.