Skip to content

Commit

Permalink
log free memory value
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 14, 2024
1 parent b9d1198 commit ccecfff
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pkg/modgearman/mainWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type mainWorker struct {
min1 float64
min5 float64
min15 float64
memTotal int
memFree int
memTotal uint64
memFree uint64
maxOpenFiles uint64
maxPossibleWorker int
curBallooningWorker int
Expand Down Expand Up @@ -383,17 +383,17 @@ func (w *mainWorker) updateMemInfo() {
case bytes.HasPrefix(scanner.Bytes(), []byte(`MemFree:`)):
values := strings.Fields(scanner.Text())
if len(values) >= 1 && w.memFree == 0 {
w.memFree = getInt(values[1])
w.memFree = uint64(getFloat(values[1]))
}
case bytes.HasPrefix(scanner.Bytes(), []byte(`MemAvailable:`)):
values := strings.Fields(scanner.Text())
if len(values) >= 1 {
w.memFree = getInt(values[1])
w.memFree = uint64(getFloat(values[1]))
}
case bytes.HasPrefix(scanner.Bytes(), []byte(`MemTotal:`)):
values := strings.Fields(scanner.Text())
if len(values) >= 1 {
w.memTotal = getInt(values[1])
w.memTotal = uint64(getFloat(values[1]))
}
default:
continue
Expand All @@ -414,8 +414,12 @@ func (w *mainWorker) checkMemory() (ok bool, reason string) {
}

usedPercent := 100 - (w.memFree*100)/w.memTotal
if w.cfg.memLimit > 0 && usedPercent >= w.cfg.memLimit {
reason := fmt.Sprintf("cannot start any more worker, memory usage is too high: %d%% > %d%%", usedPercent, w.cfg.memLimit)
if w.cfg.memLimit > 0 && usedPercent >= uint64(w.cfg.memLimit) {

Check failure on line 417 in pkg/modgearman/mainWorker.go

View workflow job for this annotation

GitHub Actions / test

G115: integer overflow conversion int -> uint64 (gosec)
reason := fmt.Sprintf("cannot start any more worker, memory usage is too high: %d%% > %d%% (free: %s)",
usedPercent,
w.cfg.memLimit,
bytes2Human(w.memFree*1024),
)
log.Debug(reason)

return false, reason
Expand Down Expand Up @@ -605,3 +609,17 @@ func (w *mainWorker) StopStatusWorker() {
w.statusWorker.Shutdown()
w.statusWorker = nil
}

// convert bytes into a human readable string
func bytes2Human(num uint64) string {
const unit = 1024
if num < unit {
return fmt.Sprintf("%d B", num)
}
div, exp := unit, 0
for n := num / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f %cB", float64(num)/float64(div), "KMGTPE"[exp])

Check failure on line 624 in pkg/modgearman/mainWorker.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)
}

0 comments on commit ccecfff

Please sign in to comment.