first pass
This commit is contained in:
commit
7822529a38
31 files changed
+7050
No files matched your search
+58
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Capacity struct {
|
||||
Total Size
|
||||
Usable Size
|
||||
}
|
||||
|
||||
func DetectCapacity(reserve Size) (Capacity, error) {
|
||||
memMB, err := totalMemoryMB()
|
||||
if err != nil {
|
||||
return Capacity{}, err
|
||||
}
|
||||
total := Size{MemoryMB: memMB, CPUs: float64(runtime.NumCPU())}
|
||||
usable := total.Sub(reserve)
|
||||
if usable.MemoryMB <= 0 || usable.CPUs <= 0 {
|
||||
return Capacity{}, fmt.Errorf("host_reserve (%s) leaves nothing on this node (%s)", reserve, total)
|
||||
}
|
||||
return Capacity{Total: total, Usable: usable}, nil
|
||||
}
|
||||
|
||||
func (c Capacity) Room(used, want Size) bool {
|
||||
return used.Add(want).Fits(c.Usable)
|
||||
}
|
||||
|
||||
func (c Capacity) Slots(size Size) int {
|
||||
byMem := c.Usable.MemoryMB / size.MemoryMB
|
||||
byCPU := int(c.Usable.CPUs/size.CPUs + 1e-9)
|
||||
return min(byMem, byCPU)
|
||||
}
|
||||
|
||||
func totalMemoryMB() (int, error) {
|
||||
f, err := os.Open("/proc/meminfo")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
fields := strings.Fields(sc.Text())
|
||||
if len(fields) >= 2 && fields[0] == "MemTotal:" {
|
||||
kb, err := strconv.Atoi(fields[1])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return kb / 1024, nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("MemTotal not found in /proc/meminfo")
|
||||
}
|
||||
Reference in new issue
Block a user