1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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     shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
30     if (shareMemoryBlock_ == nullptr) {
31     }
32     eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
33     lastFlushTime_ = std::chrono::steady_clock::now();
34 }
35 
~StackWriter()36 StackWriter::~StackWriter()
37 {
38     eventNotifier_ = nullptr;
39     ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
40     shareMemoryBlock_ = nullptr;
41 }
42 
Report() const43 void StackWriter::Report() const
44 {
45 }
46 
DoStats(long bytes)47 void StackWriter::DoStats(long bytes)
48 {
49     ++writeCount_;
50     bytesCount_ += bytes;
51     bytesPending_ += bytes;
52 }
53 
Write(const void* data, size_t size)54 long StackWriter::Write(const void* data, size_t size)
55 {
56     if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
57         return false;
58     }
59     return shareMemoryBlock_->PutRaw(reinterpret_cast<const int8_t*>(data), size);
60 }
61 
WriteTimeout(const void* data, size_t size)62 long StackWriter::WriteTimeout(const void* data, size_t size)
63 {
64     if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
65         return false;
66     }
67     return shareMemoryBlock_->PutRawTimeout(reinterpret_cast<const int8_t*>(data), size);
68 }
69 
WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize, const std::function<bool()>& callback)70 long StackWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize,
71                                           const std::function<bool()>& callback)
72 {
73     if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
74         return false;
75     }
76     if (blocked_) {
77         return shareMemoryBlock_->PutWithPayloadSync(
78             reinterpret_cast<const int8_t*>(data),
79             size,
80             reinterpret_cast<const int8_t*>(payload),
81             payloadSize,
82             callback);
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