From 591d1bd31f42e6f5af64bd663ad4de5b907a971e Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 3 Feb 2020 11:05:23 +0100 Subject: [PATCH] Add functions to retrieve 'IO wait' and 'steal' metrics on Linux --- pdns/misc.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ pdns/misc.hh | 2 ++ 2 files changed, 52 insertions(+) diff --git a/pdns/misc.cc b/pdns/misc.cc index f9248af42a..279151116a 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1236,6 +1236,56 @@ uint64_t udpErrorStats(const std::string& str) return 0; } +uint64_t getCPUIOWait(const std::string& str) +{ +#ifdef __linux__ + ifstream ifs("/proc/stat"); + if (!ifs) { + return 0; + } + + string line; + vector parts; + while (getline(ifs, line)) { + if (boost::starts_with(line, "cpu ")) { + stringtok(parts, line, " \n\t\r"); + + if (parts.size() < 6) { + break; + } + + return std::stoull(parts[5]); + } + } +#endif + return 0; +} + +uint64_t getCPUSteal(const std::string& str) +{ +#ifdef __linux__ + ifstream ifs("/proc/stat"); + if (!ifs) { + return 0; + } + + string line; + vector parts; + while (getline(ifs, line)) { + if (boost::starts_with(line, "cpu ")) { + stringtok(parts, line, " \n\t\r"); + + if (parts.size() < 9) { + break; + } + + return std::stoull(parts[8]); + } + } +#endif + return 0; +} + bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum) { if (algoName == DNSName("hmac-md5.sig-alg.reg.int") || algoName == DNSName("hmac-md5")) diff --git a/pdns/misc.hh b/pdns/misc.hh index 4bd9439a87..b4924c4274 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -551,6 +551,8 @@ uint64_t getSpecialMemoryUsage(const std::string&); uint64_t getOpenFileDescriptors(const std::string&); uint64_t getCPUTimeUser(const std::string&); uint64_t getCPUTimeSystem(const std::string&); +uint64_t getCPUIOWait(const std::string&); +uint64_t getCPUSteal(const std::string&); std::string getMACAddress(const ComboAddress& ca); template std::unique_ptr make_unique(Args&&... args) -- 2.47.2