3 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is furnished
10 * to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "cpu-boosting.h"
29 static void *plugin_handle = NULL;
31 #define CPU_BOOSTING_PLUGIN_PATH PLUGIN_PATH"/libcapi-system-resource-plugin.so"
33 static int open_cpu_boosting_plugin(void)
36 return RESOURCE_ERROR_NONE;
38 if (access(CPU_BOOSTING_PLUGIN_PATH, F_OK) != 0) {
39 _E("[CPU-BOOSTING] Cannot find a plugin file");
40 return RESOURCE_ERROR_NO_SUCH_FILE;
43 plugin_handle = dlopen(CPU_BOOSTING_PLUGIN_PATH, RTLD_NOW);
45 _E("[CPU-BOOSTING] Failed to load %s", CPU_BOOSTING_PLUGIN_PATH);
46 return RESOURCE_ERROR_NOT_SUPPORTED;
49 return RESOURCE_ERROR_NONE;
53 API int resource_set_cpu_boosting (resource_pid_t pid,
54 cpu_boosting_level_e level, int timeout_msec)
57 int (*func)(resource_pid_t pid, cpu_boosting_level_e level, int timeout_msec) = NULL;
59 ret = open_cpu_boosting_plugin();
60 if (ret != RESOURCE_ERROR_NONE)
63 func = dlsym(plugin_handle, "resource_set_cpu_boosting");
65 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
66 return RESOURCE_ERROR_NOT_SUPPORTED;
69 ret = func(pid, level, timeout_msec);
70 if (ret != RESOURCE_ERROR_NONE)
71 _E("[CPU-BOOSTING] Failed to set CPU boosting");
73 _D("[CPU-BOOSTING] %s called successfully", __func__);
78 API int resource_clear_cpu_boosting (resource_pid_t pid)
81 int (*func)(resource_pid_t pid) = NULL;
83 ret = open_cpu_boosting_plugin();
84 if (ret != RESOURCE_ERROR_NONE)
87 func = dlsym(plugin_handle, "resource_clear_cpu_boosting");
89 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
90 return RESOURCE_ERROR_NOT_SUPPORTED;
94 if (ret != RESOURCE_ERROR_NONE)
95 _E("[CPU-BOOSTING] Failed to clear CPU boosting");
97 _D("[CPU-BOOSTING] %s called successfully", __func__);
102 API int resource_get_cpu_boosting_level (resource_pid_t pid,
103 cpu_boosting_level_info_t *level)
106 int (*func)(resource_pid_t pid, cpu_boosting_level_info_t *level) = NULL;
108 ret = open_cpu_boosting_plugin();
109 if (ret != RESOURCE_ERROR_NONE)
112 func = dlsym(plugin_handle, "resource_get_cpu_boosting_level");
114 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
115 return RESOURCE_ERROR_NOT_SUPPORTED;
118 ret = func(pid, level);
119 if (ret != RESOURCE_ERROR_NONE)
120 _E("[CPU-BOOSTING] Failed to get CPU boosting info");
122 _D("[CPU-BOOSTING] %s called successfully", __func__);
127 API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process, int timeout_msec)
130 int (*func)(pid_t source_tid, const char *dest_process, int timeout_msec) = NULL;
132 ret = open_cpu_boosting_plugin();
133 if (ret != RESOURCE_ERROR_NONE)
136 func = dlsym(plugin_handle, "resource_set_cpu_inheritance");
138 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
139 return RESOURCE_ERROR_NOT_SUPPORTED;
142 ret = func(source_tid, dest_process, timeout_msec);
143 if (ret != RESOURCE_ERROR_NONE)
144 _E("[CPU-BOOSTING] Failed to inherit CPU boosting level");
146 _D("[CPU-BOOSTING] %s called successfully", __func__);
151 API int resource_clear_cpu_inheritance (pid_t source_tid, const char *dest_process)
154 int (*func)(pid_t source_tid, const char *dest_process) = NULL;
156 ret = open_cpu_boosting_plugin();
157 if (ret != RESOURCE_ERROR_NONE)
160 func = dlsym(plugin_handle, "resource_clear_cpu_inheritance");
162 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
163 return RESOURCE_ERROR_NOT_SUPPORTED;
166 ret = func(source_tid, dest_process);
167 if (ret != RESOURCE_ERROR_NONE)
168 _E("[CPU-BOOSTING] Failed to cancel CPU boosting inheritance");
170 _D("[CPU-BOOSTING] %s called successfully", __func__);
175 API int resource_register_cpu_inheritance_destination (const char *dest_process, resource_pid_t pid)
178 int (*func)(const char *dest_process, resource_pid_t pid) = NULL;
180 ret = open_cpu_boosting_plugin();
181 if (ret != RESOURCE_ERROR_NONE)
184 func = dlsym(plugin_handle, "resource_register_cpu_inheritance_destination");
186 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
187 return RESOURCE_ERROR_NOT_SUPPORTED;
190 ret = func(dest_process, pid);
191 if (ret != RESOURCE_ERROR_NONE)
192 _E("[CPU-BOOSTING] Failed to register a destination");
194 _D("[CPU-BOOSTING] %s called successfully", __func__);
199 API int resource_unregister_cpu_inheritance_destination (const char *dest_process)
202 int (*func)(const char *dest_process) = NULL;
204 ret = open_cpu_boosting_plugin();
205 if (ret != RESOURCE_ERROR_NONE)
208 func = dlsym(plugin_handle, "resource_unregister_cpu_inheritance_destination");
210 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
211 return RESOURCE_ERROR_NOT_SUPPORTED;
214 ret = func(dest_process);
215 if (ret != RESOURCE_ERROR_NONE)
216 _E("[CPU-BOOSTING] Failed to unregister a destination");
218 _D("[CPU-BOOSTING] %s called successfully", __func__);
223 static void __CONSTRUCTOR__ cpu_boosting_init(void)
227 static void __DESTRUCTOR__ cpu_boosting_exit(void)