1 /* 2 * Copyright (c) 2023-2025 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 struct TraceStatItem { 28 std::string caller; 29 bool isCallSucc; 30 bool isOverCall; 31 uint64_t latency = 0; 32 }; 33 34 struct TraceStatInfo { 35 std::string caller; 36 uint32_t failCall = 0; 37 uint32_t overCall = 0; 38 uint32_t totalCall = 0; 39 uint64_t avgLatency = 0; 40 uint64_t maxLatency = 0; 41 uint64_t totalTimeSpend = 0; 42 ToStringTraceStatInfo43 std::string ToString() const 44 { 45 std::string str; 46 str.append(caller).append(" ") 47 .append(std::to_string(failCall)).append(" ") 48 .append(std::to_string(overCall)).append(" ") 49 .append(std::to_string(totalCall)).append(" ") 50 .append(std::to_string(avgLatency)).append(" ") 51 .append(std::to_string(maxLatency)).append(" ") 52 .append(std::to_string(totalTimeSpend)); 53 return str; 54 } 55 }; 56 57 struct TraceTrafficInfo { 58 std::string caller; 59 std::string traceFile; 60 uint64_t rawSize = 0; 61 uint64_t zipSize = 0; 62 int64_t handleCostTime = 0; 63 ToStringTraceTrafficInfo64 std::string ToString() const 65 { 66 std::string str; 67 str.append(caller).append(" ") 68 .append(traceFile).append(" ") 69 .append(std::to_string(rawSize)).append(" ") 70 .append(std::to_string(zipSize)).append(" ") 71 .append(std::to_string(handleCostTime)); 72 return str; 73 } 74 }; 75 76 class TraceStatWrapper { 77 public: 78 void UpdateTraceStatInfo(uint64_t startTime, uint64_t endTime, UCollect::TraceCaller& caller, 79 const CollectResult<std::vector<std::string>>& result); 80 std::map<std::string, TraceStatInfo> GetTraceStatInfo(); 81 void ResetStatInfo(); 82 void WriteTrafficToLogFile(const std::string& trafficInfo); 83 84 private: 85 void UpdateAPIStatInfo(const TraceStatItem& item); 86 87 private: 88 std::string date_; 89 std::mutex traceMutex_; 90 std::map<std::string, TraceStatInfo> traceStatInfos_; 91 }; 92 93 class TraceDecorator : public TraceCollector, public UCDecorator { 94 public: TraceDecorator(std::shared_ptr<TraceCollector> collector)95 explicit TraceDecorator(std::shared_ptr<TraceCollector> collector) : traceCollector_(collector) {}; 96 ~TraceDecorator() = default; 97 CollectResult<std::vector<std::string>> DumpTrace(UCollect::TraceCaller caller) override; 98 CollectResult<std::vector<std::string>> DumpTraceWithDuration(UCollect::TraceCaller caller, 99 uint32_t timeLimit, uint64_t happenTime) override; 100 CollectResult<std::vector<std::string>> DumpTraceWithFilter(UCollect::TeleModule module, 101 uint32_t timeLimit, uint64_t happenTime) override; 102 CollectResult<int32_t> FilterTraceOn(UCollect::TeleModule module, uint64_t postTime) override; 103 CollectResult<int32_t> FilterTraceOff(UCollect::TeleModule module) override; 104 bool RecoverTmpTrace() override; 105 static void SaveStatSpecialInfo(); 106 static void ResetStatInfo(); 107 static void WriteTrafficAfterHandle(const TraceTrafficInfo& trace_traffic); 108 109 private: Invoke(T task,UCollect::TraceCaller & caller)110 template <typename T> auto Invoke(T task, UCollect::TraceCaller& caller) 111 { 112 uint64_t startTime = TimeUtil::GenerateTimestamp(); 113 auto result = task(); 114 if (!Parameter::IsBetaVersion() && !Parameter::IsUCollectionSwitchOn()) { 115 return result; 116 } 117 uint64_t endTime = TimeUtil::GenerateTimestamp(); 118 traceStatWrapper_.UpdateTraceStatInfo(startTime, endTime, caller, result); 119 return result; 120 } 121 122 private: 123 std::shared_ptr<TraceCollector> traceCollector_; 124 static TraceStatWrapper traceStatWrapper_; 125 }; 126 } // namespace UCollectUtil 127 } // namespace HiviewDFX 128 } // namespace OHOS 129 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_TRACE_DECORATOR_H 130