--- /dev/null
+/* MIT License
+ *
+ * Copyright (c) 2022 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 __TIZEN_SYSTEM_CPU_BOOSTING_PRIVATE_H__
+#define __TIZEN_SYSTEM_CPU_BOOSTING_PRIVATE_H__
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum cpu_boosting_command {
+ CPU_BOOSTING_COMMAND_NONE = 0,
+ CPU_BOOSTING_COMMAND_SET,
+ CPU_BOOSTING_COMMAND_CLEAR,
+ CPU_BOOSTING_COMMAND_GET,
+};
+
+typedef struct resource_cpu_boosting_input {
+ int command;
+ int timeout_msec;
+ cpu_boosting_level_e level;
+ int body_size;
+ resource_pid_t pid;
+} cpu_boosting_input_t;
+
+typedef struct resource_cpu_boosting_output {
+ bool success;
+ cpu_boosting_level_info_t level;
+} cpu_boosting_output_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __TIZEN_SYSTEM_CPU_BOOSTING_PRIVATE_H__
#include "plugin.h"
#include "cpu-boosting-type.h"
+#include "cpu-boosting-private.h"
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#ifndef gettid
+#include <sys/syscall.h>
+
+#ifdef SYS_gettid
+#define gettid() (pid_t) syscall(SYS_gettid)
+#else
+#error "SYS_gettid unavailable on this system"
+#endif
+#endif
+
+#define SOCK_PATH "/run/.resourced.socket"
+
+static int resource_create_and_connect_sock(void)
+{
+ int sock;
+ socklen_t size;
+ struct sockaddr_un sockaddr;
+
+ sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0) {
+ _E("[CPU-BOOSTING-PLUGIN] Failed to allocate a socket");
+ return -1;
+ }
+
+ sockaddr.sun_family = AF_UNIX;
+ strncpy(sockaddr.sun_path, SOCK_PATH, strlen(SOCK_PATH) + 1);
+ size = sizeof(sockaddr);
+
+ if (connect(sock, (struct sockaddr *)&sockaddr, size) < 0) {
+ _E("[CPU-BOOSTING-PLUGIN] Failed to connect to the resourced module");
+ close(sock);
+ return -1;
+ }
+
+ return sock;
+}
+
+static inline bool resource_pid_input_is_valid(resource_pid_t pid)
+{
+ if (pid.pid < 0) {
+ _E("[CPU-BOOSTING-PLUGIN] pid should be euqal or larger than 0");
+ return false;
+ }
+
+ if (pid.pid == 0 && pid.tid != NULL && pid.tid_count <= 0) {
+ _E("[CPU-BOOSTING-PLUGIN] tid count should be larger than 0");
+ return false;
+ }
+
+ return true;
+}
+
+static inline bool resource_cpu_boosting_level_input_is_valid(cpu_boosting_level_e level)
+{
+ if (level < CPU_BOOSTING_LEVEL_STRONG || level > CPU_BOOSTING_LEVEL_WEAK) {
+ _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);
+ return false;
+ }
+
+ return true;
+}
API int resource_set_cpu_boosting (resource_pid_t pid,
cpu_boosting_level_e level, int timeout_msec)
{
- _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
-
- return 0;
+ int tid;
+ int byte;
+ int ret = 0;
+ int sock;
+ cpu_boosting_input_t input;
+// cpu_boosting_output_t output;
+
+ if (!resource_pid_input_is_valid(pid))
+ return -1;
+
+ if (!resource_cpu_boosting_level_input_is_valid(level))
+ return -1;
+
+ if ((sock = resource_create_and_connect_sock()) < 0)
+ return -1;
+
+ input.command = CPU_BOOSTING_COMMAND_SET;
+ input.pid = pid;
+ input.level = level;
+ input.timeout_msec = timeout_msec;
+
+ if (input.pid.pid > 0)
+ input.body_size = 0;
+ else {
+ if (input.pid.tid == NULL) {
+ tid = gettid();
+ input.pid.tid = &tid;
+ input.pid.tid_count = 1;
+ }
+
+ input.body_size = input.pid.tid_count * sizeof(int);
+ }
+
+ byte = send(sock, (const void *)&input, sizeof(input), 0);
+ if (byte != sizeof(input)) {
+ _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
+ _E("[CPU-BOOSTING-PLUGIN] client input size is %u, but sent size is %d",
+ (unsigned int)sizeof(input), byte);
+ ret = -1;
+ goto close_sock;
+ }
+
+ if (input.body_size > 0) {
+ byte = send(sock, (const void *)input.pid.tid, input.body_size, 0);
+ if (byte != input.body_size) {
+ _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
+ _E("[CPU-BOOSTING-PLUGIN] client input size is %d, but sent size is %d",
+ input.body_size, byte);
+ ret = -1;
+ goto close_sock;
+ }
+ }
+
+/* byte = recv(sock, (void *)&output, sizeof(bool), 0);
+ if (byte != sizeof(bool)) {
+ _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
+ _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
+ (unsigned int)sizeof(bool), byte);
+ goto close_sock;
+ }
+
+
+ if (!output.success)
+ _E("[CPU-BOOSTING-PLUGIN] fail to communicate");
+ else {
+ _D("[CPU-BOOSTING-PLUGIN] success to communicate");
+ ret = 0;
+ }*/
+
+close_sock:
+ close(sock);
+
+ return ret;
}
API int resource_clear_cpu_boosting (resource_pid_t pid)