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 namespace {
22 const int WAIT_TIMEOUT_SA = 3000000;
23 const int FLUSH_FLAG = 20;
24 }
StackWriter(std::string name,uint32_t size,int smbFd,int eventFd,bool blocked,bool isSaMode)25 StackWriter::StackWriter(std::string name,
26 uint32_t size,
27 int smbFd,
28 int eventFd,
29 bool blocked,
30 bool isSaMode)
31 : pluginName_(name), blocked_(blocked)
32 {
33 shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
34 if (isSaMode && shareMemoryBlock_ != nullptr) {
35 shareMemoryBlock_->SetWaitTime(WAIT_TIMEOUT_SA);
36 }
37 eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
38 lastFlushTime_ = std::chrono::steady_clock::now();
39 }
40
~StackWriter()41 StackWriter::~StackWriter()
42 {
43 eventNotifier_ = nullptr;
44 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
45 shareMemoryBlock_ = nullptr;
46 }
47
Report() const48 void StackWriter::Report() const
49 {
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,const std::function<bool ()> & callback)75 long StackWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize,
76 const std::function<bool()>& callback)
77 {
78 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
79 return false;
80 }
81 if (blocked_) {
82 return shareMemoryBlock_->PutWithPayloadSync(
83 reinterpret_cast<const int8_t*>(data),
84 size,
85 reinterpret_cast<const int8_t*>(payload),
86 payloadSize,
87 callback);
88 } else {
89 return shareMemoryBlock_->PutWithPayloadTimeout(
90 reinterpret_cast<const int8_t*>(data), size, reinterpret_cast<const int8_t*>(payload), payloadSize);
91 }
92 }
93
PrepareFlush()94 bool StackWriter::PrepareFlush()
95 {
96 ++dataCount_;
97 return (dataCount_ % FLUSH_FLAG == 0);
98 }
99
Flush()100 bool StackWriter::Flush()
101 {
102 ++flushCount_;
103 eventNotifier_->Post(flushCount_.load());
104 lastFlushTime_ = std::chrono::steady_clock::now();
105 bytesPending_ = 0;
106 return true;
107 }
108