Skip to content

Commit

Permalink
fix issue with mem calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed May 7, 2021
1 parent 3cb0482 commit 253f7cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,19 @@ func (e *Engine) getUsableMemoryBytes() (uint64, error) {
return 0, err
}

if m.Free > e.reservedMemoryBytes {
return m.Free - e.reservedMemoryBytes, nil
// 1 -get reserved memory from current deployed workloads (theoretical max)
cap := e.statser.CurrentUnits()
// 2 -calculate total reserved memory
reserved := cap.Mru*gib + float64(e.reservedMemoryBytes)
var free float64
if reserved >= float64(m.Total) {
free = 0
} else {
free = float64(m.Total) - reserved
}

// no free memory
return 0, nil
return uint64(math.Min(float64(free), float64(m.Free))), nil

}

// Run starts reader reservation from the Source and handle them
Expand Down Expand Up @@ -517,17 +524,17 @@ func (e *Engine) updateStats() error {
// total reserved memory is calculated as the
// 1 - all reservations memory (r.Mru)
// 2 - the system reserved memory
totalReservedMemory := r.Mru + float64(e.reservedMemoryBytes/gib)
totalReservedMemoryBytes := r.Mru*gib + float64(e.reservedMemoryBytes)
// 3 - then we also compare this to actual node consumption (things that)
// are not accounted for! and we always report the max of both
sys, err := mem.VirtualMemory()
if err != nil {
return err
}

totalReservedMemory = math.Max(float64(sys.Used)/gib, totalReservedMemory)
totalReservedMemoryBytes = math.Max(float64(sys.Used), totalReservedMemoryBytes)
// and that is the value we should report
r.Mru = totalReservedMemory
r.Mru = totalReservedMemoryBytes / gib

log.Info().
Uint16("network", wl.Network).
Expand Down
1 change: 1 addition & 0 deletions pkg/provision/primitives/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (c *Counters) CurrentWorkloads() directory.WorkloadAmount {
}

// CurrentUnits return the number of each resource units reserved on the system
// Units are in GiB
func (c *Counters) CurrentUnits() directory.ResourceAmount {
gib := float64(gib)
return directory.ResourceAmount{
Expand Down

0 comments on commit 253f7cb

Please sign in to comment.