You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"fmt""os""syscall"
)
funcmain() {
filePath:="main.go"flags:=os.O_RDONLY|syscall.O_DIRECTfil, err:=os.OpenFile(filePath, flags, 0644)
iferr!=nil {
fmt.Printf("failed to open file: %s\n", err)
return
}
buf:=make([]byte, 10)
_, err=fil.ReadAt(buf, 0)
iferr!=nil {
fmt.Printf("failed to read file: %s\n", err)
return
}
fmt.Printf("succeed to read file\n")
}
What did you see happen?
root@host:/opt# go run main.go
failed to read file: read main.go: invalid argument
What did you expect to see?
succeed to read file that is opened using the O_DIRECT flag.
From the man page, it seems required to pass perfect arguments to ReadAt() so that buffer address, length, offset are all aligned.
As go is a easy-to-use language, is it worthy to handle alignment requirements inside the standard pkg, providing a more user-friend api.
The text was updated successfully, but these errors were encountered:
hzzb
changed the title
os: (*File).Pread() would probably fail if file is opened with O_DIRECT flag
os: (*File).ReadAt() would probably fail if file is opened with O_DIRECT flag
Jan 17, 2024
Thanks for your quick reply. It's by intention to open file with the O_DIRCT flag because we want to bypass kernel page cache for performance consideration. The flags we set is right. It's possible to let user application provide strictly aligned arguments when calling ReadAt(). but that's a bit of trivial. We succeed to read file when pass strict-aligned args. Kindly consult go team if it is worthing to take that trivial arguments alignment work into the standard os pkg. By doing so, future users can use ReadAt() with less efforts. community may help contribute if this is worthing.
Go version
go version go1.21.6 linux/amd64
Output of
go env
in your module/workspace:What did you do?
What did you see happen?
root@host:/opt# go run main.go
failed to read file: read main.go: invalid argument
What did you expect to see?
succeed to read file that is opened using the O_DIRECT flag.
From the man page, it seems required to pass perfect arguments to ReadAt() so that buffer address, length, offset are all aligned.
As go is a easy-to-use language, is it worthy to handle alignment requirements inside the standard pkg, providing a more user-friend api.
The text was updated successfully, but these errors were encountered: