memory : notify memory level via vconf and eventsystem 52/186252/2
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 8 Aug 2018 09:21:49 +0000 (18:21 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Thu, 9 Aug 2018 02:06:31 +0000 (11:06 +0900)
Change-Id: I465df7cd32b29f92139fa96072f6afd4a2054b7e
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
CMakeLists.txt
cfg/memory.conf
packaging/resourced-headless.spec
src/common/macro.h
src/memory/memory-common.h
src/memory/memory-killer.c
src/memory/memory-notifier.c [new file with mode: 0644]
src/memory/memory-notifier.h [new file with mode: 0644]

index 4dfff2156d4b8bd40a519616ea77d15c5180aa43..0c3fb965b0e313c1a512942c04829ded237abbb6 100644 (file)
@@ -15,6 +15,14 @@ SET(PKG_MODULES
        gio-2.0
        glib-2.0
 )
+IF("${MEMORY_MODULE}" STREQUAL "ON")
+       SET(PKG_MODULES
+               ${PKG_MODULES}
+               eventsystem
+               vconf
+               vconf-internal-keys
+       )
+ENDIF()
 INCLUDE(FindPkgConfig)
 pkg_check_modules(${PKGNAME} REQUIRED ${PKG_MODULES})
 
index 88981d138d601f77d5ab0dbe72bfc24b42040682..0f0570a4872fdcb2c109e9c069fe99b6d2bb8dc8 100644 (file)
@@ -5,8 +5,13 @@
 NumMaxVictims=         2;      5;      5;       5;      10;  # MEMORY_SIZE(MB) / 200
 
 [memory-killer.threshold]
-# Medium : Threshold to start low memory killer
-# Leave : Threshold to stop low memory killer
+# Medium
+#   - Start low memory killer
+#   - Notify HARD_WARNING via vconf and eventsystem
+# Low
+#   - Notify SOFT_WARNING via eventsystem
+# Leave
+#   - Stop low memory killer
 # MEMORY_SIZE  :  ~256MB  ~512MB  ~768MB  ~1024MB  ~2048MB        UPPER
 Medium=               10;     60;     80;     100;     160  # MB  MEMORY_SIZE * 10%
 Low=                  15;     70;     90;     120;     200  # MB  MEMORY_SIZE * 12%
index 273a0e759c747de4cdc70cec4a45c2af00b75456..540f4f71d4ddec27ea6b9eed2afd5a2aa59efc73 100644 (file)
@@ -37,6 +37,9 @@ Common library for resourced-headless modules.
 Summary:    Memory handler
 Requires:   %{name} = %{version}-%{release}
 Requires:   %{name}-common = %{version}-%{release}
+BuildRequires:  pkgconfig(eventsystem)
+BuildRequires:  pkgconfig(vconf)
+BuildRequires:  pkgconfig(vconf-internal-keys)
 %description memory
 Control memory status to prevent out of memory
 %endif
index a409298f676390809123db44a530e7cf01b762b9..a313a0be7c28446c252d97a12d0acbadd13b7a1f 100644 (file)
@@ -37,7 +37,9 @@
 #define __INIT__ __attribute__ ((section(".text.init")))
 #define __EXIT__ __attribute__ ((cold))
 
+#ifndef API
 #define API __attribute__ ((visibility("default")))
+#endif
 
 /**
  * @brief  List related macro
index ac913d8c73d496b42127f59d7611de7da4950d9e..f1d5d21e57b4aa85936308aa74ad2f327b99eb36 100644 (file)
@@ -24,7 +24,7 @@
 #ifndef __RESOURCED_HEADLESS_MEMORY_COMMON_H__
 #define __RESOURCED_HEADLESS_MEMORY_COMMON_H__
 
-#include <macro.h>
+#include <event.h>
 
 #define MEMORY_CONFIG_FILE_NAME  "memory"
 
index b5ef854267867140257aabaac00c368544f2fb36..c8d9c36af9b58734525030551252fed50743a7cf 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "memory-common.h"
 #include "memory-killer.h"
+#include "memory-notifier.h"
 
 /**
  * @brief  Config file related macro
@@ -310,7 +311,22 @@ static enum memory_level memory_killer_get_memory_level(void)
 
 API int memory_killer_work(void)
 {
-       switch (memory_killer_get_memory_level()) {
+       int ret;
+       enum memory_level memory_level = memory_killer_get_memory_level();
+
+       ret = memory_notifier_set_vconf(memory_level);
+       if (ret < 0) {
+               _E("Failed to set low memory vconf key (%d)", ret);
+               return ret;
+       }
+
+       ret = memory_notifier_send_event(memory_level);
+       if (ret < 0) {
+               _E("Failed to send memory level event (%d)", ret);
+               return ret;
+       }
+
+       switch (memory_level) {
        case MEMORY_LEVEL_NORMAL:
                _D("Current memory level : NORMAL");
                return memory_killer_work_normal();
diff --git a/src/memory/memory-notifier.c b/src/memory/memory-notifier.c
new file mode 100644 (file)
index 0000000..1404ffd
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * 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 <bundle.h>
+#include <eventsystem.h>
+#include <glib.h>
+#include <vconf.h>
+
+#include <log.h>
+#include <macro.h>
+
+#include "memory-common.h"
+#include "memory-notifier.h"
+
+API int memory_notifier_set_vconf(enum memory_level memory_level)
+{
+       int ret;
+       int cur_val;
+       int new_val;
+
+       switch (memory_level) {
+       case MEMORY_LEVEL_NORMAL:
+               new_val = VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL;
+               break;
+       case MEMORY_LEVEL_LOW:
+               /* Do nothing */
+               return 0;
+       case MEMORY_LEVEL_MEDIUM:
+               new_val = VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING;
+               break;
+       default:
+               _E("Invalid memory level (%d)", memory_level);
+               return -EINVAL;
+       }
+
+       ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &cur_val);
+       if (ret != 0) {
+               _D("Failed to get vconf key (%d)", ret);
+               cur_val = -1;
+       }
+
+       /* Prevent to overwrite same value */
+       if (cur_val == new_val)
+               return 0;
+
+       ret = vconf_set_int(VCONFKEY_SYSMAN_LOW_MEMORY, new_val);
+       if (ret != 0) {
+               _E("Failed to set vconf key (%d)", ret);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+API int memory_notifier_send_event(enum memory_level memory_level)
+{
+       bundle *arg = NULL;
+       const char *val = NULL;
+
+       switch (memory_level) {
+       case MEMORY_LEVEL_NORMAL:
+               val = EVT_VAL_MEMORY_NORMAL;
+               break;
+       case MEMORY_LEVEL_LOW:
+               val = EVT_VAL_MEMORY_SOFT_WARNING;
+               break;
+       case MEMORY_LEVEL_MEDIUM:
+               val = EVT_VAL_MEMORY_HARD_WARNING;
+               break;
+       default:
+               _E("Invalid memory level (%d)", memory_level);
+               return -EINVAL;
+       }
+
+       arg = bundle_create();
+       g_assert(arg);
+
+       bundle_add_str(arg, EVT_KEY_LOW_MEMORY, val);
+       eventsystem_send_system_event(SYS_EVENT_LOW_MEMORY, arg);
+       bundle_free(arg);
+
+       return 0;
+}
diff --git a/src/memory/memory-notifier.h b/src/memory/memory-notifier.h
new file mode 100644 (file)
index 0000000..b2eaf47
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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  memory-notifier.h
+ * @brief  Notify memory related event
+ */
+
+#ifndef __RESOURCED_HEADLESS_MEMORY_NOTIFIER_H__
+#define __RESOURCED_HEADLESS_MEMORY_NOTIFIER_H__
+
+#include "memory-common.h"
+
+/**
+ * @brief  Set VCONFKEY_SYSMAN_LOW_MEMORY in consideration of memory level
+ * @param[in] memory_level  Current memory level
+ * @return  0 on success, otherwise a negative error value
+ */
+int memory_notifier_set_vconf(enum memory_level memory_level);
+
+/**
+ * @brief  Send SYS_EVENT_LOW_MEMORY event
+ * @param[in] memory_level  Current memory level
+ * @return  0 on success, otherwise a negative error value
+ */
+int memory_notifier_send_event(enum memory_level memory_level);
+
+#endif /* __RESOURCED_HEADLESS_MEMORY_NOTIFIER_H__ */