add WatchParameter and example

Signed-off-by: chuxuezhe11 <hanjixiao@huawei.com>
This commit is contained in:
chuxuezhe11
2021-11-24 03:11:46 -08:00
parent 25a0d3577c
commit a6d2c592c4
3 changed files with 107 additions and 8 deletions
+15 -7
View File
@@ -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"
}
group("bytrace_target") {
deps = [ ":bytrace" ]
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",
":bytrace_example",
]
}
+76
View File
@@ -0,0 +1,76 @@
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <thread>
#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;
}
+16 -1
View File
@@ -22,6 +22,7 @@
#include <vector>
#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<std::string> 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