1 /*
2 * Copyright (c) 2021 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 <cinttypes>
17 #include <unistd.h>
18 #include "logging.h"
19 #include "share_memory_allocator.h"
20 #include "stack_writer.h"
21
StackWriter(std::string name,uint32_t size,int smbFd,int eventFd,bool blocked)22 StackWriter::StackWriter(std::string name,
23 uint32_t size,
24 int smbFd,
25 int eventFd,
26 bool blocked)
27 : pluginName_(name), blocked_(blocked)
28 {
29 HILOG_INFO(LOG_CORE, "%s:%s %d [%d] [%d]", __func__, name.c_str(), size, smbFd, eventFd);
30 shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
31 if (shareMemoryBlock_ == nullptr) {
32 HILOG_DEBUG(LOG_CORE, "%s:create shareMemoryBlock_ failed!", __func__);
33 }
34 eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
35 lastFlushTime_ = std::chrono::steady_clock::now();
36 }
37
~StackWriter()38 StackWriter::~StackWriter()
39 {
40 HILOG_DEBUG(LOG_CORE, "%s:destroy eventfd = %d!", __func__, eventNotifier_ ? eventNotifier_->GetFd() : -1);
41 eventNotifier_ = nullptr;
42 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
43 shareMemoryBlock_ = nullptr;
44 }
45
Report() const46 void StackWriter::Report() const
47 {
48 HILOG_DEBUG(LOG_CORE, "%s:stats B: %" PRIu64 ", P: %d, W:%" PRIu64 ", F: %d", __func__,
49 bytesCount_.load(), bytesPending_.load(), writeCount_.load(), flushCount_.load());
50 }
51
DoStats(long bytes)52 void StackWriter::DoStats(long bytes)
53 {
54 ++writeCount_;
55 bytesCount_ += bytes;
56 bytesPending_ += bytes;
57 }
58
Write(const void * data,size_t size)59 long StackWriter::Write(const void* data, size_t size)
60 {
61 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
62 return false;
63 }
64 return shareMemoryBlock_->PutRaw(reinterpret_cast<const int8_t*>(data), size);
65 }
66
WriteTimeout(const void * data,size_t size)67 long StackWriter::WriteTimeout(const void* data, size_t size)
68 {
69 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
70 return false;
71 }
72 return shareMemoryBlock_->PutRawTimeout(reinterpret_cast<const int8_t*>(data), size);
73 }
74
WriteWithPayloadTimeout(const void * data,size_t size,const void * payload,size_t payloadSize)75 long StackWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize)
76 {
77 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
78 return false;
79 }
80 if (blocked_) {
81 return shareMemoryBlock_->PutWithPayloadSync(
82 reinterpret_cast<const int8_t*>(data), size, reinterpret_cast<const int8_t*>(payload), payloadSize);
83 } else {
84 return shareMemoryBlock_->PutWithPayloadTimeout(
85 reinterpret_cast<const int8_t*>(data), size, reinterpret_cast<const int8_t*>(payload), payloadSize);
86 }
87 }
88
Flush()89 bool StackWriter::Flush()
90 {
91 ++flushCount_;
92 eventNotifier_->Post(flushCount_.load());
93 lastFlushTime_ = std::chrono::steady_clock::now();
94 bytesPending_ = 0;
95 return true;
96 }
97
Clear()98 bool StackWriter::Clear()
99 {
100 if (shareMemoryBlock_ == nullptr) {
101 return false;
102 }
103 shareMemoryBlock_->ClearShareMemoryBlock();
104 return true;
105 }
106