• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "export_db_manager.h"
17 
18 #include "hiview_logger.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 DEFINE_LOG_TAG("HiView-ExportDbManager");
23 namespace {
GetExportDetailRecord(std::shared_ptr<ExportDbStorage> storage,const std::string & moduleName)24 ExportDetailRecord GetExportDetailRecord(std::shared_ptr<ExportDbStorage> storage, const std::string& moduleName)
25 {
26     ExportDetailRecord record;
27     storage->QueryExportDetailRecord(moduleName, record);
28     return record;
29 }
30 }
31 
GetEventInheritFlagPath()32 std::string ExportDbManager::GetEventInheritFlagPath()
33 {
34     // create event inherit flag file in same level with db file
35     return dbStoreDir_ + "event_inherit_flag";
36 }
37 
GetExportEnabledSeq(const std::string & moduleName)38 int64_t ExportDbManager::GetExportEnabledSeq(const std::string& moduleName)
39 {
40     std::unique_lock<ffrt::mutex> lock(dbMutex_);
41     auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
42     ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
43     if (record.moduleName.empty()) {
44         HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
45         return INVALID_SEQ_VAL;
46     }
47     HIVIEW_LOGD("export enabled sequence is %{public}" PRId64 "", record.exportEnabledSeq);
48     return record.exportEnabledSeq;
49 }
50 
GetExportBeginSeq(const std::string & moduleName)51 int64_t ExportDbManager::GetExportBeginSeq(const std::string& moduleName)
52 {
53     HIVIEW_LOGD("get beginning sequence of event for module %{public}s to export", moduleName.c_str());
54     std::unique_lock<ffrt::mutex> lock(dbMutex_);
55     auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
56     ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
57     if (record.exportEnabledSeq == INVALID_SEQ_VAL) {
58         HIVIEW_LOGI("export switch of %{public}s is off, no need to export event", moduleName.c_str());
59         return INVALID_SEQ_VAL;
60     }
61     return std::max(record.exportEnabledSeq, record.exportedMaxSeq);
62 }
63 
HandleExportSwitchChanged(const std::string & moduleName,int64_t curSeq)64 void ExportDbManager::HandleExportSwitchChanged(const std::string& moduleName, int64_t curSeq)
65 {
66     HIVIEW_LOGI("export switch for %{public}s module is changed, current event sequence is %{public}" PRId64 "",
67         moduleName.c_str(), curSeq);
68     std::unique_lock<ffrt::mutex> lock(dbMutex_);
69     auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
70     if (GetExportDetailRecord(storage, moduleName).moduleName.empty()) {
71         HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
72         ExportDetailRecord record = {
73             .moduleName = moduleName,
74             .exportEnabledSeq = curSeq,
75             .exportedMaxSeq = INVALID_SEQ_VAL,
76         };
77         storage->InsertExportDetailRecord(record);
78         return;
79     }
80     ExportDetailRecord record {
81         .moduleName = moduleName,
82         .exportEnabledSeq = curSeq,
83     };
84     storage->UpdateExportEnabledSeq(record);
85 }
86 
HandleExportTaskFinished(const std::string & moduleName,int64_t eventSeq)87 void ExportDbManager::HandleExportTaskFinished(const std::string& moduleName, int64_t eventSeq)
88 {
89     HIVIEW_LOGI("export task of %{public}s module is finished, maximum event sequence is %{public}" PRId64 "",
90         moduleName.c_str(), eventSeq);
91     std::unique_lock<ffrt::mutex> lock(dbMutex_);
92     auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
93     if (GetExportDetailRecord(storage, moduleName).moduleName.empty()) {
94         HIVIEW_LOGW("no export details record found of %{public}s module in db", moduleName.c_str());
95         ExportDetailRecord record = {
96             .moduleName = moduleName,
97             .exportEnabledSeq = INVALID_SEQ_VAL,
98             .exportedMaxSeq = eventSeq,
99         };
100         storage->InsertExportDetailRecord(record);
101         return;
102     }
103     ExportDetailRecord record {
104         .moduleName = moduleName,
105         .exportedMaxSeq = eventSeq,
106     };
107     storage->UpdateExportedMaxSeq(record);
108 }
109 
IsUnrecordedModule(const std::string & moduleName)110 bool ExportDbManager::IsUnrecordedModule(const std::string& moduleName)
111 {
112     std::unique_lock<ffrt::mutex> lock(dbMutex_);
113     auto storage = std::make_shared<ExportDbStorage>(dbStoreDir_);
114     ExportDetailRecord record = GetExportDetailRecord(storage, moduleName);
115     return record.moduleName.empty();
116 }
117 } // HiviewDFX
118 } // OHOS