diff --git a/bin/BUILD.gn b/bin/BUILD.gn index a9ddcc605c6eb7892dcafcb2faedfb7b0c1e72a5..dc1c2442d28a9c0ad244708c8449e913e0daaac9 100755 --- a/bin/BUILD.gn +++ b/bin/BUILD.gn @@ -26,6 +26,7 @@ ohos_static_library("bytrace_inner") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "startup_l2:syspara", + "startup_l2:syspara_watchagent", #"distributedschedule:samgr_standard" ] @@ -69,16 +70,23 @@ ohos_executable("bytrace") { } ohos_prebuilt_etc("bytrace.cfg") { - if (use_musl) { - source = "./config/bytrace.cfg" - } else { - source = "./config/bytrace.rc" - } + source = "./config/bytrace.cfg" relative_install_dir = "init" subsystem_name = "developtools" part_name = "bytrace_standard" } +ohos_executable("bytrace_example") { + sources = [ "example/bytrace_example.cpp" ] + deps = [ "${innerkits_path}/native:bytrace_core" ] + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + subsystem_name = "developtools" + part_name = "bytrace_standard" +} + group("bytrace_target") { - deps = [ ":bytrace" ] + deps = [ + ":bytrace", + ":bytrace_example", + ] } diff --git a/bin/example/bytrace_example.cpp b/bin/example/bytrace_example.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df4f4210a8d13ccbdd901738187df4d6ecea1c99 --- /dev/null +++ b/bin/example/bytrace_example.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include "bytrace.h" + +using namespace std; +constexpr uint64_t label = BYTRACE_TAG_OHOS; + +void func1() +{ + cout << "func 1" << endl; + sleep(1); +} + +void func2() +{ + cout << "func 2" << endl; + sleep(2); +} + +void func3() +{ + cout << "func 3" << endl; + int num = 0; + for(int i=0; i < 5; i++) { + CountTrace(BYTRACE_TAG_OHOS, "count number", ++num); + sleep(1); + } +} + +void threadFunc1() +{ + StartAsyncTrace(label, "testAsync", 1111); + for (int i = 0; i < 5; ++i) { + cout << "t1" << endl; + sleep(1); + } +} + +void threadFunc2() +{ + for (int i = 0; i < 5; ++i) { + cout << "t2" << endl; + sleep(1); + } + FinishAsyncTrace(label, "testAsync", 1111); +} + +int main() +{ + thread t1(threadFunc1); + t1.join(); + + StartTrace(label, "testStart"); + sleep(1); + + StartTrace(label, "func1Start", 1); // 打印起始点 + func1(); + FinishTrace(label, ""); + sleep(2); + + thread t2(threadFunc2); + t2.join(); + + StartTrace(label, "func2Start", 2); + func2(); + FinishTrace(label, ""); + sleep(2); + + sleep(1); + FinishTrace(label, ""); + func3(); + + return 0; +} diff --git a/bin/src/bytrace_impl.cpp b/bin/src/bytrace_impl.cpp index 5fd8e7019fd52711f3009b8b83cbc7d12bdcbd64..982250262a8f171e3d34f7317408757f9cc81875 100755 --- a/bin/src/bytrace_impl.cpp +++ b/bin/src/bytrace_impl.cpp @@ -22,6 +22,7 @@ #include #include "bytrace.h" #include "hilog/log.h" +#include "parameter.h" #include "parameters.h" using namespace std; @@ -45,6 +46,15 @@ constexpr int NAME_MAX_SIZE = 1000; static std::vector g_markTypes = {"B", "E", "S", "F", "C"}; enum MarkerType { MARKER_BEGIN, MARKER_END, MARKER_ASYNC_BEGIN, MARKER_ASYNC_END, MARKER_INT, MARKER_MAX }; +constexpr uint64_t BYTRACE_TAG = 0xd03301; +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, BYTRACE_TAG, "BytraceCore"}; + +static void ParameterChange(const char* key, const char* value, void* context) +{ + HiLog::Info(LABEL, "ParameterChange %{public}s", key); + UpdateTraceLabel(); +} + bool IsAppValid() { // Judge if application-level tracing is enabled. @@ -94,12 +104,17 @@ void OpenTraceMarkerFile() if (g_markerFd == -1) { g_markerFd = open(traceFile.c_str(), O_WRONLY | O_CLOEXEC); if (g_markerFd == -1) { - fprintf(stderr, "Error opening trace file.\n"); + HiLog::Error(LABEL, "open trace file %{public}s failed: %{public}s", traceFile.c_str(), strerror(errno)); g_tagsProperty = 0; return; } } g_tagsProperty = GetSysParamTags(); + + if (WatchParameter(KEY_TRACE_TAG.c_str(), ParameterChange, nullptr) != 0) { + HiLog::Error(LABEL, "WatchParameter %{public}s failed", KEY_TRACE_TAG.c_str()); + return; + } g_isBytraceInit = true; } }; // namespace