Skip to content

Commit 22eecb9

Browse files
erl-angMongoDB Bot
authored andcommitted
SERVER-98971 Decorate OperationContext with an OpMemoryUse Object (#30973)
GitOrigin-RevId: 86ebbc3
1 parent 2da0567 commit 22eecb9

13 files changed

+219
-880
lines changed

src/mongo/BUILD.bazel

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ mongo_cc_library(
179179
"//src/mongo/util:assert_util.cpp",
180180
"//src/mongo/util:base64.cpp",
181181
"//src/mongo/util:boost_assert_impl.cpp",
182-
"//src/mongo/util:chunked_memory_aggregator.cpp",
183-
"//src/mongo/util:concurrent_memory_aggregator.cpp",
184182
"//src/mongo/util:duration.cpp",
185183
"//src/mongo/util:errno_util.cpp",
186184
"//src/mongo/util:exception_filter_win32.cpp",
@@ -337,9 +335,7 @@ mongo_cc_library(
337335
"//src/mongo/util:assert_util_core.h",
338336
"//src/mongo/util:base64.h",
339337
"//src/mongo/util:bufreader.h",
340-
"//src/mongo/util:chunked_memory_aggregator.h",
341338
"//src/mongo/util:clock_source.h",
342-
"//src/mongo/util:concurrent_memory_aggregator.h",
343339
"//src/mongo/util:ctype.h",
344340
"//src/mongo/util:debug_util.h",
345341
"//src/mongo/util:decimal_counter.h",
@@ -413,7 +409,6 @@ mongo_cc_library(
413409
"//conditions:default": [],
414410
}),
415411
deps = [
416-
"//src/mongo/db/memory_tracking",
417412
"//src/mongo/stdx",
418413
"//src/mongo/util:boost_assert_shim",
419414
"//src/mongo/util:debugger",

src/mongo/db/commands/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ mongo_cc_library(
644644
"//src/mongo/db/matcher/schema:encrypt_schema_types.h",
645645
"//src/mongo/db/matcher/schema:expression_internal_schema_allowed_properties.h",
646646
"//src/mongo/db/matcher/schema:json_pointer.h",
647+
"//src/mongo/db/memory_tracking:memory_usage_tracker.h",
647648
"//src/mongo/db/pipeline:accumulation_statement.h",
648649
"//src/mongo/db/pipeline:accumulator.h",
649650
"//src/mongo/db/pipeline:accumulator_for_window_functions.h",

src/mongo/db/memory_tracking/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,28 @@ exports_files(
1111

1212
mongo_cc_library(
1313
name = "memory_tracking",
14+
srcs = [
15+
"op_memory_use.cpp",
16+
],
1417
hdrs = [
1518
"memory_usage_tracker.h",
19+
"op_memory_use.h",
20+
"operation_memory_usage_tracker.h",
21+
],
22+
deps = [
23+
"//src/mongo/db:service_context",
1624
],
1725
)
1826

1927
mongo_cc_unit_test(
2028
name = "memory_tracking_test",
2129
srcs = [
2230
"memory_usage_tracker_test.cpp",
31+
"op_memory_use_test.cpp",
2332
],
2433
tags = ["mongo_unittest_first_group"],
34+
deps = [
35+
":memory_tracking",
36+
"//src/mongo/db:service_context_test_fixture",
37+
],
2538
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (C) 2025-present MongoDB, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*
17+
* As a special exception, the copyright holders give permission to link the
18+
* code of portions of this program with the OpenSSL library under certain
19+
* conditions as described in each individual source file and distribute
20+
* linked combinations including the program with the OpenSSL library. You
21+
* must comply with the Server Side Public License in all respects for
22+
* all of the code used other than as permitted herein. If you modify file(s)
23+
* with this exception, you may extend this exception to your version of the
24+
* file(s), but you are not obligated to do so. If you do not wish to do so,
25+
* delete this exception statement from your version. If you delete this
26+
* exception statement from all source files in the program, then also delete
27+
* it in the license file.
28+
*/
29+
30+
#include "mongo/db/memory_tracking/op_memory_use.h"
31+
#include "mongo/base/initializer.h"
32+
#include "mongo/db/commands/server_status_metric.h"
33+
#include "mongo/db/memory_tracking/operation_memory_usage_tracker.h"
34+
#include "mongo/util/assert_util.h"
35+
36+
namespace mongo {
37+
const OperationContext::Decoration<std::unique_ptr<OperationMemoryUsageTracker>>
38+
OpMemoryUse::operationMemoryAggregator =
39+
OperationContext::declareDecoration<std::unique_ptr<OperationMemoryUsageTracker>>();
40+
} // namespace mongo
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (C) 2025-present MongoDB, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*
17+
* As a special exception, the copyright holders give permission to link the
18+
* code of portions of this program with the OpenSSL library under certain
19+
* conditions as described in each individual source file and distribute
20+
* linked combinations including the program with the OpenSSL library. You
21+
* must comply with the Server Side Public License in all respects for
22+
* all of the code used other than as permitted herein. If you modify file(s)
23+
* with this exception, you may extend this exception to your version of the
24+
* file(s), but you are not obligated to do so. If you do not wish to do so,
25+
* delete this exception statement from your version. If you delete this
26+
* exception statement from all source files in the program, then also delete
27+
* it in the license file.
28+
*/
29+
30+
#pragma once
31+
32+
#include "mongo/db/memory_tracking/operation_memory_usage_tracker.h"
33+
#include "mongo/db/operation_context.h"
34+
#include "mongo/db/service_context.h"
35+
36+
namespace mongo {
37+
// OpMemoryUse will hold decorations which allow operations to roll up their memory use in the
38+
// operation context.
39+
class OpMemoryUse {
40+
public:
41+
// This decoration will allow all code paths under one OperationContext to tally up their memory
42+
// use to a single place. Because only the OperationContext should own this, we use a
43+
// unique_ptr. This will also allow all the executors to have the same pointer to the
44+
// operation memory tracker between the originating request and the call to getMore.
45+
static const OperationContext::Decoration<std::unique_ptr<OperationMemoryUsageTracker>>
46+
operationMemoryAggregator;
47+
};
48+
49+
}; // namespace mongo
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (C) 2025-present MongoDB, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*
17+
* As a special exception, the copyright holders give permission to link the
18+
* code of portions of this program with the OpenSSL library under certain
19+
* conditions as described in each individual source file and distribute
20+
* linked combinations including the program with the OpenSSL library. You
21+
* must comply with the Server Side Public License in all respects for
22+
* all of the code used other than as permitted herein. If you modify file(s)
23+
* with this exception, you may extend this exception to your version of the
24+
* file(s), but you are not obligated to do so. If you do not wish to do so,
25+
* delete this exception statement from your version. If you delete this
26+
* exception statement from all source files in the program, then also delete
27+
* it in the license file.
28+
*/
29+
30+
#include "mongo/db/memory_tracking/op_memory_use.h"
31+
#include "mongo/db/memory_tracking/operation_memory_usage_tracker.h"
32+
#include "mongo/db/service_context_test_fixture.h"
33+
34+
namespace mongo {
35+
namespace {
36+
37+
class MemoryTrackingTest : public ServiceContextTest {
38+
public:
39+
using ServiceContextTest::ServiceContextTest;
40+
41+
auto makeOpCtx(std::string desc = "MemoryTrackingTest",
42+
std::shared_ptr<transport::Session> session = nullptr) {
43+
auto client = getServiceContext()->getService()->makeClient(desc, session);
44+
return client->makeOperationContext();
45+
}
46+
};
47+
48+
TEST_F(MemoryTrackingTest, OpMemoryUseCanAttachToOpCtx) {
49+
auto opCtx = makeOpCtx();
50+
51+
// Attach mock memory usage stats to the opCtx and validate that the decoration was set.
52+
auto mockMemoryStats = std::make_unique<OperationMemoryUsageTracker>(10 /*currentMemoryBytes*/,
53+
15 /*maxUsedMemoryBytes*/);
54+
OpMemoryUse::operationMemoryAggregator(opCtx.get()) = std::move(mockMemoryStats);
55+
OperationMemoryUsageTracker* memoryTracker =
56+
OpMemoryUse::operationMemoryAggregator(opCtx.get()).get();
57+
ASSERT(memoryTracker);
58+
ASSERT_EQ(memoryTracker->currentMemoryBytes(), 10);
59+
ASSERT_EQ(memoryTracker->maxUsedMemoryBytes(), 15);
60+
}
61+
} // namespace
62+
} // namespace mongo
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (C) 2025-present MongoDB, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*
17+
* As a special exception, the copyright holders give permission to link the
18+
* code of portions of this program with the OpenSSL library under certain
19+
* conditions as described in each individual source file and distribute
20+
* linked combinations including the program with the OpenSSL library. You
21+
* must comply with the Server Side Public License in all respects for
22+
* all of the code used other than as permitted herein. If you modify file(s)
23+
* with this exception, you may extend this exception to your version of the
24+
* file(s), but you are not obligated to do so. If you do not wish to do so,
25+
* delete this exception statement from your version. If you delete this
26+
* exception statement from all source files in the program, then also delete
27+
* it in the license file.
28+
*/
29+
#pragma once
30+
#include <cstdint>
31+
32+
namespace mongo {
33+
// TODO SERVER-98972 Replace this.
34+
class OperationMemoryUsageTracker {
35+
public:
36+
OperationMemoryUsageTracker(int64_t currentMemoryBytes = 0, int64_t maxUsedMemoryBytes = 0)
37+
: _currentMemoryBytes(currentMemoryBytes), _maxUsedMemoryBytes(maxUsedMemoryBytes) {}
38+
39+
int64_t currentMemoryBytes() const {
40+
return _currentMemoryBytes;
41+
}
42+
43+
int64_t maxUsedMemoryBytes() const {
44+
return _maxUsedMemoryBytes;
45+
}
46+
47+
private:
48+
// Current amount of memory in use by all blocking stages.
49+
int64_t _currentMemoryBytes = 0;
50+
51+
// High-water mark: the highest amount of memory that has been allocated at one time so far.
52+
int64_t _maxUsedMemoryBytes = 0;
53+
};
54+
} // namespace mongo

src/mongo/util/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,6 @@ mongo_cc_unit_test(
921921
"base64_test.cpp",
922922
"cancellation_test.cpp",
923923
"clock_source_mock_test.cpp",
924-
"concurrent_memory_aggregator_test.cpp",
925924
"container_size_helper_test.cpp",
926925
"ctype_test.cpp",
927926
"decimal_counter_test.cpp",

src/mongo/util/chunked_memory_aggregator.cpp

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)