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 %d ", timeOutThresholdMs);
38 timeOutThresholdMs_ = timeOutThresholdMs;
39 paramList_["timeOutThresholdMs"] = std::to_string(timeOutThresholdMs_);
40 }
41
42
SetParam(const std::string & key,const std::string & value)43 void RSTimeOutDetector::SetParam(const std::string& key, const std::string& value)
44 {
45 if (paramList_.count(key) == 0) {
46 RS_LOGD("RSTimeOutDetector :: SetParam Invaild Key ");
47 return;
48 }
49 int valueInt = atoi(value.c_str());
50 if (valueInt <= 0 || valueInt > 1000000) { // 1000000Ms->1000s
51 RS_LOGD("RSTimeOutDetector :: SetParam Invaild Value ");
52 return;
53 }
54 timeOutThresholdMs_ = valueInt;
55 paramList_[key] = value;
56 }
57
58
SetLoopStartTag()59 void RSTimeOutDetector::SetLoopStartTag()
60 {
61 startTimeStampMs_ = RSEventTimer::GetSysTimeMs();
62 }
63
SetLoopFinishTag(int32_t focusAppPid,int32_t focusAppUid,std::string & focusAppBundleName,std::string & focusAppAbilityName)64 void RSTimeOutDetector::SetLoopFinishTag(
65 int32_t focusAppPid, int32_t focusAppUid, std::string& focusAppBundleName, std::string& focusAppAbilityName)
66 {
67 uint64_t finishTimeStampMs = RSEventTimer::GetSysTimeMs();
68 RS_LOGD("RSTimeOutDetector :: One loop cost Time: %" PRIu64 " ", finishTimeStampMs - startTimeStampMs_);
69 if (finishTimeStampMs > startTimeStampMs_) {
70 auto durationStampMs = finishTimeStampMs - startTimeStampMs_;
71 if (durationStampMs > static_cast<uint64_t>(timeOutThresholdMs_)) {
72 focusAppPid_ = focusAppPid;
73 focusAppUid_ = focusAppUid;
74 focusAppBundleName_ = focusAppBundleName;
75 focusAppAbilityName_ = focusAppAbilityName;
76 EventReport(durationStampMs);
77 }
78 }
79 }
80
EventReport(uint64_t costTimeMs)81 void RSTimeOutDetector::EventReport(uint64_t costTimeMs)
82 {
83 std::string msg = "RS TimeOut: one loop cost " + std::to_string(costTimeMs) + "ms";
84 RSSysEventMsg eventMsg = {
85 stringId_,
86 msg,
87 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
88 focusAppPid_,
89 focusAppUid_,
90 focusAppBundleName_,
91 focusAppAbilityName_
92 };
93 if (focusAppPid_ != -1) {
94 eventMsg.pid = focusAppPid_;
95 eventMsg.uid = focusAppUid_;
96 eventMsg.bundleName = focusAppBundleName_;
97 eventMsg.abilityName = focusAppAbilityName_;
98 RS_LOGD("RSTimeOutDetector::EventReport focusApp: %d, %d, %s, %s",
99 eventMsg.pid, eventMsg.uid, eventMsg.bundleName.c_str(), eventMsg.abilityName.c_str());
100 }
101 if (eventCallback_ != nullptr) {
102 eventCallback_(eventMsg);
103 }
104 }
105 }
106 }