• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
16 #define HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
17 
18 #include <ctime>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <mutex>
24 
25 #include "event.h"
26 #include "event_loop.h"
27 #include "log_store_ex.h"
28 #include "logger.h"
29 #include "plugin.h"
30 #include "sys_event.h"
31 
32 #include "event_logger_config.h"
33 #include "event_thread_pool.h"
34 namespace OHOS {
35 namespace HiviewDFX {
36 struct BinderInfo {
37     int client;
38     int server;
39     unsigned long wait;
40 };
41 
42 class EventLogger : public Plugin, public FileDescriptorEventCallback  {
43 public:
EventLogger()44     EventLogger() : logStore_(std::make_unique<LogStoreEx>(LOGGER_EVENT_LOG_PATH, true)),
45         startTime_(time(nullptr)),
46         inotifyFd_(0) {};
~EventLogger()47     ~EventLogger() {};
48     bool OnEvent(std::shared_ptr<Event> &event) override;
49     void OnLoad() override;
50     void OnUnload() override;
51     bool IsInterestedPipelineEvent(std::shared_ptr<Event> event) override;
52     bool OnFileDescriptorEvent(int fd, int type) override;
53     int32_t GetPollFd() override;
54     int32_t GetPollType() override;
55 private:
56     static const inline std::string LOGGER_EVENT_LOG_PATH = "/data/log/eventlog";
57     static const inline std::string MONITOR_STACK_LOG_PATH = "/data/log/faultlog/temp";
58     static const inline std::string MONITOR_STACK_FLIE_NAME[] = {
59         "jsstack",
60     };
61     static const inline std::string MONITOR_LOG_PATH[] = {
62         MONITOR_STACK_LOG_PATH,
63     };
64     static constexpr int EVENT_MAX_ID = 1000000;
65     static constexpr int MAX_FILE_NUM = 500;
66     static constexpr int MAX_FOLDER_SIZE = 50 * 1024 * 1024;
67 
68     std::unique_ptr<LogStoreEx> logStore_;
69     uint64_t startTime_;
70     std::unordered_map<std::string, std::time_t> eventTagTime_;
71     int inotifyFd_;
72     std::unordered_map<int, std::string> fileMap_;
73     std::unordered_map<std::string, EventLoggerConfig::EventLoggerConfigData> eventLoggerConfig_;
74     std::unordered_set<std::string> hitraceSet_;
75     std::shared_ptr<EventLoop> threadLoop_ = nullptr;
76     std::unordered_set<std::shared_ptr<SysEvent>> sysEventSet_;
77     std::mutex hitraceSetMutex_;
78     int const maxEventPoolCount = 5;
79     std::unique_ptr<EventThreadPool> eventPool_;
80     std::mutex intervalMutex_;
81     std::mutex finishMutex_;
82 
83     void StartLogCollect(std::shared_ptr<SysEvent> event);
84     int Getfile(std::shared_ptr<SysEvent> event, std::string& logFile);
85     bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event);
86     bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event);
87     bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile);
88     void CreateAndPublishEvent(std::string& dirPath, std::string& fileName);
89     bool HitraceCatcher(int64_t& beginTime, const std::string& hitraceTime,
90         const std::string& fullTracePath, std::shared_ptr<SysEvent> event);
91     std::string GetHitraceName(int64_t& beginTime, std::string& hitraceTime);
92     bool DetectionHiTraceMap(const std::string& name);
93     bool IsHandleAppfreeze(std::shared_ptr<SysEvent> event);
94     void CheckEventOnContinue();
95 };
96 } // namespace HiviewDFX
97 } // namespace OHOS
98 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H
99