Add cpu module. 90/16690/1
authorPrzemyslaw Kedzierski <p.kedzierski@samsung.com>
Mon, 10 Feb 2014 10:19:00 +0000 (11:19 +0100)
committerPrzemyslaw Kedzierski <p.kedzierski@samsung.com>
Fri, 21 Feb 2014 13:30:14 +0000 (14:30 +0100)
The cpu module is used for moving processes and tasks
between cgroups.

Change-Id: I7fd296c41aac74951d62d77c50bd343ebc43b986
Signed-off-by: Przemyslaw Kedzierski <p.kedzierski@samsung.com>
CMakeLists/init-needed-environment.txt
CMakeLists/resourced.txt
packaging/resourced-cpucgroup.service [new file with mode: 0644]
packaging/resourced.spec
scripts/resourced-cpucgroup.sh [new file with mode: 0644]
src/cpu/cpu.c [new file with mode: 0644]
src/cpu/cpu.conf [new file with mode: 0644]

index b2878de..c13e78b 100644 (file)
@@ -17,6 +17,9 @@ PROJECT(${fw_name})
 IF(NOT DEFINED MEMORY_MODULE)
   SET(MEMORY_MODULE "ON")
 ENDIF()
+IF(NOT DEFINED CPU_MODULE)
+  SET(CPU_MODULE "ON")
+ENDIF()
 IF(NOT DEFINED POWERTOP_MODULE)
   SET(POWERTOP_MODULE "OFF")
 ENDIF()
@@ -93,6 +96,7 @@ SET(DATAUSAGE_SOURCE_DIR        ${SOURCE_DIR}/datausage)
 SET(CGROUP_SOURCE_DIR           ${SOURCE_DIR}/cgroup)
 SET(APP-STAT_SOURCE_DIR         ${SOURCE_DIR}/app-stat)
 SET(COMMON_SOURCE_DIR           ${SOURCE_DIR}/common)
+SET(CPU_SOURCE_DIR              ${SOURCE_DIR}/cpu)
 IF("${POWERTOP_MODULE}" STREQUAL "ON")
 SET(POWERTOP-WRAPPER_SOURCE_DIR           ${SOURCE_DIR}/powertop-wrapper)
 ENDIF()
index cb1cb20..15b4135 100644 (file)
@@ -64,6 +64,12 @@ ELSEIF("${MEMORY_MODULE}" STREQUAL "OFF")
     )
 ENDIF()
 
+IF("${CPU_MODULE}" STREQUAL "ON")
+  SET(SOURCES ${SOURCES}
+    ${CPU_SOURCE_DIR}/cpu.c
+    )
+ENDIF()
+
 SET (REQUIRES_LIST ${REQUIRES_LIST}
        ecore
        dlog
@@ -105,6 +111,13 @@ ELSE()
     DESTINATION /etc/resourced RENAME memory.conf)
 ENDIF()
 
+IF("${CPU_MODULE}" STREQUAL "ON")
+  INSTALL(FILES ${CPU_SOURCE_DIR}/cpu.conf
+    DESTINATION /etc/resourced RENAME cpu.conf)
+ELSE()
+  INSTALL(PROGRAMS ${CMAKE_BINARY_DIR}/scripts/resourced-cpucgroup.sh DESTINATION bin)
+ENDIF()
+
 INSTALL(FILES ${MEMORY_SOURCE_DIR}/memory.conf DESTINATION /etc/resourced)
 
 INSTALL(FILES ${INCLUDE_PUBLIC_DIR}/resourced.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/system)
diff --git a/packaging/resourced-cpucgroup.service b/packaging/resourced-cpucgroup.service
new file mode 100644 (file)
index 0000000..8db5ad7
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=make cpucgroup
+After=graphical.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/resourced-cpucgroup.sh
+
+[Install]
+WantedBy=graphical.target
index 6a195ea..c7b8b93 100644 (file)
@@ -8,6 +8,7 @@ Source0:    %{name}-%{version}.tar.gz
 Source1:    resourced.service
 
 %define powertop_state ON
+%define cpu_module ON
 %define exclude_list_opt_full_path /opt/usr/etc/_exclude_list_file_name_
 
 
@@ -68,7 +69,8 @@ echo "\
 
 cmake . -DCMAKE_INSTALL_PREFIX=/usr -DFULLVER=%{version} -DMAJORVER=${MAJORVER} -DCMAKE_BUILD_TYPE=Release \
        -DEXCLUDE_LIST_OPT_FULL_PATH=%{exclude_list_opt_full_path} \
-       -DPOWERTOP_MODULE=%{powertop_state}
+       -DPOWERTOP_MODULE=%{powertop_state} \
+       -DCPU_MODULE=%{cpu_module}
 
 make %{?jobs:-j%jobs}
 
@@ -129,6 +131,11 @@ fi
 /usr/share/license/%{name}-powertop-wrapper
 %endif
 
