@@ -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.
3839func 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.
4445func 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.
115118type TestTimeSource struct {
116- initTime time. Time
119+ initTime crtime. Mono
117120 counter int64
118121}
119122
120123// NewTestTimeSource create a new TestTimeSource.
121124func 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
0 commit comments