Add a new internal api 35/305235/7
authorChanggyu Choi <changyu.choi@samsung.com>
Tue, 30 Jan 2024 07:43:07 +0000 (16:43 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Tue, 6 Feb 2024 00:48:16 +0000 (09:48 +0900)
Adds:
 - tizen_core_set_cpu_boosting_level()

Change-Id: I8c4303071f11f48b059b7a8a739b4aab11e0beb9
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
CMakeLists.txt
include/tizen_core_internal.h
packaging/tizen-core.spec
tizen_base/CMakeLists.txt
tizen_base/stub.cc
tizen_base/task.cc
tizen_base/task.h

index 76ccbbb4d2230564efd84e21dccbcd5a6ec97710..a28f1171a228bfe2b6da512a9aa7ad55ee73c3f1 100644 (file)
@@ -40,6 +40,7 @@ PKG_CHECK_MODULES(GLIB_DEPS REQUIRED glib-2.0)
 PKG_CHECK_MODULES(GMOCK_DEPS REQUIRED gmock)
 PKG_CHECK_MODULES(CAPI_BASE_COMMON_DEPS REQUIRED capi-base-common)
 PKG_CHECK_MODULES(TIZEN_SHARED_QUEUE_DEPS REQUIRED tizen-shared-queue)
+PKG_CHECK_MODULES(CAPI_SYSTEM_RESOURCE_DEPS REQUIRED capi-system-resource)
 
 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/${TARGET_TIZEN_CORE}.pc.in
   ${CMAKE_SOURCE_DIR}/${TARGET_TIZEN_CORE}.pc @ONLY)
index d2310c3bbfedb2e3a67123c6c04589bc835d05be..86b85e44f1e90bfc22d39a3d6026907c9a3f2c7c 100644 (file)
 extern "C" {
 #endif
 
+/**
+ * @brief Enumeration for the tizen core cpu boosting level.
+ * @since_tizen 9.0
+ */
+typedef enum {
+  TIZEN_CORE_CPU_BOOSTING_LEVEL_NONE,    /**< None level */
+  TIZEN_CORE_CPU_BOOSTING_LEVEL_STRONG,  /**< Strong level */
+  TIZEN_CORE_CPU_BOOSTING_LEVEL_MEDIUM,  /**< Medium level */
+  TIZEN_CORE_CPU_BOOSTING_LEVEL_WEAK,    /**< Weak level */
+} tizen_core_cpu_boosting_level_e;
+
 /**
  * @brief Gets the glib context from the tizen core handle.
  * @since_tizen 9.0
@@ -33,6 +44,22 @@ extern "C" {
  */
 void *tizen_core_get_glib_context(tizen_core_h h);
 
+/**
+ * @brief Sets cpu boosting level of the tizen core.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core handle
+ * @param[in] level The cpu boosting level
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #TIZEN_CORE_ERROR_INVALID_CONTEXT The core is not running
+ * @see tizen_core_add_source()
+ */
+int tizen_core_set_cpu_boosting_level(tizen_core_h h,
+                                      tizen_core_cpu_boosting_level_e level);
+
 #ifdef __cplusplus
 }
 #endif
index 35969cc5b729cba7431b51d3207a8fb864292a2d..a4c8b5a5e2b9a6f02aeb59da09cc0b3cf2cbc284 100644 (file)
@@ -13,6 +13,7 @@ BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(gmock)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(tizen-shared-queue)
+BuildRequires:  pkgconfig(capi-system-resource)
 
 %if 0%{?gcov:1}
 BuildRequires:  lcov
index 2cfbc68f4bab7a4ba4a7839327298d91f5b95ac2..dbd09ba869e52d5b70e44392ff11e164b8fa9c80 100644 (file)
@@ -22,6 +22,7 @@ APPLY_PKG_CONFIG(${TARGET_TIZEN_CORE} PUBLIC
   GLIB_DEPS
   CAPI_BASE_COMMON_DEPS
   TIZEN_SHARED_QUEUE_DEPS
+  CAPI_SYSTEM_RESOURCE_DEPS
 )
 
 INSTALL(TARGETS ${TARGET_TIZEN_CORE} DESTINATION ${LIB_INSTALL_DIR})
index 6386f05c3d1a77a7d032403b9590addd45ff04bf..0d54cb44f1331d07891376311a6d295ed0cff55b 100644 (file)
@@ -370,6 +370,20 @@ API int tizen_core_remove_source(tizen_core_h h, tizen_core_source_h source) {
   return TIZEN_CORE_ERROR_NONE;
 }
 
