BlueGit

timfa bluegit

Files

bluegit / internal / runnercfg / hardware.go

hardware.go

hardware.go 1.9 KiB Download Raw
1package runnercfg
2
3import (
4 "math"
5 "path/filepath"
6 "runtime"
7
8 "gitlab.com/Timfa/bluegit/internal/diskusage"
9)
10
11// Hardware is wat de runner van zijn machine ziet.
12type Hardware struct {
13 CPUs int
14 MemoryMB int
15 DiskFree int // GB vrij op de werkmap
16 DiskSize int // GB totaal
17}
18
19// Detect leest CPU's, het werkgeheugen en de schijf onder dir.
20func Detect(dir string) Hardware {
21 h := Hardware{CPUs: runtime.NumCPU(), MemoryMB: detectMemory()}
22 for d := dir; d != ""; {
23 if du, err := diskusage.Usage(d); err == nil {
24 h.DiskFree = int(du.Free >> 30)
25 h.DiskSize = int(du.Total >> 30)
26 break
27 }
28 next := filepath.Dir(d)
29 if next == d {
30 break
31 }
32 d = next
33 }
34 return h
35}
36
37// Recommend geeft logische waarden voor deze hardware. Uitgangspunt: een
38// CI-job wil ~2 kernen en ~2 GB; de host houdt 1 kern en 1 GB voor zichzelf,
39// en per job ~10 GB schijf voor images, checkout en cache.
40func Recommend(h Hardware) Settings {
41 r := Defaults()
42 cpu := h.CPUs
43 if cpu < 1 {
44 cpu = 1
45 }
46 mem := h.MemoryMB
47 byCPU := int(math.Max(1, float64((cpu-1)/2)))
48 byMem := 1
49 if mem > 0 {
50 byMem = int(math.Max(1, float64((mem-1024)/2048)))
51 }
52 byDisk := 64
53 if h.DiskFree > 0 {
54 byDisk = int(math.Max(1, float64(h.DiskFree/10)))
55 }
56 r.Concurrency = minInt(byCPU, byMem, byDisk, 8)
57 if r.Concurrency < 1 {
58 r.Concurrency = 1
59 }
60 share := float64(cpu-1) / float64(r.Concurrency)
61 if share < 1 {
62 share = 1
63 }
64 r.CPUs = math.Floor(share*2) / 2
65 if mem > 0 {
66 m := (mem - 1024) / r.Concurrency
67 if m < 512 {
68 m = 512
69 }
70 r.MemoryMB = m / 256 * 256
71 }
72 r.PidsLimit = 4096
73 if h.DiskFree > 0 {
74 r.MaxImages = minInt(maxInt(5, h.DiskFree/2), 50)
75 r.MaxDiskGB = maxInt(5, h.DiskFree*6/10)
76 r.PruneCacheGB = maxInt(2, h.DiskFree/10)
77 }
78 return r
79}
80
81func minInt(v ...int) int {
82 m := v[0]
83 for _, x := range v[1:] {
84 if x < m {
85 m = x
86 }
87 }
88 return m
89}
90
91func maxInt(a, b int) int {
92 if a > b {
93 return a
94 }
95 return b
96}