Repository: mesos
Updated Branches:
  refs/heads/master 8952b5950 -> 46f1aebde


Added a helper for determining the cgroup of particular pid.

These helpers perform the non-trivial steps required to determine the
cgroup of a particular pid for a particular hierarchy where a
particular subsystem is attached (in this case, we've provded
implementations for the 'cpu' subsystem hierarchy and 'memory'
subsystem hierarchy, which may be the same depending on how cgroups
are mounted).

Review: https://reviews.apache.org/r/23632


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/46f1aebd
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/46f1aebd
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/46f1aebd

Branch: refs/heads/master
Commit: 46f1aebde0905cd2320754fc8a90244c3ab0daeb
Parents: 29910a6
Author: Benjamin Hindman <[email protected]>
Authored: Wed Jul 9 11:26:46 2014 -0700
Committer: Benjamin Hindman <[email protected]>
Committed: Mon Aug 4 09:15:50 2014 -0700

----------------------------------------------------------------------
 src/linux/cgroups.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++
 src/linux/cgroups.hpp | 18 ++++++++++++++++
 2 files changed, 72 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/46f1aebd/src/linux/cgroups.cpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.cpp b/src/linux/cgroups.cpp
index af6be22..ccb86cf 100644
--- a/src/linux/cgroups.cpp
+++ b/src/linux/cgroups.cpp
@@ -1783,8 +1783,56 @@ Try<hashmap<string, uint64_t> > stat(
 }
 
 
+namespace internal {
+
+// Helper for finding the cgroup of the specified pid for the
+// specified subsystem.
+Result<string> cgroup(pid_t pid, const string& subsystem)
+{
+  // Determine cgroup for hierarchy with the subsystem attached.
+  string path = path::join("/proc", stringify(pid), "cgroup");
+
+  Try<string> read = os::read(path);
+
+  if (read.isError()) {
+    return Error("Failed to read " + path + ": " + read.error());
+  }
+
+  // Now determine the cgroup by parsing each line of the output which
+  // should be of the form "N:subsystems:cgroup" where 'N' is the
+  // hierarchy number and 'subsystems' are the attached subsystems and
+  // 'cgroup' is the relative path to the cgroup from the hierarchy
+  // path.
+  Option<string> cgroup = None();
+
+  foreach (const string& line, strings::tokenize(read.get(), "\n")) {
+    vector<string> tokens = strings::tokenize(line, ":");
+
+    if (tokens.size() != 3) {
+      return Error("Unexpected format in " + path);
+    }
+
+    foreach (const string& token, strings::tokenize(tokens[1], ",")) {
+      if (subsystem == token) {
+        cgroup = tokens[2];
+      }
+    }
+  }
+
+  return cgroup;
+}
+
+} // namespace internal {
+
+
 namespace cpu {
 
+Result<string> cgroup(pid_t pid)
+{
+  return internal::cgroup(pid, "cpu");
+}
+
+
 Try<Nothing> shares(
     const string& hierarchy,
     const string& cgroup,
@@ -1842,6 +1890,12 @@ Try<Nothing> cfs_quota_us(
 
 namespace memory {
 
+Result<string> cgroup(pid_t pid)
+{
+  return internal::cgroup(pid, "memory");
+}
+
+
 Try<Bytes> limit_in_bytes(const string& hierarchy, const string& cgroup)
 {
   Try<string> read = cgroups::read(

http://git-wip-us.apache.org/repos/asf/mesos/blob/46f1aebd/src/linux/cgroups.hpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.hpp b/src/linux/cgroups.hpp
index decad9d..c571e91 100644
--- a/src/linux/cgroups.hpp
+++ b/src/linux/cgroups.hpp
@@ -380,6 +380,12 @@ Try<hashmap<std::string, uint64_t> > stat(
 // Cpu controls.
 namespace cpu {
 
+// Returns the cgroup that the specified pid is a member of within the
+// hierarchy that the 'cpu' subsytem is mounted or None if the
+// subsystem is not mounted or the pid is not a member of a cgroup.
+Result<std::string> cgroup(pid_t pid);
+
+
 // Sets the cpu shares using cpu.shares.
 Try<Nothing> shares(
     const std::string& hierarchy,
@@ -387,6 +393,12 @@ Try<Nothing> shares(
     uint64_t shares);
 
 
+// Returns the cpu shares from cpu.shares.
+Try<uint64_t> shares(
+    const std::string& hierarchy,
+    const std::string& cgroup);
+
+
 // Sets the cfs period using cpu.cfs_period_us.
 Try<Nothing> cfs_period_us(
     const std::string& hierarchy,
@@ -412,6 +424,12 @@ Try<Nothing> cfs_quota_us(
 // Memory controls.
 namespace memory {
 
+// Returns the cgroup that the specified pid is a member of within the
+// hierarchy that the 'memory' subsytem is mounted or None if the
+// subsystem is not mounted or the pid is not a member of a cgroup.
+Result<std::string> cgroup(pid_t pid);
+
+
 // Returns the memory limit from memory.limit_in_bytes.
 Try<Bytes> limit_in_bytes(
     const std::string& hierarchy,

Reply via email to