Skip to content

cmd/internal/bio: resolve todo to query vm.max_map_count on linux for mmapLimit #65107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/cmd/internal/bio/buf_mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
package bio

import (
"os"
"runtime"
"strconv"
"strings"
"sync/atomic"
"syscall"
)
Expand All @@ -20,18 +23,32 @@ import (
// Darwin unlimited
// DragonFly 1000000 (vm.max_proc_mmap)
// FreeBSD unlimited
// Linux 65530 (vm.max_map_count) // TODO: query /proc/sys/vm/max_map_count?
// Linux reads from /proc/sys/vm/max_map_count
// NetBSD unlimited
// OpenBSD unlimited
var mmapLimit int32 = 1<<31 - 1

func init() {
// Linux is the only practically concerning OS.
if runtime.GOOS == "linux" {
mmapLimit = 30000
mmapLimit = getLinuxMaxMapCount()
}
}

func getLinuxMaxMapCount() int32 {
data, err := os.ReadFile("/proc/sys/vm/max_map_count")
if err != nil {
return 1<<31 - 1
}

maxMapCount, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
return 1<<31 - 1
}

return int32(maxMapCount)
}

func (r *Reader) sliceOS(length uint64) ([]byte, bool) {
// For small slices, don't bother with the overhead of a
// mapping, especially since we have no way to unmap it.
Expand Down