Inherit client CPU priority on cynara_check() 25/316125/1
authorKrzysztof Malysa <k.malysa@samsung.com>
Wed, 30 Oct 2024 14:19:38 +0000 (15:19 +0100)
committerKrzysztof Malysa <k.malysa@samsung.com>
Tue, 10 Dec 2024 11:36:12 +0000 (12:36 +0100)
Change-Id: Ia67d9b1239f7878e89c1e32c2282f9dbfe7249a8

packaging/cynara.spec
packaging/libcynara-commons.spec
src/CMakeLists.txt
src/client/logic/Logic.cpp
src/common/cpu-priority/cpu-priority.h [new file with mode: 0644]
src/service/main/main.cpp

index 5d0edad1b39f98f9b8c255aba89f140ec7d5dfcc..a0798517a096a0d299f45641e810b8c2d35a2778 100644 (file)
@@ -12,6 +12,7 @@ Requires:      libcynara-commons = %{version}
 Requires(post):   smack
 BuildRequires: cmake
 BuildRequires: zip
+BuildRequires: pkgconfig(capi-system-resource)
 BuildRequires: pkgconfig(libsystemd)
 BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(cynara-commons)
index 61aad11c3dec4964859db298395fc4cf685e052b..17eadf49474388091dec54ae7bf2a08adc98d2ed 100644 (file)
@@ -98,6 +98,7 @@ Cynara tests
 
 %package -n libcynara-client
 Summary:    Cynara - client libraries
+BuildRequires: pkgconfig(capi-system-resource)
 Obsoletes:  libcynara-client-commons
 Obsoletes:  libcynara-client-async
 
index 39e2c852fe54169d42ad1de038a4a9d17cbf010b..ff5b9cd826a27314ec143952e72ff1c37b02b274 100644 (file)
@@ -46,6 +46,13 @@ SET(COMMON_DEPS
     )
 ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
 
+IF (BUILD_COMMONS OR BUILD_SERVICE)
+SET(COMMON_DEPS
+    ${COMMON_DEPS}
+    capi-system-resource
+    )
+ENDIF (BUILD_COMMONS OR BUILD_SERVICE)
+
 PKG_CHECK_MODULES(CYNARA_DEP
     REQUIRED
     ${COMMON_DEPS}
index 19da773c6105d889a4324e574d20eb7edbeea4ed..c5500246494a85453edc6199c5195200a3c10460 100644 (file)
  */
 
 #include <cinttypes>
-#include <memory>
+#include <cpu-boosting.h>
+#include <unistd.h>
 
 #include <cache/CapacityCache.h>
 #include <common.h>
 #include <config/PathConfig.h>
+#include <cpu-priority/cpu-priority.h>
 #include <cynara-error.h>
 #include <exceptions/Exception.h>
 #include <exceptions/UnexpectedErrorException.h>
 #include <plugins/NaiveInterpreter.h>
 #include <protocol/Protocol.h>
 #include <protocol/ProtocolClient.h>
-#include <request/MonitorEntriesPutRequest.h>
 #include <request/CheckRequest.h>
+#include <request/MonitorEntriesPutRequest.h>
 #include <request/pointers.h>
 #include <request/SimpleCheckRequest.h>
 #include <response/CheckResponse.h>
 #include <response/pointers.h>
 #include <response/SimpleCheckResponse.h>
 #include <sockets/SocketClient.h>
+#include <utils/CallInDestructor.h>
 
 #include <logic/Logic.h>
 
@@ -69,6 +72,16 @@ Logic::Logic(const Configuration &conf) :
 
 int Logic::check(const std::string &client, const ClientSession &session, const std::string &user,
                  const std::string &privilege) {
+
+    const auto tid = gettid();
+    if (resource_set_cpu_inheritance(tid, RESOURCE_CPU_DEST_NAME, 1000) != 0)
+        LOGE("resource_set_cpu_inheritance failed");
+
+    auto resourceCpuInheritanceClearer = CallInDestructor{[tid] {
+        if (resource_clear_cpu_inheritance(tid, RESOURCE_CPU_DEST_NAME) != 0)
+            LOGE("resource_clear_cpu_inheritance failed");
+    }};
+
     if (!ensureConnection())
         return CYNARA_API_SERVICE_NOT_AVAILABLE;
 
diff --git a/src/common/cpu-priority/cpu-priority.h b/src/common/cpu-priority/cpu-priority.h
new file mode 100644 (file)
index 0000000..486061e
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * This file is licensed under the terms of MIT License or the Apache License
+ * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
+ * See the LICENSE file or the notice below for Apache License Version 2.0
+ * details.
+ *
+ * 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.
+ */
+/**
+ * @file        src/common/cpu-priority/cpu-priority.h
+ * @author      Krzysztof MaƂysa <k.malysa@samsung.com>
+ * @version     1.0
+ * @brief       This file is the header containing common cynara CPU boosting priority constants
+ */
+
+#pragma once
+
+namespace Cynara {
+
+inline constexpr auto RESOURCE_CPU_DEST_NAME = "CYNARA_DEST";
+
+} // namespace Cynara
index 6d8c48915cb005462737fc81872f99f8eb9e0952..8e01e474aa19e8daaf557bc4e3d3aef738f51552 100644 (file)
@@ -29,6 +29,7 @@
  */
 
 #include <cerrno>
+#include <cpu-boosting.h>
 #include <cstdlib>
 #include <exception>
 #include <fcntl.h>
@@ -43,6 +44,7 @@
 #endif
 
 #include <common.h>
+#include <common/cpu-priority/cpu-priority.h>
 #include <common/error/SafeStrError.h>
 #include <log/log.h>
 
@@ -135,6 +137,12 @@ int main(int argc, char **argv) {
             }
         }
 
+        resource_pid_t self_data;
+        self_data.pid = getpid();
+        if (resource_register_cpu_inheritance_destination(Cynara::RESOURCE_CPU_DEST_NAME,
+                                                          self_data) != 0)
+            LOGE("resource_register_cpu_inheritance_destination failed");
+
         Cynara::Cynara cynara;
         LOGI("Cynara service is starting ...");
         cynara.init(openFdsLimit);
@@ -155,6 +163,9 @@ int main(int argc, char **argv) {
         LOGD("Time to clean up.");
         cynara.finalize();
         LOGD("Cynara service is stopped");
+
+        if(resource_unregister_cpu_inheritance_destination(Cynara::RESOURCE_CPU_DEST_NAME) != 0)
+            LOGE("resource_unregister_cpu_inheritance_destination failed");
     } catch (std::exception &e) {
         LOGC("Cynara stoped because of unhandled exception: %s", e.what());
         return EXIT_FAILURE;