1 /*
2 * Copyright (c) 2024-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 #include "event_export_task.h"
17
18 #include "event_export_util.h"
19 #include "event_json_parser.h"
20 #include "export_db_manager.h"
21 #include "file_util.h"
22 #include "hiview_logger.h"
23 #include "setting_observer_manager.h"
24 #include "sys_event_sequence_mgr.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("HiView-EventExportFlow");
29 using ExportEventListParsers = std::map<std::string, std::shared_ptr<ExportEventListParser>>;
30 namespace {
31 constexpr int64_t BYTE_TO_MB = 1024 * 1024;
32
GetParser(ExportEventListParsers & parsers,const std::string & path)33 std::shared_ptr<ExportEventListParser> GetParser(ExportEventListParsers& parsers,
34 const std::string& path)
35 {
36 auto iter = parsers.find(path);
37 if (iter == parsers.end()) {
38 parsers.emplace(path, std::make_shared<ExportEventListParser>(path));
39 return parsers[path];
40 }
41 return iter->second;
42 }
43
GetModuleExportStartSeq(std::shared_ptr<ExportConfig> config)44 int64_t GetModuleExportStartSeq(std::shared_ptr<ExportConfig> config)
45 {
46 auto& dbMgr = ExportDbManager::GetInstance();
47 int64_t startSeq = EventStore::SysEventSequenceManager::GetInstance().GetStartSequence();
48 HIVIEW_LOGI("start sequence is %{public}" PRId64 "", startSeq);
49 if (config == nullptr || !dbMgr.IsUnrecordedModule(config->moduleName) ||
50 config->inheritedModule.empty() || dbMgr.IsUnrecordedModule(config->inheritedModule)) {
51 return startSeq;
52 }
53 return dbMgr.GetExportEndSeq(config->inheritedModule);
54 }
55
IsExportSwitchOff(std::shared_ptr<ExportConfig> config)56 bool IsExportSwitchOff(std::shared_ptr<ExportConfig> config)
57 {
58 bool isSwitchOff = (SettingObserverManager::GetInstance()->GetStringValue(config->exportSwitchParam.name) !=
59 config->exportSwitchParam.enabledVal);
60 auto& dbMgr = ExportDbManager::GetInstance();
61 if (isSwitchOff) {
62 HIVIEW_LOGI("export switch for module %{public}s is off", config->moduleName.c_str());
63 int64_t enabledSeq = dbMgr.GetExportEnabledSeq(config->moduleName);
64 if (enabledSeq != INVALID_SEQ_VAL &&
65 !FileUtil::FileExists(dbMgr.GetEventInheritFlagPath(config->moduleName))) {
66 dbMgr.HandleExportSwitchChanged(config->moduleName, INVALID_SEQ_VAL);
67 }
68 return true;
69 }
70 HIVIEW_LOGI("export switch for module %{public}s is on", config->moduleName.c_str());
71 int64_t enabledSeq = dbMgr.GetExportEnabledSeq(config->moduleName);
72 if (enabledSeq == INVALID_SEQ_VAL) {
73 enabledSeq = GetModuleExportStartSeq(config);
74 dbMgr.HandleExportSwitchChanged(config->moduleName, enabledSeq);
75 }
76 return false;
77 }
78 }
79
OnTaskRun()80 void EventExportTask::OnTaskRun()
81 {
82 if (config_ == nullptr) {
83 HIVIEW_LOGE("export config is invalid");
84 return;
85 }
86 if (IsExportSwitchOff(config_)) {
87 return;
88 }
89 if (FileUtil::GetFolderSize(config_->exportDir) >= static_cast<uint64_t>(config_->maxCapcity * BYTE_TO_MB)) {
90 HIVIEW_LOGE("event export directory is full");
91 return;
92 }
93 // init handler request
94 auto readReq = std::make_shared<EventReadRequest>();
95 if (!InitReadRequest(readReq)) {
96 HIVIEW_LOGE("failed to init read request");
97 return;
98 }
99 // init write handler
100 auto writeHandler = std::make_shared<EventWriteHandler>();
101 // init read handler
102 auto readHandler = std::make_shared<EventReadHandler>();
103 readHandler->SetEventExportedListener([this] (int64_t beginSeq, int64_t endSeq) {
104 HIVIEW_LOGW("finished exporting events in range [%{public}" PRId64 ", %{public}" PRId64 ")",
105 beginSeq, endSeq);
106 // sync export progress to db
107 ExportDbManager::GetInstance().HandleExportTaskFinished(config_->moduleName, endSeq);
108 });
109 // init handler chain
110 readHandler->SetNextHandler(writeHandler);
111 // start handler chain
112 if (!readHandler->HandleRequest(readReq)) {
113 HIVIEW_LOGE("failed to export all events in range [%{public}" PRId64 ",%{public}" PRId64 ")",
114 readReq->beginSeq, readReq->endSeq);
115 return;
116 }
117 // record export progress
118 HIVIEW_LOGI("succeed to export all events in range [%{public}" PRId64 ",%{public}" PRId64 ")", readReq->beginSeq,
119 readReq->endSeq);
120 }
121
ParseExportEventList(ExportEventList & list) const122 bool EventExportTask::ParseExportEventList(ExportEventList& list) const
123 {
124 if (config_->eventsConfigFiles.empty()) {
125 // if export event list file isn't configured, use export info configured in hisysevent.def
126 EventJsonParser::GetInstance()->GetAllCollectEvents(list);
127 return true;
128 }
129 ExportEventListParsers parsers;
130 auto iter = std::max_element(config_->eventsConfigFiles.begin(), config_->eventsConfigFiles.end(),
131 [&parsers] (const std::string& path1, const std::string& path2) {
132 auto parser1 = GetParser(parsers, path1);
133 auto parser2 = GetParser(parsers, path2);
134 return parser1->GetConfigurationVersion() < parser2->GetConfigurationVersion();
135 });
136 if (iter == config_->eventsConfigFiles.end()) {
137 HIVIEW_LOGE("no event list file path is configured.");
138 return false;
139 }
140 HIVIEW_LOGD("event list file path is %{public}s", (*iter).c_str());
141 auto parser = GetParser(parsers, *iter);
142 parser->GetExportEventList(list);
143 return true;
144 }
145
InitReadRequest(std::shared_ptr<EventReadRequest> readReq) const146 bool EventExportTask::InitReadRequest(std::shared_ptr<EventReadRequest> readReq) const
147 {
148 if (readReq == nullptr) {
149 return false;
150 }
151 readReq->beginSeq = ExportDbManager::GetInstance().GetExportBeginSeq(config_->moduleName);
152 if (readReq->beginSeq == INVALID_SEQ_VAL) {
153 HIVIEW_LOGE("invalid export: begin sequence:%{public}" PRId64 "", readReq->beginSeq);
154 return false;
155 }
156 readReq->endSeq = EventStore::SysEventSequenceManager::GetInstance().GetSequence();
157 if (readReq->beginSeq >= readReq->endSeq) {
158 HIVIEW_LOGE("invalid export range: [%{public}" PRId64 ",%{public}" PRId64 ")",
159 readReq->beginSeq, readReq->endSeq);
160 return false;
161 }
162 if (!ParseExportEventList(readReq->eventList) || readReq->eventList.empty()) {
163 HIVIEW_LOGE("failed to get a valid event export list");
164 return false;
165 }
166 readReq->moduleName = config_->moduleName;
167 readReq->maxSize = config_->maxSize;
168 readReq->exportDir = config_->exportDir;
169 readReq->taskType = config_->taskType;
170 return true;
171 }
172 } // HiviewDFX
173 } // OHOS
174