Skip to content

Commit 7d0c3ac

Browse files
committed
stopwatch: use crtime.NowMono
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
1 parent 0cb6c55 commit 7d0c3ac

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

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
@@ -4516,8 +4516,8 @@ func (ex *connExecutor) serialize() serverpb.Session {
45164516

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

45234523
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)