You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by ta...@apache.org on 2017/11/21 22:46:28 UTC

[2/3] incubator-impala git commit: IMPALA-5624: Replace "ls -l" with opendir() in ProcessStateInfo

IMPALA-5624: Replace "ls -l" with opendir() in ProcessStateInfo

Running shell commands from impalad can be problematic, because using popen() leads
to forking which causes a spike in virtual memory. To avoid this, "ls" is replaced
with POSIX API calls.

FileDescriptorMap fd_desc_ was only used to get the number of file descriptors, so
it was unneccesery work to initialize it. It is removed, and only the number of file
descriptors is computed.

The automatic test for this function is only a sanity check,  because there is no
way to know the "expected value" in advance, and the number of file desciptors can
change anytime.

Change-Id: Ibffae8069a62e100abbfa7d558b49040b095ddc0
Reviewed-on: http://gerrit.cloudera.org:8080/8546
Reviewed-by: Lars Volker <lv...@cloudera.com>
Tested-by: Impala Public Jenkins


Project: http://git-wip-us.apache.org/repos/asf/incubator-impala/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-impala/commit/baa537f8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-impala/tree/baa537f8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-impala/diff/baa537f8

Branch: refs/heads/master
Commit: baa537f8e1e2e284ef9c139a283b86a873e01ee7
Parents: 27c43fe
Author: Csaba Ringhofer <cs...@cloudera.com>
Authored: Tue Nov 14 19:37:28 2017 +0100
Committer: Impala Public Jenkins <im...@gerrit.cloudera.org>
Committed: Tue Nov 21 05:28:47 2017 +0000

----------------------------------------------------------------------
 be/src/util/proc-info-test.cc     |  1 +
 be/src/util/process-state-info.cc | 48 +++++++++++++++-------------------
 be/src/util/process-state-info.h  | 30 +++++++++------------
 3 files changed, 35 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/baa537f8/be/src/util/proc-info-test.cc
----------------------------------------------------------------------
diff --git a/be/src/util/proc-info-test.cc b/be/src/util/proc-info-test.cc
index e83e787..7946e5b 100644
--- a/be/src/util/proc-info-test.cc
+++ b/be/src/util/proc-info-test.cc
@@ -44,6 +44,7 @@ TEST(ProcessStateInfo, Basic) {
   ASSERT_GE(process_state_info.GetBytes("io/read_bytes"), 0);
   ASSERT_GE(process_state_info.GetInt("sched/prio"), 0);
   ASSERT_GE(process_state_info.GetInt("status/Threads"), 0);
+  ASSERT_GT(process_state_info.GetInt("fd/count"), 0);
 }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/baa537f8/be/src/util/process-state-info.cc
----------------------------------------------------------------------
diff --git a/be/src/util/process-state-info.cc b/be/src/util/process-state-info.cc
index c156b5c..7b8ae91 100644
--- a/be/src/util/process-state-info.cc
+++ b/be/src/util/process-state-info.cc
@@ -24,6 +24,7 @@
 #include <fstream>
 #include <sstream>
 #include <boost/algorithm/string.hpp>
+#include <dirent.h>
 #include <gutil/strings/substitute.h>
 
 #include "util/pretty-printer.h"
@@ -37,6 +38,7 @@ using boost::algorithm::join;
 using boost::algorithm::split;
 using boost::algorithm::token_compress_on;
 using boost::algorithm::trim;
+using std::to_string;
 
 using namespace strings;
 
