From: SangYoun Kwak Date: Wed, 8 Feb 2023 10:59:30 +0000 (+0900) Subject: plugin-api: resourced: Add plugin-api-resourced X-Git-Tag: accepted/tizen/unified/20230310.062652^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c6c3558f85a54dc978f45a715cb2cb681a0fff2f;p=platform%2Fcore%2Fsystem%2Flibsyscommon.git plugin-api: resourced: Add plugin-api-resourced This plugin api provides functions for resourced. Provided functions: * plugin_resourced_low_memory_killer_get_backend: get backend library * plugin_resourced_low_memory_killer_put_backend: put backend library * plugin_resourced_low_memory_killer_get_kill_candidates: choose kill candidates from procs/apps list. The files newly created during build due to this patch are as follows: (arch=armv7l) plugin-api-resourced-0.1.0-0.armv7l.rpm * /usr/lib: libplugin-api-resourced.so.0 -> libplugin-api-resourced.so.0.1.0 * /usr/lib: libplugin-api-resourced.so.0.1.0 * /usr/share/licenses/plugin-api/resourced: LICENSE.MIT plugin-api-resourced-debuginfo-0.1.0-0.armv7l.rpm * /usr/lib/debug/usr/lib/libplugin-api-resourced.so.0.1.0.debug libsyscommon-debugsource-5.0.0-0.armv7l.rpm * /usr/src/debug/libsyscommon-5.0.0-0.arm/src/plugin-api/resourced/include: plugin-resourced-memory-lmk-interface.h * /usr/src/debug/libsyscommon-5.0.0-0.arm/src/plugin-api/resourced/src: plugin-resourced-memory-lmk.c plugin-api-resourced-devel-0.1.0-0.armv7l.rpm * /usr/include/plugin: plugin-resourced-memory-lmk.h * /usr/include/plugin: plugin-resourced-memory-lmk-interface.h * /usr/lib: libplugin-api-resourced.so -> libplugin-api-resourced.so.0 * /usr/lib/pkgconfig: plugin-api-resourced.pc * /usr/share/licenses/plugin-api/resourced: LICENSE.MIT Change-Id: I3f9c2deac1220131df5bb8a3db502b40dc1bdd72 Signed-off-by: SangYoun Kwak --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e46332..c6a6b54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ PROJECT(libsyscommon C) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/shared) ADD_SUBDIRECTORY(src/plugin-api/common) +ADD_SUBDIRECTORY(src/plugin-api/resourced) # ADD_SUBDIRECTORY(src/libgdbus) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index c46cffa..415db65 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -1,4 +1,5 @@ %define plugin_api_common_version 0.1.0 +%define plugin_api_resourced_version 0.1.0 Name: libsyscommon Summary: System Libraries @@ -44,10 +45,14 @@ cp %{SOURCE1001} . %build PLUGIN_API_COMMON_MAJORVER=$(echo %{plugin_api_common_version} | cut -d'.' -f1) +PLUGIN_API_RESOURCED_MAJORVER=$(echo %{plugin_api_resourced_version} | cut -d'.' -f1) %cmake . -DFULLVER=%{version} \ -DPLUGIN_API_COMMON_VERSION=%{plugin_api_common_version} \ -DPLUGIN_API_COMMON_MAJORVER=${PLUGIN_API_COMMON_MAJORVER} \ - -DPLUGIN_API_COMMON_ENABLE_DLOG=1 + -DPLUGIN_API_RESOURCED_VERSION=%{plugin_api_resourced_version} \ + -DPLUGIN_API_RESOURCED_MAJORVER=${PLUGIN_API_RESOURCED_MAJORVER} \ + -DPLUGIN_API_COMMON_ENABLE_DLOG=1 \ + -DPLUGIN_API_RESOURCED_ENABLE_DLOG=1 make %{?_smp_mflags} %install @@ -114,3 +119,41 @@ Development header files for common plugin APIs. %{_includedir}/plugin/plugin-common*.h %{_libdir}/pkgconfig/plugin-api-common.pc %{_libdir}/libplugin-api-common.so + +%package -n plugin-api-resourced +Summary: Plugin APIs for the resourced +Version: %{plugin_api_resourced_version} +License: MIT +Requires: pkgconfig(gio-2.0) +Requires: pkgconfig(dlog) + +%description -n plugin-api-resourced +Plugin APIs for the resourced. + +%files -n plugin-api-resourced +%defattr(-,root,root,-) +%license LICENSE.MIT +%{_libdir}/libplugin-api-resourced.so.* + +%post -n plugin-api-resourced +pushd %{_libdir} +chsmack -a "_" libplugin-api-resourced.so.%{plugin_api_resourced_version} +popd +/sbin/ldconfig + +%package -n plugin-api-resourced-devel +Summary: Header files for plugin APIs for the resourced +Version: %{plugin_api_resourced_version} +License: MIT +Requires: pkgconfig(gio-2.0) +Requires: pkgconfig(dlog) + +%description -n plugin-api-resourced-devel +Development header files for plugin APIs for the resourced. + +%files -n plugin-api-resourced-devel +%defattr(-,root,root,-) +%license LICENSE.MIT +%{_includedir}/plugin/plugin-resourced*.h +%{_libdir}/pkgconfig/plugin-api-resourced.pc +%{_libdir}/libplugin-api-resourced.so diff --git a/src/plugin-api/resourced/CMakeLists.txt b/src/plugin-api/resourced/CMakeLists.txt new file mode 100644 index 0000000..c428dd1 --- /dev/null +++ b/src/plugin-api/resourced/CMakeLists.txt @@ -0,0 +1,67 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(plugin-api-resourced) + + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "${PREFIX}/bin") +SET(LIBDIR ${LIB_INSTALL_DIR}) +SET(INCLUDEDIR "${PREFIX}/include/plugin") +SET(VERSION ${PLUGIN_API_RESOURCED_VERSION}) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) +# Including below should be removed if the repository of plugin-api-common is +# separated. +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../common/include) + +if (${PLUGIN_API_RESOURCED_ENABLE_DLOG}) + SET(PKG_MODULES + glib-2.0 + dlog) + ADD_DEFINITIONS("-DENABLE_DLOG") + ADD_DEFINITIONS("-DLOG_TAG=\"PLUGIN_API_RESOURCED\"") +else() + SET(PKG_MODULES + glib-2.0) +endif() + +INCLUDE(FindPkgConfig) +pkg_check_modules(${PROJECT_NAME} REQUIRED ${PKG_MODULES}) +FOREACH(flag ${${PROJECT_NAME}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -fPIC") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -fno-omit-frame-pointer -finstrument-functions") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -lrt") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +# plugin-api-common.c and plugin-api-conf.c are unnecessary if the repository of +# plugin-api-common is separated. +# Please remove them from SRCS and add 'plugin-api-common' to PKG_MODULES. +SET(SRCS src/plugin-resourced-memory-lmk.c + ../common/src/plugin-api-common.c + ../common/src/plugin-api-conf.c) + +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} + -ldl -Wl,-z,nodelete,--no-undefined) + +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${PLUGIN_API_RESOURCED_VERSION}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${PLUGIN_API_RESOURCED_MAJORVER}) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}) +INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ + DESTINATION ${INCLUDEDIR} + FILES_MATCHING PATTERN "*.h" +) + +# CONFIGURE .pc FILE +FOREACH(include_dirs ${pkgs_INCLUDE_DIRS}) + SET(PLUGIN_RESOURCED_INCLUDEDIR "${PLUGIN_RESOURCED_INCLUDEDIR} -I${include_dirs}") +ENDFOREACH(include_dirs) +FOREACH(libraries ${pkgs_LIBRARIES}) + SET(PLUGIN_RESOURCED_LIBS "${PLUGIN_RESOURCED_LIBS} -l${libraries}") +ENDFOREACH(libraries) +CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) diff --git a/src/plugin-api/resourced/include/plugin-resourced-memory-lmk-interface.h b/src/plugin-api/resourced/include/plugin-resourced-memory-lmk-interface.h new file mode 100644 index 0000000..e936fc0 --- /dev/null +++ b/src/plugin-api/resourced/include/plugin-resourced-memory-lmk-interface.h @@ -0,0 +1,46 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __PLUGIN_RESOURCED_MEMORY_LMK_INTERFACE_H__ +#define __PLUGIN_RESOURCED_MEMORY_LMK_INTERFACE_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _plugin_backend_resourced_memory_lmk_funcs { + int (*get_kill_candidates)(GArray *candidates, + GArray *task_info_app_array, + GArray *task_info_proc_array, + unsigned long totalram_kb); + +} plugin_backend_resourced_memory_lmk_funcs; + +#ifdef __cplusplus +} +#endif + +#endif /* __PLUGIN_RESOURCED_MEMORY_LMK_INTERFACE_H__ */ diff --git a/src/plugin-api/resourced/include/plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/plugin-resourced-memory-lmk.h new file mode 100644 index 0000000..9d58ca5 --- /dev/null +++ b/src/plugin-api/resourced/include/plugin-resourced-memory-lmk.h @@ -0,0 +1,94 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __PLUGIN_RESOURCED_MEMORY_LMK_H__ +#define __PLUGIN_RESOURCED_MEMORY_LMK_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct task_info { + /* + * Mostly, there are not multiple processes with the same pgid. + * So, for the frequent case, we use pid variable to avoid + * allocating arrays. + */ + pid_t pid; + GArray *pids; + pid_t pgid; + /* oom_score_adj is smae as /proc//oom_score_adj */ + int oom_score_adj; + /* + * oom_score_lru is same as oom_score_adj or adjusted by + * proc_app_info lru_state for apps that are marked as favourite. + * + * oom_score_lru is the main value used in comparison for LMK. + */ + int oom_score_lru; + int size; + /** + * proc_app_info_oom_killed and proc_app_info_flags + * are not used if task is not an app. + * Especially, proc_app_info_oom_killed is NULL when this task + * is not an app. + */ + bool *proc_app_info_oom_killed; + int proc_app_info_flags; +}; + +/** + * @brief Get the backend data of resourced-memory-lmk module + * @return @c 0 on success, otherwise a negative error value + */ +int plugin_resourced_memory_lmk_get_backend(void); + +/** + * @brief Put the backend data of resourced-memory-lmk module + * @return @c 0 on success, otherwise a negative error value + */ +int plugin_resourced_memory_lmk_put_backend(void); + +/** + * @brief Call the get_kill_candidates function of resourced-memory-lmk module + * @param[in] candidates is an an GArray to return kill candidates + * @param[in] task_info_app_array is a GArray that contains app info + * @param[in] task_info_proc_array is a GArray that contains process info + * @param[in] totalram_kb is the total amount of memory in kibibytes + * @return @c the number of kill candidates on success, + * otherwise a negative error value + */ +int plugin_resourced_memory_lmk_get_kill_candidates(GArray *candidates, + GArray *task_info_app_array, + GArray *task_info_proc_array, + unsigned long totalram_kb); + +#ifdef __cplusplus +} +#endif + +#endif /* __PLUGIN_RESOURCED_MEMORY_LMK_H__ */ diff --git a/src/plugin-api/resourced/plugin-api-resourced.pc.in b/src/plugin-api/resourced/plugin-api-resourced.pc.in new file mode 100644 index 0000000..6b406aa --- /dev/null +++ b/src/plugin-api/resourced/plugin-api-resourced.pc.in @@ -0,0 +1,17 @@ +# Package Information for pkg-config +# +# Copyright (c) 2023 Samsung Electronics Co., Ltd. +# All rights reserved. +# + +prefix=@PREFIX@ +exec_prefix=@EXEC_PREFIX@ +libdir=@LIBDIR@ +includedir=@INCLUDEDIR@ + +Name: plugin-api-resourced +Description: Plugin APIs for the resourced +Version: @VERSION@ +Requires.private: plugin-api-common +Cflags: -I${includedir} @PLUGIN_RESOURCED_INCLUDEDIR@ +Libs: -L${libdir} -lplugin-api-resourced @PLUGIN_RESOURCED_LIBS@ diff --git a/src/plugin-api/resourced/src/common.h b/src/plugin-api/resourced/src/common.h new file mode 100644 index 0000000..236c616 --- /dev/null +++ b/src/plugin-api/resourced/src/common.h @@ -0,0 +1,42 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __COMMON_H__ +#define __COMMON_H__ + +#ifdef ENABLE_DLOG +#include + +#define _D(fmt, args...) SLOGD(fmt, ##args) +#define _I(fmt, args...) SLOGI(fmt, ##args) +#define _W(fmt, args...) SLOGW(fmt, ##args) +#define _E(fmt, args...) SLOGE(fmt, ##args) +#else +#define _D(fmt, args...) do { } while(0) +#define _I(fmt, args...) do { } while(0) +#define _W(fmt, args...) do { } while(0) +#define _E(fmt, args...) do { } while(0) +#endif + +#endif /* __COMMON_H__ */ diff --git a/src/plugin-api/resourced/src/plugin-resourced-memory-lmk.c b/src/plugin-api/resourced/src/plugin-resourced-memory-lmk.c new file mode 100644 index 0000000..f1aed5b --- /dev/null +++ b/src/plugin-api/resourced/src/plugin-resourced-memory-lmk.c @@ -0,0 +1,103 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "plugin-common.h" + +#include "common.h" +#include "plugin-resourced-memory-lmk.h" +#include "plugin-resourced-memory-lmk-interface.h" + +#ifndef EXPORT +#define EXPORT __attribute__((visibility("default"))) +#endif + +static plugin_backend_resourced_memory_lmk_funcs *funcs = NULL; + +EXPORT +int plugin_resourced_memory_lmk_get_backend(void) +{ + int ret = 0; + + if (funcs) + return 0; + + ret = plugin_common_get_backend( + PLUGIN_MODULE_RESOURCED_MEMORY_LMK, (void **)&funcs); + if (ret < 0) { + _E("Failed to get resourced_memory_lmk backend: %d", ret); + return ret; + } + + _I("Success to get resourced_memory_lmk backend: %d", ret); + + return 0; +} + +EXPORT +int plugin_resourced_memory_lmk_put_backend(void) +{ + int ret = 0; + + if (!funcs) + return 0; + + ret = plugin_common_put_backend( + PLUGIN_MODULE_RESOURCED_MEMORY_LMK, (void *)funcs); + if (ret < 0) { + _E("Failed to put resourced_memory_lmk backend: %d", ret); + return ret; + } + funcs = NULL; + + _I("Success to put resourced_memory_lmk backend: %d", ret); + + return 0; +} + +EXPORT +int plugin_resourced_memory_lmk_get_kill_candidates(GArray *candidates, + GArray *task_info_app_array, + GArray *task_info_proc_array, + unsigned long totalram_kb) +{ + int ret = 0; + + if (!funcs) { + ret = plugin_resourced_memory_lmk_get_backend(); + if (ret < 0) + return ret; + } + + if (!funcs || !funcs->get_kill_candidates) { + _E("No backend or no \"get_kill_candidates\" function"); + return -ENOTSUP; + } + + return funcs->get_kill_candidates(candidates, + task_info_app_array, + task_info_proc_array, + totalram_kb); +}