clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,60 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"slackerbot/internal/model"
|
||||
)
|
||||
|
||||
type Capacity struct {
|
||||
Total model.Size
|
||||
Usable model.Size
|
||||
}
|
||||
|
||||
func DetectCapacity(reserve model.Size) (Capacity, error) {
|
||||
memMB, err := totalMemoryMB()
|
||||
if err != nil {
|
||||
return Capacity{}, err
|
||||
}
|
||||
total := model.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 model.Size) bool {
|
||||
return used.Add(want).Fits(c.Usable)
|
||||
}
|
||||
|
||||
func (c Capacity) Slots(size model.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