diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-04-17 17:59:04 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-04-17 17:59:04 -0400 |
commit | 36c7a372530f98932c905d6cf6bbba913675a24f (patch) | |
tree | 1da6a148b2ecc07a4122bde5497cbd122212738d | |
parent | 61fc1d72075430bd58c65b1644b8eaa1ca290377 (diff) | |
download | mongo-36c7a372530f98932c905d6cf6bbba913675a24f.tar.gz |
SERVER-24610 Refactor System Stats Collectors
(cherry picked from commit aca68d91a7226a14cfdc73171eb97c4922559940)
-rw-r--r-- | src/mongo/db/ftdc/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats.cpp | 93 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats.h | 26 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats_freebsd.cpp | 37 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats_linux.cpp | 107 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats_openbsd.cpp | 37 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats_osx.cpp | 37 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats_solaris.cpp | 37 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_system_stats_windows.cpp | 37 |
9 files changed, 331 insertions, 81 deletions
diff --git a/src/mongo/db/ftdc/SConscript b/src/mongo/db/ftdc/SConscript index fbf55b3ca64..26cbb8a8382 100644 --- a/src/mongo/db/ftdc/SConscript +++ b/src/mongo/db/ftdc/SConscript @@ -45,6 +45,7 @@ env.Library( 'ftdc_commands.cpp', 'ftdc_mongod.cpp', 'ftdc_system_stats.cpp', + 'ftdc_system_stats_${TARGET_OS}.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/base', diff --git a/src/mongo/db/ftdc/ftdc_system_stats.cpp b/src/mongo/db/ftdc/ftdc_system_stats.cpp index f0ffb319284..42a2892d964 100644 --- a/src/mongo/db/ftdc/ftdc_system_stats.cpp +++ b/src/mongo/db/ftdc/ftdc_system_stats.cpp @@ -28,103 +28,34 @@ #include "mongo/platform/basic.h" +#include "mongo/db/ftdc/ftdc_system_stats.h" + #include <string> -#include <vector> #include "mongo/base/status.h" #include "mongo/base/string_data.h" #include "mongo/bson/bsonobjbuilder.h" #include "mongo/db/ftdc/collector.h" -#include "mongo/db/ftdc/controller.h" -#include "mongo/stdx/memory.h" - -#ifdef __linux__ -#include "mongo/util/procparser.h" -#endif namespace mongo { -constexpr auto kSystemMetricsCollector = "systemMetrics"; - -#ifdef __linux__ -static const std::vector<StringData> kCpuKeys{ - "btime", "cpu", "ctxt", "processes", "procs_blocked", "procs_running"}; - -// Collect all the memory keys by specifying an empty set. -static const std::vector<StringData> kMemKeys{}; +namespace { /** - * Collect metrics from the Linux /proc file system. + * Name of FTDC collector to create. */ -class LinuxSystemMetricsCollector final : public FTDCCollectorInterface { -public: - LinuxSystemMetricsCollector() : _disks(procparser::findPhysicalDisks("/sys/block")) { - for (const auto& disk : _disks) { - _disksStringData.emplace_back(disk); - } - } - - void collect(OperationContext* txn, BSONObjBuilder& builder) override { - { - BSONObjBuilder subObjBuilder(builder.subobjStart("cpu")); - processStatusErrors( - procparser::parseProcStatFile("/proc/stat", kCpuKeys, &subObjBuilder), - &subObjBuilder); - subObjBuilder.doneFast(); - } +auto kSystemMetricsCollector = "systemMetrics"; - { - BSONObjBuilder subObjBuilder(builder.subobjStart("memory")); - processStatusErrors( - procparser::parseProcMemInfoFile("/proc/meminfo", kMemKeys, &subObjBuilder), - &subObjBuilder); - subObjBuilder.doneFast(); - } - - // Skip the disks section if we could not find any disks. - // This can happen when we do not have permission to /sys/block for instance. - if (!_disksStringData.empty()) { - BSONObjBuilder subObjBuilder(builder.subobjStart("disks")); - processStatusErrors(procparser::parseProcDiskStatsFile( - "/proc/diskstats", _disksStringData, &subObjBuilder), - &subObjBuilder); - subObjBuilder.doneFast(); - } - } +} // namespace - std::string name() const override { - return kSystemMetricsCollector; - } +std::string SystemMetricsCollector::name() const { + return kSystemMetricsCollector; +} -private: - /** - * Convert any errors we see into BSON for the user to see in the final FTDC document. It is - * acceptable for the proc parser to fail, but we do not want to shutdown the FTDC loop because - * of it. We assume that the BSONBuilder is not corrupt on non-OK Status but nothing else with - * regards to the final document output. - */ - static void processStatusErrors(Status s, BSONObjBuilder* builder) { - if (!s.isOK()) { - builder->append("error", s.toString()); - } +void SystemMetricsCollector::processStatusErrors(Status s, BSONObjBuilder* builder) { + if (!s.isOK()) { + builder->append("error", s.toString()); } - -private: - // List of physical disks to collect stats from as string from findPhysicalDisks. - std::vector<std::string> _disks; - - // List of physical disks to collect stats from as StringData to pass to parseProcDiskStatsFile. - std::vector<StringData> _disksStringData; -}; - -void installSystemMetricsCollector(FTDCController* controller) { - controller->addPeriodicCollector(stdx::make_unique<LinuxSystemMetricsCollector>()); } -#else - -void installSystemMetricsCollector(FTDCController* controller) {} - -#endif - } // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats.h b/src/mongo/db/ftdc/ftdc_system_stats.h index 4d88b596da2..20d21ef4f39 100644 --- a/src/mongo/db/ftdc/ftdc_system_stats.h +++ b/src/mongo/db/ftdc/ftdc_system_stats.h @@ -26,11 +26,37 @@ * then also delete it in the license file. */ +#include <string> + +#include "mongo/base/status.h" +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/db/ftdc/controller.h" +#include "mongo/db/ftdc/controller.h" + namespace mongo { class FTDCController; /** + * Base class for system metrics collectors. Sets collector name to a common name all system metrics + * collectors to use. + */ +class SystemMetricsCollector : public FTDCCollectorInterface { +public: + std::string name() const final; + +protected: + /** + * Convert any errors we see into BSON for the user to see in the final FTDC document. It is + * acceptable for the collector to fail, but we do not want to shutdown the FTDC loop because + * of it. We assume that the BSONBuilder is not corrupt on non-OK Status but nothing else with + * regards to the final document output. + */ + static void processStatusErrors(Status s, BSONObjBuilder* builder); +}; + + +/** * Install a system metrics collector if it exists as a periodic collector. */ void installSystemMetricsCollector(FTDCController* controller); diff --git a/src/mongo/db/ftdc/ftdc_system_stats_freebsd.cpp b/src/mongo/db/ftdc/ftdc_system_stats_freebsd.cpp new file mode 100644 index 00000000000..d06092f9ba9 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats_freebsd.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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/platform/basic.h" + +#include "mongo/db/ftdc/ftdc_system_stats.h" + +namespace mongo { + +void installSystemMetricsCollector(FTDCController* controller) {} + +} // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats_linux.cpp b/src/mongo/db/ftdc/ftdc_system_stats_linux.cpp new file mode 100644 index 00000000000..163349bc317 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats_linux.cpp @@ -0,0 +1,107 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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/platform/basic.h" + +#include "mongo/db/ftdc/ftdc_system_stats.h" + +#include <string> +#include <vector> + +#include "mongo/base/status.h" +#include "mongo/base/string_data.h" +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/db/ftdc/collector.h" +#include "mongo/db/ftdc/controller.h" +#include "mongo/stdx/memory.h" +#include "mongo/util/procparser.h" + +namespace mongo { + +namespace { + +static const std::vector<StringData> kCpuKeys{ + "btime", "cpu", "ctxt", "processes", "procs_blocked", "procs_running"}; + +// Collect all the memory keys by specifying an empty set. +static const std::vector<StringData> kMemKeys{}; + +/** + * Collect metrics from the Linux /proc file system. + */ +class LinuxSystemMetricsCollector final : public SystemMetricsCollector { +public: + LinuxSystemMetricsCollector() : _disks(procparser::findPhysicalDisks("/sys/block")) { + for (const auto& disk : _disks) { + _disksStringData.emplace_back(disk); + } + } + + void collect(OperationContext* txn, BSONObjBuilder& builder) override { + { + BSONObjBuilder subObjBuilder(builder.subobjStart("cpu")); + processStatusErrors( + procparser::parseProcStatFile("/proc/stat", kCpuKeys, &subObjBuilder), + &subObjBuilder); + subObjBuilder.doneFast(); + } + + { + BSONObjBuilder subObjBuilder(builder.subobjStart("memory")); + processStatusErrors( + procparser::parseProcMemInfoFile("/proc/meminfo", kMemKeys, &subObjBuilder), + &subObjBuilder); + subObjBuilder.doneFast(); + } + + // Skip the disks section if we could not find any disks. + // This can happen when we do not have permission to /sys/block for instance. + if (!_disksStringData.empty()) { + BSONObjBuilder subObjBuilder(builder.subobjStart("disks")); + processStatusErrors(procparser::parseProcDiskStatsFile( + "/proc/diskstats", _disksStringData, &subObjBuilder), + &subObjBuilder); + subObjBuilder.doneFast(); + } + } + +private: + // List of physical disks to collect stats from as string from findPhysicalDisks. + std::vector<std::string> _disks; + + // List of physical disks to collect stats from as StringData to pass to parseProcDiskStatsFile. + std::vector<StringData> _disksStringData; +}; + +} // namespace + +void installSystemMetricsCollector(FTDCController* controller) { + controller->addPeriodicCollector(stdx::make_unique<LinuxSystemMetricsCollector>()); +} + +} // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats_openbsd.cpp b/src/mongo/db/ftdc/ftdc_system_stats_openbsd.cpp new file mode 100644 index 00000000000..d06092f9ba9 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats_openbsd.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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/platform/basic.h" + +#include "mongo/db/ftdc/ftdc_system_stats.h" + +namespace mongo { + +void installSystemMetricsCollector(FTDCController* controller) {} + +} // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats_osx.cpp b/src/mongo/db/ftdc/ftdc_system_stats_osx.cpp new file mode 100644 index 00000000000..d06092f9ba9 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats_osx.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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/platform/basic.h" + +#include "mongo/db/ftdc/ftdc_system_stats.h" + +namespace mongo { + +void installSystemMetricsCollector(FTDCController* controller) {} + +} // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats_solaris.cpp b/src/mongo/db/ftdc/ftdc_system_stats_solaris.cpp new file mode 100644 index 00000000000..d06092f9ba9 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats_solaris.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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/platform/basic.h" + +#include "mongo/db/ftdc/ftdc_system_stats.h" + +namespace mongo { + +void installSystemMetricsCollector(FTDCController* controller) {} + +} // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_system_stats_windows.cpp b/src/mongo/db/ftdc/ftdc_system_stats_windows.cpp new file mode 100644 index 00000000000..d06092f9ba9 --- /dev/null +++ b/src/mongo/db/ftdc/ftdc_system_stats_windows.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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/platform/basic.h" + +#include "mongo/db/ftdc/ftdc_system_stats.h" + +namespace mongo { + +void installSystemMetricsCollector(FTDCController* controller) {} + +} // namespace mongo |