summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/util
diff options
context:
space:
mode:
authorjoshua <80741223+jlap199@users.noreply.github.com>2022-11-12 02:47:38 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-11-12 03:19:26 +0000
commit85b89ff203ec7a70e43af24831b35a3d53a259e4 (patch)
tree8c484e57d3da06c9aa30da7e79796f23526403a7 /src/mongo/db/query/util
parent2041a8a461f42c6fc1c257e87e88c6ae695a51a8 (diff)
downloadmongo-85b89ff203ec7a70e43af24831b35a3d53a259e4.tar.gz
SERVER-71065 Implement onTelemetryStoreSizeUpdate function
Diffstat (limited to 'src/mongo/db/query/util')
-rw-r--r--src/mongo/db/query/util/memory_util.cpp124
-rw-r--r--src/mongo/db/query/util/memory_util.h66
-rw-r--r--src/mongo/db/query/util/memory_util_test.cpp73
3 files changed, 263 insertions, 0 deletions
diff --git a/src/mongo/db/query/util/memory_util.cpp b/src/mongo/db/query/util/memory_util.cpp
new file mode 100644
index 00000000000..dbd4f23bee1
--- /dev/null
+++ b/src/mongo/db/query/util/memory_util.cpp
@@ -0,0 +1,124 @@
+/**
+ * Copyright (C) 2021-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+#include "mongo/db/query/util/memory_util.h"
+#include "mongo/logv2/log.h"
+#include "mongo/util/pcre.h"
+#include "mongo/util/processinfo.h"
+#include <cstddef>
+
+#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery
+
+
+namespace mongo::memory_util {
+
+StatusWith<MemoryUnits> parseUnitString(const std::string& strUnit) {
+ if (strUnit.empty()) {
+ return Status(ErrorCodes::Error{6007010}, "Unit value cannot be empty");
+ }
+
+ if (strUnit[0] == '%') {
+ return MemoryUnits::kPercent;
+ } else if (strUnit[0] == 'M' || strUnit[0] == 'm') {
+ return MemoryUnits::kMB;
+ } else if (strUnit[0] == 'G' || strUnit[0] == 'g') {
+ return MemoryUnits::kGB;
+ }
+
+ return Status(ErrorCodes::Error{6007011}, "Incorrect unit value");
+}
+
+StatusWith<MemorySize> MemorySize::parse(const std::string& str) {
+ // Looks for a floating point number with followed by a unit suffix (MB, GB, %).
+ static auto& re = *new pcre::Regex(R"re((?i)^\s*(\d+\.?\d*)\s*(MB|GB|%)\s*$)re");
+ auto m = re.matchView(str);
+ if (!m) {
+ return {ErrorCodes::Error{6007012}, "Unable to parse memory size string"};
+ }
+ double size = std::stod(std::string{m[1]});
+ std::string strUnit{m[2]};
+
+ auto statusWithUnit = parseUnitString(strUnit);
+ if (!statusWithUnit.isOK()) {
+ return statusWithUnit.getStatus();
+ }
+
+ return MemorySize{size, statusWithUnit.getValue()};
+}
+
+size_t convertToSizeInBytes(const MemorySize& memSize) {
+ constexpr size_t kBytesInMB = 1024 * 1024;
+ constexpr size_t kMBytesInGB = 1024;
+
+ double sizeInMB = memSize.size;
+
+ switch (memSize.units) {
+ case MemoryUnits::kPercent:
+ sizeInMB *= ProcessInfo::getMemSizeMB() / 100.0;
+ break;
+ case MemoryUnits::kMB:
+ break;
+ case MemoryUnits::kGB:
+ sizeInMB *= kMBytesInGB;
+ break;
+ }
+
+ return static_cast<size_t>(sizeInMB * kBytesInMB);
+}
+
+size_t getRequestedMemSizeInBytes(const MemorySize& memSize) {
+ size_t planCacheSize = convertToSizeInBytes(memSize);
+ uassert(5968001,
+ "Cache size must be at least 1KB * number of cores",
+ planCacheSize >= 1024 * ProcessInfo::getNumCores());
+ return planCacheSize;
+}
+
+/**
+ * Sets upper limit on a storage structure's size. Either that structure's maximumSize or to
+ * percentage of the total system's memory (both known at call site), whichever is smaller.
+ */
+size_t capMemorySize(size_t requestedSizeBytes,
+ size_t maximumSizeGB,
+ double percentTotalSystemMemory) {
+ constexpr size_t kBytesInGB = 1024 * 1024 * 1024;
+ // Express maximum size in bytes.
+ const size_t maximumSizeBytes = maximumSizeGB * kBytesInGB;
+ const memory_util::MemorySize limitToProcessSize{percentTotalSystemMemory,
+ memory_util::MemoryUnits::kPercent};
+ const size_t limitToProcessSizeInBytes = convertToSizeInBytes(limitToProcessSize);
+
+ // The size will be capped by the minimum of the two values defined above.
+ const size_t upperLimit = std::min(maximumSizeBytes, limitToProcessSizeInBytes);
+
+ if (requestedSizeBytes > upperLimit) {
+ requestedSizeBytes = upperLimit;
+ }
+ return requestedSizeBytes;
+}
+} // namespace mongo::memory_util
diff --git a/src/mongo/db/query/util/memory_util.h b/src/mongo/db/query/util/memory_util.h
new file mode 100644
index 00000000000..345780b4c84
--- /dev/null
+++ b/src/mongo/db/query/util/memory_util.h
@@ -0,0 +1,66 @@
+/**
+ * Copyright (C) 2021-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include <string>
+
+#include "mongo/base/error_codes.h"
+#include "mongo/base/status_with.h"
+
+namespace mongo::memory_util {
+
+/**
+ * Defines units of memory.
+ */
+enum class MemoryUnits {
+ kPercent,
+ kMB,
+ kGB,
+};
+
+/**
+ * Represents parsed memory size parameter.
+ */
+struct MemorySize {
+ static StatusWith<MemorySize> parse(const std::string& str);
+
+ const double size;
+ const MemoryUnits units;
+};
+
+StatusWith<MemoryUnits> parseUnitString(const std::string& strUnit);
+size_t convertToSizeInBytes(const MemorySize& memSize);
+size_t capMemorySize(size_t requestedSizeBytes,
+ size_t maximumSizeGB,
+ double percentTotalSystemMemory);
+size_t getRequestedMemSizeInBytes(const MemorySize& memSize);
+
+
+} // namespace mongo::memory_util
diff --git a/src/mongo/db/query/util/memory_util_test.cpp b/src/mongo/db/query/util/memory_util_test.cpp
new file mode 100644
index 00000000000..78f7b3098d6
--- /dev/null
+++ b/src/mongo/db/query/util/memory_util_test.cpp
@@ -0,0 +1,73 @@
+/**
+ * Copyright (C) 2021-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/db/query/util/memory_util.h"
+
+#include "mongo/unittest/unittest.h"
+
+namespace mongo::memory_util {
+
+bool operator==(const MemorySize& lhs, const MemorySize& rhs) {
+ constexpr double kEpsilon = 1e-10;
+ return std::abs(lhs.size - rhs.size) < kEpsilon && lhs.units == rhs.units;
+}
+
+TEST(MemorySizeTest, ParseUnitStringPercent) {
+ ASSERT_TRUE(MemoryUnits::kPercent == parseUnitString("%"));
+}
+
+TEST(MemorySizeTest, ParseUnitStringMB) {
+ ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("MB"));
+ ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("mb"));
+ ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("mB"));
+ ASSERT_TRUE(MemoryUnits::kMB == parseUnitString("Mb"));
+}
+
+TEST(MemorySizeTest, ParseUnitStringGB) {
+ ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("GB"));
+ ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("gb"));
+ ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("gB"));
+ ASSERT_TRUE(MemoryUnits::kGB == parseUnitString("Gb"));
+}
+
+TEST(MemorySizeTest, ParseUnitStringIncorrectValue) {
+ ASSERT_NOT_OK(parseUnitString("").getStatus());
+ ASSERT_NOT_OK(parseUnitString(" ").getStatus());
+ ASSERT_NOT_OK(parseUnitString("KB").getStatus());
+}
+
+TEST(MemorySizeTest, ParseMemorySize) {
+ ASSERT_TRUE((MemorySize{10.0, MemoryUnits::kPercent}) == MemorySize::parse("10%"));
+ ASSERT_TRUE((MemorySize{300.0, MemoryUnits::kMB}) == MemorySize::parse("300MB"));
+ ASSERT_TRUE((MemorySize{4.0, MemoryUnits::kGB}) == MemorySize::parse("4GB"));
+ ASSERT_TRUE((MemorySize{5.1, MemoryUnits::kPercent}) == MemorySize::parse(" 5.1%"));
+ ASSERT_TRUE((MemorySize{11.1, MemoryUnits::kMB}) == MemorySize::parse("11.1 mb"));
+ ASSERT_TRUE((MemorySize{12.1, MemoryUnits::kGB}) == MemorySize::parse(" 12.1 Gb "));
+}
+} // namespace mongo::memory_util