1 /* 2 * Copyright (c) 2021-2025 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 #ifndef SYS_EVENT_SOURCE_H 17 #define SYS_EVENT_SOURCE_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <list> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "event_json_parser.h" 27 #include "event_server.h" 28 #include "event_source.h" 29 #include "i_controller.h" 30 #include "pipeline.h" 31 #include "platform_monitor.h" 32 #include "base/raw_data.h" 33 #include "period_file_operator.h" 34 #include "sys_event_service_adapter.h" 35 #include "sys_event_stat.h" 36 37 namespace OHOS { 38 namespace HiviewDFX { 39 class SysEventSource; 40 constexpr uint64_t DEFAULT_PERIOD_SEQ = 1; // period seq begins with 1 41 class SysEventReceiver : public EventReceiver { 42 public: SysEventReceiver(SysEventSource & source)43 explicit SysEventReceiver(SysEventSource& source): eventSource(source) {}; ~SysEventReceiver()44 ~SysEventReceiver() override {}; 45 void HandlerEvent(std::shared_ptr<EventRaw::RawData> rawData) override; 46 private: 47 SysEventSource& eventSource; 48 }; 49 50 struct SourcePeriodInfo { 51 // format: YYYYMMDDHH 52 std::string timeStamp; 53 54 // count of event which will be preserve into db file in 1 hour 55 uint64_t preserveCnt = 0; 56 57 // count of event which will be exported in 1 hour 58 uint64_t exportCnt = 0; 59 SourcePeriodInfoSourcePeriodInfo60 SourcePeriodInfo(const std::string& timeStamp, uint64_t preserveCnt, uint64_t exportCnt) 61 : timeStamp(timeStamp), preserveCnt(preserveCnt), exportCnt(exportCnt) {} 62 }; 63 64 class SysEventSource : public EventSource, public SysEventServiceBase { 65 public: 66 void OnLoad() override; 67 void OnUnload() override; 68 void StartEventSource() override; 69 void Recycle(PipelineEvent *event) override; 70 void PauseDispatch(std::weak_ptr<Plugin> plugin) override; 71 bool CheckEvent(std::shared_ptr<Event> event); 72 bool PublishPipelineEvent(std::shared_ptr<PipelineEvent> event); 73 void Dump(int fd, const std::vector<std::string>& cmds) override; 74 void OnConfigUpdate(const std::string& localCfgPath, const std::string& cloudCfgPath) override; 75 void UpdateTestType(const std::string& testType); 76 77 private: 78 void InitController(); 79 bool IsValidSysEvent(const std::shared_ptr<SysEvent> event); 80 std::shared_ptr<SysEvent> Convert2SysEvent(std::shared_ptr<Event>& event); 81 void DecorateSysEvent(const std::shared_ptr<SysEvent> event, const BaseInfo& info, uint64_t id); 82 bool IsDuplicateEvent(const uint64_t eventId); 83 std::string GetEventExportConfigFilePath(); 84 void StatisticSourcePeriodInfo(const std::shared_ptr<SysEvent> event); 85 void RecordSourcePeriodInfo(); 86 87 private: 88 EventServer eventServer_; 89 PlatformMonitor platformMonitor_; 90 std::unique_ptr<SysEventStat> sysEventStat_ = nullptr; 91 std::shared_ptr<IController> controller_; 92 std::atomic<bool> isConfigUpdated_ { false }; 93 std::string testType_; 94 std::list<uint64_t> eventIdList_; 95 std::list<std::shared_ptr<SourcePeriodInfo>> periodInfoList_; 96 uint64_t periodSeq_ = DEFAULT_PERIOD_SEQ; 97 bool isLastEventDelayed_ = false; 98 std::unique_ptr<PeriodInfoFileOperator> periodFileOpt_; 99 }; 100 } // namespace HiviewDFX 101 } // namespace OHOS 102 #endif // SYS_EVENT_SOURCE_H