@@ -83,8 +85,7 @@ int64_t ProcessStateInfo::GetBytes(const string& state_key) const {
 }
 
 void ProcessStateInfo::ReadProcIO() {
-  const string& path = Substitute("/proc/$0/io", getpid());
-  ifstream ioinfo(path.c_str(), ios::in);
+  ifstream ioinfo("/proc/self/io", ios::in);
   string line;
   while (ioinfo.good() && !ioinfo.eof()) {
     getline(ioinfo, line);
@@ -99,8 +100,7 @@ void ProcessStateInfo::ReadProcIO() {
 }
 
 void ProcessStateInfo::ReadProcCgroup() {
-  const string& path = Substitute("/proc/$0/cgroup", getpid());
-  ifstream cgroupinfo(path.c_str(), ios::in);
+  ifstream cgroupinfo("/proc/self/cgroup", ios::in);
   string line;
   while (cgroupinfo.good() && !cgroupinfo.eof()) {
     getline(cgroupinfo, line);
@@ -117,8 +117,7 @@ void ProcessStateInfo::ReadProcCgroup() {
 }
 
 void ProcessStateInfo::ReadProcSched() {
-  const string& path = Substitute("/proc/$0/sched", getpid());
-  ifstream schedinfo(path.c_str(), ios::in);
+  ifstream schedinfo("/proc/self/sched", ios::in);
   string line;
   while (schedinfo.good() && !schedinfo.eof()) {
     getline(schedinfo, line);
@@ -134,8 +133,7 @@ void ProcessStateInfo::ReadProcSched() {
 }
 
 void ProcessStateInfo::ReadProcStatus() {
-  const string& path = Substitute("/proc/$0/status", getpid());
-  ifstream statusinfo(path.c_str(), ios::in);
+  ifstream statusinfo("/proc/self/status", ios::in);
   string line;
   while (statusinfo.good() && !statusinfo.eof()) {
     getline(statusinfo, line);
@@ -150,25 +148,21 @@ void ProcessStateInfo::ReadProcStatus() {
   if (statusinfo.is_open()) statusinfo.close();
 }
 
-void ProcessStateInfo::ReadProcFileDescriptorInfo() {
-  const string& command = Substitute("ls -l /proc/$0/fd | awk '{print $$(NF-2), $$NF}'",
-      getpid());
-  FILE* fp = popen(command.c_str(), "r");
-  if (fp) {
-    int max_buffer = 1024;
-    char buf[max_buffer];
-    while (!feof(fp)) {
-      if (fgets(buf, max_buffer, fp) != NULL) {
-        string line;
-        line.append(buf);
-        vector<string> fields;
-        split(fields, line, is_any_of(" "), token_compress_on);
-        if (fields.size() < 2) continue;
-        fd_desc_[atoi(fields[0].c_str())] = fields[1];
-      }
+void ProcessStateInfo::ReadProcFileDescriptorCount() {
+  int fd_count = 0;
+  DIR *dir_stream = opendir("/proc/self/fd");
+  if (dir_stream != nullptr)
+  {
+    dirent *dir_entry;
+    // readdir() is not thread-safe according to its man page, but in glibc
+    // calling readdir() on different directory streams is thread-safe.
+    // see: www.gnu.org/software/libc/manual/html_node/Reading_002fClosing-Directory.html
+    while ((dir_entry = readdir(dir_stream)) != nullptr) {
+      if(dir_entry->d_name[0] != '.') ++fd_count; // . and .. do not count
     }
-    pclose(fp);
+    closedir(dir_stream);
   }
+  process_state_map_["fd/count"] = to_string(fd_count);
 }
 
 ProcessStateInfo::ProcessStateInfo() {
@@ -176,7 +170,7 @@ ProcessStateInfo::ProcessStateInfo() {
   ReadProcCgroup();
   ReadProcSched();
   ReadProcStatus();
-  ReadProcFileDescriptorInfo();
+  ReadProcFileDescriptorCount();
 }
 
 string ProcessStateInfo::DebugString() const {
@@ -239,7 +233,7 @@ string ProcessStateInfo::DebugString() const {
          << "    Cpus Allowed List: " << GetString("status/Cpus_allowed_list") << endl
          << "    Mems Allowed List: " << GetString("status/Mems_allowed_list") << endl
          << "  File Descriptors: " << endl
-         << "    Number of File Descriptors: " << fd_desc_.size() << endl;
+         << "    Number of File Descriptors: " << GetInt("fd/count") << endl;
   stream << endl;
   return stream.str();
 }

http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/baa537f8/be/src/util/process-state-info.h
----------------------------------------------------------------------
diff --git a/be/src/util/process-state-info.h b/be/src/util/process-state-info.h
index 7b96313..63b6559 100644
--- a/be/src/util/process-state-info.h
+++ b/be/src/util/process-state-info.h
@@ -29,7 +29,7 @@ namespace impala {
 /// ProcessStateInfo is an interface to query for process state information
 /// at runtime. This contains information about I/O, Cgroup, Scheduler,
 /// Process status, as well as the file descriptors that belong to the process.
-/// Below are some of the I/O information will be read from /proc/pid/io.
+/// Below are some of the I/O information will be read from /proc/self/io.
 /// io/rchar[int64]: Number of bytes which this task has caused to be read from storage.
 /// io/wchar[int64]: Number of bytes which this task has caused to be written to storage.
 /// io/syscr[int64]: Number of read I/O operations.
@@ -39,13 +39,13 @@ namespace impala {
 /// io/cancelled_write_bytes[int64]: Number of bytes which this process did not write,
 /// by truncating pagecache.
 
-/// Below are some of the Cgroup information that will be read from /proc/pid/cgroup.
+/// Below are some of the Cgroup information that will be read from /proc/self/cgroup.
 /// cgroup/hierarchy_id[int]: Hierarchy ID number.
 /// cgroup/subsystems[string]: Set of subsystems bound to the hierarchy.
 /// cgroup/control_group[string]: Control group in the hierarchy to which the
 /// process belongs.
 
-/// Below are some of the Scheduler information that will be read from /proc/pid/sched.
+/// Below are some of the Scheduler information that will be read from /proc/self/sched.
 /// sched/se.sum_exec_runtime[string]: Sum of execute time.
 /// sched/se.statistics.wait_max[string]: Max wait time in ready queue.
 /// sched/se.statistics.wait_sum[string]: Sum of wait time in ready queue.
@@ -58,7 +58,8 @@ namespace impala {
 /// sched/nr_involuntary_switches[int]: Number of involuntary context switches.
 /// sched/prio[int]: The process priority.
 
-/// Below are some of the Process status information that will be read from /proc/pid/status.
+/// Below are some of the Process status information that will be read
+/// from /proc/self/status.
 /// status/Threads[int]: Number of threads in process.
 /// status/VmPeak[string]: Peak virtual memory size.
 /// status/VmSize[string]: Virtual memory size.
@@ -74,9 +75,8 @@ namespace impala {
 /// status/Cpus_allowed_list[string]: List of CPUs on which this process may run.
 /// status/Mems_allowed_list[string]: Memory nodes allowed to this process.
 
-/// File Descriptors information will be read from /proc/pid/fd.
-/// The number of files the process has open.
-/// The description info for each file.
+/// File Descriptors information will be read from /proc/self/fd.
+/// fd/count[int]: The number of files the process has open.
 
 class ProcessStateInfo {
  public:
@@ -96,24 +96,20 @@ class ProcessStateInfo {
   typedef std::map<std::string, std::string> ProcessStateMap;
   ProcessStateMap process_state_map_;
 
-  /// The description info for each file.
-  typedef std::map<int, std::string> FileDescriptorMap;
-  FileDescriptorMap fd_desc_;
-
-  /// Read I/O info from /proc/<pid>/io.
+  /// Read I/O info from /proc/self/io.
   void ReadProcIO();
 
-  /// Read cgroup info from /proc/<pid>/cgroup.
+  /// Read cgroup info from /proc/self/cgroup.
   void ReadProcCgroup();
 
-  /// Read schedule info from /proc/<pid>/sched.
+  /// Read schedule info from /proc/self/sched.
   void ReadProcSched();
 
-  /// Read status from /proc/<pid>/status.
+  /// Read status from /proc/self/status.
   void ReadProcStatus();
 
-  /// Read file descriptors belong the process from /proc/<pid>/fd.
-  void ReadProcFileDescriptorInfo();
+  /// Read the number of currently open file descriptors from /proc/self/fd.
+  void ReadProcFileDescriptorCount();
 };
 
 }