1 /*
2  * Copyright (c) 2024 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 "JankAnimatorMonitor.h"
17 #include "AnimatorSceneDataProcessor.h"
18 #include "hisysevent.h"
19 #include "ActionId.h"
20 #include "JankAnimatorMonitorConverter.h"
21 #include "hiview_logger.h"
22 
23 DEFINE_LOG_LABEL(0xD002D66, "Hiview-XPerformance");
24 
25 using ActionId::JANK_ANIMATOR_FRAME;
26 using OHOS::HiviewDFX::HiSysEvent;
27 
28 namespace {
29     constexpr const uint64_t C_LEVEL = 50;
30 }
31 
JankAnimatorMonitor(IMonitorThrExecutor * thr,IAnimatorSceneDataProcessor * stats,IJankAnimatorReporter * report)32 JankAnimatorMonitor::JankAnimatorMonitor(IMonitorThrExecutor* thr, IAnimatorSceneDataProcessor* stats,
33                                          IJankAnimatorReporter* report)
34 {
35     this->exec = thr;
36     this->stats = stats;
37     this->reporter = report;
38     this->actionId = JANK_ANIMATOR_FRAME;
39 }
40 
HandleEvt(std::shared_ptr<XperfEvt> evt)41 void JankAnimatorMonitor::HandleEvt(std::shared_ptr<XperfEvt> evt)
42 {
43     HIVIEW_LOGD("[JankAnimatorMonitor::HandleEvt]");
44     if (exec != nullptr) {
45         exec->ExecuteMonitorInMainThr(this, evt);
46     } else {
47         HIVIEW_LOGE("[JankAnimatorMonitor::HandleEvt] exec is null");
48     }
49 }
50 
HandleMainThrEvt(std::shared_ptr<XperfEvt> evt)51 void JankAnimatorMonitor::HandleMainThrEvt(std::shared_ptr<XperfEvt> evt)
52 {
53     HIVIEW_LOGD("[JankAnimatorMonitor::HandleMainThrEvt]");
54     try {
55         std::shared_ptr<XperfEvt> event = evt;
56         ProcessStats(event);
57     } catch (std::invalid_argument& ex) {
58         HIVIEW_LOGE("invalid argument");
59     }
60 }
61 
ProcessStats(std::shared_ptr<XperfEvt> evt)62 void JankAnimatorMonitor::ProcessStats(std::shared_ptr<XperfEvt> evt)
63 {
64     HIVIEW_LOGD("[JankAnimatorMonitor::ProcessStats]");
65     if (stats != nullptr) {
66         stats->ProcessSceneData(evt);
67     } else {
68         HIVIEW_LOGE("[JankAnimatorMonitor::ProcessStats] stats is null");
69     }
70 }
71 
ReportMetrics(const AnimatorMetrics & metrics)72 void JankAnimatorMonitor::ReportMetrics(const AnimatorMetrics& metrics)
73 {
74     HIVIEW_LOGD("[JankAnimatorMonitor::ReportMetrics]");
75     ReportNormal(metrics);
76     try {
77         uint64_t maxFrame = (metrics.appInfo.maxFrameTime > metrics.rsInfo.maxFrameTime) ?
78                 metrics.appInfo.maxFrameTime : metrics.rsInfo.maxFrameTime;
79         uint64_t val = metrics.appInfo.isDisplayAnimator ? maxFrame : metrics.rsInfo.maxFrameTime;
80         if (val > C_LEVEL) {
81             ReportCritical(metrics, "", "");
82         }
83     } catch (std::logic_error& ex) {
84         HIVIEW_LOGE("JankAnimatorMonitor ObtainId error: %{public}s", std::string(ex.what()).c_str());
85         return;
86     }
87 }
88 
ReportNormal(const AnimatorMetrics & metrics)89 void JankAnimatorMonitor::ReportNormal(const AnimatorMetrics& metrics)
90 {
91     JankAnimatorReportEvent event = JankAnimatorMonitorConverter::ConverterReportData(metrics);
92     if (reporter != nullptr) {
93         reporter->ReportNormal(event);
94     } else {
95         HIVIEW_LOGE("JankAnimatorMonitor ReportNormal reporter is null.");
96     }
97 }
98 
ReportCritical(const AnimatorMetrics & metrics,const std::string & traceFileName,const std::string & infoFileName)99 void JankAnimatorMonitor::ReportCritical(const AnimatorMetrics& metrics, const std::string& traceFileName,
100     const std::string& infoFileName)
101 {
102     JankAnimatorReportEvent event = JankAnimatorMonitorConverter::ConverterReportData(metrics, traceFileName,
103                                                                                       infoFileName);
104     if (reporter != nullptr) {
105         reporter->ReportCritical(event);
106     } else {
107         HIVIEW_LOGE("JankAnimatorMonitor ReportCritical reporter is null.");
108     }
109 }