Files
lyphyserandHanzo Dev 26b6f703f1 systrap: express spin/deep-sleep timeouts as durations, fix arm64 idle CPU
The deepSleepTimeout/handshakeTimeout thresholds bound how long stub
threads and the dispatcher spin before deep-sleeping. They were hardcoded
in cputicks() units tuned for the amd64 TSC (~2GHz), e.g. 40000 ticks for
"20µs". But cputicks() advances at a platform-specific rate unrelated to
the CPU clock: on arm64 it reads CNTVCT_EL0, which runs at CNTFRQ_EL0 --
anywhere from tens of MHz up to ~1GHz depending on the core -- so a fixed
tick count maps to a different wall-clock duration there. On a tens-of-MHz
part, 40000 ticks is ~2ms (~100x the intended 20µs), so idle stub threads
and the dispatcher spin far too long before sleeping, burning CPU on
otherwise idle containers (issue #13361).

Neither RDTSC nor CNTVCT_EL0 is an instruction/cycle counter; both are
constant-rate time references at frequencies that differ per platform, so
the timeouts are fundamentally durations. Express them once as nanosecond
durations and convert to cputicks() units via a new per-arch
cputicksFreq():
  - arm64 reads CNTFRQ_EL0 (exact, no calibration).
  - amd64 returns the nominal 2GHz the constants were tuned against, so
    amd64 behavior is unchanged.

initSleepTimeouts() performs the conversion once in New(), before
stubInit() copies deepSleepTimeout into the stub.

Co-authored-by: Hanzo Dev <[email protected]>
2026-06-16 19:38:17 +00:00

57 lines
1.9 KiB
Go

// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package systrap
import (
"gvisor.dev/gvisor/pkg/sentry/arch"
)
func stackPointer(r *arch.Registers) uintptr {
return uintptr(r.Rsp)
}
// nominalTSCFreq is the assumed frequency in Hz of the x86 TSC read by
// cputicks(). The TSC is invariant (it ticks at a fixed reference rate
// independent of the core clock), but its exact frequency is not cheaply
// discoverable, so cputicksFreq() assumes this nominal ~2GHz.
const nominalTSCFreq = 2 * 1000 * 1000 * 1000
// cputicksFreq returns the frequency in Hz of the counter read by cputicks().
func cputicksFreq() uint64 {
return nominalTSCFreq
}
// x86 use the fs_base register to store the TLS pointer which can be
// get/set in "func (t *thread) get/setRegs(regs *arch.Registers)".
// So both of the get/setTLS() operations are noop here.
// getTLS gets the thread local storage register.
func (t *thread) getTLS(tls *uint64) error {
return nil
}
// setTLS sets the thread local storage register.
func (t *thread) setTLS(tls *uint64) error {
return nil
}
// configureSystrapAddressSpace is a no-op on amd64.
//
// On ARM64, this function overrides the default 48-bit address space
// parameters to support 39-bit and 52-bit VA widths. On amd64, the
// address space layout is fixed and does not require dynamic
// configuration, so this is intentionally empty.
func configureSystrapAddressSpace() {}