1 /*
2  * Copyright (c) 2023 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_DECORATOR_H
17 #define HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_DECORATOR_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "collect_result.h"
25 #include "time_util.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace UCollectUtil {
30 const std::string UC_STAT_LOG_PATH = "/data/log/hiview/unified_collection/ucollection_stat_detail.log";
31 const std::string UC_SEPARATOR = "::";
32 
33 struct StatInfo {
34     std::string name;
35     uint32_t totalCall = 0;
36     uint32_t failCall = 0;
37     uint64_t avgLatency = 0;
38     uint64_t maxLatency = 0;
39     uint64_t totalTimeSpend = 0;
40 
ToStringStatInfo41     std::string ToString() const
42     {
43         std::string str;
44         str.append(name).append(" ")
45         .append(std::to_string(totalCall)).append(" ")
46         .append(std::to_string(failCall)).append(" ")
47         .append(std::to_string(avgLatency)).append(" ")
48         .append(std::to_string(maxLatency)).append(" ")
49         .append(std::to_string(totalTimeSpend));
50         return str;
51     }
52 };
53 
54 class StatInfoWrapper {
55 public:
56     void UpdateStatInfo(uint64_t startTime, uint64_t endTime, const std::string& funcName, bool isCallSucc);
57     std::map<std::string, StatInfo> GetStatInfo();
58     void ResetStatInfo();
59 
60 private:
61     std::map<std::string, StatInfo> statInfos_;
62     std::mutex mutex_;
63 };
64 
65 class UCDecorator {
66 public:
UCDecorator()67     UCDecorator() {};
68     virtual ~UCDecorator() = default;
Invoke(T task,StatInfoWrapper & statInfoWrapper,const std::string & classFuncName)69     template <typename T> auto Invoke(T task, StatInfoWrapper& statInfoWrapper,
70         const std::string& classFuncName)
71     {
72         uint64_t startTime = TimeUtil::GenerateTimestamp();
73         auto result = task();
74         uint64_t endTime = TimeUtil::GenerateTimestamp();
75         bool isCallSucc;
76         if constexpr (std::is_same_v<std::decay_t<decltype(result)>, int>) {
77             isCallSucc = (result == UCollect::UcError::SUCCESS);
78         } else {
79             isCallSucc = (result.retCode == UCollect::UcError::SUCCESS);
80         }
81         statInfoWrapper.UpdateStatInfo(startTime, endTime, classFuncName, isCallSucc);
82         return result;
83     }
84 
85 public:
86     static void WriteLinesToFile(const std::vector<std::string>& stats, bool addBlankLine);
87 };
88 } // namespace UCollectUtil
89 } // namespace HiviewDFX
90 } // namespace OHOS
91 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_DECORATOR_H
92