Skip to content

Commit d80587b

Browse files
craig[bot]stevendanna
andcommitted
Merge #156922
156922: stopwatch: use crtime.NowMono r=stevendanna a=stevendanna This was pretty easy to convert to NowMono since the only caller who was observing the internal startedAt field was just using it to get the elapsed time anyway. Epic: none Release note: None Co-authored-by: Steven Danna <[email protected]>
2 parents f69b856 + 7d0c3ac commit d80587b

File tree

10 files changed

+33
-29
lines changed

10 files changed

+33
-29
lines changed

DEPS.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,10 +1754,10 @@ def go_deps():
17541754
patches = [
17551755
"@com_github_cockroachdb_cockroach//build/patches:com_github_cockroachdb_crlib.patch",
17561756
],
1757-
sha256 = "539dd737ca1da53ee5c296ea0f3aad92f6d59f577e0d169dffb5a4ad706e1728",
1758-
strip_prefix = "github.com/cockroachdb/[email protected]20251001180057-2a49e1873587",
1757+
sha256 = "a9a4b8810d52a2d18dddbbc359d060f5db854e328b4d75f12b7c5081ac25b2a2",
1758+
strip_prefix = "github.com/cockroachdb/[email protected]20251024155502-a2e0a212ef05",
17591759
urls = [
1760-
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251001180057-2a49e1873587.zip",
1760+
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251024155502-a2e0a212ef05.zip",
17611761
],
17621762
)
17631763
go_repository(

build/bazelutil/distdir_files.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ DISTDIR_FILES = {
342342
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/cmux/com_github_cockroachdb_cmux-v0.0.0-20250514152509-914d3bf9ec58.zip": "c1cf4cd99a1ad6a00f2ccd4188cbcf004cb0d56895670b2c171061ce564cd791",
343343
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/cockroach-go/v2/com_github_cockroachdb_cockroach_go_v2-v2.4.1.zip": "ba646db91152f3121a6812c7b74d12d8c0e126f7b4d3b927618b159692ceb424",
344344
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlfmt/com_github_cockroachdb_crlfmt-v0.0.0-20221214225007-b2fc5c302548.zip": "fedc01bdd6d964da0425d5eaac8efadc951e78e13f102292cc0774197f09ab63",
345-
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251001180057-2a49e1873587.zip": "539dd737ca1da53ee5c296ea0f3aad92f6d59f577e0d169dffb5a4ad706e1728",
345+
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/crlib/com_github_cockroachdb_crlib-v0.0.0-20251024155502-a2e0a212ef05.zip": "a9a4b8810d52a2d18dddbbc359d060f5db854e328b4d75f12b7c5081ac25b2a2",
346346
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/datadriven/com_github_cockroachdb_datadriven-v1.0.3-0.20251006155849-f84f9e519edd.zip": "a7ffcef0b264d9c28c36b2f9b737ff739542f472d7614938ae507e2da269f6c2",
347347
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/drpc/com_github_cockroachdb_drpc-v0.0.0-20250924114114-78d4e121902a.zip": "98b44a51f82873f93f77da80230212ab40f35044e8d38645cb1392ae03462f0b",
348348
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/errors/com_github_cockroachdb_errors-v1.12.1-0.20251010171200-64801262cd6f.zip": "4df66cc44791d4290071696abf179dc6df7b94b4cb5d29a20f39c6bf522c60ee",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ require (
132132
github.com/cockroachdb/changefeedpb v0.0.0-20250818184245-aab9e65dff20
133133
github.com/cockroachdb/cmux v0.0.0-20250514152509-914d3bf9ec58
134134
github.com/cockroachdb/cockroach-go/v2 v2.4.1
135-
github.com/cockroachdb/crlib v0.0.0-20251001180057-2a49e1873587
135+
github.com/cockroachdb/crlib v0.0.0-20251024155502-a2e0a212ef05
136136
github.com/cockroachdb/datadriven v1.0.3-0.20251006155849-f84f9e519edd
137137
github.com/cockroachdb/errors v1.12.1-0.20251010171200-64801262cd6f
138138
github.com/cockroachdb/gostdlib v1.19.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ github.com/cockroachdb/cockroach-go/v2 v2.4.1 h1:ACVT/zXsuK6waRPVYtDQpsM8pPA7IA/
550550
github.com/cockroachdb/cockroach-go/v2 v2.4.1/go.mod h1:9U179XbCx4qFWtNhc7BiWLPfuyMVQ7qdAhfrwLz1vH0=
551551
github.com/cockroachdb/crlfmt v0.0.0-20221214225007-b2fc5c302548 h1:i0bnjanlWAvM50wHMT7EFyxlt5HQusznWrkwl+HBIsU=
552552
github.com/cockroachdb/crlfmt v0.0.0-20221214225007-b2fc5c302548/go.mod h1:qtkxNlt5i3rrdirfJE/bQeW/IeLajKexErv7jEIV+Uc=
553-
github.com/cockroachdb/crlib v0.0.0-20251001180057-2a49e1873587 h1:qjG2TrBrPbGRVYp5obcAi8OSsuFJ8s1AElDImHLV9tY=
554-
github.com/cockroachdb/crlib v0.0.0-20251001180057-2a49e1873587/go.mod h1:ae57yNis2F1FThSNdPdoXfiPOVi8G1TLreCBQYPOdqo=
553+
github.com/cockroachdb/crlib v0.0.0-20251024155502-a2e0a212ef05 h1:7DV1UcpuwF78hKMC33w8ube+/wawG/57dT8Wo56+vUo=
554+
github.com/cockroachdb/crlib v0.0.0-20251024155502-a2e0a212ef05/go.mod h1:ae57yNis2F1FThSNdPdoXfiPOVi8G1TLreCBQYPOdqo=
555555
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
556556
github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
557557
github.com/cockroachdb/datadriven v1.0.3-0.20251006155849-f84f9e519edd h1:vpWCe7VvdQbQ/9wGtlH3i+Oj+9OggKci3lsASL1ydvg=

pkg/server/load_endpoint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func Test_loadEndpoint(t *testing.T) {
127127
defer leaktest.AfterTest(t)()
128128
ctx := context.Background()
129129

130-
clock := timeutil.NewTestTimeSource()
130+
clock := timeutil.NewManualTime(timeutil.Now())
131131
rss := status.NewRuntimeStatSampler(ctx, clock)
132132
metricSource := newTestMetricSource(clock)
133133
le, err := newLoadEndpoint(rss, metricSource)

pkg/sql/colflow/stats_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ func TestVectorizedStatsCollector(t *testing.T) {
9292
defer tu.cleanup(ctx)
9393
for nBatches := 1; nBatches < 5; nBatches++ {
9494
timeSource := timeutil.NewTestTimeSource()
95-
mjInputWatch := timeutil.NewTestStopWatch(timeSource.Now)
95+
mjInputWatch := timeutil.NewTestStopWatch(timeSource.NowMono)
9696
leftSource := &timeAdvancingOperator{
9797
OneInputHelper: colexecop.MakeOneInputHelper(makeFiniteChunksSourceWithBatchSize(tu.testAllocator, nBatches, coldata.BatchSize())),
9898
timeSource: timeSource,
9999
}
100100
leftInput := newVectorizedStatsCollector(
101101
leftSource, nil /* kvReader */, nil /* columnarizer */, execinfrapb.ComponentID{ID: 0},
102-
timeutil.NewTestStopWatch(timeSource.Now), nil /* memMonitors */, nil, /* diskMonitors */
102+
timeutil.NewTestStopWatch(timeSource.NowMono), nil /* memMonitors */, nil, /* diskMonitors */
103103
nil, /* inputStatsCollectors */
104104
)
105105
rightSource := &timeAdvancingOperator{
@@ -108,7 +108,7 @@ func TestVectorizedStatsCollector(t *testing.T) {
108108
}
109109
rightInput := newVectorizedStatsCollector(
110110
rightSource, nil /* kvReader */, nil /* columnarizer */, execinfrapb.ComponentID{ID: 1},
111-
timeutil.NewTestStopWatch(timeSource.Now), nil /* memMonitors */, nil, /* diskMonitors */
111+
timeutil.NewTestStopWatch(timeSource.NowMono), nil /* memMonitors */, nil, /* diskMonitors */
112112
nil, /* inputStatsCollectors */
113113
)
114114
mergeJoiner := colexecjoin.NewMergeJoinOp(

pkg/sql/conn_executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,8 +4522,8 @@ func (ex *connExecutor) serialize() serverpb.Session {
45224522

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

45294529
return serverpb.Session{

pkg/util/timeutil/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ go_library(
2222
deps = [
2323
"//pkg/util/grunning",
2424
"//pkg/util/syncutil",
25+
"@com_github_cockroachdb_crlib//crtime",
2526
"@com_github_cockroachdb_errors//:errors",
2627
"@com_github_cockroachdb_errors//errorspb",
2728
"@com_github_cockroachdb_redact//:redact",

pkg/util/timeutil/stopwatch.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cockroachdb/cockroach/pkg/util/grunning"
1212
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
13+
"github.com/cockroachdb/crlib/crtime"
1314
)
1415

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

3738
// NewStopWatch creates a new StopWatch.
3839
func NewStopWatch() *StopWatch {
39-
return newStopWatch(Now)
40+
return newStopWatch(crtime.NowMono)
4041
}
4142

4243
// NewStopWatchWithCPU creates a new StopWatch that will track CPU usage in
4344
// addition to wall-clock time.
4445
func NewStopWatchWithCPU() *StopWatch {
45-
w := newStopWatch(Now)
46+
w := NewStopWatch()
4647
if grunning.Supported {
4748
w.mu.cpuStopWatch = &cpuStopWatch{}
4849
}
@@ -51,11 +52,11 @@ func NewStopWatchWithCPU() *StopWatch {
5152

5253
// NewTestStopWatch create a new StopWatch with the given time source. It is
5354
// used for testing only.
54-
func NewTestStopWatch(timeSource func() time.Time) *StopWatch {
55+
func NewTestStopWatch(timeSource func() crtime.Mono) *StopWatch {
5556
return newStopWatch(timeSource)
5657
}
5758

58-
func newStopWatch(timeSource func() time.Time) *StopWatch {
59+
func newStopWatch(timeSource func() crtime.Mono) *StopWatch {
5960
w := &StopWatch{}
6061
w.mu.timeSource = timeSource
6162
return w
@@ -80,6 +81,8 @@ func (w *StopWatch) Stop() {
8081
defer w.mu.Unlock()
8182
if w.mu.started {
8283
w.mu.started = false
84+
// We don't use w.mu.startedAt.Elapsed() here so that testing time sources
85+
// work correctly.
8386
w.mu.elapsed += w.mu.timeSource().Sub(w.mu.startedAt)
8487
w.mu.cpuStopWatch.stop()
8588
}
@@ -101,29 +104,29 @@ func (w *StopWatch) ElapsedCPU() time.Duration {
101104
return w.mu.cpuStopWatch.elapsed()
102105
}
103106

104-
// LastStartedAt returns the time the stopwatch was last started, and a bool
107+
// CurrentElapsed returns the duration the stopwatch has been started and a bool
105108
// indicating if the stopwatch is currently started.
106-
func (w *StopWatch) LastStartedAt() (startedAt time.Time, started bool) {
109+
func (w *StopWatch) CurrentElapsed() (elapsed time.Duration, started bool) {
107110
w.mu.Lock()
108111
defer w.mu.Unlock()
109-
return w.mu.startedAt, w.mu.started
112+
return w.mu.startedAt.Elapsed(), w.mu.started
110113
}
111114

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

120123
// NewTestTimeSource create a new TestTimeSource.
121124
func NewTestTimeSource() *TestTimeSource {
122-
return &TestTimeSource{initTime: Now()}
125+
return &TestTimeSource{initTime: crtime.NowMono()}
123126
}
124127

125128
// Now tells the current time according to t.
126-
func (t *TestTimeSource) Now() time.Time {
129+
func (t *TestTimeSource) NowMono() crtime.Mono {
127130
return t.initTime.Add(time.Duration(t.counter))
128131
}
129132

pkg/util/timeutil/stopwatch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
// the internal startedAt time.
2222
func TestStopWatchStart(t *testing.T) {
2323
timeSource := timeutil.NewTestTimeSource()
24-
w := timeutil.NewTestStopWatch(timeSource.Now)
24+
w := timeutil.NewTestStopWatch(timeSource.NowMono)
2525

2626
w.Start()
2727
timeSource.Advance()
@@ -37,7 +37,7 @@ func TestStopWatchStart(t *testing.T) {
3737
// state of the stop watch.
3838
func TestStopWatchStop(t *testing.T) {
3939
timeSource := timeutil.NewTestTimeSource()
40-
w := timeutil.NewTestStopWatch(timeSource.Now)
40+
w := timeutil.NewTestStopWatch(timeSource.NowMono)
4141

4242
w.Start()
4343
timeSource.Advance()
@@ -55,7 +55,7 @@ func TestStopWatchStop(t *testing.T) {
5555
// correctly.
5656
func TestStopWatchElapsed(t *testing.T) {
5757
timeSource := timeutil.NewTestTimeSource()
58-
w := timeutil.NewTestStopWatch(timeSource.Now)
58+
w := timeutil.NewTestStopWatch(timeSource.NowMono)
5959
expected := time.Duration(10)
6060

6161
w.Start()

0 commit comments

Comments
 (0)