Skip to content

Commit 6f72d36

Browse files
committed
SERVER-23243 Replace Listener::getElapsedTimeMillis() in scoped_timer.cpp
1 parent 974ed0e commit 6f72d36

20 files changed

+328
-239
lines changed

src/mongo/db/exec/SConscript

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ env.CppUnitTest(
112112
"exec",
113113
"$BUILD_DIR/mongo/db/serveronly",
114114
"$BUILD_DIR/mongo/dbtests/mocklib",
115+
"$BUILD_DIR/mongo/util/clock_source_mock",
115116
"$BUILD_DIR/mongo/util/ntservice_mock",
116117
],
117118
NO_CRUTCH = True,
@@ -127,6 +128,7 @@ env.CppUnitTest(
127128
"$BUILD_DIR/mongo/db/serveronly",
128129
"$BUILD_DIR/mongo/dbtests/mocklib",
129130
"$BUILD_DIR/mongo/db/query/collation/collator_interface_mock",
131+
"$BUILD_DIR/mongo/util/clock_source_mock",
130132
"$BUILD_DIR/mongo/util/ntservice_mock",
131133
],
132134
NO_CRUTCH = True,

src/mongo/db/exec/cached_plan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Status CachedPlanStage::pickBestPlan(PlanYieldPolicy* yieldPolicy) {
7474
// Adds the amount of time taken by pickBestPlan() to executionTimeMillis. There's lots of
7575
// execution work that happens here, so this is needed for the time accounting to
7676
// make sense.
77-
ScopedTimer timer(&_commonStats.executionTimeMillis);
77+
ScopedTimer timer(getClock(), &_commonStats.executionTimeMillis);
7878

7979
// If we work this many times during the trial period, then we will replan the
8080
// query from scratch.

src/mongo/db/exec/multi_plan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Status MultiPlanStage::pickBestPlan(PlanYieldPolicy* yieldPolicy) {
203203
// Adds the amount of time taken by pickBestPlan() to executionTimeMillis. There's lots of
204204
// execution work that happens here, so this is needed for the time accounting to
205205
// make sense.
206-
ScopedTimer timer(&_commonStats.executionTimeMillis);
206+
ScopedTimer timer(getClock(), &_commonStats.executionTimeMillis);
207207

208208
size_t numWorks = getTrialPeriodWorks(getOpCtx(), _collection);
209209
size_t numResults = getTrialPeriodNumToReturn(*_query);

src/mongo/db/exec/plan_stage.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
#include "mongo/db/exec/plan_stage.h"
3434

3535
#include "mongo/db/exec/scoped_timer.h"
36+
#include "mongo/db/operation_context.h"
37+
#include "mongo/db/service_context.h"
3638

3739
namespace mongo {
3840

3941
PlanStage::StageState PlanStage::work(WorkingSetID* out) {
40-
ScopedTimer timer(&_commonStats.executionTimeMillis);
42+
invariant(_opCtx);
43+
ScopedTimer timer(getClock(), &_commonStats.executionTimeMillis);
4144
++_commonStats.works;
4245

4346
StageState workResult = doWork(out);
@@ -102,4 +105,8 @@ void PlanStage::reattachToOperationContext(OperationContext* opCtx) {
102105
doReattachToOperationContext();
103106
}
104107

108+
ClockSource* PlanStage::getClock() const {
109+
return _opCtx->getServiceContext()->getFastClockSource();
110+
}
111+
105112
} // namespace mongo

src/mongo/db/exec/plan_stage.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737

3838
namespace mongo {
3939

40+
class ClockSource;
4041
class Collection;
41-
class RecordId;
4242
class OperationContext;
43+
class RecordId;
4344

4445
/**
4546
* A PlanStage ("stage") is the basic building block of a "Query Execution Plan." A stage is
@@ -357,6 +358,8 @@ class PlanStage {
357358
*/
358359
virtual void doInvalidate(OperationContext* txn, const RecordId& dl, InvalidationType type) {}
359360

361+
ClockSource* getClock() const;
362+
360363
OperationContext* getOpCtx() const {
361364
return _opCtx;
362365
}

src/mongo/db/exec/queued_data_stage_test.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@
3131
//
3232

3333
#include "mongo/db/exec/queued_data_stage.h"
34+
3435
#include "mongo/db/exec/working_set.h"
36+
#include "mongo/db/operation_context_noop.h"
37+
#include "mongo/db/service_context.h"
38+
#include "mongo/db/service_context_noop.h"
3539
#include "mongo/stdx/memory.h"
3640
#include "mongo/unittest/unittest.h"
41+
#include "mongo/util/clock_source_mock.h"
3742

3843
using namespace mongo;
3944

@@ -42,12 +47,38 @@ namespace {
4247
using std::unique_ptr;
4348
using stdx::make_unique;
4449

50+
class QueuedDataStageTest : public unittest::Test {
51+
public:
52+
QueuedDataStageTest() {
53+
_service = stdx::make_unique<ServiceContextNoop>();
54+
_service.get()->setFastClockSource(stdx::make_unique<ClockSourceMock>());
55+
_client = _service.get()->makeClient("test");
56+
_opCtxNoop.reset(new OperationContextNoop(_client.get(), 0));
57+
_opCtx = _opCtxNoop.get();
58+
}
59+
60+
protected:
61+
OperationContext* getOpCtx() {
62+
return _opCtx;
63+
}
64+
65+
private:
66+
OperationContext* _opCtx;
67+
68+
// Members of a class are destroyed in reverse order of declaration.
69+
// The UniqueClient must be destroyed before the ServiceContextNoop is destroyed.
70+
// The OperationContextNoop must be destroyed before the UniqueClient is destroyed.
71+
std::unique_ptr<ServiceContextNoop> _service;
72+
ServiceContext::UniqueClient _client;
73+
std::unique_ptr<OperationContextNoop> _opCtxNoop;
74+
};
75+
4576
//
4677
// Basic test that we get out valid stats objects.
4778
//
48-
TEST(QueuedDataStageTest, getValidStats) {
79+
TEST_F(QueuedDataStageTest, getValidStats) {
4980
WorkingSet ws;
50-
auto mock = make_unique<QueuedDataStage>(nullptr, &ws);
81+
auto mock = make_unique<QueuedDataStage>(getOpCtx(), &ws);
5182
const CommonStats* commonStats = mock->getCommonStats();
5283
ASSERT_EQUALS(commonStats->works, static_cast<size_t>(0));
5384
const SpecificStats* specificStats = mock->getSpecificStats();
@@ -59,10 +90,10 @@ TEST(QueuedDataStageTest, getValidStats) {
5990
//
6091
// Test that our stats are updated as we perform operations.
6192
//
62-
TEST(QueuedDataStageTest, validateStats) {
93+
TEST_F(QueuedDataStageTest, validateStats) {
6394
WorkingSet ws;
6495
WorkingSetID wsID;
65-
auto mock = make_unique<QueuedDataStage>(nullptr, &ws);
96+
auto mock = make_unique<QueuedDataStage>(getOpCtx(), &ws);
6697

6798
// make sure that we're at all zero
6899
const CommonStats* stats = mock->getCommonStats();

src/mongo/db/exec/scoped_timer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@
2929
#include "mongo/platform/basic.h"
3030

3131
#include "mongo/db/exec/scoped_timer.h"
32-
33-
#include "mongo/util/net/listen.h"
32+
#include "mongo/util/clock_source.h"
3433

3534
namespace mongo {
3635

37-
ScopedTimer::ScopedTimer(long long* counter)
38-
: _counter(counter), _start(Listener::getElapsedTimeMillis()) {}
36+
ScopedTimer::ScopedTimer(ClockSource* cs, long long* counter)
37+
: _clock(cs), _counter(counter), _start(cs->now()) {}
3938

4039
ScopedTimer::~ScopedTimer() {
41-
long long elapsed = Listener::getElapsedTimeMillis() - _start;
40+
long long elapsed = durationCount<Milliseconds>(_clock->now() - _start);
4241
*_counter += elapsed;
4342
}
4443

src/mongo/db/exec/scoped_timer.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030

3131
#include "mongo/base/disallow_copying.h"
3232

33+
#include "mongo/util/time_support.h"
34+
3335
namespace mongo {
3436

37+
class ClockSource;
38+
3539
/**
3640
* This class increments a counter by a rough estimate of the time elapsed since its
3741
* construction when it goes out of scope.
@@ -40,19 +44,17 @@ class ScopedTimer {
4044
MONGO_DISALLOW_COPYING(ScopedTimer);
4145

4246
public:
43-
ScopedTimer(long long* counter);
47+
ScopedTimer(ClockSource* cs, long long* counter);
4448

4549
~ScopedTimer();
4650

4751
private:
48-
// Default constructor disallowed.
49-
ScopedTimer();
50-
52+
ClockSource* const _clock;
5153
// Reference to the counter that we are incrementing with the elapsed time.
5254
long long* _counter;
5355

5456
// Time at which the timer was constructed.
55-
long long _start;
57+
const Date_t _start;
5658
};
5759

5860
} // namespace mongo

0 commit comments

Comments
 (0)