proc_io.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package procfs
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. )
  6. // ProcIO models the content of /proc/<pid>/io.
  7. type ProcIO struct {
  8. // Chars read.
  9. RChar uint64
  10. // Chars written.
  11. WChar uint64
  12. // Read syscalls.
  13. SyscR uint64
  14. // Write syscalls.
  15. SyscW uint64
  16. // Bytes read.
  17. ReadBytes uint64
  18. // Bytes written.
  19. WriteBytes uint64
  20. // Bytes written, but taking into account truncation. See
  21. // Documentation/filesystems/proc.txt in the kernel sources for
  22. // detailed explanation.
  23. CancelledWriteBytes int64
  24. }
  25. // NewIO creates a new ProcIO instance from a given Proc instance.
  26. func (p Proc) NewIO() (ProcIO, error) {
  27. pio := ProcIO{}
  28. f, err := p.open("io")
  29. if err != nil {
  30. return pio, err
  31. }
  32. defer f.Close()
  33. data, err := ioutil.ReadAll(f)
  34. if err != nil {
  35. return pio, err
  36. }
  37. ioFormat := "rchar: %d\nwchar: %d\nsyscr: %d\nsyscw: %d\n" +
  38. "read_bytes: %d\nwrite_bytes: %d\n" +
  39. "cancelled_write_bytes: %d\n"
  40. _, err = fmt.Sscanf(string(data), ioFormat, &pio.RChar, &pio.WChar, &pio.SyscR,
  41. &pio.SyscW, &pio.ReadBytes, &pio.WriteBytes, &pio.CancelledWriteBytes)
  42. if err != nil {
  43. return pio, err
  44. }
  45. return pio, nil
  46. }