disk_linux.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains the Linux implementation of process disk IO counter retrieval.
  17. package metrics
  18. import (
  19. "bufio"
  20. "fmt"
  21. "io"
  22. "os"
  23. "strconv"
  24. "strings"
  25. )
  26. // ReadDiskStats retrieves the disk IO stats belonging to the current process.
  27. func ReadDiskStats(stats *DiskStats) error {
  28. // Open the process disk IO counter file
  29. inf, err := os.Open(fmt.Sprintf("/proc/%d/io", os.Getpid()))
  30. if err != nil {
  31. return err
  32. }
  33. defer inf.Close()
  34. in := bufio.NewReader(inf)
  35. // Iterate over the IO counter, and extract what we need
  36. for {
  37. // Read the next line and split to key and value
  38. line, err := in.ReadString('\n')
  39. if err != nil {
  40. if err == io.EOF {
  41. return nil
  42. }
  43. return err
  44. }
  45. parts := strings.Split(line, ":")
  46. if len(parts) != 2 {
  47. continue
  48. }
  49. key := strings.TrimSpace(parts[0])
  50. value, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64)
  51. if err != nil {
  52. return err
  53. }
  54. // Update the counter based on the key
  55. switch key {
  56. case "syscr":
  57. stats.ReadCount = value
  58. case "syscw":
  59. stats.WriteCount = value
  60. case "rchar":
  61. stats.ReadBytes = value
  62. case "wchar":
  63. stats.WriteBytes = value
  64. }
  65. }
  66. }