1 /*
2  * Copyright (c) 2023-2024 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 #ifndef HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_TRACE_DECORATOR_H
17 #define HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_TRACE_DECORATOR_H
18 
19 #include <mutex>
20 
21 #include "trace_collector.h"
22 #include "decorator.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace UCollectUtil {
27 const float TRACE_COMPRESS_RATIO = 0.1428; // 0.1428, an empirical value, ie. 1/7 compress ratio
28 struct TraceStatItem {
29     std::string caller;
30     bool isCallSucc;
31     bool isOverCall;
32     uint32_t latency = 0;
33 };
34 
35 const std::unordered_map<UCollect::TraceCaller, std::string> CallerMap {
36     {UCollect::RELIABILITY, "RELIABILITY"},
37     {UCollect::XPERF, "XPERF"},
38     {UCollect::XPOWER, "XPOWER"},
39     {UCollect::BETACLUB, "BETACLUB"},
40     {UCollect::DEVELOP, "DEVELOP"},
41     {UCollect::OTHER, "OTHER"}
42 };
43 
44 struct TraceStatInfo {
45     std::string caller;
46     uint32_t failCall = 0;
47     uint32_t overCall = 0;
48     uint32_t totalCall = 0;
49     uint64_t avgLatency = 0;
50     uint64_t maxLatency = 0;
51     uint64_t totalTimeSpend = 0;
52 
ToStringTraceStatInfo53     std::string ToString() const
54     {
55         std::string str;
56         str.append(caller).append(" ")
57         .append(std::to_string(failCall)).append(" ")
58         .append(std::to_string(overCall)).append(" ")
59         .append(std::to_string(totalCall)).append(" ")
60         .append(std::to_string(avgLatency)).append(" ")
61         .append(std::to_string(maxLatency)).append(" ")
62         .append(std::to_string(totalTimeSpend));
63         return str;
64     }
65 };
66 
67 struct TraceTrafficInfo {
68     std::string caller;
69     std::string traceFile;
70     uint32_t rawSize = 0;
71     uint32_t usedSize = 0;
72     uint64_t timeSpent = 0;
73     uint64_t timeStamp = 0;
74 
ToStringTraceTrafficInfo75     std::string ToString() const
76     {
77         std::string str;
78         str.append(caller).append(" ")
79         .append(traceFile).append(" ")
80         .append(std::to_string(rawSize)).append(" ")
81         .append(std::to_string(usedSize)).append(" ")
82         .append(std::to_string(timeSpent)).append(" ")
83         .append(std::to_string(timeStamp));
84         return str;
85     }
86 };
87 
88 class TraceStatWrapper {
89 public:
90     void UpdateTraceStatInfo(uint64_t startTime, uint64_t endTime, UCollect::TraceCaller& caller,
91         const CollectResult<std::vector<std::string>>& result);
92     std::map<std::string, TraceStatInfo> GetTraceStatInfo();
93     std::map<std::string, std::vector<std::string>> GetTrafficStatInfo();
94     void ResetStatInfo();
95 
96 private:
97     void UpdateAPIStatInfo(const TraceStatItem& item);
98     void UpdateTrafficInfo(const std::string& caller, uint64_t latency,
99         const CollectResult<std::vector<std::string>>& result);
100 
101 private:
102     std::mutex traceMutex_;
103     std::map<std::string, TraceStatInfo> traceStatInfos_;
104     std::map<std::string, std::vector<std::string>> trafficStatInfos_;
105 };
106 
107 class TraceDecorator : public TraceCollector, public UCDecorator {
108 public:
TraceDecorator(std::shared_ptr<TraceCollector> collector)109     TraceDecorator(std::shared_ptr<TraceCollector> collector) : traceCollector_(collector) {};
110     virtual ~TraceDecorator() = default;
111     virtual CollectResult<std::vector<std::string>> DumpTrace(UCollect::TraceCaller &caller) override;
112     virtual CollectResult<std::vector<std::string>> DumpTraceWithDuration(UCollect::TraceCaller &caller,
113         uint32_t timeLimit) override;
114     virtual CollectResult<int32_t> TraceOn() override;
115     virtual CollectResult<std::vector<std::string>> TraceOff() override;
116     static void SaveStatSpecialInfo();
117     static void SaveStatCommonInfo();
118     static void ResetStatInfo();
119 
120 private:
Invoke(T task,TraceStatWrapper & traceStatWrapper,UCollect::TraceCaller & caller)121     template <typename T> auto Invoke(T task, TraceStatWrapper& traceStatWrapper, UCollect::TraceCaller& caller)
122     {
123         uint64_t startTime = TimeUtil::GenerateTimestamp();
124         auto result = task();
125         uint64_t endTime = TimeUtil::GenerateTimestamp();
126         traceStatWrapper.UpdateTraceStatInfo(startTime, endTime, caller, result);
127         return result;
128     }
129 
130 private:
131     std::shared_ptr<TraceCollector> traceCollector_;
132     static StatInfoWrapper statInfoWrapper_;
133     static TraceStatWrapper traceStatWrapper_;
134 };
135 } // namespace UCollectUtil
136 } // namespace HiviewDFX
137 } // namespace OHOS
138 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_TRACE_DECORATOR_H
139