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 #ifndef ROSEN_RENDER_SERVICE_BASE_RS_NODE_STATS_H
17 #define ROSEN_RENDER_SERVICE_BASE_RS_NODE_STATS_H
18
19 #include <cstdint>
20 #include <mutex>
21 #include <string>
22 #include <tuple>
23 #include <unordered_set>
24 #include <vector>
25
26 #include "common/rs_common_def.h"
27 #include "nocopyable.h"
28
29 namespace OHOS {
30 namespace Rosen {
31 using RSNodeCount = uint32_t;
32 using RSNodeDescription = std::string;
33 using RSNodeStatsType = std::tuple<RSNodeCount, NodeId, RSNodeDescription>;
34
CreateRSNodeStatsItem(RSNodeCount nodeCount,NodeId nodeId,RSNodeDescription nodeDesc)35 inline RSNodeStatsType CreateRSNodeStatsItem(RSNodeCount nodeCount, NodeId nodeId, RSNodeDescription nodeDesc)
36 {
37 RSNodeStatsType nodeStats{nodeCount, nodeId, nodeDesc};
38 return nodeStats;
39 }
40
41 enum class RSNodeStatsUpdateMode : size_t {
42 SIMPLE_APPEND = 0,
43 DISCARD_DUPLICATE = 1
44 };
45
46 class RSNodeStats {
47 public:
48 static RSNodeStats& GetInstance();
49 void AddNodeStats(const RSNodeStatsType& nodeStats,
50 RSNodeStatsUpdateMode updateMode = RSNodeStatsUpdateMode::DISCARD_DUPLICATE);
51 void ClearNodeStats();
52 void ReportRSNodeLimitExceeded();
53
54 private:
55 RSNodeStats() = default;
56 ~RSNodeStats() = default;
57 DISALLOW_COPY_AND_MOVE(RSNodeStats);
58
59 void SortNodeStats(bool isSortByNodeCountDescendingOrder = true);
60 std::pair<RSNodeDescription, RSNodeCount> GetNodeStatsToReportByIndex(size_t index) const;
61 RSNodeDescription CheckEmptyAndReviseNodeDescription(const RSNodeDescription& nodeDescription) const;
62 void GetCurrentRSNodeLimit(uint32_t& rsNodeLimit, uint32_t& rsNodeReportLimit) const;
63 int64_t GetCurrentSystimeMs() const;
64 int64_t GetCurrentSteadyTimeMs() const;
65
66 static constexpr int64_t TIMESTAMP_INITIAL = -1;
67 static constexpr int64_t REPORT_INTERVAL_LIMIT = 10000; // 10s
68 static constexpr int RS_NODE_LIMIT_PROPERTY_MIN = 1;
69 static constexpr int RS_NODE_LIMIT_PROPERTY_MAX = 1000;
70 static constexpr float RS_NODE_LIMIT_REPORT_RATIO = 1.5f;
71 static inline const std::string RS_NODE_LIMIT_EXCEEDED_EVENT_NAME = "RS_NODE_LIMIT_EXCEEDED";
72
73 int64_t lastReportTime_ = TIMESTAMP_INITIAL;
74 int64_t lastReportTimeSteady_ = TIMESTAMP_INITIAL;
75 std::mutex mutex_;
76 std::vector<RSNodeStatsType> rsNodeStatsVec_;
77 std::unordered_set<NodeId> rsNodeIdSet_;
78 uint32_t rsNodeCountTotal_ = 0;
79 };
80
81 } // namespace Rosen
82 } // namespace OHOS
83
84 #endif // ROSEN_RENDER_SERVICE_BASE_RS_NODE_STATS_H
85