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 #include "platform/common/rs_event_manager.h"
17 
18 namespace OHOS {
19 namespace Rosen {
GetSysTimeMs()20 uint64_t RSEventTimer::GetSysTimeMs()
21 {
22     auto now = std::chrono::steady_clock::now().time_since_epoch();
23     return std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
24 }
25 
CreateRSTimeOutDetector(int timeOutThresholdMs,std::string detectorStringId)26 std::shared_ptr<RSBaseEventDetector> RSBaseEventDetector::CreateRSTimeOutDetector(int timeOutThresholdMs,
27     std::string detectorStringId)
28 {
29     std::shared_ptr<RSTimeOutDetector> eventPtr = std::make_shared<RSTimeOutDetector>(timeOutThresholdMs,
30     detectorStringId);
31     return  eventPtr;
32 }
33 
RSTimeOutDetector(int timeOutThresholdMs,std::string detectorStringId)34 RSTimeOutDetector::RSTimeOutDetector(int timeOutThresholdMs,
35     std::string detectorStringId) :RSBaseEventDetector(detectorStringId)
36 {
37     RS_LOGD("RSTimeOutDetector ::RSTimeOutDetector timeOutThresholdMs is %{public}d ", timeOutThresholdMs);
38     timeOutThresholdMs_ = timeOutThresholdMs;
39     paramList_["timeOutThresholdMs"] = std::to_string(timeOutThresholdMs_);
40 }
41 
SetParam(const std::string & key,const std::string & value)42 void RSTimeOutDetector::SetParam(const std::string& key, const std::string& value)
43 {
44     if (paramList_.count(key) == 0) {
45         RS_LOGD("RSTimeOutDetector :: SetParam Invalid Key ");
46         return;
47     }
48     int valueInt = atoi(value.c_str());
49     if (valueInt <= 0 || valueInt > 1000000) { // 1000000Ms->1000s
50         RS_LOGD("RSTimeOutDetector :: SetParam Invalid Value ");
51         return;
52     }
53     timeOutThresholdMs_ = valueInt;
54     paramList_[key] = value;
55 }
56 
SetLoopStartTag()57 void RSTimeOutDetector::SetLoopStartTag()
58 {
59     startTimeStampMs_ = RSEventTimer::GetSysTimeMs();
60 }
61 
SetLoopFinishTag(int32_t focusAppPid,int32_t focusAppUid,std::string & focusAppBundleName,std::string & focusAppAbilityName)62 void RSTimeOutDetector::SetLoopFinishTag(
63     int32_t focusAppPid, int32_t focusAppUid, std::string& focusAppBundleName, std::string& focusAppAbilityName)
64 {
65     uint64_t finishTimeStampMs = RSEventTimer::GetSysTimeMs();
66     if (finishTimeStampMs > startTimeStampMs_) {
67         auto durationStampMs = finishTimeStampMs - startTimeStampMs_;
68         if (durationStampMs > static_cast<uint64_t>(timeOutThresholdMs_)) {
69             focusAppPid_ = focusAppPid;
70             focusAppUid_ = focusAppUid;
71             focusAppBundleName_ = focusAppBundleName;
72             focusAppAbilityName_ = focusAppAbilityName;
73             EventReport(durationStampMs);
74         }
75     }
76 }
77 
EventReport(uint64_t costTimeMs)78 void RSTimeOutDetector::EventReport(uint64_t costTimeMs)
79 {
80     std::string msg = "RS TimeOut: one loop cost " + std::to_string(costTimeMs) + "ms";
81     RSSysEventMsg eventMsg = {
82         stringId_,
83         msg,
84         OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
85         focusAppPid_,
86         focusAppUid_,
87         focusAppBundleName_,
88         focusAppAbilityName_
89     };
90     if (eventCallback_ != nullptr) {
91         eventCallback_(eventMsg);
92     }
93 }
94 }
95 }