Move dlopen and dlsym to the constructor 89/279289/4 accepted/tizen/unified/20220808.135402 submit/tizen/20220805.071952
authorUnsung Lee <unsung.lee@samsung.com>
Fri, 5 Aug 2022 06:57:41 +0000 (15:57 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Fri, 5 Aug 2022 07:12:04 +0000 (16:12 +0900)
Change-Id: I33b8185e5449bd224051e009a9130097bd3662bf
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/cpu-boosting.c

index daf5826ad19f7e05d8c717659e6d41a5978bf882..56ba7f48741e77047fcddae1a4ce7199c2ebc2c2 100644 (file)
 #include "common.h"
 
 static void *plugin_handle = NULL;
+int (*plugin_resource_set_cpu_boosting)(resource_pid_t pid, cpu_boosting_level_e level, cpu_boosting_flag_e flags, int timeout_msec) = NULL;
+int (*plugin_resource_clear_cpu_boosting)(resource_pid_t pid) = NULL;
+int (*plugin_resource_get_cpu_boosting_level)(resource_pid_t pid, cpu_boosting_level_info_t *level) = NULL;
+int (*plugin_resource_set_cpu_inheritance)(pid_t source_tid, const char *dest_process, int timeout_msec) = NULL;
+int (*plugin_resource_clear_cpu_inheritance)(pid_t source_tid, const char *dest_process) = NULL;
+int (*plugin_resource_register_cpu_inheritance_destination)(const char *dest_process, resource_pid_t pid) = NULL;
+int (*plugin_resource_unregister_cpu_inheritance_destination)(const char *dest_process) = NULL;
 
 #define CPU_BOOSTING_PLUGIN_PATH             PLUGIN_PATH"/libcapi-system-resource-plugin.so"
 
 static int open_cpu_boosting_plugin(void)
 {
-       if (plugin_handle)
-               return RESOURCE_ERROR_NONE;
-
        if (access(CPU_BOOSTING_PLUGIN_PATH, F_OK) != 0) {
                _E("[CPU-BOOSTING] Cannot find a plugin file");
                return RESOURCE_ERROR_NO_SUCH_FILE;
        }
-       else {
-               plugin_handle = dlopen(CPU_BOOSTING_PLUGIN_PATH, RTLD_NOW);
-               if (!plugin_handle) {
-                       _E("[CPU-BOOSTING] Failed to load %s", CPU_BOOSTING_PLUGIN_PATH);
-                       return RESOURCE_ERROR_NOT_SUPPORTED;
-               }
-
-               return RESOURCE_ERROR_NONE;
+
+       plugin_handle = dlopen(CPU_BOOSTING_PLUGIN_PATH, RTLD_NOW);
+       if (!plugin_handle) {
+               _E("[CPU-BOOSTING] Failed to load %s", CPU_BOOSTING_PLUGIN_PATH);
+               return RESOURCE_ERROR_NOT_SUPPORTED;
        }
+
+       plugin_resource_set_cpu_boosting =
+               dlsym(plugin_handle, "resource_set_cpu_boosting");
+       plugin_resource_clear_cpu_boosting =
+               dlsym(plugin_handle, "resource_clear_cpu_boosting");
+       plugin_resource_get_cpu_boosting_level =
+               dlsym(plugin_handle, "resource_get_cpu_boosting_level");
+       plugin_resource_set_cpu_inheritance =
+               dlsym(plugin_handle, "resource_set_cpu_inheritance");
+       plugin_resource_clear_cpu_inheritance =
+               dlsym(plugin_handle, "resource_clear_cpu_inheritance");
+       plugin_resource_register_cpu_inheritance_destination =
+               dlsym(plugin_handle, "resource_register_cpu_inheritance_destination");
+       plugin_resource_unregister_cpu_inheritance_destination =
+               dlsym(plugin_handle, "resource_unregister_cpu_inheritance_destination");
+
+       return RESOURCE_ERROR_NONE;
 }
 
 API int resource_set_cpu_boosting (resource_pid_t pid,
@@ -56,13 +74,8 @@ API int resource_set_cpu_boosting (resource_pid_t pid,
        int ret;
        int (*func)(resource_pid_t pid, cpu_boosting_level_e level, cpu_boosting_flag_e flags, int timeout_msec) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_set_cpu_boosting");
+       func = plugin_resource_set_cpu_boosting;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -80,13 +93,8 @@ API int resource_clear_cpu_boosting (resource_pid_t pid)
        int ret;
        int (*func)(resource_pid_t pid) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_clear_cpu_boosting");
+       func = plugin_resource_clear_cpu_boosting;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -105,13 +113,8 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid,
        int ret;
        int (*func)(resource_pid_t pid, cpu_boosting_level_info_t *level) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_get_cpu_boosting_level");
+       func = plugin_resource_get_cpu_boosting_level;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -129,13 +132,8 @@ API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process
        int ret;
        int (*func)(pid_t source_tid, const char *dest_process, int timeout_msec) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_set_cpu_inheritance");
+       func = plugin_resource_set_cpu_inheritance;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -153,13 +151,8 @@ API int resource_clear_cpu_inheritance (pid_t source_tid, const char *dest_proce
        int ret;
        int (*func)(pid_t source_tid, const char *dest_process) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_clear_cpu_inheritance");
+       func = plugin_resource_clear_cpu_inheritance;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -177,13 +170,8 @@ API int resource_register_cpu_inheritance_destination (const char *dest_process,
        int ret;
        int (*func)(const char *dest_process, resource_pid_t pid) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_register_cpu_inheritance_destination");
+       func = plugin_resource_register_cpu_inheritance_destination;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -201,13 +189,8 @@ API int resource_unregister_cpu_inheritance_destination (const char *dest_proces
        int ret;
        int (*func)(const char *dest_process) = NULL;
 
-       ret = open_cpu_boosting_plugin();
-       if (ret != RESOURCE_ERROR_NONE)
-               return ret;
-
-       func = dlsym(plugin_handle, "resource_unregister_cpu_inheritance_destination");
+       func = plugin_resource_unregister_cpu_inheritance_destination;
        if (!func) {
-               _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
                return RESOURCE_ERROR_NOT_SUPPORTED;
        }
 
@@ -222,6 +205,7 @@ API int resource_unregister_cpu_inheritance_destination (const char *dest_proces
 
 static void __CONSTRUCTOR__ cpu_boosting_init(void)
 {
+       open_cpu_boosting_plugin();
 }
 
 static void __DESTRUCTOR__ cpu_boosting_exit(void)