lib/memory: export process_memory_limit_bytes metric, which shows the amounts of memory the current process has access to

This metric is equivalent to `vm_available_memory_bytes`, but it has better name,
since the metric is related to a process, not VictoriaMetrics itself.

Leave `vm_available_memory_bytes` for backwards compatibility.
This commit is contained in:
Aliaksandr Valialkin 2022-04-07 15:21:17 +03:00
parent 2b59fff526
commit a96eb16329
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 11 additions and 6 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/metrics"
) )
var ( var (
@ -14,11 +15,15 @@ var (
allowedBytes = flagutil.NewBytes("memory.allowedBytes", 0, `Allowed size of system memory VictoriaMetrics caches may occupy. This option overrides -memory.allowedPercent if set to a non-zero value. Too low a value may increase the cache miss rate usually resulting in higher CPU and disk IO usage. Too high a value may evict too much data from OS page cache resulting in higher disk IO usage`) allowedBytes = flagutil.NewBytes("memory.allowedBytes", 0, `Allowed size of system memory VictoriaMetrics caches may occupy. This option overrides -memory.allowedPercent if set to a non-zero value. Too low a value may increase the cache miss rate usually resulting in higher CPU and disk IO usage. Too high a value may evict too much data from OS page cache resulting in higher disk IO usage`)
) )
var _ = metrics.NewGauge("process_memory_limit_bytes", func() float64 {
return float64(memoryLimit)
})
var ( var (
allowedMemory int allowedMemory int
remainingMemory int remainingMemory int
memoryLimit int
) )
var once sync.Once var once sync.Once
func initOnce() { func initOnce() {
@ -26,18 +31,18 @@ func initOnce() {
// Do not use logger.Panicf here, since logger may be uninitialized yet. // 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")) panic(fmt.Errorf("BUG: memory.Allowed must be called only after flag.Parse call"))
} }
mem := sysTotalMemory() memoryLimit = sysTotalMemory()
if allowedBytes.N <= 0 { if allowedBytes.N <= 0 {
if *allowedPercent < 1 || *allowedPercent > 200 { if *allowedPercent < 1 || *allowedPercent > 200 {
logger.Panicf("FATAL: -memory.allowedPercent must be in the range [1...200]; got %g", *allowedPercent) logger.Panicf("FATAL: -memory.allowedPercent must be in the range [1...200]; got %g", *allowedPercent)
} }
percent := *allowedPercent / 100 percent := *allowedPercent / 100
allowedMemory = int(float64(mem) * percent) allowedMemory = int(float64(memoryLimit) * percent)
remainingMemory = mem - allowedMemory remainingMemory = memoryLimit - allowedMemory
logger.Infof("limiting caches to %d bytes, leaving %d bytes to the OS according to -memory.allowedPercent=%g", allowedMemory, remainingMemory, *allowedPercent) logger.Infof("limiting caches to %d bytes, leaving %d bytes to the OS according to -memory.allowedPercent=%g", allowedMemory, remainingMemory, *allowedPercent)
} else { } else {
allowedMemory = allowedBytes.N allowedMemory = allowedBytes.N
remainingMemory = mem - allowedMemory remainingMemory = memoryLimit - allowedMemory
logger.Infof("limiting caches to %d bytes, leaving %d bytes to the OS according to -memory.allowedBytes=%s", allowedMemory, remainingMemory, allowedBytes.String()) logger.Infof("limiting caches to %d bytes, leaving %d bytes to the OS according to -memory.allowedBytes=%s", allowedMemory, remainingMemory, allowedBytes.String())
} }
} }

View file

@ -131,7 +131,7 @@ var rawItemsShardsPerTable = func() int {
if multiplier > 16 { if multiplier > 16 {
multiplier = 16 multiplier = 16
} }
return (cpus * multiplier + 1) / 2 return (cpus*multiplier + 1) / 2
}() }()
const maxBlocksPerShard = 256 const maxBlocksPerShard = 256