• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef API_DURATION_STATISTICS_H
17 #define API_DURATION_STATISTICS_H
18 
19 #include <shared_mutex>
20 
21 namespace OHOS {
22 namespace MMI {
23 class ApiDurationStatistics {
24 public:
25     enum Api : int32_t {
26         IS_SCREEN_CAPTURE_WORKING = 0, // IScreenCaptureWorking
27         GET_DEFAULT_DISPLAY, // GetDefaultDisplay
28         GET_SYSTEM_ABILITY_MANAGER, // GetSystemAbilityManager
29         IS_FOLDABLE, // IsFoldable
30         IS_SCREEN_LOCKED, // IsScreenLocked
31         RS_NOTIFY_TOUCH_EVENT, // RSInterfaces::NotifyTouchEvent
32         RESOURCE_SCHEDULE_REPORT_DATA, // ResourceSchedule::ReportData
33         GET_CUR_RENDERER_CHANGE_INFOS, // GetCurrentRendererChangeInfos
34         GET_PROC_RUNNING_INFOS_BY_UID, // GetProcessRunningInfosByUserId
35         TELEPHONY_CALL_MGR_INIT, // Telephony::CallManagerClient::Init
36         TELEPHONY_CALL_MGR_MUTE_RINGER, // Telephony::CallManagerCLient::MuteRinger
37         TELEPHONY_CALL_MGR_HANG_UP_CALL, // Telephony::CallManagerCLient::HangUpCall
38         TELEPHONY_CALL_MGR_REJECT_CALL, // Telephony::CallManagerClient::RejectCall
39         RE_SCREEN_MODE_CHANGE_LISTENER, // RegisterScreenModeChangeListener
40         SET_ON_REMOTE_DIED_CALLBACK, // SetOnRemoteDiedCallback
41         REG_SCREEN_CAPTURE_LISTENER, // RegisterScreenCaptureMonitorListener
42         ABILITY_MGR_START_EXT_ABILITY, // StartExtensionAbility
43         ABILITY_MGR_CLIENT_START_ABILITY, // AbilityManagerClient::StartAbility
44         ABILITY_MGR_CONNECT_ABILITY, // AbilityManagerClient::ConnectAbility
45         GET_RUNNING_PROCESS_INFO_BY_PID, // GetRunningProcessInfoByPid
46         REGISTER_APP_DEBUG_LISTENER, // RegisterAppDebugListener
47         UNREGISTER_APP_DEBUG_LISTENER, // UnregisterAppDebugListener
48         PUBLISH_COMMON_EVENT, // PublishCommonEvent
49         GET_VISIBILITY_WINDOW_INFO // GetVisibilityWindowInfo
50     };
51 
52     enum class Threshold : int32_t {
53         LESS_THAN_3MS = 3,
54         LESS_THAN_5MS = 5,
55         LESS_THAN_10MS = 10,
56         GREATER_THAN_10MS = 11,
57         MAX_DURATION = GREATER_THAN_10MS + 1
58     };
59 
60     using DurationBox = std::unordered_map<Threshold, int32_t>;
61 
62     ApiDurationStatistics() = default;
63     ~ApiDurationStatistics() = default;
64 
65     void RecordDuration(Api api, int32_t durationMS);
66     void ResetApiStatistics();
67     bool IsLimitMatched();
68     std::unordered_map<Api, DurationBox> GetDurationBox();
69     std::string ApiToString(Api api);
70     std::vector<int32_t> GetDurationDistribution(Api api);
71 
72 private:
73     Threshold GetCurrentThreshold(int32_t duration);
74 private:
75     std::unordered_map<Api, DurationBox> apiDurations_;
76     std::atomic_int32_t apiCallingCount_ { 0 };
77     static std::unordered_map<Api, std::string> apiNames_;
78     static int32_t COUNT_LIMIT_TO_DFX_RADAR;
79     std::shared_mutex mtx_;
80 };
81 
82 } // namespace MMI
83 } // namespace OHOS
84 #endif // API_DURATION_STATISTICS_H