From 3fa3646359a9c612519fade29176da073b10663c Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 22 Aug 2023 19:55:51 +0900 Subject: [PATCH 01/16] plugin-api: common: Remove log macro from common.h to use libsyscommon/log.h Change-Id: I4de1c0b035397d4c58a702665696bd64233d4885 Signed-off-by: Chanwoo Choi --- src/plugin-api/common/src/common.h | 14 -------------- src/plugin-api/common/src/syscommon-plugin-api-common.c | 2 ++ src/plugin-api/common/src/syscommon-plugin-api-conf.c | 2 ++ 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/plugin-api/common/src/common.h b/src/plugin-api/common/src/common.h index 44e99fd..d33b4ae 100644 --- a/src/plugin-api/common/src/common.h +++ b/src/plugin-api/common/src/common.h @@ -33,20 +33,6 @@ extern "C" { #endif -#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 - #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0])) enum plugin_license { diff --git a/src/plugin-api/common/src/syscommon-plugin-api-common.c b/src/plugin-api/common/src/syscommon-plugin-api-common.c index b6fb3db..7ff51d3 100644 --- a/src/plugin-api/common/src/syscommon-plugin-api-common.c +++ b/src/plugin-api/common/src/syscommon-plugin-api-common.c @@ -35,6 +35,8 @@ #include +#include + #include "common.h" #include "syscommon-plugin-api-conf.h" diff --git a/src/plugin-api/common/src/syscommon-plugin-api-conf.c b/src/plugin-api/common/src/syscommon-plugin-api-conf.c index 0525b0a..1e25c4a 100644 --- a/src/plugin-api/common/src/syscommon-plugin-api-conf.c +++ b/src/plugin-api/common/src/syscommon-plugin-api-conf.c @@ -27,6 +27,8 @@ #include #include +#include + #include #include -- 2.7.4 From 46d0830e85e75e4478278f3a042567c430076a3b Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 23 Aug 2023 15:46:51 +0900 Subject: [PATCH 02/16] plugin-api: deviced: Add attribute for display config New attribute: - id: DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_POWERKEY_DOUBLEPRESS - type: SYSCOMMON_RESMAN_DATA_TYPE_INT - setter: X - getter: O New attribute: - id: DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_LONGPRESS_INTERVAL - type: SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE - setter: X - getter: O New attribute: - id: DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_TOUCH_WAKEUP - type: SYSCOMMON_RESMAN_DATA_TYPE_INT - setter: X - getter: O Change-Id: I841459f76e6a4a75379c53d9875006823917c570 Signed-off-by: Youngjae Cho --- .../syscommon-plugin-deviced-display-interface.h | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h index aec7526..c3fb99d 100644 --- a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h +++ b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h @@ -32,16 +32,19 @@ extern "C" { #include #include -#define DEVICED_DISPLAY_ATTR_INT_GET_MAX_BRIGHTNESS (1ULL << 0) -#define DEVICED_DISPLAY_ATTR_INT_GET_CURRENT_STATE (1ULL << 1) -#define DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE (1ULL << 2) -#define DEVICED_DISPLAY_ATTR_INT_DPMS_STATE (1ULL << 3) -#define DEVICED_DISPLAY_ATTR_TUPLE2_SET_DISPLAY_DIRECT (1ULL << 4) -#define DEVICED_DISPLAY_ATTR_INT_CUSTOM_BRIGHTNESS (1ULL << 5) -#define DEVICED_DISPLAY_ATTR_UINT64_GET_ACTOR_CAPABILITY (1ULL << 6) -#define DEVICED_DISPLAY_ATTR_TUPLE3_SET_ACTOR_CAPABILITY (1ULL << 7) -#define DEVICED_DISPLAY_ATTR_INT_GET_TOUCH_EVENT_BLOCKED (1ULL << 8) -#define DEVICED_DISPLAY_ATTR_INT_SET_RELEASE_LOCK_ALL (1ULL << 9) +#define DEVICED_DISPLAY_ATTR_INT_GET_MAX_BRIGHTNESS (1ULL << 0) +#define DEVICED_DISPLAY_ATTR_INT_GET_CURRENT_STATE (1ULL << 1) +#define DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE (1ULL << 2) +#define DEVICED_DISPLAY_ATTR_INT_DPMS_STATE (1ULL << 3) +#define DEVICED_DISPLAY_ATTR_TUPLE2_SET_DISPLAY_DIRECT (1ULL << 4) +#define DEVICED_DISPLAY_ATTR_INT_CUSTOM_BRIGHTNESS (1ULL << 5) +#define DEVICED_DISPLAY_ATTR_UINT64_GET_ACTOR_CAPABILITY (1ULL << 6) +#define DEVICED_DISPLAY_ATTR_TUPLE3_SET_ACTOR_CAPABILITY (1ULL << 7) +#define DEVICED_DISPLAY_ATTR_INT_GET_TOUCH_EVENT_BLOCKED (1ULL << 8) +#define DEVICED_DISPLAY_ATTR_INT_SET_RELEASE_LOCK_ALL (1ULL << 9) +#define DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_POWERKEY_DOUBLEPRESS (1ULL << 10) +#define DEVICED_DISPLAY_ATTR_DOUBLE_GET_CONFIG_LONGPRESS_INTERVAL (1ULL << 11) +#define DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_TOUCH_WAKEUP (1ULL << 12) enum syscommon_deviced_display_state { SYSCOMMON_DEVICED_DISPLAY_STATE_START, -- 2.7.4 From 68b8bc2bf8f0afb4f528968775444a7579320122 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 23 Aug 2023 17:09:44 +0900 Subject: [PATCH 03/16] plugin-api: deviced: Add syscommon_plugin_deviced_display_load_config() Add plugin API that loads display configuration that intialized by plugin. Change-Id: Ie4767a61db7dd1a222866a23f71c98b056a58607 Signed-off-by: Youngjae Cho --- .../system/syscommon-plugin-deviced-display-interface.h | 2 +- .../include/system/syscommon-plugin-deviced-display.h | 6 ++++++ .../deviced/src/syscommon-plugin-deviced-display.c | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h index c3fb99d..3b7ee20 100644 --- a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h +++ b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h @@ -131,7 +131,7 @@ enum syscommon_deviced_display_capability { #define DEVICED_DISPLAY_SCREEN_TIMEOUT_INFINITE ((~0) >> 1) typedef struct _syscommon_plugin_backend_deviced_display_funcs { - /* NONE */ + int (*load_display_config) (struct syscommon_deviced_display_config **); } syscommon_plugin_backend_deviced_display_funcs; #ifdef __cplusplus diff --git a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display.h b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display.h index d284e62..b49a5bd 100644 --- a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display.h +++ b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display.h @@ -44,6 +44,12 @@ int syscommon_plugin_deviced_display_get_backend(void); */ int syscommon_plugin_deviced_display_put_backend(void); +/** + * @brief Load the backend config of deviced-displaymodule + * @return @c 0 on success, otherwise a negative error value + */ +int syscommon_plugin_deviced_display_load_config(struct syscommon_deviced_display_config **); + #ifdef __cplusplus } #endif diff --git a/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c b/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c index 8819c47..00caf73 100644 --- a/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c +++ b/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c @@ -77,3 +77,17 @@ int syscommon_plugin_deviced_display_put_backend(void) return 0; } + +EXPORT +int syscommon_plugin_deviced_display_load_config(struct syscommon_deviced_display_config **data) +{ + int ret = 0; + + if (!g_display_funcs) { + ret = syscommon_plugin_deviced_display_get_backend(); + if (ret < 0) + return ret; + } + + return g_display_funcs->load_display_config(data); +} -- 2.7.4 From e1fa338dee2a4687940c29ed9167258f1f8bdf2d Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Fri, 25 Aug 2023 09:57:55 +0900 Subject: [PATCH 04/16] plugin-api: deviced: Add assert() to prevent NULL dereferencing Change-Id: I55a46976ff30100468e249e02a769099da9fa0f3 Signed-off-by: Youngjae Cho --- src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c b/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c index 00caf73..57495fb 100644 --- a/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c +++ b/src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c @@ -21,6 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ +#include + #include #include @@ -89,5 +91,7 @@ int syscommon_plugin_deviced_display_load_config(struct syscommon_deviced_displa return ret; } + assert(g_display_funcs); + return g_display_funcs->load_display_config(data); } -- 2.7.4 From fd6bd3f76fe427c90ffa8af087de26713fdc90fb Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Wed, 9 Aug 2023 17:17:47 +0900 Subject: [PATCH 05/16] plugin-api: resourced: Add cpu boosting module Add cpu boosting governor function to be called when CPU PSI is triggered in resourced. The goal of cpu boosting governor is finding out cpu contention reason and making actions to do in cpu boosting controller. A new function called cpu boosting governor is added: - int syscommon_plugin_resourced_cpu_boosting_governor_govern_request (GHashTable *cpu_boosting_info_table, cpu_boosting_level_e cpu_boosting_level, GSList **cpu_boosting_controller_action); * This function receives cpu_boosting_info_table and cpu_boosting_level as inputs and gives cpu_boosting_controller_action as an output. If return value of this function is not 0, then cpu_boosting_controller_action is meaningless. Change-Id: Id6f7854278560fd8816a2e15021cccd7261b7027 Signed-off-by: Unsung Lee --- packaging/libsyscommon.spec | 1 + .../include/system/syscommon-plugin-common.h | 1 + .../common/src/syscommon-plugin-api-list.h | 18 ++++ src/plugin-api/resourced/CMakeLists.txt | 7 +- ...ommon-plugin-resourced-cpu-boosting-interface.h | 62 ++++++++++++ .../syscommon-plugin-resourced-cpu-boosting.h | 67 +++++++++++++ .../src/syscommon-plugin-resourced-cpu-boosting.c | 104 +++++++++++++++++++++ 7 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h create mode 100644 src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h create mode 100644 src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index bb2d003..4d2d613 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -20,6 +20,7 @@ BuildRequires: pkgconfig(gio-unix-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(json-c) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(capi-system-resource) Requires: /bin/cp Requires(post): /sbin/ldconfig diff --git a/src/plugin-api/common/include/system/syscommon-plugin-common.h b/src/plugin-api/common/include/system/syscommon-plugin-common.h index 13b35d7..528c339 100644 --- a/src/plugin-api/common/include/system/syscommon-plugin-common.h +++ b/src/plugin-api/common/include/system/syscommon-plugin-common.h @@ -37,6 +37,7 @@ enum syscommon_plugin_module { SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY, SYSCOMMON_PLUGIN_MODULE_DEVICED_INPUT, SYSCOMMON_PLUGIN_MODULE_DEVICED_DISPLAY, + SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING, SYSCOMMON_PLUGIN_MODULE_END, }; diff --git a/src/plugin-api/common/src/syscommon-plugin-api-list.h b/src/plugin-api/common/src/syscommon-plugin-api-list.h index 9271370..248d699 100644 --- a/src/plugin-api/common/src/syscommon-plugin-api-list.h +++ b/src/plugin-api/common/src/syscommon-plugin-api-list.h @@ -60,6 +60,12 @@ static struct plugin_abi_version_match abi_version_match_data[SYSCOMMON_PLUGIN_M .backend_min_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, }, }, + [SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING] = { + [0] = { + .platform_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, + .backend_min_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, + }, + }, }; static struct __plugin_module_info g_plugin_module_info[] = { @@ -111,6 +117,18 @@ static struct __plugin_module_info g_plugin_module_info[] = { .num_abi_versions = ARRAY_SIZE(abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_DEVICED_DISPLAY]), .abi_versions = abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_DEVICED_DISPLAY], }, + [SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING] = { + .group = PLUGIN_GROUP_RESOURCED, + .module = SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING, + .license = PLUGIN_LICENSE_APACHE_2_0, + .module_name = "SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING", + .backend_module_name = "resourced-cpu-boosting", + .library_name = "/usr/lib/system/plugin/libplugin-backend-resourced-cpu-boosting.so", + .library_name_64bit = "/usr/lib64/system/plugin/libplugin-backend-resourced-cpu-boosting.so", + .symbol_name = "system_plugin_backend_resourced_cpu_boosting_data", + .num_abi_versions = ARRAY_SIZE(abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING]), + .abi_versions = abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING], + }, }; #endif /* __PLUGIN_API_LIST_H__ */ diff --git a/src/plugin-api/resourced/CMakeLists.txt b/src/plugin-api/resourced/CMakeLists.txt index d595753..7b9dbe4 100644 --- a/src/plugin-api/resourced/CMakeLists.txt +++ b/src/plugin-api/resourced/CMakeLists.txt @@ -16,12 +16,14 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../common/include) if (${PLUGIN_API_RESOURCED_ENABLE_DLOG}) SET(PKG_MODULES glib-2.0 - dlog) + dlog + capi-system-resource) ADD_DEFINITIONS("-DENABLE_DLOG") ADD_DEFINITIONS("-DLOG_TAG=\"SYSTEM_PLUGIN_API_RESOURCED\"") else() SET(PKG_MODULES - glib-2.0) + glib-2.0 + capi-system-resource) endif() INCLUDE(FindPkgConfig) @@ -41,6 +43,7 @@ SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") # Please remove them from SRCS and add 'syscommon-plugin-api-common' # to PKG_MODULES. SET(SRCS src/syscommon-plugin-resourced-memory-lmk.c + src/syscommon-plugin-resourced-cpu-boosting.c ../common/src/syscommon-plugin-api-common.c ../common/src/syscommon-plugin-api-conf.c) diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h new file mode 100644 index 0000000..5e43274 --- /dev/null +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h @@ -0,0 +1,62 @@ +/** + * 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 __SYSCOMMON_PLUGIN_RESOURCED_CPU_BOOSTING_INTERFACE_H__ +#define __SYSCOMMON_PLUGIN_RESOURCED_CPU_BOOSTING_INTERFACE_H__ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct syscommon_resourced_cpu_boosting_input { + cpu_boosting_input_t client_input; + int sock; + guint *gsource_id; + bool remove_input; +}; + +struct syscommon_resourced_cpu_boosting_info { + pid_t tid; + cpu_boosting_level_e level; /* current boosting level */ + guint gsource_id; /* timer id */ + int ref_cnt; /* reference count */ + cpu_boosting_flag_e cpu_boosting_flags; +}; + +typedef struct _syscommon_plugin_backend_resourced_cpu_boosting_funcs { + int (*cpu_boosting_governor_govern_request) ( + GHashTable *cpu_boosting_info_table, + cpu_boosting_level_e cpu_boosting_level, + GSList **cpu_boosting_controller_action) +} syscommon_plugin_backend_resourced_cpu_boosting_funcs; + +#ifdef __cplusplus +} +#endif + +#endif /* __SYSCOMMON_PLUGIN_RESOURCED_CPU_BOOSTING_INTERFACE_H__ */ diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h new file mode 100644 index 0000000..1242b73 --- /dev/null +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h @@ -0,0 +1,67 @@ +/** + * 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 __SYSCOMMON_PLUGIN_RESOURCED_CPU_BOOSTING_H__ +#define __SYSCOMMON_PLUGIN_RESOURCED_CPU_BOOSTING_H__ + +#include "syscommon-plugin-resourced-cpu-boosting-interface.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get the backend data of resourced-cpu-boosting module + * @return @c 0 on success, otherwise a negative error value + */ +int syscommon_plugin_resourced_cpu_boosting_get_backend(void); + +/** + * @brief Put the backend data of resourced-cpu-boosting module + * @return @c 0 on success, otherwise a negative error value + */ +int syscommon_plugin_resourced_cpu_boosting_put_backend(void); + +/** + * @brief Analyze cpu contention reason and + * make a list to do for cpu boosting controller + * @param[in] cpu_boosting_info_table is a GHashTable containing cpu boosting info + * of the currently boosted thread + * @param[in] cpu_boosting_level is cpu boosting level among CPU_BOOSTING_LEVEL_STRONG, + * CPU_BOOSTING_LEVEL_MEDIUM, and CPU_BOOSTING_LEVEL_WEAK + * @param[out] cpu_boosting_controller_action is a GSList containing actions + * to be taken by cpu boosting controller + * @return @c zero on success, + * otherwise a negative error value + */ +int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( + GHashTable *cpu_boosting_info_table, + cpu_boosting_level_e cpu_boosting_level, + GSList **cpu_boosting_controller_action); + +#ifdef __cplusplus +} +#endif + +#endif /* __SYSCOMMON_PLUGIN_RESOURCED_CPU_BOOSTING_H__ */ diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c new file mode 100644 index 0000000..1bd8eea --- /dev/null +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c @@ -0,0 +1,104 @@ +/** + * 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 + +#include +#include + +#ifndef EXPORT +#define EXPORT __attribute__((visibility("default"))) +#endif + +static syscommon_plugin_backend_resourced_cpu_boosting_funcs *funcs = NULL; + +EXPORT +int syscommon_plugin_resourced_cpu_boosting_get_backend(void) +{ + int ret = 0; + + if (funcs) + return 0; + + ret = syscommon_plugin_common_get_backend( + SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING, + (void **)&funcs); + if (ret < 0) { + _E("Failed to get resourced_cpu_boosting backend: %d", ret); + return ret; + } + + _I("Success to get resourced_cpu_boosting backend: %d", ret); + + return 0; +} + +EXPORT +int syscommon_plugin_resourced_cpu_boosting_put_backend(void) +{ + int ret = 0; + + if (!funcs) + return 0; + + ret = syscommon_plugin_common_put_backend( + SYSCOMMON_PLUGIN_MODULE_RESOURCED_CPU_BOOSTING, + (void *)funcs); + if (ret < 0) { + _E("Failed to put resourced_cpu_boosting backend: %d", ret); + return ret; + } + funcs = NULL; + + _I("Success to put resourced_cpu_boosting backend: %d", ret); + + return 0; +} + +EXPORT +int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( + GHashTable *cpu_boosting_info_table, + cpu_boosting_level_e cpu_boosting_level, + GSList **cpu_boosting_controller_action) + +{ + int ret = 0; + + if (!funcs) { + ret = syscommon_plugin_resourced_cpu_boosting_get_backend(); + if (ret < 0) + return ret; + } + + assert(funcs); + if (!funcs->cpu_boosting_governor_govern_request) { + _E("No \"cpu_boosting_governor_govern_request\" function"); + return -ENOTSUP; + } + + return funcs->cpu_boosting_governor_govern_request(cpu_boosting_info_table, + cpu_boosting_level, cpu_boosting_controller_action); +} -- 2.7.4 From b1e09d725217b32726d19483bcecac2e797c5e2a Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Wed, 30 Aug 2023 17:09:51 +0900 Subject: [PATCH 06/16] plugin-api: resourced: Add is_cpu_contention_alleviated Add is_cpu_contention_alleviated function to check whether cpu contention is alleviated or not. Prototype of new function is like below: - bool syscommon_plugin_resourced_is_cpu_contention_alleviated( guint timer_id, guint latest_timer_id); * After a stall event is monitored, a timer with some interval set. If no more stall events between setting timer and timeout, timer_id is equal to latest_timer_id. Change-Id: I0136f445827ae49d002fd8dcb0919cef0ada40b9 Signed-off-by: Unsung Lee --- ...common-plugin-resourced-cpu-boosting-interface.h | 4 +++- .../syscommon-plugin-resourced-cpu-boosting.h | 9 +++++++++ .../src/syscommon-plugin-resourced-cpu-boosting.c | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h index 5e43274..73c0c40 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h @@ -52,7 +52,9 @@ typedef struct _syscommon_plugin_backend_resourced_cpu_boosting_funcs { int (*cpu_boosting_governor_govern_request) ( GHashTable *cpu_boosting_info_table, cpu_boosting_level_e cpu_boosting_level, - GSList **cpu_boosting_controller_action) + GSList **cpu_boosting_controller_action); + bool (*is_cpu_contention_alleviated) + (guint timer_id, guint latest_timer_id); } syscommon_plugin_backend_resourced_cpu_boosting_funcs; #ifdef __cplusplus diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h index 1242b73..598ccbd 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h @@ -60,6 +60,15 @@ int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( cpu_boosting_level_e cpu_boosting_level, GSList **cpu_boosting_controller_action); +/** + * @brief Check whehter cpu contention is alleviated + * @param[in] timer id when handling cpu boosting level + * @param[in] timer id when monitoring the latest stall event + * @return @c true when cpu contention is alleviated, otherwise false + */ +bool syscommon_plugin_resourced_is_cpu_contention_alleviated( + guint timer_id, guint latest_timer_id); + #ifdef __cplusplus } #endif diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c index 1bd8eea..4d6739b 100644 --- a/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c @@ -102,3 +102,24 @@ int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( return funcs->cpu_boosting_governor_govern_request(cpu_boosting_info_table, cpu_boosting_level, cpu_boosting_controller_action); } + +EXPORT +bool syscommon_plugin_resourced_is_cpu_contention_alleviated( + guint timer_id, guint latest_timer_id) +{ + int ret = 0; + + if (!funcs) { + ret = syscommon_plugin_resourced_cpu_boosting_get_backend(); + if (ret < 0) + return ret; + } + + assert(funcs); + if (!funcs->is_cpu_contention_alleviated) { + _E("No \"is_cpu_contention_alleviated\" function"); + return -ENOTSUP; + } + + return funcs->is_cpu_contention_alleviated(timer_id, latest_timer_id); +} -- 2.7.4 From c07f0071360337731b37a5659b3016bcf519ed2a Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Mon, 4 Sep 2023 15:36:47 +0900 Subject: [PATCH 07/16] plugin-api: resourced: Add is_cpu_busy argument into govenor func Add is_cpu_busy argument into syscommon_plugin_resourced_cpu_boosting_governor_govern_request() function to check whether system cpu is busy or not. If system cpu is not busy, then resourced cpu boosting will ignore stall event immediately. Function called cpu boosting governor is modified: - int syscommon_plugin_resourced_cpu_boosting_governor_govern_request (GHashTable *cpu_boosting_info_table, cpu_boosting_level_e cpu_boosting_level, GSList **cpu_boosting_controller_action, bool *is_cpu_busy); * output argument called is_cpu_busy is added. Change-Id: I0827f4b5ac5ffec8c64f34c124e89176e24a6c3c Signed-off-by: Unsung Lee --- .../system/syscommon-plugin-resourced-cpu-boosting-interface.h | 2 +- .../include/system/syscommon-plugin-resourced-cpu-boosting.h | 3 ++- .../resourced/src/syscommon-plugin-resourced-cpu-boosting.c | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h index 73c0c40..72fefd0 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting-interface.h @@ -52,7 +52,7 @@ typedef struct _syscommon_plugin_backend_resourced_cpu_boosting_funcs { int (*cpu_boosting_governor_govern_request) ( GHashTable *cpu_boosting_info_table, cpu_boosting_level_e cpu_boosting_level, - GSList **cpu_boosting_controller_action); + GSList **cpu_boosting_controller_action, bool *is_cpu_busy); bool (*is_cpu_contention_alleviated) (guint timer_id, guint latest_timer_id); } syscommon_plugin_backend_resourced_cpu_boosting_funcs; diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h index 598ccbd..e337bd8 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-cpu-boosting.h @@ -52,13 +52,14 @@ int syscommon_plugin_resourced_cpu_boosting_put_backend(void); * CPU_BOOSTING_LEVEL_MEDIUM, and CPU_BOOSTING_LEVEL_WEAK * @param[out] cpu_boosting_controller_action is a GSList containing actions * to be taken by cpu boosting controller + * @param[out] is_cpu_busy is a bool info whehter system cpu is busy or not * @return @c zero on success, * otherwise a negative error value */ int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( GHashTable *cpu_boosting_info_table, cpu_boosting_level_e cpu_boosting_level, - GSList **cpu_boosting_controller_action); + GSList **cpu_boosting_controller_action, bool *is_cpu_busy); /** * @brief Check whehter cpu contention is alleviated diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c index 4d6739b..79bbe5c 100644 --- a/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-cpu-boosting.c @@ -82,7 +82,7 @@ EXPORT int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( GHashTable *cpu_boosting_info_table, cpu_boosting_level_e cpu_boosting_level, - GSList **cpu_boosting_controller_action) + GSList **cpu_boosting_controller_action, bool *is_cpu_busy) { int ret = 0; @@ -100,7 +100,8 @@ int syscommon_plugin_resourced_cpu_boosting_governor_govern_request( } return funcs->cpu_boosting_governor_govern_request(cpu_boosting_info_table, - cpu_boosting_level, cpu_boosting_controller_action); + cpu_boosting_level, cpu_boosting_controller_action, + is_cpu_busy); } EXPORT -- 2.7.4 From fc317d4802f5a515094fb29d58a0392d198d4497 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 6 Sep 2023 20:16:21 +0900 Subject: [PATCH 08/16] resource-manager: Add missing cases for unset_resource_attr_interest() Change-Id: Ibc6b4d62c3209a8826d349ae3b818c8a9c68e4f9 Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 62dddca..3320ee9 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1864,6 +1864,8 @@ unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64 case SYSCOMMON_RESMAN_DATA_TYPE_UINT64: case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE: case SYSCOMMON_RESMAN_DATA_TYPE_STRING: + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_WITH_1_USER_DATA: + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_WITH_2_USER_DATA: free(attr_value->data); attr_value->data = NULL; break; -- 2.7.4 From 617110ad7fb7e15be6e18237ae357854ae7fa298 Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 5 Sep 2023 10:41:26 +0900 Subject: [PATCH 09/16] plugin-api: resourced: Move common struct and enum from *.h to *-interface.h Move common struct and enum, referened by both resourced and resourced backend, from syscommon-plugin-resourced-memory-lmk.h to syscommon-plugin-resourced-memory-lmk-interface.h for consistency. *.h can be referenced only by resourced. On the other hand, *-interface.h can be referenced by both side. Therefore, common struct and enum should be defined in *-interface.h. Change-Id: I0529e21da6688eeebb9a0583521dc69c2e400f2c Signed-off-by: Unsung Lee --- ...scommon-plugin-resourced-memory-lmk-interface.h | 56 +++++++++++++++++++++ .../system/syscommon-plugin-resourced-memory-lmk.h | 58 +--------------------- .../src/syscommon-plugin-resourced-memory-lmk.c | 1 - 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h index a07f0c4..2d3d746 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h @@ -31,6 +31,62 @@ extern "C" { #endif +enum syscommon_resourced_memory_lmk_oom_level { + OOM_LEVEL_BACKGROUND_LEAST_RECENTLY_USED, + OOM_LEVEL_BACKGROUND_MOST_RECENTLY_USED, + OOM_LEVEL_FOREGROUND, + OOM_LEVEL_FOREGROUND_AND_PROC, + OOM_LEVEL_ALL, +}; + +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; + + /** + * TODO: A process can have more than one window, + * and an app can have more than one process. Therefore, + * a task_info can have more than one window. + */ + int window_pid; + int x; + int y; + int z; + int w; + int h; + int layer; + int opaque; + int visibility; + bool is_transformed; + bool alpha; + bool is_focused; + bool is_mapped; +}; + typedef struct _syscommon_plugin_backend_resourced_memory_lmk_funcs { int (*get_kill_candidates)(GArray *candidates, GArray *task_info_app_array, diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h index d3d5b67..b2ac1b0 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h @@ -28,66 +28,12 @@ #include #include +#include "syscommon-plugin-resourced-memory-lmk-interface.h" + #ifdef __cplusplus extern "C" { #endif -enum syscommon_resourced_memory_lmk_oom_level { - OOM_LEVEL_BACKGROUND_LEAST_RECENTLY_USED, - OOM_LEVEL_BACKGROUND_MOST_RECENTLY_USED, - OOM_LEVEL_FOREGROUND, - OOM_LEVEL_FOREGROUND_AND_PROC, - OOM_LEVEL_ALL, -}; - -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; - - /** - * TODO: A process can have more than one window, - * and an app can have more than one process. Therefore, - * a task_info can have more than one window. - */ - int window_pid; - int x; - int y; - int z; - int w; - int h; - int layer; - int opaque; - int visibility; - bool is_transformed; - bool alpha; - bool is_focused; - bool is_mapped; -}; - /** * @brief Get the backend data of resourced-memory-lmk module * @return @c 0 on success, otherwise a negative error value diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c index 0d97201..6ccb8b2 100644 --- a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c @@ -29,7 +29,6 @@ #include #include -#include #ifndef EXPORT #define EXPORT __attribute__((visibility("default"))) -- 2.7.4 From 418dfa5feca0f07cf72d714711a4d805e29fa532 Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 5 Sep 2023 10:59:14 +0900 Subject: [PATCH 10/16] plugin-api: resourced: Add working set size related info in task_info resourced requires active, inactive, and swap memory size, because working set size-based resourced-lmk-governor should know them to calculate memory efficiency of each app. Normally, this information is located in memory.stat of memcg. In addition, some apps are free from LMK according to working set size. Change-Id: I8d20a3be57391fb4813f42a4704dc91f43ce1101 Signed-off-by: Unsung Lee --- .../system/syscommon-plugin-resourced-memory-lmk-interface.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h index 2d3d746..e14f60a 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h @@ -85,6 +85,13 @@ struct task_info { bool alpha; bool is_focused; bool is_mapped; + + /* These elements are required to calculate working set size of a task */ + unsigned long active_memory_kb; + unsigned long inactive_memory_kb; + unsigned long swap_memory_kb; + + bool is_free_from_kill; }; typedef struct _syscommon_plugin_backend_resourced_memory_lmk_funcs { -- 2.7.4 From bb33cf580796f0db99aa9b7a66569cd7b4c4f75e Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 5 Sep 2023 11:24:00 +0900 Subject: [PATCH 11/16] plugin-api: resourced: Add lmk_try_count in get_kill_candidates_post_with_wss resourced-lmk-governor needs to know the number of killing cycle. This is because, the governor should know whether memory thrasing happens or not. If lowmem state is not recovered during the threshold cycle, then the governor skips working set size calculation and does not give benefit to apps with effcient memory usage. Change-Id: I4ba581d10b967665ae3155205cba9b30b0e3fa6b Signed-off-by: Unsung Lee --- .../include/system/syscommon-plugin-resourced-memory-lmk-interface.h | 2 +- .../resourced/include/system/syscommon-plugin-resourced-memory-lmk.h | 3 ++- src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h index e14f60a..73578aa 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk-interface.h @@ -100,7 +100,7 @@ typedef struct _syscommon_plugin_backend_resourced_memory_lmk_funcs { GArray *task_info_proc_array, unsigned long totalram_kb); int (*get_kill_candidates_post)(GArray *candidates); - int (*get_kill_candidates_post_with_wss)(GArray *candidates); + int (*get_kill_candidates_post_with_wss)(GArray *candidates, int lmk_try_count); int (*get_kill_candidates_post_with_foreground)(GArray *candidates, enum syscommon_resourced_memory_lmk_oom_level oom_level); } syscommon_plugin_backend_resourced_memory_lmk_funcs; diff --git a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h index b2ac1b0..66525b1 100644 --- a/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h +++ b/src/plugin-api/resourced/include/system/syscommon-plugin-resourced-memory-lmk.h @@ -77,11 +77,12 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post( according to working set size rule * @param[in] candidates is a GArray to return kill candidates, it is already sorted by get_kill_candidates + * @param[in] lmk_try_count is the number of LMK in a event * @return @c zero on success, * otherwise a negative error value */ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( - GArray *candidates); + GArray *candidates, int lmk_try_count); /** * @brief Reorder candidates list sorted first by get_kill_candidates fuction diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c index 6ccb8b2..c5fe92f 100644 --- a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c @@ -128,7 +128,7 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post( EXPORT int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( - GArray *candidates) + GArray *candidates, int lmk_try_count) { int ret = 0; @@ -144,7 +144,7 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( return -ENOTSUP; } - return funcs->get_kill_candidates_post_with_wss(candidates); + return funcs->get_kill_candidates_post_with_wss(candidates, lmk_try_count); } EXPORT -- 2.7.4 From e699d67ace10af45d22d2e883d9ce4d7bb53009f Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 6 Sep 2023 16:49:26 +0900 Subject: [PATCH 12/16] resource-manager: Change type name of multiple data SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64 -> SYSCOMMON_RESMAN_DATA_TYPE_2_UINT64 SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64 -> SYSCOMMON_RESMAN_DATA_TYPE_3_UINT64 SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64 -> SYSCOMMON_RESMAN_DATA_TYPE_4_UINT64 Change-Id: I840db7996a3c7a0dba7afaa8465489a038edaeb0 Signed-off-by: Youngjae Cho --- include/libsyscommon/resource-type.h | 6 +++--- src/resource-manager/resource-manager.c | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/libsyscommon/resource-type.h b/include/libsyscommon/resource-type.h index b2f98ba..96cd412 100644 --- a/include/libsyscommon/resource-type.h +++ b/include/libsyscommon/resource-type.h @@ -36,9 +36,9 @@ enum syscommon_resman_data_type { SYSCOMMON_RESMAN_DATA_TYPE_PTR, SYSCOMMON_RESMAN_DATA_TYPE_BOOLEAN, - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64, - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64, - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_2_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_3_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_4_UINT64, SYSCOMMON_RESMAN_DATA_TYPE_UINT64_WITH_1_USER_DATA, SYSCOMMON_RESMAN_DATA_TYPE_UINT64_WITH_2_USER_DATA, diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 3320ee9..8af6bf3 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1648,7 +1648,7 @@ syscommon_resman_set_resource_attr_uint64_2(int resource_id, u_int64_t attr_id, return -EINVAL; return set_resource_attr_value_data_2_tuple(resource, attr_id, - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_2_UINT64, &data1, &data2, 1, 1); } @@ -1662,7 +1662,7 @@ syscommon_resman_set_resource_attr_uint64_3(int resource_id, u_int64_t attr_id, return -EINVAL; return set_resource_attr_value_data_3_tuple(resource, attr_id, - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_3_UINT64, &data1, &data2, &data3, 1, 1, 1); } @@ -1676,7 +1676,7 @@ syscommon_resman_set_resource_attr_uint64_4(int resource_id, u_int64_t attr_id, return -EINVAL; return set_resource_attr_value_data_4_tuple(resource, attr_id, - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_4_UINT64, &data1, &data2, &data3, &data4, 1, 1, 1, 1); } @@ -1767,9 +1767,9 @@ set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t case SYSCOMMON_RESMAN_DATA_TYPE_ARRAY: attr_value->data = calloc(1, sizeof(struct syscommon_resman_array_value)); break; - case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64: - case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64: - case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64: + case SYSCOMMON_RESMAN_DATA_TYPE_2_UINT64: + case SYSCOMMON_RESMAN_DATA_TYPE_3_UINT64: + case SYSCOMMON_RESMAN_DATA_TYPE_4_UINT64: /** * FIXME: N-tuple data doesn't support the getter temporarily. * Don't store the passed data to attr_value. -- 2.7.4 From 169077f342bf1a2a3dd23dc9a5ff811466e0321b Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 11 Sep 2023 14:27:30 +0900 Subject: [PATCH 13/16] plugin-api: deviced: Remove _GET_/_SET_ from attribute name Change-Id: I632a52421adc20411716dfd633e52f3d2e36fd8e Signed-off-by: Youngjae Cho --- .../syscommon-plugin-deviced-display-interface.h | 26 +++++++++++----------- .../syscommon-plugin-deviced-power-interface.h | 8 +++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h index 3b7ee20..efd8fa1 100644 --- a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h +++ b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h @@ -32,19 +32,19 @@ extern "C" { #include #include -#define DEVICED_DISPLAY_ATTR_INT_GET_MAX_BRIGHTNESS (1ULL << 0) -#define DEVICED_DISPLAY_ATTR_INT_GET_CURRENT_STATE (1ULL << 1) -#define DEVICED_DISPLAY_ATTR_TUPLE2_SET_CURRENT_STATE (1ULL << 2) -#define DEVICED_DISPLAY_ATTR_INT_DPMS_STATE (1ULL << 3) -#define DEVICED_DISPLAY_ATTR_TUPLE2_SET_DISPLAY_DIRECT (1ULL << 4) -#define DEVICED_DISPLAY_ATTR_INT_CUSTOM_BRIGHTNESS (1ULL << 5) -#define DEVICED_DISPLAY_ATTR_UINT64_GET_ACTOR_CAPABILITY (1ULL << 6) -#define DEVICED_DISPLAY_ATTR_TUPLE3_SET_ACTOR_CAPABILITY (1ULL << 7) -#define DEVICED_DISPLAY_ATTR_INT_GET_TOUCH_EVENT_BLOCKED (1ULL << 8) -#define DEVICED_DISPLAY_ATTR_INT_SET_RELEASE_LOCK_ALL (1ULL << 9) -#define DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_POWERKEY_DOUBLEPRESS (1ULL << 10) -#define DEVICED_DISPLAY_ATTR_DOUBLE_GET_CONFIG_LONGPRESS_INTERVAL (1ULL << 11) -#define DEVICED_DISPLAY_ATTR_INT_GET_CONFIG_TOUCH_WAKEUP (1ULL << 12) +#define DEVICED_DISPLAY_ATTR_INT_MAX_BRIGHTNESS (1ULL << 0) +#define DEVICED_DISPLAY_ATTR_INT_CURRENT_STATE (1ULL << 1) +#define DEVICED_DISPLAY_ATTR_TUPLE2_CURRENT_STATE (1ULL << 2) +#define DEVICED_DISPLAY_ATTR_INT_DPMS_STATE (1ULL << 3) +#define DEVICED_DISPLAY_ATTR_TUPLE2_DISPLAY_DIRECT (1ULL << 4) +#define DEVICED_DISPLAY_ATTR_INT_CUSTOM_BRIGHTNESS (1ULL << 5) +#define DEVICED_DISPLAY_ATTR_UINT64_ACTOR_CAPABILITY (1ULL << 6) +#define DEVICED_DISPLAY_ATTR_TUPLE3_ACTOR_CAPABILITY (1ULL << 7) +#define DEVICED_DISPLAY_ATTR_INT_TOUCH_EVENT_BLOCKED (1ULL << 8) +#define DEVICED_DISPLAY_ATTR_INT_RELEASE_LOCK_ALL (1ULL << 9) +#define DEVICED_DISPLAY_ATTR_INT_CONFIG_POWERKEY_DOUBLEPRESS (1ULL << 10) +#define DEVICED_DISPLAY_ATTR_DOUBLE_CONFIG_LONGPRESS_INTERVAL (1ULL << 11) +#define DEVICED_DISPLAY_ATTR_INT_CONFIG_TOUCH_WAKEUP (1ULL << 12) enum syscommon_deviced_display_state { SYSCOMMON_DEVICED_DISPLAY_STATE_START, diff --git a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-power-interface.h b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-power-interface.h index b7eb153..9fc6ade 100644 --- a/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-power-interface.h +++ b/src/plugin-api/deviced/include/system/syscommon-plugin-deviced-power-interface.h @@ -31,12 +31,12 @@ extern "C" { #endif -#define DEVICED_POWER_ATTR_SET_UINT64_4_CURRENT_STATE (1ULL << 0) +#define DEVICED_POWER_ATTR_UINT64_4_CURRENT_STATE (1ULL << 0) #define DEVICED_POWER_ATTR_UINT64_CURRENT_STATE (1ULL << 1) #define DEVICED_POWER_ATTR_INT_WAKEUP_REASON (1ULL << 2) -#define DEVICED_POWER_ATTR_TUPLE2_SET_WAKELOCK (1ULL << 3) -#define DEVICED_POWER_ATTR_INT_GET_VITAL_MODE (1ULL << 4) -#define DEVICED_POWER_ATTR_TUPLE2_SET_HISTORY_LOG (1ULL << 5) +#define DEVICED_POWER_ATTR_TUPLE2_WAKELOCK (1ULL << 3) +#define DEVICED_POWER_ATTR_INT_VITAL_MODE (1ULL << 4) +#define DEVICED_POWER_ATTR_TUPLE2_HISTORY_LOG (1ULL << 5) enum { DEVICED_POWER_STATE_MIN_INDEX, -- 2.7.4 From 69d24886f3cb4e904ba93618f260807013f8f677 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 7 Sep 2023 21:44:00 +0900 Subject: [PATCH 14/16] resource-manager: Validate attribute before update it Add check_attr_validate() for all NORMAL type getter, making it affect the update_resource_attr(). Change-Id: Ic2be4c565226fa4f9a4be14118a44bd9aa5a9468 Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 8af6bf3..72171d8 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1140,6 +1140,9 @@ syscommon_resman_get_resource_attr_int(int resource_id, u_int64_t attr_id, int32 if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_INT)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1156,6 +1159,9 @@ syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_INT64)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1172,6 +1178,9 @@ syscommon_resman_get_resource_attr_uint(int resource_id, u_int64_t attr_id, u_in if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1188,6 +1197,9 @@ syscommon_resman_get_resource_attr_uint64(int resource_id, u_int64_t attr_id, u_ if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT64)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1204,6 +1216,9 @@ syscommon_resman_get_resource_attr_uint64_with_1_user_data(int resource_id, u_in if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT64_WITH_1_USER_DATA)) + return -EINVAL; + ret = update_resource_attr_with_1_user_data(resource, attr_id, user_data1, 1); if (ret < 0) return ret; @@ -1220,6 +1235,9 @@ syscommon_resman_get_resource_attr_uint64_with_2_user_data(int resource_id, u_in if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_UINT64_WITH_2_USER_DATA)) + return -EINVAL; + ret = update_resource_attr_with_2_user_data(resource, attr_id, user_data1, user_data2, 1, 1); if (ret < 0) return ret; @@ -1236,6 +1254,9 @@ syscommon_resman_get_resource_attr_double(int resource_id, u_int64_t attr_id, do if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1252,6 +1273,9 @@ syscommon_resman_get_resource_attr_string(int resource_id, u_int64_t attr_id, ch if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_STRING)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1268,6 +1292,9 @@ syscommon_resman_get_resource_attr_array(int resource_id, u_int64_t attr_id, str if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_ARRAY)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; @@ -1284,6 +1311,9 @@ syscommon_resman_get_resource_attr_ptr(int resource_id, u_int64_t attr_id, void if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) return -EINVAL; + if (!check_attr_validate(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_PTR)) + return -EINVAL; + ret = update_resource_attr(resource, attr_id); if (ret < 0) return ret; -- 2.7.4 From 74af1d5bd188e484309021792b27522f32c30f8c Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 7 Sep 2023 17:03:56 +0900 Subject: [PATCH 15/16] tests: Restructure test Instead of building testsuite into a shared object and the main() dlopens it, the macro * #define TESTSUITE() has been introduced for testsuite. It runs the testsuite and report to the main() how many testcases have failed. Change-Id: I6a9330601b96f7795442a85631dc666a97fe6d47 Signed-off-by: Youngjae Cho --- tests/CMakeLists.txt | 25 +++++++++++++------ tests/libcommon/CMakeLists.txt | 16 ------------ tests/libcommon/test-common.c | 56 ++++++++++++++++++++---------------------- tests/test-main.c | 54 +++++++++++----------------------------- tests/test-main.h | 8 ++++++ tests/test-mock.c | 1 - 6 files changed, 65 insertions(+), 95 deletions(-) delete mode 100644 tests/libcommon/CMakeLists.txt diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bfd44ea..132c796 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,20 +1,29 @@ SET(TEST_DRIVER "test-driver") -FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) -SET(TEST_SUITE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) -ADD_DEFINITIONS("-DTEST_SUITE_DIRECTORY=\"${TEST_SUITE_DIRECTORY}\"") +FILE(GLOB TEST_DRIVER_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") +FILE(GLOB LIBCOMMON_SRCS + "${CMAKE_SOURCE_DIR}/tests/libcommon/*.c" + "${CMAKE_SOURCE_DIR}/src/libcommon/*.c") + +SET(SRCS + ${TEST_DRIVER_SRCS} + ${LIBCOMMON_SRCS} + ) + +SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open") +SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED + glib-2.0 + capi-system-info cmocka) ADD_EXECUTABLE(${TEST_DRIVER} ${SRCS}) -TARGET_LINK_LIBRARIES(${TEST_DRIVER} cmocka dl) -SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "-Wl,--export-dynamic") +TARGET_LINK_LIBRARIES(${TEST_DRIVER} ${REQUIRED_PKGS_LDFLAGS}) +SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "${WRAP_FLAGS} -Wl,--export-dynamic") ADD_CUSTOM_TARGET(run-test ALL "./${TEST_DRIVER}") ADD_DEPENDENCIES(run-test ${TEST_DRIVER}) - -# add test suites -ADD_SUBDIRECTORY(libcommon) diff --git a/tests/libcommon/CMakeLists.txt b/tests/libcommon/CMakeLists.txt deleted file mode 100644 index 9fe27d3..0000000 --- a/tests/libcommon/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -SET(TEST_SUITE "test-driver-libcommon") - -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) - -FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") -FILE(GLOB ORIG_SRCS "${CMAKE_SOURCE_DIR}/src/libcommon/*.c") - -ADD_LIBRARY(${TEST_SUITE} MODULE ${SRCS} ${ORIG_SRCS}) -SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${TEST_SUITE_DIRECTORY}) -ADD_DEPENDENCIES(${TEST_DRIVER} ${TEST_SUITE}) - -SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open") -SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") -SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LINK_FLAGS ${WRAP_FLAGS}) - -TARGET_LINK_LIBRARIES(${TEST_SUITE} cmocka glib-2.0) diff --git a/tests/libcommon/test-common.c b/tests/libcommon/test-common.c index f34ce27..9f97bd2 100644 --- a/tests/libcommon/test-common.c +++ b/tests/libcommon/test-common.c @@ -6,11 +6,11 @@ #include #include -#include "libsyscommon/file.h" -#include "libsyscommon/list.h" -#include "libsyscommon/ini-parser.h" -#include "libsyscommon/bitmap.h" -#include "libsyscommon/notifier.h" +#include +#include +#include +#include +#include #include "../test-main.h" #include "../test-mock.h" @@ -501,28 +501,24 @@ static void test_syscommon_notifier_emit_notify_priority_p1(void **state) syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_8, (void *)(intptr_t)0x1234); } -int run_test_suite(void) -{ - const struct CMUnitTest testsuite[] = { - cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown), - cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown), - cmocka_unit_test(test_sys_get_str_n2), - cmocka_unit_test(test_list_append_p), - cmocka_unit_test(test_list_prepend_p), - cmocka_unit_test(test_config_parse_p), - cmocka_unit_test(test_init_bitmap_p), - cmocka_unit_test(test_init_bitmap_n), - cmocka_unit_test(test_test_bit), - cmocka_unit_test(test_all_bit), - cmocka_unit_test(test_syscommon_notifier_emit_notify_p1), - cmocka_unit_test(test_syscommon_notifier_emit_notify_p2), - cmocka_unit_test(test_syscommon_notifier_emit_notify_p3), - cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p), - cmocka_unit_test(test_destroy_callback_p1), - cmocka_unit_test(test_destroy_callback_p2), - cmocka_unit_test(test_destroy_callback_p3), - cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1), - }; - - return cmocka_run_group_tests(testsuite, NULL, NULL); -} +static const struct CMUnitTest testsuite_libcommon[] = { + cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown), + cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown), + cmocka_unit_test(test_sys_get_str_n2), + cmocka_unit_test(test_list_append_p), + cmocka_unit_test(test_list_prepend_p), + cmocka_unit_test(test_config_parse_p), + cmocka_unit_test(test_init_bitmap_p), + cmocka_unit_test(test_init_bitmap_n), + cmocka_unit_test(test_test_bit), + cmocka_unit_test(test_all_bit), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p1), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p2), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p3), + cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p), + cmocka_unit_test(test_destroy_callback_p1), + cmocka_unit_test(test_destroy_callback_p2), + cmocka_unit_test(test_destroy_callback_p3), + cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1), +}; +TESTSUITE(testsuite_libcommon) diff --git a/tests/test-main.c b/tests/test-main.c index 5af3e94..aa899b4 100644 --- a/tests/test-main.c +++ b/tests/test-main.c @@ -6,46 +6,20 @@ #include "test-main.h" -int main(int argc, char *argv[]) -{ - int fail = 0; - DIR *dir; - struct dirent *ent; - - dir = opendir(TEST_SUITE_DIRECTORY); - if (!dir) { - printf("Failed to opendir, %m\n"); - return 1; - } - - while ((ent = readdir(dir))) { - void *handle; - char sopath[512] = {0, }; - int (*run_test_suite) (void); - - if (!strstr(ent->d_name, ".so")) - continue; - - snprintf(sopath, sizeof(sopath), "%s/%s", TEST_SUITE_DIRECTORY, ent->d_name); - - handle = dlopen(sopath, RTLD_LAZY); - if (!handle) { - printf("Failed to dlopen, %s\n", dlerror()); - continue; - } +static int totalfail = 0; - run_test_suite = dlsym(handle, "run_test_suite"); - if (!run_test_suite) { - printf("Failed to dlsym, %s\n", dlerror()); - dlclose(handle); - continue; - } - - fail += run_test_suite(); - dlclose(handle); - } - - closedir(dir); +void testsuite_report_failcount(int failcount) +{ + totalfail += failcount; +} - return fail; +int main(int argc, char *argv[]) +{ + /** + * All test suites will run by constructor before the main() and the totalfail + * will be updated when each testsuite calls testsuite_record_failcount(), + * notifying about how many testcases they have failed. Therefore, the main() + * just need to return totalfail. + */ + return totalfail; } diff --git a/tests/test-main.h b/tests/test-main.h index 4b04a85..0430e4d 100644 --- a/tests/test-main.h +++ b/tests/test-main.h @@ -6,5 +6,13 @@ #include #include +void testsuite_report_failcount(int failcount); + +#define TESTSUITE(cmocka_unittest) \ +static void __attribute__((constructor)) run(void) \ +{ \ + testsuite_report_failcount(cmocka_run_group_tests(cmocka_unittest, NULL, NULL)); \ +} + #endif //__TEST_MAIN_H__ diff --git a/tests/test-mock.c b/tests/test-mock.c index 4e7f260..58eb80b 100644 --- a/tests/test-mock.c +++ b/tests/test-mock.c @@ -29,7 +29,6 @@ int __wrap_open64(const char *pathname, int flags) return __wrap_open(pathname, flags); } - void mock_open(const char *pathname, int flags) { will_return(__wrap_open, pathname); -- 2.7.4 From 8d3d43228304e7e920f13b2bcf55a7fc283f65e2 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 7 Sep 2023 21:37:59 +0900 Subject: [PATCH 16/16] tests: Add test for resource-manager Change-Id: Id16676523c3c503ce76593d408b1701cc3f6cc20 Signed-off-by: Youngjae Cho --- tests/CMakeLists.txt | 6 +- tests/resource-manager/resource-driver-test.c | 278 ++++++++++++++ tests/resource-manager/resource-driver-test.h | 6 + .../syscommon-plugin-libsyscommon-test-interface.h | 25 ++ tests/resource-manager/test.c | 424 +++++++++++++++++++++ tests/test-main.h | 7 +- 6 files changed, 744 insertions(+), 2 deletions(-) create mode 100644 tests/resource-manager/resource-driver-test.c create mode 100644 tests/resource-manager/resource-driver-test.h create mode 100644 tests/resource-manager/syscommon-plugin-libsyscommon-test-interface.h create mode 100644 tests/resource-manager/test.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 132c796..c20c070 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,11 +6,14 @@ FILE(GLOB TEST_DRIVER_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c") FILE(GLOB LIBCOMMON_SRCS "${CMAKE_SOURCE_DIR}/tests/libcommon/*.c" "${CMAKE_SOURCE_DIR}/src/libcommon/*.c") +FILE(GLOB RESOURCE_MANAGER_SRCS + "${CMAKE_SOURCE_DIR}/tests/resource-manager/*.c" + "${CMAKE_SOURCE_DIR}/src/resource-manager/*.c") SET(SRCS ${TEST_DRIVER_SRCS} ${LIBCOMMON_SRCS} - ) + ${RESOURCE_MANAGER_SRCS}) SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open") SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") @@ -18,6 +21,7 @@ SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64") INCLUDE(FindPkgConfig) PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED glib-2.0 + json-c capi-system-info cmocka) diff --git a/tests/resource-manager/resource-driver-test.c b/tests/resource-manager/resource-driver-test.c new file mode 100644 index 0000000..5ebca3f --- /dev/null +++ b/tests/resource-manager/resource-driver-test.c @@ -0,0 +1,278 @@ +#include +#include + +#include +#include + +#include "syscommon-plugin-libsyscommon-test-interface.h" +#include "../test-main.h" + +typedef union { + int32_t i32; + int64_t i64; + uint32_t u32; + uint64_t u64; + double d; + void* p; + bool b; +} resource_attr_data_t; + +static int test_driver_create(int resource_id) +{ + return mock_type(int); +} + +static void test_driver_delete(int resource_id) +{ + function_called(); +} + +static int test_driver_init(void) +{ + return mock_type(int); +} + +static void test_driver_exit(void) +{ + function_called(); +} + +static bool test_is_support(int resource_id, const struct syscommon_resman_resource_attribute *attr) +{ + return mock_type(bool); +} + +static int test_get(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + void *data) +{ + int ret = 0; + resource_attr_data_t attr_data = { 0, }; + + switch (attr->id) { + case TEST_ATTR_INT: + case TEST_ATTR_INT_IS_SUPPORT_FUNC: + case TEST_ATTR_INT_NO_IS_SUPPORT_FUNC: + case TEST_ATTR_INT_NO_GETTER: + case TEST_ATTR_INT_NO_SETTER: + attr_data.i32 = mock_type(int); + break; + case TEST_ATTR_INT64: + attr_data.i64 = mock_type(int64_t); + break; + case TEST_ATTR_UINT: + attr_data.u32 = mock_type(uint32_t); + break; + case TEST_ATTR_UINT64: + attr_data.u64 = mock_type(uint64_t); + break; + case TEST_ATTR_DOUBLE: + attr_data.d = mock_type(double); + break; + } + + ret = mock_type(int); + + if (ret < 0) + return ret; + + switch (attr->type) { + case SYSCOMMON_RESMAN_DATA_TYPE_INT: + *(int32_t *) data = attr_data.i32; + break; + case SYSCOMMON_RESMAN_DATA_TYPE_INT64: + *(int64_t *) data = attr_data.i64; + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT: + *(uint32_t *) data = attr_data.u32; + break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64: + *(uint64_t *) data = attr_data.u64; + break; + case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE: + *(double *) data = attr_data.d; + break; + case SYSCOMMON_RESMAN_DATA_TYPE_PTR: + *(void **) data = attr_data.p; + break; + case SYSCOMMON_RESMAN_DATA_TYPE_BOOLEAN: + *(bool *) data = attr_data.b; + break; + default: + return -EINVAL; + } + + return ret; +} + +static int test_set(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + const void *data, int count) +{ + check_expected_ptr(data); + + return mock_type(int); +} + +static const struct syscommon_resman_resource_attribute test_attrs[] = { + { + .name = "TEST_ATTR_INT", + .id = TEST_ATTR_INT, + .type = SYSCOMMON_RESMAN_DATA_TYPE_INT, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_INT64", + .id = TEST_ATTR_INT64, + .type = SYSCOMMON_RESMAN_DATA_TYPE_INT64, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_UINT", + .id = TEST_ATTR_UINT, + .type = SYSCOMMON_RESMAN_DATA_TYPE_UINT, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_UINT64", + .id = TEST_ATTR_UINT64, + .type = SYSCOMMON_RESMAN_DATA_TYPE_UINT64, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_DOUBLE", + .id = TEST_ATTR_DOUBLE, + .type = SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_2_UINT64", + .id = TEST_ATTR_2_UINT64, + .type = SYSCOMMON_RESMAN_DATA_TYPE_2_UINT64, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_3_UINT64", + .id = TEST_ATTR_3_UINT64, + .type = SYSCOMMON_RESMAN_DATA_TYPE_3_UINT64, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + .name = "TEST_ATTR_4_UINT64", + .id = TEST_ATTR_4_UINT64, + .type = SYSCOMMON_RESMAN_DATA_TYPE_4_UINT64, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + /* Attribute for testing .is_support operation */ + .name = "TEST_ATTR_INT_IS_SUPPORT_FUNC", + .id = TEST_ATTR_INT_IS_SUPPORT_FUNC, + .type = SYSCOMMON_RESMAN_DATA_TYPE_INT, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + .is_supported = test_is_support, + }, + }, { + /** + * Attribute for testing the .get undertakes what .is_support should have done + * when the .is_support operation is not given + */ + .name = "TEST_ATTR_INT_NO_IS_SUPPORT_FUNC", + .id = TEST_ATTR_INT_NO_IS_SUPPORT_FUNC, + .type = SYSCOMMON_RESMAN_DATA_TYPE_INT, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .set = test_set, + }, + }, { + /** + * Attribute for testing not existing .get operation + */ + .name = "TEST_ATTR_INT_NO_GETTER", + .id = TEST_ATTR_INT_NO_GETTER, + .type = SYSCOMMON_RESMAN_DATA_TYPE_INT, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .set = test_set, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + }, { + /** + * Attribute for testing not existing .set operation + */ + .name = "TEST_ATTR_INT_NO_SETTER", + .id = TEST_ATTR_INT_NO_SETTER, + .type = SYSCOMMON_RESMAN_DATA_TYPE_INT, + .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC, + .ops = { + .get = test_get, + .is_supported = syscommon_resman_resource_attr_supported_always, + }, + } +}; + +static const struct syscommon_resman_resource_driver test_driver = { + .name = "test-driver", + .type = TEST_RESOURCE_TYPE_NORMAL_SINGLETON, + .flag = SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE, + .attrs = test_attrs, + .num_attrs = sizeof(test_attrs)/sizeof(test_attrs[0]), + .ops = { + .create = test_driver_create, + .delete = test_driver_delete, + .init = test_driver_init, + .exit = test_driver_exit, + }, +}; + +int resource_driver_test_setup(void **state) +{ + /** + * Instead of using macro SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(), + * use function. + * + * There two macros, SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER() and TESTSUITE(), + * are both using __attribute__((constructor)). And its order cannot be explicitly + * specified. However, the SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER() must be followed + * by the TESTSUITE(). Therefore, explicitly registers the driver via setup function + * prior to the execution of the testsuite. + */ + syscommon_resman_add_resource_driver(&test_driver); + + return 0; +} diff --git a/tests/resource-manager/resource-driver-test.h b/tests/resource-manager/resource-driver-test.h new file mode 100644 index 0000000..6b5f8ee --- /dev/null +++ b/tests/resource-manager/resource-driver-test.h @@ -0,0 +1,6 @@ +#ifndef __RESOURCE_DRIVER_TEST_H__ +#define __RESOURCE_DRIVER_TEST_H__ + +int resource_driver_test_setup(void **state); + +#endif /* __RESOURCE_DRIVER_TEST_H__ */ diff --git a/tests/resource-manager/syscommon-plugin-libsyscommon-test-interface.h b/tests/resource-manager/syscommon-plugin-libsyscommon-test-interface.h new file mode 100644 index 0000000..72b2cd1 --- /dev/null +++ b/tests/resource-manager/syscommon-plugin-libsyscommon-test-interface.h @@ -0,0 +1,25 @@ +#ifndef __TEST_RESOURCE_DRIVER_H__ +#define __TEST_RESOURCE_DRIVER_H__ + +enum libsyscommon_test_resource_type { + TEST_RESOURCE_TYPE_NORMAL, + TEST_RESOURCE_TYPE_NORMAL_SINGLETON, + TEST_RESOURCE_TYPE_INVALID_ATTRIBUTE, + TEST_RESOURCE_TYPE_NOT_EXIST, +}; + +#define TEST_ATTR_INT (1ULL << 0) +#define TEST_ATTR_INT64 (1ULL << 1) +#define TEST_ATTR_UINT (1ULL << 2) +#define TEST_ATTR_UINT64 (1ULL << 3) +#define TEST_ATTR_DOUBLE (1ULL << 4) +#define TEST_ATTR_2_UINT64 (1ULL << 5) +#define TEST_ATTR_3_UINT64 (1ULL << 6) +#define TEST_ATTR_4_UINT64 (1ULL << 7) +#define TEST_ATTR_INT_IS_SUPPORT_FUNC (1ULL << 8) +#define TEST_ATTR_INT_NO_IS_SUPPORT_FUNC (1ULL << 9) +#define TEST_ATTR_INT_NO_GETTER (1ULL << 10) +#define TEST_ATTR_INT_NO_SETTER (1ULL << 11) +#define TEST_ATTR_MAX (1ULL << 12) + +#endif /* __TEST_RESOURCE_DRIVER_H__ */ diff --git a/tests/resource-manager/test.c b/tests/resource-manager/test.c new file mode 100644 index 0000000..ccf35e4 --- /dev/null +++ b/tests/resource-manager/test.c @@ -0,0 +1,424 @@ +#include + +#include "resource-driver-test.h" +#include "syscommon-plugin-libsyscommon-test-interface.h" +#include "../test-main.h" + +static int setup_normal_resource(void) +{ + int resource_id; + int ret; + + /** + * To successfully create a resource instance of type TEST_RESOURCE_TYPE_NORMAL, + * it is necessary to configure 3 mocks below. + * 1. driver->ops.create() must success, returning 0. + * 2. The attribute->ops.is_support() of the attribute TEST_ATTR_INT_IS_SUPPORT_FUNC + * must success, returning true. + * 3. The attribute->ops.get() of the attribuet TEST_ATTR_INT_NO_IS_SUPPORT_FUNC + * must success, returnin 0. It needs reserving 2 value, the first one is an + * arbitrary value to be consumed by the ops.get(), and the latter one is an + * effective return value to be returned by the ops.get(). + */ + will_return(test_driver_create, 0); /* precondition 1 */ + will_return(test_is_support, true); /* precondition 2 */ + will_return(test_get, 123456); /* precondition 3 */ + will_return(test_get, 0); /* precondition 3 */ + + ret = syscommon_resman_create_resource(&resource_id, TEST_RESOURCE_TYPE_NORMAL_SINGLETON); + assert_int_equal(ret, 0); + + return resource_id; +} + +static void cleanup_normal_resource(int resource_id) +{ + expect_function_call(test_driver_delete); + syscommon_resman_delete_resource(resource_id); +} + +static void test_create_resource_pass(void **state) +{ + int resource_id; + int ret; + + /* Assume that the driver->ops.create() success and return 0 */ + will_return(test_driver_create, 0); + /* Set attribute TEST_ATTR_INT_IS_SUPPORT_FUNC's is_support() function to return true */ + will_return(test_is_support, true); + /* Set attribute TEST_ATTR_INT_NO_IS_SUPPORT_FUNC's get() function to return 0 */ + will_return(test_get, 121212); /* arbitrary value to be consumed */ + will_return(test_get, 0); + + ret = syscommon_resman_create_resource(&resource_id, TEST_RESOURCE_TYPE_NORMAL_SINGLETON); + + /* Check return value */ + assert_int_equal(ret, 0); + + /* Check attribute interest and memory allocation for attr_value->data */ + for (unsigned long long i = TEST_ATTR_INT; i < TEST_ATTR_MAX; i <<= 1) { + /* It must have been interested in all attribute */ + bool interest = syscommon_resman_is_resource_attr_interested(resource_id, i); + assert_true(interest); + + struct syscommon_resman_resource_attribute_value *attr_value + = syscommon_resman_get_resource_attr_value(resource_id, i); + assert_non_null(attr_value); + + switch (i) { + case TEST_ATTR_INT: + case TEST_ATTR_INT_IS_SUPPORT_FUNC: + case TEST_ATTR_INT_NO_IS_SUPPORT_FUNC: + case TEST_ATTR_INT_NO_GETTER: + case TEST_ATTR_INT_NO_SETTER: + case TEST_ATTR_INT64: + case TEST_ATTR_UINT: + case TEST_ATTR_UINT64: + case TEST_ATTR_DOUBLE: + /** + * Check created resource has attr_value that has + * successfully been allocated memory for data + */ + assert_non_null(attr_value->data); + break; + case TEST_ATTR_2_UINT64: + case TEST_ATTR_3_UINT64: + case TEST_ATTR_4_UINT64: + /** + * FIXME: Currently, memory allocation for the above type + * has not been implemented yet. Therefore it must be NULL. + */ + assert_null(attr_value->data); + break; + default: + assert_true(0); /* fail */ + } + } + + /* Check driver->ops.delete() is called on deleting resource */ + expect_function_call(test_driver_delete); + syscommon_resman_delete_resource(resource_id); +} + +static void test_create_resource_fail_driver_ops_create(void **state) +{ + int resource_id; + int ret; + + /* Assume that the driver->ops.create() fail and return -123 */ + will_return(test_driver_create, -123); + /* Additionally, test_driver_delete() should be called on fail */ + expect_function_call(test_driver_delete); + + ret = syscommon_resman_create_resource(&resource_id, TEST_RESOURCE_TYPE_NORMAL_SINGLETON); + + assert_int_equal(ret, -123); +} + +static void test_create_resource_fail_invalid_parameter_1(void **state) +{ + int ret; + + ret = syscommon_resman_create_resource(NULL, TEST_RESOURCE_TYPE_NORMAL_SINGLETON); + + assert_int_equal(ret, -EINVAL); +} + +static void test_create_resource_fail_invalid_parameter_2(void **state) +{ + int ret; + int resource_id; + + ret = syscommon_resman_create_resource(&resource_id, TEST_RESOURCE_TYPE_NOT_EXIST); + + assert_int_equal(ret, -EINVAL); +} + +static void test_create_resource_fail_is_support_1(void **state) +{ + int ret; + int resource_id; + + /* Assume that the driver->ops.create() success and return 0 */ + will_return(test_driver_create, 0); + /* Set attribute TEST_ATTR_INT_IS_SUPPORT_FUNC's is_support() function to return false */ + will_return(test_is_support, false); + /* Additionally, test_driver_delete() should be called on fail */ + expect_function_call(test_driver_delete); + + ret = syscommon_resman_create_resource(&resource_id, TEST_RESOURCE_TYPE_NORMAL_SINGLETON); + + assert_int_equal(ret, -ENOTSUP); +} + +static void test_create_resource_fail_is_support_2(void **state) +{ + int ret; + int resource_id; + + /* Assume that the driver->ops.create() success and return 0 */ + will_return(test_driver_create, 0); + /* Set attribute TEST_ATTR_INT_IS_SUPPORT_FUNC's is_support() function to return true */ + will_return(test_is_support, true); + /* The attr->ops.get() return negative, fail */ + will_return(test_get, 123123); + will_return(test_get, -1); + /* Additionally, test_driver_delete() should be called on fail */ + expect_function_call(test_driver_delete); + + ret = syscommon_resman_create_resource(&resource_id, TEST_RESOURCE_TYPE_NORMAL_SINGLETON); + + assert_int_equal(ret, -ENOTSUP); +} + +static void test_get_resource_attr_int_pass(void **state) +{ + int resource_id; + int value; + struct syscommon_resman_resource_attribute_value *attr_value; + int ret; + + resource_id = setup_normal_resource(); + + will_return(test_get, 131313); + will_return(test_get, 0); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_INT, &value); + assert_int_equal(ret, 0); + assert_int_equal(value, 131313); + + attr_value = syscommon_resman_get_resource_attr_value(resource_id, TEST_ATTR_INT); + assert_non_null(attr_value); + assert_int_equal(*(int *) attr_value->data, 131313); + + cleanup_normal_resource(resource_id); +} + +static void test_get_resource_attr_int_fail_getter(void **state) +{ + int resource_id; + int value; + struct syscommon_resman_resource_attribute_value *attr_value; + int ret; + + resource_id = setup_normal_resource(); + + /* First try, success */ + will_return(test_get, 9494); + will_return(test_get, 0 /* success */); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_INT, &value); + assert_int_equal(ret, 0); + assert_int_equal(value, 9494); + + attr_value = syscommon_resman_get_resource_attr_value(resource_id, TEST_ATTR_INT); + assert_non_null(attr_value); + assert_int_equal(*(int *) attr_value->data, 9494); + + /* Second try, fail */ + will_return(test_get, 777777); + will_return(test_get, -EPIPE /* An error occured */); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_INT, &value); + assert_int_equal(ret, -EPIPE); + + /* The attr_value MUST NOT be changed */ + attr_value = syscommon_resman_get_resource_attr_value(resource_id, TEST_ATTR_INT); + assert_non_null(attr_value); + assert_int_equal(*(int *) attr_value->data, 9494); + + cleanup_normal_resource(resource_id); +} + +static void test_get_resource_attr_int_fail_no_getter(void **state) +{ + int resource_id; + int value; + int ret; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_INT_NO_GETTER, &value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static void test_get_resource_attr_int_fail_unmatched_type(void **state) +{ + int resource_id; + int value; + int ret; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_INT64, &value); + assert_int_equal(ret, -EINVAL); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_UINT, &value); + assert_int_equal(ret, -EINVAL); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_UINT64, &value); + assert_int_equal(ret, -EINVAL); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_DOUBLE, &value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +/* DEFECT: The NULL checking for the third parameter is not being checked */ +__attribute__((unused)) +static void test_get_resource_attr_int_fail_invalid_parameter_1(void **state) +{ + int resource_id; + int ret; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_INT, NULL /* invalid */); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static void test_get_resource_attr_int_fail_invalid_parameter_2(void **state) +{ + int resource_id; + int value; + int ret; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_get_resource_attr_int(resource_id, TEST_ATTR_MAX /* invalid */, &value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static void test_get_resource_attr_int_fail_invalid_parameter_3(void **state) +{ + int resource_id; + int value; + int ret; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_get_resource_attr_int(-1 /* invalid */, TEST_ATTR_INT, &value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static void test_set_resource_attr_int_pass(void **state) +{ + int resource_id; + int ret; + const int value = 565656; + + resource_id = setup_normal_resource(); + + expect_memory(test_set, data, &value, sizeof(int)); + will_return(test_set, 0); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_INT, value); + assert_int_equal(ret, 0); + + cleanup_normal_resource(resource_id); +} + +static void test_set_resource_attr_int_fail_setter(void **state) +{ + int resource_id; + int ret; + const int value = 444888; + + resource_id = setup_normal_resource(); + + expect_memory(test_set, data, &value, sizeof(int)); + will_return(test_set, -EPIPE /* An error occured */); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_INT, value); + assert_int_equal(ret, -EPIPE); + + cleanup_normal_resource(resource_id); +} + +static void test_set_resource_attr_int_fail_no_setter(void **state) +{ + int resource_id; + int ret; + const int value = 444888; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_INT_NO_SETTER, value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static void test_set_resource_attr_int_fail_unmatched_type(void **state) +{ + int resource_id; + int ret; + const int value = 9911; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_INT64, value); + assert_int_equal(ret, -EINVAL); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_UINT, value); + assert_int_equal(ret, -EINVAL); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_UINT64, value); + assert_int_equal(ret, -EINVAL); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_DOUBLE, value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static void test_set_resource_attr_int_fail_invalid_parameter_1(void **state) +{ + int resource_id; + int ret; + const int value = 543210; + + resource_id = setup_normal_resource(); + + ret = syscommon_resman_set_resource_attr_int(resource_id, TEST_ATTR_MAX, value); + assert_int_equal(ret, -EINVAL); + + cleanup_normal_resource(resource_id); +} + +static const struct CMUnitTest resource_manager_testsuite[] = { + cmocka_unit_test(test_create_resource_pass), + cmocka_unit_test(test_create_resource_fail_driver_ops_create), + cmocka_unit_test(test_create_resource_fail_invalid_parameter_1), + cmocka_unit_test(test_create_resource_fail_invalid_parameter_2), + cmocka_unit_test(test_create_resource_fail_is_support_1), + cmocka_unit_test(test_create_resource_fail_is_support_2), + + cmocka_unit_test(test_get_resource_attr_int_pass), + cmocka_unit_test(test_get_resource_attr_int_fail_getter), + cmocka_unit_test(test_get_resource_attr_int_fail_no_getter), + cmocka_unit_test(test_get_resource_attr_int_fail_unmatched_type), + /** + * DEFECT: It is not being checked that the third parameter is NULL. + * Enable this TC after adding check code, and it will get pass. + */ + //cmocka_unit_test(test_get_resource_attr_int_fail_invalid_parameter_1), + cmocka_unit_test(test_get_resource_attr_int_fail_invalid_parameter_2), + cmocka_unit_test(test_get_resource_attr_int_fail_invalid_parameter_3), + + cmocka_unit_test(test_set_resource_attr_int_pass), + cmocka_unit_test(test_set_resource_attr_int_fail_setter), + cmocka_unit_test(test_set_resource_attr_int_fail_no_setter), + cmocka_unit_test(test_set_resource_attr_int_fail_unmatched_type), + cmocka_unit_test(test_set_resource_attr_int_fail_invalid_parameter_1), +}; +TESTSUITE_FIXTURE(resource_manager_testsuite, resource_driver_test_setup, NULL); diff --git a/tests/test-main.h b/tests/test-main.h index 0430e4d..891a096 100644 --- a/tests/test-main.h +++ b/tests/test-main.h @@ -14,5 +14,10 @@ static void __attribute__((constructor)) run(void) \ testsuite_report_failcount(cmocka_run_group_tests(cmocka_unittest, NULL, NULL)); \ } -#endif //__TEST_MAIN_H__ +#define TESTSUITE_FIXTURE(cmocka_unittest, setup, teardown) \ +static void __attribute__((constructor)) run(void) \ +{ \ + testsuite_report_failcount(cmocka_run_group_tests(cmocka_unittest, setup, teardown)); \ +} +#endif //__TEST_MAIN_H__ -- 2.7.4