Modify tid and pid from int to pid_t
[platform/core/api/resource.git] / src / cpu-boosting.c
1 /* MIT License
2  *
3  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * THE SOFTWARE. */
22
23 #include <unistd.h>
24 #include <dlfcn.h>
25
26 #include "cpu-boosting.h"
27 #include "common.h"
28
29 static void *plugin_handle = NULL;
30
31 #define CPU_BOOSTING_PLUGIN_PATH             PLUGIN_PATH"/libcapi-system-resource-plugin.so"
32
33 static int open_cpu_boosting_plugin(void)
34 {
35         if (plugin_handle)
36                 return RESOURCE_ERROR_NONE;
37
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;
41         }
42         else {
43                 plugin_handle = dlopen(CPU_BOOSTING_PLUGIN_PATH, RTLD_NOW);
44                 if (!plugin_handle) {
45                         _E("[CPU-BOOSTING] Failed to load %s", CPU_BOOSTING_PLUGIN_PATH);
46                         return RESOURCE_ERROR_NOT_SUPPORTED;
47                 }
48
49                 return RESOURCE_ERROR_NONE;
50         }
51 }
52
53 API int resource_set_cpu_boosting (resource_pid_t pid,
54                 cpu_boosting_level_e level, int timeout_msec)
55 {
56         int ret;
57         int (*func)(resource_pid_t pid, cpu_boosting_level_e level, int timeout_msec) = NULL;
58
59         ret = open_cpu_boosting_plugin();
60         if (ret != RESOURCE_ERROR_NONE)
61                 return ret;
62
63         func = dlsym(plugin_handle, "resource_set_cpu_boosting");
64         if (!func) {
65                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
66                 return RESOURCE_ERROR_NOT_SUPPORTED;
67         }
68
69         ret = func(pid, level, timeout_msec);
70         if (ret != RESOURCE_ERROR_NONE)
71                 _E("[CPU-BOOSTING] Failed to set CPU boosting");
72         else
73                 _D("[CPU-BOOSTING] %s called successfully", __func__);
74
75         return ret;
76 }
77
78 API int resource_clear_cpu_boosting (resource_pid_t pid)
79 {
80         int ret;
81         int (*func)(resource_pid_t pid) = NULL;
82
83         ret = open_cpu_boosting_plugin();
84         if (ret != RESOURCE_ERROR_NONE)
85                 return ret;
86
87         func = dlsym(plugin_handle, "resource_clear_cpu_boosting");
88         if (!func) {
89                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
90                 return RESOURCE_ERROR_NOT_SUPPORTED;
91         }
92
93         ret = func(pid);
94         if (ret != RESOURCE_ERROR_NONE)
95                 _E("[CPU-BOOSTING] Failed to clear CPU boosting");
96         else
97                 _D("[CPU-BOOSTING] %s called successfully", __func__);
98
99         return ret;
100 }
101
102 API int resource_get_cpu_boosting_level (resource_pid_t pid,
103                 cpu_boosting_level_info_t *level)
104 {
105         int ret;
106         int (*func)(resource_pid_t pid, cpu_boosting_level_info_t *level) = NULL;
107
108         ret = open_cpu_boosting_plugin();
109         if (ret != RESOURCE_ERROR_NONE)
110                 return ret;
111
112         func = dlsym(plugin_handle, "resource_get_cpu_boosting_level");
113         if (!func) {
114                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
115                 return RESOURCE_ERROR_NOT_SUPPORTED;
116         }
117
118         ret = func(pid, level);
119         if (ret != RESOURCE_ERROR_NONE)
120                 _E("[CPU-BOOSTING] Failed to get CPU boosting info");
121         else
122                 _D("[CPU-BOOSTING] %s called successfully", __func__);
123
124         return ret;
125 }
126
127 API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process, int timeout_msec)
128 {
129         int ret;
130         int (*func)(pid_t source_tid, const char *dest_process, int timeout_msec) = NULL;
131
132         ret = open_cpu_boosting_plugin();
133         if (ret != RESOURCE_ERROR_NONE)
134                 return ret;
135
136         func = dlsym(plugin_handle, "resource_set_cpu_inheritance");
137         if (!func) {
138                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
139                 return RESOURCE_ERROR_NOT_SUPPORTED;
140         }
141
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");
145         else
146                 _D("[CPU-BOOSTING] %s called successfully", __func__);
147
148         return ret;
149 }
150
151 API int resource_clear_cpu_inheritance (pid_t source_tid, const char *dest_process)
152 {
153         int ret;
154         int (*func)(pid_t source_tid, const char *dest_process) = NULL;
155
156         ret = open_cpu_boosting_plugin();
157         if (ret != RESOURCE_ERROR_NONE)
158                 return ret;
159
160         func = dlsym(plugin_handle, "resource_clear_cpu_inheritance");
161         if (!func) {
162                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
163                 return RESOURCE_ERROR_NOT_SUPPORTED;
164         }
165
166         ret = func(source_tid, dest_process);
167         if (ret != RESOURCE_ERROR_NONE)
168                 _E("[CPU-BOOSTING] Failed to cancel CPU boosting inheritance");
169         else
170                 _D("[CPU-BOOSTING] %s called successfully", __func__);
171
172         return ret;
173 }
174
175 API int resource_register_cpu_inheritance_destination (const char *dest_process, resource_pid_t pid)
176 {
177         int ret;
178         int (*func)(const char *dest_process, resource_pid_t pid) = NULL;
179
180         ret = open_cpu_boosting_plugin();
181         if (ret != RESOURCE_ERROR_NONE)
182                 return ret;
183
184         func = dlsym(plugin_handle, "resource_register_cpu_inheritance_destination");
185         if (!func) {
186                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
187                 return RESOURCE_ERROR_NOT_SUPPORTED;
188         }
189
190         ret = func(dest_process, pid);
191         if (ret != RESOURCE_ERROR_NONE)
192                 _E("[CPU-BOOSTING] Failed to register a destination");
193         else
194                 _D("[CPU-BOOSTING] %s called successfully", __func__);
195
196         return ret;
197 }
198
199 API int resource_unregister_cpu_inheritance_destination (const char *dest_process)
200 {
201         int ret;
202         int (*func)(const char *dest_process) = NULL;
203
204         ret = open_cpu_boosting_plugin();
205         if (ret != RESOURCE_ERROR_NONE)
206                 return ret;
207
208         func = dlsym(plugin_handle, "resource_unregister_cpu_inheritance_destination");
209         if (!func) {
210                 _E("[CPU-BOOSTING] dlsym failed: %s", dlerror());
211                 return RESOURCE_ERROR_NOT_SUPPORTED;
212         }
213
214         ret = func(dest_process);
215         if (ret != RESOURCE_ERROR_NONE)
216                 _E("[CPU-BOOSTING] Failed to unregister a destination");
217         else
218                 _D("[CPU-BOOSTING] %s called successfully", __func__);
219
220         return ret;
221 }
222
223 static void __CONSTRUCTOR__ cpu_boosting_init(void)
224 {
225 }
226
227 static void __DESTRUCTOR__ cpu_boosting_exit(void)
228 {
229 }