sysinfo.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package sysinfo
  2. import "github.com/docker/docker/pkg/parsers"
  3. // SysInfo stores information about which features a kernel supports.
  4. // TODO Windows: Factor out platform specific capabilities.
  5. type SysInfo struct {
  6. // Whether the kernel supports AppArmor or not
  7. AppArmor bool
  8. // Whether the kernel supports Seccomp or not
  9. Seccomp bool
  10. cgroupMemInfo
  11. cgroupCPUInfo
  12. cgroupBlkioInfo
  13. cgroupCpusetInfo
  14. cgroupPids
  15. // Whether IPv4 forwarding is supported or not, if this was disabled, networking will not work
  16. IPv4ForwardingDisabled bool
  17. // Whether bridge-nf-call-iptables is supported or not
  18. BridgeNFCallIPTablesDisabled bool
  19. // Whether bridge-nf-call-ip6tables is supported or not
  20. BridgeNFCallIP6TablesDisabled bool
  21. // Whether the cgroup has the mountpoint of "devices" or not
  22. CgroupDevicesEnabled bool
  23. }
  24. type cgroupMemInfo struct {
  25. // Whether memory limit is supported or not
  26. MemoryLimit bool
  27. // Whether swap limit is supported or not
  28. SwapLimit bool
  29. // Whether soft limit is supported or not
  30. MemoryReservation bool
  31. // Whether OOM killer disable is supported or not
  32. OomKillDisable bool
  33. // Whether memory swappiness is supported or not
  34. MemorySwappiness bool
  35. // Whether kernel memory limit is supported or not
  36. KernelMemory bool
  37. }
  38. type cgroupCPUInfo struct {
  39. // Whether CPU shares is supported or not
  40. CPUShares bool
  41. // Whether CPU CFS(Completely Fair Scheduler) period is supported or not
  42. CPUCfsPeriod bool
  43. // Whether CPU CFS(Completely Fair Scheduler) quota is supported or not
  44. CPUCfsQuota bool
  45. // Whether CPU real-time period is supported or not
  46. CPURealtimePeriod bool
  47. // Whether CPU real-time runtime is supported or not
  48. CPURealtimeRuntime bool
  49. }
  50. type cgroupBlkioInfo struct {
  51. // Whether Block IO weight is supported or not
  52. BlkioWeight bool
  53. // Whether Block IO weight_device is supported or not
  54. BlkioWeightDevice bool
  55. // Whether Block IO read limit in bytes per second is supported or not
  56. BlkioReadBpsDevice bool
  57. // Whether Block IO write limit in bytes per second is supported or not
  58. BlkioWriteBpsDevice bool
  59. // Whether Block IO read limit in IO per second is supported or not
  60. BlkioReadIOpsDevice bool
  61. // Whether Block IO write limit in IO per second is supported or not
  62. BlkioWriteIOpsDevice bool
  63. }
  64. type cgroupCpusetInfo struct {
  65. // Whether Cpuset is supported or not
  66. Cpuset bool
  67. // Available Cpuset's cpus
  68. Cpus string
  69. // Available Cpuset's memory nodes
  70. Mems string
  71. }
  72. type cgroupPids struct {
  73. // Whether Pids Limit is supported or not
  74. PidsLimit bool
  75. }
  76. // IsCpusetCpusAvailable returns `true` if the provided string set is contained
  77. // in cgroup's cpuset.cpus set, `false` otherwise.
  78. // If error is not nil a parsing error occurred.
  79. func (c cgroupCpusetInfo) IsCpusetCpusAvailable(provided string) (bool, error) {
  80. return isCpusetListAvailable(provided, c.Cpus)
  81. }
  82. // IsCpusetMemsAvailable returns `true` if the provided string set is contained
  83. // in cgroup's cpuset.mems set, `false` otherwise.
  84. // If error is not nil a parsing error occurred.
  85. func (c cgroupCpusetInfo) IsCpusetMemsAvailable(provided string) (bool, error) {
  86. return isCpusetListAvailable(provided, c.Mems)
  87. }
  88. func isCpusetListAvailable(provided, available string) (bool, error) {
  89. parsedProvided, err := parsers.ParseUintList(provided)
  90. if err != nil {
  91. return false, err
  92. }
  93. parsedAvailable, err := parsers.ParseUintList(available)
  94. if err != nil {
  95. return false, err
  96. }
  97. for k := range parsedProvided {
  98. if !parsedAvailable[k] {
  99. return false, nil
  100. }
  101. }
  102. return true, nil
  103. }
  104. // Returns bit count of 1, used by NumCPU
  105. func popcnt(x uint64) (n byte) {
  106. x -= (x >> 1) & 0x5555555555555555
  107. x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
  108. x += x >> 4
  109. x &= 0x0f0f0f0f0f0f0f0f
  110. x *= 0x0101010101010101
  111. return byte(x >> 56)
  112. }