Add blink feature for daemon processes 93/309293/6
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 8 Apr 2024 08:42:48 +0000 (17:42 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 8 Apr 2024 10:46:16 +0000 (19:46 +0900)
The term "blink" is utilized to denote momentary movement.
If the environmental variable "AUL_BLINK=1" is present, the AUL library will
initiate boosting for the process as soon as it is loaded.
The "AUL_BLINK_TIMEOUT" is a variable that enables the setting of
a time-out duration. The unit of measurement is milliseconds, with
a default value of 5000 milliseconds.

Change-Id: I93dd570b7d442d0033e409b2f9a6a352f864ccbd
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
CMakeLists.txt
packaging/aul.spec
src/CMakeLists.txt
src/aul/CMakeLists.txt
src/blink/CMakeLists.txt [new file with mode: 0644]
src/blink/aul_blink.cc [new file with mode: 0644]
src/blink/log_private.hh [new file with mode: 0644]

index f432afe..14e86ac 100644 (file)
@@ -36,6 +36,7 @@ SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
 ## Target
 SET(TARGET_AUL "aul")
 SET(TARGET_AUL_SERVER "aul-server")
+SET(TARGET_AUL_BLINK "aul-blink")
 
 ENABLE_TESTING()
 SET(TARGET_AUL_UNIT_TESTS "aul-unit-tests")
index 99f2307..4e8cfc9 100644 (file)
@@ -272,6 +272,7 @@ chsmack -a 'User::Home' %{TZ_SYS_DB}/.component.db-journal
 %license LICENSE
 %manifest %{name}.manifest
 %attr(0644,root,root) %{_libdir}/libaul.so.*
+%attr(0644,root,root) %{_libdir}/libaul-blink.so
 %config %{_sysconfdir}/dbus-1/system.d/aul.conf
 %{_bindir}/aulctl
 %{_bindir}/aul_test
index 341502b..b0db260 100644 (file)
@@ -1,4 +1,5 @@
 ADD_SUBDIRECTORY(aul)
+ADD_SUBDIRECTORY(blink)
 ADD_SUBDIRECTORY(parser)
 ADD_SUBDIRECTORY(rsc-mgr)
 ADD_SUBDIRECTORY(server)
index 2d9de98..f09929d 100644 (file)
@@ -53,6 +53,8 @@ APPLY_PKG_CONFIG(${TARGET_AUL} PUBLIC
   XDGMIME_DEPS
 )
 
+TARGET_LINK_LIBRARIES(${TARGET_AUL} PRIVATE ${TARGET_AUL_BLINK})
+
 INSTALL(TARGETS ${TARGET_AUL}
   DESTINATION ${LIB_INSTALL_DIR}
   COMPONENT RuntimeLibraries)
diff --git a/src/blink/CMakeLists.txt b/src/blink/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4fe5d30
--- /dev/null
@@ -0,0 +1,18 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SRCS)
+
+ADD_LIBRARY(${TARGET_AUL_BLINK} SHARED ${SRCS})
+
+TARGET_INCLUDE_DIRECTORIES(${TARGET_AUL_BLINK} PUBLIC
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_SOURCE_DIR}/../
+)
+
+APPLY_PKG_CONFIG(${TARGET_AUL_BLINK} PUBLIC
+  CAPI_SYSTEM_RESOURCE_DEPS
+  DLOG_DEPS
+  GLIB_DEPS
+)
+
+INSTALL(TARGETS ${TARGET_AUL_BLINK}
+  DESTINATION ${LIB_INSTALL_DIR}
+  COMPONENT RuntimeLibraries)
diff --git a/src/blink/aul_blink.cc b/src/blink/aul_blink.cc
new file mode 100644 (file)
index 0000000..13d8e76
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <cpu-boosting.h>
+#include <glib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <cctype>
+
+#include "blink/log_private.hh"
+
+namespace aul {
+
+constexpr const char kAulBlink[] = "AUL_BLINK";
+constexpr const char kAulBlinkTimeout[] = "AUL_BLINK_TIMEOUT";
+
+class Blink {
+ public:
+  Blink() {
+    const char* blink_str = getenv(kAulBlink);
+    if (blink_str && !strcmp(blink_str, "1")) {
+      const char* timeout = getenv(kAulBlinkTimeout);
+      if (timeout && isdigit(timeout[0]))
+        timeout_ = atoi(timeout);
+
+      Set();
+    }
+  }
+
+  ~Blink() { Clear(); }
+
+ private:
+  void Set() {
+    pid_t pid = getpid();
+    resource_pid_t res_pid = {
+        .pid = 0,
+        .tid = &pid,
+        .tid_count = 1,
+    };
+
+    int ret =
+        resource_set_cpu_boosting(res_pid, CPU_BOOSTING_LEVEL_STRONG,
+                                  CPU_BOOSTING_RESET_ON_FORK, -1);
+    if (ret != 0) {
+      _E("resource_set_cpu_boosting() is failed. error=%d", ret);
+      return;
+    }
+
+    source_ = g_timeout_add(timeout_, OnTimeout, this);
+    enabled_ = true;
+    _W("Set");
+  }
+
+  void Clear() {
+    if (source_ != 0) {
+      g_source_remove(source_);
+      source_ = 0;
+    }
+
+    if (enabled_) {
+      pid_t pid = getpid();
+      resource_pid_t res_pid = {
+          .pid = 0,
+          .tid = &pid,
+          .tid_count = 1,
+      };
+      resource_clear_cpu_boosting(res_pid);
+      enabled_ = false;
+      _W("Clear");
+    }
+  }
+
+  static gboolean OnTimeout(gpointer user_data) {
+    auto* blink = static_cast<Blink*>(user_data);
+    blink->source_ = 0;
+    blink->Clear();
+    return G_SOURCE_REMOVE;
+  }
+
+ private:
+  bool enabled_ = false;
+  int timeout_ = 5000;
+  guint source_ = 0;
+};
+
+Blink blink;
+
+}  // namespace aul
diff --git a/src/blink/log_private.hh b/src/blink/log_private.hh
new file mode 100644 (file)
index 0000000..56a5511
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 BLINK_LOG_PRIVATE_HH_
+#define BLINK_LOG_PRIVATE_HH_
+
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "AUL_BLINK"
+
+#undef _E
+#define _E LOGE
+
+#undef _W
+#define _W LOGW
+
+#undef _I
+#define _I LOGI
+
+#undef _D
+#define _D LOGD
+
+#endif  // BLINK_LOG_PRIVATE_HH_