1 /*
2  * Copyright (c) 2023 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 "hisysevent_rust_querier.h"
17 
18 #include "hilog/log.h"
19 #include "hisysevent_record_convertor.h"
20 
21 #undef LOG_DOMAIN
22 #define LOG_DOMAIN 0xD002D08
23 
24 #undef LOG_TAG
25 #define LOG_TAG "HISYSEVENT_RUST_QURIER"
26 
27 namespace {
28 using HiSysEventRecordCls = OHOS::HiviewDFX::HiSysEventRecord;
29 using OHOS::HiviewDFX::HiSysEventRecordConvertor;
30 }
31 
HiSysEventRustQuerier(HiSysEventRustQuerierC * querier)32 HiSysEventRustQuerier::HiSysEventRustQuerier(HiSysEventRustQuerierC* querier)
33     : querier_(querier)
34 {
35 }
36 
~HiSysEventRustQuerier()37 HiSysEventRustQuerier::~HiSysEventRustQuerier()
38 {
39     RecycleQuerier(querier_);
40 }
41 
OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)42 void HiSysEventRustQuerier::OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)
43 {
44     if (querier_ == nullptr) {
45         HILOG_ERROR(LOG_CORE, "OnQuery callback is null");
46         return;
47     }
48     if (sysEvents == nullptr || sysEvents->empty()) {
49         querier_->onQueryWrapperCb(querier_->onQueryRustCb, nullptr, 0);
50         return;
51     }
52     size_t size = sysEvents->size();
53     auto records = new(std::nothrow) HiSysEventRecordC[size];
54     if (records == nullptr) {
55         return;
56     }
57     for (size_t i = 0; i < size; i++) {
58         HiSysEventRecordConvertor::InitRecord(records[i]);
59         if (HiSysEventRecordConvertor::ConvertRecord(sysEvents->at(i), records[i]) != 0) {
60             HILOG_ERROR(LOG_CORE, "Failed to convert record, index=%{public}zu, size=%{public}zu",  i, size);
61             HiSysEventRecordConvertor::DeleteRecords(&records, i + 1); // +1 for release the current record
62             return;
63         }
64     }
65     querier_->onQueryWrapperCb(querier_->onQueryRustCb, records, size);
66     HiSysEventRecordConvertor::DeleteRecords(&records, size);
67 }
68 
OnComplete(int32_t reason,int32_t total)69 void HiSysEventRustQuerier::OnComplete(int32_t reason, int32_t total)
70 {
71     if (querier_ == nullptr) {
72         HILOG_ERROR(LOG_CORE, "OnComplete callback is null");
73         return;
74     }
75     querier_->onCompleteWrapperCb(querier_->onCompleteRustCb, reason, total);
76 }
77 
RecycleQuerier(HiSysEventRustQuerierC * querier)78 void HiSysEventRustQuerier::RecycleQuerier(HiSysEventRustQuerierC* querier)
79 {
80     std::lock_guard<std::mutex> lock(querierMutex_);
81     if ((querier == nullptr) || (querier->status == STATUS_MEM_FREED)) {
82         return;
83     }
84     if (querier->status == STATUS_MEM_NEED_RECYCLE) {
85         querier->status = STATUS_MEM_FREED;
86         delete querier;
87         return;
88     }
89     querier->status = STATUS_MEM_NEED_RECYCLE;
90 }