common/Util.cpp | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-)
New commits: commit 1e42b530e688fb72c5ae152d22fa72cf11b2f8cb Author: Ashod Nakashian <[email protected]> Date: Wed Feb 8 23:30:28 2017 -0500 wsd: read memory RSS directly from /proc/pid/stat There is no need to use `ps` here as reading directly is trivial and has far less overhead. Change-Id: I27d0432c1f3a9d35763d67fc445d8bd828f1b27e Reviewed-on: https://gerrit.libreoffice.org/34052 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/common/Util.cpp b/common/Util.cpp index b52ce8f..b959029 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -196,33 +196,40 @@ namespace Util size_t getMemoryUsageRSS(const Poco::Process::PID pid) { - if (pid <= 0) - { - return 0; - } + static const auto pageSizeBytes = getpagesize(); - try + if (pid > 0) { - const auto cmd = "ps o rss= -p " + std::to_string(pid); - FILE* fp = popen(cmd.c_str(), "r"); - if (fp == nullptr) + const auto cmd = "/proc/" + std::to_string(pid) + "/stat"; + FILE* fp = fopen(cmd.c_str(), "r"); + if (fp != nullptr) { - return 0; - } + size_t rss = 0; + char line[4096] = { 0 }; + if (fgets(line, sizeof (line), fp)) + { + const std::string s(line); + int index = 1; + auto pos = s.find(' '); + while (pos != std::string::npos) + { + if (index == 23) + { + // Convert from memory pages to KB. + rss = strtol(&s[pos], nullptr, 10); + rss *= pageSizeBytes; + rss /= 1024; + break; + } + + ++index; + pos = s.find(' ', pos + 1); + } + } - std::string sResponse; - char cmdBuffer[1024]; - while (fgets(cmdBuffer, sizeof(cmdBuffer) - 1, fp) != nullptr) - { - sResponse += cmdBuffer; + fclose(fp); + return rss; } - pclose(fp); - - return std::stoi(sResponse); - } - catch (const std::exception&) - { - LOG_WRN("Trying to find memory of invalid/dead PID " << pid); } return 0; _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
