1 /* 2 * Copyright (c) 2021-2022 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 RENDER_SERVICE_BASE_COMMON_RS_DETECTOR_H 17 #define RENDER_SERVICE_BASE_COMMON_RS_DETECTOR_H 18 19 #include <map> 20 #include <memory> 21 #include <atomic> 22 #include "rs_log.h" 23 #include "base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include/hisysevent.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 struct RSSysEventMsg final { 28 std::string stringId; 29 std::string msg; 30 OHOS::HiviewDFX::HiSysEvent::EventType eventType; 31 int32_t pid; 32 int32_t uid; 33 std::string bundleName; 34 std::string abilityName; 35 }; 36 37 class RSEventTimer final { 38 public: 39 ~RSEventTimer() = default; 40 static uint64_t GetSysTimeMs(); 41 private: 42 RSEventTimer() = default; 43 }; 44 45 class RSBaseEventDetector { 46 public: 47 using EventReportCallback = std::function<void(const RSSysEventMsg&)>; 48 static std::shared_ptr<RSBaseEventDetector> CreateRSTimeOutDetector(int timeOutThresholdMs, 49 std::string detectorStringId); ~RSBaseEventDetector()50 virtual ~RSBaseEventDetector() 51 { 52 ClearParamList(); 53 RS_LOGD("RSBaseEventDetector::~RSBaseEventDetector finish"); 54 } 55 GetStringId()56 std::string GetStringId() 57 { 58 return stringId_; 59 } 60 GetParamList()61 const std::map<std::string, std::string>& GetParamList() 62 { 63 return paramList_; 64 } 65 AddEventReportCallback(const EventReportCallback & callback)66 void AddEventReportCallback(const EventReportCallback& callback) 67 { 68 eventCallback_ = callback; 69 } 70 71 virtual void SetParam(const std::string& key, const std::string& value) = 0; 72 virtual void SetLoopStartTag() = 0; 73 virtual void SetLoopFinishTag( 74 int32_t focusAppPid, int32_t focusAppUid, 75 std::string& focusAppBundleName, std::string& focusAppAbilityName) = 0; 76 77 protected: 78 RSBaseEventDetector() = default; RSBaseEventDetector(std::string stringId)79 RSBaseEventDetector(std::string stringId) 80 { 81 stringId_ = stringId; 82 } 83 ClearParamList()84 void ClearParamList() 85 { 86 paramList_.clear(); 87 std::map<std::string, std::string> tempParamList; 88 paramList_.swap(tempParamList); 89 RS_LOGD("RSBaseEventDetector::ClearParamList finish"); 90 } 91 92 std::map<std::string, std::string> paramList_; // key: paramName 93 std::string stringId_; 94 EventReportCallback eventCallback_; 95 int32_t focusAppPid_ = -1; 96 int32_t focusAppUid_ = -1; 97 std::string focusAppBundleName_ = ""; 98 std::string focusAppAbilityName_ = ""; 99 }; 100 101 102 class RSTimeOutDetector : public RSBaseEventDetector { 103 public: 104 RSTimeOutDetector(int timeOutThresholdMs, std::string detectorStringId); 105 ~RSTimeOutDetector() = default; 106 void SetParam(const std::string& key, const std::string& value) override; 107 void SetLoopStartTag() override; 108 void SetLoopFinishTag( 109 int32_t focusAppPid, int32_t focusAppUid, 110 std::string& focusAppBundleName, std::string& focusAppAbilityName) override; 111 private: 112 void EventReport(uint64_t costTimeMs); 113 int timeOutThresholdMs_ = INT_MAX; // default: No Detector 114 std::atomic_uint64_t startTimeStampMs_ = 0; 115 }; 116 } 117 } 118 119 #endif // RENDER_SERVICE_BASE_COMMON_RS_DETECTOR_H