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 IMF_HISYSEVENT_REPORTER_H 17 #define IMF_HISYSEVENT_REPORTER_H 18 19 #include <cstdint> 20 #include <unordered_map> 21 22 #include "global.h" 23 #include "imf_hisysevent_info.h" 24 #include "imf_hisysevent_util.h" 25 #include "timer.h" 26 namespace OHOS { 27 namespace MiscServices { 28 class ImfHiSysEventReporter { 29 public: 30 static constexpr uint32_t HISYSEVENT_TIMER_TASK_INTERNAL = 3 * 60 * 60 * 1000; // updated three hourly 31 static constexpr uint32_t HISYSEVENT_STATISTICS_INTERNAL = 30 * 60 * 1000; 32 static constexpr uint32_t COUNT_STATISTICS_INTERVAL_NUM = 33 HISYSEVENT_TIMER_TASK_INTERNAL / HISYSEVENT_STATISTICS_INTERNAL; // 6 34 void ReportEvent(ImfEventType eventType, const HiSysOriginalInfo &info); 35 std::string GetSelfName(); 36 uint32_t GetStatisticalIntervalIndex(); 37 void ResetTimerStartTime(int64_t time); 38 39 protected: 40 ImfHiSysEventReporter() = default; 41 ~ImfHiSysEventReporter(); 42 43 private: IsValidErrCode(int32_t errCode)44 virtual bool IsValidErrCode(int32_t errCode) 45 { 46 return false; 47 } IsFault(int32_t errCode)48 virtual bool IsFault(int32_t errCode) 49 { 50 return false; 51 } RecordStatisticsEvent(ImfStatisticsEvent event,const HiSysOriginalInfo & info)52 virtual void RecordStatisticsEvent(ImfStatisticsEvent event, const HiSysOriginalInfo &info){}; ReportStatisticsEvent()53 virtual void ReportStatisticsEvent(){}; 54 void StartTimer(); 55 void StopTimer(); 56 void TimerCallback(); 57 void ReportFaultEvent(ImfFaultEvent event, const HiSysOriginalInfo &info); 58 std::string GenerateFaultEventKey(ImfFaultEvent event, const HiSysOriginalInfo &info); 59 std::pair<bool, int64_t> GenerateFaultReportInfo(ImfFaultEvent event, const HiSysOriginalInfo &info); 60 void ClearFaultEventInfo(); 61 static constexpr uint32_t FAULT_REPORT_INTERVAL = 5 * 60 * 1000; 62 static constexpr uint32_t FAULT_RETENTION_PERIOD = 10 * 60 * 1000; 63 const std::unordered_map<ImfEventType, std::pair<ImfFaultEvent, ImfStatisticsEvent>> EVENT_MAPPING = { 64 { CLIENT_ATTACH, { CLIENT_ATTACH_FAILED, CLIENT_ATTACH_STATISTICS } }, 65 { CLIENT_SHOW, { CLIENT_SHOW_FAILED, CLIENT_SHOW_STATISTICS } }, 66 { IME_START_INPUT, { IME_START_INPUT_FAILED, IME_START_INPUT_STATISTICS } }, 67 { BASE_TEXT_OPERATOR, { BASE_TEXT_OPERATION_FAILED, BASE_TEXT_OPERATION_STATISTICS } }, 68 }; 69 using FaultEventHandler = void (*)(const std::string &selfName, int64_t faultNum, const HiSysOriginalInfo &info); 70 const std::unordered_map<uint32_t, FaultEventHandler> FAULT_EVENT_HANDLERS = { 71 { CLIENT_ATTACH_FAILED, ImfHiSysEventUtil::ReportClientAttachFault }, 72 { CLIENT_SHOW_FAILED, ImfHiSysEventUtil::ReportClientShowFault }, 73 { IME_START_INPUT_FAILED, ImfHiSysEventUtil::ReportImeStartInputFault }, 74 { BASE_TEXT_OPERATION_FAILED, ImfHiSysEventUtil::ReportBaseTextOperationFault } 75 }; 76 int64_t timerStartTime_ = 0; 77 std::mutex selfNameLock_; 78 std::string selfName_; 79 std::mutex faultEventRecordsLock_; 80 // first:event+eventCode+errCode+peerName+peerUserId second: first:last report time second:report num 81 std::unordered_map<std::string, std::pair<int64_t, uint32_t>> faultEventRecords_; 82 std::mutex timerLock_; 83 Utils::Timer timer_{ "OS_imfHiSysEventTimer" }; 84 uint32_t timerId_{ 0 }; 85 }; 86 } // namespace MiscServices 87 } // namespace OHOS 88 89 #endif // IMF_HISYSEVENT_REPORTER_H