1 /* 2 * Copyright (c) 2024 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 #ifndef NATIVE_LEAK_DETECTOR_H 16 #define NATIVE_LEAK_DETECTOR_H 17 18 #include <map> 19 #include <memory> 20 #include <string> 21 #include <unordered_map> 22 23 #include "fault_common_base.h" 24 #include "fault_detector_base.h" 25 #include "fault_info_base.h" 26 #include "fault_state_base.h" 27 #include "ffrt.h" 28 #include "singleton.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 class NativeLeakDetector : public FaultDetectorBase, public Singleton<NativeLeakDetector> { 33 DECLARE_SINGLETON(NativeLeakDetector); 34 35 public: 36 void PrepareNativeLeakEnv(); 37 void ProcessUserEvent(const std::string &name, const std::string &msg, uint32_t pid); 38 ErrCode MainProcess() override; 39 void OnChangeState(std::shared_ptr<FaultInfoBase> &monitorInfo, FaultStateType stateType) override; 40 ErrCode ExeNextStateProcess(std::shared_ptr<FaultInfoBase> monitorInfo, FaultStateType stateType) override; 41 FaultStateBase* GetStateObj(FaultStateType stateType) override; 42 43 private: 44 void NativeLeakConfigParse(); 45 void InitMonitorInfo(); 46 void UpdateUserMonitorInfo(); 47 void RecordNativeInfo(); 48 void RemoveInvalidLeakedPid(); 49 void RemoveInvalidUserInfo(); 50 void UpdateProcessedPidsList(); 51 void RemoveFinishedInfo(int64_t pid); 52 void DoProcessNativeLeak(); 53 bool JudgeNativeLeak(std::shared_ptr<FaultInfoBase> &monitorInfo); 54 void AddMonitorToList(std::shared_ptr<FaultInfoBase> &monitorInfo); 55 56 private: 57 uint32_t loopCnt_ { 0 }; 58 uint32_t funcLoopCnt_ { 0 }; 59 uint32_t sampleInterval_ { 0 }; 60 uint32_t updateInterval_ { 0 }; 61 uint64_t defauleThreshold_ { 0 }; 62 63 ffrt::mutex nativeDetectorMtx_; 64 std::unordered_map<std::string, uint64_t> thresholdLists_; 65 std::map<pid_t, std::shared_ptr<FaultInfoBase>> grayList_; 66 std::map<pid_t, std::string> monitoredPidsList_; 67 }; 68 } // namespace HiviewDFX 69 } // namespace OHOS 70 #endif // NATIVE_LEAK_DETECTOR_H 71