• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 HIVIEWDFX_HIVIEW_TRACE_STRATEGY_H
17 #define HIVIEWDFX_HIVIEW_TRACE_STRATEGY_H
18 #include <memory>
19 #include <string>
20 #include <unistd.h>
21 #include <vector>
22 
23 #include "trace_caller.h"
24 #include "trace_flow_controller.h"
25 #include "trace_state_machine.h"
26 #include "trace_utils.h"
27 
28 namespace OHOS::HiviewDFX {
29 
30 class TraceStrategy {
31 public:
TraceStrategy(int32_t maxDuration,uint64_t happenTime,const std::string & caller,TraceScenario scenario)32     TraceStrategy(int32_t maxDuration, uint64_t happenTime, const std::string &caller, TraceScenario scenario)
33         : maxDuration_(maxDuration), happenTime_(happenTime), caller_(caller), scenario_(scenario) {}
34     virtual ~TraceStrategy() = default;
35     virtual TraceRet DoDump(std::vector<std::string> &outputFile) = 0;
36 
37 protected:
38     int32_t maxDuration_;
39     uint64_t happenTime_;
40     std::string caller_;
41     TraceScenario scenario_;
42 
43     virtual void DoClean(const std::string &tracePath, uint32_t threshold, bool hasPrefix);
44     virtual bool IsMine(const std::string &fileName) = 0;
45     TraceRet DumpTrace(DumpEvent &dumpEvent, TraceRetInfo &traceRetInfo) const;
46 };
47 
48 // Develop strategy: no flow control, trace put in /data/log/hiview/unified_collection/trace/special dir
49 class TraceDevStrategy : public TraceStrategy {
50 public:
TraceDevStrategy(int32_t maxDuration,uint64_t happenTime,const std::string & caller,TraceScenario scenario)51     TraceDevStrategy(int32_t maxDuration, uint64_t happenTime, const std::string &caller, TraceScenario scenario)
52         : TraceStrategy(maxDuration, happenTime, caller, scenario) {}
53     TraceRet DoDump(std::vector<std::string> &outputFile) override;
54 
55 protected:
56     bool IsMine(const std::string &fileName) override;
57 };
58 
59 // flow control strategy: flow control db caller, trace put in /data/log/hiview/unified_collection/trace/share dir
60 class TraceFlowControlStrategy : public TraceStrategy {
61 public:
TraceFlowControlStrategy(int32_t maxDuration,uint64_t happenTime,const std::string & caller)62     TraceFlowControlStrategy(int32_t maxDuration, uint64_t happenTime, const std::string &caller)
63         : TraceStrategy(maxDuration, happenTime, caller, TraceScenario::TRACE_COMMON)
64     {
65         flowController_ = std::make_shared<TraceFlowController>(caller);
66     }
67     TraceRet DoDump(std::vector<std::string> &outputFile) override;
68 
69 protected:
70     bool IsMine(const std::string &fileName) override;
71 
72 private:
73     std::shared_ptr<TraceFlowController> flowController_ = nullptr;
74 };
75 
76 /*
77  * MixedStrategy: Develop strategy + flow control strategy
78  * first: no flow control, trace put in /data/log/hiview/unified_collection/trace/special dir
79  * second: flow control db caller, according flow control result and other condition,
80  *     decide whether put trace in /data/log/hiview/unified_collection/trace/share dir
81 */
82 class TraceMixedStrategy : public TraceStrategy {
83 public:
TraceMixedStrategy(int32_t maxDuration,uint64_t happenTime,const std::string & caller)84     TraceMixedStrategy(int32_t maxDuration, uint64_t happenTime, const std::string &caller)
85         : TraceStrategy(maxDuration, happenTime, caller, TraceScenario::TRACE_COMMON)
86     {
87         flowController_ = std::make_shared<TraceFlowController>(caller);
88     }
89     TraceRet DoDump(std::vector<std::string> &outputFile) override;
90 
91 protected:
92     bool IsMine(const std::string &fileName) override;
93 
94 private:
95     std::shared_ptr<TraceFlowController> flowController_ = nullptr;
96 };
97 
98 // Only telemetry to dump trace
99 class TelemetryStrategy : public TraceStrategy  {
100 public:
TelemetryStrategy(const std::vector<int32_t> & pidList,int32_t maxDuration,uint64_t happenTime,const std::string & module)101     TelemetryStrategy(const std::vector<int32_t > &pidList, int32_t maxDuration, uint64_t happenTime,
102         const std::string &module)
103         : TraceStrategy(maxDuration, happenTime, module, TraceScenario::TRACE_TELEMETRY), pidList_(pidList)
104     {
105         flowController_ = std::make_shared<TraceFlowController>(BusinessName::TELEMETRY);
106     }
107     TraceRet DoDump(std::vector<std::string> &outputFile) override;
108 
109 protected:
IsMine(const std::string & fileName)110     bool IsMine(const std::string &fileName) override
111     {
112         return true;
113     }
114 
115 private:
116     std::vector<int32_t > pidList_;
117     std::shared_ptr<TraceFlowController> flowController_ = nullptr;
118 };
119 
120 // Only for app to dump trace
121 class TraceAppStrategy : public TraceStrategy  {
122 public:
TraceAppStrategy(std::shared_ptr<AppCallerEvent> appCallerEvent)123     explicit TraceAppStrategy(std::shared_ptr<AppCallerEvent> appCallerEvent)
124         : TraceStrategy(0, 0, ClientName::APP, TraceScenario::TRACE_DYNAMIC)
125     {
126         appCallerEvent_ = appCallerEvent;
127         flowController_ = std::make_shared<TraceFlowController>(ClientName::APP);
128     }
129     TraceRet DoDump(std::vector<std::string> &outputFile) override;
130 
131 protected:
132     bool IsMine(const std::string &fileName) override;
133 
134 private:
135     void InnerShareAppEvent(std::shared_ptr<AppCallerEvent> appCallerEvent);
136     void InnerReportMainThreadJankForTrace(std::shared_ptr<AppCallerEvent> appCallerEvent);
137     void CleanOldAppTrace();
138     std::string InnerMakeTraceFileName(std::shared_ptr<AppCallerEvent> appCallerEvent);
139     std::shared_ptr<AppCallerEvent> appCallerEvent_;
140     std::shared_ptr<TraceFlowController> flowController_ = nullptr;
141 };
142 
143 class TraceFactory {
144 public:
145     static std::shared_ptr<TraceStrategy> CreateTraceStrategy(UCollect::TraceCaller caller, int32_t timeLimit,
146         uint64_t happenTime);
147 
148     static std::shared_ptr<TraceStrategy> CreateTraceStrategy(UCollect::TraceClient client, int32_t timeLimit,
149         uint64_t happenTime);
150 
151     static std::shared_ptr<TraceAppStrategy> CreateAppStrategy(std::shared_ptr<AppCallerEvent> appCallerEvent);
152 };
153 }
154 #endif // HIVIEWDFX_HIVIEW_TRACE_STRATEGY_H
155