blkio.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // +build linux
  2. package fs
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/opencontainers/runc/libcontainer/cgroups"
  11. "github.com/opencontainers/runc/libcontainer/configs"
  12. )
  13. type BlkioGroup struct {
  14. }
  15. func (s *BlkioGroup) Name() string {
  16. return "blkio"
  17. }
  18. func (s *BlkioGroup) Apply(d *cgroupData) error {
  19. _, err := d.join("blkio")
  20. if err != nil && !cgroups.IsNotFound(err) {
  21. return err
  22. }
  23. return nil
  24. }
  25. func (s *BlkioGroup) Set(path string, cgroup *configs.Cgroup) error {
  26. if cgroup.Resources.BlkioWeight != 0 {
  27. if err := writeFile(path, "blkio.weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioWeight), 10)); err != nil {
  28. return err
  29. }
  30. }
  31. if cgroup.Resources.BlkioLeafWeight != 0 {
  32. if err := writeFile(path, "blkio.leaf_weight", strconv.FormatUint(uint64(cgroup.Resources.BlkioLeafWeight), 10)); err != nil {
  33. return err
  34. }
  35. }
  36. for _, wd := range cgroup.Resources.BlkioWeightDevice {
  37. if err := writeFile(path, "blkio.weight_device", wd.WeightString()); err != nil {
  38. return err
  39. }
  40. if err := writeFile(path, "blkio.leaf_weight_device", wd.LeafWeightString()); err != nil {
  41. return err
  42. }
  43. }
  44. for _, td := range cgroup.Resources.BlkioThrottleReadBpsDevice {
  45. if err := writeFile(path, "blkio.throttle.read_bps_device", td.String()); err != nil {
  46. return err
  47. }
  48. }
  49. for _, td := range cgroup.Resources.BlkioThrottleWriteBpsDevice {
  50. if err := writeFile(path, "blkio.throttle.write_bps_device", td.String()); err != nil {
  51. return err
  52. }
  53. }
  54. for _, td := range cgroup.Resources.BlkioThrottleReadIOPSDevice {
  55. if err := writeFile(path, "blkio.throttle.read_iops_device", td.String()); err != nil {
  56. return err
  57. }
  58. }
  59. for _, td := range cgroup.Resources.BlkioThrottleWriteIOPSDevice {
  60. if err := writeFile(path, "blkio.throttle.write_iops_device", td.String()); err != nil {
  61. return err
  62. }
  63. }
  64. return nil
  65. }
  66. func (s *BlkioGroup) Remove(d *cgroupData) error {
  67. return removePath(d.path("blkio"))
  68. }
  69. /*
  70. examples:
  71. blkio.sectors
  72. 8:0 6792
  73. blkio.io_service_bytes
  74. 8:0 Read 1282048
  75. 8:0 Write 2195456
  76. 8:0 Sync 2195456
  77. 8:0 Async 1282048
  78. 8:0 Total 3477504
  79. Total 3477504
  80. blkio.io_serviced
  81. 8:0 Read 124
  82. 8:0 Write 104
  83. 8:0 Sync 104
  84. 8:0 Async 124
  85. 8:0 Total 228
  86. Total 228
  87. blkio.io_queued
  88. 8:0 Read 0
  89. 8:0 Write 0
  90. 8:0 Sync 0
  91. 8:0 Async 0
  92. 8:0 Total 0
  93. Total 0
  94. */
  95. func splitBlkioStatLine(r rune) bool {
  96. return r == ' ' || r == ':'
  97. }
  98. func getBlkioStat(path string) ([]cgroups.BlkioStatEntry, error) {
  99. var blkioStats []cgroups.BlkioStatEntry
  100. f, err := os.Open(path)
  101. if err != nil {
  102. if os.IsNotExist(err) {
  103. return blkioStats, nil
  104. }
  105. return nil, err
  106. }
  107. defer f.Close()
  108. sc := bufio.NewScanner(f)
  109. for sc.Scan() {
  110. // format: dev type amount
  111. fields := strings.FieldsFunc(sc.Text(), splitBlkioStatLine)
  112. if len(fields) < 3 {
  113. if len(fields) == 2 && fields[0] == "Total" {
  114. // skip total line
  115. continue
  116. } else {
  117. return nil, fmt.Errorf("Invalid line found while parsing %s: %s", path, sc.Text())
  118. }
  119. }
  120. v, err := strconv.ParseUint(fields[0], 10, 64)
  121. if err != nil {
  122. return nil, err
  123. }
  124. major := v
  125. v, err = strconv.ParseUint(fields[1], 10, 64)
  126. if err != nil {
  127. return nil, err
  128. }
  129. minor := v
  130. op := ""
  131. valueField := 2
  132. if len(fields) == 4 {
  133. op = fields[2]
  134. valueField = 3
  135. }
  136. v, err = strconv.ParseUint(fields[valueField], 10, 64)
  137. if err != nil {
  138. return nil, err
  139. }
  140. blkioStats = append(blkioStats, cgroups.BlkioStatEntry{Major: major, Minor: minor, Op: op, Value: v})
  141. }
  142. return blkioStats, nil
  143. }
  144. func (s *BlkioGroup) GetStats(path string, stats *cgroups.Stats) error {
  145. // Try to read CFQ stats available on all CFQ enabled kernels first
  146. if blkioStats, err := getBlkioStat(filepath.Join(path, "blkio.io_serviced_recursive")); err == nil && blkioStats != nil {
  147. return getCFQStats(path, stats)
  148. }
  149. return getStats(path, stats) // Use generic stats as fallback
  150. }
  151. func getCFQStats(path string, stats *cgroups.Stats) error {
  152. var blkioStats []cgroups.BlkioStatEntry
  153. var err error
  154. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.sectors_recursive")); err != nil {
  155. return err
  156. }
  157. stats.BlkioStats.SectorsRecursive = blkioStats
  158. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_service_bytes_recursive")); err != nil {
  159. return err
  160. }
  161. stats.BlkioStats.IoServiceBytesRecursive = blkioStats
  162. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_serviced_recursive")); err != nil {
  163. return err
  164. }
  165. stats.BlkioStats.IoServicedRecursive = blkioStats
  166. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_queued_recursive")); err != nil {
  167. return err
  168. }
  169. stats.BlkioStats.IoQueuedRecursive = blkioStats
  170. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_service_time_recursive")); err != nil {
  171. return err
  172. }
  173. stats.BlkioStats.IoServiceTimeRecursive = blkioStats
  174. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_wait_time_recursive")); err != nil {
  175. return err
  176. }
  177. stats.BlkioStats.IoWaitTimeRecursive = blkioStats
  178. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.io_merged_recursive")); err != nil {
  179. return err
  180. }
  181. stats.BlkioStats.IoMergedRecursive = blkioStats
  182. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.time_recursive")); err != nil {
  183. return err
  184. }
  185. stats.BlkioStats.IoTimeRecursive = blkioStats
  186. return nil
  187. }
  188. func getStats(path string, stats *cgroups.Stats) error {
  189. var blkioStats []cgroups.BlkioStatEntry
  190. var err error
  191. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.throttle.io_service_bytes")); err != nil {
  192. return err
  193. }
  194. stats.BlkioStats.IoServiceBytesRecursive = blkioStats
  195. if blkioStats, err = getBlkioStat(filepath.Join(path, "blkio.throttle.io_serviced")); err != nil {
  196. return err
  197. }
  198. stats.BlkioStats.IoServicedRecursive = blkioStats
  199. return nil
  200. }