• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HISYSEVENT_RUST_QURIER" };
23 using HiSysEventRecordCls = OHOS::HiviewDFX::HiSysEventRecord;
24 using OHOS::HiviewDFX::HiLog;
25 using OHOS::HiviewDFX::HiSysEventRecordConvertor;
26 }
27 
HiSysEventRustQuerier(HiSysEventRustQuerierC * querier)28 HiSysEventRustQuerier::HiSysEventRustQuerier(HiSysEventRustQuerierC* querier)
29     : querier_(querier)
30 {
31 }
32 
~HiSysEventRustQuerier()33 HiSysEventRustQuerier::~HiSysEventRustQuerier()
34 {
35     RecycleQuerier(querier_);
36 }
37 
OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)38 void HiSysEventRustQuerier::OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)
39 {
40     if (querier_ == nullptr) {
41         HiLog::Error(LABEL, "OnQuery callback is null");
42         return;
43     }
44     if (sysEvents == nullptr || sysEvents->empty()) {
45         querier_->onQueryWrapperCb(querier_->onQueryRustCb, nullptr, 0);
46         return;
47     }
48     size_t size = sysEvents->size();
49     auto records = new(std::nothrow) HiSysEventRecordC[size];
50     for (size_t i = 0; i < size; i++) {
51         HiSysEventRecordConvertor::InitRecord(records[i]);
52         if (HiSysEventRecordConvertor::ConvertRecord(sysEvents->at(i), records[i]) != 0) {
53             HiLog::Error(LABEL, "Failed to covert record, index=%{public}zu, size=%{public}zu",  i, size);
54             HiSysEventRecordConvertor::DeleteRecords(&records, i + 1); // +1 for release the current record
55             return;
56         }
57     }
58     querier_->onQueryWrapperCb(querier_->onQueryRustCb, records, size);
59     HiSysEventRecordConvertor::DeleteRecords(&records, size);
60 }
61 
OnComplete(int32_t reason,int32_t total)62 void HiSysEventRustQuerier::OnComplete(int32_t reason, int32_t total)
63 {
64     if (querier_ == nullptr) {
65         HiLog::Error(LABEL, "OnComplete callback is null");
66         return;
67     }
68     querier_->onCompleteWrapperCb(querier_->onCompleteRustCb, reason, total);
69 }
70 
RecycleQuerier(HiSysEventRustQuerierC * querier)71 void HiSysEventRustQuerier::RecycleQuerier(HiSysEventRustQuerierC* querier)
72 {
73     std::lock_guard<std::mutex> lock(querierMutex_);
74     if ((querier == nullptr) || (querier->status == STATUS_MEM_FREED)) {
75         return;
76     }
77     if (querier->status == STATUS_MEM_NEED_RECYCLE) {
78         querier->status = STATUS_MEM_FREED;
79         delete querier;
80         return;
81     }
82     querier->status = STATUS_MEM_NEED_RECYCLE;
83 }