Always use fallocate() on Linux for SHM creation

This commit is contained in:
Kovid Goyal
2023-09-23 10:55:15 +05:30
parent 6619bf33b0
commit 5fede41205
3 changed files with 6 additions and 10 deletions

View File

@@ -5,12 +5,12 @@
package shm
import (
"errors"
"fmt"
"golang.org/x/sys/unix"
)
var _ = fmt.Print
func Fallocate_simple(fd int, size int64) (err error) {
return unix.ENOSYS
return errors.ErrUnsupported
}

View File

@@ -92,16 +92,15 @@ func CreateTemp(pattern string, size uint64) (MMap, error) {
return create_temp(pattern, size)
}
var force_use_of_fallocate bool = false
func truncate_or_unlink(ans *os.File, size uint64) (err error) {
fd := int(ans.Fd())
if err = Fallocate_simple(fd, int64(size)); err != nil {
if force_use_of_fallocate {
sz := int64(size)
if err = Fallocate_simple(fd, sz); err != nil {
if !errors.Is(err, errors.ErrUnsupported) {
return err
}
for {
err = unix.Ftruncate(fd, int64(size))
err = unix.Ftruncate(fd, sz)
if !errors.Is(err, unix.EINTR) {
break
}

View File

@@ -9,15 +9,12 @@ import (
"io/fs"
"os"
"reflect"
"runtime"
"testing"
)
var _ = fmt.Print
func TestSHM(t *testing.T) {
force_use_of_fallocate = runtime.GOOS == "linux"
defer func() { force_use_of_fallocate = false }()
data := make([]byte, 13347)
_, _ = rand.Read(data)
mm, err := CreateTemp("test-kitty-shm-", uint64(len(data)))