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
24 #include "cpu-boosting-type.h"
25 #include "cpu-boosting-private.h"
31 #include <sys/socket.h>
32 #include <sys/types.h>
35 #include <sys/syscall.h>
38 #define gettid() (pid_t) syscall(SYS_gettid)
40 #error "SYS_gettid unavailable on this system"
44 #define SOCK_PATH "/run/.resourced.socket"
46 static int resource_create_and_connect_sock(void)
50 struct sockaddr_un sockaddr;
52 sock = socket(AF_UNIX, SOCK_STREAM, 0);
54 _E("[CPU-BOOSTING-PLUGIN] Failed to allocate a socket");
58 sockaddr.sun_family = AF_UNIX;
59 strncpy(sockaddr.sun_path, SOCK_PATH, strlen(SOCK_PATH) + 1);
60 size = sizeof(sockaddr);
62 if (connect(sock, (struct sockaddr *)&sockaddr, size) < 0) {
63 _E("[CPU-BOOSTING-PLUGIN] Failed to connect to the resourced module");
71 static inline bool resource_pid_input_is_valid(resource_pid_t pid)
74 _E("[CPU-BOOSTING-PLUGIN] pid should be euqal or larger than 0");
78 if (pid.pid == 0 && pid.tid != NULL && pid.tid_count <= 0) {
79 _E("[CPU-BOOSTING-PLUGIN] tid count should be larger than 0");
86 static inline bool resource_cpu_boosting_level_input_is_valid(cpu_boosting_level_e level)
88 if (level < CPU_BOOSTING_LEVEL_STRONG || level > CPU_BOOSTING_LEVEL_WEAK) {
89 _E("[CPU-BOOSTING-PLUGIN] cpu boosting level should be located between %d and %d, but current level = %d", CPU_BOOSTING_LEVEL_STRONG, CPU_BOOSTING_LEVEL_WEAK, level);
96 API int resource_set_cpu_boosting (resource_pid_t pid,
97 cpu_boosting_level_e level, int timeout_msec)
103 cpu_boosting_input_t input;
104 // cpu_boosting_output_t output;
106 if (!resource_pid_input_is_valid(pid))
109 if (!resource_cpu_boosting_level_input_is_valid(level))
112 if ((sock = resource_create_and_connect_sock()) < 0)
115 input.command = CPU_BOOSTING_COMMAND_SET;
118 input.timeout_msec = timeout_msec;
120 if (input.pid.pid > 0)
123 if (input.pid.tid == NULL) {
125 input.pid.tid = &tid;
126 input.pid.tid_count = 1;
129 input.body_size = input.pid.tid_count * sizeof(pid_t);
132 byte = send(sock, (const void *)&input, sizeof(input), 0);
133 if (byte != sizeof(input)) {
134 _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
135 _E("[CPU-BOOSTING-PLUGIN] client input size is %u, but sent size is %d",
136 (unsigned int)sizeof(input), byte);
141 if (input.body_size > 0) {
142 byte = send(sock, (const void *)input.pid.tid, input.body_size, 0);
143 if (byte != input.body_size) {
144 _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
145 _E("[CPU-BOOSTING-PLUGIN] client input size is %d, but sent size is %d",
146 input.body_size, byte);
152 /* byte = recv(sock, (void *)&output, sizeof(bool), 0);
153 if (byte != sizeof(bool)) {
154 _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
155 _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
156 (unsigned int)sizeof(bool), byte);
162 _E("[CPU-BOOSTING-PLUGIN] fail to communicate");
164 _D("[CPU-BOOSTING-PLUGIN] success to communicate");
174 API int resource_clear_cpu_boosting (resource_pid_t pid)
176 _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
181 API int resource_get_cpu_boosting_level (resource_pid_t pid,
182 cpu_boosting_level_info_t *level)
184 _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
189 API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process, int timeout_msec)
191 _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
196 API int resource_clear_cpu_inheritance (pid_t source_tid, const char *dest_process)
198 _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
203 API int resource_register_cpu_inheritance_destination (const char *dest_process, resource_pid_t pid)
205 _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
210 API int resource_unregister_cpu_inheritance_destination (const char *dest_process)
212 _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
217 void __CONSTRUCTOR__ cpu_boosting_plugin_init(void)
219 _D("[CPU-BOOSTING-PLUGIN] CPU boosting plugin Module is loaded");
222 void __DESTRUCTOR__ cpu_boosting_plugin_exit(void)
224 _D("[CPU-BOOSTING-PLUGIN] CPU boosting plugin Module is unloaded");