@@ -27,6 +27,7 @@ import (
2727 "github.com/cockroachdb/cockroach/pkg/util/log"
2828 "github.com/cockroachdb/cockroach/pkg/util/stop"
2929 "github.com/cockroachdb/cockroach/pkg/util/timeutil"
30+ "github.com/cockroachdb/cockroach/pkg/util/tracing"
3031 "github.com/cockroachdb/errors"
3132 "github.com/cockroachdb/pebble"
3233)
@@ -170,7 +171,7 @@ type Controller interface {
170171 ) (Handle , error )
171172 // AdmittedKVWorkDone is called after the admitted KV work is done
172173 // executing.
173- AdmittedKVWorkDone (Handle , * StoreWriteBytes )
174+ AdmittedKVWorkDone (context. Context , Handle , * StoreWriteBytes )
174175 // AdmitRangefeedRequest must be called before serving rangefeed requests.
175176 // If enabled, it returns a non-nil Pacer that's to be used within rangefeed
176177 // catchup scans (typically CPU-intensive and affecting scheduling
@@ -246,7 +247,9 @@ type Handle struct {
246247 raftAdmissionMeta * kvflowcontrolpb.RaftAdmissionMeta
247248
248249 callAdmittedWorkDoneOnKVAdmissionQ bool
249- cpuStart time.Duration
250+ // Used for measuring post-admission CPU, even for cases that were not
251+ // subject to admission control.
252+ cpuStart time.Duration
250253}
251254
252255// AnnotateCtx annotates the given context with request-scoped admission
@@ -293,7 +296,7 @@ func (n *controllerImpl) AdmitKVWork(
293296 ba * kvpb.BatchRequest ,
294297) (_ Handle , retErr error ) {
295298 if n .kvAdmissionQ == nil {
296- return Handle {}, nil
299+ return Handle {cpuStart : grunning . Time () }, nil
297300 }
298301 admissionInfo := workInfoForBatch (n .settings , requestTenantID , rangeTenantID , ba )
299302 ah := Handle {tenantID : admissionInfo .TenantID }
@@ -414,31 +417,32 @@ func (n *controllerImpl) AdmitKVWork(
414417 if err != nil {
415418 return Handle {}, err
416419 }
417- if callAdmittedWorkDoneOnKVAdmissionQ {
418- // We include the time to do other activities like intent resolution,
419- // since it is acceptable to charge them to the tenant.
420- ah .cpuStart = grunning .Time ()
421- }
422420 ah .callAdmittedWorkDoneOnKVAdmissionQ = callAdmittedWorkDoneOnKVAdmissionQ
423421 }
424422 }
423+ // We include the time to do other activities like intent resolution,
424+ // since it is acceptable to charge them to the tenant.
425+ ah .cpuStart = grunning .Time ()
425426 return ah , nil
426427}
427428
428429// AdmittedKVWorkDone implements the Controller interface.
429- func (n * controllerImpl ) AdmittedKVWorkDone (ah Handle , writeBytes * StoreWriteBytes ) {
430+ func (n * controllerImpl ) AdmittedKVWorkDone (ctx context. Context , ah Handle , writeBytes * StoreWriteBytes ) {
430431 n .elasticCPUGrantCoordinator .ElasticCPUWorkQueue .AdmittedWorkDone (ah .elasticCPUWorkHandle )
431- if ah .callAdmittedWorkDoneOnKVAdmissionQ {
432- cpuTime := grunning .Time () - ah .cpuStart
433- if cpuTime < 0 {
434- // We sometimes see cpuTime to be negative. We use 1ns here, arbitrarily.
435- // This issue is tracked by
436- // https://github.com/cockroachdb/cockroach/issues/126681.
437- if buildutil .CrdbTestBuild {
438- log .KvDistribution .Warningf (context .Background (), "grunning.Time() should be non-decreasing, cpuTime=%s" , cpuTime )
439- }
440- cpuTime = 1
432+ cpuTime := grunning .Time () - ah .cpuStart
433+ if cpuTime < 0 {
434+ // We sometimes see cpuTime to be negative. We use 1ns here, arbitrarily.
435+ // This issue is tracked by
436+ // https://github.com/cockroachdb/cockroach/issues/126681.
437+ if buildutil .CrdbTestBuild {
438+ log .KvDistribution .Warningf (context .Background (), "grunning.Time() should be non-decreasing, cpuTime=%s" , cpuTime )
441439 }
440+ cpuTime = 1
441+ }
442+ if span := tracing .SpanFromContext (ctx ); span != nil {
443+ span .RecordStructured (& kvpb.AdmittedWorkDoneEvent {CPUTime : uint64 (cpuTime .Nanoseconds ())})
444+ }
445+ if ah .callAdmittedWorkDoneOnKVAdmissionQ {
442446 n .kvAdmissionQ .AdmittedWorkDone (ah .tenantID , cpuTime )
443447 }
444448 if ah .storeAdmissionQ != nil {
0 commit comments