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 #include <iostream>
16 #include "include/CPU.h"
17 #include "include/DDR.h"
18 #include "include/GPU.h"
19 #include "include/FPS.h"
20 #include "include/profiler_fps.h"
21 #include "include/RAM.h"
22 #include "include/Network.h"
23 #include "include/Power.h"
24 #include "include/Temperature.h"
25 #include "include/ByTrace.h"
26 #include "include/sp_utils.h"
27 #include "include/sp_profiler_factory.h"
28 #include "include/Dubai.h"
29 #include "include/Capture.h"
30 #include "include/navigation.h"
31 #include "include/sp_log.h"
32 
33 namespace OHOS {
34 namespace SmartPerf {
GetProfilerItem(MessageType messageType)35 SpProfiler *SpProfilerFactory::GetProfilerItem(MessageType messageType)
36 {
37     SpProfiler* profiler = nullptr;
38     switch (messageType) {
39         case MessageType::GET_CPU_FREQ_LOAD:
40             OHOS::SmartPerf::CPU::GetInstance().IsFindHap();
41             profiler = &CPU::GetInstance();
42             break;
43         case MessageType::GET_FPS_AND_JITTERS:
44         case MessageType::GET_LOW_POWER_FPS:
45         case MessageType::GET_CUR_FPS:
46             profiler = &ProfilerFPS::GetInstance();
47             break;
48         case MessageType::GET_GPU_FREQ:
49         case MessageType::GET_GPU_LOAD:
50             profiler = &GPU::GetInstance();
51             break;
52         case MessageType::GET_DDR_FREQ:
53             profiler = &DDR::GetInstance();
54             break;
55         case MessageType::GET_RAM_INFO:
56             profiler = &RAM::GetInstance();
57             break;
58         default:
59             break;
60     }
61     if (profiler == nullptr) {
62         profiler = GetProfilerItemContinue(messageType);
63     }
64     return profiler;
65 }
66 
GetProfilerItemContinue(MessageType messageType)67 SpProfiler *SpProfilerFactory::GetProfilerItemContinue(MessageType messageType)
68 {
69     SpProfiler* profiler = nullptr;
70     switch (messageType) {
71         case MessageType::GET_TEMPERATURE:
72             profiler = &Temperature::GetInstance();
73             break;
74         case MessageType::GET_POWER:
75             profiler = &Power::GetInstance();
76             break;
77         case MessageType::CATCH_TRACE_CONFIG:
78             ProfilerFPS::GetInstance().SetTraceCatch();
79             break;
80         case MessageType::GET_CAPTURE:
81             Capture::GetInstance().SocketMessage();
82             profiler = &Capture::GetInstance();
83             break;
84         case MessageType::SET_DUBAI_DB:
85             DumpDubaiAndMoveDb();
86             break;
87         case MessageType::CATCH_NETWORK_TRAFFIC:
88         case MessageType::GET_NETWORK_TRAFFIC:
89             profiler = &Network::GetInstance();
90             break;
91         default:
92             break;
93     }
94     return profiler;
95 }
96 
DumpDubaiAndMoveDb()97 void SpProfilerFactory::DumpDubaiAndMoveDb()
98 {
99     Dubai::DumpDubaiBegin();
100     Dubai::DumpDubaiFinish();
101     Dubai::MoveDubaiDb();
102 }
103 
SetProfilerPkg(const std::string &pkg)104 void SpProfilerFactory::SetProfilerPkg(const std::string &pkg)
105 {
106     FPS &fps = FPS::GetInstance();
107     fps.SetPackageName(pkg);
108     ProfilerFPS &profilerFps = ProfilerFPS::GetInstance();
109     profilerFps.SetPackageName(pkg);
110     RAM &ram = RAM::GetInstance();
111     ram.SetPackageName(pkg);
112     CPU &cpu = CPU::GetInstance();
113     cpu.SetPackageName(pkg);
114 }
115 
SetProfilerPidByPkg(std::string &pid)116 void SpProfilerFactory::SetProfilerPidByPkg(std::string &pid)
117 {
118     FPS &fps = FPS::GetInstance();
119     fps.SetProcessId(pid);
120     ProfilerFPS &profilerFps = ProfilerFPS::GetInstance();
121     profilerFps.SetProcessId(pid);
122     RAM &ram = RAM::GetInstance();
123     ram.SetProcessId(pid);
124     CPU &cpu = CPU::GetInstance();
125     cpu.SetProcessId(pid);
126     Navigation &nav = Navigation::GetInstance();
127     nav.SetProcessId(pid);
128 }
129 
SetProfilerLayer(const std::string &layer)130 void SpProfilerFactory::SetProfilerLayer(const std::string &layer)
131 {
132     FPS &fps = FPS::GetInstance();
133     fps.SetLayerName(layer);
134 }
135 
SetProfilerGameLayer(const std::string &isGameView)136 void SpProfilerFactory::SetProfilerGameLayer(const std::string &isGameView)
137 {
138     ProfilerFPS &profilerFps = ProfilerFPS::GetInstance();
139     profilerFps.SetGameLayer(isGameView);
140 }
141 
SetByTrace(std::string message)142 void SpProfilerFactory::SetByTrace(std::string message)
143 {
144     std::vector<std::string> values;
145     std::string delimiter = "||";
146     std::string delim = "=";
147     SPUtils::StrSplit(message, delimiter, values);
148     int mSum = 0;
149     int mInterval = 0;
150     long long mThreshold = 0;
151     int lowFps = 0;
152     for (std::string vItem : values) {
153         std::vector<std::string> vItems;
154         SPUtils::StrSplit(vItem, delim, vItems);
155         if (vItems[0] == "traceSum") {
156             mSum = std::stoi(vItems[1]);
157         }
158         if (vItems[0] == "fpsJitterTime") {
159             mThreshold = std::stoi(vItems[1]);
160         }
161         if (vItems[0] == "catchInterval") {
162             mInterval = std::stoi(vItems[1]);
163         }
164         if (vItems[0] == "lowFps") {
165             lowFps = std::stoi(vItems[1]);
166         }
167     }
168     const ByTrace &bTrace = ByTrace::GetInstance();
169     if (message.find("traceSum") != std::string::npos) {
170         int mCurNum = 1;
171         bTrace.SetTraceConfig(mSum, mInterval, mThreshold, lowFps, mCurNum);
172     }
173 }
GetCmdProfilerItem(CommandType commandType, bool cmdFlag)174 SpProfiler *SpProfilerFactory::GetCmdProfilerItem(CommandType commandType, bool cmdFlag)
175 {
176     SpProfiler *profiler = nullptr;
177     switch (commandType) {
178         case CommandType::CT_C:
179             if (cmdFlag) {
180                 profiler = &CPU::GetInstance();
181             }
182             break;
183         case CommandType::CT_G:
184             profiler = &GPU::GetInstance();
185             break;
186         case CommandType::CT_F:
187             if (cmdFlag) {
188                 profiler = &FPS::GetInstance();
189             }
190             break;
191         case CommandType::CT_D:
192             profiler = &DDR::GetInstance();
193             break;
194         case CommandType::CT_P:
195             profiler = &Power::GetInstance();
196             break;
197         case CommandType::CT_T:
198             profiler = &Temperature::GetInstance();
199             break;
200         case CommandType::CT_R:
201             if (cmdFlag) {
202                 profiler = &RAM::GetInstance();
203             }
204             break;
205         case CommandType::CT_NET:
206             profiler = &Network::GetInstance();
207             break;
208         case CommandType::CT_NAV:
209             profiler = &Navigation::GetInstance();
210             break;
211         case CommandType::CT_TTRACE:
212             ProfilerFPS::GetInstance().SetTraceCatch();
213             break;
214         default:
215             break;
216     }
217     if (profiler == nullptr) {
218         profiler = GetCmdProfilerItemContinue(commandType, cmdFlag);
219     }
220     return profiler;
221 }
222 
GetCmdProfilerItemContinue(CommandType commandType, bool cmdFlag)223 SpProfiler *SpProfilerFactory::GetCmdProfilerItemContinue(CommandType commandType, bool cmdFlag)
224 {
225     SpProfiler *profiler = nullptr;
226     switch (commandType) {
227         case CommandType::CT_SNAPSHOT:
228             if (cmdFlag) {
229                 profiler = &Capture::GetInstance();
230             }
231             break;
232         default:
233             break;
234     }
235     return profiler;
236 }
237 }
238 }
239