Skip to content

Commit 31614dd

Browse files
craig[bot]dt
andcommitted
Merge #159205
159205: util/admission: add runtime.Yield to pacer.Pace() calls r=dt a=dt Including for otherwise disabled pacers, such as in features where elastic CPU limiting has been kept behind a setting. Release note (performance change): Various background tasks and jobs now more actively yield to foreground work when it is waiting to run. Epic: CRDB-37540. Co-authored-by: David Taylor <[email protected]>
2 parents bbc06e5 + 9ba8a92 commit 31614dd

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

pkg/kv/bulk/cpu_pacer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ var cpuPacerRequestDuration = settings.RegisterDurationSetting(
2525
50*time.Millisecond,
2626
)
2727

28+
var yieldIfNoPacer = settings.RegisterBoolSetting(
29+
settings.ApplicationLevel,
30+
"bulkio.elastic_cpu_control.always_yield.enabled",
31+
"if true, yield the CPU as needed even when time-based elastic pacing is not enabled",
32+
true,
33+
)
34+
2835
// NewCPUPacer creates a new AC pacer for SST batcher. It may return an empty
2936
// Pacer which noops if pacing is disabled or its arguments are nil.
3037
func NewCPUPacer(ctx context.Context, db *kv.DB, setting *settings.BoolSetting) *admission.Pacer {
3138
if db == nil || db.AdmissionPacerFactory == nil || !setting.Get(db.SettingsValues()) {
3239
log.Dev.Infof(ctx, "admission control is not configured to pace bulk ingestion")
40+
41+
if db != nil && yieldIfNoPacer.Get(db.SettingsValues()) {
42+
// Return a Pacer that just yields.
43+
return &admission.Pacer{Yield: true}
44+
}
3345
return nil
3446
}
3547
tenantID, ok := roachpb.ClientTenantFromContext(ctx)

pkg/util/admission/elastic_cpu_grant_coordinator.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package admission
88
import (
99
"time"
1010

11+
"github.com/cockroachdb/cockroach/pkg/settings"
1112
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
1213
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
1314
"github.com/cockroachdb/cockroach/pkg/util/log"
@@ -83,14 +84,22 @@ func (e *ElasticCPUGrantCoordinator) tryGrant() {
8384
e.elasticCPUGranter.tryGrant()
8485
}
8586

87+
var yieldInPacer = settings.RegisterBoolSetting(
88+
settings.ApplicationLevel,
89+
"admission.elastic_cpu.yield_in_pacer.enabled",
90+
"when true, time-based elastic CPU pacing additionally yields CPU as-needed according to the scheduler",
91+
true,
92+
)
93+
8694
// NewPacer implements the PacerMaker interface.
8795
func (e *ElasticCPUGrantCoordinator) NewPacer(unit time.Duration, wi WorkInfo) *Pacer {
8896
if e == nil {
8997
return nil
9098
}
9199
return &Pacer{
92-
unit: unit,
93-
wi: wi,
94-
wq: e.ElasticCPUWorkQueue,
100+
unit: unit,
101+
wi: wi,
102+
wq: e.ElasticCPUWorkQueue,
103+
Yield: yieldInPacer.Get(&e.ElasticCPUWorkQueue.settings.SV),
95104
}
96105
}

pkg/util/admission/pacer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package admission
77

88
import (
99
"context"
10+
"runtime"
1011
"time"
1112
)
1213

@@ -21,6 +22,13 @@ type Pacer struct {
2122
wq *ElasticCPUWorkQueue
2223

2324
cur *ElasticCPUWorkHandle
25+
26+
// Yield, if true, indicates that the Pacer should runtime.Yield() in each
27+
// Pace() call, even if the call is otherwise a no-op due to wq being nil i.e.
28+
// when time-based pacing is not enabled. Eventually this might just become
29+
// the default behavior for nil *Pacer, but the bool allows it to be opt-in
30+
// initially.
31+
Yield bool
2432
}
2533

2634
// Pace will block as needed to pace work that calls it. It is
@@ -39,6 +47,14 @@ func (p *Pacer) Pace(ctx context.Context) (readmitted bool, err error) {
3947
return false, nil
4048
}
4149

50+
if p.Yield {
51+
runtime.Yield()
52+
}
53+
54+
if p.wq == nil {
55+
return false, nil
56+
}
57+
4258
if overLimit, _ := p.cur.OverLimit(); overLimit {
4359
p.wq.AdmittedWorkDone(p.cur)
4460
p.cur = nil

0 commit comments

Comments
 (0)