helpers_windows.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // +build windows
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  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. */
  14. package kuberuntime
  15. import (
  16. "github.com/docker/docker/pkg/sysinfo"
  17. )
  18. const (
  19. // Taken from https://docs.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/resource-controls
  20. minSharesProcess = 5000
  21. minSharesHyperV = 10
  22. maxShares = 10000
  23. milliCPUToCPU = 1000
  24. )
  25. // milliCPUToShares converts milliCPU to CPU shares
  26. func milliCPUToShares(milliCPU int64, hyperv bool) int64 {
  27. var minShares int64 = minSharesProcess
  28. if hyperv {
  29. minShares = minSharesHyperV
  30. }
  31. if milliCPU == 0 {
  32. // Return here to really match kernel default for zero milliCPU.
  33. return minShares
  34. }
  35. // Conceptually (milliCPU / milliCPUToCPU) * sharesPerCPU, but factored to improve rounding.
  36. totalCPU := sysinfo.NumCPU()
  37. shares := (milliCPU * (maxShares - minShares)) / int64(totalCPU) / milliCPUToCPU
  38. if shares < minShares {
  39. return minShares
  40. }
  41. if shares > maxShares {
  42. return maxShares
  43. }
  44. return shares
  45. }