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 "platform/ohos/rs_node_stats.h"
17 
18 #include <algorithm>
19 #include <chrono>
20 #include <sys/time.h>
21 #include <unistd.h>
22 
23 #include "hisysevent.h"
24 #include "rs_trace.h"
25 
26 #include "common/rs_background_thread.h"
27 #include "platform/common/rs_log.h"
28 #include "platform/common/rs_system_properties.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33 constexpr size_t TOP1_INDEX = 0;
34 constexpr size_t TOP2_INDEX = 1;
35 constexpr size_t TOP3_INDEX = 2;
36 constexpr size_t EMPTY_NODE_DESCRIPTION_SIZE = 0;
37 constexpr RSNodeCount NONE_NODE_COUNT = 0;
38 const RSNodeDescription NONE_NODE_DESCRIPTION = "NaN";
39 const RSNodeDescription EMPTY_NODE_DESCRIPTION = "EmptyName";
40 }
41 
GetInstance()42 RSNodeStats& RSNodeStats::GetInstance()
43 {
44     static RSNodeStats instance;
45     return instance;
46 }
47 
AddNodeStats(const RSNodeStatsType & nodeStats,RSNodeStatsUpdateMode updateMode)48 void RSNodeStats::AddNodeStats(const RSNodeStatsType& nodeStats, RSNodeStatsUpdateMode updateMode)
49 {
50     std::lock_guard<std::mutex> lock(mutex_);
51     NodeId nodeId = std::get<NodeId>(nodeStats);
52     if (updateMode == RSNodeStatsUpdateMode::DISCARD_DUPLICATE && rsNodeIdSet_.find(nodeId) != rsNodeIdSet_.end()) {
53         return;
54     }
55     rsNodeIdSet_.insert(nodeId);
56     rsNodeStatsVec_.push_back(nodeStats);
57     rsNodeCountTotal_ += std::get<RSNodeCount>(nodeStats);
58 }
59 
ClearNodeStats()60 void RSNodeStats::ClearNodeStats()
61 {
62     std::lock_guard<std::mutex> lock(mutex_);
63     rsNodeIdSet_.clear();
64     rsNodeStatsVec_.clear();
65     rsNodeCountTotal_ = 0;
66 }
67 
ReportRSNodeLimitExceeded()68 void RSNodeStats::ReportRSNodeLimitExceeded()
69 {
70     std::lock_guard<std::mutex> lock(mutex_);
71     uint32_t rsNodeCountTotal = rsNodeCountTotal_;
72     uint32_t rsNodeLimit = 0;
73     uint32_t rsNodeReportLimit = 0;
74     GetCurrentRSNodeLimit(rsNodeLimit, rsNodeReportLimit);
75     if (rsNodeCountTotal < rsNodeReportLimit) {
76         return;
77     }
78     int64_t curSysTime = GetCurrentSystimeMs();
79     int64_t curSteadyTime = GetCurrentSteadyTimeMs();
80     if (lastReportTime_ != TIMESTAMP_INITIAL && lastReportTimeSteady_ != TIMESTAMP_INITIAL &&
81         (curSysTime - lastReportTime_ <= REPORT_INTERVAL_LIMIT ||
82         curSteadyTime - lastReportTimeSteady_ <= REPORT_INTERVAL_LIMIT)) {
83         return;
84     }
85     SortNodeStats();
86     const auto nodeInfoTop1 = GetNodeStatsToReportByIndex(TOP1_INDEX);
87     const auto nodeInfoTop2 = GetNodeStatsToReportByIndex(TOP2_INDEX);
88     const auto nodeInfoTop3 = GetNodeStatsToReportByIndex(TOP3_INDEX);
89     uint32_t appWindowTotal = static_cast<uint32_t>(rsNodeStatsVec_.size());
90     RS_TRACE_NAME_FMT("RSNodeStats::ReportRSNodeLimitExceeded "
91                       "nodeCount=%u, top1=%u [%s], top2=%u [%s], top3=%u [%s], timestamp=%" PRId64,
92                       rsNodeCountTotal, nodeInfoTop1.second, nodeInfoTop1.first.c_str(), nodeInfoTop2.second,
93                       nodeInfoTop2.first.c_str(), nodeInfoTop3.second, nodeInfoTop3.first.c_str(), curSysTime);
94     RSBackgroundThread::Instance().PostTask([
95         rsNodeLimit, rsNodeCountTotal, curSysTime, appWindowTotal, nodeInfoTop1, nodeInfoTop2, nodeInfoTop3]() {
96         RS_TRACE_NAME("RSNodeStats::ReportRSNodeLimitExceeded in RSBackgroundThread");
97         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::GRAPHIC,
98                         RS_NODE_LIMIT_EXCEEDED_EVENT_NAME,
99                         OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
100                         "RS_NODE_LIMIT", rsNodeLimit,
101                         "RS_ACTUAL_NODE", rsNodeCountTotal,
102                         "TIMESTAMP", static_cast<uint64_t>(curSysTime),
103                         "RS_APP_WINDOW_TOTAL", appWindowTotal,
104                         "RS_TOP1_APP_NAME", nodeInfoTop1.first,
105                         "RS_TOP1_APP_NODE", nodeInfoTop1.second,
106                         "RS_TOP2_APP_NAME", nodeInfoTop2.first,
107                         "RS_TOP2_APP_NODE", nodeInfoTop2.second,
108                         "RS_TOP3_APP_NAME", nodeInfoTop3.first,
109                         "RS_TOP3_APP_NODE", nodeInfoTop3.second);
110     });
111     lastReportTime_ = curSysTime;
112     lastReportTimeSteady_ = curSteadyTime;
113 }
114 
SortNodeStats(bool isSortByNodeCountDescendingOrder)115 void RSNodeStats::SortNodeStats(bool isSortByNodeCountDescendingOrder)
116 {
117     std::sort(rsNodeStatsVec_.begin(), rsNodeStatsVec_.end());
118     if (isSortByNodeCountDescendingOrder) {
119         std::reverse(rsNodeStatsVec_.begin(), rsNodeStatsVec_.end());
120     }
121 }
122 
GetNodeStatsToReportByIndex(size_t index) const123 std::pair<RSNodeDescription, RSNodeCount> RSNodeStats::GetNodeStatsToReportByIndex(size_t index) const
124 {
125     if (index < rsNodeStatsVec_.size()) {
126         const RSNodeStatsType& nodeStats = rsNodeStatsVec_.at(index);
127         RSNodeDescription nodeDescription = std::get<RSNodeDescription>(nodeStats);
128         nodeDescription = CheckEmptyAndReviseNodeDescription(nodeDescription);
129         RSNodeCount nodeCount = std::get<RSNodeCount>(nodeStats);
130         return std::make_pair(nodeDescription, nodeCount);
131     }
132     return std::make_pair(NONE_NODE_DESCRIPTION, NONE_NODE_COUNT);
133 }
134 
CheckEmptyAndReviseNodeDescription(const RSNodeDescription & nodeDescription) const135 RSNodeDescription RSNodeStats::CheckEmptyAndReviseNodeDescription(const RSNodeDescription& nodeDescription) const
136 {
137     if (nodeDescription.size() == EMPTY_NODE_DESCRIPTION_SIZE) {
138         return EMPTY_NODE_DESCRIPTION;
139     }
140     return nodeDescription;
141 }
142 
GetCurrentRSNodeLimit(uint32_t & rsNodeLimit,uint32_t & rsNodeReportLimit) const143 void RSNodeStats::GetCurrentRSNodeLimit(uint32_t& rsNodeLimit, uint32_t& rsNodeReportLimit) const
144 {
145     int rsNodeLimitProperty = std::clamp(
146         RSSystemProperties::GetRSNodeLimit(), RS_NODE_LIMIT_PROPERTY_MIN, RS_NODE_LIMIT_PROPERTY_MAX);
147     rsNodeLimit = static_cast<uint32_t>(rsNodeLimitProperty);
148     rsNodeReportLimit = static_cast<uint32_t>(rsNodeLimitProperty * RS_NODE_LIMIT_REPORT_RATIO);
149 }
150 
GetCurrentSystimeMs() const151 int64_t RSNodeStats::GetCurrentSystimeMs() const
152 {
153     auto curTime = std::chrono::system_clock::now().time_since_epoch();
154     int64_t curSysTime = std::chrono::duration_cast<std::chrono::milliseconds>(curTime).count();
155     return curSysTime;
156 }
157 
GetCurrentSteadyTimeMs() const158 int64_t RSNodeStats::GetCurrentSteadyTimeMs() const
159 {
160     auto curTime = std::chrono::steady_clock::now().time_since_epoch();
161     int64_t curSteadyTime = std::chrono::duration_cast<std::chrono::milliseconds>(curTime).count();
162     return curSteadyTime;
163 }
164 
165 } // namespace Rosen
166 } // namespace OHOS
167