Apply CPU inheritance boosting to improve connection performance 66/302266/1 accepted/tizen/7.0/unified/20231208.173545
authorYoungHun Kim <yh8004.kim@samsung.com>
Fri, 20 Oct 2023 08:02:02 +0000 (17:02 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Tue, 5 Dec 2023 03:21:48 +0000 (12:21 +0900)
Change-Id: I7d61b8c6957c86273ff91af833aa0c8597c82a85

CMakeLists.txt
client/src/muse_client.c
core/include/muse_core_boost.h [new file with mode: 0644]
core/include/muse_core_internal.h
core/src/muse_core.c
core/src/muse_core_boost.c [new file with mode: 0644]
packaging/mused.spec
server/CMakeLists.txt
server/include/muse_server_private.h
server/src/muse_server_private.c

index 9ed5bdb..48e8f9c 100644 (file)
@@ -15,7 +15,8 @@ SET(dependents
     dlog
     glib-2.0
     json-c
-    mm-common)
+    mm-common
+    capi-system-resource)
 
 SET(pc_dependents
     mm-common)
index c2241b1..d1c0b8f 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "muse_client.h"
+#include "muse_core_boost.h"
 #include "muse_core_internal.h"
 
 #ifdef LOG_TAG
@@ -49,8 +50,9 @@ static void _mc_constructor(void) __attribute__((constructor));
 static int _mc_new(muse_channel_e channel);
 static void _mc_table_new(void);
 static gpointer _mc_get_fd_ptr(int sock_fd);
-
 static int _muse_client_new(muse_channel_e channel);
+static void _muse_client_boosting_start(ms_boost_t *bst);
+static void _muse_client_boosting_stop(ms_boost_t *bst);
 
 #ifdef MUSE_USE_CLIENT_SIGHANDLER
 static gboolean _mc_table_remove_func(gpointer key, gpointer value, gpointer user_data)
@@ -207,8 +209,29 @@ static gpointer _mc_get_fd_ptr(int sock_fd)
        return g_hash_table_lookup(mc_table, GINT_TO_POINTER(sock_fd));
 }
 
