1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define LOG_TAG "DdsTrace"
17
18 #include "dds_trace.h"
19 #include <atomic>
20 #include <cinttypes>
21 #include "bytrace.h"
22 #include "log_print.h"
23 #include "time_utils.h"
24 #include "reporter.h"
25
26 namespace OHOS {
27 namespace DistributedKv {
28 static constexpr uint64_t BYTRACE_LABEL = BYTRACE_TAG_DISTRIBUTEDDATA;
29
30 std::atomic_uint DdsTrace::switchOption = DdsTrace::API_PERFORMANCE_TRACE_ON;
31 std::atomic_uint DdsTrace::indexCount = 0; // the value is changed by different thread
32 std::atomic_bool DdsTrace::isSetBytraceEnabled = false;
33
DdsTrace(const std::string & value,bool isSend)34 DdsTrace::DdsTrace(const std::string& value, bool isSend)
35 {
36 traceValue = value;
37 traceCount = ++indexCount;
38 isSendToHiview = isSend;
39 SetBytraceEnable();
40
41 Start(value);
42 }
43
~DdsTrace()44 DdsTrace::~DdsTrace()
45 {
46 Finish(traceValue);
47 }
48
SetMiddleTrace(const std::string & beforeValue,const std::string & afterValue)49 void DdsTrace::SetMiddleTrace(const std::string& beforeValue, const std::string& afterValue)
50 {
51 traceValue = afterValue;
52 Middle(beforeValue, afterValue);
53 }
54
Start(const std::string & value)55 void DdsTrace::Start(const std::string& value)
56 {
57 if (switchOption == DEBUG_CLOSE) {
58 return;
59 }
60 if ((switchOption & BYTRACE_ON) == BYTRACE_ON) {
61 StartTrace(BYTRACE_LABEL, value);
62 }
63 if ((switchOption & API_PERFORMANCE_TRACE_ON) == API_PERFORMANCE_TRACE_ON) {
64 lastTime = TimeUtils::CurrentTimeMicros();
65 }
66 ZLOGD("DdsTrace-Start: Trace[%{public}u] %{public}s In", traceCount, value.c_str());
67 }
68
Middle(const std::string & beforeValue,const std::string & afterValue)69 void DdsTrace::Middle(const std::string& beforeValue, const std::string& afterValue)
70 {
71 if (switchOption == DEBUG_CLOSE) {
72 return;
73 }
74 if (switchOption & BYTRACE_ON) {
75 MiddleTrace(BYTRACE_LABEL, beforeValue, afterValue);
76 }
77 ZLOGD("DdsTrace-Middle: Trace[%{public}u] %{public}s --- %{public}s", traceCount,
78 beforeValue.c_str(), afterValue.c_str());
79 }
80
Finish(const std::string & value)81 void DdsTrace::Finish(const std::string& value)
82 {
83 uint64_t delta = 0;
84 if (switchOption == DEBUG_CLOSE) {
85 return;
86 }
87 if (switchOption & BYTRACE_ON) {
88 FinishTrace(BYTRACE_LABEL);
89 }
90 if (switchOption & API_PERFORMANCE_TRACE_ON) {
91 delta = TimeUtils::CurrentTimeMicros() - lastTime;
92 if (isSendToHiview) {
93 Reporter::GetInstance()->ApiPerformanceStatistic()->Report({value, delta, delta, delta});
94 }
95 }
96 ZLOGD("DdsTrace-Finish: Trace[%u] %{public}s Out: %{public}" PRIu64"us.", traceCount, value.c_str(), delta);
97 }
98
SetBytraceEnable()99 bool DdsTrace::SetBytraceEnable()
100 {
101 if (isSetBytraceEnabled) {
102 return true;
103 }
104
105 UpdateTraceLabel();
106 isSetBytraceEnabled = true;
107 DdsTrace::switchOption = DdsTrace::BYTRACE_ON | DdsTrace::API_PERFORMANCE_TRACE_ON;
108
109 ZLOGD("success, current tag is true");
110 return true;
111 }
112 } // namespace OHOS
113 } // namespace DistributedKv
114