1 /*
2 * Copyright (c) 2025 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 "api_duration_statistics.h"
17
18 #undef MMI_LOG_DOMAIN
19 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "ApiDurationStatistics"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 const std::string UNKNOWN_API { "UNKNOWN_API" };
27 using ApiDurationsType = std::unordered_map<ApiDurationStatistics::Api, ApiDurationStatistics::DurationBox>;
28 }
29
30 int32_t ApiDurationStatistics::COUNT_LIMIT_TO_DFX_RADAR { 1000 };
31 std::unordered_map<ApiDurationStatistics::Api, std::string> ApiDurationStatistics::apiNames_ {
32 { ApiDurationStatistics::Api::IS_SCREEN_CAPTURE_WORKING, "IS_SCREEN_CAPTURE_WORKING" },
33 { ApiDurationStatistics::Api::GET_DEFAULT_DISPLAY, "GET_DEFAULT_DISPLAY" },
34 { ApiDurationStatistics::Api::GET_SYSTEM_ABILITY_MANAGER, "GET_SYSTEM_ABILITY_MANAGER" },
35 { ApiDurationStatistics::Api::IS_FOLDABLE, "IS_FOLDABLE" },
36 { ApiDurationStatistics::Api::IS_SCREEN_LOCKED, "IS_SCREEN_LOCKED" },
37 { ApiDurationStatistics::Api::RS_NOTIFY_TOUCH_EVENT, "RS_NOTIFY_TOUCH_EVENT" },
38 { ApiDurationStatistics::Api::RESOURCE_SCHEDULE_REPORT_DATA, "RESOURCE_SCHEDULE_REPORT_DATA" },
39 { ApiDurationStatistics::Api::GET_CUR_RENDERER_CHANGE_INFOS, "GET_CUR_RENDERER_CHANGE_INFOS" },
40 { ApiDurationStatistics::Api::GET_PROC_RUNNING_INFOS_BY_UID, "GET_PROC_RUNNING_INFOS_BY_UID" },
41 { ApiDurationStatistics::Api::TELEPHONY_CALL_MGR_INIT, "TELEPHONY_CALL_MGR_INIT" },
42 { ApiDurationStatistics::Api::TELEPHONY_CALL_MGR_MUTE_RINGER, "TELEPHONY_CALL_MGR_MUTE_RINGER" },
43 { ApiDurationStatistics::Api::TELEPHONY_CALL_MGR_HANG_UP_CALL, "TELEPHONY_CALL_MGR_HANG_UP_CALL" },
44 { ApiDurationStatistics::Api::TELEPHONY_CALL_MGR_REJECT_CALL, "TELEPHONY_CALL_MGR_REJECT_CALL" },
45 { ApiDurationStatistics::Api::RE_SCREEN_MODE_CHANGE_LISTENER, "RE_SCREEN_MODE_CHANGE_LISTENER" },
46 { ApiDurationStatistics::Api::SET_ON_REMOTE_DIED_CALLBACK, "SET_ON_REMOTE_DIED_CALLBACK" },
47 { ApiDurationStatistics::Api::REG_SCREEN_CAPTURE_LISTENER,
48 "REGISTER_SCREEN_CAPTURE_MONITOR_LISTENER" },
49 { ApiDurationStatistics::Api::ABILITY_MGR_START_EXT_ABILITY,
50 "ABILITY_MGR_START_EXT_ABILITY" },
51 { ApiDurationStatistics::Api::ABILITY_MGR_CLIENT_START_ABILITY, "ABILITY_MGR_CLIENT_START_ABILITY" },
52 { ApiDurationStatistics::Api::ABILITY_MGR_CONNECT_ABILITY, "ABILITY_MGR_CLIENT_CONNECT_ABILITY" },
53 { ApiDurationStatistics::Api::GET_RUNNING_PROCESS_INFO_BY_PID, "GET_RUNNING_PROCESS_INFO_BY_PID" },
54 { ApiDurationStatistics::Api::REGISTER_APP_DEBUG_LISTENER, "REGISTER_APP_DEBUG_LISTENER" },
55 { ApiDurationStatistics::Api::UNREGISTER_APP_DEBUG_LISTENER, "UNREGISTER_APP_DEBUG_LISTENER" },
56 { ApiDurationStatistics::Api::PUBLISH_COMMON_EVENT, "PUBLISH_COMMON_EVENT" },
57 { ApiDurationStatistics::Api::GET_VISIBILITY_WINDOW_INFO, "GET_VISIBILITY_WINDOW_INFO" }
58 };
59
RecordDuration(Api api,int32_t durationMS)60 void ApiDurationStatistics::RecordDuration(Api api, int32_t durationMS)
61 {
62 auto threshold = GetCurrentThreshold(durationMS);
63 {
64 std::unique_lock<std::shared_mutex> lock(mtx_);
65 apiDurations_[api][threshold] += 1;
66 }
67 ++apiCallingCount_;
68 }
69
ResetApiStatistics()70 void ApiDurationStatistics::ResetApiStatistics()
71 {
72 apiCallingCount_ = 0;
73 std::unique_lock<std::shared_mutex> lock(mtx_);
74 apiDurations_.clear();
75 }
76
IsLimitMatched()77 bool ApiDurationStatistics::IsLimitMatched()
78 {
79 return apiCallingCount_ >= COUNT_LIMIT_TO_DFX_RADAR;
80 }
81
GetDurationBox()82 ApiDurationsType ApiDurationStatistics::GetDurationBox()
83 {
84 std::shared_lock<std::shared_mutex> lock(mtx_);
85 return apiDurations_;
86 }
87
ApiToString(Api api)88 std::string ApiDurationStatistics::ApiToString(Api api)
89 {
90 if (apiNames_.find(api) != apiNames_.end()) {
91 return apiNames_[api];
92 }
93 return UNKNOWN_API;
94 }
95
GetCurrentThreshold(int32_t duration)96 ApiDurationStatistics::Threshold ApiDurationStatistics::GetCurrentThreshold(int32_t duration)
97 {
98 if (duration <= static_cast<int32_t> (Threshold::LESS_THAN_3MS)) {
99 return Threshold::LESS_THAN_3MS;
100 } else if (duration <= static_cast<int32_t> (Threshold::LESS_THAN_5MS)) {
101 return Threshold::LESS_THAN_5MS;
102 } else if (duration <= static_cast<int32_t> (Threshold::LESS_THAN_10MS)) {
103 return Threshold::LESS_THAN_10MS;
104 } else {
105 return Threshold::GREATER_THAN_10MS;
106 }
107 }
108
GetDurationDistribution(Api api)109 std::vector<int32_t> ApiDurationStatistics::GetDurationDistribution(Api api)
110 {
111 std::shared_lock<std::shared_mutex> lock(mtx_);
112 if (apiDurations_.find(api)== apiDurations_.end()) {
113 return { 0, 0, 0, 0 };
114 }
115 auto durationBox = apiDurations_[api];
116
117 std::vector<int32_t> durations;
118 if (durationBox.find(Threshold::LESS_THAN_3MS) != durationBox.end()) {
119 durations.push_back(durationBox[Threshold::LESS_THAN_3MS]);
120 } else {
121 durations.push_back(0);
122 }
123 if (durationBox.find(Threshold::LESS_THAN_5MS) != durationBox.end()) {
124 durations.push_back(durationBox[Threshold::LESS_THAN_5MS]);
125 } else {
126 durations.push_back(0);
127 }
128 if (durationBox.find(Threshold::LESS_THAN_10MS) != durationBox.end()) {
129 durations.push_back(durationBox[Threshold::LESS_THAN_10MS]);
130 } else {
131 durations.push_back(0);
132 }
133 if (durationBox.find(Threshold::GREATER_THAN_10MS) != durationBox.end()) {
134 durations.push_back(durationBox[Threshold::GREATER_THAN_10MS]);
135 } else {
136 durations.push_back(0);
137 }
138 return durations;
139 }
140
141 } // namespace MMI
142 } // namespace OHOS
143