diff --git a/OAT.xml b/OAT.xml
old mode 100755
new mode 100644
index eead9a2d3..3f53b0694
--- a/OAT.xml
+++ b/OAT.xml
@@ -59,9 +59,13 @@ Note:If the text contains special characters, please escape them according to th
+
+
+
+
@@ -70,6 +74,13 @@ Note:If the text contains special characters, please escape them according to th
+
+
+
+
+
+
+
diff --git a/build/config.gni b/build/config.gni
index 2721b5505..777c98e4d 100755
--- a/build/config.gni
+++ b/build/config.gni
@@ -14,8 +14,7 @@
OHOS_PROFILER_DIR = get_path_info("..", "abspath")
OHOS_PROFILER_3RDPARTY_DIR = get_path_info("../../../third_party/", "abspath")
OHOS_PROFILER_3RDPARTY_GRPC_DIR = "${OHOS_PROFILER_3RDPARTY_DIR}/grpc"
-OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR =
- "${OHOS_PROFILER_3RDPARTY_DIR}/protobuf"
+OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR = "${OHOS_PROFILER_3RDPARTY_DIR}/protobuf"
OHOS_PROFILER_3RDPARTY_GOOGLETEST_DIR =
"${OHOS_PROFILER_3RDPARTY_DIR}/googletest"
@@ -31,5 +30,7 @@ print("build_l2 =", build_l2)
print("OHOS_PROFILER_DIR = ", OHOS_PROFILER_DIR)
print("OHOS_PROFILER_3RDPARTY_DIR = ", OHOS_PROFILER_3RDPARTY_DIR)
print("OHOS_PROFILER_3RDPARTY_GRPC_DIR = ", OHOS_PROFILER_3RDPARTY_GRPC_DIR)
-print("OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR = ", OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR)
-print("OHOS_PROFILER_3RDPARTY_GOOGLETEST_DIR", OHOS_PROFILER_3RDPARTY_GOOGLETEST_DIR)
+print("OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR = ",
+ OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR)
+print("OHOS_PROFILER_3RDPARTY_GOOGLETEST_DIR",
+ OHOS_PROFILER_3RDPARTY_GOOGLETEST_DIR)
diff --git a/device/BUILD.gn b/device/BUILD.gn
old mode 100755
new mode 100644
index 16555dc98..476d36642
--- a/device/BUILD.gn
+++ b/device/BUILD.gn
@@ -17,10 +17,12 @@ group("hiprofiler_targets") {
deps = [
"cmds:hiprofiler_cmd",
"plugins/api:hiprofiler_plugins",
+ "plugins/bytrace_plugin:bytraceplugin",
+ "plugins/cpu_plugin:cpudataplugin",
+ "plugins/cpu_plugin:cpudataplugintest",
"plugins/memory_plugin:memdataplugin",
"services/profiler_service:hiprofilerd",
"services/shared_memory:shared_memory",
- "plugins/bytrace_plugin:bytraceplugin",
]
}
@@ -29,6 +31,7 @@ group("unittest") {
deps = [
"base/test:unittest",
"plugins/api/test:unittest",
+ "plugins/cpu_plugin/test:unittest",
"plugins/memory_plugin/test:unittest",
"services/ipc/test:unittest",
"services/plugin_service/test:unittest",
diff --git a/device/base/BUILD.gn b/device/base/BUILD.gn
old mode 100755
new mode 100644
index 8fbd228f4..baecf98fb
--- a/device/base/BUILD.gn
+++ b/device/base/BUILD.gn
@@ -21,7 +21,7 @@ config("hiprofiler_test_config") {
cflags += [ "-g" ]
}
- if (enable_coverage && current_toolchain == host_toolchain) {
+ if (enable_coverage) {
cflags += [
# clang coverage options:
"--coverage",
@@ -36,19 +36,26 @@ config("hiprofiler_test_config") {
config("hiprofiler_base_config") {
include_dirs = [
"include",
- "//utils/native/base/include"
+ "//utils/native/base/include",
]
}
ohos_source_set("hiprofiler_base") {
sources = [
+ "src/epoll_event_poller.cpp",
+ "src/event_notifier.cpp",
+ "src/i_semaphore.cpp",
+ "src/posix_semaphore.cpp",
+ "src/pthread_semaphore.cpp",
"src/schedule_task_manager.cpp",
+ "src/std_semaphore.cpp",
]
public_configs = [
":hiprofiler_test_config",
":hiprofiler_base_config",
]
+ public_deps = [ "//utils/native/base:utilsecurec" ]
if (current_toolchain != host_toolchain) {
defines = [ "HAVE_HILOG" ]
if (build_l2) {
diff --git a/device/base/config.gni b/device/base/config.gni
old mode 100755
new mode 100644
diff --git a/device/base/include/epoll_event_poller.h b/device/base/include/epoll_event_poller.h
new file mode 100644
index 000000000..8560ddd2a
--- /dev/null
+++ b/device/base/include/epoll_event_poller.h
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * 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 EPOLL_EVENT_POLLER_H
+#define EPOLL_EVENT_POLLER_H
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include "nocopyable.h"
+
+class EpollEventPoller {
+public:
+ explicit EpollEventPoller(int timeOut);
+ ~EpollEventPoller();
+
+ using OnReadableCallback = std::function;
+ using OnWritableCallback = std::function;
+
+ bool AddFileDescriptor(int fd, const OnReadableCallback& onReadable, const OnWritableCallback& onWritable = {});
+
+ bool RemoveFileDescriptor(int fd);
+
+ bool Init();
+ bool Start();
+ bool Stop();
+ bool Finalize();
+
+private:
+ enum State {
+ INITIAL,
+ INITIED,
+ STARTED,
+ };
+ static constexpr int INVALID_FD = -1;
+ struct EventContext {
+ int fd = INVALID_FD;
+ OnReadableCallback onReadable;
+ OnWritableCallback onWritable;
+ };
+ using EventContextPtr = std::shared_ptr;
+
+ void Run();
+ bool UpdateEvent(int op, const EventContextPtr& ctx);
+ void HandleEvent(int events, const EventContext& ctx);
+
+ bool AddContextLocked(const EventContextPtr& ctx);
+ bool RemoveContextLocked(const EventContextPtr& ctx);
+
+ void OnNotify();
+ bool Notify(uint64_t value = 1);
+
+private:
+ std::mutex mutex_;
+ std::mutex vecMutex_;
+ std::thread pollThread_;
+ int timeOut_ = 0;
+ int epollFd_ = INVALID_FD;
+ int eventFd_ = INVALID_FD;
+ std::atomic state_ = INITIAL;
+ std::atomic running_ = false;
+ std::vector fileDescriptors_;
+ std::unordered_map context_;
+
+ DISALLOW_COPY_AND_MOVE(EpollEventPoller);
+};
+#endif // EPOLL_EVENT_POLLER_H
diff --git a/device/base/include/event_notifier.h b/device/base/include/event_notifier.h
new file mode 100644
index 000000000..dd43c74d5
--- /dev/null
+++ b/device/base/include/event_notifier.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * 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 EVENT_NOTIFIER_H
+#define EVENT_NOTIFIER_H
+
+#include
+#include
+#include "nocopyable.h"
+
+class EventNotifier;
+
+using EventNotifierPtr = std::shared_ptr;
+
+class EventNotifier {
+public:
+ enum {
+ NONBLOCK = (1u << 0),
+ SEMAPHORE = (1u << 1),
+ };
+
+ static EventNotifierPtr Create(unsigned int initValue = 0, unsigned int mask = 0);
+ static EventNotifierPtr CreateWithFd(int fd);
+
+ int GetFd() const;
+
+ bool IsNonBlocking() const;
+ bool IsSemaphore() const;
+
+ uint64_t Take() const;
+ bool Post(uint64_t value) const;
+
+ EventNotifier(unsigned int initValue, unsigned int mask);
+ EventNotifier(int fd);
+ ~EventNotifier();
+
+private:
+ int fd_;
+ int flags_;
+
+ DISALLOW_COPY_AND_MOVE(EventNotifier);
+};
+
+#endif // EVENT_NOTIFIER_H
\ No newline at end of file
diff --git a/device/base/include/i_semaphore.h b/device/base/include/i_semaphore.h
new file mode 100644
index 000000000..83f86c637
--- /dev/null
+++ b/device/base/include/i_semaphore.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2021 Huawei Device Co., Ltd.
+ * 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 OHOS_PROFILER_ABSTRACT_SEMAPHORE_H
+#define OHOS_PROFILER_ABSTRACT_SEMAPHORE_H
+
+#include
+#include
+
+class ISemaphore {
+public:
+ virtual ~ISemaphore() {}
+ virtual bool Wait() = 0;
+ virtual bool TryWait() = 0;
+ bool TimedWait(int seconds)
+ {
+ return TimedWait(seconds, 0);
+ }
+ virtual bool TimedWait(int seconds, int nanoSeconds) = 0;
+ virtual bool Post() = 0;
+ virtual unsigned int Value() const = 0;
+};
+using SemaphorePtr = std::shared_ptr;
+
+enum SemaphoreFactoryType : int { STD_SEMAPHORE_FACTORY, POSIX_SEMAPHORE_FACTORY, PTHREAD_SEMAPHORE_FACTORY };
+
+class ISemaphoreFactory {
+public:
+ virtual SemaphorePtr Create(unsigned int value) = 0;
+};
+
+ISemaphoreFactory& GetSemaphoreFactory(SemaphoreFactoryType type = STD_SEMAPHORE_FACTORY);
+
+#endif // OHOS_PROFILER_ABSTRACT_SEMAPHORE_H
\ No newline at end of file
diff --git a/device/base/include/logging.h b/device/base/include/logging.h
old mode 100755
new mode 100644
index 0c4b912de..a0b3d1f55
--- a/device/base/include/logging.h
+++ b/device/base/include/logging.h
@@ -13,44 +13,52 @@
* limitations under the License.
*/
-#ifndef LOGGING_H
-#define LOGGING_H
+#ifndef OHOS_PROFILER_LOGGING_H
+#define OHOS_PROFILER_LOGGING_H
+
+#undef NDEBUG
#ifndef LOG_TAG
-#define LOG_TAG ""
+#define LOG_TAG "Hiprofiler"
+#endif
+
+#define PROFILER_SUBSYSTEM 0x0000
+#ifndef LOG_DOMAIN
+#define LOG_DOMAIN PROFILER_SUBSYSTEM
+#endif
+
+#ifndef UNUSED_PARAMETER
+#define UNUSED_PARAMETER(x) ((void)x)
#endif
#ifdef HAVE_HILOG
#include "hilog/log.h"
-
-#undef LOG_TAG
-#define LOG_TAG "Hiprofiler"
-
-#else
+#include
+#else // HAVE_HILOG
#include
#include
#include
-#include
#include
+#include
#include
#include
#include
-static inline long GetTid()
+static inline long GetTid(void)
{
return syscall(SYS_gettid);
}
enum {
- HILOG_UNKNOWN = 0,
- HILOG_DEFAULT,
- HILOG_VERBOSE,
- HILOG_DEBUG,
- HILOG_INFO,
- HILOG_WARN,
- HILOG_ERROR,
- HILOG_FATAL,
- HILOG_SILENT,
+ LOG_UNKNOWN = 0,
+ LOG_DEFAULT,
+ LOG_VERBOSE,
+ LOG_DEBUG,
+ LOG_INFO,
+ LOG_WARN,
+ LOG_ERROR,
+ LOG_FATAL,
+ LOG_SILENT,
};
namespace {
@@ -66,60 +74,136 @@ static inline std::string GetTimeStr()
localtime_r(&ts.tv_sec, &tmStruct);
size_t used = strftime(timeStr, sizeof(timeStr), "%m-%d %H:%M:%S", &tmStruct);
snprintf_s(&timeStr[used], sizeof(timeStr) - used, sizeof(timeStr) - used - 1, ".%03ld",
- ts.tv_nsec / NS_PER_MS_LOG);
+ ts.tv_nsec / NS_PER_MS_LOG);
return timeStr;
}
-typedef const char *ConstCharPtr;
+typedef const char* ConstCharPtr;
-static inline int HiLogPrintArgs(int prio, ConstCharPtr tag, ConstCharPtr fmt, va_list vargs)
+static inline int HiLogPrintArgs(int prio, int domain, ConstCharPtr tag, ConstCharPtr fmt, va_list vargs)
{
static std::mutex mtx;
static std::vector prioNames = {"U", " ", "V", "D", "I", "W", "E", "F", "S"};
std::unique_lock lock(mtx);
- int count =
- fprintf(stderr, "%s %7d %7ld %5s %s ", GetTimeStr().c_str(), getpid(), GetTid(), prioNames[prio].c_str(), tag);
+ int count = fprintf(stderr, "%04x %s %7d %7ld %5s %s ", domain, GetTimeStr().c_str(), getpid(), GetTid(),
+ prioNames[prio].c_str(), tag);
+ if (count < 0) {
+ return 0;
+ }
count += vfprintf(stderr, fmt, vargs);
count += fprintf(stderr, "\n");
+ fflush(stderr);
return count;
}
-static inline int __hilog_log_print(int prio, ConstCharPtr tag, ConstCharPtr fmt, ...)
+static inline int HiLogPrint(int type, int prio, int domain, ConstCharPtr tag, ConstCharPtr fmt, ...)
{
- int count;
+ int count = 0;
va_list vargs;
+ UNUSED_PARAMETER(type);
va_start(vargs, fmt);
- count = HiLogPrintArgs(prio, tag, fmt, vargs);
+ count = HiLogPrintArgs(prio, domain, tag, fmt, vargs);
va_end(vargs);
return count;
}
-#define HILOG_DEBUG(LOG_CORE, fmt, ...) __hilog_log_print(HILOG_DEBUG, LOG_TAG, fmt, ##__VA_ARGS__)
-#define HILOG_INFO(LOG_CORE, fmt, ...) __hilog_log_print(HILOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
-#define HILOG_WARN(LOG_CORE, fmt, ...) __hilog_log_print(HILOG_WARN, LOG_TAG, fmt, ##__VA_ARGS__)
-#define HILOG_ERROR(LOG_CORE, fmt, ...) __hilog_log_print(HILOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
-
+#ifndef LOG_CORE
+#define LOG_CORE 0
#endif
+#define HILOG_DEBUG(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
+#define HILOG_INFO(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_INFO, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
+#define HILOG_WARN(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_WARN, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
+#define HILOG_ERROR(LOG_CORE, fmt, ...) HiLogPrint(LOG_CORE, LOG_ERROR, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__)
+
+#endif // HAVE_HILOG
+
+#ifndef NDEBUG
+#include
+namespace logging {
+inline void StringReplace(std::string& str, const std::string& oldStr, const std::string& newStr)
+{
+ std::string::size_type pos = 0u;
+ while ((pos = str.find(oldStr, pos)) != std::string::npos) {
+ str.replace(pos, oldStr.length(), newStr);
+ pos += newStr.length();
+ }
+}
+
+static inline std::string StringFormat(const char* fmt, ...)
+{
+ va_list vargs;
+ char buf[1024] = {0};
+ std::string format(fmt);
+ StringReplace(format, "%{public}", "%");
+
+ va_start(vargs, fmt);
+ if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format.c_str(), vargs) < 0) {
+ return nullptr;
+ }
+
+ va_end(vargs);
+ return buf;
+}
+} // logging
+
+#ifdef HILOG_DEBUG
+#undef HILOG_DEBUG
+#endif
+
+#ifdef HILOG_INFO
+#undef HILOG_INFO
+#endif
+
+#ifdef HILOG_WARN
+#undef HILOG_WARN
+#endif
+
+#ifdef HILOG_ERROR
+#undef HILOG_ERROR
+#endif
+
+#ifdef HILOG_PRINT
+#undef HILOG_PRINT
+#endif
+
+#ifdef HAVE_HILOG
+#define HILOG_PRINT(type, level, fmt, ...) \
+ HiLogPrint(type, level, LOG_DOMAIN, LOG_TAG, "%{public}s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
+#else
+#define HILOG_PRINT(type, level, fmt, ...) \
+ HiLogPrint(type, level, LOG_DOMAIN, LOG_TAG, "%s", logging::StringFormat(fmt, ##__VA_ARGS__).c_str())
+#endif
+
+#define HILOG_DEBUG(type, fmt, ...) HILOG_PRINT(type, LOG_DEBUG, fmt, ##__VA_ARGS__)
+#define HILOG_INFO(type, fmt, ...) HILOG_PRINT(type, LOG_INFO, fmt, ##__VA_ARGS__)
+#define HILOG_WARN(type, fmt, ...) HILOG_PRINT(type, LOG_WARN, fmt, ##__VA_ARGS__)
+#define HILOG_ERROR(type, fmt, ...) HILOG_PRINT(type, LOG_ERROR, fmt, ##__VA_ARGS__)
+#endif // NDEBUG
+
#define STD_PTR(K, T) std::K##_ptr
-#define CHECK_NOTNULL(ptr, retval, fmt, ...) \
- do { \
- if (ptr == nullptr) { \
- HILOG_WARN(LOG_CORE, "CHECK_NOTNULL(%s) in %s:%d FAILED, " fmt, #ptr, __func__, __LINE__, ##__VA_ARGS__); \
- return retval; \
- } \
- } while (0)
+#define NO_RETVAL /* retval */
-#define CHECK_TRUE(expr, retval, fmt, ...) \
+#define CHECK_NOTNULL(ptr, retval, fmt, ...) \
do { \
- if (!(expr)) { \
- HILOG_WARN(LOG_CORE, "CHECK_TRUE(%s) in %s:%d FAILED, " fmt, #expr, __func__, __LINE__, ##__VA_ARGS__); \
+ if (ptr == nullptr) { \
+ HILOG_WARN(LOG_CORE, "CHECK_NOTNULL(%{public}s) in %{public}s:%{public}d FAILED, " fmt, #ptr, __func__, \
+ __LINE__, ##__VA_ARGS__); \
return retval; \
} \
} while (0)
+#define CHECK_TRUE(expr, retval, fmt, ...) \
+ do { \
+ if (!(expr)) { \
+ HILOG_WARN(LOG_CORE, "CHECK_TRUE(%{public}s) in %{public}s:%{public}d FAILED, " fmt, #expr, __func__, \
+ __LINE__, ##__VA_ARGS__); \
+ return retval; \
+ } \
+ } while (0)
+
#define RETURN_IF(expr, retval, fmt, ...) \
do { \
if ((expr)) { \
diff --git a/device/base/include/schedule_task_manager.h b/device/base/include/schedule_task_manager.h
index 0ce47f5dc..9edc9e460 100644
--- a/device/base/include/schedule_task_manager.h
+++ b/device/base/include/schedule_task_manager.h
@@ -13,11 +13,12 @@
* limitations under the License.
*/
-#ifndef SCHEDULE_TASK_MANAGER_H
-#define SCHEDULE_TASK_MANAGER_H
+#ifndef OHOS_PROFILER_SCHEDULE_TASK_MANAGER_H
+#define OHOS_PROFILER_SCHEDULE_TASK_MANAGER_H
#include
#include
+#include
#include
#include