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