summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp')
-rw-r--r--src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp
index 7c954df00ab..383b5186d8e 100644
--- a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp
+++ b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp
@@ -665,4 +665,84 @@ Status WiredTigerUtil::exportTableToBSON(WT_SESSION* session,
return Status::OK();
}
+Status WiredTigerUtil::exportOperationStatsInfoToBSON(WT_SESSION* session,
+ const std::string& uri,
+ const std::string& config,
+ BSONObjBuilder* bob) {
+ invariant(session);
+ invariant(bob);
+
+ // Map the statistics to a name and type desired for user consumption. There are two types of
+ // operation statistics - data and wait.
+ enum class Section { DATA, WAIT };
+ static std::map<int, std::pair<StringData, Section>> statNameMap = {
+ {WT_STAT_SESSION_BYTES_READ, std::make_pair("bytesRead", Section::DATA)},
+ {WT_STAT_SESSION_BYTES_WRITE, std::make_pair("bytesWritten", Section::DATA)},
+ {WT_STAT_SESSION_LOCK_DHANDLE_WAIT, std::make_pair("handleLock", Section::WAIT)},
+ {WT_STAT_SESSION_READ_TIME, std::make_pair("timeReadingMicros", Section::DATA)},
+ {WT_STAT_SESSION_WRITE_TIME, std::make_pair("timeWritingMicros", Section::DATA)},
+ {WT_STAT_SESSION_LOCK_SCHEMA_WAIT, std::make_pair("schemaLock", Section::WAIT)},
+ {WT_STAT_SESSION_CACHE_TIME, std::make_pair("cache", Section::WAIT)}};
+
+ WT_CURSOR* c = nullptr;
+ const char* cursorConfig = config.empty() ? nullptr : config.c_str();
+ int ret = session->open_cursor(session, uri.c_str(), nullptr, cursorConfig, &c);
+ if (ret != 0) {
+ return Status(ErrorCodes::CursorNotFound,
+ str::stream() << "unable to open cursor at URI " << uri << ". reason: "
+ << wiredtiger_strerror(ret));
+ }
+ invariant(c);
+ ON_BLOCK_EXIT(c->close, c);
+
+ BSONObjBuilder* dataSection = nullptr;
+ BSONObjBuilder* waitSection = nullptr;
+ const char* desc;
+ uint64_t value;
+ uint64_t key;
+ while (c->next(c) == 0 && c->get_key(c, &key) == 0) {
+ fassert(51035, c->get_value(c, &desc, nullptr, &value) == 0);
+
+ StringData statName;
+ // Find the user consumable name for this statistic.
+ auto statIt = statNameMap.find(key);
+ invariant(statIt != statNameMap.end());
+ statName = statIt->second.first;
+
+ Section subs = statIt->second.second;
+ long long casted_val = _castStatisticsValue<long long>(value);
+
+ // Add this statistic only if higher than zero.
+ if (casted_val > 0) {
+ // Gather the statistic into its own subsection in the BSONObj.
+ switch (subs) {
+ case Section::DATA:
+ if (!dataSection)
+ dataSection = new BSONObjBuilder();
+
+ dataSection->append(statName, casted_val);
+ break;
+ case Section::WAIT:
+ if (!waitSection)
+ waitSection = new BSONObjBuilder();
+
+ waitSection->append(statName, casted_val);
+ break;
+ default:
+ return Status(ErrorCodes::BadValue,
+ str::stream() << "Unexpected storage statistics type.");
+ }
+ }
+ }
+
+ if (dataSection)
+ bob->append("data", dataSection->obj());
+ if (waitSection)
+ bob->append("timeWaitingMicros", waitSection->obj());
+
+ // Reset the statistics so that the next fetch gives the recent values.
+ c->reset(c);
+ return Status::OK();
+}
+
} // namespace mongo