proc-manager : add proc-manager module 44/180344/10
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 28 May 2018 06:37:46 +0000 (15:37 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Tue, 7 Aug 2018 06:00:15 +0000 (15:00 +0900)
- Detect changes in app's life cycle and take appropriate action
- In current,
  - Detect app launching
  - Set oom_score_adj if osa_predefined app(defined in config file)

Change-Id: I340764c4d94e82b98c1ffeb6c051be610b14ffb6
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
CMakeLists.txt
cfg/proc-manager.conf [new file with mode: 0644]
packaging/resourced-headless.conf
packaging/resourced-headless.spec
src/proc-manager/proc-manager-common.h [new file with mode: 0644]
src/proc-manager/proc-manager-dbus.c [new file with mode: 0644]
src/proc-manager/proc-manager-dbus.h [new file with mode: 0644]
src/proc-manager/proc-manager.c [new file with mode: 0644]
src/proc-manager/resourced-headless-proc-manager.manifest [new file with mode: 0644]
src/proc-usage/proc-usage-dbus.h

index b76ed34f57bc03efebce5c8ce4d3059030e90c98..4dfff2156d4b8bd40a519616ea77d15c5180aa43 100644 (file)
@@ -67,15 +67,20 @@ INCLUDE_DIRECTORIES(${${COMMON_LIBRARY}_DIR})
 
 BUILD_MODULE(${PKGNAME} "daemon" "binary")
 
-IF("${PROC_USAGE_MODULE}" STREQUAL "ON")
-       BUILD_MODULE("${PKGNAME}-proc-usage" "proc-usage" "library")
-ENDIF()
-
 IF("${MEMORY_MODULE}" STREQUAL "ON")
        BUILD_MODULE("${PKGNAME}-memory" "memory" "library")
        ADD_CONFIG("memory")
 ENDIF()
 
+IF("${PROC_MANAGER_MODULE}" STREQUAL "ON")
+       BUILD_MODULE("${PKGNAME}-proc-manager" "proc-manager" "library")
+       ADD_CONFIG("proc-manager")
+ENDIF()
+
+IF("${PROC_USAGE_MODULE}" STREQUAL "ON")
+       BUILD_MODULE("${PKGNAME}-proc-usage" "proc-usage" "library")
+ENDIF()
+
 IF("${TEST_MODULE}" STREQUAL "ON")
        BUILD_MODULE("${PKGNAME}-test" "test" "library")
 ENDIF()
diff --git a/cfg/proc-manager.conf b/cfg/proc-manager.conf
new file mode 100644 (file)
index 0000000..08ac0f6
--- /dev/null
@@ -0,0 +1,4 @@
+[proc-manager.oom_score_predefined_app]
+# After app is launched, set it's oom_score_adj to SCORE
+# -1000 <= SCORE <= 1000
+# APPID=SCORE
index ac2ab977ab6c64dc92e69e4698cb7b9b03e200d0..b1776b33093253e7ca649d8cf6613a4e4b0ba0b7 100644 (file)
@@ -6,6 +6,10 @@
         <allow send_destination="org.tizen.resourced"/>
     </policy>
 
+    <policy user="app_fw">
+        <allow send_destinaion="org.tizen.resourced"/>
+    </policy>
+
     <policy context="default">
         <deny send_destination="org.tizen.resourced"/>
                <check send_destination="org.tizen.resourced"
index 97c1e112e24fd3d7a13d77cbe67e4eb737c72aeb..273a0e759c747de4cdc70cec4a45c2af00b75456 100644 (file)
@@ -9,6 +9,7 @@ Source101:  %{name}.service
 Source102:  %{name}.conf
 
 %define memory_module ON
+%define proc_manager_module ON
 %define proc_usage_module ON
 %define test_module    ON
 
@@ -40,6 +41,15 @@ Requires:   %{name}-common = %{version}-%{release}
 Control memory status to prevent out of memory
 %endif
 
+%if %{proc_manager_module} == ON
+%package proc-manager
+Summary:       App life cycle monitor
+Requires:   %{name} = %{version}-%{release}
+Requires:   %{name}-common = %{version}-%{release}
+%description proc-manager
+Detect changes in application life cycle and take appropriate action
+%endif
+
 %if %{proc_usage_module} == ON
 %package proc-usage
 Summary:       Provide process usage information
@@ -78,9 +88,10 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
        -DMAJORVER=${MAJORVER}  \
        -DLIB_DIR=%{_libdir}    \
        -DCONFIG_INSTALL_DIR=%{config_install_dir}      \
-       -DPROC_USAGE_MODULE=%{proc_usage_module}        \
        -DMEMORY_MODULE=%{memory_module}        \
-       -DTEST_MODULE=%{test_module}    \
+       -DPROC_MANAGER_MODULE=%{proc_manager_module}    \
+       -DPROC_USAGE_MODULE=%{proc_usage_module}        \
+       -DTEST_MODULE=%{test_module}
 
 make %{?jobs:-j%jobs}
 
@@ -118,6 +129,14 @@ install -m 0644 %{SOURCE102} %{buildroot}%{_sysconfdir}/dbus-1/system.d/resource
 %config %{config_install_dir}/memory.conf
 %endif
 
+%if %{proc_manager_module} == ON
+%files proc-manager
+%license LICENSE
+%manifest %{name}-proc-manager.manifest
+%{_libdir}/libresourced-headless-proc-manager.so*
+%config %{config_install_dir}/proc-manager.conf
+%endif
+
 %if %{proc_usage_module} == ON
 %files proc-usage
 %license LICENSE
diff --git a/src/proc-manager/proc-manager-common.h b/src/proc-manager/proc-manager-common.h
new file mode 100644 (file)
index 0000000..382dfc4
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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  proc-manager-common.h
+ * @brief  Shared contents in the proc-manager module
+ */
+
+#ifndef __RESOURCED_HEADLESS_PROC_MANAGER_COMMON_H__
+#define __RESOURCED_HEADLESS_PROC_MANAGER_COMMON_H__
+
+#include <macro.h>
+
+/**
+ * @brief  Config file related macro
+ */
+#define PROC_MANAGER_CONFIG_FILE_NAME     "proc-manager"
+
+#define PROC_MANAGER_GROUP                "proc-manager"
+#define PROC_MANAGER_OSA_PREDEFINED_APP_GROUP  PROC_MANAGER_GROUP".oom_score_predefined_app"
+
+/**
+ * @details  Key(appid), Value(predefined oom_score_adj)
+ */
+GHashTable *proc_manager_osa_predefined_app_list;
+
+#endif /* __RESOURCED_HEADLESS_PROC_MANAGER_COMMON_H__ */
diff --git a/src/proc-manager/proc-manager-dbus.c b/src/proc-manager/proc-manager-dbus.c
new file mode 100644 (file)
index 0000000..8287f71
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include <gio/gio.h>
+#include <glib.h>
+
+#include <dbus.h>
+#include <event.h>
+#include <log.h>
+#include <macro.h>
+#include <procfs.h>
+
+#include "proc-manager-common.h"
+#include "proc-manager-dbus.h"
+
+/**
+ * @brief  D-Bus signal handler for AppLaunch
+ */
+static void proc_manager_dbus_app_launch(GVariant *params)
+{
+       int ret;
+       int *predefined_osa = NULL;
+       pid_t pid = 0;
+       char *appid = NULL;
+       char *pkgid = NULL;
+       char *pkgtype = NULL;
+
+       g_variant_get(params, "(i&s&s&s)", &pid, &appid, &pkgid, &pkgtype);
+       if (pid == 0 || !appid || !pkgid || !pkgtype) {
+               _D("Receive invalid D-Bus message. Ignore it");
+               return;
+       }
+
+       _D("App launched : pid(%u), appid(%s), pkgid(%s), pkgtype(%s)",
+                       pid, appid, pkgid, pkgtype);
+
+       predefined_osa = (int *)g_hash_table_lookup(proc_manager_osa_predefined_app_list, (gpointer)appid);
+       if (predefined_osa) {
+               _D("%s is oom_score_adj predefined app. Set to %d", appid, *predefined_osa);
+               ret = procfs_set_oom_score_adj(pid, *predefined_osa);
+               if (ret < 0)
+                       _E("Failed to set oom_score_adj for PID %u (%d)", pid, ret);
+       }
+}
+
+static struct dbus_signal proc_manager_dbus_signals[] = {
+       { "AppLaunch", proc_manager_dbus_app_launch },
+};
+
+int proc_manager_dbus_init(void)
+{
+       int ret;
+
+       ret = dbus_register_signals(AUL_DBUS_PATH_APPSTATUS, AUL_DBUS_INTERFACE_APPSTATUS,
+                       proc_manager_dbus_signals, ARRAY_SIZE(proc_manager_dbus_signals));
+       if (ret < 0) {
+               _E("Failed to register proc-manager D-Bus signals (%d)", ret);
+               return ret;
+       }
+
+       return 0;
+}
diff --git a/src/proc-manager/proc-manager-dbus.h b/src/proc-manager/proc-manager-dbus.h
new file mode 100644 (file)
index 0000000..79e96d2
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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  proc-manager-dbus.h
+ * @brief  D-Bus handlers for proc-manager module
+ */
+
+#ifndef __RESOURCED_HEADLESS_PROC_MANAGER_DBUS_H__
+#define __RESOURCED_HEADLESS_PROC_MANAGER_DBUS_H__
+
+#define AUL_DBUS_PATH_APPSTATUS       "/Org/Tizen/Aul/AppStatus"
+#define AUL_DBUS_INTERFACE_APPSTATUS  "org.tizen.aul.AppStatus"
+
+int proc_manager_dbus_init(void);
+
+#endif /* __RESOURCED_HEADLESS_PROC_MANAGER_DBUS_H__ */
diff --git a/src/proc-manager/proc-manager.c b/src/proc-manager/proc-manager.c
new file mode 100644 (file)
index 0000000..91f02f3
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include <stdlib.h>
+
+#include <config-parser.h>
+#include <log.h>
+#include <macro.h>
+#include <module.h>
+#include <procfs.h>
+
+#include "proc-manager-common.h"
+#include "proc-manager-dbus.h"
+
+/**
+ * @brief  Make a hashtable about oom_score_adj predefined app
+ */
+static void proc_manager_make_predefined_osa_app_list(const char *key, const void *value)
+{
+       int tmp;
+       gint *predefined_osa;
+       gchar *appid;
+
+       tmp = atoi((const char *)value);
+
+       if (tmp < OOM_SCORE_ADJ_MIN || tmp > OOM_SCORE_ADJ_MAX) {
+               _E("%s : NOT included (invalid value %d)", key, tmp);
+               return;
+       }
+
+       appid = g_strndup(key, strlen(key));
+       predefined_osa = g_malloc(sizeof(gint));
+       *predefined_osa = tmp;
+       g_assert(appid && predefined_osa);
+
+       g_hash_table_insert(proc_manager_osa_predefined_app_list, appid, predefined_osa);
+       _D("%s : included (%d)", appid, *predefined_osa);
+}
+
+/**
+ * @brief  Read proc-manager config file and configure this module
+ * @return  0 on success, otherwise a negative error value
+ */
+static int proc_manager_read_config(void)
+{
+       int ret;
+
+       /* Read config file */
+       ret = config_parser_open(PROC_MANAGER_CONFIG_FILE_NAME);
+       if (ret < 0) {
+               _E("Failed to open proc-manager config file (%d)", ret);
+               return ret;
+       }
+
+       /* Make predefined oom_score_adj app list */
+       proc_manager_osa_predefined_app_list = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+
+       _D("========== Predefined oom_score_adj app list ==========");
+       config_parser_foreach(PROC_MANAGER_CONFIG_FILE_NAME, PROC_MANAGER_OSA_PREDEFINED_APP_GROUP,
+                       proc_manager_make_predefined_osa_app_list);
+       _D("==========           Total %d app(s)           ==========", g_hash_table_size(proc_manager_osa_predefined_app_list));
+
+       return 0;
+}
+
+static int __INIT__ proc_manager_init(void)
+{
+       int ret;
+
+       ret = proc_manager_read_config();
+       if (ret < 0) {
+               _E("Failed to read config file (%d)", ret);
+               return ret;
+       }
+
+       ret = proc_manager_dbus_init();
+       if (ret < 0) {
+               _E("Failed to initialize proc-manager-dbus (%d)", ret);
+               g_hash_table_destroy(proc_manager_osa_predefined_app_list);
+               return ret;
+       }
+
+       return 0;
+}
+
+static int __EXIT__ proc_manager_exit(void)
+{
+       if (proc_manager_osa_predefined_app_list)
+               g_hash_table_destroy(proc_manager_osa_predefined_app_list);
+
+       config_parser_close(PROC_MANAGER_CONFIG_FILE_NAME);
+
+       return 0;
+}
+
+static struct module proc_manager_module = {
+       .name = "proc-manager",
+       .priority = MODULE_PRIORITY_HIGH,
+       .init = proc_manager_init,
+       .exit = proc_manager_exit,
+       .event_handler = NULL,
+};
+
+MODULE_REGISTER(&proc_manager_module)
diff --git a/src/proc-manager/resourced-headless-proc-manager.manifest b/src/proc-manager/resourced-headless-proc-manager.manifest
new file mode 100644 (file)
index 0000000..a76fdba
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_" />
+       </request>
+</manifest>
index e813f16311d753b1cb8705734c31eb8803b9956b..fcc81d8f13c93378a4f3f3eb3cca15a149ec7368 100644 (file)
@@ -27,9 +27,6 @@
 #define RDHL_DBUS_PATH_PROC                    RDHL_DBUS_PATH(Process)
 #define RDHL_DBUS_INTERFACE_PROC       RDHL_DBUS_INTERFACE(process)
 
-#define AUL_DBUS_PATH_APPSTATUS                        "org.tizen.aul.AppStatus"
-#define AUL_DBUS_INTERFACE_APPSTATUS   "/Org/Tizen/Aul/AppStatus"
-
 int proc_usage_dbus_init(void);
 
 #endif /* __RESOURCED_HEADLESS_PROC_USAGE_DBUS_H__ */