lxcpp: cgroups API 08/49208/5
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Thu, 8 Oct 2015 12:03:43 +0000 (14:03 +0200)
committerJan Olszak <j.olszak@samsung.com>
Fri, 9 Oct 2015 15:11:50 +0000 (08:11 -0700)
[Feature]       Control-groups API for containers
[Cause]         N/A
[Solution]      N/A
[Verification]  N/A

Change-Id: I69605383b40e3b3e1a8c2f6942e85023b367728e

libs/lxcpp/CMakeLists.txt
libs/lxcpp/cgroups/cgroup.cpp [new file with mode: 0644]
libs/lxcpp/cgroups/cgroup.hpp [new file with mode: 0644]
libs/lxcpp/cgroups/subsystem.hpp [new file with mode: 0644]

index 9e49843..0d35bad 100644 (file)
@@ -33,13 +33,14 @@ FILE(GLOB HEADERS_COMMANDS commands/*.hpp)
 
 FILE(GLOB SRCS          *.cpp *.hpp)
 FILE(GLOB SRCS_COMMANDS commands/*.cpp)
+FILE(GLOB SRCS_CGROUPS  cgroups/*.cpp)
 
 SET(_LIB_VERSION_ "${VERSION}")
 SET(_LIB_SOVERSION_ "0")
 SET(PC_FILE "lib${PROJECT_NAME}.pc")
 
 ## Setup target ################################################################
-ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS} ${SRCS_COMMANDS})
+ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS} ${SRCS_COMMANDS} ${SRCS_CGROUPS})
 SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
     SOVERSION   ${_LIB_SOVERSION_}
     VERSION     ${_LIB_VERSION_}
diff --git a/libs/lxcpp/cgroups/cgroup.cpp b/libs/lxcpp/cgroups/cgroup.cpp
new file mode 100644 (file)
index 0000000..574af61
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Krzysztof Dynowski (k.dynowski@samsumg.com)
+ * @brief   Control-groups management
+ */
+
+#include "lxcpp/cgroups/cgroup.hpp"
+
+// added this file now, to make hpp go through compilation
diff --git a/libs/lxcpp/cgroups/cgroup.hpp b/libs/lxcpp/cgroups/cgroup.hpp
new file mode 100644 (file)
index 0000000..b47268f
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Krzysztof Dynowski (k.dynowski@samsumg.com)
+ * @brief   Control-groups management
+ */
+
+#ifndef LXCPP_CGROUPS_CGROUP_HPP
+#define LXCPP_CGROUPS_CGROUP_HPP
+
+#include "lxcpp/cgroups/subsystem.hpp"
+
+class CGroup {
+
+public:
+    /**
+     * Define control-group object
+     */
+    CGroup(const Subsystem& subsys, const std::string& name) :
+        mSubsys(subsys),
+        mName(name)
+    {
+    }
+
+    /**
+     * Check if cgroup exists
+     * @return true if cgroup path (subsys.path / mName) exists
+     */
+    bool exists();
+
+    /**
+     * Create cgroup directory
+     * Equivalent of: mkdir subsys.path / mName
+     */
+    void create();
+
+    /**
+     * Destroy cgroup directory
+     * Equivalent of: rmdir subsys.path / mName
+     * Note: set memory.force_empty before removing a cgroup to avoid moving out-of-use page caches to parent
+     */
+    void destroy();
+
+    /**
+     * Set cgroup parameter to value (name validity depends on subsystem)
+     * Equivalent of: echo value > mSubsys_path/mName/mSubsys_name.param
+     */
+    void setValue(const std::string& param, const std::string& value);
+
+    /**
+     * Get cgroup parameter
+     * Equivalent of: cat mSubsys_path/mName/mSubsys_name.param
+     */
+    std::string getValue(const std::string& key);
+
+    /**
+     * Move process to this cgroup (process can't be removed from a cgroup)
+     * Equivalent of: echo pid > mSubsys_path/mName/tasks
+     */
+    void moveProcess(pid_t pid);
+
+private:
+    const Subsystem& mSubsys; // referred subsystem
+    const std::string& mName; // path relative to subsystem "root"
+};
+
+#endif // LXCPP_CGROUPS_CGROUP_HPP
diff --git a/libs/lxcpp/cgroups/subsystem.hpp b/libs/lxcpp/cgroups/subsystem.hpp
new file mode 100644 (file)
index 0000000..bb9887e
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Krzysztof Dynowski (k.dynowski@samsumg.com)
+ * @brief   Control-groups management
+ */
+
+#ifndef LXCPP_CGROUPS_SUBSYSTEM_HPP
+#define LXCPP_CGROUPS_SUBSYSTEM_HPP
+
+#include <sys/types.h>
+
+#include <string>
+#include <vector>
+
+class Subsystem {
+public:
+    /**
+     * Define subsystem object
+     */
+    Subsystem(const std::string& name);
+
+    /**
+     * Check if named subsystem is supported by the kernel
+     * @return true if subsystem is listed in /proc/cgroups
+     */
+    bool isAvailable();
+
+    /**
+     * Check if named subsystem is mounted (added to hierarchy)
+     * @return true if subsystem has a mount point (as read from /proc/mounts)
+     */
+    bool isAttached();
+
+    /**
+     * Attach subsystem hierarchy to filesystem
+     * Equivalent of: mount -t cgroup -o subs(coma-sep) subs(underln-sep) path
+     */
+    static void attach(const std::string& path, std::vector<std::string> subs);
+
+    /**
+     * Detach subsstem hierarchy from filesystem
+     * Equivalent of: umount path
+     */
+    static void detach(const std::string& path);
+
+    /**
+     * Get list of available subsytems
+     * @return parse contents of /proc/cgroups
+     */
+    static std::vector<std::string> availableSubsystems();
+
+    /**
+     * Get control groups list for a process (in format subsys_name:cgroup_name)
+     * eg. "cpu:/user/test_user"
+     * Equivalent of: cat /proc/pid/cgroup
+     */
+    static std::vector<std::string> getCGroups(pid_t pid);
+
+private:
+    const std::string& mName;
+    std::string mPath;
+};
+
+#endif // LXCPP_CGROUPS_SUBSYSTEM_HPP