Repository: mesos
Updated Branches:
  refs/heads/master 1b70debfd -> 867ff24ba


Added cpuacct subsystem to cgroups.

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


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

Branch: refs/heads/master
Commit: 9a5eaca9e3ab7ac01ae5f884b32e8c570da4f799
Parents: 1b70deb
Author: Jojy Varghese <[email protected]>
Authored: Tue Jul 14 17:10:03 2015 -0700
Committer: Timothy Chen <[email protected]>
Committed: Tue Jul 14 17:57:50 2015 -0700

----------------------------------------------------------------------
 src/linux/cgroups.cpp       | 51 ++++++++++++++++++++++++++++++++++++++++
 src/linux/cgroups.hpp       | 35 +++++++++++++++++++++++++++
 src/tests/cgroups_tests.cpp | 14 +++++++++++
 3 files changed, 100 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/9a5eaca9/src/linux/cgroups.cpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.cpp b/src/linux/cgroups.cpp
index 4c006d0..b7d11ac 100644
--- a/src/linux/cgroups.cpp
+++ b/src/linux/cgroups.cpp
@@ -1991,6 +1991,57 @@ Try<Nothing> cfs_quota_us(
 
 } // namespace cpu {
 
+namespace cpuacct {
+
+Result<string> cgroup(pid_t pid)
+{
+  return internal::cgroup(pid, "cpuacct");
+}
+
+
+Try<Stats> stat(
+    const string& hierarchy,
+    const string& cgroup)
+{
+  const Try<hashmap<string, uint64_t>> stats =
+    cgroups::stat(hierarchy, cgroup, "cpuacct.stat");
+
+  if (!stats.isSome()) {
+    return Error(stats.error());
+  }
+
+  if (!stats.get().contains("user") || !stats.get().contains("system")) {
+    return Error("Failed to get user/system value from cpuacct.stat");
+  }
+
+  // Get user ticks per second. This value is constant for the lifetime of a
+  // process.
+  // TODO(Jojy): Move system constants to a separate compilation unit.
+  static long userTicks = sysconf(_SC_CLK_TCK);
+  if (userTicks <= 0) {
+    return ErrnoError("Failed to get _SC_CLK_TCK");
+  }
+
+  Try<Duration> user =
+    Duration::create((double) stats.get().at("user") / userTicks);
+
+  if (user.isError()) {
+    return Error(
+        "Failed to convert user ticks to Duration: " + user.error());
+  }
+
+  Try<Duration> system =
+    Duration::create((double) stats.get().at("system") / userTicks);
+
+  if (system.isError()) {
+    return Error(
+        "Failed to convert system ticks to Duration: " + system.error());
+  }
+
+  return Stats({user.get(), system.get()});
+}
+
+} // namespace cpuacct {
 
 namespace memory {
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/9a5eaca9/src/linux/cgroups.hpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.hpp b/src/linux/cgroups.hpp
index 73b9831..a47f9a2 100644
--- a/src/linux/cgroups.hpp
+++ b/src/linux/cgroups.hpp
@@ -432,6 +432,41 @@ Try<Nothing> cfs_quota_us(
 } // namespace cpu {
 
 
+// Cpuacct subsystem.
+namespace cpuacct {
+
+// Returns the cgroup that the specified pid is a member of within the
+// hierarchy that the 'cpuacct' subsytem is mounted or None if the
+// subsystem is not mounted or the pid is not a member of a cgroup.
+//
+// @param   pid   process id for which cgroup is queried within the cpuacct
+//                subsytem.
+// @return  Some cgroup in case there was a valid cgroup found for the pid.
+//          Error if there was any error in processing.
+Result<std::string> cgroup(pid_t pid);
+
+
+// Encapsulates the 'stat' information exposed by the cpuacct subsystem.
+struct Stats
+{
+  const Duration user;
+  const Duration system;
+};
+
+
+// Returns 'Stats' for a given hierarchy and cgroup.
+//
+// @param   hierarchy   hierarchy for the 'cpuacct' subsystem.
+// @param   cgroup      cgroup for a given process.
+// @return  Some<Stats> if sucessful.
+//          Error in case of any error during processing.
+Try<Stats> stat(
+    const std::string& hierarchy,
+    const std::string& cgroup);
+
+} // namespace cpuacct {
+
+
 // Memory controls.
 namespace memory {
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/9a5eaca9/src/tests/cgroups_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/cgroups_tests.cpp b/src/tests/cgroups_tests.cpp
index 475f48a..af4b37b 100644
--- a/src/tests/cgroups_tests.cpp
+++ b/src/tests/cgroups_tests.cpp
@@ -1186,6 +1186,20 @@ TEST_F(CgroupsAnyHierarchyMemoryPressureTest, 
ROOT_IncreasePageCache)
   EXPECT_LT(0u, low);
 }
 
+// Tests the cpuacct::stat API. This test just tests for ANY value returned by
+// the API.
+TEST_F(CgroupsAnyHierarchyWithCpuAcctMemoryTest, ROOT_CGROUPS_CpuAcctsStats)
+{
+  const std::string hierarchy = path::join(baseHierarchy, "cpuacct");
+  ASSERT_SOME(cgroups::create(hierarchy, TEST_CGROUPS_ROOT));
+
+  CHECK_SOME(cgroups::assign(hierarchy, TEST_CGROUPS_ROOT, ::getpid()));
+
+  ASSERT_SOME(cgroups::cpuacct::stat(hierarchy, TEST_CGROUPS_ROOT));
+
+  AWAIT_READY(cgroups::destroy(hierarchy, TEST_CGROUPS_ROOT));
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {

Reply via email to