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