@@ -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
@@ -292,11 +295,19 @@ func (n *controllerImpl) AdmitKVWork(
292295 rangeTenantID roachpb.TenantID ,
293296 ba * kvpb.BatchRequest ,
294297) (_ Handle , retErr error ) {
298+ ah := Handle {}
299+ defer func () {
300+ if retErr == nil {
301+ // We include the time to do other activities like intent resolution,
302+ // since it is acceptable to charge them to the tenant.
303+ ah .cpuStart = grunning .Time ()
304+ }
305+ }()
295306 if n .kvAdmissionQ == nil {
296- return Handle {} , nil
307+ return ah , nil
297308 }
298309 admissionInfo := workInfoForBatch (n .settings , requestTenantID , rangeTenantID , ba )
299- ah := Handle { tenantID : admissionInfo .TenantID }
310+ ah . tenantID = admissionInfo .TenantID
300311 admissionEnabled := true
301312 // Don't subject HeartbeatTxnRequest to the storeAdmissionQ. Even though
302313 // it would bypass admission, it would consume a slot. When writes are
@@ -414,31 +425,31 @@ func (n *controllerImpl) AdmitKVWork(
414425 if err != nil {
415426 return Handle {}, err
416427 }
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- }
422428 ah .callAdmittedWorkDoneOnKVAdmissionQ = callAdmittedWorkDoneOnKVAdmissionQ
423429 }
424430 }
425431 return ah , nil
426432}
427433
428434// AdmittedKVWorkDone implements the Controller interface.
429- func (n * controllerImpl ) AdmittedKVWorkDone (ah Handle , writeBytes * StoreWriteBytes ) {
435+ func (n * controllerImpl ) AdmittedKVWorkDone (
436+ ctx context.Context , ah Handle , writeBytes * StoreWriteBytes ,
437+ ) {
430438 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
439+ cpuTime := grunning .Time () - ah .cpuStart
440+ if cpuTime < 0 {
441+ // We sometimes see cpuTime to be negative. We use 1ns here, arbitrarily.
442+ // This issue is tracked by
443+ // https://github.com/cockroachdb/cockroach/issues/126681.
444+ if buildutil .CrdbTestBuild {
445+ log .KvDistribution .Warningf (context .Background (), "grunning.Time() should be non-decreasing, cpuTime=%s" , cpuTime )
441446 }
447+ cpuTime = 1
448+ }
449+ if span := tracing .SpanFromContext (ctx ); span != nil {
450+ span .RecordStructured (& kvpb.AdmittedWorkDoneEvent {CPUTime : uint64 (cpuTime .Nanoseconds ())})
451+ }
452+ if ah .callAdmittedWorkDoneOnKVAdmissionQ {
442453 n .kvAdmissionQ .AdmittedWorkDone (ah .tenantID , cpuTime )
443454 }
444455 if ah .storeAdmissionQ != nil {
0 commit comments