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 "export_db_manager.h"
17
18 #include "file_util.h"
19 #include "hiview_global.h"
20 #include "hiview_logger.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 DEFINE_LOG_TAG("HiView-EventExportDb");
25 namespace {
GetExportDetailRecord(std::shared_ptr<ExportDbStorage> storage,const std::string & moduleName)26 ExportDetailRecord GetExportDetailRecord(std::shared_ptr<ExportDbStorage> storage, const std::string& moduleName)
27 {
28 ExportDetailRecord record;
29 storage->QueryExportDetailRecord(moduleName, record);
30 return record;
31 }
32
GetExportDbDir()33 std::string GetExportDbDir()
34 {
35 auto& context = HiviewGlobal::GetInstance();
36 if (context == nullptr) {
37 return "";
38 }
39 std::string configDir = context->GetHiViewDirectory(HiviewContext::DirectoryType::WORK_DIRECTORY);
40 return FileUtil::IncludeTrailingPathDelimiter(configDir.append("sys_event_export"));
41 }
42 }
43
GetInstance()44 ExportDbManager& ExportDbManager::GetInstance()
45 {
46 static ExportDbManager instance;
47 return instance;
48 }
49
ExportDbManager()50 ExportDbManager::ExportDbManager()
51 {
52 dbStoreDir_ = GetExportDbDir();
53 }
54
GetEventInheritFlagPath(const std::string & moduleName)55 std::string ExportDbManager::GetEventInheritFlagPath(const std::string& moduleName)
56 {
57 // create event inherit flag file in same level with db file
58 std::string tagName("event_inherit_flag");
59 tagName.append("_").append(moduleName);
60 return dbStoreDir_ + tagName;
61 }
62
GetExportEnabledSeq(const std::string & moduleName)63 int64_t ExportDbManager::GetExportEnabledSeq(const std::string& moduleName)
64 {
65 std::unique_lock<ffrt::mutex> lock(dbMutex_);
66 auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
67 ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
68 if (record.moduleName.empty()) {
69 HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
70 return INVALID_SEQ_VAL;
71 }
72 HIVIEW_LOGD("export enabled sequence is %{public}" PRId64 "", record.exportEnabledSeq);
73 return record.exportEnabledSeq;
74 }
75
GetExportBeginSeq(const std::string & moduleName)76 int64_t ExportDbManager::GetExportBeginSeq(const std::string& moduleName)
77 {
78 HIVIEW_LOGD("get beginning sequence of event for module %{public}s to export", moduleName.c_str());
79 std::unique_lock<ffrt::mutex> lock(dbMutex_);
80 auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
81 ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
82 if (record.exportEnabledSeq == INVALID_SEQ_VAL) {
83 HIVIEW_LOGI("export end sequence is invalid for module: %{public}s", moduleName.c_str());
84 return INVALID_SEQ_VAL;
85 }
86 return std::max(record.exportEnabledSeq, record.exportedMaxSeq);
87 }
88
GetExportEndSeq(const std::string & moduleName)89 int64_t ExportDbManager::GetExportEndSeq(const std::string& moduleName)
90 {
91 std::unique_lock<ffrt::mutex> lock(dbMutex_);
92 auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
93 ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
94 if (record.exportedMaxSeq == INVALID_SEQ_VAL) {
95 HIVIEW_LOGI("export switch of %{public}s is off, no need to export event", moduleName.c_str());
96 }
97 return record.exportedMaxSeq;
98 }
99
HandleExportSwitchChanged(const std::string & moduleName,int64_t curSeq)100 void ExportDbManager::HandleExportSwitchChanged(const std::string& moduleName, int64_t curSeq)
101 {
102 HIVIEW_LOGI("export switch for %{public}s module is changed, current event sequence is %{public}" PRId64 "",
103 moduleName.c_str(), curSeq);
104 std::unique_lock<ffrt::mutex> lock(dbMutex_);
105 auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
106 if (GetExportDetailRecord(storage, moduleName).moduleName.empty()) {
107 HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
108 ExportDetailRecord record = {
109 .moduleName = moduleName,
110 .exportEnabledSeq = curSeq,
111 .exportedMaxSeq = INVALID_SEQ_VAL,
112 };
113 storage->InsertExportDetailRecord(record);
114 return;
115 }
116 ExportDetailRecord record {
117 .moduleName = moduleName,
118 .exportEnabledSeq = curSeq,
119 };
120 storage->UpdateExportEnabledSeq(record);
121 }
122
HandleExportTaskFinished(const std::string & moduleName,int64_t eventSeq)123 void ExportDbManager::HandleExportTaskFinished(const std::string& moduleName, int64_t eventSeq)
124 {
125 HIVIEW_LOGI("export task of %{public}s module is finished, maximum event sequence is %{public}" PRId64 "",
126 moduleName.c_str(), eventSeq);
127 std::unique_lock<ffrt::mutex> lock(dbMutex_);
128 auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
129 if (GetExportDetailRecord(storage, moduleName).moduleName.empty()) {
130 HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
131 ExportDetailRecord record = {
132 .moduleName = moduleName,
133 .exportEnabledSeq = INVALID_SEQ_VAL,
134 .exportedMaxSeq = eventSeq,
135 };
136 storage->InsertExportDetailRecord(record);
137 return;
138 }
139 ExportDetailRecord record {
140 .moduleName = moduleName,
141 .exportedMaxSeq = eventSeq,
142 };
143 storage->UpdateExportedMaxSeq(record);
144 }
145
IsUnrecordedModule(const std::string & moduleName)146 bool ExportDbManager::IsUnrecordedModule(const std::string& moduleName)
147 {
148 std::unique_lock<ffrt::mutex> lock(dbMutex_);
149 auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
150 ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
151 return record.moduleName.empty();
152 }
153 } // HiviewDFX
154 } // OHOS