+API int tizen_core_set_cpu_boosting_level(tizen_core_h h,
+    tizen_core_cpu_boosting_level_e level) {
+  if (h == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* core = static_cast<tizen_base::tizen_core::Task*>(h);
+  if (!core->SetCpuBoostingLevel(level))
+    return TIZEN_CORE_ERROR_INVALID_CONTEXT;
+
+  return TIZEN_CORE_ERROR_NONE;
+}
+
 API int tizen_core_source_create(tizen_core_source_h* h) {
   if (h == nullptr) {
     _E("Invalid parameter");
index 9fc5b85dd571ca075a4c4310ca629a28ce357fe0..b3b5690a0eb4422edcae036befe965dbe1d9a362 100644 (file)
@@ -16,7 +16,9 @@
 
 #include "tizen_base/task.h"
 
+#include <cpu-boosting.h>
 #include <glib.h>
+#include <unistd.h>
 
 #include <stdexcept>
 #include <utility>
@@ -102,13 +104,13 @@ class EventSource : public Source {
  public:
   EventSource(std::shared_ptr<Task> task,
               std::shared_ptr<event::EventBroker<T>> broker)
-      : Source(nullptr), task_(std::move(task)), broker_(std::move(broker)) {
-  }
+      : Source(nullptr), task_(std::move(task)), broker_(std::move(broker)) {}
 
   void Emit(std::shared_ptr<event::EventObject<T>> object) {
     broker_->Emit(std::move(object));
     auto source = task_->AddIdleJob([=]() {
-      while (!broker_->Empty()) broker_->Process();
+      while (!broker_->Empty())
+        broker_->Process();
       return false;
     });
     auto* idle_source = static_cast<IdleSource*>(source.get());
@@ -120,6 +122,37 @@ class EventSource : public Source {
   std::shared_ptr<event::EventBroker<T>> broker_;
 };
 
+resource_cpu_boosting_level ConvertCpuBoostingLevel(
+    tizen_core_cpu_boosting_level_e level) {
+  switch (level) {
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_NONE:
+      return CPU_BOOSTING_LEVEL_NONE;
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_STRONG:
+      return CPU_BOOSTING_LEVEL_STRONG;
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_MEDIUM:
+      return CPU_BOOSTING_LEVEL_MEDIUM;
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_WEAK:
+      return CPU_BOOSTING_LEVEL_WEAK;
+    default:
+      return CPU_BOOSTING_LEVEL_NONE;
+  }
+}
+
+const char* CpuBoostingLevelToStr(tizen_core_cpu_boosting_level_e level) {
+  switch (level) {
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_NONE:
+      return "NONE";
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_STRONG:
+      return "STRONG";
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_MEDIUM:
+      return "MEDIUM";
+    case TIZEN_CORE_CPU_BOOSTING_LEVEL_WEAK:
+      return "WEAK";
+    default:
+      return "NONE";
+  }
+}
+
 }  // namespace
 
 Task::Task(std::string name, bool use_thread)
@@ -162,10 +195,18 @@ void Task::Quit() {
 }
 
 void Task::Run() {
-  if (use_thread_)
-    thread_ = std::thread([&]() -> void { ThreadLoop(); });
-  else
+  if (use_thread_) {
+    thread_ = std::thread([&]() -> void {
+      tid_ = gettid();
+      ThreadLoop();
+      tid_ = -1;
+      cpu_boosting_level_ = TIZEN_CORE_CPU_BOOSTING_LEVEL_NONE;
+    });
+  } else {
+    tid_ = getpid();
     g_main_loop_run(loop_);
+    tid_ = -1;
+  }
 }
 
 bool Task::IsRunning() const {
@@ -229,6 +270,36 @@ void Task::EmitEvent(std::shared_ptr<event::EventObject<T>> object) {
   }
 }
 
+bool Task::SetCpuBoostingLevel(tizen_core_cpu_boosting_level_e level) {
+  std::lock_guard<std::recursive_mutex> lock(mutex_);
+  if (tid_ == -1) {
+    _E("Not running yet.");
+    return false;
+  }
+
+  if (cpu_boosting_level_ == level)
+    return true;
+
+  resource_pid_t res_pid = {
+      .pid = 0,
+      .tid = &tid_,
+      .tid_count = 1,
+  };
+
+  resource_cpu_boosting_level native_level = ConvertCpuBoostingLevel(level);
+  int ret = resource_set_cpu_boosting(res_pid, native_level,
+                                      static_cast<cpu_boosting_flag_e>(0), -1);
+  if (ret != 0) {
+    _E("Failed to set cpu boosting. ret(%d)", ret);
+    return false;
+  }
+
+  _I("Set cpu boosting level(%s -> %s).",
+      CpuBoostingLevelToStr(cpu_boosting_level_), CpuBoostingLevelToStr(level));
+  cpu_boosting_level_ = level;
+  return true;
+}
+
 void Task::ThreadLoop() {
   g_main_context_push_thread_default(context_->GetHandle());
   g_main_loop_run(loop_);
index 5cc2cfab9abe1a13d2331ad64e03f68f82a95fcd..c24572ec122e75b350f6e3f9fa8e9b687f0250b2 100644 (file)
@@ -26,6 +26,7 @@
 #include <string>
 #include <thread>
 
+#include "tizen_core_internal.h"
 #include "tizen_base/channel/receiver.h"
 #include "tizen_base/context.h"
 #include "tizen_base/event/event_broker.h"
@@ -73,10 +74,15 @@ class EXPORT_API Task : public ILoop,
   template <typename T>
   void EmitEvent(std::shared_ptr<event::EventObject<T>> object);
 
+  bool SetCpuBoostingLevel(tizen_core_cpu_boosting_level_e level);
+
  private:
   void ThreadLoop();
 
  private:
+  pid_t tid_ = -1;
+  tizen_core_cpu_boosting_level_e cpu_boosting_level_ =
+      TIZEN_CORE_CPU_BOOSTING_LEVEL_NONE;
   std::string name_;
   bool use_thread_;
   std::shared_ptr<Context> context_;