+#memps
+%attr(-,root, root) %{_bindir}/memps
+
+%config  /etc/resourced/cpu.conf
+
 %files devel
 %{_libdir}/pkgconfig/*.pc
 %{_includedir}/system/proc_stat.h
diff --git a/scripts/resourced-cpucgroup.sh b/scripts/resourced-cpucgroup.sh
new file mode 100644 (file)
index 0000000..053279f
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+CPU_CGROUP="/sys/fs/cgroup/cpu/background"
+CPU_SHARE="50"
+CPU_CONTROL_LIST="indicator net-config"
+
+/bin/mkdir -pm 755 $CPU_CGROUP
+echo $CPU_SHARE > $CPU_CGROUP/cpu.shares
+for list in $CPU_CONTROL_LIST; do
+        pid=`/usr/bin/pgrep $list`
+        if [ "z${pid}" != "z" ]; then
+                echo $pid > $CPU_CGROUP/cgroup.procs
+        fi
+done
+
diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
new file mode 100644 (file)
index 0000000..ce56851
--- /dev/null
@@ -0,0 +1,150 @@
+/*
+ * resourced
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file cpu.c
+ *
+ * @desc cpu module
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ */
+#include <dirent.h>
+#include <errno.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include "cpu-common.h"
+#include "proc-main.h"
+#include "proc-process.h"
+#include "macro.h"
+#include "module.h"
+#include "module-data.h"
+#include "resourced.h"
+#include "trace.h"
+#include "vconf.h"
+#include "cgroup.h"
+#include "config-parser.h"
+#include "const.h"
+
+#include <fcntl.h>               /*mkdirat */
+#include <sys/stat.h>            /*mkdirat */
+#include <stdlib.h>
+
+#define CPU_DEFAULT_CGROUP "/sys/fs/cgroup/cpu"
+#define CPU_CONTROL_GROUP "/sys/fs/cgroup/cpu/background"
+#define CPU_CONF_FILE                  "/etc/resourced/cpu.conf"
+#define CPU_CONF_SECTION       "CONTROL"
+#define CPU_CONF_PREDEFINE     "PREDEFINE"
+#define CPU_SHARE      "/cpu.shares"
+
+static int make_cpu_cgroup(char* path)
+{
+       DIR *dir = 0;
+       int fd;
+       dir = opendir(CPU_DEFAULT_CGROUP);
+       if (!dir) {
+               _E("cpu cgroup doesn't exit : %d", errno);
+               return RESOURCED_ERROR_UNINITIALIZED;
+       }
+       fd = dirfd(dir);
+       if (fd < 0) {
+               _E("fail to get fd about cpu cgroup : %d", errno);
+               closedir(dir);
+               return RESOURCED_ERROR_UNINITIALIZED;
+       }
+       if (mkdirat(fd, path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IXOTH) < 0) {
+               _E("fail to get fd about cpu cgroup : %d", errno);
+               closedir(dir);
+               return RESOURCED_ERROR_UNINITIALIZED;
+       }
+       closedir(dir);
+       return RESOURCED_ERROR_NONE;
+}
+
+static int cpu_move_cgroup(pid_t pid, char *path)\r
+{
+       return cgroup_write_node(path, CGROUP_FILE_NAME, pid);
+}
+
+static int load_cpu_config(struct parse_result *result, void *user_data)
+{
+       pid_t pid = 0, value;
+       if (!result)
+               return -EINVAL;
+
+       if (strcmp(result->section, CPU_CONF_SECTION))
+               return RESOURCED_ERROR_NO_DATA;
+       if (!strcmp(result->name, CPU_CONF_PREDEFINE)) {
+               pid = find_pid_from_cmdline(result->value);
+               if (pid > 0)
+                       cpu_move_cgroup(pid, CPU_CONTROL_GROUP);
+       } else if (!strcmp(result->name, "CPU_SHARE")) {
+               value = atoi(result->value);
+               if (value)
+                       cgroup_write_node(CPU_CONTROL_GROUP, CPU_SHARE, value);
+       }
+       return RESOURCED_ERROR_NONE;
+}
+
+static int resourced_cpu_control(void *data)
+{
+       struct cpu_data_type *c_data;
+       int ret = RESOURCED_ERROR_NONE;
+       pid_t pid;
+       c_data = (struct cpu_data_type *)data;
+       if (!c_data)
+               return RESOURCED_ERROR_NO_DATA;
+       pid = c_data->pid;
+       _D("resourced_cpu_control : type = %d, pid = %d", c_data->control_type, pid);
+       switch(c_data->control_type) {
+       case CPU_SET_LAUNCH:
+       case CPU_SET_FOREGROUND:
+               ret = cpu_move_cgroup(pid, CPU_DEFAULT_CGROUP);
+               break;
+       case CPU_SET_BACKGROUND:
+               ret = cpu_move_cgroup(pid, CPU_CONTROL_GROUP);
+               break;
+       }
+       return ret;
+}
+
+static int resourced_cpu_init(void *data)
+{
+       int ret_code;
+
+       _D("resourced_cpu_init");
+       ret_code = make_cpu_cgroup(CPU_CONTROL_GROUP);
+       ret_value_msg_if(ret_code < 0, ret_code, "cpu init failed\n");
+       config_parse(CPU_CONF_FILE, load_cpu_config, NULL);
+       return RESOURCED_ERROR_NONE;
+}
+
+static int resourced_cpu_finalize(void *data)
+{
+       return RESOURCED_ERROR_NONE;
+}
+
+static struct module_ops cpu_modules_ops = {
+       .priority = MODULE_PRIORITY_NORMAL,
+       .name = "cpu",
+       .init = resourced_cpu_init,
+       .exit = resourced_cpu_finalize,
+       .control = resourced_cpu_control,
+};
+
+MODULE_REGISTER(&cpu_modules_ops)
diff --git a/src/cpu/cpu.conf b/src/cpu/cpu.conf
new file mode 100644 (file)
index 0000000..ee2f6f8
--- /dev/null
@@ -0,0 +1,7 @@
+[CONTROL]
+# predefined process list
+PREDEFINE=indicator
+PREDEFINE=net-config
+
+# background cpu-share
+CPU_SHARE=50