+static void _muse_client_boosting_start(ms_boost_t *bst)
+{
+       bst->is_boosted = muse_boosting_start();
+       if (!bst->is_boosted)
+               return;
+
+       bst->is_inheritted = muse_boosting_set_cpu_inheritance();
+}
+
+static void _muse_client_boosting_stop(ms_boost_t *bst)
+{
+       if (bst->is_inheritted)
+               bst->is_inheritted = !muse_boosting_clear_cpu_inheritance();
+
+       if (bst->is_boosted)
+               bst->is_boosted = !muse_boosting_stop();
+}
+
 static int _muse_client_new(muse_channel_e channel)
 {
+       int fd = -1;
+       ms_boost_t bst = { 0 };
+
        if (!muse_server_is_ready()) {
                LOGE("muse server is not ready");
                return MM_ERROR_UNKNOWN;
@@ -217,7 +240,13 @@ static int _muse_client_new(muse_channel_e channel)
        if (channel == MUSE_CHANNEL_MSG)
                _mc_table_new();
 
-       return _mc_new(channel);
+       _muse_client_boosting_start(&bst);
+
+       fd = _mc_new(channel);
+
+       _muse_client_boosting_stop(&bst);
+
+       return fd;
 }
 
 int muse_client_new(void)
diff --git a/core/include/muse_core_boost.h b/core/include/muse_core_boost.h
new file mode 100644 (file)
index 0000000..3348672
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * muse-core
+ *
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: YoungHun Kim <yh8004.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __MUSE_CORE_BOOST_H__
+#define __MUSE_CORE_BOOST_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <glib.h>
+#include <cpu-boosting.h>
+
+typedef struct ms_boost {
+       gboolean is_boosted;
+       gboolean is_inheritted;
+       gboolean is_inheritance_registered;
+} ms_boost_t;
+
+gboolean muse_boosting_start(void);
+gboolean muse_boosting_stop(void);
+gboolean muse_boosting_set_cpu_inheritance(void);
+gboolean muse_boosting_clear_cpu_inheritance(void);
+gboolean muse_boosting_register_cpu_inheritance(void);
+gboolean muse_boosting_unregister_cpu_inheritance(void);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /*__MUSE_CORE_BOOST_H__*/
index 9802b94..ff3b4d1 100644 (file)
@@ -49,6 +49,11 @@ extern "C" {
 #include <iniparser.h>
 #include <time.h>
 
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG                                                "MUSED_CORE"
+
 #define CONFFILE                                       SYSCONFDIR"/multimedia/mused.conf"
 #define MUSE_HOST                                      "muse:hosts"
 #define INI_PARSER_COMMA                       ","
index 2cd55da..81e02b7 100644 (file)
@@ -23,9 +23,6 @@
 #include <json.h>
 #include <sys/mman.h>
 
-#undef LOG_TAG
-#define LOG_TAG                                                                "MUSED_CORE"
-
 #define STR_TIME_FORMAT                                                "%m-%d %H:%M:%S"
 #define MUSE_MIN_SEC_VALUE                                     60
 #define MUSE_HOUR_MIN_VALUE                                    60
diff --git a/core/src/muse_core_boost.c b/core/src/muse_core_boost.c
new file mode 100644 (file)
index 0000000..ca20040
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * muse-core
+ *
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: YoungHun Kim <yh8004.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <sys/types.h>
+
+#include "muse_core_boost.h"
+#include "muse_core_internal.h"
+
+#define RESOURCE_CPU_DEST_NAME "muse-server"
+#define RESOURCE_TIMEOUT_MS            1000
+
+#ifndef RESOURCE_ERROR_NONE
+#define RESOURCE_ERROR_NONE            0
+#endif
+
+gboolean muse_boosting_start(void)
+{
+       pid_t source_tid = getpid();
+       resource_pid_t res_pid = {
+               .pid = 0,
+               .tid = &source_tid,
+               .tid_count = 1
+       };
+
+       int ret = resource_set_cpu_boosting(res_pid, CPU_BOOSTING_LEVEL_STRONG, (cpu_boosting_flag_e)0, -1);
+       if (ret != RESOURCE_ERROR_NONE) {
+               LOGE("resource_set_cpu_boosting() is failed. error 0x%x", ret);
+               return FALSE;
+       }
+
+       LOGI("resource_set_cpu_boosting success");
+
+       return TRUE;
+}
+
+gboolean muse_boosting_stop(void)
+{
+       pid_t pid = getpid();
+       resource_pid_t res_pid = {
+               .pid = 0,
+               .tid = &pid,
+               .tid_count = 1
+       };
+
+       int ret = resource_clear_cpu_boosting(res_pid);
+       if (ret != RESOURCE_ERROR_NONE) {
+               LOGE("resource_clear_cpu_boosting() is failed, error 0x%x", ret);
+               return FALSE;
+       }
+
+       LOGI("resource_clear_cpu_boosting success");
+
+       return TRUE;
+}
+
+// for Client(IPC Sender)
+gboolean muse_boosting_set_cpu_inheritance(void)
+{
+       pid_t pid = getpid();
+
+       int ret = resource_set_cpu_inheritance(pid, RESOURCE_CPU_DEST_NAME, RESOURCE_TIMEOUT_MS);
+       if (ret != RESOURCE_ERROR_NONE) {
+               LOGE("resource_set_cpu_inheritance() failed, error 0x%x", ret);
+               return FALSE;
+       }
+
+       LOGI("resource_set_cpu_inheritance success");
+
+       return TRUE;
+}
+
+gboolean muse_boosting_clear_cpu_inheritance(void)
+{
+       pid_t pid = getpid();
+
+       int ret = resource_clear_cpu_inheritance(pid, RESOURCE_CPU_DEST_NAME);
+       if (ret != RESOURCE_ERROR_NONE) {
+               LOGE("resource_clear_cpu_inheritance() failed, error 0x%x", ret);
+               return FALSE;
+       }
+
+       LOGI("resource_clear_cpu_inheritance() success");
+
+       return TRUE;
+}
+
+// for SERVER(IPC Receiver)
+gboolean muse_boosting_register_cpu_inheritance(void)
+{
+       resource_pid_t res_pid = { 0 };
+       res_pid.pid = getpid();
+
+       int ret = resource_register_cpu_inheritance_destination(RESOURCE_CPU_DEST_NAME, res_pid);
+       if (ret != RESOURCE_ERROR_NONE) {
+               LOGE("resource_register_cpu_inheritance_destination() failed, error 0x%x", ret);
+               return FALSE;
+       }
+
+       LOGI("resource_register_cpu_inheritance_destination() success");
+
+       return TRUE;
+}
+
+gboolean muse_boosting_unregister_cpu_inheritance(void)
+{
+       int ret = resource_unregister_cpu_inheritance_destination(RESOURCE_CPU_DEST_NAME);
+       if (ret != RESOURCE_ERROR_NONE) {
+               LOGE("resource_unregister_cpu_inheritance_destination() failed, error 0x%x", ret);
+               return FALSE;
+       }
+
+       LOGI("resource_unregister_cpu_inheritance_destination() success");
+
+       return TRUE;
+}
+
index 3540be5..68609e6 100644 (file)
@@ -1,6 +1,6 @@
 Name:       mused
 Summary:    A multimedia daemon
-Version:    0.3.176
+Version:    0.3.177
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0
@@ -29,11 +29,11 @@ BuildRequires: pkgconfig(json-c)
 BuildRequires: pkgconfig(libtbm)
 BuildRequires: pkgconfig(libtzplatform-config)
 BuildRequires: pkgconfig(mm-common)
+BuildRequires: pkgconfig(capi-system-resource)
 %if "%{tizen_profile_name}" == "tv"
 BuildRequires: pkgconfig(libresourced)
 BuildRequires: pkgconfig(lwipc)
 BuildRequires: pkgconfig(ttrace)
-BuildRequires: pkgconfig(capi-system-resource)
 %else
 BuildRequires: pkgconfig(storage)
 BuildRequires: pkgconfig(mm-resource-manager)
index 2644893..b55d968 100644 (file)
@@ -36,11 +36,6 @@ SET(dependents ${dependents} " storage")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMUSE_USE_EXTERNAL_STORAGE_STATE_CHANGE")
 ENDIF(${MUSE_STORAGE_ENABLE})
 
-IF(${MUSE_BOOST_ENABLE})
-SET(dependents ${dependents} " capi-system-resource")
-SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMUSE_USE_BOOST")
-ENDIF(${MUSE_BOOST_ENABLE})
-
 INCLUDE(FindPkgConfig)
 pkg_check_modules(${MUSE_SERVER} REQUIRED ${dependents})
 FOREACH(flag ${${MUSE_SERVER}_CFLAGS})
index b59070e..6667b33 100644 (file)
@@ -22,6 +22,7 @@
 #ifndef __MUSE_SERVER_PRIVATE_H__
 #define __MUSE_SERVER_PRIVATE_H__
 
+#include "muse_core_boost.h"
 #include "muse_server.h"
 #include "muse_server_config.h"
 #include "muse_server_connection.h"
@@ -45,7 +46,9 @@
 extern "C" {
 #endif
 
+#ifdef LOG_TAG
 #undef LOG_TAG
+#endif
 #define LOG_TAG                                                "MUSED_SERVER"
 #define MSG_KEY_PID                                    "pid"
 #define MS_TIMEOUT_SEC                         1
@@ -74,12 +77,6 @@ typedef struct ms_diag {
        GAsyncQueue *msg_aq;
 } ms_diag_t;
 
-typedef struct ms_boost {
-       gboolean is_boosted;
-       gboolean (*start_func)(void);
-       gboolean (*stop_func)(void);
-} ms_boost_t;
-
 typedef struct _muse_server {
        int msg_fd;
        int data_fd;
index 889118e..808c491 100644 (file)
 #include <malloc.h>
 #include <tzplatform_config.h>
 
-#ifdef MUSE_USE_BOOST
-#include <cpu-boosting.h>
-#endif
-
 #if !GLIB_CHECK_VERSION(2, 58, 0)
 #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
 #endif
@@ -88,59 +84,6 @@ static void _ms_process_init(void);
 static void _ms_launch_foreground(void);
 static void _ms_daemonize(void);
 
-#ifdef MUSE_USE_BOOST
-gboolean _ms_boosting_start(void)
-{
-       pid_t current_pid = getpid();
-       resource_pid_t res_pid = {
-               .pid = 0,
-               .tid = &current_pid,
-               .tid_count = 1,
-       };
-
-       // flags, timeout parameter is not working on VD
-       int ret = resource_set_cpu_boosting(res_pid, CPU_BOOSTING_LEVEL_STRONG, (cpu_boosting_flag_e)0, -1);
-       if (ret != 0) {
-               LOGE("resource_set_cpu_boosting() is failed. error %d", ret);
-               return FALSE;
-       }
-
-       LOGI("Leave");
-
-       return TRUE;
-}
-
-gboolean _ms_boosting_stop(void)
-{
-       pid_t current_pid = getpid();
-       resource_pid_t res_pid = {
-               .pid = 0,
-               .tid = &current_pid,
-               .tid_count = 1,
-       };
-
-       int ret = resource_clear_cpu_boosting(res_pid);
-       if (ret != 0) {
-               LOGE("resource_clear_cpu_boosting() is failed. error: %d", ret);
-               return FALSE;
-       }
-
-       LOGI("Leave");
-
-       return TRUE;
-}
-#endif
-
-void _ms_boosting_init(void)
-{
-       muse_server->boost.is_boosted = FALSE;
-
-#ifdef MUSE_USE_BOOST
-       muse_server->boost.start_func = _ms_boosting_start;
-       muse_server->boost.stop_func = _ms_boosting_stop;
-#endif
-}
-
 #ifdef MUSE_USE_LWIPC
 static void _ms_wait_event(void);
 
@@ -325,7 +268,11 @@ static void _ms_get_module_addr(int fd, intptr_t *module_addr)
 
 static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition condition, gpointer data)
 {
-       int server_sockfd, client_sockfd, pid, idx, len;
+       int server_sockfd = -1;
+       int client_sockfd = -1;
+       int pid;
+       int idx;
+       int len;
        socklen_t client_len;
        struct sockaddr_un client_address;
        muse_channel_e channel = GPOINTER_TO_INT(data);
@@ -356,16 +303,13 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio
 
        if (!ms_is_server_ready()) {
                LOGW("Now mused state is not ready...");
-               _ms_unlock_state();
-               ms_terminate(SIGABRT);
+               goto out;
        }
 
        server_sockfd = g_io_channel_unix_get_fd(source);
        if (!muse_core_fd_is_valid(server_sockfd)) {
                LOGE("Critical Error : server %d is invalid", server_sockfd);
-               _ms_unlock_state();
-               muse_core_dump_fd_state(server_sockfd);
-               ms_terminate(SIGABRT);
+               goto out;
        }
 
        client_len = (socklen_t)sizeof(client_address);
@@ -374,10 +318,7 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio
        client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
        if (!muse_core_fd_is_valid(client_sockfd)) {
                LOGE("Critical Error : accept %d is invalid", client_sockfd);
-               muse_core_dump_fd_state(client_sockfd);
-               close(server_sockfd);
-               _ms_unlock_state();
-               ms_terminate(SIGABRT);
+               goto out;
        }
 
        LOGI("server : %d client [%s channel] : %d", server_sockfd, channel_name[channel], client_sockfd);
@@ -385,11 +326,9 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio
        pid = _ms_get_pid(client_sockfd);
        if (pid == -1) {
                close(client_sockfd);
-               _ms_unlock_state();
-               return FALSE;
+               goto out;
        }
 
-
        if (channel == MUSE_CHANNEL_MSG) {
                m = g_new0(muse_module_t, 1);
                SECURE_LOGI("module [%p]", m);
@@ -473,8 +412,10 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio
 
        return TRUE;
 out:
-       close(server_sockfd);
-       close(client_sockfd);
+       if (muse_core_fd_is_valid(server_sockfd))
+               close(server_sockfd);
+       if (muse_core_fd_is_valid(client_sockfd))
+               close(client_sockfd);
 
        if (m)
                muse_core_connection_close(m->ch[MUSE_CHANNEL_MSG].sock_fd);
@@ -483,6 +424,8 @@ out:
 
        LOGE("FALSE");
 
+       ms_terminate(SIGABRT);
+
        return FALSE;
 }
 
@@ -1054,8 +997,6 @@ void ms_init(char **argv)
 
        muse_server = g_new0(muse_server_t, 1);
 
-       _ms_boosting_init();
-
        for (idx = 1; argv[idx] != NULL; idx++) {
                if (strcmp(argv[idx], "-D") == 0) {
                        LOGI("enable daemonize");
@@ -1070,8 +1011,7 @@ void ms_init(char **argv)
        else
                _ms_launch_foreground();
 
-       if (muse_server->boost.start_func)
-               muse_server->boost.is_boosted = muse_server->boost.start_func();
+       muse_server->boost.is_boosted = muse_boosting_start();
 
 #ifdef MUSE_VIP_REGISTERED
        proc_stat_set_vip_process();
@@ -1122,7 +1062,9 @@ void ms_init(char **argv)
 #endif
 
        if (muse_server->boost.is_boosted)
-               muse_server->boost.is_boosted = !muse_server->boost.stop_func();
+               muse_server->boost.is_boosted = !muse_boosting_stop();
+
+       muse_server->boost.is_inheritance_registered = muse_boosting_register_cpu_inheritance();
 
        LOGW("Leave");
 }
@@ -1169,6 +1111,9 @@ int ms_deinit(void)
 
        ms_set_state(MUSE_SERVER_STATE_IDLE);
 
+       if (muse_server->boost.is_inheritance_registered)
+               muse_server->boost.is_inheritance_registered = !muse_boosting_unregister_cpu_inheritance();
+
        _ms_diag_deinit();
 
 #ifdef MUSE_USE_WATCHDOG