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 #include "base/perfmonitor/perf_monitor.h"
17
18 #include "base/log/event_report.h"
19 #include "base/perfmonitor/perf_constants.h"
20 #include "base/perfmonitor/perf_interfaces.h"
21 #include "base/ressched/ressched_report.h"
22 #include "base/utils/system_properties.h"
23 #include "core/common/ace_application_info.h"
24
25 namespace OHOS::Ace {
26 using namespace std;
27 PerfMonitor* PerfMonitor::pMonitor = nullptr;
28 std::once_flag PerfMonitor::initFlag;
29 const int32_t JANK_SKIPPED_THRESHOLD = SystemProperties::GetJankFrameThreshold();
30 // Obtain the last three digits of the full path
31 constexpr uint32_t PATH_DEPTH = 3;
32
ParsePageUrl(const std::string & pagePath)33 std::string ParsePageUrl(const std::string& pagePath)
34 {
35 std::string res;
36 std::vector<std::string> paths;
37 StringUtils::StringSplitter(pagePath, '/', paths);
38 uint32_t pathSize = paths.size();
39 if (pathSize < PATH_DEPTH) {
40 return pagePath;
41 }
42 for (uint32_t i = pathSize - PATH_DEPTH; i < pathSize; i++) {
43 res = res + "/" + paths[i];
44 }
45 return res;
46 }
47
GetPerfMonitor()48 PerfMonitor* PerfMonitor::GetPerfMonitor()
49 {
50 std::call_once(initFlag, &PerfMonitor::InitInstance);
51 return pMonitor;
52 }
53
InitInstance()54 void PerfMonitor::InitInstance()
55 {
56 pMonitor = new PerfMonitor();
57 }
58
Start(const std::string & sceneId,PerfActionType type,const std::string & note)59 void PerfMonitor::Start(const std::string& sceneId, PerfActionType type, const std::string& note)
60 {
61 if (PerfInterfaces::IsScrollJank(sceneId)) {
62 PerfInterfaces::SetScrollState(true);
63 }
64 if (apsMonitor_ != nullptr) {
65 apsMonitor_->SetApsScene(sceneId, true);
66 }
67 PerfInterfaces::Start(sceneId, type, note);
68 if (sceneId == PerfConstants::ABILITY_OR_PAGE_SWITCH) {
69 ResSchedReport::GetInstance().ResSchedDataReport("ability_or_page_switch_start");
70 }
71 }
72
StartCommercial(const std::string & sceneId,PerfActionType type,const std::string & note)73 void PerfMonitor::StartCommercial(const std::string& sceneId, PerfActionType type, const std::string& note)
74 {
75 if (PerfInterfaces::IsScrollJank(sceneId)) {
76 PerfInterfaces::SetScrollState(true);
77 }
78 if (apsMonitor_ != nullptr) {
79 apsMonitor_->SetApsScene(sceneId, true);
80 }
81 PerfInterfaces::StartCommercial(sceneId, type, note);
82 }
83
End(const std::string & sceneId,bool isRsRender)84 void PerfMonitor::End(const std::string& sceneId, bool isRsRender)
85 {
86 if (PerfInterfaces::IsScrollJank(sceneId)) {
87 PerfInterfaces::SetScrollState(false);
88 }
89 if (apsMonitor_ != nullptr) {
90 apsMonitor_->SetApsScene(sceneId, false);
91 }
92
93 PerfInterfaces::End(sceneId, isRsRender);
94 if (sceneId == PerfConstants::ABILITY_OR_PAGE_SWITCH) {
95 ResSchedReport::GetInstance().ResSchedDataReport("ability_or_page_switch_end");
96 }
97 }
98
EndCommercial(const std::string & sceneId,bool isRsRender)99 void PerfMonitor::EndCommercial(const std::string& sceneId, bool isRsRender)
100 {
101 if (PerfInterfaces::IsScrollJank(sceneId)) {
102 PerfInterfaces::SetScrollState(false);
103 }
104 if (apsMonitor_ != nullptr) {
105 apsMonitor_->SetApsScene(sceneId, false);
106 }
107
108 PerfInterfaces::EndCommercial(sceneId, isRsRender);
109 }
110
RecordInputEvent(PerfActionType type,PerfSourceType sourceType,int64_t time)111 void PerfMonitor::RecordInputEvent(PerfActionType type, PerfSourceType sourceType, int64_t time)
112 {
113 PerfInterfaces::RecordInputEvent(type, sourceType, time);
114 }
115
SetFrameTime(int64_t vsyncTime,int64_t duration,double jank,const std::string & windowName)116 void PerfMonitor::SetFrameTime(int64_t vsyncTime, int64_t duration, double jank, const std::string& windowName)
117 {
118 PerfInterfaces::SetFrameTime(vsyncTime, duration, jank, windowName);
119 }
120
SetSubHealthInfo(const std::string & info,const std::string & reason,const int32_t duration)121 void PerfMonitor::SetSubHealthInfo(const std::string& info, const std::string& reason, const int32_t duration)
122 {
123 PerfInterfaces::SetSubHealthInfo(info, reason, duration);
124 }
125
ReportJankFrameApp(double jank)126 void PerfMonitor::ReportJankFrameApp(double jank)
127 {
128 PerfInterfaces::ReportJankFrameApp(jank, JANK_SKIPPED_THRESHOLD);
129 }
130
SetPageUrl(const std::string & pageUrl)131 void PerfMonitor::SetPageUrl(const std::string& pageUrl)
132 {
133 std::string newPageUrl = ParsePageUrl(pageUrl);
134 PerfInterfaces::SetPageUrl(newPageUrl);
135 }
136
GetPageUrl()137 std::string PerfMonitor::GetPageUrl()
138 {
139 return PerfInterfaces::GetPageUrl();
140 }
141
SetPageName(const std::string & pageName)142 void PerfMonitor::SetPageName(const std::string& pageName)
143 {
144 PerfInterfaces::SetPageName(pageName);
145 }
146
GetPageName()147 std::string PerfMonitor::GetPageName()
148 {
149 return PerfInterfaces::GetPageName();
150 }
151
ReportPageShowMsg(const std::string & pageUrl,const std::string & bundleName,const std::string & pageName)152 void PerfMonitor::ReportPageShowMsg(const std::string& pageUrl, const std::string& bundleName,
153 const std::string& pageName)
154 {
155 std::string newPageUrl = ParsePageUrl(pageUrl);
156 PerfInterfaces::ReportPageShowMsg(newPageUrl, bundleName, pageName);
157 }
158
GetInputTime(const std::string & sceneId,PerfActionType type,const std::string & note)159 int64_t PerfMonitor::GetInputTime(const std::string& sceneId, PerfActionType type, const std::string& note)
160 {
161 return PerfInterfaces::GetInputTime(sceneId, type, note);
162 }
163
SetAppStartStatus()164 void PerfMonitor::SetAppStartStatus()
165 {
166 PerfInterfaces::SetAppStartStatus();
167 }
168
SetAppForeground(bool isShow)169 void PerfMonitor::SetAppForeground(bool isShow)
170 {
171 PerfInterfaces::SetAppForeground(isShow);
172 }
173
RecordWindowRectResize(OHOS::Ace::WindowSizeChangeReason reason,const std::string & bundleName)174 void PerfMonitor::RecordWindowRectResize(OHOS::Ace::WindowSizeChangeReason reason, const std::string& bundleName)
175 {
176 switch (reason) {
177 case OHOS::Ace::WindowSizeChangeReason::DRAG_START:
178 PerfInterfaces::Start(PerfConstants::WINDOW_RECT_RESIZE, PerfActionType::LAST_DOWN, bundleName.c_str());
179 break;
180 case OHOS::Ace::WindowSizeChangeReason::DRAG_END:
181 PerfInterfaces::End(PerfConstants::WINDOW_RECT_RESIZE, true);
182 break;
183 default:
184 break;
185 }
186 }
187
NotifyAppJankStatsBegin()188 void PerfMonitor::NotifyAppJankStatsBegin()
189 {
190 PerfInterfaces::NotifyAppJankStatsBegin();
191 }
192
NotifyAppJankStatsEnd()193 void PerfMonitor::NotifyAppJankStatsEnd()
194 {
195 PerfInterfaces::NotifyAppJankStatsEnd();
196 }
197
SetApplicationInfo()198 void PerfMonitor::SetApplicationInfo()
199 {
200 PerfInterfaces::SetApplicationInfo();
201 }
202
SetApsMonitor(const std::shared_ptr<ApsMonitor> & apsMonitor)203 void PerfMonitor::SetApsMonitor(const std::shared_ptr<ApsMonitor>& apsMonitor)
204 {
205 apsMonitor_ = apsMonitor;
206 }
207
208 } // namespace OHOS::Ace
209