Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1754,10 +1754,10 @@ def go_deps():
patches = [
"@com_github_cockroachdb_cockroach//build/patches:com_github_cockroachdb_crlib.patch",
],
sha256 = "539dd737ca1da53ee5c296ea0f3aad92f6d59f577e0d169dffb5a4ad706e1728",
strip_prefix = "github.com/cockroachdb/crlib@v0.0.0-20251001180057-2a49e1873587",
sha256 = "a9a4b8810d52a2d18dddbbc359d060f5db854e328b4d75f12b7c5081ac25b2a2",
strip_prefix = "github.com/cockroachdb/crlib@v0.0.0-20251024155502-a2e0a212ef05",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251001180057-2a49e1873587.zip",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251024155502-a2e0a212ef05.zip",
],
)
go_repository(
Expand Down
2 changes: 1 addition & 1 deletion build/bazelutil/distdir_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ DISTDIR_FILES = {
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/cmux/com_github_cockroachdb_cmux-v0.0.0-20250514152509-914d3bf9ec58.zip": "c1cf4cd99a1ad6a00f2ccd4188cbcf004cb0d56895670b2c171061ce564cd791",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/cockroach-go/v2/com_github_cockroachdb_cockroach_go_v2-v2.4.1.zip": "ba646db91152f3121a6812c7b74d12d8c0e126f7b4d3b927618b159692ceb424",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlfmt/com_github_cockroachdb_crlfmt-v0.0.0-20221214225007-b2fc5c302548.zip": "fedc01bdd6d964da0425d5eaac8efadc951e78e13f102292cc0774197f09ab63",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251001180057-2a49e1873587.zip": "539dd737ca1da53ee5c296ea0f3aad92f6d59f577e0d169dffb5a4ad706e1728",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251024155502-a2e0a212ef05.zip": "a9a4b8810d52a2d18dddbbc359d060f5db854e328b4d75f12b7c5081ac25b2a2",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/datadriven/com_github_cockroachdb_datadriven-v1.0.3-0.20251006155849-f84f9e519edd.zip": "a7ffcef0b264d9c28c36b2f9b737ff739542f472d7614938ae507e2da269f6c2",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/drpc/com_github_cockroachdb_drpc-v0.0.0-20250924114114-78d4e121902a.zip": "98b44a51f82873f93f77da80230212ab40f35044e8d38645cb1392ae03462f0b",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/errors/com_github_cockroachdb_errors-v1.12.1-0.20251010171200-64801262cd6f.zip": "4df66cc44791d4290071696abf179dc6df7b94b4cb5d29a20f39c6bf522c60ee",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ require (
github.com/cockroachdb/changefeedpb v0.0.0-20250818184245-aab9e65dff20
github.com/cockroachdb/cmux v0.0.0-20250514152509-914d3bf9ec58
github.com/cockroachdb/cockroach-go/v2 v2.4.1
github.com/cockroachdb/crlib v0.0.0-20251001180057-2a49e1873587
github.com/cockroachdb/crlib v0.0.0-20251024155502-a2e0a212ef05
github.com/cockroachdb/datadriven v1.0.3-0.20251006155849-f84f9e519edd
github.com/cockroachdb/errors v1.12.1-0.20251010171200-64801262cd6f
github.com/cockroachdb/gostdlib v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ github.com/cockroachdb/cockroach-go/v2 v2.4.1 h1:ACVT/zXsuK6waRPVYtDQpsM8pPA7IA/
github.com/cockroachdb/cockroach-go/v2 v2.4.1/go.mod h1:9U179XbCx4qFWtNhc7BiWLPfuyMVQ7qdAhfrwLz1vH0=
github.com/cockroachdb/crlfmt v0.0.0-20221214225007-b2fc5c302548 h1:i0bnjanlWAvM50wHMT7EFyxlt5HQusznWrkwl+HBIsU=
github.com/cockroachdb/crlfmt v0.0.0-20221214225007-b2fc5c302548/go.mod h1:qtkxNlt5i3rrdirfJE/bQeW/IeLajKexErv7jEIV+Uc=
github.com/cockroachdb/crlib v0.0.0-20251001180057-2a49e1873587 h1:qjG2TrBrPbGRVYp5obcAi8OSsuFJ8s1AElDImHLV9tY=
github.com/cockroachdb/crlib v0.0.0-20251001180057-2a49e1873587/go.mod h1:ae57yNis2F1FThSNdPdoXfiPOVi8G1TLreCBQYPOdqo=
github.com/cockroachdb/crlib v0.0.0-20251024155502-a2e0a212ef05 h1:7DV1UcpuwF78hKMC33w8ube+/wawG/57dT8Wo56+vUo=
github.com/cockroachdb/crlib v0.0.0-20251024155502-a2e0a212ef05/go.mod h1:ae57yNis2F1FThSNdPdoXfiPOVi8G1TLreCBQYPOdqo=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
github.com/cockroachdb/datadriven v1.0.3-0.20251006155849-f84f9e519edd h1:vpWCe7VvdQbQ/9wGtlH3i+Oj+9OggKci3lsASL1ydvg=
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/load_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func Test_loadEndpoint(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()

clock := timeutil.NewTestTimeSource()
clock := timeutil.NewManualTime(timeutil.Now())
rss := status.NewRuntimeStatSampler(ctx, clock)
metricSource := newTestMetricSource(clock)
le, err := newLoadEndpoint(rss, metricSource)
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/colflow/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ func TestVectorizedStatsCollector(t *testing.T) {
defer tu.cleanup(ctx)
for nBatches := 1; nBatches < 5; nBatches++ {
timeSource := timeutil.NewTestTimeSource()
mjInputWatch := timeutil.NewTestStopWatch(timeSource.Now)
mjInputWatch := timeutil.NewTestStopWatch(timeSource.NowMono)
leftSource := &timeAdvancingOperator{
OneInputHelper: colexecop.MakeOneInputHelper(makeFiniteChunksSourceWithBatchSize(tu.testAllocator, nBatches, coldata.BatchSize())),
timeSource: timeSource,
}
leftInput := newVectorizedStatsCollector(
leftSource, nil /* kvReader */, nil /* columnarizer */, execinfrapb.ComponentID{ID: 0},
timeutil.NewTestStopWatch(timeSource.Now), nil /* memMonitors */, nil, /* diskMonitors */
timeutil.NewTestStopWatch(timeSource.NowMono), nil /* memMonitors */, nil, /* diskMonitors */
nil, /* inputStatsCollectors */
)
rightSource := &timeAdvancingOperator{
Expand All @@ -108,7 +108,7 @@ func TestVectorizedStatsCollector(t *testing.T) {
}
rightInput := newVectorizedStatsCollector(
rightSource, nil /* kvReader */, nil /* columnarizer */, execinfrapb.ComponentID{ID: 1},
timeutil.NewTestStopWatch(timeSource.Now), nil /* memMonitors */, nil, /* diskMonitors */
timeutil.NewTestStopWatch(timeSource.NowMono), nil /* memMonitors */, nil, /* diskMonitors */
nil, /* inputStatsCollectors */
)
mergeJoiner := colexecjoin.NewMergeJoinOp(
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4516,8 +4516,8 @@ func (ex *connExecutor) serialize() serverpb.Session {

txnFingerprintIDs := ex.txnFingerprintIDCache.GetAllTxnFingerprintIDs()
sessionActiveTime := ex.totalActiveTimeStopWatch.Elapsed()
if startedAt, started := ex.totalActiveTimeStopWatch.LastStartedAt(); started {
sessionActiveTime = time.Duration(sessionActiveTime.Nanoseconds() + timeutil.Since(startedAt).Nanoseconds())
if elapsed, started := ex.totalActiveTimeStopWatch.CurrentElapsed(); started {
sessionActiveTime = time.Duration(sessionActiveTime.Nanoseconds() + elapsed.Nanoseconds())
}

return serverpb.Session{
Expand Down
1 change: 1 addition & 0 deletions pkg/util/timeutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
deps = [
"//pkg/util/grunning",
"//pkg/util/syncutil",
"@com_github_cockroachdb_crlib//crtime",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_errors//errorspb",
"@com_github_cockroachdb_redact//:redact",
Expand Down
29 changes: 16 additions & 13 deletions pkg/util/timeutil/stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/util/grunning"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/crlib/crtime"
)

// StopWatch is a utility stop watch that can be safely started and stopped
Expand All @@ -21,13 +22,13 @@ type StopWatch struct {
// stopped after that.
started bool
// startedAt is the time when the stop watch was started.
startedAt time.Time
startedAt crtime.Mono
// elapsed is the total time measured by the stop watch (i.e. between
// all Starts and Stops).
elapsed time.Duration
// timeSource is the source of time used by the stop watch. It is always
// timeutil.Now except for tests.
timeSource func() time.Time
// crtime.NowMono except for tests.
timeSource func() crtime.Mono
// cpuStopWatch is used to track CPU usage. It may be nil, in which case any
// operations on it are no-ops.
cpuStopWatch *cpuStopWatch
Expand All @@ -36,13 +37,13 @@ type StopWatch struct {

// NewStopWatch creates a new StopWatch.
func NewStopWatch() *StopWatch {
return newStopWatch(Now)
return newStopWatch(crtime.NowMono)
}

// NewStopWatchWithCPU creates a new StopWatch that will track CPU usage in
// addition to wall-clock time.
func NewStopWatchWithCPU() *StopWatch {
w := newStopWatch(Now)
w := NewStopWatch()
if grunning.Supported {
w.mu.cpuStopWatch = &cpuStopWatch{}
}
Expand All @@ -51,11 +52,11 @@ func NewStopWatchWithCPU() *StopWatch {

// NewTestStopWatch create a new StopWatch with the given time source. It is
// used for testing only.
func NewTestStopWatch(timeSource func() time.Time) *StopWatch {
func NewTestStopWatch(timeSource func() crtime.Mono) *StopWatch {
return newStopWatch(timeSource)
}

func newStopWatch(timeSource func() time.Time) *StopWatch {
func newStopWatch(timeSource func() crtime.Mono) *StopWatch {
w := &StopWatch{}
w.mu.timeSource = timeSource
return w
Expand All @@ -80,6 +81,8 @@ func (w *StopWatch) Stop() {
defer w.mu.Unlock()
if w.mu.started {
w.mu.started = false
// We don't use w.mu.startedAt.Elapsed() here so that testing time sources
// work correctly.
w.mu.elapsed += w.mu.timeSource().Sub(w.mu.startedAt)
w.mu.cpuStopWatch.stop()
}
Expand All @@ -101,29 +104,29 @@ func (w *StopWatch) ElapsedCPU() time.Duration {
return w.mu.cpuStopWatch.elapsed()
}

// LastStartedAt returns the time the stopwatch was last started, and a bool
// CurrentElapsed returns the duration the stopwatch has been started and a bool
// indicating if the stopwatch is currently started.
func (w *StopWatch) LastStartedAt() (startedAt time.Time, started bool) {
func (w *StopWatch) CurrentElapsed() (elapsed time.Duration, started bool) {
w.mu.Lock()
defer w.mu.Unlock()
return w.mu.startedAt, w.mu.started
return w.mu.startedAt.Elapsed(), w.mu.started
}

// TestTimeSource is a source of time that remembers when it was created (in
// terms of the real time) and returns the time based on its creation time and
// the number of "advances" it has had. It is used for testing only.
type TestTimeSource struct {
initTime time.Time
initTime crtime.Mono
counter int64
}

// NewTestTimeSource create a new TestTimeSource.
func NewTestTimeSource() *TestTimeSource {
return &TestTimeSource{initTime: Now()}
return &TestTimeSource{initTime: crtime.NowMono()}
}

// Now tells the current time according to t.
func (t *TestTimeSource) Now() time.Time {
func (t *TestTimeSource) NowMono() crtime.Mono {
return t.initTime.Add(time.Duration(t.counter))
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/util/timeutil/stopwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// the internal startedAt time.
func TestStopWatchStart(t *testing.T) {
timeSource := timeutil.NewTestTimeSource()
w := timeutil.NewTestStopWatch(timeSource.Now)
w := timeutil.NewTestStopWatch(timeSource.NowMono)

w.Start()
timeSource.Advance()
Expand All @@ -37,7 +37,7 @@ func TestStopWatchStart(t *testing.T) {
// state of the stop watch.
func TestStopWatchStop(t *testing.T) {
timeSource := timeutil.NewTestTimeSource()
w := timeutil.NewTestStopWatch(timeSource.Now)
w := timeutil.NewTestStopWatch(timeSource.NowMono)

w.Start()
timeSource.Advance()
Expand All @@ -55,7 +55,7 @@ func TestStopWatchStop(t *testing.T) {
// correctly.
func TestStopWatchElapsed(t *testing.T) {
timeSource := timeutil.NewTestTimeSource()
w := timeutil.NewTestStopWatch(timeSource.Now)
w := timeutil.NewTestStopWatch(timeSource.NowMono)
expected := time.Duration(10)

w.Start()
Expand Down