From d71608fc031df40112feb9ab038b94398e044a76 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Tue, 30 Apr 2024 13:49:36 -0700 Subject: [PATCH] platform/systrap: never enable fast paths if only one cpu is avaliable Fast paths here is when one process is spinning to wait for another one. It definitely can't work on one cpu. Before: BM_GetpidOpt 28491 ns 28586 ns 24138 After: BM_GetpidOpt 7578 ns 7600 ns 100000 PiperOrigin-RevId: 629518277 --- pkg/sentry/platform/systrap/metrics.go | 7 +++++++ pkg/sentry/platform/systrap/systrap.go | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/pkg/sentry/platform/systrap/metrics.go b/pkg/sentry/platform/systrap/metrics.go index 9a2f17f89..de6555ea5 100644 --- a/pkg/sentry/platform/systrap/metrics.go +++ b/pkg/sentry/platform/systrap/metrics.go @@ -60,6 +60,10 @@ const ( minNecessaryRecordings = 5 ) +// neverEnableFastPath is used for completely disabling the fast path. +// It is set once so doesn't need any synchronizations. +var neverEnableFastPath bool + // latencyRecorder is used to collect latency metrics. type latencyRecorder struct { stubBound latencyBuckets @@ -414,6 +418,9 @@ func (s *fastPathState) shouldDisableStubFP(stubMedian, sentryMedian cpuTicks) b // fastpath state machine described above. func sentryOffStubOff(s *fastPathState) { + if neverEnableFastPath { + return + } periodStubBoundMedian := latencies.stubBound.getMedian() s.stubBoundBaselineLatency.merge(&latencies.stubBound) latencies.stubBound.reset() diff --git a/pkg/sentry/platform/systrap/systrap.go b/pkg/sentry/platform/systrap/systrap.go index 6292eb192..7a5310f7a 100644 --- a/pkg/sentry/platform/systrap/systrap.go +++ b/pkg/sentry/platform/systrap/systrap.go @@ -51,6 +51,7 @@ package systrap import ( "fmt" "os" + "runtime" "sync" "golang.org/x/sys/unix" @@ -324,6 +325,9 @@ func New() (*Systrap, error) { } stubInitialized.Do(func() { + // Don't use sentry and stub fast paths if here is just one cpu. + neverEnableFastPath = min(runtime.NumCPU(), runtime.GOMAXPROCS(0)) == 1 + // Initialize the stub. stubInit()