proc_io.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2018 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. )
  19. // ProcIO models the content of /proc/<pid>/io.
  20. type ProcIO struct {
  21. // Chars read.
  22. RChar uint64
  23. // Chars written.
  24. WChar uint64
  25. // Read syscalls.
  26. SyscR uint64
  27. // Write syscalls.
  28. SyscW uint64
  29. // Bytes read.
  30. ReadBytes uint64
  31. // Bytes written.
  32. WriteBytes uint64
  33. // Bytes written, but taking into account truncation. See
  34. // Documentation/filesystems/proc.txt in the kernel sources for
  35. // detailed explanation.
  36. CancelledWriteBytes int64
  37. }
  38. // IO creates a new ProcIO instance from a given Proc instance.
  39. func (p Proc) IO() (ProcIO, error) {
  40. pio := ProcIO{}
  41. f, err := os.Open(p.path("io"))
  42. if err != nil {
  43. return pio, err
  44. }
  45. defer f.Close()
  46. data, err := ioutil.ReadAll(f)
  47. if err != nil {
  48. return pio, err
  49. }
  50. ioFormat := "rchar: %d\nwchar: %d\nsyscr: %d\nsyscw: %d\n" +
  51. "read_bytes: %d\nwrite_bytes: %d\n" +
  52. "cancelled_write_bytes: %d\n"
  53. _, err = fmt.Sscanf(string(data), ioFormat, &pio.RChar, &pio.WChar, &pio.SyscR,
  54. &pio.SyscW, &pio.ReadBytes, &pio.WriteBytes, &pio.CancelledWriteBytes)
  55. return pio, err
  56. }