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 FAULT_DETECTOR_MANAGER_H
16 #define FAULT_DETECTOR_MANAGER_H
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "plugin.h"
23 #include "plugin_factory.h"
24 #include "app_event_publisher.h"
25 #include "event_loop.h"
26 #include "event_source.h"
27 #include "fault_common_base.h"
28 #include "ffrt.h"
29 #include "sys_event.h"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 class FaultDetectorManager : public FileDescriptorEventCallback, public AppEventPublisher  {
34 public:
35     bool OnEvent(std::shared_ptr<Event> &event) override;
36     bool ReadyToLoad() override;
37     void OnLoad() override;
38     void OnUnload() override;
39     void OnEventListeningCallback(const Event &msg) override;
40     void AddAppEventHandler(std::shared_ptr<AppEventHandler> handler) override;
41 #if defined(__HIVIEW_OHOS__)
42     bool OnFileDescriptorEvent(int fd, int type) override;
43     int32_t GetPollFd() override;
44     int32_t GetPollType() override;
45 #endif
46 
47 private:
48     void PrepareFaultDetectorEnv();
49     void InitDetectorTask();
50     void MonitorProcess();
51     void HandleNativeLeakDetection();
52 
53 private:
54     std::vector<DetectorType> detectorList_;
55     static constexpr uint64_t TASK_TIMER_INTERVAL = 5; // 5s
56     static const inline std::string eventNameLowmem = "LOWMEM";
57 
58     std::map<std::string, int> fileMap_;
59     bool isLoopContinue_ { false };
60     ffrt::task_handle processTaskHandle_;
61 };
62 } // namespace HiviewDFX
63 } // namespace OHOS
64 #endif // FAULT_DETECTOR_MANAGER_H
65