mmap_linux.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2020 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package z
  17. import (
  18. "fmt"
  19. "os"
  20. "reflect"
  21. "unsafe"
  22. "golang.org/x/sys/unix"
  23. )
  24. // mmap uses the mmap system call to memory-map a file. If writable is true,
  25. // memory protection of the pages is set so that they may be written to as well.
  26. func mmap(fd *os.File, writable bool, size int64) ([]byte, error) {
  27. mtype := unix.PROT_READ
  28. if writable {
  29. mtype |= unix.PROT_WRITE
  30. }
  31. return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED)
  32. }
  33. // mremap is a Linux-specific system call to remap pages in memory. This can be used in place of munmap + mmap.
  34. func mremap(data []byte, size int) ([]byte, error) {
  35. // taken from <https://github.com/torvalds/linux/blob/f8394f232b1eab649ce2df5c5f15b0e528c92091/include/uapi/linux/mman.h#L8>
  36. const MREMAP_MAYMOVE = 0x1
  37. header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
  38. mmapAddr, mmapSize, errno := unix.Syscall6(
  39. unix.SYS_MREMAP,
  40. header.Data,
  41. uintptr(header.Len),
  42. uintptr(size),
  43. uintptr(MREMAP_MAYMOVE),
  44. 0,
  45. 0,
  46. )
  47. if errno != 0 {
  48. return nil, errno
  49. }
  50. if mmapSize != uintptr(size) {
  51. return nil, fmt.Errorf("mremap size mismatch: requested: %d got: %d", size, mmapSize)
  52. }
  53. header.Data = mmapAddr
  54. header.Cap = size
  55. header.Len = size
  56. return data, nil
  57. }
  58. // munmap unmaps a previously mapped slice.
  59. //
  60. // unix.Munmap maintains an internal list of mmapped addresses, and only calls munmap
  61. // if the address is present in that list. If we use mremap, this list is not updated.
  62. // To bypass this, we call munmap ourselves.
  63. func munmap(data []byte) error {
  64. if len(data) == 0 || len(data) != cap(data) {
  65. return unix.EINVAL
  66. }
  67. _, _, errno := unix.Syscall(
  68. unix.SYS_MUNMAP,
  69. uintptr(unsafe.Pointer(&data[0])),
  70. uintptr(len(data)),
  71. 0,
  72. )
  73. if errno != 0 {
  74. return errno
  75. }
  76. return nil
  77. }
  78. // madvise uses the madvise system call to give advise about the use of memory
  79. // when using a slice that is memory-mapped to a file. Set the readahead flag to
  80. // false if page references are expected in random order.
  81. func madvise(b []byte, readahead bool) error {
  82. flags := unix.MADV_NORMAL
  83. if !readahead {
  84. flags = unix.MADV_RANDOM
  85. }
  86. return unix.Madvise(b, flags)
  87. }
  88. // msync writes any modified data to persistent storage.
  89. func msync(b []byte) error {
  90. return unix.Msync(b, unix.MS_SYNC)
  91. }