diff options
Diffstat (limited to 'src/mongo/util/procparser_test.cpp')
| -rw-r--r-- | src/mongo/util/procparser_test.cpp | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/mongo/util/procparser_test.cpp b/src/mongo/util/procparser_test.cpp index 04edee43380..597c5dde272 100644 --- a/src/mongo/util/procparser_test.cpp +++ b/src/mongo/util/procparser_test.cpp @@ -34,6 +34,7 @@ #include "mongo/util/procparser.h" #include <boost/filesystem.hpp> +#include <fcntl.h> #include <map> #include "mongo/bson/bsonobj.h" @@ -80,6 +81,25 @@ StringMap toNestedStringMap(BSONObj& obj) { return map; } +bool isPSISupported(StringData filename) { + int fd = open(filename.toString().c_str(), 0); + if (fd == -1) { + return false; + } + ScopeGuard scopedGuard([fd] { close(fd); }); + + std::array<char, 1> buf; + + while (read(fd, buf.data(), buf.size()) == -1) { + auto ec = lastPosixError(); + if (ec == posixError(EOPNOTSUPP)) { + return false; + } + ASSERT_EQ(ec, posixError(EINTR)); + } + return true; +} + #define ASSERT_KEY(_key) ASSERT_TRUE(stringMap.find(_key) != stringMap.end()); #define ASSERT_NO_KEY(_key) ASSERT_TRUE(stringMap.find(_key) == stringMap.end()); #define ASSERT_KEY_AND_VALUE(_key, _value) ASSERT_EQUALS(stringMap.at(_key), _value); @@ -113,6 +133,11 @@ StringMap toNestedStringMap(BSONObj& obj) { ASSERT_OK(procparser::parseProcVMStat(_keys, _x, &builder)); \ auto obj = builder.obj(); \ auto stringMap = toStringMap(obj); +#define ASSERT_PARSE_PRESSURE(_x) \ + BSONObjBuilder builder; \ + ASSERT_OK(procparser::parseProcPressure(_x, &builder)); \ + auto obj = builder.obj(); \ + auto stringMap = toStringMap(obj); TEST(FTDCProcStat, TestStat) { @@ -878,5 +903,112 @@ TEST(FTDCProcVMStat, TestLocalNonExistentVMStat) { ASSERT_NOT_OK(procparser::parseProcVMStatFile("/proc/does_not_exist", keys, &builder)); } +TEST(FTDCProcPressure, TestSuccess) { + // Normal cases + { + ASSERT_PARSE_PRESSURE( + "some avg10=0.10 avg60=6.50 avg300=1.00 total=14\nfull avg10=2.30 " + "avg60=0.00 avg300=0.14 total=10"); + ASSERT(obj["some"]["totalMicros"].Double() == 14); + + ASSERT(obj["full"]["totalMicros"].Double() == 10); + } + { + ASSERT_PARSE_PRESSURE("some avg10=0.10 avg60=6.50 avg300=1.00 total=14"); + ASSERT(obj["some"]["totalMicros"].Double() == 14); + + ASSERT(!obj["full"]); + } + { + ASSERT_PARSE_PRESSURE( + "some avg10=0.10 avg60=6.50 avg300=1.00 total=14\nfull avg10=2.30 " + "avg60=0.00 avg300=0.14 total=10"); + ASSERT(obj["some"]["totalMicros"].Double() == 14); + + ASSERT(obj["full"]["totalMicros"].Double() == 10); + } +} + +TEST(FTDCProcPressure, TestFailure) { + // Failure cases + { + BSONObjBuilder builder; + ASSERT_NOT_OK(procparser::parseProcPressureFile("", "", &builder)); + } + + { + BSONObjBuilder builder; + ASSERT_NOT_OK( + procparser::parseProcPressureFile("cpu", "/proc/non-existent-file", &builder)); + } + + // 'total' is not found in the data given. + { + BSONObjBuilder builder; + ASSERT_NOT_OK( + procparser::parseProcPressure("some avg10=0.10 avg60=6.50 avg300=1.00", &builder)); + } + + // 'total' is not found in one of the rows. + { + BSONObjBuilder builder; + ASSERT_NOT_OK( + procparser::parseProcPressure("some avg10=0.10 avg60=6.50 avg300=1.00\nfull avg10=2.30 " + "avg60=0.00 avg300=0.14 total=10", + &builder)); + } + + // 'total' is not given a valid number value. + { + BSONObjBuilder builder; + ASSERT_NOT_OK(procparser::parseProcPressure( + "some avg10=0.10 avg60=6.50 avg300=1.00 total=invalid", &builder)); + } +} + +TEST(FTDCProcPressure, TestLocalPressureInfo) { + if (isPSISupported("/proc/pressure/cpu")) { + BSONObjBuilder builder; + + ASSERT_OK(procparser::parseProcPressureFile("cpu", "/proc/pressure/cpu", &builder)); + + BSONObj obj = builder.obj(); + ASSERT(obj.hasField("cpu")); + ASSERT(obj["cpu"]["some"]); + ASSERT(obj["cpu"]["some"]["totalMicros"]); + + // After linux kernel 5.13, /proc/pressure/cpu includes 'full' filled with 0. + ASSERT(!obj["cpu"]["full"] || obj["cpu"]["full"]["totalMicros"].Double() == 0); + } + + if (isPSISupported("/proc/pressure/memory")) { + BSONObjBuilder builder; + + ASSERT_OK(procparser::parseProcPressureFile("memory", "/proc/pressure/memory", &builder)); + + BSONObj obj = builder.obj(); + ASSERT(obj.hasField("memory")); + ASSERT(obj["memory"]["some"]); + ASSERT(obj["memory"]["some"]["totalMicros"]); + + ASSERT(obj["memory"]["full"]); + ASSERT(obj["memory"]["full"]["totalMicros"]); + } + + if (isPSISupported("/proc/pressure/io")) { + BSONObjBuilder builder; + + ASSERT_OK(procparser::parseProcPressureFile("io", "/proc/pressure/io", &builder)); + + BSONObj obj = builder.obj(); + ASSERT(obj.hasField("io")); + ASSERT(obj["io"]["some"]); + ASSERT(obj["io"]["some"]["totalMicros"]); + + ASSERT(obj["io"]["full"]); + ASSERT(obj["io"]["full"]["totalMicros"]); + } +} + } // namespace } // namespace mongo |
