limits.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //go:build windows
  2. package jobobject
  3. import (
  4. "errors"
  5. "fmt"
  6. "unsafe"
  7. "github.com/Microsoft/hcsshim/internal/winapi"
  8. "golang.org/x/sys/windows"
  9. )
  10. const (
  11. memoryLimitMax uint64 = 0xffffffffffffffff
  12. )
  13. func isFlagSet(flag, controlFlags uint32) bool {
  14. return (flag & controlFlags) == flag
  15. }
  16. // SetResourceLimits sets resource limits on the job object (cpu, memory, storage).
  17. func (job *JobObject) SetResourceLimits(limits *JobLimits) error {
  18. // Go through and check what limits were specified and apply them to the job.
  19. if limits.MemoryLimitInBytes != 0 {
  20. if err := job.SetMemoryLimit(limits.MemoryLimitInBytes); err != nil {
  21. return fmt.Errorf("failed to set job object memory limit: %w", err)
  22. }
  23. }
  24. if limits.CPULimit != 0 {
  25. if err := job.SetCPULimit(RateBased, limits.CPULimit); err != nil {
  26. return fmt.Errorf("failed to set job object cpu limit: %w", err)
  27. }
  28. } else if limits.CPUWeight != 0 {
  29. if err := job.SetCPULimit(WeightBased, limits.CPUWeight); err != nil {
  30. return fmt.Errorf("failed to set job object cpu limit: %w", err)
  31. }
  32. }
  33. if limits.MaxBandwidth != 0 || limits.MaxIOPS != 0 {
  34. if err := job.SetIOLimit(limits.MaxBandwidth, limits.MaxIOPS); err != nil {
  35. return fmt.Errorf("failed to set io limit on job object: %w", err)
  36. }
  37. }
  38. return nil
  39. }
  40. // SetTerminateOnLastHandleClose sets the job object flag that specifies that the job should terminate
  41. // all processes in the job on the last open handle being closed.
  42. func (job *JobObject) SetTerminateOnLastHandleClose() error {
  43. info, err := job.getExtendedInformation()
  44. if err != nil {
  45. return err
  46. }
  47. info.BasicLimitInformation.LimitFlags |= windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
  48. return job.setExtendedInformation(info)
  49. }
  50. // SetMemoryLimit sets the memory limit of the job object based on the given `memoryLimitInBytes`.
  51. func (job *JobObject) SetMemoryLimit(memoryLimitInBytes uint64) error {
  52. if memoryLimitInBytes >= memoryLimitMax {
  53. return errors.New("memory limit specified exceeds the max size")
  54. }
  55. info, err := job.getExtendedInformation()
  56. if err != nil {
  57. return err
  58. }
  59. info.JobMemoryLimit = uintptr(memoryLimitInBytes)
  60. info.BasicLimitInformation.LimitFlags |= windows.JOB_OBJECT_LIMIT_JOB_MEMORY
  61. return job.setExtendedInformation(info)
  62. }
  63. // GetMemoryLimit gets the memory limit in bytes of the job object.
  64. func (job *JobObject) GetMemoryLimit() (uint64, error) {
  65. info, err := job.getExtendedInformation()
  66. if err != nil {
  67. return 0, err
  68. }
  69. return uint64(info.JobMemoryLimit), nil
  70. }
  71. // SetCPULimit sets the CPU limit depending on the specified `CPURateControlType` to
  72. // `rateControlValue` for the job object.
  73. func (job *JobObject) SetCPULimit(rateControlType CPURateControlType, rateControlValue uint32) error {
  74. cpuInfo, err := job.getCPURateControlInformation()
  75. if err != nil {
  76. return err
  77. }
  78. switch rateControlType {
  79. case WeightBased:
  80. if rateControlValue < cpuWeightMin || rateControlValue > cpuWeightMax {
  81. return fmt.Errorf("processor weight value of `%d` is invalid", rateControlValue)
  82. }
  83. cpuInfo.ControlFlags |= winapi.JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | winapi.JOB_OBJECT_CPU_RATE_CONTROL_WEIGHT_BASED
  84. cpuInfo.Value = rateControlValue
  85. case RateBased:
  86. if rateControlValue < cpuLimitMin || rateControlValue > cpuLimitMax {
  87. return fmt.Errorf("processor rate of `%d` is invalid", rateControlValue)
  88. }
  89. cpuInfo.ControlFlags |= winapi.JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | winapi.JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP
  90. cpuInfo.Value = rateControlValue
  91. default:
  92. return errors.New("invalid job object cpu rate control type")
  93. }
  94. return job.setCPURateControlInfo(cpuInfo)
  95. }
  96. // GetCPULimit gets the cpu limits for the job object.
  97. // `rateControlType` is used to indicate what type of cpu limit to query for.
  98. func (job *JobObject) GetCPULimit(rateControlType CPURateControlType) (uint32, error) {
  99. info, err := job.getCPURateControlInformation()
  100. if err != nil {
  101. return 0, err
  102. }
  103. if !isFlagSet(winapi.JOB_OBJECT_CPU_RATE_CONTROL_ENABLE, info.ControlFlags) {
  104. return 0, errors.New("the job does not have cpu rate control enabled")
  105. }
  106. switch rateControlType {
  107. case WeightBased:
  108. if !isFlagSet(winapi.JOB_OBJECT_CPU_RATE_CONTROL_WEIGHT_BASED, info.ControlFlags) {
  109. return 0, errors.New("cannot get cpu weight for job object without cpu weight option set")
  110. }
  111. case RateBased:
  112. if !isFlagSet(winapi.JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP, info.ControlFlags) {
  113. return 0, errors.New("cannot get cpu rate hard cap for job object without cpu rate hard cap option set")
  114. }
  115. default:
  116. return 0, errors.New("invalid job object cpu rate control type")
  117. }
  118. return info.Value, nil
  119. }
  120. // SetCPUAffinity sets the processor affinity for the job object.
  121. // The affinity is passed in as a bitmask.
  122. func (job *JobObject) SetCPUAffinity(affinityBitMask uint64) error {
  123. info, err := job.getExtendedInformation()
  124. if err != nil {
  125. return err
  126. }
  127. info.BasicLimitInformation.LimitFlags |= uint32(windows.JOB_OBJECT_LIMIT_AFFINITY)
  128. info.BasicLimitInformation.Affinity = uintptr(affinityBitMask)
  129. return job.setExtendedInformation(info)
  130. }
  131. // GetCPUAffinity gets the processor affinity for the job object.
  132. // The returned affinity is a bitmask.
  133. func (job *JobObject) GetCPUAffinity() (uint64, error) {
  134. info, err := job.getExtendedInformation()
  135. if err != nil {
  136. return 0, err
  137. }
  138. return uint64(info.BasicLimitInformation.Affinity), nil
  139. }
  140. // SetIOLimit sets the IO limits specified on the job object.
  141. func (job *JobObject) SetIOLimit(maxBandwidth, maxIOPS int64) error {
  142. ioInfo, err := job.getIOLimit()
  143. if err != nil {
  144. return err
  145. }
  146. ioInfo.ControlFlags |= winapi.JOB_OBJECT_IO_RATE_CONTROL_ENABLE
  147. if maxBandwidth != 0 {
  148. ioInfo.MaxBandwidth = maxBandwidth
  149. }
  150. if maxIOPS != 0 {
  151. ioInfo.MaxIops = maxIOPS
  152. }
  153. return job.setIORateControlInfo(ioInfo)
  154. }
  155. // GetIOMaxBandwidthLimit gets the max bandwidth for the job object.
  156. func (job *JobObject) GetIOMaxBandwidthLimit() (int64, error) {
  157. info, err := job.getIOLimit()
  158. if err != nil {
  159. return 0, err
  160. }
  161. return info.MaxBandwidth, nil
  162. }
  163. // GetIOMaxIopsLimit gets the max iops for the job object.
  164. func (job *JobObject) GetIOMaxIopsLimit() (int64, error) {
  165. info, err := job.getIOLimit()
  166. if err != nil {
  167. return 0, err
  168. }
  169. return info.MaxIops, nil
  170. }
  171. // Helper function for getting a job object's extended information.
  172. func (job *JobObject) getExtendedInformation() (*windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION, error) {
  173. job.handleLock.RLock()
  174. defer job.handleLock.RUnlock()
  175. if job.handle == 0 {
  176. return nil, ErrAlreadyClosed
  177. }
  178. info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{}
  179. if err := winapi.QueryInformationJobObject(
  180. job.handle,
  181. windows.JobObjectExtendedLimitInformation,
  182. unsafe.Pointer(&info),
  183. uint32(unsafe.Sizeof(info)),
  184. nil,
  185. ); err != nil {
  186. return nil, fmt.Errorf("query %v returned error: %w", info, err)
  187. }
  188. return &info, nil
  189. }
  190. // Helper function for getting a job object's CPU rate control information.
  191. func (job *JobObject) getCPURateControlInformation() (*winapi.JOBOBJECT_CPU_RATE_CONTROL_INFORMATION, error) {
  192. job.handleLock.RLock()
  193. defer job.handleLock.RUnlock()
  194. if job.handle == 0 {
  195. return nil, ErrAlreadyClosed
  196. }
  197. info := winapi.JOBOBJECT_CPU_RATE_CONTROL_INFORMATION{}
  198. if err := winapi.QueryInformationJobObject(
  199. job.handle,
  200. windows.JobObjectCpuRateControlInformation,
  201. unsafe.Pointer(&info),
  202. uint32(unsafe.Sizeof(info)),
  203. nil,
  204. ); err != nil {
  205. return nil, fmt.Errorf("query %v returned error: %w", info, err)
  206. }
  207. return &info, nil
  208. }
  209. // Helper function for setting a job object's extended information.
  210. func (job *JobObject) setExtendedInformation(info *windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION) error {
  211. job.handleLock.RLock()
  212. defer job.handleLock.RUnlock()
  213. if job.handle == 0 {
  214. return ErrAlreadyClosed
  215. }
  216. if _, err := windows.SetInformationJobObject(
  217. job.handle,
  218. windows.JobObjectExtendedLimitInformation,
  219. uintptr(unsafe.Pointer(info)),
  220. uint32(unsafe.Sizeof(*info)),
  221. ); err != nil {
  222. return fmt.Errorf("failed to set Extended info %v on job object: %w", info, err)
  223. }
  224. return nil
  225. }
  226. // Helper function for querying job handle for IO limit information.
  227. func (job *JobObject) getIOLimit() (*winapi.JOBOBJECT_IO_RATE_CONTROL_INFORMATION, error) {
  228. job.handleLock.RLock()
  229. defer job.handleLock.RUnlock()
  230. if job.handle == 0 {
  231. return nil, ErrAlreadyClosed
  232. }
  233. ioInfo := &winapi.JOBOBJECT_IO_RATE_CONTROL_INFORMATION{}
  234. var blockCount uint32 = 1
  235. if _, err := winapi.QueryIoRateControlInformationJobObject(
  236. job.handle,
  237. nil,
  238. &ioInfo,
  239. &blockCount,
  240. ); err != nil {
  241. return nil, fmt.Errorf("query %v returned error: %w", ioInfo, err)
  242. }
  243. if !isFlagSet(winapi.JOB_OBJECT_IO_RATE_CONTROL_ENABLE, ioInfo.ControlFlags) {
  244. return nil, fmt.Errorf("query %v cannot get IO limits for job object without IO rate control option set", ioInfo)
  245. }
  246. return ioInfo, nil
  247. }
  248. // Helper function for setting a job object's IO rate control information.
  249. func (job *JobObject) setIORateControlInfo(ioInfo *winapi.JOBOBJECT_IO_RATE_CONTROL_INFORMATION) error {
  250. job.handleLock.RLock()
  251. defer job.handleLock.RUnlock()
  252. if job.handle == 0 {
  253. return ErrAlreadyClosed
  254. }
  255. if _, err := winapi.SetIoRateControlInformationJobObject(job.handle, ioInfo); err != nil {
  256. return fmt.Errorf("failed to set IO limit info %v on job object: %w", ioInfo, err)
  257. }
  258. return nil
  259. }
  260. // Helper function for setting a job object's CPU rate control information.
  261. func (job *JobObject) setCPURateControlInfo(cpuInfo *winapi.JOBOBJECT_CPU_RATE_CONTROL_INFORMATION) error {
  262. job.handleLock.RLock()
  263. defer job.handleLock.RUnlock()
  264. if job.handle == 0 {
  265. return ErrAlreadyClosed
  266. }
  267. if _, err := windows.SetInformationJobObject(
  268. job.handle,
  269. windows.JobObjectCpuRateControlInformation,
  270. uintptr(unsafe.Pointer(cpuInfo)),
  271. uint32(unsafe.Sizeof(cpuInfo)),
  272. ); err != nil {
  273. return fmt.Errorf("failed to set cpu limit info %v on job object: %w", cpuInfo, err)
  274. }
  275. return nil
  276. }