Skip to content

Commit 9f4c702

Browse files
committed
pkg/limayaml: adjust the default CPUs and memory size
CPUs: 4 → min(4, host CPU cores) Memory: 4GiB → min(4 GiB, half of host memory) Signed-off-by: Akihiro Suda <[email protected]>
1 parent 8498a19 commit 9f4c702

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

examples/default.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ images:
3333
- location: "https://cloud-images.ubuntu.com/releases/23.04/release/ubuntu-23.04-server-cloudimg-arm64.img"
3434
arch: "aarch64"
3535

36-
# CPUs: if you see performance issues, try limiting cpus to 1.
37-
# 🟢 Builtin default: 4
36+
# CPUs
37+
# 🟢 Builtin default: min(4, host CPU cores)
3838
cpus: null
3939

4040
# Memory size
41-
# 🟢 Builtin default: "4GiB"
41+
# 🟢 Builtin default: min("4GiB", half of host memory)
4242
memory: null
4343

4444
# Disk size

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/mikefarah/yq/v4 v4.34.1
3232
github.com/nxadm/tail v1.4.8
3333
github.com/opencontainers/go-digest v1.0.0
34+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
3435
github.com/sethvargo/go-password v0.2.0
3536
github.com/sirupsen/logrus v1.9.3
3637
github.com/spf13/cobra v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=
236236
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
237237
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
238238
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
239+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
240+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
239241
github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us=
240242
github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
241243
github.com/pierrec/lz4 v2.3.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=

pkg/limayaml/defaults.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"strconv"
1212
"text/template"
1313

14+
"github.com/docker/go-units"
1415
"github.com/lima-vm/lima/pkg/networks"
16+
"github.com/pbnjay/memory"
1517

1618
"github.com/lima-vm/lima/pkg/guestagent/api"
1719
"github.com/lima-vm/lima/pkg/osutil"
@@ -76,6 +78,31 @@ func MACAddress(uniqueID string) string {
7678
return hw.String()
7779
}
7880

81+
func defaultCPUs() int {
82+
const x = 4
83+
if hostCPUs := runtime.NumCPU(); hostCPUs < x {
84+
return hostCPUs
85+
}
86+
return x
87+
}
88+
89+
func defaultMemory() uint64 {
90+
const x uint64 = 4 * 1024 * 1024 * 1024
91+
if halfOfHostMemory := memory.TotalMemory() / 2; halfOfHostMemory < x {
92+
return halfOfHostMemory
93+
}
94+
return x
95+
}
96+
97+
func defaultMemoryAsString() string {
98+
return units.BytesSize(float64(defaultMemory()))
99+
}
100+
101+
func defaultDiskSizeAsString() string {
102+
// currently just hardcoded
103+
return "100GiB"
104+
}
105+
79106
// FillDefault updates undefined fields in y with defaults from d (or built-in default), and overwrites with values from o.
80107
// Both d and o may be empty.
81108
//
@@ -174,7 +201,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
174201
y.CPUs = o.CPUs
175202
}
176203
if y.CPUs == nil || *y.CPUs == 0 {
177-
y.CPUs = pointer.Int(4)
204+
y.CPUs = pointer.Int(defaultCPUs())
178205
}
179206

180207
if y.Memory == nil {
@@ -184,7 +211,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
184211
y.Memory = o.Memory
185212
}
186213
if y.Memory == nil || *y.Memory == "" {
187-
y.Memory = pointer.String("4GiB")
214+
y.Memory = pointer.String(defaultMemoryAsString())
188215
}
189216

190217
if y.Disk == nil {
@@ -194,7 +221,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
194221
y.Disk = o.Disk
195222
}
196223
if y.Disk == nil || *y.Disk == "" {
197-
y.Disk = pointer.String("100GiB")
224+
y.Disk = pointer.String(defaultDiskSizeAsString())
198225
}
199226

200227
y.AdditionalDisks = append(append(o.AdditionalDisks, y.AdditionalDisks...), d.AdditionalDisks...)

pkg/limayaml/defaults_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func TestFillDefault(t *testing.T) {
5959
X8664: "qemu64",
6060
RISCV64: "rv64",
6161
},
62-
CPUs: pointer.Int(4),
63-
Memory: pointer.String("4GiB"),
64-
Disk: pointer.String("100GiB"),
62+
CPUs: pointer.Int(defaultCPUs()),
63+
Memory: pointer.String(defaultMemoryAsString()),
64+
Disk: pointer.String(defaultDiskSizeAsString()),
6565
Containerd: Containerd{
6666
System: pointer.Bool(false),
6767
User: pointer.Bool(true),

0 commit comments

Comments
 (0)