2019-05-22 21:16:55 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
2020-08-14 16:12:10 +00:00
|
|
|
var (
|
|
|
|
allowedPercent = flag.Float64("memory.allowedPercent", 60, "Allowed percent of system memory VictoriaMetrics caches may occupy. "+
|
|
|
|
"See also -memory.allowedBytes. "+
|
|
|
|
"Too low value may increase cache miss rate, which usually results in higher CPU and disk IO usage. "+
|
|
|
|
"Too high value may evict too much data from OS page cache, which will result in higher disk IO usage")
|
|
|
|
allowedBytes = flag.Int("memory.allowedBytes", 0, "Allowed size of system memory VictoriaMetrics caches may occupy. "+
|
|
|
|
"This option overrides -memory.allowedPercent if set to non-zero value. "+
|
|
|
|
"Too low value may increase cache miss rate, which usually results in higher CPU and disk IO usage. "+
|
|
|
|
"Too high value may evict too much data from OS page cache, which will result in higher disk IO usage")
|
|
|
|
)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2019-08-25 11:39:39 +00:00
|
|
|
var (
|
|
|
|
allowedMemory int
|
|
|
|
remainingMemory int
|
|
|
|
)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
|
2019-08-25 11:39:39 +00:00
|
|
|
func initOnce() {
|
|
|
|
if !flag.Parsed() {
|
|
|
|
// Do not use logger.Panicf here, since logger may be uninitialized yet.
|
|
|
|
panic(fmt.Errorf("BUG: memory.Allowed must be called only after flag.Parse call"))
|
|
|
|
}
|
|
|
|
mem := sysTotalMemory()
|
2020-08-14 16:12:10 +00:00
|
|
|
if *allowedBytes <= 0 {
|
|
|
|
if *allowedPercent < 1 || *allowedPercent > 200 {
|
|
|
|
logger.Panicf("FATAL: -memory.allowedPercent must be in the range [1...200]; got %f", *allowedPercent)
|
|
|
|
}
|
|
|
|
percent := *allowedPercent / 100
|
|
|
|
allowedMemory = int(float64(mem) * percent)
|
|
|
|
} else {
|
|
|
|
allowedMemory = *allowedBytes
|
|
|
|
}
|
2019-08-25 11:39:39 +00:00
|
|
|
remainingMemory = mem - allowedMemory
|
2020-08-14 16:12:10 +00:00
|
|
|
logger.Infof("limiting caches to %d bytes, leaving %d bytes to the OS according to -memory.allowedPercent=%f and -memory.allowedBytes=%d",
|
|
|
|
allowedMemory, remainingMemory, *allowedPercent, *allowedBytes)
|
2019-08-25 11:39:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Allowed returns the amount of system memory allowed to use by the app.
|
|
|
|
//
|
|
|
|
// The function must be called only after flag.Parse is called.
|
|
|
|
func Allowed() int {
|
2019-08-25 11:39:39 +00:00
|
|
|
once.Do(initOnce)
|
2019-05-22 21:16:55 +00:00
|
|
|
return allowedMemory
|
|
|
|
}
|
2019-08-25 11:39:39 +00:00
|
|
|
|
|
|
|
// Remaining returns the amount of memory remaining to the OS.
|
|
|
|
//
|
|
|
|
// This function must be called only after flag.Parse is called.
|
|
|
|
func Remaining() int {
|
|
|
|
once.Do(initOnce)
|
|
|
|
return remainingMemory
|
|
|
|
}
|