mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-30 02:02:43 +00:00
* add warning for when MAP_POPULATE mmap flag not set * Make mmap flags method handle any flags, where MAP_POPULATE is just one of them * Only have the log print out on restores * Add test, make logic more consistent * Add changelog * Add godoc for test * Make test less dangerous
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package raft
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func init() {
|
|
getMmapFlags = getMmapFlagsLinux
|
|
usingMapPopulate = usingMapPopulateLinux
|
|
}
|
|
|
|
func getMmapFlagsLinux(dbPath string) int {
|
|
if setMapPopulateFlag(dbPath) {
|
|
return unix.MAP_POPULATE
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
// setMapPopulateFlag determines whether we should set the MAP_POPULATE flag, which
|
|
// prepopulates page tables to be mapped in the virtual memory space,
|
|
// helping reduce slowness at runtime caused by page faults.
|
|
// We only want to set this flag if we've determined there's enough memory on the system available to do so.
|
|
func setMapPopulateFlag(dbPath string) bool {
|
|
if os.Getenv("VAULT_RAFT_DISABLE_MAP_POPULATE") != "" {
|
|
return false
|
|
}
|
|
stat, err := os.Stat(dbPath)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
size := stat.Size()
|
|
|
|
v, err := mem.VirtualMemoryWithContext(context.Background())
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// We won't worry about swap, since we already tell people not to use it.
|
|
if v.Total > uint64(size) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// the unix.MAP_POPULATE constant only exists on Linux,
|
|
// so reference to this constant can only live in a *_linux.go file
|
|
func usingMapPopulateLinux(mmapFlag int) bool {
|
|
if mmapFlag == unix.MAP_POPULATE {
|
|
return true
|
|
}
|
|
return false
|
|
}
|