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 #include "platform/common/rs_system_properties.h"
18 
19 namespace OHOS {
20 namespace Rosen {
~RSEventManager()21 RSEventManager::~RSEventManager()
22 {
23     Clear();
24 }
25 
Clear()26 void RSEventManager::Clear()
27 {
28     eventDetectorList_.clear();
29     eventStateList_.clear();
30     std::map<std::string, std::weak_ptr<RSBaseEventDetector>> tempDetectorList;
31     std::map<std::string, RSEventState> tempStateList;
32     eventDetectorList_.swap(tempDetectorList);
33     eventStateList_.swap(tempStateList);
34     RS_LOGD("RSEventManager::Clear finish");
35 }
36 
DumpDetectorParam(std::shared_ptr<RSBaseEventDetector> detectorPtr,std::string & dumpString)37 void RSEventManager::DumpDetectorParam(std::shared_ptr<RSBaseEventDetector> detectorPtr, std::string& dumpString)
38 {
39     if (detectorPtr == nullptr) {
40         RS_LOGD("RSEventManager::DumpDetectorParam detectorPtr nullptr");
41         return;
42     }
43     const auto& paramList = detectorPtr->GetParamList();
44     for (const auto& item : paramList) {
45         std::string paraName = "rosen.RsDFXEvent." + detectorPtr->GetStringId() +
46             "." + item.first + ": ";
47         dumpString.append(paraName + item.second + "\n");
48     }
49 }
50 
DumpEventIntervalMs(std::shared_ptr<RSBaseEventDetector> detectorPtr,std::string & dumpString)51 void RSEventManager::DumpEventIntervalMs(std::shared_ptr<RSBaseEventDetector> detectorPtr, std::string& dumpString)
52 {
53     if (detectorPtr == nullptr) {
54         RS_LOGD("RSEventManager::DumpEventIntervalMs detectorPtr nullptr");
55         return;
56     }
57     if (eventStateList_.count(detectorPtr->GetStringId()) == 0) {
58         RS_LOGD("RSEventManager::DumpEventIntervalMs detector:%s is not in list",
59             detectorPtr->GetStringId().c_str());
60         return;
61     }
62     std::string paraName = "rosen.RsDFXEvent." + detectorPtr->GetStringId() +
63         ".eventIntervalMs" + ": ";
64     dumpString.append(paraName + std::to_string(eventStateList_[detectorPtr->GetStringId()].eventIntervalMs) + "\n");
65 }
66 
DumpAllEventParam(std::string & dumpString)67 void RSEventManager::DumpAllEventParam(std::string& dumpString)
68 {
69     std::unique_lock<std::mutex> listLock(listMutex_);
70     for (auto& item : eventDetectorList_) {
71         auto detectorPtr = item.second.lock();
72         if (detectorPtr == nullptr) {
73             RS_LOGD("RSEventManager::DumpAllEventParam failed: nullptr");
74             continue;
75         }
76         DumpDetectorParam(detectorPtr, dumpString);
77         DumpEventIntervalMs(detectorPtr, dumpString);
78     }
79 }
80 
UpdateDetectorParam(std::shared_ptr<RSBaseEventDetector> detectorPtr)81 void RSEventManager::UpdateDetectorParam(std::shared_ptr<RSBaseEventDetector> detectorPtr)
82 {
83     if (detectorPtr == nullptr) {
84         RS_LOGD("RSEventManager::UpdateDetectorParam detectorPtr nullptr");
85         return;
86     }
87     const auto& paramList = detectorPtr->GetParamList();
88     for (const auto& item : paramList) {
89         std::string paraName = "rosen.RsDFXEvent." + detectorPtr->GetStringId() +
90             "." + item.first;
91         RS_LOGD("RSEventManager::UpdateDetectorParam paraName: %{public}s", paraName.c_str());
92         detectorPtr->SetParam(item.first, RSSystemProperties::GetRSEventProperty(paraName));
93     }
94 }
95 
UpdateEventIntervalMs(std::shared_ptr<RSBaseEventDetector> detectorPtr)96 void RSEventManager::UpdateEventIntervalMs(std::shared_ptr<RSBaseEventDetector> detectorPtr)
97 {
98     if (detectorPtr == nullptr) {
99         RS_LOGD("RSEventManager::UpdateEventIntervalMs detectorPtr nullptr");
100         return;
101     }
102     if (eventStateList_.count(detectorPtr->GetStringId()) == 0) {
103         RS_LOGD("RSEventManager::UpdateEventIntervalMs detector:%s is not in list",
104             detectorPtr->GetStringId().c_str());
105         return;
106     }
107     std::string paraName = "rosen.RsDFXEvent." + detectorPtr->GetStringId() +
108         ".eventIntervalMs";
109     RS_LOGD("RSEventManager::UpdateEventIntervalMs paraName: %{public}s", paraName.c_str());
110     int valueInt = atoi(RSSystemProperties::GetRSEventProperty(paraName).c_str());
111     if (valueInt <= 0 || valueInt > 1000000) { // 1000000 ms ->1000s
112         RS_LOGD("RSEventManager::UpdateEventIntervalMs detector:%{public}s Invalid Value:%{public}d",
113             detectorPtr->GetStringId().c_str(), valueInt);
114         return;
115     }
116     eventStateList_[detectorPtr->GetStringId()].eventIntervalMs = valueInt;
117     RS_LOGD("RSEventManager::UpdateEventIntervalMs detector:%{public}s eventIntervalMs:%{public}d success",
118         detectorPtr->GetStringId().c_str(), valueInt);
119 }
120 
UpdateParam()121 void RSEventManager::UpdateParam()
122 {
123     updateCount_++;
124     if (updateCount_ < updateThreshold_) {
125         return;
126     }
127     updateCount_ = 0;
128     std::unique_lock<std::mutex> listLock(listMutex_);
129     for (auto& item : eventDetectorList_) {
130         auto detectorPtr = item.second.lock();
131         if (detectorPtr == nullptr) {
132             RS_LOGD("RSEventManager::UpdateParam failed: nullptr");
133             continue;
134         }
135         UpdateDetectorParam(detectorPtr);
136         UpdateEventIntervalMs(detectorPtr);
137     }
138 }
139 
AddEvent(const std::shared_ptr<RSBaseEventDetector> & detectorPtr,int eventIntervalMs)140 void RSEventManager::AddEvent(const std::shared_ptr<RSBaseEventDetector>& detectorPtr, int eventIntervalMs)
141 {
142     if (detectorPtr == nullptr || eventIntervalMs <= 0) {
143         RS_LOGD("RSEventManager::AddEvent detectorPtr nullptr");
144         return;
145     }
146     std::weak_ptr<RSBaseEventDetector> detectorWeakPtr(detectorPtr);
147     {
148         std::unique_lock<std::mutex> listLock(listMutex_);
149         if (eventDetectorList_.count(detectorPtr->GetStringId()) != 0) {
150             RS_LOGD("RSEventManager::AddEvent %{public}s failed ", detectorPtr->GetStringId().c_str());
151             return;
152         }
153         detectorPtr->AddEventReportCallback([this](const RSSysEventMsg& eventMsg) {
154             this->EventReport(eventMsg);
155             });
156         eventDetectorList_[detectorPtr->GetStringId()] = detectorWeakPtr;
157         RSEventState state = {
158             eventIntervalMs,
159             0
160         };
161         eventStateList_[detectorPtr->GetStringId()] = state;
162         RS_LOGD("RSEventManager::AddEvent %{public}s success ", detectorPtr->GetStringId().c_str());
163     }
164 }
165 
RemoveEvent(std::string stringId)166 void RSEventManager::RemoveEvent(std::string stringId)
167 {
168     std::unique_lock<std::mutex> listLock(listMutex_);
169     if (eventDetectorList_.count(stringId) != 0) {
170         RS_LOGD("RSEventManager::RemoveEvent %{public}s failed ", stringId.c_str());
171         return;
172     }
173     eventDetectorList_.erase(stringId);
174     eventStateList_.erase(stringId);
175     RS_LOGD("RSEventManager::RemoveEvent %{public}s success ", stringId.c_str());
176 }
177 
EventReport(const RSSysEventMsg & eventMsg)178 void RSEventManager::EventReport(const RSSysEventMsg& eventMsg)
179 {
180     std::unique_lock<std::mutex> listLock(listMutex_);
181     if (eventStateList_.count(eventMsg.stringId) == 0) {
182         RS_LOGD("RSEventManager::EventReport %{public}s failed ", eventMsg.stringId.c_str());
183         return;
184     }
185     RSEventState& state = eventStateList_[eventMsg.stringId];
186     uint64_t currentTimeMs = RSEventTimer::GetSysTimeMs();
187     if (currentTimeMs > state.prevEventTimeStampMs&&
188     	currentTimeMs - state.prevEventTimeStampMs > static_cast<uint64_t>(state.eventIntervalMs)) {
189             if (eventMsg.pid != -1) {
190                 HiSysEventWrite(
191                     OHOS::HiviewDFX::HiSysEvent::Domain::GRAPHIC,
192                     eventMsg.stringId,
193                     eventMsg.eventType,
194                     "PID", eventMsg.pid,
195                     "UID", eventMsg.uid,
196                     "BUNDLE_NAME", eventMsg.bundleName,
197                     "ABILITY_NAME", eventMsg.abilityName,
198                     "MSG", eventMsg.msg);
199             } else {
200                 HiSysEventWrite(
201                     OHOS::HiviewDFX::HiSysEvent::Domain::GRAPHIC,
202                     eventMsg.stringId,
203                     eventMsg.eventType,
204                     "MSG", eventMsg.msg);
205             }
206             state.prevEventTimeStampMs = currentTimeMs;
207             RS_LOGD("RSEventManager::EventReport %{public}s success ", eventMsg.stringId.c_str());
208     }
209 }
210 }
211 }