Boost up cynara thread 02/299802/3
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 10 Oct 2023 10:25:19 +0000 (19:25 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 10 Oct 2023 11:06:26 +0000 (20:06 +0900)
In some device, the cynara thread is very slow. It makes a launch failure issue.
This patch applies the cpu boost to the cynara thread of amd when amd needs
to check whether the caller has permissions or not.

Change-Id: Ice8632bc530f23259961f1f1d8d78b1c890277f3
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/modules/cynara-core/cynara_manager.cc
src/modules/cynara-core/cynara_manager.hh

index 4fa6544..cdc2106 100644 (file)
@@ -61,8 +61,46 @@ bool IsCpuBoostingHighLevel(pid_t pid) {
   return false;
 }
 
-void SetComm() {
-  pid_t tid = syscall(__NR_gettid);
+void DoBoost(pid_t tid) {
+  if (tid < 1)
+    return;
+
+  resource_pid_t res_pid = {
+    .pid = 0,
+    .tid = &tid,
+    .tid_count = 1,
+  };
+
+  int ret = resource_set_cpu_boosting(res_pid, CPU_BOOSTING_LEVEL_MEDIUM,
+      static_cast<cpu_boosting_flag_e>(0), -1);
+  if (ret != 0) {
+    _E("resource_set_cpu_boosting() is failed. error: %d", ret);
+    return;
+  }
+
+  _D("resource_set_cpu_boosting() is successful. tid(%d)", tid);
+}
+
+void ClearBoost(pid_t tid) {
+    if (tid < 1)
+    return;
+
+  resource_pid_t res_pid = {
+      .pid = 0,
+      .tid = &tid,
+      .tid_count = 1,
+  };
+
+  int ret = resource_clear_cpu_boosting(res_pid);
+  if (ret!= 0) {
+    _E("resource_clear_cpu_boosting() is failed. error: %d", ret);
+    return;
+  }
+
+  _D("resource_clear_cpu_boosting() is successful. tid(%d)", tid);
+}
+
+void SetComm(pid_t tid) {
   std::string path = "/proc/" + std::to_string(tid) + "/comm";
   int fd = open(path.c_str(), O_WRONLY);
   if (fd < 0) {
@@ -174,6 +212,7 @@ void CynaraManager::Dispose() {
   if (disposed_)
     return;
 
+  UnsetBoostingTimer();
   SendRequest(std::make_shared<CynaraRequest>(true));
   thread_.join();
   disposed_ = true;
@@ -288,7 +327,30 @@ CynaraManager::~CynaraManager() {
   Dispose();
 }
 
+void CynaraManager::SetBoostingTimer() {
+  if (boosting_timer_ == 0)
+    DoBoost(tid_);
+  else
+    g_source_remove(boosting_timer_);
+
+  boosting_timer_ = g_timeout_add(5000,
+      [](gpointer user_data) {
+        auto* cynara_manager = static_cast<CynaraManager*>(user_data);
+        cynara_manager->boosting_timer_ = 0;
+        ClearBoost(cynara_manager->tid_);
+        return G_SOURCE_REMOVE;
+      }, this);
+}
+
+void CynaraManager::UnsetBoostingTimer() {
+  if (boosting_timer_ != 0) {
+    g_source_remove(boosting_timer_);
+    boosting_timer_ = 0;
+  }
+}
+
 void CynaraManager::SendRequest(std::shared_ptr<CynaraRequest> request) {
+  SetBoostingTimer();
   queue_.Push(std::move(request));
 }
 
@@ -311,7 +373,8 @@ void CynaraManager::Init() {
 
 void CynaraManager::CynaraThread() {
   _W("BEGIN");
-  SetComm();
+  tid_ = syscall(__NR_gettid);
+  SetComm(tid_);
 
   char err_buf[128];
   cynara* cynara = nullptr;
index 7045671..d207d30 100644 (file)
@@ -18,6 +18,8 @@
 #define CYNARA_MANAGER_HH_
 
 #include <glib.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include <list>
 #include <memory>
@@ -59,6 +61,8 @@ class CynaraManager {
   void CynaraThread();
   static gboolean RequestCb(gpointer user_data);
   static gboolean ResponseCb(gpointer user_data);
+  void SetBoostingTimer();
+  void UnsetBoostingTimer();
 
  private:
   bool disposed_ = true;
@@ -67,6 +71,8 @@ class CynaraManager {
   std::unordered_map<unsigned int, std::shared_ptr<CynaraResponseCb>> res_cbs_;
   std::unordered_map<int, std::shared_ptr<CynaraChecker>> checkers_;
   std::list<std::shared_ptr<CynaraSubChecker>> sub_checkers_;
+  pid_t tid_ = 0;
+  guint boosting_timer_ = 0;
 };
 
 }  // namespace cynara_core