proc_io.go 1.1 KB

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