项目作者: brk0v

项目描述 :
Direct IO Golang Writer (Linux O_DIRECT)
高级语言: Go
项目地址: git://github.com/brk0v/directio.git
创建时间: 2019-02-20T22:22:50Z
项目社区:https://github.com/brk0v/directio

开源协议:Apache License 2.0

下载


Linux Direct IO Writer

Direct IO writer using O_DIRECT

Example:

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "net/http"
  6. "os"
  7. "syscall"
  8. "github.com/brk0v/directio"
  9. )
  10. func main() {
  11. // Open file with O_DIRECT
  12. flags := os.O_WRONLY | os.O_EXCL | os.O_CREATE | syscall.O_DIRECT
  13. f, err := os.OpenFile("/tmp/mini.iso", flags, 0644)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. defer f.Close()
  18. // Use directio writer
  19. dio, err := directio.New(f)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. defer dio.Flush()
  24. // Downloading iso image
  25. resp, err := http.Get("http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso")
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer resp.Body.Close()
  30. // Write the body to file
  31. _, err = io.Copy(dio, resp.Body)
  32. }

Check that dio bypass linux pagecache using vmtouch:

  1. $ vmtouch /tmp/mini.iso
  2. Files: 1
  3. Directories: 0
  4. Resident Pages: 1/16384 4K/64M 0.0061%
  5. Elapsed: 0.000356 seconds

or using my https://github.com/brk0v/cpager to check per cgroup pagecache usage:

  1. $ sudo ~/go/bin/cpager /tmp/mini.iso
  2. Files: 1
  3. Directories: 0
  4. Resident Pages: 1/16385 4K/64M 0.0%
  5. cgmem inode percent pages path
  6. - 100.0% 16384 not charged
  7. 2187 0.0% 1 /sys/fs/cgroup/memory/user.slice/user-1000.slice/session-3.scope