aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/float.go
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2025-05-05 13:44:26 -0400
committerMichael Pratt <mpratt@google.com>2025-05-21 10:21:55 -0700
commite6dacf91ffb0a356aa692ab5c46411e2eef913f3 (patch)
treefc337b1d3fe594503468a6af639ed6459442ee2a /src/runtime/float.go
parentf12c66fbed546645389cf184b0e2ffd6ad9f78ec (diff)
downloadgo-e6dacf91ffb0a356aa692ab5c46411e2eef913f3.tar.xz
runtime: use cgroup CPU limit to set GOMAXPROCS
This CL adds two related features enabled by default via compatibility GODEBUGs containermaxprocs and updatemaxprocs. On Linux, containermaxprocs makes the Go runtime consider cgroup CPU bandwidth limits (quota/period) when setting GOMAXPROCS. If the cgroup limit is lower than the number of logical CPUs available, then the cgroup limit takes precedence. On all OSes, updatemaxprocs makes the Go runtime periodically recalculate the default GOMAXPROCS value and update GOMAXPROCS if it has changed. If GOMAXPROCS is set manually, this update does not occur. This is intended primarily to detect changes to cgroup limits, but it applies on all OSes because the CPU affinity mask can change as well. The runtime only considers the limit in the leaf cgroup (the one that actually contains the process), caching the CPU limit file descriptor(s), which are periodically reread for updates. This is a small departure from the original proposed design. It will not consider limits of parent cgroups (which may be lower than the leaf), and it will not detection cgroup migration after process start. We can consider changing this in the future, but the simpler approach is less invasive; less risk to packages that have some awareness of runtime internals. e.g., if the runtime periodically opens new files during execution, file descriptor leak detection is difficult to implement in a stable way. For #73193. Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest Change-Id: I6a6a636c631c1ae577fb8254960377ba91c5dc98 Reviewed-on: https://go-review.googlesource.com/c/go/+/670497 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime/float.go')
-rw-r--r--src/runtime/float.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/runtime/float.go b/src/runtime/float.go
index 9f281c4045..d8573c103b 100644
--- a/src/runtime/float.go
+++ b/src/runtime/float.go
@@ -6,6 +6,12 @@ package runtime
import "unsafe"
+const (
+ float64Mask = 0x7FF
+ float64Shift = 64 - 11 - 1
+ float64Bias = 1023
+)
+
var inf = float64frombits(0x7FF0000000000000)
// isNaN reports whether f is an IEEE 754 “not-a-number” value.
@@ -52,3 +58,76 @@ func float64bits(f float64) uint64 {
func float64frombits(b uint64) float64 {
return *(*float64)(unsafe.Pointer(&b))
}
+
+// floor returns the greatest integer value less than or equal to x.
+//
+// Special cases are:
+//
+// floor(±0) = ±0
+// floor(±Inf) = ±Inf
+// floor(NaN) = NaN
+//
+// N.B. Portable floor copied from math. math also has optimized arch-specific
+// implementations.
+func floor(x float64) float64 {
+ if x == 0 || isNaN(x) || isInf(x) {
+ return x
+ }
+ if x < 0 {
+ d, fract := modf(-x)
+ if fract != 0.0 {
+ d = d + 1
+ }
+ return -d
+ }
+ d, _ := modf(x)
+ return d
+}
+
+// ceil returns the least integer value greater than or equal to x.
+//
+// Special cases are:
+//
+// Ceil(±0) = ±0
+// Ceil(±Inf) = ±Inf
+// Ceil(NaN) = NaN
+//
+// N.B. Portable ceil copied from math. math also has optimized arch-specific
+// implementations.
+func ceil(x float64) float64 {
+ return -floor(-x)
+}
+
+// modf returns integer and fractional floating-point numbers
+// that sum to f. Both values have the same sign as f.
+//
+// Special cases are:
+//
+// Modf(±Inf) = ±Inf, NaN
+// Modf(NaN) = NaN, NaN
+//
+// N.B. Portable modf copied from math. math also has optimized arch-specific
+// implementations.
+func modf(f float64) (int float64, frac float64) {
+ if f < 1 {
+ switch {
+ case f < 0:
+ int, frac = modf(-f)
+ return -int, -frac
+ case f == 0:
+ return f, f // Return -0, -0 when f == -0
+ }
+ return 0, f
+ }
+
+ x := float64bits(f)
+ e := uint(x>>float64Shift)&float64Mask - float64Bias
+
+ // Keep the top 12+e bits, the integer part; clear the rest.
+ if e < 64-12 {
+ x &^= 1<<(64-12-e) - 1
+ }
+ int = float64frombits(x)
+ frac = f - int
+ return
+}