buf_mmap.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin dragonfly freebsd linux netbsd openbsd
  5. package bio
  6. import (
  7. "runtime"
  8. "sync/atomic"
  9. "syscall"
  10. )
  11. // mmapLimit is the maximum number of mmaped regions to create before
  12. // falling back to reading into a heap-allocated slice. This exists
  13. // because some operating systems place a limit on the number of
  14. // distinct mapped regions per process. As of this writing:
  15. //
  16. // Darwin unlimited
  17. // DragonFly 1000000 (vm.max_proc_mmap)
  18. // FreeBSD unlimited
  19. // Linux 65530 (vm.max_map_count) // TODO: query /proc/sys/vm/max_map_count?
  20. // NetBSD unlimited
  21. // OpenBSD unlimited
  22. var mmapLimit int32 = 1<<31 - 1
  23. func init() {
  24. // Linux is the only practically concerning OS.
  25. if runtime.GOOS == "linux" {
  26. mmapLimit = 30000
  27. }
  28. }
  29. func (r *Reader) sliceOS(length uint64) ([]byte, bool) {
  30. // For small slices, don't bother with the overhead of a
  31. // mapping, especially since we have no way to unmap it.
  32. const threshold = 16 << 10
  33. if length < threshold {
  34. return nil, false
  35. }
  36. // Have we reached the mmap limit?
  37. if atomic.AddInt32(&mmapLimit, -1) < 0 {
  38. atomic.AddInt32(&mmapLimit, 1)
  39. return nil, false
  40. }
  41. // Page-align the offset.
  42. off := r.Offset()
  43. align := syscall.Getpagesize()
  44. aoff := off &^ int64(align-1)
  45. data, err := syscall.Mmap(int(r.f.Fd()), aoff, int(length+uint64(off-aoff)), syscall.PROT_READ, syscall.MAP_SHARED|syscall.MAP_FILE)
  46. if err != nil {
  47. return nil, false
  48. }
  49. data = data[off-aoff:]
  50. r.MustSeek(int64(length), 1)
  51. return data, true
  52. }